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

  1. /*----------------------------
  2.    FONTS.C -- GPI Image Fonts
  3.   ----------------------------*/
  4.  
  5. #define INCL_WIN
  6. #define INCL_GPI
  7. #include <os2.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "easyfont.h"
  11.  
  12. #define LCID_MYFONT 1L
  13.  
  14. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  15.  
  16. int main (void)
  17.      {
  18.      static CHAR  szClientClass [] = "Fonts" ;
  19.      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU  |
  20.                                  FCF_SIZEBORDER    | FCF_MINMAX   |
  21.                                  FCF_SHELLPOSITION | FCF_TASKLIST |
  22.                                  FCF_VERTSCROLL    | FCF_HORZSCROLL ;
  23.      HAB          hab ;
  24.      HMQ          hmq ;
  25.      HWND         hwndFrame, hwndClient ;
  26.      QMSG         qmsg ;
  27.  
  28.      hab = WinInitialize (0) ;
  29.      hmq = WinCreateMsgQueue (hab, 0) ;
  30.  
  31.      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
  32.  
  33.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  34.                                      &flFrameFlags, szClientClass, NULL,
  35.                                      0L, NULL, 0, &hwndClient) ;
  36.  
  37.      WinSendMsg (hwndFrame, WM_SETICON,
  38.                  WinQuerySysPointer (HWND_DESKTOP, SPTR_APPICON, FALSE),
  39.                  NULL) ;
  40.  
  41.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  42.           WinDispatchMsg (hab, &qmsg) ;
  43.  
  44.      WinDestroyWindow (hwndFrame) ;
  45.      WinDestroyMsgQueue (hmq) ;
  46.      WinTerminate (hab) ;
  47.      return 0 ;
  48.      }
  49.  
  50. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  51.      {
  52.      static CHAR   *szFace[] = { "System", "Courier",
  53.                                  "Helv",   "Tms Rmn" } ;
  54.      static CHAR   *szSize[] = { "8", "10", "12", "14", "18", "24" } ;
  55.      static CHAR   *szSel[]  = { "Normal",     "Italic",  "Underscore",
  56.                                  "Strike-out", "Bold" } ;
  57.      static CHAR   szBuffer[80] ;
  58.      static HWND   hwndVscroll, hwndHscroll ;
  59.      static USHORT idFace[] = { FONTFACE_SYSTEM, FONTFACE_COUR,
  60.                                 FONTFACE_HELV,   FONTFACE_TIMES } ;
  61.      static USHORT idSize[] = { FONTSIZE_8,  FONTSIZE_10, FONTSIZE_12,
  62.                                 FONTSIZE_14, FONTSIZE_18, FONTSIZE_24 } ;
  63.      static USHORT afsSel[] = { 0, FATTR_SEL_ITALIC,    FATTR_SEL_UNDERSCORE,
  64.                                    FATTR_SEL_STRIKEOUT, FATTR_SEL_BOLD } ;
  65.      static SHORT  sVscrollMax = sizeof idFace / sizeof idFace[0] - 1,
  66.                    sHscrollMax = sizeof afsSel / sizeof afsSel[0] - 1,
  67.                    cxClient, cyClient, sHscrollPos, sVscrollPos ;
  68.      FONTMETRICS   fm ;
  69.      HPS           hps;
  70.      HWND          hwndFrame ;
  71.      POINTL        ptl ;
  72.      SHORT         sIndex ;
  73.  
  74.      switch (msg)
  75.           {
  76.           case WM_CREATE:
  77.                hps = WinGetPS (hwnd) ;
  78.                EzfQueryFonts (hps) ;
  79.                WinReleasePS (hps) ;
  80.  
  81.                hwndFrame   = WinQueryWindow (hwnd, QW_PARENT, FALSE),
  82.                hwndVscroll = WinWindowFromID (hwndFrame, FID_VERTSCROLL) ;
  83.                hwndHscroll = WinWindowFromID (hwndFrame, FID_HORZSCROLL) ;
  84.  
  85.                WinSendMsg (hwndVscroll, SBM_SETSCROLLBAR,
  86.                            MPFROM2SHORT (sVscrollPos, 0),
  87.                            MPFROM2SHORT (0, sVscrollMax)) ;
  88.  
  89.                WinSendMsg (hwndHscroll, SBM_SETSCROLLBAR,
  90.                            MPFROM2SHORT (sHscrollPos, 0),
  91.                            MPFROM2SHORT (0, sHscrollMax)) ;
  92.                return 0 ;
  93.  
  94.           case WM_SIZE:
  95.                cxClient = SHORT1FROMMP (mp2) ;
  96.                cyClient = SHORT2FROMMP (mp2) ;
  97.                return 0 ;
  98.  
  99.           case WM_VSCROLL:
  100.                switch (SHORT2FROMMP (mp2))
  101.                     {
  102.                     case SB_LINEUP:
  103.                     case SB_PAGEUP:
  104.                          sVscrollPos = max (0, sVscrollPos - 1) ;
  105.                          break ;
  106.  
  107.                     case SB_LINEDOWN:
  108.                     case SB_PAGEDOWN:
  109.                          sVscrollPos = min (sVscrollMax, sVscrollPos + 1) ;
  110.                          break ;
  111.  
  112.                     case SB_SLIDERPOSITION:
  113.                          sVscrollPos = SHORT1FROMMP (mp2) ;
  114.                          break ;
  115.  
  116.                     default:
  117.                          return 0 ;
  118.                     }
  119.                WinSendMsg (hwndVscroll, SBM_SETPOS,
  120.                            MPFROM2SHORT (sVscrollPos, 0), NULL) ;
  121.  
  122.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  123.                return 0 ;
  124.  
  125.           case WM_HSCROLL:
  126.                switch (SHORT2FROMMP (mp2))
  127.                     {
  128.                     case SB_LINELEFT:
  129.                     case SB_PAGELEFT:
  130.                          sHscrollPos = max (0, sHscrollPos - 1) ;
  131.                          break ;
  132.  
  133.                     case SB_LINERIGHT:
  134.                     case SB_PAGERIGHT:
  135.                          sHscrollPos = min (sHscrollMax, sHscrollPos + 1) ;
  136.                          break ;
  137.  
  138.                     case SB_SLIDERPOSITION:
  139.                          sHscrollPos = SHORT1FROMMP (mp2) ;
  140.                          break ;
  141.  
  142.                     default:
  143.                          return 0 ;
  144.                     }
  145.                WinSendMsg (hwndHscroll, SBM_SETPOS,
  146.                            MPFROM2SHORT (sHscrollPos, 0), NULL) ;
  147.  
  148.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  149.                return 0 ;
  150.  
  151.           case WM_CHAR:
  152.                switch (CHARMSG(&msg)->vkey)
  153.                     {
  154.                     case VK_LEFT:
  155.                     case VK_RIGHT:
  156.                          return WinSendMsg (hwndHscroll, msg, mp1, mp2) ;
  157.                     case VK_UP:
  158.                     case VK_DOWN:
  159.                     case VK_PAGEUP:
  160.                     case VK_PAGEDOWN:
  161.                          return WinSendMsg (hwndVscroll, msg, mp1, mp2) ;
  162.                     }
  163.                break ;
  164.  
  165.           case WM_PAINT:
  166.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  167.                GpiErase (hps) ;
  168.  
  169.                ptl.x = 0 ;
  170.                ptl.y = cyClient ;
  171.  
  172.                for (sIndex = 0 ; sIndex < 6 ; sIndex++)
  173.                     if (EzfCreateLogFont (hps, LCID_MYFONT,
  174.                                           idFace[sVscrollPos],
  175.                                           idSize[sIndex],
  176.                                           afsSel[sHscrollPos]))
  177.                          {
  178.                          GpiSetCharSet (hps, LCID_MYFONT) ;
  179.                          GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
  180.  
  181.                          ptl.y -= fm.lMaxBaselineExt ;
  182.  
  183.                          GpiCharStringAt (hps, &ptl,
  184.                               (LONG) sprintf (szBuffer, "%s, %s point, %s",
  185.                                               szFace[sVscrollPos],
  186.                                               szSize[sIndex],
  187.                                               szSel[sHscrollPos]),
  188.                               szBuffer) ;
  189.  
  190.                          GpiSetCharSet (hps, LCID_DEFAULT) ;
  191.                          GpiDeleteSetId (hps, LCID_MYFONT) ;
  192.                          }
  193.  
  194.                WinEndPaint (hps) ;
  195.                return 0 ;
  196.           }
  197.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  198.      }
  199.