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

  1. /* MetWnd.C:
  2.  *  original code by Robert Hess for FONTWORK, 1988
  3.  *  modified by Steve Firebaugh  4/1/90
  4.  *
  5.  *  This is code to support the two FONTMETRICS windows.
  6.  *  A large amount of it is simply concerned with handling the
  7.  *   painting correctly and dealing with the scroll bars.  Note
  8.  *   that we are *not* using a list box here.
  9.  */
  10.  
  11. #define INCL_WIN
  12. #define INCL_GPI
  13. #include <os2.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include "smf.h"
  17.  
  18. #define NUMLINES 49
  19.  
  20.   /* hack.  encode a 0,1 window identifier in window id
  21.    *  i.e. one window i.d will be SF_UNIQUEID, the other
  22.    *  will be SF_UNIQUEID + 1
  23.    */
  24. #define SF_UNIQUEID     57
  25.  
  26. /* formatting strings for the various lines displayed */
  27. char szPrint[] = "%s = %s";
  28. char  iPrint[] = "%s = %d";
  29. char  aPrint[] = "%s = %ld (%d)";
  30. char  xPrint[] = "%s = 0x%4.4X";
  31. char  szTmsRmn[] = "8.Helv";
  32. int   length;
  33. char szText[80];
  34.  
  35. FONTMETRICS localFont;
  36. PSZ dcData[9] = {NULL, (PSZ)"DISPLAY"};
  37.  
  38.  
  39. metExtraBytes  meb[2];
  40. HWND hMetWnd;
  41. HWND hwndClient0, hwndClient1;
  42.  
  43.  
  44. int cdecl MetInit (HWND hwnd)
  45. {
  46.     LONG ctlData;
  47.  
  48.     if (!WinRegisterClass (hab,
  49.                "FontMet",
  50.                MetWndProc,
  51.                CS_SYNCPAINT | CS_SIZEREDRAW,
  52.                            0))
  53.     return FALSE;
  54.  
  55.     ctlData = FCF_VERTSCROLL | FCF_BORDER ;
  56.  
  57.     hMet0 = WinCreateStdWindow (hwnd ,
  58.                             NULL,
  59.                 &ctlData,
  60.                 "FontMet",
  61.                             "Metrics a",
  62.                             0L,
  63.                             NULL,
  64.                             SF_UNIQUEID + 0,
  65.                             &hwndClient0);
  66.     WinSetWindowPos (hMet0, HWND_TOP, XMET0, YMET0, CXMET, CYMET,
  67.                      SWP_MOVE | SWP_SIZE | SWP_SHOW);
  68.  
  69.     hMet1 = WinCreateStdWindow (hwnd ,
  70.                             NULL,
  71.                 &ctlData,
  72.                 "FontMet",
  73.                             "Metrics b",
  74.                             0L,
  75.                             NULL,
  76.                             SF_UNIQUEID + 1,
  77.                             &hwndClient1);
  78.  
  79.     WinSetWindowPos (hMet1, HWND_TOP, XMET1, YMET1, CXMET, CYMET,
  80.                      SWP_MOVE | SWP_SIZE | SWP_SHOW);
  81.  
  82.  
  83.     return TRUE;
  84. }
  85.  
  86.  
  87. MRESULT EXPENTRY MetWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  88. {
  89.      // CHAR          szBuffer [80] ;
  90.      HPS           hPS;
  91.      POINTL       ptl, box[TXTBOX_COUNT], temp;
  92.      LONG          actual;
  93.      SHORT         sLine, sPaintBeg, sPaintEnd, sVscrollInc ;
  94.      RECTL       rclInvalid, rctl;
  95.      BOOL       update, forceScroll;
  96.      metExtraBytes *pmeb;
  97.      USHORT         id;
  98.  
  99.     /* the frame i.d. selects between 0 & 1 in the meb array  */
  100.     id = WinQueryWindowUShort (WinQueryWindow (hwnd, QW_PARENT, FALSE),
  101.                                QWS_ID) - SF_UNIQUEID;
  102.     if (id != id % 2)  DosBeep (1000,1000);  /* just to be sure in [0,1] */
  103.     pmeb = &(meb[id]);
  104.  
  105.  
  106.     switch (msg) {
  107.  
  108.         case WM_CREATE: {
  109.                SIZEL         sizel;
  110.  
  111.                pmeb->cxChar = 8;
  112.                pmeb->cyChar = 14;
  113.                pmeb->cyDesc = 4;
  114.                pmeb->theFontSem = NULL;  /* no fontmetrics yet */
  115.  
  116.                pmeb->hwndVscroll = WinWindowFromID (
  117.                                    WinQueryWindow (hwnd, QW_PARENT, FALSE),
  118.                    FID_VERTSCROLL) ;
  119.  
  120.                pmeb->hFontDC = DevOpenDC (hab, OD_INFO,
  121.                                           (PSZ)"*", 2L, dcData, NULL);
  122.            sizel.cx = sizel.cy = 0L;
  123.                pmeb->hFontPS = GpiCreatePS (hab, pmeb->hFontDC, &sizel,
  124.                          PU_PELS | GPIT_MICRO | GPIA_ASSOC);
  125.  
  126.  
  127.                WinSetPresParam (hwnd, PP_FONTNAMESIZE,
  128.                     (ULONG) sizeof (szTmsRmn), szTmsRmn);
  129.  
  130.                }
  131.            return 0 ;
  132.  
  133.     case WM_DESTROY:
  134.             GpiDestroyPS (pmeb->hFontPS);
  135.         break;
  136.  
  137.     case WM_SIZE:
  138.                pmeb->hwndVscroll = WinWindowFromID (
  139.                                    WinQueryWindow (hwnd, QW_PARENT, FALSE),
  140.                    FID_VERTSCROLL) ;
  141.  
  142.                pmeb->cxClient = SHORT1FROMMP (mp2) - 1;
  143.                pmeb->cyClient = SHORT2FROMMP (mp2) - 1;
  144.  
  145.  
  146.                pmeb->sVscrollMax = max (0, NUMLINES - pmeb->cyClient /
  147.                                         pmeb->cyChar) ;
  148.                pmeb->sVscrollPos = min (pmeb->sVscrollPos, pmeb->sVscrollMax) ;
  149.  
  150.                WinSendMsg (pmeb->hwndVscroll, SBM_SETSCROLLBAR,
  151.                                         MPFROM2SHORT (pmeb->sVscrollPos, 0),
  152.                                         MPFROM2SHORT (0, pmeb->sVscrollMax)) ;
  153.  
  154.                WinEnableWindow (pmeb->hwndVscroll,
  155.                                         pmeb->sVscrollMax ? TRUE : FALSE) ;
  156.                return 0 ;
  157.  
  158.           case WM_CALCVALIDRECTS:
  159.                return MRFROMSHORT (CVR_ALIGNLEFT | CVR_ALIGNTOP) ;
  160.  
  161.       case WM_VSCROLL:
  162.         update = TRUE;
  163.         forceScroll = FALSE;
  164.         switch (SHORT2FROMMP (mp2))
  165.                     {
  166.                     case SB_LINEUP:
  167.                          sVscrollInc = -1 ;
  168.                          break ;
  169.  
  170.                     case SB_LINEDOWN:
  171.                          sVscrollInc = 1 ;
  172.                          break ;
  173.  
  174.                     case SB_PAGEUP:
  175.                          sVscrollInc = min (-1, -pmeb->cyClient / pmeb->cyChar) ;
  176.                          break ;
  177.  
  178.                     case SB_PAGEDOWN:
  179.                          sVscrollInc = max (1, pmeb->cyClient / pmeb->cyChar) ;
  180.                          break ;
  181.  
  182.             case SB_SLIDERTRACK:
  183.              update = FALSE;
  184.                          sVscrollInc = SHORT1FROMMP (mp2) - pmeb->sVscrollPos;
  185.              break ;
  186.  
  187.             case SB_SLIDERPOSITION:
  188.             forceScroll = TRUE;
  189.             sVscrollInc = 0;
  190.             break;
  191.  
  192.                     default:
  193.                          sVscrollInc = 0 ;
  194.                          break ;
  195.                     }
  196.  
  197.                sVscrollInc = max (- pmeb->sVscrollPos, min (sVscrollInc,
  198.                                     pmeb->sVscrollMax-pmeb->sVscrollPos));
  199.            if (sVscrollInc || forceScroll) {
  200.                     pmeb->sVscrollPos += sVscrollInc ;
  201.                     WinScrollWindow (hwnd, 0, pmeb->cyChar * sVscrollInc,
  202.                                    NULL, NULL, NULL, NULL, SW_INVALIDATERGN) ;
  203.  
  204.                     if (update) WinSendMsg (pmeb->hwndVscroll, SBM_SETPOS,
  205.                                         MPFROM2SHORT (pmeb->sVscrollPos, 0), NULL) ;
  206.                     WinUpdateWindow (hwnd) ;
  207.                     }
  208.                return 0 ;
  209.  
  210.           case WM_MAIN_MET_CHANGE:
  211.             pmeb->theFont = ( *(FONTMETRICS *)mp1);
  212.             pmeb->theFontSem = TRUE;
  213.  
  214.  
  215.             /* fall through to WM_PAINT */
  216.             WinInvalidateRect (hwnd, NULL, FALSE);
  217.  
  218.           case WM_PAINT:
  219.  
  220.               hPS = WinBeginPaint (hwnd, NULL, &rclInvalid) ;
  221.               GpiErase (hPS) ;
  222.               if ( pmeb->theFontSem == NULL) {
  223.                 WinEndPaint(hPS);
  224.                 return 0L;
  225.               }
  226.               /* else we do have a fontmetrics structure ... */
  227.  
  228.               sPaintBeg = max (0, pmeb->sVscrollPos +
  229.                              (pmeb->cyClient - (SHORT) rclInvalid.yTop) / pmeb->cyChar) ;
  230.               sPaintEnd = min (NUMLINES, pmeb->sVscrollPos +
  231.                              (pmeb->cyClient - (SHORT) rclInvalid.yBottom)
  232.                                   / pmeb->cyChar + 1) ;
  233.  
  234.            for (sLine = sPaintBeg ; sLine < sPaintEnd ; sLine++) {
  235.                     ptl.x = pmeb->cxChar/2 ;
  236.                     ptl.y = pmeb->cyClient - pmeb->cyChar *
  237.                                       (sLine + 1 - pmeb->sVscrollPos) + pmeb->cyDesc ;
  238.  
  239.                     localFont = pmeb->theFont;
  240.  
  241.             switch (sLine) {
  242.  
  243. case  0: length = sprintf (szText,szPrint, "szFamilyname", localFont.szFamilyname); break;
  244. case  1: length = sprintf (szText,szPrint, "szFacename", localFont.szFacename); break;
  245. case  2: length = sprintf (szText, iPrint, "idRegistry", localFont.idRegistry); break;
  246. case  3: length = sprintf (szText, iPrint, "usCodePage", localFont.usCodePage); break;
  247.  
  248. case  4:
  249.  
  250.     GpiQueryTextBox (pmeb->hFontPS, 1L, (PSZ)"M ", TXTBOX_COUNT, box);
  251.     actual = box[2].y - box[4].y;
  252.  
  253.     GpiResetBoundaryData (pmeb->hFontPS);
  254.     temp.x = temp.y = 0;
  255.     GpiMove (pmeb->hFontPS, &temp);
  256.     GpiCharString (pmeb->hFontPS, 1L, (PSZ)"M");
  257.     GpiQueryBoundaryData(pmeb->hFontPS, &rctl);
  258.     actual = rctl.yTop - rctl.yBottom;
  259.  
  260.     GpiSetColor (hPS, CLR_GREEN);
  261.     length = sprintf (szText, aPrint, "lEmHeight", localFont.lEmHeight, (int)actual);
  262.     break;
  263.  
  264. case  5:
  265.  
  266.     GpiQueryTextBox (pmeb->hFontPS, 1L, (PSZ)"x ", TXTBOX_COUNT, box);
  267.     actual = box[2].y - box[4].y;
  268.  
  269.     GpiResetBoundaryData (pmeb->hFontPS);
  270.     temp.x = temp.y = 0;
  271.     GpiMove (pmeb->hFontPS, &temp);
  272.     GpiCharString (pmeb->hFontPS, 1L, (PSZ)"x");
  273.     GpiQueryBoundaryData(pmeb->hFontPS, &rctl);
  274.     actual = rctl.yTop - rctl.yBottom;
  275.  
  276.     GpiSetColor (hPS, CLR_BLUE);
  277.     length = sprintf (szText, aPrint, "lXHeight", localFont.lXHeight, (int)actual);
  278.     break;
  279.  
  280. case  6:
  281.  
  282.     GpiQueryTextBox (pmeb->hFontPS, 1L, (PSZ)"\177 ", TXTBOX_COUNT, box);
  283.     actual = box[2].y - box[4].y;
  284.  
  285.     GpiResetBoundaryData (pmeb->hFontPS);
  286.     temp.x = temp.y = 0;
  287.     GpiMove (pmeb->hFontPS, &temp);
  288.     GpiCharString (pmeb->hFontPS, 1L, (PSZ)"\177");
  289.     GpiQueryBoundaryData(pmeb->hFontPS, &rctl);
  290.     actual = rctl.yTop - rctl.yBottom;
  291.  
  292.     GpiSetColor (hPS, CLR_DARKGREEN);
  293.     length = sprintf (szText, aPrint, "lMaxAscender", localFont.lMaxAscender, (int)actual);
  294.     break;
  295.  
  296. case  7:
  297.  
  298.     GpiQueryTextBox (pmeb->hFontPS, 1L, (PSZ)"\177 ", TXTBOX_COUNT, box);
  299.     actual = box[4].y - box[3].y;
  300.  
  301.     GpiResetBoundaryData (pmeb->hFontPS);
  302.     temp.x = temp.y = 0;
  303.     GpiMove (pmeb->hFontPS, &temp);
  304.     GpiCharString (pmeb->hFontPS, 1L, (PSZ)"\177");
  305.     GpiQueryBoundaryData(pmeb->hFontPS, &rctl);
  306.     actual = rctl.yTop - rctl.yBottom;
  307.  
  308.     GpiSetColor (hPS, CLR_DARKGREEN);
  309.     length = sprintf (szText, aPrint, "lMaxDescender", localFont.lMaxDescender, (int)actual);
  310.     break;
  311.  
  312. case  8:
  313.  
  314.     GpiQueryTextBox (pmeb->hFontPS, 1L, (PSZ)"d ", TXTBOX_COUNT, box);
  315.     actual = box[2].y - box[4].y;
  316.  
  317.     GpiResetBoundaryData (pmeb->hFontPS);
  318.     temp.x = temp.y = 0;
  319.     GpiMove (pmeb->hFontPS, &temp);
  320.     GpiCharString (pmeb->hFontPS, 1L, (PSZ)"\177");
  321.     GpiQueryBoundaryData(pmeb->hFontPS, &rctl);
  322.     actual = rctl.yTop - rctl.yBottom;
  323.  
  324.     GpiSetColor (hPS, CLR_RED);
  325.     length = sprintf (szText, aPrint, "lLowerCaseAscent", localFont.lLowerCaseAscent, (int)actual);
  326.     break;
  327.  
  328. case  9:
  329.  
  330.     GpiQueryTextBox (pmeb->hFontPS, 1L, (PSZ)"g ", TXTBOX_COUNT, box);
  331.     actual = box[4].y - box[3].y;
  332.  
  333.     GpiResetBoundaryData (pmeb->hFontPS);
  334.     temp.x = temp.y = 0;
  335.     GpiMove (pmeb->hFontPS, &temp);
  336.     GpiCharString (pmeb->hFontPS, 1L, (PSZ)"g");
  337.     GpiQueryBoundaryData(pmeb->hFontPS, &rctl);
  338.     actual = rctl.yTop - rctl.yBottom;
  339.  
  340.     GpiSetColor (hPS, CLR_RED);
  341.     length = sprintf (szText, aPrint, "lLowerCaseDescent", localFont.lLowerCaseDescent, (int)actual);
  342.     break;
  343.  
  344. case 10: length = sprintf (szText, iPrint, "lInternalLeading", localFont.lInternalLeading); break;
  345. case 11: length = sprintf (szText, iPrint, "lExternalLeading", localFont.lExternalLeading); break;
  346. case 12: length = sprintf (szText, iPrint, "lAveCharWidth", localFont.lAveCharWidth); break;
  347. case 13: length = sprintf (szText, iPrint, "lMaxCharInc", localFont.lMaxCharInc); break;
  348. case 14: length = sprintf (szText, iPrint, "lEmInc", localFont.lEmInc); break;
  349. case 15: length = sprintf (szText, iPrint, "lMaxBaselineExt", localFont.lMaxBaselineExt); break;
  350. case 16: length = sprintf (szText, iPrint, "sCharSlope", localFont.sCharSlope); break;
  351. case 17: length = sprintf (szText, iPrint, "sInLineDir", localFont.sInlineDir); break;
  352. case 18: length = sprintf (szText, iPrint, "sCharRot", localFont.sCharRot); break;
  353. case 19: length = sprintf (szText, iPrint, "usWeightClass", localFont.usWeightClass); break;
  354. case 20: length = sprintf (szText, iPrint, "usWidthClass", localFont.usWidthClass); break;
  355. case 21: length = sprintf (szText, iPrint, "sXDeviceRes", localFont.sXDeviceRes); break;
  356. case 22: length = sprintf (szText, iPrint, "sYDeviceRes", localFont.sYDeviceRes); break;
  357. case 23: length = sprintf (szText, iPrint, "sFirstChar", localFont.sFirstChar); break;
  358. case 24: length = sprintf (szText, iPrint, "sLastChar", localFont.sLastChar); break;
  359. case 25: length = sprintf (szText, iPrint, "sDefaultChar", localFont.sDefaultChar); break;
  360. case 26: length = sprintf (szText, iPrint, "sBreakChar", localFont.sBreakChar); break;
  361. case 27: length = sprintf (szText, iPrint, "sNominalPointSize", localFont.sNominalPointSize); break;
  362. case 28: length = sprintf (szText, iPrint, "sMinimumPointSize", localFont.sMinimumPointSize); break;
  363. case 29: length = sprintf (szText, iPrint, "sMaximumPointSize", localFont.sMaximumPointSize); break;
  364. case 30: length = sprintf (szText, xPrint, "fsType", localFont.fsType); break;
  365. case 31: length = sprintf (szText, xPrint, "fsDefn", localFont.fsDefn); break;
  366. case 32: length = sprintf (szText, xPrint, "fsSelection", localFont.fsSelection); break;
  367. case 33: length = sprintf (szText, xPrint, "fsCapabilities", localFont.fsCapabilities); break;
  368. case 34: length = sprintf (szText, iPrint, "lSubscriptXSize", localFont.lSubscriptXSize); break;
  369. case 35: length = sprintf (szText, iPrint, "lSubscriptYSize", localFont.lSubscriptYSize); break;
  370. case 36: length = sprintf (szText, iPrint, "lSubscriptXOffset", localFont.lSubscriptXOffset); break;
  371. case 37: length = sprintf (szText, iPrint, "lSubscriptYOffset", localFont.lSubscriptYOffset); break;
  372. case 38: length = sprintf (szText, iPrint, "lSupersciptXSize", localFont.lSuperscriptXSize); break;
  373. case 39: length = sprintf (szText, iPrint, "lSuperscriptYSize", localFont.lSuperscriptYSize); break;
  374. case 40: length = sprintf (szText, iPrint, "lSuperscriptXOffset", localFont.lSuperscriptXOffset); break;
  375. case 41: length = sprintf (szText, iPrint, "lSupscriptYOffset", localFont.lSuperscriptYOffset); break;
  376. case 42: length = sprintf (szText, iPrint, "lUnderscoreSize", localFont.lUnderscoreSize); break;
  377. case 43: length = sprintf (szText, iPrint, "lUnderscorePosition", localFont.lUnderscorePosition); break;
  378. case 44: length = sprintf (szText, iPrint, "lStrikeoutSize", localFont.lStrikeoutSize); break;
  379. case 45: length = sprintf (szText, iPrint, "lStrikeoutPosition", localFont.lStrikeoutPosition); break;
  380. case 46: length = sprintf (szText, iPrint, "sKerningPairs", localFont.sKerningPairs); break;
  381. case 47: length = sprintf (szText, iPrint, "sFamilyClass", localFont.sFamilyClass); break;
  382. case 48: length = sprintf (szText, iPrint, "lMatch", localFont.lMatch);
  383.  
  384.             }
  385.  
  386.                     GpiCharStringAt (hPS, &ptl, (LONG)length, szText);
  387.             GpiSetColor (hPS, CLR_BLACK);
  388.            }
  389.  
  390.            WinEndPaint (hPS) ;
  391.                return 0 ;
  392.           }
  393.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  394.      }
  395.