home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MSJV3-3.ZIP / GPICODE.ALL < prev    next >
Text File  |  1988-04-28  |  16KB  |  564 lines

  1. Microsoft Systems Journal
  2. Volume 3; Issue 3; May, 1988
  3.  
  4.  
  5. Code Listings For:
  6.  
  7.     CACHEDPS; MICROPS; NORMALPS
  8.     pp. 9-18
  9.  
  10.  
  11. Author(s): Charles Petzold
  12. Title:     The Graphics Programming Interface: A Guide to OS/2 
  13.            Presentation Spaces
  14.  
  15.  
  16.  
  17.  
  18. Figure 3
  19. =========
  20.  
  21.  
  22.  
  23. #--------------------
  24. # CACHEDPS make file
  25. #--------------------
  26.  
  27. cachedps.obj : cachedps.c
  28.      cl -c -G2sw -W2 -Zp cachedps.c
  29.  
  30. cachedps.exe : cachedps.obj cachedps.def
  31.      link cachedps, /align:16, NUL, os2, cachedps
  32.  
  33.  
  34. ==============================================================================
  35.  
  36.  
  37. ;-------------------------------------
  38. ; CACHEDPS.DEF module definition file
  39. ;-------------------------------------
  40.  
  41. NAME           CACHEDPS
  42. DESCRIPTION    'Demonstrates Cached Micro-PS (C) Charles Petzold, 1988'
  43. PROTMODE
  44. HEAPSIZE       1024
  45. STACKSIZE      8192
  46. EXPORTS        ClientWndProc
  47.  
  48.  
  49.  
  50. ==============================================================================
  51.  
  52.  
  53. /*--------------------------------------------
  54.    CACHEDPS.C -- Demonstrates Cached Micro-PS
  55.   --------------------------------------------*/
  56.  
  57. #define INCL_GPI
  58.  
  59. #include <os2.h>
  60.  
  61. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  62.  
  63. main ()
  64.      {
  65.      static CHAR szClientClass [] = "CachedPS" ;
  66.      HAB    hab ;
  67.      HMQ    hmq ;
  68.      HWND   hwndFrame, hwndClient ;
  69.      QMSG   qmsg ;
  70.  
  71.      hab = WinInitialize (0) ;
  72.      hmq = WinCreateMsgQueue (hab, 0) ;
  73.  
  74.      WinRegisterClass (hab, szClientClass, ClientWndProc,
  75.                                            CS_SIZEREDRAW, 0) ;
  76.  
  77.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP,
  78.                     WS_VISIBLE | FS_SIZEBORDER | FS_TITLEBAR
  79.                                | FS_SYSMENU    | FS_MINMAX,
  80.                     szClientClass, "Cached Micro-PS",
  81.                     0L, NULL, 0, &hwndClient) ;
  82.  
  83.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  84.           WinDispatchMsg (hab, &qmsg) ;
  85.  
  86.      WinDestroyWindow (hwndFrame) ;
  87.      WinDestroyMsgQueue (hmq) ;
  88.      WinTerminate (hab) ;
  89.  
  90.      return 0 ;
  91.      }
  92.  
  93. MRESULT EXPENTRY ClientWndProc (hwnd, msg, mp1, mp2)
  94.      HWND          hwnd ;
  95.      USHORT        msg ;
  96.      MPARAM        mp1 ;
  97.      MPARAM        mp2 ;
  98.      {
  99.      static CHAR   szText []      = "Graphics Programming Interface" ;
  100.      static LONG   lTextLength    = sizeof szText - 1L ;
  101.      static LONG   alColorData [] = { CLR_BACKGROUND, RGB_WHITE,
  102.                                       CLR_NEUTRAL,    RGB_BLACK } ;
  103.      static POINTL ptlTextStart, aptlLineStart [4],
  104.                    aptlTextBox [TXTBOX_COUNT] ;
  105.      static SHORT  cxClient, cyClient ;
  106.      HPS           hps ;
  107.      POINTL        ptl ;
  108.      SHORT         sIndex ;
  109.  
  110.      switch (msg)
  111.           {
  112.           case WM_CREATE:
  113.                hps = WinGetPS (hwnd) ;
  114.  
  115.                GpiQueryTextBox (hps, lTextLength, szText,
  116.                                 TXTBOX_COUNT, aptlTextBox) ;
  117.  
  118.                WinReleasePS (hps) ;
  119.                break ;
  120.  
  121.           case WM_SIZE:
  122.                cxClient = LOUSHORT (mp2) ;
  123.                cyClient = HIUSHORT (mp2) ;
  124.  
  125.                ptlTextStart.x = (cxClient - 
  126.                                 aptlTextBox [TXTBOX_BOTTOMRIGHT].x -
  127.                                 aptlTextBox [TXTBOX_BOTTOMLEFT].x) / 2 ;
  128.  
  129.                ptlTextStart.y = (cyClient -
  130.                                 aptlTextBox [TXTBOX_TOPLEFT].y -
  131.                                 aptlTextBox [TXTBOX_BOTTOMLEFT].y) / 2 ;
  132.  
  133.                for (sIndex = 0 ; sIndex < 4 ; sIndex ++)
  134.                     {
  135.                     aptlLineStart [sIndex] = aptlTextBox [sIndex] ;
  136.                     aptlLineStart [sIndex].x += ptlTextStart.x ;
  137.                     aptlLineStart [sIndex].y += ptlTextStart.y ;
  138.                     }
  139.                break ;
  140.  
  141.           case WM_PAINT:
  142.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  143.  
  144.                GpiSavePS (hps) ;                         /* temp fix */
  145.                GpiResetPS (hps, GRES_ATTRS) ;            /* temp fix */
  146.                GpiCreateLogColorTable (hps, LCOL_RESET,
  147.                     LCOLF_INDRGB, 0L, 4L, alColorData) ; /* temp fix */
  148.  
  149.                GpiErase (hps) ;
  150.  
  151.                GpiSetColor (hps, CLR_RED) ;
  152.  
  153.                GpiCharStringAt (hps, &ptlTextStart,
  154.                                    lTextLength, szText) ;
  155.  
  156.                GpiSetLineType (hps, LINETYPE_DOT) ;
  157.  
  158.                GpiMove (hps, aptlLineStart + TXTBOX_BOTTOMLEFT) ;
  159.                ptl.x = 0 ;
  160.                ptl.y = 0 ;
  161.                GpiLine (hps, &ptl) ;
  162.  
  163.                GpiMove (hps, aptlLineStart + TXTBOX_BOTTOMRIGHT) ;
  164.                ptl.x = cxClient ;
  165.                GpiLine (hps, &ptl) ;
  166.  
  167.                GpiMove (hps, aptlLineStart + TXTBOX_TOPRIGHT) ;
  168.                ptl.y = cyClient ;
  169.                GpiLine (hps, &ptl) ;
  170.  
  171.                GpiMove (hps, aptlLineStart + TXTBOX_TOPLEFT) ;
  172.                ptl.x = 0 ;
  173.                GpiLine (hps, &ptl) ;
  174.  
  175.                GpiRestorePS (hps, -1L) ;    /* temp fix */
  176.  
  177.                WinEndPaint (hps) ;
  178.                break ;
  179.  
  180.           default:
  181.                return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  182.           }
  183.      return FALSE ;
  184.      }
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. Figure 4
  192. ========
  193.  
  194.  
  195.  
  196. #-------------------
  197. # MICROPS make file
  198. #-------------------
  199.  
  200. microps.obj : microps.c
  201.      cl -c -G2sw -W2 -Zp microps.c
  202.  
  203. microps.exe : microps.obj microps.def
  204.      link microps, /align:16, NUL, os2, microps
  205.  
  206.  
  207. ==============================================================================
  208.  
  209.  
  210. ;------------------------------------
  211. ; MICROPS.DEF module definition file
  212. ;------------------------------------
  213.  
  214. NAME           MICROPS
  215. DESCRIPTION    'Demonstrates Micro-PS (C) Charles Petzold, 1988'
  216. PROTMODE
  217. HEAPSIZE       1024
  218. STACKSIZE      8192
  219. EXPORTS        ClientWndProc
  220.  
  221.  
  222. ==============================================================================
  223.  
  224.  
  225. /*------------------------------------
  226.    MICROPS.C -- Demonstrates Micro-PS
  227.   ------------------------------------*/
  228.  
  229. #define INCL_WIN
  230. #define INCL_GPI
  231.  
  232. #include <os2.h>
  233.  
  234. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  235.  
  236. HAB  hab ;
  237.  
  238. main ()
  239.      {
  240.      static CHAR szClientClass [] = "MicroPS" ;
  241.      HMQ    hmq ;
  242.      HWND   hwndFrame, hwndClient ;
  243.      QMSG   qmsg ;
  244.  
  245.      hab = WinInitialize (0) ;
  246.      hmq = WinCreateMsgQueue (hab, 0) ;
  247.  
  248.      WinRegisterClass (hab, szClientClass, ClientWndProc,
  249.                                            CS_SIZEREDRAW, 0) ;
  250.  
  251.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP,
  252.                     WS_VISIBLE | FS_SIZEBORDER | FS_TITLEBAR
  253.                                | FS_SYSMENU    | FS_MINMAX,
  254.                     szClientClass, "Micro-PS",
  255.                     0L, NULL, 0, &hwndClient) ;
  256.  
  257.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  258.           WinDispatchMsg (hab, &qmsg) ;
  259.  
  260.      WinDestroyWindow (hwndFrame) ;
  261.      WinDestroyMsgQueue (hmq) ;
  262.      WinTerminate (hab) ;
  263.  
  264.      return 0 ;
  265.      }
  266.  
  267. MRESULT EXPENTRY ClientWndProc (hwnd, msg, mp1, mp2)
  268.      HWND          hwnd ;
  269.      USHORT        msg ;
  270.      MPARAM        mp1 ;
  271.      MPARAM        mp2 ;
  272.      {
  273.      static CHAR   szText []      = "Graphics Programming Interface" ;
  274.      static HPS    hps ;
  275.      static LONG   lTextLength    = sizeof szText - 1L ;
  276.      static LONG   alColorData [] = { CLR_BACKGROUND, RGB_WHITE,
  277.                                       CLR_NEUTRAL,    RGB_BLACK } ;
  278.      static POINTL ptlTextStart, aptlLineStart [4],
  279.                    aptlTextBox [TXTBOX_COUNT] ;
  280.      static SHORT  cxClient, cyClient ;
  281.      HDC           hdc ;
  282.      POINTL        ptl ;
  283.      SHORT         sIndex ;
  284.      SIZEL         sizl ;
  285.  
  286.      switch (msg)
  287.           {
  288.           case WM_CREATE:
  289.                hdc = WinOpenWindowDC (hwnd) ;
  290.  
  291.                sizl.cx = 0 ;
  292.                sizl.cy = 0 ;
  293.  
  294.                hps = GpiCreatePS (hab, hdc, &sizl, PU_PELS |
  295.                                    GPIF_DEFAULT | GPIT_MICRO |
  296.                                    GPIM_NORMAL  | GPIA_ASSOC) ;
  297.  
  298.                GpiCreateLogColorTable (hps, LCOL_RESET,
  299.                     LCOLF_INDRGB, 0L, 4L, alColorData) ; /* temp fix */
  300.  
  301.                GpiQueryTextBox (hps, lTextLength, szText,
  302.                                 TXTBOX_COUNT, aptlTextBox) ;
  303.  
  304.                GpiSetColor (hps, CLR_RED) ;
  305.                GpiSetLineType (hps, LINETYPE_DOT) ;
  306.                break ;
  307.  
  308.           case WM_SIZE:
  309.                cxClient = LOUSHORT (mp2) ;
  310.                cyClient = HIUSHORT (mp2) ;
  311.  
  312.                ptlTextStart.x = (cxClient - 
  313.                                 aptlTextBox [TXTBOX_BOTTOMRIGHT].x -
  314.                                 aptlTextBox [TXTBOX_BOTTOMLEFT].x) / 2 ;
  315.  
  316.                ptlTextStart.y = (cyClient -
  317.                                 aptlTextBox [TXTBOX_TOPLEFT].y -
  318.                                 aptlTextBox [TXTBOX_BOTTOMLEFT].y) / 2 ;
  319.  
  320.                for (sIndex = 0 ; sIndex < 4 ; sIndex ++)
  321.                     {
  322.                     aptlLineStart [sIndex] = aptlTextBox [sIndex] ;
  323.                     aptlLineStart [sIndex].x += ptlTextStart.x ;
  324.                     aptlLineStart [sIndex].y += ptlTextStart.y ;
  325.                     }
  326.  
  327.                break ;
  328.  
  329.           case WM_PAINT:
  330.                /* BeginPaint (hwnd, hps, NULL) ; */
  331.  
  332.                GpiErase (hps) ;
  333.  
  334.                GpiCharStringAt (hps, &ptlTextStart,
  335.                                    lTextLength, szText) ;
  336.  
  337.                GpiMove (hps, aptlLineStart + TXTBOX_BOTTOMLEFT) ;
  338.                ptl.x = 0 ;
  339.                ptl.y = 0 ;
  340.                GpiLine (hps, &ptl) ;
  341.  
  342.                GpiMove (hps, aptlLineStart + TXTBOX_BOTTOMRIGHT) ;
  343.                ptl.x = cxClient ;
  344.                GpiLine (hps, &ptl) ;
  345.  
  346.                GpiMove (hps, aptlLineStart + TXTBOX_TOPRIGHT) ;
  347.                ptl.y = cyClient ;
  348.                GpiLine (hps, &ptl) ;
  349.  
  350.                GpiMove (hps, aptlLineStart + TXTBOX_TOPLEFT) ;
  351.                ptl.x = 0 ;
  352.                GpiLine (hps, &ptl) ;
  353.  
  354.                /* EndPaint (hps) ; */
  355.  
  356.                WinValidateRect (hwnd, NULL, FALSE) ;    /* temp fix */
  357.                break ;
  358.  
  359.           case WM_DESTROY:
  360.                GpiDestroyPS (hps) ;
  361.                break ;
  362.  
  363.           default:
  364.                return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  365.           }
  366.      return FALSE ;
  367.      }
  368.  
  369.  
  370.  
  371.  
  372.  
  373. Figure 6
  374. ========
  375.  
  376.  
  377. #--------------------
  378. # NORMALPS make file
  379. #--------------------
  380.  
  381. normalps.obj : normalps.c
  382.      cl -c -G2sw -W2 -Zp normalps.c
  383.  
  384. normalps.exe : normalps.obj normalps.def
  385.      link normalps, /align:16, NUL, os2, normalps
  386.  
  387.  
  388.  
  389. ==============================================================================
  390.  
  391.  
  392.  
  393. ;-------------------------------------
  394. ; NORMALPS.DEF module definition file
  395. ;-------------------------------------
  396.  
  397. NAME           NORMALPS
  398. DESCRIPTION    'Demonstrates Normal-PS (C) Charles Petzold, 1988'
  399. PROTMODE
  400. HEAPSIZE       1024
  401. STACKSIZE      8192
  402. EXPORTS        ClientWndProc
  403.  
  404.  
  405. ==============================================================================
  406.  
  407.  
  408.  
  409. /*--------------------------------------
  410.    NORMALPS.C -- Demonstrates Normal-PS
  411.   --------------------------------------*/
  412.  
  413. #define INCL_WIN
  414. #define INCL_GPI
  415.  
  416. #include <os2.h>
  417.  
  418. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  419.  
  420. HAB  hab ;
  421.  
  422. main ()
  423.      {
  424.      static CHAR szClientClass [] = "NormalPS" ;
  425.      HMQ    hmq ;
  426.      HWND   hwndFrame, hwndClient ;
  427.      QMSG   qmsg ;
  428.  
  429.      hab = WinInitialize (0) ;
  430.      hmq = WinCreateMsgQueue (hab, 0) ;
  431.  
  432.      WinRegisterClass (hab, szClientClass, ClientWndProc,
  433.                                            CS_SIZEREDRAW, 0) ;
  434.  
  435.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP,
  436.                     WS_VISIBLE | FS_SIZEBORDER | FS_TITLEBAR
  437.                                | FS_SYSMENU    | FS_MINMAX,
  438.                     szClientClass, "Normal-PS",
  439.                     0L, NULL, 0, &hwndClient) ;
  440.  
  441.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  442.           WinDispatchMsg (hab, &qmsg) ;
  443.  
  444.      WinDestroyWindow (hwndFrame) ;
  445.      WinDestroyMsgQueue (hmq) ;
  446.      WinTerminate (hab) ;
  447.  
  448.      return 0 ;
  449.      }
  450.  
  451. MRESULT EXPENTRY ClientWndProc (hwnd, msg, mp1, mp2)
  452.      HWND          hwnd ;
  453.      USHORT        msg ;
  454.      MPARAM        mp1 ;
  455.      MPARAM        mp2 ;
  456.      {
  457.      static CHAR   szText []      = "Graphics Programming Interface" ;
  458.      static HPS    hps ;
  459.      static LONG   lSegmentName   = 1 ;
  460.      static LONG   lTextLength    = sizeof szText - 1L ;
  461.      static LONG   alColorData [] = { CLR_BACKGROUND, RGB_WHITE,
  462.                                       CLR_NEUTRAL,    RGB_BLACK } ;
  463.      static POINTL aptlTextBox [TXTBOX_COUNT] ;
  464.      HDC           hdc ;
  465.      POINTL        ptl, ptlTextStart, aptlLineStart [4] ;
  466.      SHORT         cxClient, cyClient, sIndex ;
  467.      SIZEL         sizl ;
  468.  
  469.      switch (msg)
  470.           {
  471.           case WM_CREATE:
  472.                hdc = WinOpenWindowDC (hwnd) ;
  473.  
  474.                sizl.cx = 0 ;
  475.                sizl.cy = 0 ;
  476.  
  477.                hps = GpiCreatePS (hab, hdc, &sizl, PU_PELS    |
  478.                                    GPIF_DEFAULT | GPIT_NORMAL |
  479.                                    GPIM_NORMAL  | GPIA_ASSOC) ;
  480.  
  481.                GpiCreateLogColorTable (hps, LCOL_RESET,
  482.                     LCOLF_INDRGB, 0L, 4L, alColorData) ; /* temp fix */
  483.  
  484.                GpiQueryTextBox (hps, lTextLength, szText,
  485.                                 TXTBOX_COUNT, aptlTextBox) ;
  486.  
  487.                GpiSetDrawControl (hps, DCTL_ERASE, DCTL_ON) ;
  488.                GpiSetDrawingMode (hps, DM_RETAIN) ;
  489.                break ;
  490.  
  491.           case WM_SIZE:
  492.                cxClient = LOUSHORT (mp2) ;
  493.                cyClient = HIUSHORT (mp2) ;
  494.  
  495.                ptlTextStart.x = (cxClient - 
  496.                                 aptlTextBox [TXTBOX_BOTTOMRIGHT].x -
  497.                                 aptlTextBox [TXTBOX_BOTTOMLEFT].x) / 2 ;
  498.  
  499.                ptlTextStart.y = (cyClient -
  500.                                 aptlTextBox [TXTBOX_TOPLEFT].y -
  501.                                 aptlTextBox [TXTBOX_BOTTOMLEFT].y) / 2 ;
  502.  
  503.                for (sIndex = 0 ; sIndex < 4 ; sIndex ++)
  504.                     {
  505.                     aptlLineStart [sIndex] = aptlTextBox [sIndex] ;
  506.                     aptlLineStart [sIndex].x += ptlTextStart.x ;
  507.                     aptlLineStart [sIndex].y += ptlTextStart.y ;
  508.                     }
  509.  
  510.                GpiDeleteSegment (hps, lSegmentName) ;
  511.  
  512.                GpiOpenSegment (hps, lSegmentName) ;
  513.                     {
  514.                     GpiSetColor (hps, CLR_RED) ;
  515.  
  516.                     GpiCharStringAt (hps, &ptlTextStart,
  517.                                      lTextLength, szText) ;
  518.  
  519.                     GpiSetLineType (hps, LINETYPE_DOT) ;
  520.  
  521.                     GpiMove (hps, aptlLineStart + TXTBOX_BOTTOMLEFT) ;
  522.                     ptl.x = 0 ;
  523.                     ptl.y = 0 ;
  524.                     GpiLine (hps, &ptl) ;
  525.  
  526.                     GpiMove (hps, aptlLineStart + TXTBOX_BOTTOMRIGHT) ;
  527.                     ptl.x = cxClient ;
  528.                     GpiLine (hps, &ptl) ;
  529.  
  530.                     GpiMove (hps, aptlLineStart + TXTBOX_TOPRIGHT) ;
  531.                     ptl.y = cyClient ;
  532.                     GpiLine (hps, &ptl) ;
  533.  
  534.                     GpiMove (hps, aptlLineStart + TXTBOX_TOPLEFT) ;
  535.                     ptl.x = 0 ;
  536.                     GpiLine (hps, &ptl) ;
  537.                     }
  538.                GpiCloseSegment (hps) ;
  539.                break ;
  540.  
  541.           case WM_PAINT:
  542.                /* WinBeginPaint (hwnd, hps, NULL) ; */
  543.  
  544.                GpiDrawChain (hps) ;
  545.  
  546.                /* WinEndPaint (hps) ; */
  547.  
  548.                WinValidateRect (hwnd, NULL, FALSE) ;   /* temp fix */
  549.                break ;
  550.  
  551.           case WM_DESTROY:
  552.                GpiDeleteSegment (hps, lSegmentName) ;
  553.                GpiAssociate (hps, NULL) ;
  554.                GpiDestroyPS (hps) ;
  555.                break ;
  556.  
  557.           default:
  558.                return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  559.           }
  560.      return FALSE ;
  561.      }
  562.  
  563.  
  564.