home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / S12145.ZIP / TRANSFRM.C < prev    next >
C/C++ Source or Header  |  1988-07-31  |  13KB  |  375 lines

  1. /*-------------------------------------------
  2.    TRANSFRM.C -- Demonstrates GPI Transforms
  3.   -------------------------------------------*/
  4.  
  5. #define INCL_WIN
  6. #define INCL_GPI
  7. #include <os2.h>
  8. #include <math.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "transfrm.h"
  12.  
  13. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  14.  
  15. HAB  hab ;
  16.  
  17. int main (void)
  18.      {
  19.      static CHAR szClientClass [] = "Transfrm" ;
  20.      HMQ         hmq ;
  21.      HWND        hwndFrame, hwndClient ;
  22.      QMSG        qmsg ;
  23.      ULONG       flFrameFlags = FCF_STANDARD ;
  24.      ULONG       flFrameStyle = WS_VISIBLE ;
  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, flFrameStyle,
  32.                                      &flFrameFlags, szClientClass,
  33.                                      "GPI Transform Demo",
  34.                                      0L, NULL, ID_RESOURCE, &hwndClient) ;
  35.  
  36.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  37.           WinDispatchMsg (hab, &qmsg) ;
  38.  
  39.      WinDestroyWindow (hwndFrame) ;
  40.      WinDestroyMsgQueue (hmq) ;
  41.      WinTerminate (hab) ;
  42.      return 0 ;
  43.      }
  44.           /*-------------------------------------
  45.              Functions for managing dialog boxes
  46.             -------------------------------------*/
  47.  
  48. VOID SetFixedItem (HWND hwnd, USHORT idItem, FIXED fxValue)
  49.      {
  50.      CHAR szBuffer [20] ;
  51.  
  52.      sprintf (szBuffer, "%.3f", fxValue / 65536.0) ;
  53.      WinSetDlgItemText (hwnd, idItem, szBuffer) ;
  54.      }
  55.  
  56. FIXED GetFixedItem (HWND hwnd, USHORT idItem)
  57.      {
  58.      CHAR szBuffer [20] ;
  59.  
  60.      WinQueryDlgItemText (hwnd, idItem, sizeof szBuffer, szBuffer) ;
  61.      return (FIXED) (atof (szBuffer) * 65536) ;
  62.      }
  63.  
  64. VOID SetLongItem (HWND hwnd, USHORT idItem, LONG lValue)
  65.      {
  66.      CHAR szBuffer [20] ;
  67.  
  68.      ltoa (lValue, szBuffer, 10) ;
  69.      WinSetDlgItemText (hwnd, idItem, szBuffer) ;
  70.      }
  71.  
  72. LONG GetLongItem (HWND hwnd, USHORT idItem)
  73.      {
  74.      CHAR   szBuffer [20] ;
  75.  
  76.      WinQueryDlgItemText (hwnd, idItem, sizeof szBuffer, szBuffer) ;
  77.      return atol (szBuffer) ;
  78.      }
  79.           /*------------------------------
  80.              Dialog box window procedures
  81.             ------------------------------*/
  82.  
  83. MRESULT EXPENTRY AngleDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  84.      {
  85.      static PMATRIXLF pmatlf ;
  86.      FIXED            fxAngle, fxScale ;
  87.  
  88.      switch (msg)
  89.           {
  90.           case WM_INITDLG:
  91.                pmatlf = (PMATRIXLF) mp2 ;
  92.                return 0 ;
  93.  
  94.           case WM_COMMAND:
  95.                switch (COMMANDMSG(&msg)->cmd)
  96.                     {
  97.                     case DID_OK:
  98.                          fxAngle = GetFixedItem (hwnd, IDD_ANGLE) ;
  99.                          fxScale = GetFixedItem (hwnd, IDD_SCALE) ;
  100.  
  101.                          pmatlf->fxM11 = (FIXED) (fxScale * cos (6.283 *
  102.                                             fxAngle / 65536 / 360)) ;
  103.  
  104.                          pmatlf->fxM22 = pmatlf->fxM11 ;
  105.  
  106.                          pmatlf->fxM12 = (FIXED) (fxScale * sin (6.283 *
  107.                                              fxAngle / 65536 / 360)) ;
  108.  
  109.                          pmatlf->fxM21 = - pmatlf->fxM12 ;
  110.  
  111.                          WinDismissDlg (hwnd, TRUE) ;
  112.                          return 0 ;
  113.  
  114.                     case DID_CANCEL:
  115.                          WinDismissDlg (hwnd, FALSE) ;
  116.                          return 0 ;
  117.                     }
  118.                break ;
  119.           }
  120.      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  121.      }
  122.  
  123. MRESULT EXPENTRY MatrixDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  124.      {
  125.      static PMATRIXLF pmatlf ;
  126.  
  127.      switch (msg)
  128.           {
  129.           case WM_INITDLG:
  130.                pmatlf = (PMATRIXLF) mp2 ;
  131.  
  132.                SetFixedItem (hwnd, IDD_FXM11, pmatlf->fxM11) ;
  133.                SetFixedItem (hwnd, IDD_FXM12, pmatlf->fxM12) ;
  134.                SetFixedItem (hwnd, IDD_FXM21, pmatlf->fxM21) ;
  135.                SetFixedItem (hwnd, IDD_FXM22, pmatlf->fxM22) ;
  136.                
  137.                SetLongItem (hwnd, IDD_LM31, pmatlf->lM31) ;
  138.                SetLongItem (hwnd, IDD_LM32, pmatlf->lM32) ;
  139.                return 0 ;
  140.  
  141.           case WM_COMMAND:
  142.                switch (COMMANDMSG(&msg)->cmd)
  143.                     {
  144.                     case IDD_ANGLEPOP:
  145.                          if (!WinDlgBox (HWND_DESKTOP, hwnd,
  146.                                         AngleDlgProc, NULL,
  147.                                         IDD_ANGLEBOX, pmatlf))
  148.                               return 0 ;
  149.  
  150.                          SetFixedItem (hwnd, IDD_FXM11, pmatlf->fxM11) ;
  151.                          SetFixedItem (hwnd, IDD_FXM12, pmatlf->fxM12) ;
  152.                          SetFixedItem (hwnd, IDD_FXM21, pmatlf->fxM21) ;
  153.                          SetFixedItem (hwnd, IDD_FXM22, pmatlf->fxM22) ;
  154.                          return 0 ;
  155.  
  156.                     case DID_OK:
  157.                          pmatlf->fxM11 = GetFixedItem (hwnd, IDD_FXM11);
  158.                          pmatlf->fxM12 = GetFixedItem (hwnd, IDD_FXM12);
  159.                          pmatlf->fxM21 = GetFixedItem (hwnd, IDD_FXM21);
  160.                          pmatlf->fxM22 = GetFixedItem (hwnd, IDD_FXM22);
  161.  
  162.                          pmatlf->lM31 = GetLongItem (hwnd, IDD_LM31) ;
  163.                          pmatlf->lM32 = GetLongItem (hwnd, IDD_LM32) ;
  164.  
  165.                          WinDismissDlg (hwnd, TRUE) ;
  166.                          return 0 ;
  167.  
  168.                     case DID_CANCEL:
  169.                          WinDismissDlg (hwnd, FALSE) ;
  170.                          return 0 ;
  171.                     }
  172.                break ;
  173.           }
  174.      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  175.      }
  176.  
  177. MRESULT EXPENTRY AboutDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  178.      {
  179.      switch (msg)
  180.           {
  181.           case WM_COMMAND:
  182.                switch (COMMANDMSG(&msg)->cmd)
  183.                     {
  184.                     case DID_OK:
  185.                     case DID_CANCEL:
  186.                          WinDismissDlg (hwnd, TRUE) ;
  187.                          return 0 ;
  188.                     }
  189.                break ;
  190.           }
  191.      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  192.      }
  193.           /*-------------------
  194.              Drawing functions
  195.             -------------------*/
  196.  
  197. VOID DrawLine (HPS hps, SHORT x1, SHORT y1, SHORT x2, SHORT y2)
  198.      {
  199.      POINTL ptl ;
  200.  
  201.      ptl.x = x1 ; ptl.y = y1 ; GpiMove (hps, &ptl) ;
  202.      ptl.x = x2 ; ptl.y = y2 ; GpiLine (hps, &ptl) ;
  203.      }
  204.  
  205. VOID DrawRuler (HPS hps)
  206.      {
  207.      SHORT sIndex ;
  208.  
  209.      DrawLine (hps, -350,    0, 350,   0) ;
  210.      DrawLine (hps,    0, -350,   0, 350) ;
  211.  
  212.      for (sIndex = -300 ; sIndex <= 300 ; sIndex += 100)
  213.           {
  214.           DrawLine (hps, sIndex,     -5, sIndex,      5) ;
  215.           DrawLine (hps,     -5, sIndex,      5, sIndex) ;
  216.           }
  217.      }
  218.  
  219. VOID DrawFigure (HPS hps)
  220.      {
  221.      static POINTL aptlFigure [] = 
  222.                {
  223.                 -20,  100,  20, 100,  20,   90,  10,   90,   10,   85,
  224.                  20,   85,  20,  75,  35,   75,  35,   70,   20,   70,
  225.                  20,   65,   5,  65,   5,   60,  20,   60,   10,   50,
  226.                  10,   40,  25,  40,  85,  100, 100,  100,  100,   85,
  227.                  25,   10,  25, -25,  50,  -85,  75,  -85,   75, -100,
  228.                  35, -100,   0, -15, -35, -100, -75, -100,  -75,  -85,
  229.                 -50,  -85, -25, -25, -25,   10, -85,  -50, -100,  -50,
  230.                -100,  -35, -25,  40, -10,   40, -10,   50,  -20,   60,
  231.                 -20,  100
  232.                } ;
  233.      static POINTL aptlBox [] =
  234.                {
  235.                -100, -100, 100, -100, 100, 100, -100, 100
  236.                } ;
  237.  
  238.      GpiMove (hps, aptlFigure) ;
  239.      GpiPolyLine (hps, sizeof aptlFigure / sizeof aptlFigure [0] - 1L,
  240.                   aptlFigure + 1) ;
  241.  
  242.      GpiSetLineType (hps, LINETYPE_DOT) ;
  243.  
  244.      GpiMove (hps, aptlBox + 3) ;
  245.      GpiPolyLine (hps, 4L, aptlBox) ;
  246.  
  247.      GpiSetLineType (hps, LINETYPE_DEFAULT) ;
  248.      }
  249.           /*-------------------------
  250.              Client window procedure
  251.             -------------------------*/
  252.  
  253. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  254.      {
  255.      static HPS      hps ;
  256.      static MATRIXLF matlfModel ;
  257.      static POINTL   aptl [3] = { -100, -100, 100, -100, -100,  100 } ;
  258.      HDC             hdc ;
  259.      MATRIXLF        matlf ;
  260.      POINTL          ptl ;
  261.      SIZEL           sizl ;
  262.      enum            { LowerLeft, LowerRight, UpperLeft } ;
  263.  
  264.      switch (msg)
  265.           {
  266.           case WM_CREATE:
  267.                hdc = WinOpenWindowDC (hwnd) ;
  268.  
  269.                sizl.cx = sizl.cy = 0 ;
  270.                hps = GpiCreatePS (hab, hdc, &sizl,
  271.                                   PU_LOENGLISH | GPIF_DEFAULT |
  272.                                   GPIT_MICRO   | GPIA_ASSOC) ;
  273.  
  274.                GpiQueryModelTransformMatrix (hps, 9L, &matlfModel) ;
  275.                return 0 ;
  276.  
  277.           case WM_SIZE:
  278.                ptl.x = SHORT1FROMMP (mp2) / 2 ; /* Half window width  */
  279.                ptl.y = SHORT2FROMMP (mp2) / 2 ; /* Half window height */
  280.  
  281.                GpiConvert (hps, CVTC_DEVICE, CVTC_PAGE, 1L, &ptl) ;
  282.  
  283.                GpiQueryDefaultViewMatrix (hps, 9L, &matlf) ;
  284.                
  285.                matlf.lM31 = ptl.x ;
  286.                matlf.lM32 = ptl.y ;
  287.  
  288.                GpiSetDefaultViewMatrix (hps, 9L, &matlf,
  289.                                         TRANSFORM_REPLACE) ; 
  290.                return 0 ;
  291.  
  292.           case WM_BUTTON1DOWN:
  293.                ptl.x = MOUSEMSG(&msg)->x ;
  294.                ptl.y = MOUSEMSG(&msg)->y ;
  295.  
  296.                GpiConvert (hps, CVTC_DEVICE, CVTC_MODEL, 1L, &ptl) ;
  297.  
  298.                if (WinGetKeyState (HWND_DESKTOP, VK_CTRL) < 0)
  299.                     aptl [UpperLeft] = ptl ;
  300.  
  301.                else if (WinGetKeyState (HWND_DESKTOP, VK_SHIFT) < 0)
  302.                     aptl [LowerRight] = ptl ;
  303.  
  304.                else                          // neither shift nor control
  305.                     aptl [LowerLeft] = ptl ;
  306.  
  307.                matlfModel.fxM11 = 65536 * (aptl [LowerRight].x -
  308.                                            aptl [LowerLeft].x) / 200 ;
  309.                matlfModel.fxM12 = 65536 * (aptl [LowerRight].y -
  310.                                            aptl [LowerLeft].y) / 200 ;
  311.                matlfModel.fxM21 = 65536 * (aptl [UpperLeft].x -
  312.                                            aptl [LowerLeft].x) / 200 ;
  313.                matlfModel.fxM22 = 65536 * (aptl [UpperLeft].y -
  314.                                            aptl [LowerLeft].y) / 200 ;
  315.                matlfModel.lM31 = (aptl [LowerRight].x +
  316.                                   aptl [UpperLeft].x) / 2 ;
  317.                matlfModel.lM32 = (aptl [LowerRight].y +
  318.                                   aptl [UpperLeft].y) / 2 ;
  319.  
  320.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  321.                return 1 ;
  322.  
  323.           case WM_COMMAND:
  324.                switch (COMMANDMSG(&msg)->cmd)
  325.                     {
  326.                     case IDM_SELECT:
  327.                          if (WinDlgBox (HWND_DESKTOP, hwnd,
  328.                                    MatrixDlgProc, NULL,
  329.                                    IDD_MATRIXBOX, &matlfModel))
  330.                               {
  331.                               GpiSetModelTransformMatrix (hps, 9L,
  332.                                    &matlfModel, TRANSFORM_REPLACE) ;
  333.  
  334.                               aptl [LowerLeft].x  = -100 ;
  335.                               aptl [LowerLeft].y  = -100 ;
  336.                               aptl [LowerRight].x =  100 ;
  337.                               aptl [LowerRight].y = -100 ;
  338.                               aptl [UpperLeft].x  = -100 ;
  339.                               aptl [UpperLeft].y  =  100 ;
  340.  
  341.                               GpiConvert (hps, CVTC_WORLD, CVTC_MODEL,
  342.                                              3L, aptl) ;
  343.  
  344.                               WinInvalidateRect (hwnd, NULL, FALSE) ;
  345.                               }
  346.                          return 0 ;
  347.  
  348.                     case IDM_ABOUT:
  349.                          WinDlgBox (HWND_DESKTOP, hwnd, AboutDlgProc,
  350.                                     NULL, IDD_ABOUTBOX, NULL) ;
  351.                          return 0 ;
  352.                     }
  353.                break ;
  354.  
  355.           case WM_PAINT:
  356.                WinBeginPaint (hwnd, hps, NULL) ;
  357.                GpiErase (hps) ;
  358.  
  359.                GpiSetModelTransformMatrix (hps, 0L, NULL, TRANSFORM_REPLACE) ;
  360.                DrawRuler (hps) ;
  361.      
  362.                GpiSetModelTransformMatrix (hps, 9L, &matlfModel,
  363.                                            TRANSFORM_REPLACE) ;
  364.                DrawFigure (hps) ;
  365.  
  366.                WinEndPaint (hps) ;
  367.                return 0 ;
  368.  
  369.           case WM_DESTROY:
  370.                GpiDestroyPS (hps) ;
  371.                return 0 ;
  372.           }
  373.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  374.      }
  375.