home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol8n19.zip / BEZIER.C < prev    next >
Text File  |  1989-10-02  |  5KB  |  171 lines

  1.  
  2.  
  3. /*-------------------------------------------------------------------
  4.    BEZIER.C -- GPI Bezier Spline Demonstration Program
  5.                (c) 1989, Charles Petzold and Ziff Communications Co.
  6.                PC Magazine, Volume 8, Number 19
  7.   -------------------------------------------------------------------*/
  8.  
  9. #define INCL_WIN
  10. #define INCL_GPI
  11. #include <os2.h>
  12.  
  13. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  14.  
  15. int main (void)
  16.      {
  17.      static CHAR  szClientClass[] = "Bezier" ;
  18.      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU  |
  19.                                  FCF_SIZEBORDER    | FCF_MINMAX   |
  20.                                  FCF_SHELLPOSITION | FCF_TASKLIST ;
  21.      HAB          hab ;
  22.      HMQ          hmq ;
  23.      HWND         hwndFrame, hwndClient ;
  24.      QMSG         qmsg ;
  25.  
  26.      hab = WinInitialize (0) ;
  27.      hmq = WinCreateMsgQueue (hab, 0) ;
  28.  
  29.      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
  30.  
  31.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  32.                                      &flFrameFlags, szClientClass, NULL,
  33.                                      0L, NULL, 0, &hwndClient) ;
  34.  
  35.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  36.           WinDispatchMsg (hab, &qmsg) ;
  37.  
  38.      WinDestroyWindow (hwndFrame) ;
  39.      WinDestroyMsgQueue (hmq) ;
  40.      WinTerminate (hab) ;
  41.      return 0 ;
  42.      }
  43.  
  44. VOID DrawSpline (HPS hps, POINTL *pptlEnd1, POINTL *pptlEnd2,
  45.                           POINTL *pptlCtl1, POINTL *pptlCtl2)
  46.      {
  47.      POINTL aptl[4] ;
  48.  
  49.                // Set 4 point structures
  50.  
  51.      aptl[0] = *pptlEnd1 ;
  52.      aptl[1] = *pptlCtl1 ;
  53.      aptl[2] = *pptlCtl2 ;
  54.      aptl[3] = *pptlEnd2 ;
  55.  
  56.                // Draw dotted straight lines
  57.  
  58.      GpiSetLineType (hps, LINETYPE_ALTERNATE) ;
  59.      GpiMove (hps, aptl + 0) ;
  60.      GpiLine (hps, aptl + 1) ;
  61.      GpiMove (hps, aptl + 3) ;
  62.      GpiLine (hps, aptl + 2) ;
  63.  
  64.                // Draw spline
  65.  
  66.      GpiSetLineType (hps, LINETYPE_SOLID) ;
  67.      GpiMove (hps, aptl) ;
  68.      GpiPolySpline (hps, 3L, aptl + 1) ;
  69.      }
  70.  
  71. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  72.      {
  73.      static BOOL   fButton1Down, fButton2Down ;
  74.      static POINTL ptlEnd1, ptlEnd2, ptlCtl1, ptlCtl2 ;
  75.      HPS           hps ;
  76.      SHORT         cxClient, cyClient ;
  77.  
  78.      switch (msg)
  79.           {
  80.           case WM_SIZE:
  81.                cxClient = SHORT1FROMMP (mp2) ;
  82.                cyClient = SHORT2FROMMP (mp2) ;
  83.  
  84.                ptlEnd1.x = cxClient / 3 ;
  85.                ptlEnd1.y = cyClient / 2 ;
  86.  
  87.                ptlEnd2.x = 2 * cxClient / 3 ;
  88.                ptlEnd2.y = cyClient / 2 ;
  89.  
  90.                ptlCtl1.x = cxClient / 2 ;
  91.                ptlCtl1.y = 3 * cyClient / 4 ;
  92.  
  93.                ptlCtl2.x = cxClient / 2 ;
  94.                ptlCtl2.y = cyClient / 4 ;
  95.                return 0 ;
  96.  
  97.           case WM_BUTTON1DOWN:
  98.                if (!fButton2Down)
  99.                     {
  100.                     WinSetCapture (HWND_DESKTOP, hwnd) ;
  101.                     fButton1Down = TRUE ;
  102.                     }
  103.                else
  104.                     WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  105.  
  106.                break ;
  107.  
  108.           case WM_BUTTON2DOWN:
  109.                if (!fButton1Down)
  110.                     {
  111.                     WinSetCapture (HWND_DESKTOP, hwnd) ;
  112.                     fButton2Down = TRUE ;
  113.                     }
  114.                else
  115.                     WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  116.  
  117.                break ;
  118.  
  119.           case WM_BUTTON1UP:
  120.                if (fButton1Down)
  121.                     {
  122.                     WinSetCapture (HWND_DESKTOP, NULL) ;
  123.                     fButton1Down = FALSE ;
  124.                     }
  125.                break ;
  126.  
  127.           case WM_BUTTON2UP:
  128.                if (fButton2Down)
  129.                     {
  130.                     WinSetCapture (HWND_DESKTOP, NULL) ;
  131.                     fButton2Down = FALSE ;
  132.                     }
  133.                break ;
  134.  
  135.           case WM_MOUSEMOVE:
  136.                if (!fButton1Down && !fButton2Down)
  137.                     break ;
  138.  
  139.                hps = WinGetPS (hwnd) ;
  140.                GpiSetColor (hps, CLR_BACKGROUND) ;
  141.                DrawSpline (hps, &ptlEnd1, &ptlEnd2, &ptlCtl1, &ptlCtl2) ;
  142.  
  143.                if (fButton1Down)
  144.                     {
  145.                     ptlCtl1.x = MOUSEMSG(&msg)->x ;
  146.                     ptlCtl1.y = MOUSEMSG(&msg)->y ;
  147.                     }
  148.                else
  149.                     {
  150.                     ptlCtl2.x = MOUSEMSG(&msg)->x ;
  151.                     ptlCtl2.y = MOUSEMSG(&msg)->y ;
  152.                     }
  153.  
  154.                GpiSetColor (hps, CLR_NEUTRAL) ;
  155.                DrawSpline (hps, &ptlEnd1, &ptlEnd2, &ptlCtl1, &ptlCtl2) ;
  156.                WinReleasePS (hps) ;
  157.                break ;
  158.  
  159.           case WM_PAINT:
  160.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  161.                GpiErase (hps) ;
  162.  
  163.                DrawSpline (hps, &ptlEnd1, &ptlEnd2, &ptlCtl1, &ptlCtl2) ;
  164.  
  165.                WinEndPaint (hps) ;
  166.                return 0 ;
  167.           }
  168.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  169.      }
  170.  
  171.