home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / FNTLST.ZIP / FONTLIST.C < prev    next >
Text File  |  1989-04-26  |  13KB  |  313 lines

  1. /*--------------------------------------------------------------
  2.    FONTLIST.C -- PM Font List Program (c) 1989, Charles Petzold
  3.   --------------------------------------------------------------*/
  4.  
  5. #define INCL_WIN
  6. #define INCL_GPI
  7. #include <os2.h>
  8. #include <limits.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #define LCID_MYFONT 1L
  13.  
  14. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  15.  
  16. int main (void)
  17.      {
  18.      static CHAR  szClientClass [] = "FontList" ;
  19.      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU  |
  20.                                  FCF_SIZEBORDER    | FCF_MINMAX   |
  21.                                  FCF_SHELLPOSITION | FCF_TASKLIST |
  22.                                  FCF_VERTSCROLL ;
  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.      if (hwndFrame != NULL)
  38.           {
  39.           WinSendMsg (hwndFrame, WM_SETICON,
  40.                       WinQuerySysPointer (HWND_DESKTOP, SPTR_APPICON, FALSE),
  41.                       NULL) ;
  42.  
  43.           while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  44.                WinDispatchMsg (hab, &qmsg) ;
  45.  
  46.           WinDestroyWindow (hwndFrame) ;
  47.           }
  48.      else
  49.           WinMessageBox (HWND_DESKTOP, HWND_DESKTOP,
  50.                          "Cannot allocate memory for FONTMETRICS structures.",
  51.                          szClientClass, 0, MB_OK | MB_ICONEXCLAMATION) ;
  52.  
  53.      WinDestroyMsgQueue (hmq) ;
  54.      WinTerminate (hab) ;
  55.      return 0 ;
  56.      }
  57.  
  58. LONG RightJustifyStringAt (HPS hps, POINTL *pptl, LONG lLength, CHAR *pchText)
  59.      {
  60.      POINTL aptlTextBox[TXTBOX_COUNT] ;
  61.  
  62.      GpiQueryTextBox (hps, lLength, pchText, TXTBOX_COUNT, aptlTextBox) ;
  63.      pptl->x -= aptlTextBox[TXTBOX_CONCAT].x ;
  64.      return GpiCharStringAt (hps, pptl, lLength, pchText) ;
  65.      }
  66.  
  67. VOID ShowFontMetrics (HPS hps, PFONTMETRICS pfmIn, SHORT cyClient)
  68.      {
  69.      static FONTMETRICS  fm ;
  70.      static struct {
  71.                    SHORT sCol ;
  72.                    SHORT sRow ;
  73.                    CHAR  *szLabel ;
  74.                    SHORT sFormat ;
  75.                    VOID  *pItem ;
  76.                    }
  77.                    disp [] =
  78.                    {
  79.                    0,  0, "szFamilyname"       , 0,  fm.szFamilyname       ,
  80.                    0,  1, "szFacename"         , 0,  fm.szFacename         ,
  81.                    0,  2, "idRegistry"         , 1, &fm.idRegistry         ,
  82.                    0,  3, "usCodePage"         , 2, &fm.usCodePage         ,
  83.                    0,  4, "lEmHeight"          , 3, &fm.lEmHeight          ,
  84.                    0,  5, "lXHeight"           , 3, &fm.lXHeight           ,
  85.                    0,  6, "lMaxAscender"       , 3, &fm.lMaxAscender       ,
  86.                    0,  7, "lMaxDescender"      , 3, &fm.lMaxDescender      ,
  87.                    0,  8, "lLowerCaseAscent"   , 3, &fm.lLowerCaseAscent   ,
  88.                    0,  9, "lLowerCaseDescent"  , 3, &fm.lLowerCaseDescent  ,
  89.                    0, 10, "lInternalLeading"   , 3, &fm.lInternalLeading   ,
  90.                    0, 11, "lExternalLeading"   , 3, &fm.lExternalLeading   ,
  91.                    0, 12, "lAveCharWidth"      , 3, &fm.lAveCharWidth      ,
  92.                    0, 13, "lMaxCharInc"        , 3, &fm.lMaxCharInc        ,
  93.                    0, 14, "lEmInc"             , 3, &fm.lEmInc             ,
  94.                    0, 15, "lMaxBaselineExt"    , 3, &fm.lMaxBaselineExt    ,
  95.                    0, 16, "sCharSlope"         , 2, &fm.sCharSlope         ,
  96.                    1,  2, "sInlineDir"         , 2, &fm.sInlineDir         ,
  97.                    1,  3, "sCharRot"           , 2, &fm.sCharRot           ,
  98.                    1,  4, "usWeightClass"      , 1, &fm.usWeightClass      ,
  99.                    1,  5, "usWidthClass"       , 1, &fm.usWidthClass       ,
  100.                    1,  6, "sXDeviceRes"        , 2, &fm.sXDeviceRes        ,
  101.                    1,  7, "sYDeviceRes"        , 2, &fm.sYDeviceRes        ,
  102.                    1,  8, "sFirstChar"         , 2, &fm.sFirstChar         ,
  103.                    1,  9, "sLastChar"          , 2, &fm.sLastChar          ,
  104.                    1, 10, "sDefaultChar"       , 2, &fm.sDefaultChar       ,
  105.                    1, 11, "sBreakChar"         , 2, &fm.sBreakChar         ,
  106.                    1, 12, "sNominalPointSize"  , 2, &fm.sNominalPointSize  ,
  107.                    1, 13, "sMinimumPointSize"  , 2, &fm.sMinimumPointSize  ,
  108.                    1, 14, "sMaximumPointSize"  , 2, &fm.sMaximumPointSize  ,
  109.                    1, 15, "fsType"             , 1, &fm.fsType             ,
  110.                    1, 16, "fsDefn"             , 1, &fm.fsDefn             ,
  111.                    2,  0, "fsSelection"        , 1, &fm.fsSelection        ,
  112.                    2,  1, "fsCapabilities"     , 1, &fm.fsCapabilities     ,
  113.                    2,  2, "lSubscriptXSize"    , 3, &fm.lSubscriptXSize    ,
  114.                    2,  3, "lSubscriptYSize"    , 3, &fm.lSubscriptYSize    ,
  115.                    2,  4, "lSubscriptXOffset"  , 3, &fm.lSubscriptXOffset  ,
  116.                    2,  5, "lSubscriptYOffset"  , 3, &fm.lSubscriptYOffset  ,
  117.                    2,  6, "lSuperscriptXSize"  , 3, &fm.lSuperscriptXSize  ,
  118.                    2,  7, "lSuperscriptYSize"  , 3, &fm.lSuperscriptYSize  ,
  119.                    2,  8, "lSuperscriptXOffset", 3, &fm.lSuperscriptXOffset,
  120.                    2,  9, "lSuperscriptYOffset", 3, &fm.lSuperscriptYOffset,
  121.                    2, 10, "lUnderscoreSize"    , 3, &fm.lUnderscoreSize    ,
  122.                    2, 11, "lUnderscorePosition", 3, &fm.lUnderscorePosition,
  123.                    2, 12, "lStrikeoutSize"     , 3, &fm.lStrikeoutSize     ,
  124.                    2, 13, "lStrikeoutPosition" , 3, &fm.lStrikeoutPosition ,
  125.                    2, 14, "sKerningPairs"      , 2, &fm.sKerningPairs      ,
  126.                    2, 15, "sReserved"          , 2, &fm.sReserved          ,
  127.                    2, 16, "lMatch"             , 3, &fm.lMatch
  128.                    } ;
  129.      CHAR                szBuffer [32] ;
  130.      INT                 i ;
  131.      LONG                cxChar, cyChar, cyDesc ;
  132.      POINTL              ptl ;
  133.  
  134.      GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
  135.      cxChar = fm.lAveCharWidth ;
  136.      cyChar = fm.lMaxBaselineExt ;
  137.      cyDesc = fm.lMaxDescender ;
  138.  
  139.      fm = *pfmIn ;
  140.  
  141.      for (i = 0 ; i < sizeof disp / sizeof disp[0] ; i++)
  142.           {
  143.           ptl.x = 32 * cxChar * disp[i].sCol ;
  144.           ptl.y = cyClient - cyChar * (1 + disp[i].sRow) + cyDesc ;
  145.  
  146.           GpiCharStringAt (hps, &ptl, (LONG) strlen (disp[i].szLabel),
  147.                           disp[i].szLabel) ;
  148.  
  149.           switch (disp[i].sFormat)
  150.                {
  151.                case 0:                       // CHAR
  152.                     ptl.x += 20 * cxChar ;
  153.                     GpiCharStringAt (hps, &ptl,
  154.                                      (LONG) strlen (disp[i].pItem),
  155.                                      disp[i].pItem) ;
  156.                     break ;
  157.  
  158.                case 1:                       // USHORT
  159.                     ptl.x += 30 * cxChar ;
  160.                     RightJustifyStringAt (hps, &ptl,
  161.                          (LONG) sprintf (szBuffer, "%u",
  162.                                          * (USHORT *) disp[i].pItem),
  163.                          szBuffer) ;
  164.                     break ;
  165.  
  166.                case 2:                       // SHORT
  167.                     ptl.x += 30 * cxChar ;
  168.                     RightJustifyStringAt (hps, &ptl,
  169.                          (LONG) sprintf (szBuffer, "%u",
  170.                                          * (SHORT *) disp[i].pItem),
  171.                          szBuffer) ;
  172.                     break ;
  173.  
  174.                case 3:                       // LONG
  175.                     ptl.x += 30 * cxChar ;
  176.                     RightJustifyStringAt (hps, &ptl,
  177.                          (LONG) sprintf (szBuffer, "%u",
  178.                                          * (LONG *) disp[i].pItem),
  179.                          szBuffer) ;
  180.                     break ;
  181.                }
  182.           }
  183.      }
  184.  
  185. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  186.      {
  187.      static FATTRS      fat ;
  188.      static HWND        hwndVScroll ;
  189.      static FONTMETRICS *pfm ;
  190.      static SHORT       cxClient, cyClient ;
  191.      static USHORT      usNumberFonts, usCurrentFont ;
  192.      HPS                hps ;
  193.      LONG               lRequestFonts, lNumberFonts ;
  194.      POINTL             ptl ;
  195.  
  196.      switch (msg)
  197.           {
  198.           case WM_CREATE:
  199.                               // Get all FONTMETRICS structures in pfm
  200.  
  201.                hps = WinGetPS (hwnd) ;
  202.                lRequestFonts = 0 ;
  203.                lNumberFonts = GpiQueryFonts (hps, QF_PUBLIC, NULL,
  204.                                              &lRequestFonts, 0L, NULL) ;
  205.  
  206.                if (lNumberFonts > USHRT_MAX)
  207.                     return 1 ;
  208.  
  209.                if (lNumberFonts * sizeof (FONTMETRICS) > UINT_MAX)
  210.                     return 1 ;
  211.  
  212.                pfm = malloc ((UINT) (lNumberFonts * sizeof (FONTMETRICS))) ;
  213.  
  214.                if (pfm == NULL)
  215.                     return 1 ;
  216.  
  217.                GpiQueryFonts (hps, QF_PUBLIC, NULL, &lNumberFonts,
  218.                               (LONG) sizeof (FONTMETRICS), pfm) ;
  219.  
  220.                usNumberFonts = (USHORT) lNumberFonts ;
  221.                WinReleasePS (hps) ;
  222.  
  223.                               // Initialize scroll bar
  224.  
  225.                hwndVScroll = WinWindowFromID (
  226.                                  WinQueryWindow (hwnd, QW_PARENT, FALSE),
  227.                                  FID_VERTSCROLL) ;
  228.  
  229.                WinSendMsg (hwndVScroll, SBM_SETSCROLLBAR,
  230.                            MPFROM2SHORT (usCurrentFont, 0),
  231.                            MPFROM2SHORT (0, usNumberFonts - 1)) ;
  232.                return 0 ;
  233.  
  234.           case WM_SIZE:
  235.                cxClient = SHORT1FROMMP (mp2) ;
  236.                cyClient = SHORT2FROMMP (mp2) ;
  237.                return 0 ;
  238.  
  239.           case WM_CHAR:
  240.                return WinSendMsg (hwndVScroll, msg, mp1, mp2) ;
  241.  
  242.           case WM_VSCROLL:
  243.                switch (SHORT2FROMMP (mp2))
  244.                     {
  245.                     case SB_LINEUP:
  246.                          usCurrentFont -= 1 ;
  247.                          break ;
  248.  
  249.                     case SB_LINEDOWN:
  250.                          usCurrentFont += 1 ;
  251.                          break ;
  252.  
  253.                     case SB_PAGEUP:
  254.                          usCurrentFont -= 10 ;
  255.                          break ;
  256.  
  257.                     case SB_PAGEDOWN:
  258.                          usCurrentFont += 10 ;
  259.                          break ;
  260.  
  261.                     case SB_SLIDERPOSITION:
  262.                          usCurrentFont = SHORT1FROMMP (mp2) ;
  263.                          break ;
  264.  
  265.                     default:
  266.                          return 0 ;
  267.                     }
  268.                usCurrentFont = max (0, min (usCurrentFont, usNumberFonts - 1));
  269.  
  270.                if (usCurrentFont != (USHORT) WinSendMsg (hwndVScroll,
  271.                                                   SBM_QUERYPOS, NULL, NULL))
  272.                     {
  273.                     WinSendMsg (hwndVScroll, SBM_SETPOS,
  274.                                 MPFROM2SHORT (usCurrentFont, 0), NULL) ;
  275.  
  276.                     WinInvalidateRect (hwnd, NULL, FALSE) ;
  277.                     }
  278.                return 0 ;
  279.  
  280.           case WM_PAINT:
  281.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  282.                GpiErase (hps) ;
  283.  
  284.                ShowFontMetrics (hps, pfm + usCurrentFont, cyClient) ;
  285.  
  286.                fat.usRecordLength = sizeof fat ;
  287.                fat.lMatch         = pfm[usCurrentFont].lMatch ;
  288.                fat.fsSelection    = 0 ;
  289.                strcpy (fat.szFacename, pfm[usCurrentFont].szFacename) ;
  290.                GpiCreateLogFont (hps, NULL, LCID_MYFONT, &fat) ;
  291.                GpiSetCharSet (hps, LCID_MYFONT) ;
  292.  
  293.                ptl.x = 0 ;
  294.                ptl.y = pfm[usCurrentFont].lMaxDescender ;
  295.  
  296.                GpiCharStringAt (hps, &ptl,
  297.                                 pfm[usCurrentFont].fsDefn & 1 ? 6L : 52L,
  298.                                 "AaBbCcDdEeFfGgHhIiJjKkLlMm"
  299.                                 "NnOoPpQqRrSsTtUuVvWwXxYyZz") ;
  300.  
  301.                GpiSetCharSet (hps, LCID_DEFAULT) ;
  302.                GpiDeleteSetId (hps, LCID_MYFONT) ;
  303.                WinEndPaint (hps) ;
  304.                return 0 ;
  305.  
  306.           case WM_DESTROY:
  307.                free (pfm) ;
  308.                return 0 ;
  309.           }
  310.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  311.      }
  312. 
  313.