home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / SMF.ZIP / ALLWND.C next >
C/C++ Source or Header  |  1990-06-14  |  10KB  |  375 lines

  1. /* allwnd.c:  Show all the fonts via their own look
  2.  *  original code by Steve Firebaugh  4/20/90
  3.  */
  4.  
  5. /* additions:
  6.  *  1.  Be clever about placing the fonts on the screen
  7.  *          based on the total number we are going to put up
  8.  *  2.  Display only the fonts which match the screen resolution.
  9.  *          optionally
  10.  *  3.  Save the window with fonts as a big bitmap once.  Later
  11.  *          just blt it back to the screen.  [ done 6/9/90 ]
  12.  */
  13.  
  14.  
  15.  
  16. #define INCL_WIN
  17. #define INCL_GPI
  18. #include <os2.h>
  19. #include <stdio.h>
  20. #include "smf.h"
  21.  
  22. /* our allwnd is a frame, subclassed after the WM_CREATE goes through.
  23.  *  we need a time to do initializations thus ...
  24.  */
  25. #define WM_USER_INIT    WM_USER+57
  26.  
  27. /* used in the font placement and mouse hitesting functions */
  28. #define XINCR   150
  29. #define YINCR    20
  30. #define PERCOL   20
  31.  
  32.  
  33. /* indent the allwnd window this much from its parent */
  34. #define  BORDER  3
  35.  
  36. VOID QueryAllFonts (HPS);       //forward declaration.  HACK!
  37.  
  38.  
  39. static  PFONTMETRICS pfm;
  40.  
  41. /* used to draw a bitmap into memory, then blit on the screen */
  42. HPS hpsMemoryBitmap = NULL;
  43.  
  44. /* size of the allwnd window.  Also used to size memory bitmap */
  45. RECTL  recWindow;
  46.  
  47. /* the allwnd window is a frame.  Subclass it and remember its windproc */
  48. PFNWP   pfnOld = NULL;
  49.  
  50.  
  51. VOID AllInit (HWND hwnd)
  52. {
  53.  
  54.     /* first set up the window */
  55.     {
  56.     WinQueryWindowRect( hwnd, &recWindow);
  57.  
  58.     recWindow.xRight = (recWindow.xRight - recWindow.xLeft - 2*BORDER);
  59.     recWindow.yTop   = (recWindow.yTop - recWindow.yBottom - 2*BORDER);
  60.  
  61.     hwndAll = WinCreateWindow (hwnd, WC_FRAME, "AllWnd", FS_BORDER ,
  62.                 BORDER, BORDER,
  63.                 (SHORT) recWindow.xRight,
  64.                 (SHORT) recWindow.yTop,
  65.                 hwnd, HWND_TOP, 999, NULL, NULL);
  66.  
  67.     pfnOld = WinSubclassWindow (hwndAll, allWndProc);
  68.     WinPostMsg (hwndAll, WM_USER_INIT, 0L, 0L);
  69.     }
  70.  
  71.  
  72.     /* now fill in the array of all FONTMETRICS */
  73.     {
  74.     LONG  nFonts, lTemp = 0;
  75.     HPS   hps;
  76.     SEL   sel;
  77.  
  78.     hps = WinGetPS (hwndAll);
  79.  
  80.     /* Determine the number of fonts. */
  81.     nFonts = GpiQueryFonts(hps, QF_PUBLIC, NULL, &lTemp,
  82.          (LONG) sizeof(FONTMETRICS), NULL);
  83.  
  84.     /* Allocate space for the font metrics. */
  85.     DosAllocSeg((USHORT) (sizeof(FONTMETRICS) * nFonts),
  86.         &sel, SEG_NONSHARED);
  87.     pfm = MAKEP(sel, 0);
  88.  
  89.     /* Retrieve the font metrics. */
  90.     GpiQueryFonts(hps, QF_PUBLIC, NULL, &nFonts,
  91.       (LONG) sizeof(FONTMETRICS), pfm);
  92.  
  93.     WinReleasePS(hps);
  94.  
  95.  
  96.     /* beginning of controversial bitmap addition. */
  97.     {
  98.     HDC hdc;
  99.     static SIZEL sizl = { 0, 0 };    /* use same page size as device */
  100.     DEVOPENSTRUC dop;
  101.  
  102.     dop.pszLogAddress = NULL;
  103.     dop.pszDriverName = (PSZ) "DISPLAY";
  104.     dop.pdriv = NULL;
  105.     dop.pszDataType = NULL;
  106.  
  107.     /* Create the memory device context. */
  108.  
  109.     hdc = DevOpenDC(hab, OD_MEMORY, "*", 4L, &dop, NULL);
  110.  
  111.     /* Create the presentation and associate the memory device context. */
  112.  
  113.     hpsMemoryBitmap = GpiCreatePS(hab, hdc, &sizl,
  114.                               PU_PELS | GPIT_MICRO | GPIA_ASSOC);
  115.  
  116.     {
  117.     BITMAPINFOHEADER bmp;
  118.     HBITMAP hbm;
  119.  
  120.     bmp.cbFix = sizeof (BITMAPINFOHEADER);
  121.     bmp.cx = (USHORT) recWindow.xRight - recWindow.xLeft;
  122.     bmp.cy = (USHORT) recWindow.yTop - recWindow.yBottom;
  123.     bmp.cPlanes = 1;
  124.     bmp.cBitCount = 4;
  125.  
  126.     hbm = GpiCreateBitmap(hpsMemoryBitmap, &bmp, 0L, NULL, NULL);
  127.  
  128.     /* Set the bitmap and draw in it. */
  129.  
  130.     GpiSetBitmap(hpsMemoryBitmap, hbm);  /* sets bitmap in device context */
  131.     // QueryAllFonts (hpsMemoryBitmap);  /* do the drawing */
  132.  
  133.  
  134.     }
  135.     }
  136.     /* end of bitmap stuff */
  137.  
  138.  
  139.  
  140.     }
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147. /* Need two (inverse) mappings.
  148.  *
  149.  *  lMatch to (x,y)
  150.  *
  151.  *        column index = lMatch div PERCOL
  152.  *        row index    = lMatch mod PERCOL
  153.  *
  154.  *        x = column index * XINCR
  155.  *        y = row index    * YINCR
  156.  *
  157.  *
  158.  *  (x,y) to lMatch
  159.  *
  160.  *        lMatch = (column index * PERCOL) +        // x contribution
  161.  *                 row index                        // y contribution
  162.  */
  163.  
  164. LONG   fnlmatchtox (LONG lMatch, LONG nFonts)
  165. {
  166.     return (LONG) XINCR * (lMatch/PERCOL);
  167. }
  168.  
  169. LONG   fnlmatchtoy (LONG lMatch, LONG nFonts)
  170. {
  171.     return (LONG) YINCR * (lMatch%PERCOL);
  172. }
  173.  
  174.  
  175. LONG   fnxytolmatch (SHORT x, SHORT y)
  176. {
  177.     LONG rowindex, colindex, lMatch;
  178.  
  179.     rowindex = y / (LONG) YINCR;
  180.     colindex = x / (LONG) XINCR;
  181.  
  182.     lMatch = colindex * PERCOL + rowindex;
  183.     return lMatch;
  184. }
  185.  
  186.  
  187.  
  188. /********************** ClientWndProc **************************/
  189. MRESULT EXPENTRY allWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  190.    {
  191.    HPS    hps;
  192.    RECTL  rcl;
  193.    static POINTL aptl[4];
  194.    static BOOL fGrabbedBitmap = FALSE;
  195.  
  196.    switch (msg) {
  197.  
  198.     case WM_USER_INIT:
  199.         aptl[0].x = aptl[0].y = aptl[2].x = aptl[2].y = 0;
  200.         aptl[1].x = aptl[3].x = recWindow.xRight;
  201.         aptl[1].y = aptl[3].y = recWindow.yTop;
  202.         break;
  203.  
  204.  
  205.     case WM_BUTTON1DOWN: {
  206.         SHORT x,y;
  207.         LONG  lMatch;
  208.         SHORT index;
  209.  
  210.         /* figure out which of the fonts was selected */
  211.         x= SHORT1FROMMP (mp1);
  212.         y= SHORT2FROMMP (mp1);
  213.         lMatch = fnxytolmatch (x,y);
  214.         index = (SHORT) lMatch -1;
  215.  
  216.         /* pass the font metric to the main window.  it passes to metric wnd */
  217.         WinPostMsg (hwndMain, WM_ALL_MAIN_FONTSELECTED,
  218.                     (MPARAM) &(pfm[index]), 0L);
  219.         WinShowWindow (hwndAll, FALSE);
  220.         } break;
  221.  
  222.     case WM_PAINT:
  223.         hps = WinBeginPaint (hwnd, NULL, &rcl);
  224.  
  225.         if (!fGrabbedBitmap) {
  226.             QueryAllFonts (hps);  /* draw to the visible window */
  227.  
  228.            GpiBitBlt(hpsMemoryBitmap,  /* slide the bits off into memory */
  229.                 hps,
  230.                 4L,
  231.                 aptl,
  232.                 ROP_SRCCOPY,
  233.                 BBO_IGNORE);
  234.  
  235.             fGrabbedBitmap = TRUE;
  236.         }
  237.         else {  /* we have the memory bitmap drawn, blit it out */
  238.  
  239.             GpiBitBlt(hps,        /* target presentation space      */
  240.                 hpsMemoryBitmap,              /* source presentation space      */
  241.                 4L,               /* four points needed to compress */
  242.                 aptl,             /* points to source and target    */
  243.                 ROP_SRCCOPY,      /* copy source replacing target   */
  244.                 BBO_IGNORE);      /* discard extra rows and columns */
  245.         }
  246.         WinEndPaint (hps);
  247.         return 0;
  248.      }
  249.    if (pfnOld != NULL) return pfnOld (hwnd, msg, mp1, mp2);
  250.    }
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. VOID QueryAllFonts (HPS hps)
  261. {
  262.     SHORT   i;
  263.     LONG cFonts, lTemp = 0L;
  264.     SEL sel;
  265.     PFONTMETRICS pfm;
  266.     CHAR    buffer [200];
  267.     LONG    nFonts;
  268.  
  269.     GpiErase (hps);
  270.  
  271.     /* Determine the number of fonts. */
  272.     nFonts = GpiQueryFonts(hps, QF_PUBLIC, NULL, &lTemp,
  273.          (LONG) sizeof(FONTMETRICS), NULL);
  274.  
  275.     /* Allocate space for the font metrics. */
  276.     DosAllocSeg((USHORT) (sizeof(FONTMETRICS) * nFonts),
  277.         &sel, SEG_NONSHARED);
  278.     pfm = MAKEP(sel, 0);
  279.  
  280.     /* Retrieve the font metrics. */
  281.     cFonts = GpiQueryFonts(hps, QF_PUBLIC, NULL, &nFonts,
  282.       (LONG) sizeof(FONTMETRICS), pfm);
  283.  
  284.  
  285.  
  286.     for (i = (SHORT) nFonts; i > 0; i--) {
  287.         /******************************/ {
  288.         USHORT ii;
  289.         POINTL ptl ;
  290.         FATTRS fat;
  291.         ptl.x = fnlmatchtox((LONG)i, nFonts);
  292.         ptl.y = fnlmatchtoy((LONG)i, nFonts);
  293.  
  294.         fat.usRecordLength = sizeof(FATTRS); /* sets size of structure      */
  295.         fat.fsSelection = 0;            /* uses default selection           */
  296.         fat.lMatch = (LONG) pfm[i-1].lMatch;
  297.         fat.idRegistry = 0;             /* uses default registry            */
  298.         fat.usCodePage = 850;           /* code-page 850                    */
  299.         fat.lAveCharWidth =
  300.         fat.lMaxBaselineExt = 0;
  301.         fat.fsType = 0;                 /* uses default type                */
  302.         fat.fsFontUse = NULL; /* does not mix with graphics  */
  303.  
  304.         for (ii=0; fat.szFacename[ii] = pfm[i-1].szFacename[ii]; ii++);
  305.  
  306.         GpiCreateLogFont(hps,           /* presentation space               */
  307.                          NULL,              /* does not use logical font name   */
  308.                          (LONG) i,       /* local identifier                 */
  309.                          &fat);             /* structure with font attributes   */
  310.  
  311.         GpiSetCharSet(hps, (LONG) i);    /* sets font for presentation space */
  312.         GpiCharStringAt(hps, &ptl, (LONG) ii, fat.szFacename);
  313.         ii = sprintf (buffer, " %d ", fat.lMatch);
  314.         GpiCharString (hps, (LONG) ii, buffer);
  315.  
  316.  
  317.         if (pfm[i-1].fsDefn & FM_DEFN_OUTLINE)
  318.              GpiCharString (hps, 3L," !V");
  319.  
  320.         } /******************************/
  321.  
  322.     }
  323.  
  324. }
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342. /* at some point, add the feature whereby the allwnd
  343.  *  screen is drawn into memory first, and then just blted
  344.  *  to the terminal.
  345.  * The following may be useful code. ??
  346.  
  347. {
  348.  
  349.  
  350. /* structures used in creating the hdc
  351. SIZEL        sizell = {0,0};
  352. DEVOPENSTRUC dopl   = { NULL, "DISPLAY",
  353.                        NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  354.  
  355.  
  356. static HDC  hdcMemory;
  357. static HPS  hpsMemory = NULL;
  358. static HBITMAP     hbm;
  359. static BITMAPINFOHEADER bmih = { sizeof (BITMAPINFOHEADER),
  360.                             400, 300,
  361.                             1, 1};
  362. BITMAPINFO  bmi;
  363. CHAR    buffer[12000];
  364.  
  365.     hdcMemory = DevOpenDC (hab, OD_MEMORY, "*", 4L, &dopl, NULL);
  366.     hpsMemory = GpiCreatePS (hab, hdcMemory, &sizell, PU_PELS |
  367.                                     GPIT_MICRO | GPIA_ASSOC);
  368.  
  369.     hbm = GpiCreateBitmap (hpsMemory, &bmih, CBM_INIT, buffer, &bmi);
  370.     QueryAllFonts(hpsMemory);
  371. }
  372.  
  373. /*
  374. */
  375.