home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / graphics.zip / GTEST1.C < prev    next >
Text File  |  1993-10-28  |  8KB  |  296 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_RENDERFMT:
  118.       GraphRenderClipbd(hGraph, SHORT1FROMMP(mp1));
  119.       break;
  120.  
  121.    case WM_RENDERALLFMTS:
  122.       GraphRenderClipbd(hGraph, CF_METAFILE);
  123.       GraphRenderClipbd(hGraph, CF_BITMAP);
  124.       break;
  125.  
  126.    case WM_BUTTON1DOWN:
  127.       if(hGraph)
  128.          GraphSelectObject(hGraph, GRAPH_NONE, 0);
  129.       break;
  130.  
  131.    case WM_BUTTON2DOWN:
  132.       if(hGraph) {
  133.          GRAPHPOINTER   gptr;
  134.          GraphQueryPointer(hGraph,
  135.                            LOUSHORT(mp1),
  136.                            HIUSHORT(mp1),
  137.                            &gptr);
  138.          GraphSelectObject(hGraph, gptr.lRegion, gptr.lGroup);
  139.          hwndMenu = GraphPopupMenu(hGraph,
  140.                                    SHORT1FROMMP(mp1),
  141.                                    SHORT2FROMMP(mp1),
  142.                                    WinQueryWindow(hwnd, QW_PARENT));
  143.       } /* endif */
  144.       return(0);
  145.  
  146.    case WM_COMMAND:
  147.       if(hGraph && 
  148.          ((LOUSHORT(mp2) == CMDSRC_MENU) || 
  149.           (LOUSHORT(mp2) == CMDSRC_ACCELERATOR))) {
  150.          GraphSelectMenuItem(hGraph, LOUSHORT(mp1));
  151.       } /* endif */
  152.       break;
  153.  
  154.    case WM_DESTROY:
  155.       if(hGraph) GraphDestroy(hGraph);
  156.       break;
  157.    } /* endswitch */
  158.  
  159.   return (WinDefWindowProc(hwnd, msg, mp1, mp2));
  160. }
  161.  
  162. HGRAPH InitTest(HWND hwnd)
  163.  
  164. {
  165.    HGRAPH   hGraph = (HGRAPH)0;
  166.    RECTL    rectl;
  167.  
  168.    /* Create the SVP Graph */
  169.    if (GraphCreate(hwnd, (PRECTL)0, &hGraph)) {
  170.  
  171.       int      i;
  172.       LONG     lXValue;
  173.       short    sYValue;
  174.       float    afXData[DATA_ARRAY_SIZE];
  175.       double   afYData[DATA_ARRAY_SIZE];
  176.  
  177.       /* Set graph options */
  178.       GraphSetOptions(hGraph,
  179.                       GRAPH_FRAME_WINDOW |
  180.                       GRAPH_FRAME_PLOT |
  181.                       GRAPH_CLASSIFICATION_ENABLED |
  182.                       GRAPH_LEGEND_ENABLED |
  183.                       GRAPH_CORRELATE_GROUPS |
  184.                       GRAPH_CORRELATE_DATA |
  185.                       GRAPH_LEGEND_OUTSIDE |
  186.                       GRAPH_LEGEND_TOPRIGHT |
  187.                       GRAPH_LEGEND_LINE,
  188.                       TRUE);
  189.  
  190.       /* Set region colors */
  191.       GraphSetRegionColor(hGraph, GRAPH_LEGEND, CLR_WHITE);
  192.       GraphSetRegionColor(hGraph, GRAPH_PLOT, CLR_YELLOW);
  193.       GraphSetRegionColor(hGraph, GRAPH_WINDOW, CLR_GREEN);
  194.  
  195.       /* Set the title of the graph */
  196.       GraphSetRegionColor(hGraph,
  197.                           GRAPH_TITLE,
  198.                           CLR_CYAN);
  199.       GraphSetRegionText(hGraph,
  200.                          GRAPH_TITLE,
  201.                          "Test Program");
  202.  
  203.       /* Set the title of the left margin */
  204.       GraphSetRegionColor(hGraph,
  205.                           GRAPH_Y_TITLE,
  206.                           CLR_BLUE);
  207.       GraphSetRegionText(hGraph,
  208.                          GRAPH_Y_TITLE,
  209.                          "Left Margin (Units)");
  210.  
  211.       /* Set the title of the footing */
  212.       GraphSetRegionColor(hGraph,
  213.                           GRAPH_X_TITLE,
  214.                           CLR_BLUE);
  215.       GraphSetRegionText(hGraph,
  216.                          GRAPH_X_TITLE,
  217.                          "Footing (Units)");
  218.  
  219.       /* Set the title of the right margin */
  220.       GraphSetRegionColor(hGraph,
  221.                           GRAPH_2Y_TITLE,
  222.                           CLR_RED);
  223.       GraphSetRegionFont(hGraph,
  224.                          GRAPH_2Y_TITLE,
  225.                          "Helvetica Italic",
  226.                          MAKEFIXED(8, 0));
  227.       GraphSetRegionText(hGraph,
  228.                          GRAPH_2Y_TITLE,
  229.                          "Right Margin");
  230.  
  231.       /* Set the classification and color */
  232.       GraphSetRegionColor(hGraph,
  233.                           GRAPH_CLASS_TOPLEFT,
  234.                           CLR_RED);
  235.       GraphSetRegionText(hGraph,
  236.                          GRAPH_CLASS_TOPLEFT,
  237.                          "Private");
  238.  
  239.       /* Set the group count */
  240.       GraphSetGroupCount(hGraph, 1);
  241.  
  242.       /* Setup the data */
  243.       GraphSetupData(hGraph,
  244.                      1,
  245.                      GRAPH_DATA_FLOAT,
  246.                      GRAPH_DATA_DOUBLE,
  247.                      DATA_ARRAY_SIZE);
  248.  
  249.       /* Set the group marker */
  250.       GraphSetGroupMarker(hGraph, 1, GRAPH_MARKSYM_DIAMOND);
  251.  
  252.       /* Setup the float test data */
  253.       for (i=0; i<DATA_ARRAY_SIZE; i++) {
  254.          afXData[i] = (float)(3000.0 + (20.0 * i));
  255.          afYData[i] = 5000.0 + (10000.0 * sin((double)i/2.0));
  256.       } /* endfor */
  257.       GraphSetXData( hGraph,
  258.                      1,
  259.                      afXData);
  260.       GraphSetYData( hGraph,
  261.                      1,
  262.                      afYData);
  263.  
  264.       /* Set group 1 title */
  265.       GraphSetGroupText(hGraph, 1, "Alpha");
  266.  
  267.       GraphSetGroupCount(hGraph, 2);
  268.  
  269.       /* Setup the data */
  270.       GraphSetupData(hGraph,
  271.                      2,
  272.                      GRAPH_DATA_LONG,
  273.                      GRAPH_DATA_SHORT,
  274.                      10);
  275.  
  276.       /* Initialize the data */
  277.       for (i=0; i<10; i++) {
  278.          lXValue = 4000 + i*100;
  279.          sYValue = (short)(i * 200);
  280.          GraphSetData(hGraph,
  281.                       2,
  282.                       i,
  283.                       &lXValue,
  284.                       &sYValue);
  285.       } /* endfor */
  286.  
  287.       /* Set the group 2 title */
  288.       GraphSetGroupText(hGraph, 2, "Beta");
  289.  
  290.    } /* endif */
  291.  
  292.    return (hGraph);
  293. }
  294.  
  295.  
  296.