home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / fontedit / fontedit.c < prev    next >
Text File  |  1996-01-07  |  33KB  |  997 lines

  1. #define  NOVIRTUALKEYCODES
  2.  
  3. #include "windows.h"
  4. #include <windowsx.h>
  5. #include "fontedit.h"
  6. #include "fcntl.h"
  7. #include <stdlib.h>   // Causes redefinition warnings.
  8. #include <string.h>
  9.  
  10. /****************************************************************************/
  11. /*              Shared Variables (Home base)                                */
  12. /****************************************************************************/
  13.  
  14. FARPROC lpHeaderProc;                  /* Pointer to Dialog Box Procedure */
  15. FARPROC lpReSizeProc;                  /* Pointer to Dialog Box Procedure */
  16. FARPROC lpWidthProc;                   /* Pointer to Dialog Box Procedure */
  17.  
  18. FontHeaderType font;                    /* Structure of Font File Header */
  19. LONG lSizeOfOldFontHeader = 0;                  /* Old packed header size.              */
  20. LONG lSizeOfOldFontHeader30 = 0;                /* Old 3.0 packed header size.  */
  21. LONG lSizeOfOldGlyph20 = 0;             /* Old packed 2.0 glyph info size. */
  22. LONG lSizeOfOldGlyph30 = 0;                     /* Old packed 3.0 glyph info size. */
  23. extern CHAR szFontFile[];               /* Name of Font File */
  24. extern CHAR szFontFileFull[];               /* Name of Font File */
  25. extern CHAR szNewFile[];                /* Temporary Name */
  26. extern BOOL NewFile;                    /* flag indicating if file was opened
  27.                                            by selecting NEW menu item */
  28. HCURSOR hCross;                         /* handle to a "+" shaped cursor */
  29. HANDLE hInst;                           /* Handle to Window Instance */
  30. HANDLE hgPrev;
  31. INT  swH = 0;                           /* Position in Show Window 0-100 */
  32. BYTE iChar = 65;                        /* Character being edited */
  33. BYTE jChar = 65;                        /* Last Char. of edit block */
  34. CHAR szFaceName[szNamesMax] = {""};     /* Face Name of Font */
  35. DWORD offsets[257];                     /* Offsets Table */
  36. CHAR *vrgsz[CSTRINGS];                  /* string table */
  37.  
  38. BOOL fChanged = FALSE;                  /* Note if we did anything */
  39. BOOL fLoaded = FALSE;                   /* Set if a font is loaded */
  40. BOOL fEdited = FALSE;                   /* Character was edited */
  41. INT  iFontFormat;                       /* Set to id of current font format */
  42. INT  iFontFormatPrev;                   /* Set to id of prev. font format */
  43. BOOL fReadOnly;
  44.  
  45. HWND hBox = NULL;                       /* Handle to Edit Window */
  46. HWND hFont = NULL;                      /* Handle to Show window */
  47. HDC hMemDC = NULL;                      /* Handle to Memory Display Context */
  48. HBITMAP hBitmap = NULL;                 /* Handle to our work bit map */
  49.  
  50. CHAR matBox [wBoxLim] [kBoxLim];        /* array to hold Box */
  51. WORD wBox = 10;                 /* Width of Character(s) */
  52. WORD kBox = 16;                 /* Height of Characters */
  53. DWORD kStuff;                   /* Height of font window caption etc. */
  54.  
  55. OFSTRUCT ofstrFile;             /* holds info of file - filled in by dlgopen/save */
  56. //WORD     fp = NULL;             /* global fp of current file - " " " " " " */
  57.  
  58. /*** string arrays for using stringtable ***/
  59. CHAR    szAppName[MAX_STR_LEN];
  60. CHAR    szIFN[MAX_STR_LEN];         /* illegal filename */
  61. CHAR    szFNF[MAX_STR_LEN];         /* file not found   */
  62. CHAR    szREF[MAX_STR_LEN];         /* replace existing file */
  63. CHAR    szSCC[MAX_STR_LEN];         /* save current changes */
  64. CHAR    szEOF[MAX_STR_LEN];         /* error opening file */
  65. CHAR    szECF[MAX_STR_LEN];         /* error creating file */
  66. CHAR    szFRO[MAX_STR_LEN];         /* error creating file */
  67. CHAR    szExt[MAX_STR_LEN];         /* .FNT */
  68. CHAR    szExtDesc[MAX_STR_LEN];     /* Font Files(.FNT) */
  69. CHAR    szNEWFONT[MAX_STR_LEN];     /* Font Files(.FNT) */
  70. CHAR    szFilter[MAX_STR_LEN];     /* Font Files(.FNT) */
  71.  
  72.  
  73. /* size of system font */
  74. INT     cSysWidth;
  75. INT     cSysHeight;
  76.  
  77. /****************************************************************************/
  78. /*              Local Variables                                             */
  79. /****************************************************************************/
  80.  
  81. BYTE FindMouse();               /* Find where mouse is */
  82.  
  83. HBRUSH hbrWhite;
  84. HBRUSH hbrBlack;
  85. HBRUSH hbrGray;
  86. HBRUSH hbrDkGray;
  87. HBRUSH hbrBackGround;
  88.  
  89. BYTE downChar;                          /* Where mouse was pressed */
  90. BYTE lastChar;                          /* Where mouse was last */
  91. WORD wBoxOld;                           /* So we can catch any changes */
  92. RECT rectWin;                           /* Client Rectangle */
  93. LONG origin;                           /* Position in Show Window pixels */
  94. BOOL fFirstShow = TRUE;                /* first time Show Window displayed? */
  95.  
  96. /****************************************************************************/
  97. /*              Local Functions                                             */
  98. /****************************************************************************/
  99.  
  100. VOID InvertFont(HDC, BYTE, BYTE);
  101. BOOL FontEditInit(HANDLE);
  102. VOID InitSizes(VOID);
  103.  
  104. /*****************************************************************************
  105.  * FontShowHorzScroll(hFont, code, posNew)
  106.  *
  107.  * purpose: intepret scroll message for tiny window and call scroll funtion
  108.  *          accordingly
  109.  *
  110.  * params: HWND hFont : handle to tiny window
  111.  *         int code   : scroll message
  112.  *         int posNew : thumb position
  113.  * returns: none
  114.  *
  115.  * side effects: scroll position variable altered
  116.  *
  117.  ****************************************************************************/
  118. VOID
  119. FontShowHorzScroll(
  120.     HWND hFont,
  121.     INT code,
  122.     INT posNew
  123.         )
  124. {
  125.     WORD   wChar;
  126.     RECT   rect;
  127.  
  128.     /* Get dimensions */
  129.     GetClientRect(hFont, (LPRECT)&rect);
  130.  
  131.     if (font.AvgWidth == 0 ) { // for Japan
  132.         font.AvgWidth = 1 ;
  133.         }
  134.     /* Make a fair guess as to how many characters are in the window */
  135.     /* Convert sorta to hex too. */
  136.     wChar=(WORD)(11*(rect.right-rect.left)/(font.AvgWidth*16));
  137.  
  138.     switch (code)
  139.         {
  140.         case SB_LINEUP:     /* line left */
  141.             swH -= 1;
  142.             break;
  143.         case SB_LINEDOWN:   /* line right */
  144.             swH += 1;
  145.             break;
  146.         case SB_PAGEUP:     /* page left */
  147.             swH -= wChar;
  148.             break;
  149.         case SB_PAGEDOWN:   /* page right */
  150.             swH += wChar;
  151.             break;
  152.         case SB_THUMBPOSITION:
  153.             swH = posNew;
  154.             break;
  155.         case SB_THUMBTRACK:
  156.             return;
  157.     }
  158.     ScrollFont();
  159. }
  160.  
  161. /*****************************************************************************
  162.  * FontShowPaint(hDC)
  163.  *
  164.  * purpose: repaint the little window at the bottom
  165.  *
  166.  * params:  HDC hDC : handle to display context.
  167.  *
  168.  * returns: none
  169.  *
  170.  * side effects: some scroll globals altered.
  171.  *
  172.  ****************************************************************************/
  173.  
  174. VOID
  175. FontShowPaint(
  176.         HDC hDC
  177.         )
  178. {
  179.         DWORD range, nBits;
  180.         INT nx;
  181.  
  182.     nx=GetSystemMetrics(SM_CXBORDER);
  183.     /* 320 --> 300 */
  184.     nBits = min(300, rectWin.right - rectWin.left-nx-nx) - 24;
  185.         /* Window width (pixels) */
  186.     /* 320 --> 300 */
  187.     range = max(0, (font.WidthBytes << 3) - min(300, nBits));
  188.     origin = (swH * range) / 100;               /* Global variable */
  189.     BitBlt(hDC, 12, 2, nBits, font.PixHeight,
  190.            hMemDC, origin, 0, NOTSRCCOPY);
  191.     /* Now highlight the current value of iChar */
  192.     InvertFont(hDC, iChar, jChar);
  193. }
  194.  
  195.  
  196. /*****************************************************************************
  197.  * MouseInFont(ptMouse)
  198.  *
  199.  * purpose: Select new char in tiny Show window by inverting current char
  200.  *          and un-inverting last one
  201.  *
  202.  * params:  POINT ptMouse  : mouse coordinates
  203.  *
  204.  * returns: none
  205.  *
  206.  * side effects: pixels inverted(uninverted) in bitmap.
  207.  *
  208.  ****************************************************************************/
  209. VOID
  210. MouseInFont(
  211.         POINT ptMouse                    /*  .. to get new iChar */
  212.         )
  213. {
  214.         HDC hDC;
  215.  
  216.     BoxToChar(iChar);           /* Replace edited character in Box if changed */
  217.     hDC = GetDC(hFont);
  218.     /* UnInvert present inverted region */
  219.     InvertFont(hDC, iChar, jChar);
  220.     /* Find where mouse is */
  221.     lastChar = downChar = iChar = jChar = FindMouse(ptMouse);
  222.     /* Invert Character touched */
  223.     InvertFont(hDC, iChar, jChar);
  224.     ReleaseDC(hFont, hDC);
  225. }
  226.  
  227.  
  228. /*****************************************************************************
  229.  *
  230.  * purpose:
  231.  *
  232.  * params:
  233.  *
  234.  * returns:
  235.  *
  236.  * side effects:
  237.  *
  238.  ****************************************************************************/
  239. VOID
  240. MouseMoveFont(
  241.         POINT ptMouse
  242.         )
  243.         /* Mouse raised in font */
  244.         /*  .. to get new jChar */
  245. {
  246.         UNREFERENCED_PARAMETER(ptMouse);
  247.     return;     /********* NOT YET IN USE !! ********/
  248.  
  249. #if 0
  250.         BYTE newChar;
  251.         HDC hDC;
  252.  
  253.     newChar = FindMouse(ptMouse);               /* Find where mouse is */
  254.     if (newChar == lastChar)            /* Did we move ? */
  255.         return;                         /* No: return */
  256.     lastChar = newChar;
  257.     hDC = GetDC(hFont);
  258.     if (newChar > jChar)
  259.         {
  260.         InvertFont(hDC, jChar + 1, newChar);    /* extend jChar */
  261.         jChar = newChar;
  262.         }
  263.     else if (newChar < iChar)           /* iChar & jChar switch */
  264.         {
  265.         InvertFont(hDC, newChar, iChar - 1);
  266.         iChar = newChar;
  267.         }
  268.     else
  269.         {
  270.         if (newChar >= downChar)        /* width reduced on right */
  271.             {
  272.             InvertFont(hDC, newChar + 1, jChar);
  273.             jChar = newChar;
  274.             }
  275.         if (newChar <= downChar)        /* width reduced on left */
  276.             {
  277.             InvertFont(hDC, iChar, newChar - 1);
  278.             iChar = newChar;
  279.             }
  280.         }
  281.     ReleaseDC(hFont, hDC);
  282. #endif
  283. }
  284.  
  285.  
  286. /*****************************************************************************
  287.  * InvertFont(hDC, iChar, jChar)
  288.  *
  289.  * purpose: inverts color on selected chars (iChar thro' jChar) in the Show
  290.  *          Window
  291.  *
  292.  * params:  HDC hDC    : handle to display context
  293.  *          char iChar : start char
  294.  *          char jChar : end char
  295.  *
  296.  * returns: none
  297.  *
  298.  * side effects: changes pixel values in bitmap
  299.  *
  300.  ****************************************************************************/
  301. VOID
  302. InvertFont(
  303.         HDC hDC,
  304.         BYTE iChar,
  305.         BYTE jChar
  306.         )
  307. {
  308.     PatBlt(hDC,                         /* Use Blt to Invert it */
  309.         12 + offsets[iChar] - origin,   /* X: Position */
  310.         2,                              /* Y: Allow for top band */
  311.         offsets[jChar + 1] - offsets[iChar],    /* dx: Compute width */
  312.         font.PixHeight,                 /* dy: Look up the Height */
  313.         DSTINVERT);                     /* Z: Tell it to invert it */
  314. }
  315.  
  316.  
  317. /*****************************************************************************
  318.  * MouseOutFont(ptMouse)
  319.  *
  320.  * purpose: brings selected char to edit box and sets up tiny window for
  321.  *          repainting
  322.  *
  323.  * params:  POINT ptMouse : mouse location
  324.  *
  325.  * returns: none
  326.  *
  327.  * side effects: changes matBox(2-d array containing ready pixel info. on
  328.  *               char being edited)
  329.  *
  330.  ****************************************************************************/
  331. VOID
  332. MouseOutFont(
  333.         POINT ptMouse
  334.         )
  335.         /* Mouse raised in font */
  336.         /*  .. to get new jChar */
  337. {
  338.     MouseMoveFont(ptMouse);     /* Check on iChar and jChar */
  339.     CharToBox(iChar);
  340.     InvalidateRect(hFont, (LPRECT)NULL, TRUE);  /* Cause repaint */
  341. }
  342.  
  343.  
  344. /*****************************************************************************
  345.  * BYTE FindMouse(ptMouse)
  346.  *
  347.  * purpose: Locate number of character(in tiny window at bottom) under mouse
  348.  *
  349.  * params:  POINT ptMouse : mouse location
  350.  *
  351.  * returns: number of character
  352.  *
  353.  * side effects: alters scroll position variable
  354.  *
  355.  ****************************************************************************/
  356.  
  357. BYTE
  358. FindMouse(
  359.         POINT ptMouse
  360.         )
  361. {
  362.         int  iChar;
  363.         LONG   x;
  364.  
  365.  
  366.     x = ptMouse.x + origin - 12;        /* Horizontal position of mouse */
  367.     if (x < 0)
  368.         iChar = font.FirstChar;
  369.     else
  370.         {
  371.         iChar = x / font.AvgWidth + font.FirstChar;
  372.         /* Right if Fixed Pitch -- Best Guess if variable width */
  373.         if (!font.PixWidth)                     /* Scan for new iChar */
  374.             {
  375.             if (iChar > (int)font.LastChar)
  376.                    iChar = (int)font.LastChar;  /* Don't overshoot */
  377.  
  378.             while (iChar < (int)font.LastChar && offsets[iChar] < (DWORD)x)
  379.                 iChar++;
  380.             while (iChar > (int)font.FirstChar && offsets[iChar] > (DWORD)x)
  381.                 iChar--;
  382.             }
  383.         }
  384.  
  385.     /* Bug fix: prevent nil character from showing if mouse is pressed way
  386.        over on right side of font scroll with narrow fixed fonts. */
  387.     if (iChar > (int)font.LastChar)
  388.         iChar = (int)font.LastChar;  /* Don't overshoot */
  389.  
  390.     if (iChar < (int)font.FirstChar)
  391.         iChar = (int)font.FirstChar;  /* Don't undershoot */
  392.  
  393.     if (ptMouse.x <= 12L)
  394.         {
  395.         swH -= 1;
  396.         ScrollFont();
  397.         }
  398.     if (ptMouse.x > 308L)
  399.         {
  400.         swH += 1;
  401.         ScrollFont();
  402.         }
  403.     return (BYTE)iChar;
  404. }
  405.  
  406.  
  407. /*****************************************************************************
  408.  * ScrollFont()
  409.  *
  410.  * purpose: scrolls tiny window at bottom
  411.  *
  412.  * params:  none
  413.  *
  414.  * returns: none
  415.  *
  416.  * side effects: alters scroll position variable
  417.  *
  418.  ****************************************************************************/
  419. VOID
  420. ScrollFont(
  421.         VOID
  422.         )
  423. {
  424.         HDC hDC;
  425.  
  426.     swH = max(0, swH);
  427.     swH = min(100, swH);        /* maintain 0 - 100 range */
  428.     SetScrollPos(hFont, SB_HORZ, swH, TRUE);     /* Move thumb */
  429.     hDC = GetDC(hFont);
  430.     FontShowPaint(hDC);
  431.     ReleaseDC(hFont, hDC);
  432. }
  433.  
  434.  
  435. /*****************************************************************************
  436.  * BoxToChar(iChar)
  437.  *
  438.  * purpose: sets pixels in bitmap according to matBox(2-d array containing
  439.  *          ready pixel info. on char being edited)
  440.  *
  441.  * params:  BYTE iChar : index of char in bitmap offset array(offsets)
  442.  *
  443.  * returns: none
  444.  *
  445.  * side effects: changes font bitmap
  446.  *
  447.  ****************************************************************************/
  448. VOID
  449. BoxToChar(
  450.         BYTE iChar
  451.         )
  452. {
  453.         DWORD x, y, offset;
  454.  
  455.     if (!fEdited)
  456.         return;
  457.  
  458.     if (wBox != wBoxOld)                /* I.e if width has changed */
  459.         CharWidth(iChar, wBox);         /* .. go fix it */
  460.  
  461.     offset = offsets[iChar];
  462.     for (x = 0; x < wBox; x++)
  463.         {
  464.         for (y = 0; y < kBox; y++)
  465.             SetPixel(hMemDC, offset + x, y,
  466.                 matBox[x] [y] == TRUE ? WHITE : BLACK);
  467.         }
  468.     fEdited = FALSE;
  469. }
  470.  
  471.  
  472. /*****************************************************************************
  473.  * CharToBox(iChar)
  474.  *
  475.  * purpose: assigns matBox(2-d array containing ready pixel info. on char being
  476.  *          edited) according to pixels in portion of bitmap corresp. to char.
  477.  *
  478.  * params:  BYTE iChar : index of char in bitmap offset array(offsets)
  479.  *
  480.  * returns: none
  481.  *
  482.  * side effects: changes matBox
  483.  *
  484.  ****************************************************************************/
  485. VOID
  486. CharToBox(
  487.         BYTE iChar
  488.         )
  489. {
  490.         DWORD x, y, offset;
  491.         HMENU hMenu;
  492.  
  493.     ClearBox();
  494.     offset = offsets[iChar];
  495.     wBox = wBoxOld = (WORD) (offsets[iChar + 1] - offset); /* Edit Box width */
  496.     kBox = font.PixHeight;                              /* Edit Box Height */
  497.     for (x = 0; x < wBox; x++)
  498.         for (y = 0; y < kBox; y++)
  499.             matBox[x][y] = (BYTE) (GetPixel(hMemDC, offset + x, y) ? 1 : 0);
  500.     InvalidateRect(hBox, (LPRECT)NULL, TRUE);
  501.     fEdited = FALSE;            /* Not Changed Yet */
  502.     hMenu = GetMenu(hBox);
  503.     EnableMenuItem(hMenu, BOX_UNDO, MF_GRAYED);
  504.     EnableMenuItem(hMenu, BOX_REFRESH, MF_GRAYED);
  505. }
  506.  
  507.  
  508. /*****************************************************************************/
  509. /*              WinMain  and  Friends                                        */
  510. /*****************************************************************************/
  511.  
  512.  
  513. /* Procedure called every time a new instance of the application
  514. ** is created */
  515.  
  516. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, INT cmdShow)
  517. {
  518.         MSG    msg;
  519.         WORD   message;
  520.         INT    nx;
  521.         HANDLE hAccel;
  522.  
  523.     hInst = hInstance;
  524.     hgPrev=hPrevInstance;
  525.  
  526.  
  527.         if (!FontEditInit( hInstance ))
  528.             return FALSE;
  529.  
  530.  
  531.  
  532.     LoadString(hInstance, IDS_APPNAME, szAppName, MAX_STR_LEN);
  533.     LoadString(hInstance, IDS_IFN, szIFN, MAX_STR_LEN); /* illegal filename */
  534.     LoadString(hInstance, IDS_FNF, szFNF, MAX_STR_LEN); /* file not found   */
  535.     LoadString(hInstance, IDS_REF, szREF, MAX_STR_LEN);
  536.                         /* replace existing file */
  537.     LoadString(hInstance, IDS_SCC, szSCC, MAX_STR_LEN);
  538.                         /* save current changes */
  539.     LoadString(hInstance, IDS_EOF, szEOF, MAX_STR_LEN); /* error opening file */
  540.     LoadString(hInstance, IDS_ECF, szECF, MAX_STR_LEN);
  541.         // File is read only.
  542.     LoadString(hInstance, IDS_FRO, szFRO, MAX_STR_LEN);
  543.                         /* error creating file */
  544.     LoadString(hInstance, IDS_EXT, szExt, MAX_STR_LEN); /* default file ext. */
  545.  
  546.         // File extension description for common dialog.
  547.     LoadString(hInstance, IDS_EXTDESC, szExtDesc, MAX_STR_LEN);
  548.         // Message for new font warning box.
  549.     LoadString(hInstance, IDS_NEW_FONT, szNEWFONT, MAX_STR_LEN);
  550.     memset(szFilter, 0, MAX_STR_LEN);
  551.     LoadString(hInstance, IDS_EXTDESC, szFilter, MAX_STR_LEN);
  552.     LoadString(hInstance, IDS_STARDOTFNT, szFilter+strlen(szFilter)+1,
  553.                                 MAX_STR_LEN-(strlen(szFilter)+1));
  554.  
  555.  
  556.     ClearBox();
  557.  
  558.         /* Create a window instance of class "FontEdit" */
  559.  
  560.     hBox = CreateWindow((LPSTR) vszFontEdit,
  561.                       (LPSTR)szAppName,
  562.                       WS_TILEDWINDOW,
  563.                       56,34,
  564.                       GetSystemMetrics(SM_CXFULLSCREEN)/2,
  565.                       GetSystemMetrics(SM_CYFULLSCREEN)/2,
  566.                       (HWND)NULL,        /* no parent */
  567.                       (HMENU)NULL,       /* use class menu */
  568.                       (HANDLE)hInstance, /* handle to window instance */
  569.                       (LPSTR)NULL        /* no params to pass on */
  570.                       );
  571.  
  572.     ShowWindow(hBox, cmdShow);
  573.     UpdateWindow(hBox);
  574.     InitSizes();     /* 11/21/86 - linsh - get system char width & height */
  575.  
  576.  
  577.     GetWindowRect(hBox, (LPRECT)&rectWin);
  578.     nx=GetSystemMetrics(SM_CXBORDER);
  579.     hFont = CreateWindow((LPSTR) vszFontShow,
  580.                        (LPSTR) "",
  581.                        WS_BORDER|WS_HSCROLL|WS_CAPTION,
  582.                        rectWin.left,
  583.                        rectWin.bottom-56,
  584.                        min(300, rectWin.right - rectWin.left-nx-nx),
  585.                        50 - GetSystemMetrics(SM_CYBORDER),
  586.                        (HWND)hBox,
  587.                        (HMENU)NULL,
  588.                        (HANDLE)hInstance,
  589.                        (LPSTR)NULL
  590.                        );
  591.  
  592.  
  593.     /* Get address of Dialog Box procedure */
  594.     lpHeaderProc = MakeProcInstance((FARPROC)HeaderProc, hInstance);
  595.     lpReSizeProc = MakeProcInstance((FARPROC)ReSizeProc, hInstance);
  596.     lpWidthProc = MakeProcInstance((FARPROC)WidthProc, hInstance);
  597.  
  598.     /* Start it loading if it's not iconic */
  599.     if (!IsIconic(hBox))
  600.         {
  601.         if (lpszCmdLine[0])     /* If we have a font name use it */
  602.             {
  603.             BOOL   fDot;
  604.             INT   i;
  605.  
  606.             /* Copy the specified file and make it upper case */
  607.             lstrcpy((LPSTR)szFontFile, CharUpper((LPSTR)lpszCmdLine));
  608.  
  609.             fDot = FALSE;
  610.             nx=lstrlen((LPSTR)szFontFile);
  611.  
  612.             for (i = 0; i < nx; i++)
  613.                 {
  614.                 if (szFontFile[i] == '.')       /* Add .FNT if none */
  615.                     fDot = TRUE;
  616.  
  617.                 if (szFontFile[i] == ' ')
  618.                     szFontFile[i]=0;
  619.                 }
  620.  
  621.             if (!fDot)
  622.                 lstrcat((LPSTR)szFontFile, (LPSTR)vszDotFNT);
  623.  
  624.             lstrcpy((LPSTR)szNewFile, (LPSTR)szFontFile);
  625.  
  626.             /* Do this thing that someone forgot.  THe dialog open does it
  627.                and affects the Save As initialization. */
  628.             (HFILE)OpenFile((LPSTR)szNewFile, &ofstrFile, OF_READ);
  629.  
  630.             message = FONT_START;
  631.             }
  632.         else            /* Start by doing a regular file load */
  633.             message = FONT_LOAD;
  634.  
  635.         SetFocus(hBox);
  636.         PostMessage(hBox, WM_COMMAND, message, (LONG)0);
  637.         }
  638.  
  639.     if (!fLoaded)
  640.         ShowWindow(hFont, SW_HIDE);
  641.  
  642.     hAccel=LoadAccelerators(hInstance, "FE");
  643.  
  644.     /* Quit message will terminate application */
  645.     while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  646.         {
  647.         if (!TranslateAccelerator (hBox, hAccel, &msg))
  648.             {
  649.             TranslateMessage((LPMSG)&msg);
  650.             DispatchMessage((LPMSG)&msg);
  651.             }
  652.         }
  653.  
  654.     return msg.wParam;
  655. }
  656.  
  657. /*****************************************************************************
  658.  * int FontEditInit( hInstance )
  659.  *
  660.  * purpose: Initialises a whole lot of stuff
  661.  *
  662.  * params:  HANDLE hInstance : handle to instance of application
  663.  *
  664.  * returns:     TRUE : if everything goes off well
  665.  *             FALSE : error in initialisation
  666.  *
  667.  * side effects: a whole lot
  668.  *
  669.  ****************************************************************************/
  670.  
  671. /* Procedure called when the application is loaded */
  672. BOOL
  673. FontEditInit(
  674.         HANDLE hInstance
  675.         )
  676. {
  677.     INT cch;          /* all variables used to load srings from .RC file */
  678.     INT cchRemaining;
  679.     CHAR *pch;
  680.     HANDLE hStrings;
  681.     INT i;
  682.     CHAR ColorStr[CCHCOLORSTRING]; /* background color inf. fronm win.ini */
  683.     BYTE bR, bG, bB;  /* the three primary color values ret. from win.ini */
  684.     // CHAR cDummy;      /* dummy char to hold the " " char */
  685.     PWNDCLASS   pFontEditClass;         /* Edit Box window class */
  686.     PWNDCLASS   pFontShowClass;         /* Show Font window class */
  687.  
  688.         //
  689.         // Initialize the conversion routines to read in the old font structure
  690.         // off of disk.
  691.         //
  692.  
  693.         if (fConvStructInit () == FALSE) {
  694.  
  695.                 return FALSE;
  696.         }
  697.  
  698.     /* load strings from resource file */
  699.     if (!(pch = (CHAR *)(hStrings =
  700.                 LocalAlloc (LPTR, cchRemaining = CCHSTRINGSMAX))))
  701.           return FALSE;        /* unable to alloc. Try increasing initial
  702.                                   heapsize in .DEf file */
  703.     for (i=0; i< CSTRINGS; i++){
  704.         cch = 1+LoadString (hInstance, (WORD) i, (LPTSTR)pch, cchRemaining);
  705.         if (cch < 2){
  706.             return FALSE;
  707.                 }
  708.         vrgsz[i] = pch;
  709.         pch += cch;
  710.         if ((cchRemaining -= cch ) <= 0)
  711.             return FALSE;
  712.     }
  713.  
  714.     /* Allocate class structure in local heap */
  715.     pFontEditClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  716.     if (pFontEditClass == NULL)
  717.         return FALSE;
  718.  
  719.     /* set up some default brushes */
  720.     hbrWhite = GetStockObject( WHITE_BRUSH );
  721.     hbrBlack = GetStockObject( BLACK_BRUSH );
  722.     hbrGray  = GetStockObject( GRAY_BRUSH );
  723.     hbrDkGray  = GetStockObject( DKGRAY_BRUSH );
  724.  
  725.     /* get App. Workspace color from win.ini - LR */
  726.     if (GetProfileString(vszcolors, vszAppWorkspace, "",ColorStr,
  727.                                                        CCHCOLORSTRING)){
  728.         bR = (BYTE)atoi((const char *)strtok (ColorStr, " "));
  729.         bG = (BYTE)atoi((const char *)strtok (NULL, " "));
  730.         bB = (BYTE)atoi((const char *)strtok (NULL, "\n"));
  731.         hbrBackGround = CreateSolidBrush(RGB(bR, bG, bB));
  732.     }
  733.     else  /* set to lt. blue background */
  734.         hbrBackGround = CreateSolidBrush((LONG)0x00FF8000);
  735.  
  736.     /* get necessary resources */
  737.     pFontEditClass->hCursor  = LoadCursor( NULL, IDC_ARROW);
  738.     pFontEditClass->hIcon    = LoadIcon( hInstance,(LPSTR)vszFontEdit);
  739.     pFontEditClass->lpszMenuName = (LPSTR)vszFontEdit;
  740.     pFontEditClass->lpszClassName = (LPSTR)vszFontEdit;
  741.     pFontEditClass->hbrBackground = hbrBackGround;
  742.     pFontEditClass->hInstance     = hInstance;
  743.  
  744.     pFontEditClass->style = CS_VREDRAW | CS_HREDRAW;
  745.  
  746.     /* Register our Window Proc */
  747.     pFontEditClass->lpfnWndProc = (WNDPROC)FontEditWndProc;
  748.  
  749.     /* register this new class with WINDOWS */
  750.     if (!hgPrev)
  751.         if (!RegisterClass( (LPWNDCLASS)pFontEditClass ) )
  752.             return FALSE;   /* Initialization failed */
  753.  
  754.     /* Now repeat the performance for the Show window */
  755.  
  756.     pFontShowClass = pFontEditClass;
  757.     /* get necessary resources */
  758.     pFontShowClass->hCursor  = LoadCursor(NULL,IDC_ARROW);
  759.     pFontShowClass->hIcon    = (HICON)NULL;
  760.     pFontShowClass->lpszMenuName = (HMENU)NULL;
  761.     pFontShowClass->lpszClassName = (LPSTR)vszFontShow;
  762.     pFontShowClass->hbrBackground = hbrGray;
  763.     pFontShowClass->hInstance     = hInstance;
  764.     pFontShowClass->style = CS_VREDRAW | CS_HREDRAW;
  765.  
  766.     /* Register our Window Proc */
  767.     pFontShowClass->lpfnWndProc = (WNDPROC)FontShowWndProc;
  768.  
  769.     /* register this new class with WINDOWS */
  770.     if (!hgPrev)
  771.         if (!RegisterClass( (LPWNDCLASS)pFontShowClass ) )
  772.             return FALSE;   /* Initialization failed */
  773.  
  774.     LocalFree( (HANDLE)pFontShowClass );
  775.  
  776.     return TRUE;    /* Initialization succeeded */
  777. }
  778.  
  779. /*****************************************************************************
  780.  * InitSizes()
  781.  *
  782.  * purpose: gets system char width and height
  783.  *
  784.  * params:  none
  785.  *
  786.  * returns: none
  787.  *
  788.  * side effects: sets width and height globals
  789.  *
  790.  ****************************************************************************/
  791. VOID
  792. InitSizes(
  793.         VOID
  794.         )
  795. {
  796.     HDC hDC;
  797.     TEXTMETRIC tm;
  798.  
  799.     hDC = GetDC(hFont);
  800.     GetTextMetrics(hDC, (LPTEXTMETRIC) &tm);
  801.     cSysHeight = tm.tmHeight + tm.tmExternalLeading;;
  802.     cSysWidth  = tm.tmAveCharWidth;
  803.     ReleaseDC(hFont, hDC);
  804. }
  805.  
  806.  
  807. /*****************************************************************************
  808.  * long  APIENTRY FontShowWndProc(hFont, message, wParam, lParam)
  809.  *
  810.  * purpose: Window function for tiny window at the bottom (showing font chars)
  811.  *          Independently processes paint, scroll, keyboard and mouse
  812.  *          messages for the window
  813.  *
  814.  * params:  as for window functions
  815.  *
  816.  * side effects: lots
  817.  *
  818.  ****************************************************************************/
  819.  
  820. /* Procedures which make up the window class. */
  821. LONG  APIENTRY
  822. FontShowWndProc(
  823.         HWND   hFont,
  824.         WORD   message,
  825.         WPARAM wParam,
  826.         LPARAM lParam
  827.         )
  828. {
  829.         PAINTSTRUCT  ps;
  830.         // RECT         rectWin;
  831.         POINT        pt;
  832.  
  833.         ((pt).x = ((*((POINTS *)&(lParam)))).x, (pt).y = ((*((POINTS *)&(lParam)))).y);
  834.  
  835.     switch (message)
  836.     {
  837.  
  838.         case WM_SIZE :
  839.             switch (wParam){
  840.                 /* if main window is iconised, hide the child window */
  841.                 case SIZEICONIC :
  842.                     ShowWindow(hFont, SW_HIDE);
  843.                     break;
  844.                 default:
  845.                     return(DefWindowProc(hFont, message, wParam, lParam));
  846.                     break;
  847.             }
  848.             break;
  849.  
  850.  
  851.         case WM_PAINT:
  852.             /* Time for the window to draw itself. */
  853.             BeginPaint(hFont, (LPPAINTSTRUCT)&ps);
  854.             FontShowPaint(ps.hdc);
  855.             EndPaint(hFont, (LPPAINTSTRUCT)&ps);
  856.             break;
  857.  
  858.         /* For each of following mouse window messages, wParam contains
  859.         ** bits indicating whether or not various virtual keys are down,
  860.         ** and lParam is a POINT containing the mouse coordinates.   The
  861.         ** keydown bits of wParam are:  MK_LBUTTON (set if Left Button is
  862.         ** down); MK_RBUTTON (set if Right Button is down); MK_SHIFT (set
  863.         ** if Shift Key is down); MK_ALTERNATE (set if Alt Key is down);
  864.         ** and MK_CONTROL (set if Control Key is down). */
  865.  
  866.         case WM_LBUTTONDOWN:
  867.             MouseInFont(pt);              /* .. set iChar */
  868.             break;
  869.         case WM_LBUTTONUP:
  870.             MouseOutFont(pt);     /* .. set jChar */
  871.             break;
  872.         case WM_RBUTTONDOWN:
  873.         case WM_RBUTTONUP:
  874.         case WM_MOUSEMOVE:                      /* If mouse is down */
  875.             if (wParam & MK_LBUTTON)
  876.                 MouseMoveFont(pt);        /* .. set jChar */
  877.             break;
  878.         case WM_LBUTTONDBLCLK:
  879.         case WM_RBUTTONDBLCLK:
  880.             break;
  881.  
  882.         case WM_HSCROLL:
  883.             /* wParam contains the scroll code.
  884.             ** For the thumb movement codes, the low
  885.             ** word of lParam contain the new scroll position.
  886.             ** Possible values for wParam are: SB_LINEUP, SB_LINEDOWN,
  887.             ** SB_PAGEUP, SB_PAGEDOWN, SB_THUMBPOSITION, SB_THUMBTRACK */
  888.             /* Horizontal scroll bar input.  Parameters same as for
  889.             ** WM_HSCROLL.  UP and DOWN should be interpreted as LEFT
  890.             ** and RIGHT, respectively. */
  891.             FontShowHorzScroll(hFont, LOWORD(wParam),
  892.                                 HIWORD(wParam));
  893.             break;
  894.         case WM_CLOSE:
  895.             break;   /* don't allow this window to be closed before
  896.                         bigger one */
  897.  
  898.         default:
  899.  
  900.             /* Everything else comes here.  This call MUST exist
  901.             ** in your window proc.  */
  902.  
  903.             return(DefWindowProc(hFont, message, wParam, lParam));
  904.             break;
  905.  
  906.     }
  907.  
  908.     /* A window proc should always return something */
  909.     return(0L);
  910. }
  911.  
  912.  
  913. VOID
  914. FontRename(
  915.         CHAR * szError
  916.         )
  917. {
  918.     CHAR *szTitle[MAX_STR_LEN+MAX_FNAME_LEN];
  919.  
  920.     if (szError[0])
  921.         ErrorBox(hBox, szError);
  922.     else
  923.         {
  924.         lstrcpy((LPSTR)szFontFileFull, (LPSTR)szNewFile);
  925.         lstrcpy((LPSTR)szTitle, (LPSTR)szAppName);
  926.         lstrcat((LPSTR)szTitle, (LPSTR)vszBlankDashBlank);
  927.         lstrcat((LPSTR)szTitle, (LPSTR)szFontFile);
  928.         SetWindowText(hBox, (LPSTR)szTitle);
  929.         }
  930. }
  931.  
  932.  
  933. /*****************************************************************************
  934.  * ResizeShow()
  935.  *
  936.  * purpose:   resize tiny window at bottom (showing font chars) in proportion
  937.  *            to large window
  938.  *
  939.  * params:    none
  940.  *
  941.  * returns:   none
  942.  *
  943.  * side effects:
  944.  *
  945.  ****************************************************************************/
  946.  
  947. VOID
  948. ResizeShow(
  949.         VOID
  950.         )
  951. {
  952.         INT    height;
  953.         INT    nx;
  954.  
  955.     /* size message for hbox goes thru before hfont created. 06-Jul-1987. */
  956.     if (!hFont)
  957.         return;
  958.  
  959.     GetWindowRect(hBox, (LPRECT)&rectWin);
  960.     kStuff = GetkStuff();               /* For check on second pass */
  961.  
  962. #if 0
  963.     height = font.PixHeight + kStuff + 20;  /* 4 Height we want box */
  964. #else
  965.     height = font.PixHeight + GetSystemMetrics(SM_CYHSCROLL)
  966.                             + GetSystemMetrics(SM_CYCAPTION)
  967.                             + 4*GetSystemMetrics(SM_CYBORDER)/*20*/;     /* 4*/
  968. #endif
  969.  
  970.      nx=GetSystemMetrics(SM_CXBORDER);
  971.      MoveWindow(hFont,
  972.                 rectWin.left +nx+4,
  973.                 rectWin.bottom - (height + GetSystemMetrics(SM_CYBORDER)+2),
  974.                 min(300, rectWin.right - rectWin.left-nx-nx),
  975.                 height,
  976.                 TRUE);
  977.     if (!IsWindowVisible(hFont) && fLoaded)
  978.         ShowWindow(hFont, SW_SHOW);
  979. }
  980.  
  981.  
  982. DWORD
  983. GetkStuff(
  984.         VOID
  985.         )     /* Get size of menu, scrollbar etc. */
  986. {
  987.         RECT rect, rectWin;
  988.  
  989.     /* size message for hbox goes thru before hfont created. 06-Jul-1987. */
  990.     if (!hFont)
  991.         return 0;
  992.  
  993.     GetClientRect(hFont, (LPRECT)&rect);     /* How much do WE get? */
  994.     GetWindowRect(hFont, (LPRECT)&rectWin);
  995.     return (DWORD) (rectWin.bottom - rectWin.top) - (rect.bottom - rect.top);
  996. }
  997.