home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / graph.zip / GTEST1.C < prev    next >
Text File  |  1993-10-04  |  8KB  |  287 lines

  1. #define  INCL_DOS
  2. #define  INCL_WIN
  3. #define  INCL_GPI
  4. #include <stdlib.h>
  5. #include <os2.h>
  6. #include <graphics.h>
  7. #include "math.h"
  8.  
  9. /*
  10. ** Local defines
  11. */
  12. #define DATA_ARRAY_SIZE 100
  13.  
  14. /*
  15. ** Local Function Prototypes
  16. */
  17. MRESULT EXPENTRY TestWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  18. HGRAPH InitTest(HWND hwnd);
  19.  
  20. /*
  21. ** Global variables
  22. */
  23. static HWND hwndMenu = (HWND)0;
  24. static char *szTestClass = "GraphTest";
  25.  
  26. /*
  27. ** Main Program
  28. */
  29.  
  30. int main(int argc, char **argv)
  31. {
  32.    HAB   hab;
  33.    HMQ   hmq;
  34.    QMSG  qmsg;
  35.    HWND  hwndFrame, hwndClient;
  36.    ULONG flCreateFlags = FCF_SYSMENU | FCF_TITLEBAR | FCF_SIZEBORDER |
  37.                          FCF_MINMAX  | FCF_SHELLPOSITION |
  38.                          FCF_TASKLIST;
  39.  
  40.    /* Initialize into PM Session */
  41.    if(!(hab = WinInitialize(0))) {
  42.       DosBeep(1200, 1000);
  43.       return(-1);
  44.    } /* endif */
  45.  
  46.    /* Create a message queue */
  47.    if(!(hmq = WinCreateMsgQueue(hab, 50)))  {
  48.       DosBeep(1200, 1000);
  49.       WinTerminate(hab);
  50.       return(-1);
  51.    } /* endif */
  52.  
  53.    /* Register the SVP Notebook window Class */
  54.    WinRegisterClass(hab,
  55.                     szTestClass,
  56.                     TestWndProc,
  57.                     CS_CLIPCHILDREN | CS_CLIPSIBLINGS,
  58.                     4);
  59.  
  60.    /* Create the test window */
  61.    hwndFrame = WinCreateStdWindow(HWND_DESKTOP,
  62.                                   WS_VISIBLE,
  63.                                   &flCreateFlags,
  64.                                   szTestClass,
  65.                                   "Graphic Test Program",
  66.                                   (HMODULE)0,
  67.                                   0,
  68.                                   0,
  69.                                   &hwndClient);
  70.  
  71.  
  72.    /* Get/Dispatch Message loop */
  73.    while(WinGetMsg(hab, &qmsg, (HWND)0, 0, 0)) {
  74.       WinDispatchMsg(hab, &qmsg);
  75.    } /* endwhile */
  76.  
  77.    /* Destroy the message queue */
  78.    WinDestroyMsgQueue(hmq);
  79.  
  80.    /* Terminate the PM Session */
  81.    WinTerminate(hab);
  82.  
  83.    return(0);
  84. }
  85.  
  86. MRESULT EXPENTRY TestWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  87.  
  88. {
  89.    static   HWND   hwndMenu = (HWND)0;
  90.    static   int    nSelectedObject = GRAPH_NONE;
  91.    static   int    nSelectedGroup  = 0;
  92.    HGRAPH   hGraph = WinQueryWindowPtr(hwnd, QWL_USER);
  93.  
  94.    switch (msg) {
  95.    case WM_CREATE:
  96.       hGraph = InitTest(hwnd);
  97.       WinSetWindowPtr(hwnd, QWL_USER, hGraph);
  98.       return(MPFROMLONG(FALSE));
  99.  
  100.    case WM_SIZE:
  101.       if(hGraph) {
  102.          GraphSetWindowPos(hGraph, 0, 0, SHORT1FROMMP(mp2), SHORT2FROMMP(mp2));
  103.       } /* endif */
  104.       break;
  105.  
  106.    case WM_PAINT: {
  107.       RECTL    rectl;
  108.       POINTL   aptl[4];
  109.       HPS   hps = WinBeginPaint(hwnd, (HPS)0, &rectl);
  110.  
  111.       GraphPaint(hps, &rectl, hGraph);
  112.  
  113.       WinEndPaint(hps);
  114.       break;
  115.    } /* endcase */
  116.  
  117.    case WM_BUTTON1DOWN:
  118.       if(hGraph)
  119.          GraphSelectObject(hGraph, GRAPH_NONE, 0);
  120.       break;
  121.  
  122.    case WM_BUTTON2DOWN:
  123.       if(hGraph) {
  124.          GRAPHPOINTER   gptr;
  125.          GraphQueryPointer(hGraph,
  126.                            LOUSHORT(mp1),
  127.                            HIUSHORT(mp1),
  128.                            &gptr);
  129.          GraphSelectObject(hGraph, gptr.lRegion, gptr.lGroup);
  130.          hwndMenu = GraphPopupMenu(hGraph,
  131.                                    SHORT1FROMMP(mp1),
  132.                                    SHORT2FROMMP(mp1),
  133.                                    WinQueryWindow(hwnd, QW_PARENT));
  134.       } /* endif */
  135.       return(0);
  136.  
  137.    case WM_COMMAND:
  138.       if(hGraph && 
  139.          ((LOUSHORT(mp2) == CMDSRC_MENU) || 
  140.           (LOUSHORT(mp2) == CMDSRC_ACCELERATOR))) {
  141.          GraphSelectMenuItem(hGraph, LOUSHORT(mp1));
  142.       } /* endif */
  143.       break;
  144.  
  145.    case WM_DESTROY:
  146.       if(hGraph) GraphDestroy(hGraph);
  147.       break;
  148.    } /* endswitch */
  149.  
  150.   return (WinDefWindowProc(hwnd, msg, mp1, mp2));
  151. }
  152.  
  153. HGRAPH InitTest(HWND hwnd)
  154.  
  155. {
  156.    HGRAPH   hGraph = (HGRAPH)0;
  157.    RECTL    rectl;
  158.  
  159.    /* Create the SVP Graph */
  160.    if (GraphCreate(hwnd, (PRECTL)0, &hGraph)) {
  161.  
  162.       int      i;
  163.       LONG     lXValue;
  164.       short    sYValue;
  165.       double   afXData[DATA_ARRAY_SIZE];
  166.       float    afYData[DATA_ARRAY_SIZE];
  167.  
  168.       /* Set graph options */
  169.       GraphSetOptions(hGraph,
  170.                       GRAPH_FRAME_WINDOW |
  171.                       GRAPH_FRAME_PLOT |
  172.                       GRAPH_CLASSIFICATION_ENABLED |
  173.                       GRAPH_LEGEND_ENABLED |
  174.                       GRAPH_CORRELATE_GROUPS |
  175.                       GRAPH_CORRELATE_DATA |
  176.                       GRAPH_LEGEND_OUTSIDE |
  177.                       GRAPH_LEGEND_TOPRIGHT |
  178.                       GRAPH_LEGEND_LINE,
  179.                       TRUE);
  180.  
  181.       /* Set region colors */
  182.       GraphSetRegionColor(hGraph, GRAPH_LEGEND, CLR_WHITE);
  183.       GraphSetRegionColor(hGraph, GRAPH_PLOT, CLR_YELLOW);
  184.       GraphSetRegionColor(hGraph, GRAPH_WINDOW, CLR_GREEN);
  185.  
  186.       /* Set the title of the graph */
  187.       GraphSetRegionColor(hGraph,
  188.                           GRAPH_TITLE,
  189.                           CLR_CYAN);
  190.       GraphSetRegionText(hGraph,
  191.                          GRAPH_TITLE,
  192.                          "Test Program");
  193.  
  194.       /* Set the title of the left margin */
  195.       GraphSetRegionColor(hGraph,
  196.                           GRAPH_Y_TITLE,
  197.                           CLR_BLUE);
  198.       GraphSetRegionText(hGraph,
  199.                          GRAPH_Y_TITLE,
  200.                          "Left Margin (Units)");
  201.  
  202.       /* Set the title of the footing */
  203.       GraphSetRegionColor(hGraph,
  204.                           GRAPH_X_TITLE,
  205.                           CLR_BLUE);
  206.       GraphSetRegionText(hGraph,
  207.                          GRAPH_X_TITLE,
  208.                          "Footing (Units)");
  209.  
  210.       /* Set the title of the right margin */
  211.       GraphSetRegionColor(hGraph,
  212.                           GRAPH_2Y_TITLE,
  213.                           CLR_RED);
  214.       GraphSetRegionFont(hGraph,
  215.                          GRAPH_2Y_TITLE,
  216.                          "Helvetica Italic",
  217.                          MAKEFIXED(8, 0));
  218.       GraphSetRegionText(hGraph,
  219.                          GRAPH_2Y_TITLE,
  220.                          "Right Margin");
  221.  
  222.       /* Set the classification and color */
  223.       GraphSetRegionColor(hGraph,
  224.                           GRAPH_CLASS_TOPLEFT,
  225.                           CLR_RED);
  226.       GraphSetRegionText(hGraph,
  227.                          GRAPH_CLASS_TOPLEFT,
  228.                          "Private");
  229.  
  230.       /* Set the group count */
  231.       GraphSetGroupCount(hGraph, 1);
  232.  
  233.       /* Setup the data */
  234.       GraphSetupData(hGraph,
  235.                      1,
  236.                      GRAPH_DATA_DOUBLE,
  237.                      GRAPH_DATA_FLOAT,
  238.                      DATA_ARRAY_SIZE);
  239.  
  240.       /* Set the group marker */
  241.       GraphSetGroupMarker(hGraph, 1, GRAPH_MARKSYM_DIAMOND);
  242.  
  243.       /* Setup the float test data */
  244.       for (i=0; i<DATA_ARRAY_SIZE; i++) {
  245.          afXData[i] = 1000.0 + (20.0 * i);
  246.          afYData[i] = (float)(5000 - (i * 100));
  247.       } /* endfor */
  248.       GraphSetXData( hGraph,
  249.                      1,
  250.                      afXData);
  251.       GraphSetYData( hGraph,
  252.                      1,
  253.                      afYData);
  254.  
  255.       /* Set group 1 title */
  256.       GraphSetGroupText(hGraph, 1, "Alpha");
  257.  
  258.       GraphSetGroupCount(hGraph, 2);
  259.  
  260.       /* Setup the data */
  261.       GraphSetupData(hGraph,
  262.                      2,
  263.                      GRAPH_DATA_LONG,
  264.                      GRAPH_DATA_SHORT,
  265.                      10);
  266.  
  267.       /* Initialize the data */
  268.       for (i=0; i<10; i++) {
  269.          lXValue = 4000 + i*100;
  270.          sYValue = (short)(i * 200);
  271.          GraphSetData(hGraph,
  272.                       2,
  273.                       i,
  274.                       &lXValue,
  275.                       &sYValue);
  276.       } /* endfor */
  277.  
  278.       /* Set the group 2 title */
  279.       GraphSetGroupText(hGraph, 2, "Beta");
  280.  
  281.    } /* endif */
  282.  
  283.    return (hGraph);
  284. }
  285.  
  286.  
  287.