home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk11 / petzold / chap04 / sysvals3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-20  |  8.8 KB  |  241 lines

  1. /*---------------------------------------------------
  2.    SYSVALS3.C -- System Values Display Program No. 3
  3.   ---------------------------------------------------*/
  4.  
  5. #define INCL_WIN
  6. #define INCL_GPI
  7. #include <os2.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "sysvals.h"
  11.  
  12. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  13.  
  14. int main (void)
  15.      {
  16.      static CHAR  szClientClass [] = "SysVals3" ;
  17.      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU  |
  18.                                  FCF_SIZEBORDER    | FCF_MINMAX   |
  19.                                  FCF_SHELLPOSITION | FCF_TASKLIST |
  20.                                  FCF_VERTSCROLL    | FCF_HORZSCROLL ;
  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.      WinSendMsg (hwndFrame, WM_SETICON,
  36.                  WinQuerySysPointer (HWND_DESKTOP, SPTR_APPICON, FALSE),
  37.                  NULL) ;
  38.  
  39.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  40.           WinDispatchMsg (hab, &qmsg) ;
  41.  
  42.      WinDestroyWindow (hwndFrame) ;
  43.      WinDestroyMsgQueue (hmq) ;
  44.      WinTerminate (hab) ;
  45.      return 0 ;
  46.      }
  47.  
  48. LONG RtJustCharStringAt (HPS hps, POINTL *pptl, LONG lLength, CHAR *pchText)
  49.      {
  50.      POINTL aptlTextBox[TXTBOX_COUNT] ;
  51.  
  52.      GpiQueryTextBox (hps, lLength, pchText, TXTBOX_COUNT, aptlTextBox) ;
  53.  
  54.      pptl->x -= aptlTextBox[TXTBOX_CONCAT].x ;
  55.  
  56.      return GpiCharStringAt (hps, pptl, lLength, pchText) ;
  57.      }
  58.  
  59. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  60.      {
  61.      static HWND  hwndHscroll, hwndVscroll ;
  62.      static SHORT sHscrollMax, sVscrollMax, sHscrollPos, sVscrollPos,
  63.                   cxChar, cxCaps, cyChar, cyDesc, cxClient, cyClient,
  64.                   cxTextTotal ;
  65.      CHAR         szBuffer [10] ;
  66.      FONTMETRICS  fm ;
  67.      HPS          hps ;
  68.      POINTL       ptl ;
  69.      SHORT        sLine, sPaintBeg, sPaintEnd, sHscrollInc, sVscrollInc ;
  70.      RECTL        rclInvalid ;
  71.  
  72.      switch (msg)
  73.           {
  74.           case WM_CREATE:
  75.                hps = WinGetPS (hwnd) ;
  76.                GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
  77.  
  78.                cxChar = (SHORT) fm.lAveCharWidth ;
  79.                cxCaps = (SHORT) fm.lEmInc ;
  80.                cyChar = (SHORT) fm.lMaxBaselineExt ;
  81.                cyDesc = (SHORT) fm.lMaxDescender ;
  82.  
  83.                WinReleasePS (hps) ;
  84.  
  85.                cxTextTotal = 28 * cxCaps + 38 * cxChar ;
  86.  
  87.                hwndHscroll = WinWindowFromID (
  88.                                    WinQueryWindow (hwnd, QW_PARENT, FALSE),
  89.                                    FID_HORZSCROLL) ;
  90.  
  91.                hwndVscroll = WinWindowFromID (
  92.                                    WinQueryWindow (hwnd, QW_PARENT, FALSE),
  93.                                    FID_VERTSCROLL) ;
  94.                return 0 ;
  95.  
  96.           case WM_SIZE:
  97.                cxClient = SHORT1FROMMP (mp2) ;
  98.                cyClient = SHORT2FROMMP (mp2) ;
  99.  
  100.                sHscrollMax = max (0, cxTextTotal - cxClient) ;
  101.                sHscrollPos = min (sHscrollPos, sHscrollMax) ;
  102.  
  103.                WinSendMsg (hwndHscroll, SBM_SETSCROLLBAR,
  104.                                         MPFROM2SHORT (sHscrollPos, 0),
  105.                                         MPFROM2SHORT (0, sHscrollMax)) ;
  106.  
  107.                WinEnableWindow (hwndHscroll, sHscrollMax ? TRUE : FALSE) ;
  108.  
  109.                sVscrollMax = max (0, NUMLINES - cyClient / cyChar) ;
  110.                sVscrollPos = min (sVscrollPos, sVscrollMax) ;
  111.  
  112.                WinSendMsg (hwndVscroll, SBM_SETSCROLLBAR,
  113.                                         MPFROM2SHORT (sVscrollPos, 0),
  114.                                         MPFROM2SHORT (0, sVscrollMax)) ;
  115.  
  116.                WinEnableWindow (hwndVscroll, sVscrollMax ? TRUE : FALSE) ;
  117.                return 0 ;
  118.  
  119.           case WM_HSCROLL:
  120.                switch (SHORT2FROMMP (mp2))
  121.                     {
  122.                     case SB_LINELEFT:
  123.                          sHscrollInc = -cxCaps ;
  124.                          break ;
  125.  
  126.                     case SB_LINERIGHT:
  127.                          sHscrollInc = cxCaps ;
  128.                          break ;
  129.  
  130.                     case SB_PAGELEFT:
  131.                          sHscrollInc = -8 * cxCaps ;
  132.                          break ;
  133.  
  134.                     case SB_PAGERIGHT:
  135.                          sHscrollInc = 8 * cxCaps ;
  136.                          break ;
  137.  
  138.                     case SB_SLIDERPOSITION:
  139.                          sHscrollInc = SHORT1FROMMP (mp2) - sHscrollPos;
  140.                          break ;
  141.  
  142.                     default:
  143.                          sHscrollInc = 0 ;
  144.                          break ;
  145.                     }
  146.                sHscrollInc = max (-sHscrollPos,
  147.                              min (sHscrollInc, sHscrollMax - sHscrollPos)) ;
  148.  
  149.                if (sHscrollInc != 0)
  150.                     {
  151.                     sHscrollPos += sHscrollInc ;
  152.                     WinScrollWindow (hwnd, -sHscrollInc, 0,
  153.                                    NULL, NULL, NULL, NULL, SW_INVALIDATERGN) ;
  154.  
  155.                     WinSendMsg (hwndHscroll, SBM_SETPOS,
  156.                                 MPFROMSHORT (sHscrollPos), NULL) ;
  157.                     }
  158.                return 0 ;
  159.  
  160.           case WM_VSCROLL:
  161.                switch (SHORT2FROMMP (mp2))
  162.                     {
  163.                     case SB_LINEUP:
  164.                          sVscrollInc = -1 ;
  165.                          break ;
  166.  
  167.                     case SB_LINEDOWN:
  168.                          sVscrollInc = 1 ;
  169.                          break ;
  170.  
  171.                     case SB_PAGEUP:
  172.                          sVscrollInc = min (-1, -cyClient / cyChar) ;
  173.                          break ;
  174.  
  175.                     case SB_PAGEDOWN:
  176.                          sVscrollInc = max (1, cyClient / cyChar) ;
  177.                          break ;
  178.  
  179.                     case SB_SLIDERTRACK:
  180.                          sVscrollInc = SHORT1FROMMP (mp2) - sVscrollPos;
  181.                          break ;
  182.  
  183.                     default:
  184.                          sVscrollInc = 0 ;
  185.                          break ;
  186.                     }
  187.                sVscrollInc = max (-sVscrollPos,
  188.                              min (sVscrollInc, sVscrollMax - sVscrollPos)) ;
  189.  
  190.                if (sVscrollInc != 0)
  191.                     {
  192.                     sVscrollPos += sVscrollInc ;
  193.                     WinScrollWindow (hwnd, 0, cyChar * sVscrollInc,
  194.                                    NULL, NULL, NULL, NULL, SW_INVALIDATERGN) ;
  195.  
  196.                     WinSendMsg (hwndVscroll, SBM_SETPOS,
  197.                                 MPFROMSHORT (sVscrollPos), NULL) ;
  198.  
  199.                     WinUpdateWindow (hwnd) ;
  200.                     }
  201.                return 0 ;
  202.  
  203.           case WM_PAINT:
  204.                hps = WinBeginPaint (hwnd, NULL, &rclInvalid) ;
  205.                GpiErase (hps) ;
  206.  
  207.                sPaintBeg = max (0, sVscrollPos +
  208.                               (cyClient - (SHORT) rclInvalid.yTop) / cyChar) ;
  209.                sPaintEnd = min (NUMLINES, sVscrollPos +
  210.                               (cyClient - (SHORT) rclInvalid.yBottom)
  211.                                    / cyChar + 1) ;
  212.  
  213.                for (sLine = sPaintBeg ; sLine < sPaintEnd ; sLine++)
  214.                     {
  215.                     ptl.x = cxCaps - sHscrollPos ;
  216.                     ptl.y = cyClient - cyChar * (sLine + 1 - sVscrollPos)
  217.                                      + cyDesc ;
  218.  
  219.                     GpiCharStringAt (hps, &ptl,
  220.                               (LONG) strlen (sysvals[sLine].szIdentifier),
  221.                               sysvals[sLine].szIdentifier) ;
  222.  
  223.                     ptl.x += 20 * cxCaps ;
  224.                     GpiCharStringAt (hps, &ptl,
  225.                               (LONG) strlen (sysvals[sLine].szDescription),
  226.                               sysvals[sLine].szDescription) ;
  227.  
  228.                     ltoa (WinQuerySysValue (HWND_DESKTOP,
  229.                                sysvals[sLine].sIndex), szBuffer, 10) ;
  230.  
  231.                     ptl.x += 38 * cxChar + 6 * cxCaps ;
  232.                     RtJustCharStringAt (hps, &ptl, (LONG) strlen (szBuffer),
  233.                                         szBuffer) ;
  234.                     }
  235.                WinEndPaint (hps) ;
  236.                return 0 ;
  237.           }
  238.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  239.      }
  240. 
  241.