Annotation of mstools/samples/fontview/dialogs.c, revision 1.1.1.3

1.1       root        1: #define NOMINMAX
                      2: #include <windows.h>
                      3: #include "FontView.h"
                      4: 
                      5: #include <stdlib.h>
                      6: 
1.1.1.2   root        7: #if !defined (APIENTRY)
                      8: #define APIENTRY FAR PASCAL
1.1       root        9: #endif
                     10: 
                     11: BOOL CenterWindow (HWND, HWND);
                     12: 
                     13: 
                     14: typedef struct FONTENUM {
1.1.1.2   root       15:     short       ft;
                     16:     TEXTMETRIC  tm;
                     17:     LOGFONT     lf;
1.1       root       18: } FONTSTRUCT;
                     19: typedef FONTSTRUCT      *PFONTSTRUCT;
                     20: typedef FONTSTRUCT NEAR *NPFONTSTRUCT;
                     21: typedef FONTSTRUCT FAR  *LPFONTSTRUCT;
                     22: 
                     23: typedef struct FONTLIST {
1.1.1.2   root       24:     int     count;
                     25:     HANDLE  hList;
1.1       root       26: } FONTLIST;
                     27: typedef FONTLIST        *PFONTLIST;
                     28: typedef FONTLIST NEAR   *NPFONTLIST;
                     29: typedef FONTLIST FAR    *LPFONTLIST;
                     30: 
                     31: 
                     32: void SetDlgItemValue (HWND hDlg, int nIDDlgItem, int wValue, BOOL bSigned, int nBase)
                     33: {
1.1.1.2   root       34:     char szValue[20];
1.1       root       35: 
1.1.1.2   root       36:     switch (nBase) {
                     37:         case 8:
                     38:             wsprintf (szValue, "0o%o", wValue);
                     39:             SetDlgItemText (hDlg, nIDDlgItem, szValue);
                     40:             break;
                     41:         case 16:
                     42:             wsprintf (szValue, "0x%x", wValue);
                     43:             SetDlgItemText (hDlg, nIDDlgItem, szValue);
                     44:             break;
                     45:         default:
                     46:             SetDlgItemInt (hDlg, nIDDlgItem, wValue, bSigned);
                     47:             break;
                     48:     }
1.1       root       49: }
                     50: 
                     51: int FAR PASCAL DlgEnumFontSizes (lpLogFont, lpTextMetric, nFontType, lpData)
1.1.1.2   root       52:     LPLOGFONT lpLogFont;
                     53:     LPTEXTMETRIC lpTextMetric;
                     54:     short nFontType;
                     55:     LPHANDLE lpData;
1.1       root       56: {
1.1.1.2   root       57:     HANDLE hFonts;
                     58:     LPFONTSTRUCT pFS;
                     59:     LPFONTLIST pFL;
                     60: 
                     61:     /*
                     62:         This function will lock down the incoming handle, properly alloc, and realloc the
                     63:         handle within it to hold the data of the fonts enumerated, then unlock the handle.
                     64:     */
                     65:     hFonts = *lpData;
                     66:     pFL = (LPFONTLIST)GlobalLock (hFonts);
                     67:     if (!pFL) {
                     68:         return FALSE;
                     69:     } else if (pFL->count == 0) {
                     70:         pFL->hList = GlobalAlloc (GHND, sizeof(FONTSTRUCT));
                     71:     } else {
                     72:         pFL->hList = GlobalReAlloc (pFL->hList, sizeof(FONTSTRUCT)*(1+pFL->count), GMEM_MOVEABLE);
                     73:     }
                     74: 
                     75:     if (pFL->hList) {
                     76:         pFS = (LPFONTSTRUCT)GlobalLock (pFL->hList);
                     77:         if (pFS) {
                     78:             pFS[pFL->count].ft = nFontType;
                     79:             pFS[pFL->count].tm = *lpTextMetric;
                     80:             pFS[pFL->count].lf = *lpLogFont;
                     81:             GlobalUnlock (pFL->hList);
                     82:             pFL->count++;
                     83:         }
                     84:         GlobalUnlock (hFonts);
                     85: 
                     86:     } else {
                     87:         GlobalUnlock (hFonts);
                     88:         return FALSE;
                     89:     }
1.1       root       90: 
1.1.1.2   root       91:     return TRUE;
1.1       root       92: }
                     93: 
                     94: int FAR PASCAL DlgEnumFontNames (lpLogFont, lpTextMetric, nFontType, lpData)
1.1.1.2   root       95:     LPLOGFONT lpLogFont;
                     96:     LPTEXTMETRIC lpTextMetric;
                     97:     short nFontType;
                     98:     LPHANDLE lpData;
1.1       root       99: {
1.1.1.2   root      100:     HDC  hdc;
                    101:     HWND hwnd;
                    102:     HANDLE hInst;
                    103:     FARPROC lpFontEnumProc;
1.1       root      104: 
                    105: /*
                    106:    This function is just a pass through. For each face encountered, it will in turn enumerate all
                    107:    sizes available. the lpData, which is a FAR * to a FONTSTRUCT structure will simply be passed
                    108:    on to the second enumeration procedure which will fill it in.
                    109:  */
1.1.1.2   root      110:     hwnd = GetFocus();
                    111: #if defined (WIN32)
                    112:     hInst = (HANDLE)GetWindowLong (hwnd, GWL_HINSTANCE);
1.1       root      113: #elif defined (WIN16)
1.1.1.2   root      114:     hInst = (HANDLE)GetWindowWord (hwnd, GWW_HINSTANCE);
1.1       root      115: #endif
                    116: 
1.1.1.3 ! root      117:     lpFontEnumProc = MakeProcInstance((FARPROC)DlgEnumFontSizes, hInst);
1.1.1.2   root      118:     if (lpFontEnumProc) {
                    119:         hdc  = GetDC(hwnd);
1.1.1.3 ! root      120:        EnumFonts (hdc, lpLogFont->lfFaceName, (FONTENUMPROC)lpFontEnumProc, (LPARAM)lpData);
1.1.1.2   root      121:         ReleaseDC(hwnd, hdc);
                    122:         FreeProcInstance (lpFontEnumProc);
                    123:     } else {
                    124:         MessageBox (GetFocus(), "Couldn't create a proc instance", "FontView", MB_OK);
                    125:         return FALSE;
                    126:     }
                    127:     return TRUE;
                    128: 
                    129:     lpTextMetric;  // unreferenced formal parameter
                    130:     nFontType;     // unreferenced formal parameter
1.1       root      131: }
                    132: 
                    133: 
                    134: 
                    135: BOOL APIENTRY SimpleDlgProc(HWND hwnd, UINT msg, UINT wParam, LONG lParam)
                    136: {
1.1.1.2   root      137:     int wmId;
                    138:     static HBITMAP hbmFontView;
                    139:     static BITMAP bmFontView;
                    140:     RECT rect;
                    141:     HDC hdc, hdcSrc;
                    142:     HBITMAP hbmOld;
                    143:     PAINTSTRUCT ps;
                    144:     HANDLE hInst;
1.1       root      145: 
1.1.1.2   root      146: #if defined (WIN32)
                    147:     hInst = (HANDLE)GetWindowLong (hwnd, GWL_HINSTANCE);
                    148: #elif defined (WIN16)
                    149:     hInst = (HANDLE)GetWindowWord (hwnd, GWW_HINSTANCE);
                    150: #endif
1.1       root      151: 
1.1.1.2   root      152:     switch (msg) {
                    153:         case WM_INITDIALOG:
                    154:             CenterWindow (hwnd, GetWindow (hwnd, GW_OWNER));
                    155:             hbmFontView = LoadBitmap (hInst, "FONTVIEW");
                    156:             GetObject (hbmFontView,sizeof(BITMAP), &bmFontView);
                    157:             if (!hbmFontView) MessageBeep(0);
                    158:             return (TRUE);
                    159: 
                    160:         case WM_DESTROY:
                    161:             DeleteObject (hbmFontView);
                    162:             break;
                    163: 
                    164:         case WM_PAINT:
                    165:             hdc = BeginPaint (hwnd, &ps);
                    166:             GetWindowRect (hwnd, &rect);
                    167:             ScreenToClient (hwnd, (LPPOINT)&rect.left);
                    168:             ScreenToClient (hwnd, (LPPOINT)&rect.right);
                    169:             hdc = GetDC (hwnd);
                    170:             hdcSrc = CreateCompatibleDC (hdc);
                    171:             hbmOld = SelectObject (hdcSrc, hbmFontView);
                    172:             if (!BitBlt (hdc, 0, 0, bmFontView.bmWidth, bmFontView.bmHeight, hdcSrc, 0, 0, SRCCOPY)) {
                    173:                 MessageBeep(0);
                    174:             }
                    175:             SelectObject (hdcSrc, hbmOld);
                    176:             DeleteDC (hdcSrc);
                    177:             EndPaint (hwnd, &ps);
                    178:             break;
                    179: 
                    180:         case WM_COMMAND:
                    181: #if defined (WIN32)
                    182:             wmId = LOWORD(wParam);
1.1       root      183: #elif defined (WIN16)
1.1.1.2   root      184:             wmId = wParam;
1.1       root      185: #endif
1.1.1.2   root      186:             switch (wmId) {
1.1       root      187: 
1.1.1.2   root      188:                 case IDOK:
                    189:                     EndDialog(hwnd, TRUE);
                    190:                     return (TRUE);
                    191: 
                    192:                 case IDCANCEL:
                    193:                     EndDialog(hwnd, TRUE);
                    194:                     return (TRUE);
                    195:             }
                    196:             break;
                    197:     }
                    198:     return (FALSE);
1.1       root      199: 
1.1.1.2   root      200:     lParam; // unreferenced formal parameter
1.1       root      201: }
                    202: 
1.1.1.2   root      203: 
                    204: /*
                    205:     This dialog will present edit controls for all the parameters of a CreateFont call.
                    206:     The user can put any value in any of the fields, no validation is done. These parameters
                    207:     are then used to create a font with, and that will be the font that will be displayed.
                    208:  */
1.1       root      209: BOOL APIENTRY CreateDlgProc(HWND hwnd, UINT msg, UINT wParam, LONG lParam)
                    210: {
1.1.1.2   root      211:     int wmId, i;
                    212:     BOOL bDone;
1.1       root      213: static LOGFONT lfDlg;
                    214: static LPLOGFONT lplf;
                    215: 
1.1.1.2   root      216:     switch (msg) {
                    217:         case WM_INITDIALOG:
                    218:             CenterWindow (hwnd, GetWindow (hwnd, GW_OWNER));
                    219:             lplf = (LOGFONT *)lParam;
                    220:             lfDlg = *lplf;
                    221:             SetDlgItemInt (hwnd, CFD_HEIGHT, (int)lfDlg.lfHeight, TRUE);
                    222:             SetDlgItemInt (hwnd, CFD_WIDTH, (int)lfDlg.lfWidth, TRUE);
                    223:             SetDlgItemInt (hwnd, CFD_ESCAPEMENT, (int)lfDlg.lfEscapement, TRUE);
                    224:             SetDlgItemInt (hwnd, CFD_ORIENTATION, (int)lfDlg.lfOrientation, TRUE);
                    225:             SetDlgItemInt (hwnd, CFD_WEIGHT, (int)lfDlg.lfWeight, FALSE);
                    226:             SetDlgItemInt (hwnd, CFD_ITALIC, (int)lfDlg.lfItalic, FALSE);
                    227:             SetDlgItemInt (hwnd, CFD_UNDERLINE, (int)lfDlg.lfUnderline, FALSE);
                    228:             SetDlgItemInt (hwnd, CFD_STRIKEOUT, (int)lfDlg.lfStrikeOut, FALSE);
                    229:             SetDlgItemInt (hwnd, CFD_CHARSET, (int)lfDlg.lfCharSet, FALSE);
                    230:             SetDlgItemInt (hwnd, CFD_OUTPUTPRECISION, (int)lfDlg.lfOutPrecision, FALSE);
                    231:             SetDlgItemInt (hwnd, CFD_CLIPPRECISION, (int)lfDlg.lfClipPrecision, FALSE);
                    232:             SetDlgItemInt (hwnd, CFD_QUALITY, (int)lfDlg.lfQuality, FALSE);
                    233:             SetDlgItemInt (hwnd, CFD_PITCHANDFAMILY, (int)lfDlg.lfPitchAndFamily, FALSE);
                    234:             SetDlgItemText(hwnd, CFD_FACENAME, lfDlg.lfFaceName);
                    235: 
                    236:             return (TRUE);
                    237: 
                    238:         case WM_COMMAND:
                    239: #if defined (WIN32)
                    240:             wmId = LOWORD(wParam);
1.1       root      241: #elif defined (WIN16)
1.1.1.2   root      242:             wmId = wParam;
1.1       root      243: #endif
1.1.1.2   root      244:             switch (wmId) {
                    245:                 case CFD_DEFAULT:
                    246:                     // Set all elements to ZERO. This will give us a 'default' font
                    247:                     for (i=CFD_BASE; i<=CFD_PITCHANDFAMILY; i++) {
                    248:                         SetDlgItemInt (hwnd, i, 0, FALSE);
                    249:                     }
                    250:                     SetDlgItemText (hwnd, CFD_FACENAME, "");
                    251:                     break;
                    252: 
                    253:                 case IDOK:
                    254:                     // Get the data from the edit control, we will then use this for a 'CreatFont' call
                    255:                     lfDlg.lfHeight = GetDlgItemInt (hwnd, CFD_HEIGHT, &bDone, TRUE);
                    256:                     lfDlg.lfWidth = GetDlgItemInt (hwnd, CFD_WIDTH, &bDone, TRUE);
                    257:                     lfDlg.lfEscapement = GetDlgItemInt (hwnd, CFD_ESCAPEMENT, &bDone, TRUE);
                    258:                     lfDlg.lfOrientation = GetDlgItemInt (hwnd, CFD_ORIENTATION, &bDone, TRUE);
                    259:                     lfDlg.lfWeight = GetDlgItemInt (hwnd, CFD_WEIGHT, &bDone, FALSE);
                    260:                     lfDlg.lfItalic = (BYTE)GetDlgItemInt (hwnd, CFD_ITALIC, &bDone, FALSE);
                    261:                     lfDlg.lfUnderline = (BYTE)GetDlgItemInt (hwnd, CFD_UNDERLINE, &bDone, FALSE);
                    262:                     lfDlg.lfStrikeOut = (BYTE)GetDlgItemInt (hwnd, CFD_STRIKEOUT, &bDone, FALSE);
                    263:                     lfDlg.lfCharSet = (BYTE)GetDlgItemInt (hwnd, CFD_CHARSET, &bDone, FALSE);
                    264:                     lfDlg.lfOutPrecision = (BYTE)GetDlgItemInt (hwnd, CFD_OUTPUTPRECISION, &bDone, FALSE);
                    265:                     lfDlg.lfClipPrecision = (BYTE)GetDlgItemInt (hwnd, CFD_CLIPPRECISION, &bDone, FALSE);
                    266:                     lfDlg.lfQuality = (BYTE)GetDlgItemInt (hwnd, CFD_QUALITY, &bDone, FALSE);
                    267:                     lfDlg.lfPitchAndFamily = (BYTE)GetDlgItemInt (hwnd, CFD_PITCHANDFAMILY, &bDone, FALSE);
                    268:                     GetDlgItemText(hwnd, CFD_FACENAME, lfDlg.lfFaceName,20);
                    269:                     // and copy the data into our external structure
                    270:                     *lplf = lfDlg;
                    271:                     EndDialog(hwnd, TRUE);
                    272:                     return (TRUE);
                    273: 
                    274:                 case IDCANCEL:
                    275:                     // Exit without changing anything
                    276:                     EndDialog(hwnd, TRUE);
                    277:                     return (TRUE);
                    278: 
                    279:             }
                    280:             break;
                    281:     }
                    282:     return (FALSE);
1.1       root      283: 
1.1.1.2   root      284:     lParam; //unreferenced formal paramter
1.1       root      285: }
                    286: 
                    287: /*
1.1.1.2   root      288:  This dialog will display the TEXTMETRIC data that is retrieved from a 'GetTextMetric' call.
                    289:  If the 'Select' button is clicked, then the current CreateFont data will be replaced with
                    290:  as much data from TEXTMETRIC as possible.
1.1       root      291: */
                    292: BOOL APIENTRY MetricsDlgProc(HWND hwnd, UINT msg, UINT wParam, LONG lParam)
                    293: {
                    294: static LOGFONT lfDlg;
                    295: static LPLOGFONT lplf;
                    296: static TEXTMETRIC tm;
                    297: static char szFacename[LF_FACESIZE];
1.1.1.2   root      298:     int wmId;
                    299:     BOOL bDone;
                    300:     TEXTMETRIC tmTmp;
                    301:     HFONT hfont, hfontPrev;
                    302:     HDC   hdc;
                    303:     char szBuffer[LF_FACESIZE+15];
                    304: 
                    305: 
                    306:     switch (msg) {
                    307:         case WM_INITDIALOG:
                    308:             CenterWindow (hwnd, GetWindow (hwnd, GW_OWNER));
                    309:             lplf = (LOGFONT *)lParam;
                    310:             lfDlg = *lplf;
                    311: 
                    312:             hfont = CreateFontIndirect (&lfDlg);
                    313:             hdc = GetDC (hwnd);
                    314:             hfontPrev = SelectObject (hdc, hfont);
                    315:             GetTextMetrics (hdc, &tm);
                    316:             GetTextFace    (hdc, sizeof(szFacename), szFacename);
                    317:             SelectObject (hdc, hfontPrev);
                    318:             DeleteObject (hfont);
                    319:             ReleaseDC (hwnd, hdc);
                    320: 
                    321:             wsprintf (szBuffer, "TextMetrics: %s", (LPSTR)szFacename);
                    322:             SetWindowText (hwnd, szBuffer);
                    323: 
                    324:             SetDlgItemInt (hwnd, TMD_HEIGHT, (int)tm.tmHeight, TRUE);
                    325:             SetDlgItemInt (hwnd, TMD_ASCENT, (int)tm.tmAscent, TRUE);
                    326:             SetDlgItemInt (hwnd, TMD_DESCENT, (int)tm.tmDescent, TRUE);
                    327:             SetDlgItemInt (hwnd, TMD_INTERNAL, (int)tm.tmInternalLeading, TRUE);
                    328:             SetDlgItemInt (hwnd, TMD_EXTERNAL, (int)tm.tmExternalLeading, TRUE);
                    329:             SetDlgItemInt (hwnd, TMD_AVEWIDTH, (int)tm.tmAveCharWidth, TRUE);
                    330:             SetDlgItemInt (hwnd, TMD_MAXWIDTH, (int)tm.tmMaxCharWidth, TRUE);
                    331:             SetDlgItemInt (hwnd, TMD_WEIGHT, (int)tm.tmWeight, TRUE);
                    332:             SetDlgItemInt (hwnd, TMD_ITALIC, (int)tm.tmItalic, FALSE);
                    333:             SetDlgItemInt (hwnd, TMD_UNDERLINE, (int)tm.tmUnderlined, FALSE);
                    334:             SetDlgItemInt (hwnd, TMD_STRUCKOUT, (int)tm.tmStruckOut, FALSE);
                    335:             SetDlgItemInt (hwnd, TMD_FIRSTCHAR, (int)tm.tmFirstChar, FALSE);
                    336:             SetDlgItemInt (hwnd, TMD_LASTCHAR, (int)tm.tmLastChar, FALSE);
                    337:             SetDlgItemInt (hwnd, TMD_DEFAULTCHAR, (int)tm.tmDefaultChar, FALSE);
                    338:             SetDlgItemInt (hwnd, TMD_BREAKCHAR, (int)tm.tmBreakChar, FALSE);
                    339:             SetDlgItemInt (hwnd, TMD_PITCHANDFAMILY, (int)tm.tmPitchAndFamily, FALSE);
                    340:             SetDlgItemInt (hwnd, TMD_CHARSET, (int)tm.tmCharSet, FALSE);
                    341:             SetDlgItemInt (hwnd, TMD_OVERHANG, (int)tm.tmOverhang, TRUE);
                    342:             SetDlgItemInt (hwnd, TMD_DIGITIZEDASPECTX, (int)tm.tmDigitizedAspectX, TRUE);
                    343:             SetDlgItemInt (hwnd, TMD_DIGITIZEDASPECTY, (int)tm.tmDigitizedAspectY, TRUE);
                    344: 
                    345:             return (TRUE);
                    346: 
                    347:         case WM_COMMAND:
                    348: #if defined (WIN32)
                    349:             wmId = LOWORD(wParam);
1.1       root      350: #elif defined (WIN16)
1.1.1.2   root      351:             wmId = wParam;
1.1       root      352: #endif
1.1.1.2   root      353:             switch (wmId) {
                    354:                 case IDOK:
                    355:                     // Lets pull in as much data from the TEXTMETRIC structure as possible...
                    356:                     lfDlg.lfHeight         = tm.tmHeight;
                    357:                     lfDlg.lfWidth          = tm.tmAveCharWidth;
                    358:                     //lfDlg.lfEscapement - No Use
                    359:                     //lfDlg.lfOrientation - No Use
                    360:                     lfDlg.lfWeight         = tm.tmWeight;
                    361:                     lfDlg.lfItalic         = tm.tmItalic;
                    362:                     lfDlg.lfUnderline      = tm.tmUnderlined;
                    363:                     lfDlg.lfStrikeOut      = tm.tmStruckOut;
                    364:                     lfDlg.lfCharSet        = tm.tmCharSet;
                    365:                     //lfDlg.lfOutPrecision - No Use
                    366:                     //lfDlg.lfClipPrecision - No Use
                    367:                     //lfDlg.lfQuality - No Use
                    368:                     lfDlg.lfPitchAndFamily = tm.tmPitchAndFamily;
                    369:                     lstrcpy(lfDlg.lfFaceName, szFacename);
                    370: 
                    371:                     // Lets create a font with this new data
                    372:                     hfont = CreateFontIndirect (&lfDlg);
                    373:                     hdc = GetDC (hwnd);
                    374:                     hfontPrev = SelectObject (hdc, hfont);
                    375:                     GetTextMetrics (hdc, &tmTmp);
                    376:                     // Get the face name
                    377:                     GetTextFace    (hdc, sizeof(szBuffer), szBuffer);
                    378:                     SelectObject (hdc, hfontPrev);
                    379:                     DeleteObject (hfont);
                    380:                     ReleaseDC (hwnd, hdc);
                    381: 
                    382:                     // And verify that we did indeed get the same font.
                    383:                     bDone = TRUE;
                    384:                     bDone = bDone && (tm.tmHeight==tmTmp.tmHeight);
                    385:                     bDone = bDone && (tm.tmAscent==tmTmp.tmAscent);
                    386:                     bDone = bDone && (tm.tmDescent==tmTmp.tmDescent);
                    387:                     bDone = bDone && (tm.tmInternalLeading==tmTmp.tmInternalLeading);
                    388:                     bDone = bDone && (tm.tmExternalLeading==tmTmp.tmExternalLeading);
                    389:                     bDone = bDone && (tm.tmAveCharWidth==tmTmp.tmAveCharWidth);
                    390:                     bDone = bDone && (tm.tmMaxCharWidth==tmTmp.tmMaxCharWidth);
                    391:                     bDone = bDone && (tm.tmWeight==tmTmp.tmWeight);
                    392:                     bDone = bDone && (tm.tmItalic==tmTmp.tmItalic);
                    393:                     bDone = bDone && (tm.tmUnderlined==tmTmp.tmUnderlined);
                    394:                     bDone = bDone && (tm.tmStruckOut==tmTmp.tmStruckOut);
                    395:                     bDone = bDone && (tm.tmFirstChar==tmTmp.tmFirstChar);
                    396:                     bDone = bDone && (tm.tmLastChar==tmTmp.tmLastChar);
                    397:                     bDone = bDone && (tm.tmDefaultChar==tmTmp.tmDefaultChar);
                    398:                     bDone = bDone && (tm.tmBreakChar==tmTmp.tmBreakChar);
                    399:                     bDone = bDone && (tm.tmPitchAndFamily==tmTmp.tmPitchAndFamily);
                    400:                     bDone = bDone && (tm.tmCharSet==tmTmp.tmCharSet);
                    401:                     // Did it work?
                    402:                     if (bDone) {
                    403:                         *lplf = lfDlg;
                    404:                         EndDialog(hwnd, TRUE);
                    405:                         return (TRUE);
                    406:                    } else {
                    407:                        // We need to take a close look at the font verification
                    408:                        // code. Currently, it sometimes will report that the
                    409:                        // font didn't get properly selected, even if it did.
                    410:                        *lplf = lfDlg;
                    411:                         EndDialog(hwnd, TRUE);
                    412:                        return (TRUE);
1.1       root      413: 
1.1.1.2   root      414:                        // This is what we want to do once we beef up the font
                    415:                        // verification code:
                    416:                        lfDlg = *lplf;
                    417:                         MessageBox (GetFocus(),
                    418:                             "Unable to re-create font from TextMetrics",
                    419:                             "FontView", MB_OK);
                    420:                     }
                    421:                     break;
                    422: 
                    423:                 case IDCANCEL:
                    424:                     EndDialog(hwnd, TRUE);
                    425:                     return (TRUE);
                    426: 
                    427:             }
                    428:             break;
                    429:     }
                    430:     return (FALSE);
1.1       root      431: 
1.1.1.2   root      432:     /* Just For Reference */
                    433:     lParam;
1.1       root      434: }
                    435: 
                    436: 
                    437: BOOL FillEnumFields (HWND hwnd, int iType, LPTEXTMETRIC ptm, LPLOGFONT plf, int nBase)
                    438: {
1.1.1.2   root      439:             SetDlgItemValue (hwnd, TMD_HEIGHT, (int)ptm->tmHeight, TRUE, nBase);
                    440:             SetDlgItemValue (hwnd, TMD_ASCENT, (int)ptm->tmAscent, TRUE, nBase);
                    441:             SetDlgItemValue (hwnd, TMD_DESCENT, (int)ptm->tmDescent, TRUE, nBase);
                    442:             SetDlgItemValue (hwnd, TMD_INTERNAL, (int)ptm->tmInternalLeading, TRUE, nBase);
                    443:             SetDlgItemValue (hwnd, TMD_EXTERNAL, (int)ptm->tmExternalLeading, TRUE, nBase);
                    444:             SetDlgItemValue (hwnd, TMD_AVEWIDTH, (int)ptm->tmAveCharWidth, TRUE, nBase);
                    445:             SetDlgItemValue (hwnd, TMD_MAXWIDTH, (int)ptm->tmMaxCharWidth, TRUE, nBase);
                    446:             SetDlgItemValue (hwnd, TMD_WEIGHT, (int)ptm->tmWeight, TRUE, nBase);
                    447:             SetDlgItemValue (hwnd, TMD_ITALIC, (int)ptm->tmItalic, FALSE, nBase);
                    448:             SetDlgItemValue (hwnd, TMD_UNDERLINE, (int)ptm->tmUnderlined, FALSE, nBase);
                    449:             SetDlgItemValue (hwnd, TMD_STRUCKOUT, (int)ptm->tmStruckOut, FALSE, nBase);
                    450:             SetDlgItemValue (hwnd, TMD_FIRSTCHAR, (int)ptm->tmFirstChar, FALSE, nBase);
                    451:             SetDlgItemValue (hwnd, TMD_LASTCHAR, (int)ptm->tmLastChar, FALSE, nBase);
                    452:             SetDlgItemValue (hwnd, TMD_DEFAULTCHAR, (int)ptm->tmDefaultChar, FALSE, nBase);
                    453:             SetDlgItemValue (hwnd, TMD_BREAKCHAR, (int)ptm->tmBreakChar, FALSE, nBase);
                    454:             SetDlgItemValue (hwnd, TMD_PITCHANDFAMILY, (int)ptm->tmPitchAndFamily, FALSE, nBase);
                    455:             SetDlgItemValue (hwnd, TMD_CHARSET, (int)ptm->tmCharSet, FALSE, nBase);
                    456:             SetDlgItemValue (hwnd, TMD_OVERHANG, (int)ptm->tmOverhang, TRUE, nBase);
                    457:             SetDlgItemValue (hwnd, TMD_DIGITIZEDASPECTX, (int)ptm->tmDigitizedAspectX, TRUE, nBase);
                    458:             SetDlgItemValue (hwnd, TMD_DIGITIZEDASPECTY, (int)ptm->tmDigitizedAspectY, TRUE, nBase);
                    459: 
                    460:             SetDlgItemValue (hwnd, CFD_HEIGHT, (int)plf->lfHeight, TRUE, nBase);
                    461:             SetDlgItemValue (hwnd, CFD_WIDTH, (int)plf->lfWidth, TRUE, nBase);
                    462:             SetDlgItemValue (hwnd, CFD_ESCAPEMENT, (int)plf->lfEscapement, TRUE, nBase);
                    463:             SetDlgItemValue (hwnd, CFD_ORIENTATION, (int)plf->lfOrientation, TRUE, nBase);
                    464:             SetDlgItemValue (hwnd, CFD_WEIGHT, (int)plf->lfWeight, FALSE, nBase);
                    465:             SetDlgItemValue (hwnd, CFD_ITALIC, (int)plf->lfItalic, FALSE, nBase);
                    466:             SetDlgItemValue (hwnd, CFD_UNDERLINE, (int)plf->lfUnderline, FALSE, nBase);
                    467:             SetDlgItemValue (hwnd, CFD_STRIKEOUT, (int)plf->lfStrikeOut, FALSE, nBase);
                    468:             SetDlgItemValue (hwnd, CFD_CHARSET, (int)plf->lfCharSet, FALSE, nBase);
                    469:             SetDlgItemValue (hwnd, CFD_OUTPUTPRECISION, (int)plf->lfOutPrecision, FALSE, nBase);
                    470:             SetDlgItemValue (hwnd, CFD_CLIPPRECISION, (int)plf->lfClipPrecision, FALSE, nBase);
                    471:             SetDlgItemValue (hwnd, CFD_QUALITY, (int)plf->lfQuality, FALSE, nBase);
                    472:             SetDlgItemValue (hwnd, CFD_PITCHANDFAMILY, (int)plf->lfPitchAndFamily, FALSE, nBase);
                    473:             SetDlgItemText(hwnd, CFD_FACENAME, plf->lfFaceName);
1.1       root      474: 
1.1.1.2   root      475:             SetDlgItemValue (hwnd, ED_TYPE, (int)iType, FALSE, nBase);
1.1       root      476: 
1.1.1.2   root      477:             return TRUE;
1.1       root      478: }
                    479: 
                    480: 
                    481: BOOL DrawSample (HWND hwnd, LPLOGFONT plf)
                    482: {
1.1.1.2   root      483:     HFONT   hfont, hfontPrev;
                    484:     HDC     hdc;
                    485:     RECT    r;
                    486: 
                    487:     hfont = CreateFontIndirect (plf);
                    488:     hdc = GetDC (hwnd);
                    489:     hfontPrev = SelectObject (hdc, hfont);
                    490:     GetWindowRect (hwnd, &r);
                    491:     ScreenToClient (hwnd, (LPPOINT)&r.left);
                    492:     ScreenToClient (hwnd, (LPPOINT)&r.right);
                    493: 
                    494:     Rectangle (hdc, r.left, r.top, r.right, r.bottom);
                    495: 
                    496:     InflateRect (&r, -1, -1);
                    497:     SetTextAlign (hdc, TA_BOTTOM | TA_CENTER);
                    498:     ExtTextOut (hdc, r.left + ((r.right-r.left)/2), r.bottom, ETO_CLIPPED, &r,"AaBbCcDdEe 012345", 17, NULL);
                    499: 
                    500:     SelectObject (hdc, hfontPrev);
                    501:     DeleteObject (hfont);
                    502:     ReleaseDC (hwnd, hdc);
                    503:     return TRUE;
1.1       root      504: }
                    505: 
                    506: 
1.1.1.2   root      507: /*
                    508:     Display a dialog that the user can use to enumerate through all of the fonts in the system.
                    509:     Show him not only all of the metrics for the font, but a sample of the font as well.
                    510:     If the user picks the 'Select' button, then the Metrics of this font will be used for
                    511:     the CreateFont call.
                    512: */
1.1       root      513: BOOL APIENTRY EnumDlgProc(HWND hwnd, UINT msg, UINT wParam, LONG lParam)
                    514: {
1.1.1.2   root      515:     static HANDLE /*TO FONTLIST*/ hFonts;
                    516:     static int iLoc=0;
                    517:     static int count=0;
                    518:     static BOOL bHex = FALSE;
                    519:     static LOGFONT *lplf;
                    520:     static LOGFONT lfDlg;
                    521:     int wmId, i, j;
                    522:     HANDLE hInst;
                    523:     HWND   hwndItem;
                    524:     HDC hdc;
                    525:     FARPROC lpEnumFonts;
                    526:     LPFONTLIST    pFL;
                    527:     LPFONTSTRUCT  pFS;
                    528:     char szTmp[80];
                    529:     HFONT hfont, hfontPrev;
                    530:     BOOL bDone;
                    531:     TEXTMETRIC tmTmp;
                    532: 
                    533: 
                    534:     switch (msg) {
                    535:         case WM_INITDIALOG:
                    536:             lplf = (LOGFONT *)lParam;
                    537:             lfDlg = *lplf;
                    538:             iLoc = 0;
                    539:             count = 0;
                    540:             bHex = FALSE;
                    541:             /* First, lets enumerate ALL fonts, and store them in our list */
                    542:             hFonts = GlobalAlloc (GHND, sizeof(FONTLIST));
                    543:             if (hFonts) {
1.1       root      544: 
1.1.1.2   root      545: #if defined (WIN32)
                    546:     hInst = (HANDLE)GetWindowLong (hwnd, GWL_HINSTANCE);
1.1       root      547: #elif defined (WIN16)
1.1.1.2   root      548:     hInst = (HANDLE)GetWindowWord (hwnd, GWW_HINSTANCE);
1.1       root      549: #endif
                    550: 
1.1.1.3 ! root      551:                 lpEnumFonts = MakeProcInstance((FARPROC)DlgEnumFontNames, hInst);
1.1.1.2   root      552:                 if (lpEnumFonts) {
                    553:                     hdc  = GetDC(hwnd);
                    554:                     // The enumeration function will lock down the handle
1.1.1.3 ! root      555:                    EnumFonts (hdc, NULL, (FONTENUMPROC)lpEnumFonts, (LPARAM)&hFonts);
1.1.1.2   root      556:                     // The handle will come back to us properly unlocked
                    557:                     ReleaseDC(hwnd, hdc);
                    558:                     FreeProcInstance (lpEnumFonts);
                    559: 
                    560:                  }
                    561:             }
                    562:             pFL = (LPFONTLIST)GlobalLock (hFonts);
                    563:             if (pFL) {
                    564:                 pFS = (LPFONTSTRUCT)GlobalLock (pFL->hList);
                    565:                 if (pFS) {
                    566:                     iLoc = 0;
                    567:                     j = 100;
                    568:                     for (i=0; i<pFL->count; i++) {
                    569:                         if (lstrcmp(lfDlg.lfFaceName, pFS[i].lf.lfFaceName) == 0) {
                    570:                             if (abs(lfDlg.lfHeight-pFS[i].lf.lfHeight) < j) {
                    571:                                 j = abs(lfDlg.lfHeight-pFS[i].lf.lfHeight);
                    572:                                 iLoc = i;
                    573:                             }
                    574:                         }
                    575:                     }
                    576:                     FillEnumFields (hwnd, pFS[iLoc].ft, &pFS[iLoc].tm, &pFS[iLoc].lf, (bHex?16:10));
                    577:                     hwndItem = GetDlgItem (hwnd, ED_SAMPLE);
                    578:                     DrawSample (hwndItem, &pFS[iLoc].lf);
                    579:                     GlobalUnlock (pFL->hList);
                    580: 
                    581:                 }
                    582:                 count = pFL->count;
                    583:                 GlobalUnlock (hFonts);
                    584: 
                    585:             }
                    586:             /* now fill in the dialog values */
                    587:             wsprintf (szTmp, "EnumFonts %d of %d", iLoc+1, count);
                    588:             SetWindowText (hwnd, szTmp);
                    589:             CenterWindow (hwnd, GetWindow (hwnd, GW_OWNER));
                    590: 
                    591:             return TRUE;
                    592: 
                    593:         case WM_PAINT:
                    594:             pFL = (LPFONTLIST)GlobalLock (hFonts);
                    595:             if (pFL) {
                    596:                 pFS = (LPFONTSTRUCT)GlobalLock (pFL->hList);
                    597:                 if (pFS) {
                    598:                     FillEnumFields (hwnd, pFS[iLoc].ft, &pFS[iLoc].tm, &pFS[iLoc].lf, (bHex?16:10));
                    599:                     hwndItem = GetDlgItem (hwnd, ED_SAMPLE);
                    600:                     DrawSample (hwndItem, &pFS[iLoc].lf);
                    601:                     GlobalUnlock (pFL->hList);
                    602:                 }
                    603:                 GlobalUnlock (hFonts);
                    604:             }
                    605:             return 0;
                    606: 
                    607:         case WM_COMMAND:
                    608: #if defined (WIN32)
                    609:             wmId = LOWORD(wParam);
1.1       root      610: #elif defined (WIN16)
1.1.1.2   root      611:             wmId = wParam;
1.1       root      612: #endif
1.1.1.2   root      613:             switch (wmId) {
1.1       root      614: 
1.1.1.2   root      615:                 case ED_HEX:
                    616:                     // Display the data in either Hex mode or Dec mode.
                    617:                     bHex = !bHex;
                    618:                     CheckDlgButton (hwnd, wmId, bHex);
                    619:                     pFL = (LPFONTLIST)GlobalLock (hFonts);
                    620:                     if (pFL) {
                    621:                         pFS = (LPFONTSTRUCT)GlobalLock (pFL->hList);
                    622:                         if (pFS) {
                    623:                             FillEnumFields (hwnd, pFS[iLoc].ft, &pFS[iLoc].tm, &pFS[iLoc].lf, (bHex?16:10));
                    624:                             if (GlobalUnlock (pFL->hList)) {
                    625:                                 MessageBox (GetFocus(), "In HEX", "Unlock pFL->hList", MB_OK);
                    626:                             }
                    627:                         }
                    628:                         if (GlobalUnlock (hFonts)) {
                    629:                             MessageBox (GetFocus(), "In HEX", "Unlock hFonts", MB_OK);
                    630:                         }
                    631: 
                    632:                     }
                    633:                     break;
                    634: 
                    635:                 case ED_PREV:
                    636:                     pFL = (LPFONTLIST)GlobalLock (hFonts);
                    637:                     if (pFL) {
                    638:                         if (iLoc > 0) {
                    639:                             iLoc--;
                    640:                             pFS = (LPFONTSTRUCT)GlobalLock (pFL->hList);
                    641:                             if (pFS) {
                    642:                                 FillEnumFields (hwnd, pFS[iLoc].ft, &pFS[iLoc].tm, &pFS[iLoc].lf, (bHex?16:10));
                    643:                                 hwndItem = GetDlgItem (hwnd, ED_SAMPLE);
                    644:                                 DrawSample (hwndItem, &pFS[iLoc].lf);
                    645:                                 GlobalUnlock (pFL->hList);
                    646:                             }
                    647:                         }
                    648:                         GlobalUnlock (hFonts);
                    649:                     }
                    650:                     wsprintf (szTmp, "EnumFonts %d of %d", iLoc+1, count);
                    651:                     SetWindowText (hwnd, szTmp);
                    652:                     break;
                    653: 
                    654:                 case ED_NEXT:
                    655:                     pFL = (LPFONTLIST)GlobalLock (hFonts);
                    656:                     if (pFL) {
                    657:                         if ((pFL->count-1) > iLoc) {
                    658:                             iLoc++;
                    659:                             pFS = (LPFONTSTRUCT)GlobalLock (pFL->hList);
                    660:                             if (pFS) {
                    661:                                 FillEnumFields (hwnd, pFS[iLoc].ft, &pFS[iLoc].tm, &pFS[iLoc].lf, (bHex?16:10));
                    662:                                 hwndItem = GetDlgItem (hwnd, ED_SAMPLE);
                    663:                                 DrawSample (hwndItem, &pFS[iLoc].lf);
                    664:                                 GlobalUnlock (pFL->hList);
                    665:                             }
                    666: 
                    667:                         }
                    668:                         GlobalUnlock (hFonts);
                    669:                     }
                    670:                     wsprintf (szTmp, "EnumFonts %d of %d", iLoc+1, count);
                    671:                     SetWindowText (hwnd, szTmp);
                    672:                     break;
                    673: 
                    674:                 case IDOK:
                    675:                     // Copy the LOGFONT structure from the enumeration list, into our private LF
                    676:                     pFL = (LPFONTLIST)GlobalLock (hFonts);
                    677:                     bDone = FALSE;
                    678:                     if (pFL) {
                    679:                         if ((pFL->count-1) >= iLoc) {
                    680:                             pFS = (LPFONTSTRUCT)GlobalLock (pFL->hList);
                    681:                             if (pFS) {
                    682:                                 lfDlg = pFS[iLoc].lf;
                    683: 
                    684:                     // Lets create a font with this new data
                    685:                     hfont = CreateFontIndirect (&lfDlg);
                    686:                     hdc = GetDC (hwnd);
                    687:                     hfontPrev = SelectObject (hdc, hfont);
                    688:                     GetTextMetrics (hdc, &tmTmp);
                    689:                     // Get the face name
                    690:                     GetTextFace    (hdc, sizeof(szTmp), szTmp);
                    691:                     SelectObject (hdc, hfontPrev);
                    692:                     DeleteObject (hfont);
                    693:                     ReleaseDC (hwnd, hdc);
                    694: 
                    695:                     // And verify that we did indeed get the same font.
                    696:                     bDone = TRUE;
                    697:                     bDone = bDone && (pFS[iLoc].tm.tmHeight==tmTmp.tmHeight);
                    698:                     bDone = bDone && (pFS[iLoc].tm.tmAscent==tmTmp.tmAscent);
                    699:                     bDone = bDone && (pFS[iLoc].tm.tmDescent==tmTmp.tmDescent);
                    700:                     bDone = bDone && (pFS[iLoc].tm.tmInternalLeading==tmTmp.tmInternalLeading);
                    701:                     bDone = bDone && (pFS[iLoc].tm.tmExternalLeading==tmTmp.tmExternalLeading);
                    702:                     bDone = bDone && (pFS[iLoc].tm.tmAveCharWidth==tmTmp.tmAveCharWidth);
                    703:                     bDone = bDone && (pFS[iLoc].tm.tmMaxCharWidth==tmTmp.tmMaxCharWidth);
                    704:                     bDone = bDone && (pFS[iLoc].tm.tmWeight==tmTmp.tmWeight);
                    705:                     bDone = bDone && (pFS[iLoc].tm.tmItalic==tmTmp.tmItalic);
                    706:                     bDone = bDone && (pFS[iLoc].tm.tmUnderlined==tmTmp.tmUnderlined);
                    707:                     bDone = bDone && (pFS[iLoc].tm.tmStruckOut==tmTmp.tmStruckOut);
                    708:                     bDone = bDone && (pFS[iLoc].tm.tmFirstChar==tmTmp.tmFirstChar);
                    709:                     bDone = bDone && (pFS[iLoc].tm.tmLastChar==tmTmp.tmLastChar);
                    710:                     bDone = bDone && (pFS[iLoc].tm.tmDefaultChar==tmTmp.tmDefaultChar);
                    711:                     bDone = bDone && (pFS[iLoc].tm.tmBreakChar==tmTmp.tmBreakChar);
                    712:                     bDone = bDone && (pFS[iLoc].tm.tmPitchAndFamily==tmTmp.tmPitchAndFamily);
                    713:                     bDone = bDone && (pFS[iLoc].tm.tmCharSet==tmTmp.tmCharSet);
                    714:                     // Did it work?
                    715:                     if (bDone) {
                    716:                         *lplf = lfDlg;
                    717:                         //EndDialog(hwnd, TRUE);
                    718:                         //return (TRUE);
                    719:                    } else {
                    720:                        // Again, font verification code is'nt quite up to
                    721:                        // snuff yet, so just Select the font anyway:
                    722:                        *lplf = lfDlg;
                    723: 
                    724:                        // ...and this is what we want to do once we fix the
                    725:                        // font verification code:
                    726:                        //lfDlg = *lplf;
                    727:                        //MessageBox (GetFocus(),
                    728:                        //    "Unable to re-create font from TextMetrics",
                    729:                        //    "FontView", MB_OK);
                    730:                     }
                    731: 
                    732: 
                    733:                                 GlobalUnlock (pFL->hList);
                    734:                             }
                    735:                         }
                    736:                         GlobalUnlock (hFonts);
                    737:                     }
                    738: 
                    739: 
                    740:                     // Now set our master lf to this value. This way, we 'could' verify that the
                    741:                     // LOGFONT structure will in fact select this particular font, we just aren't yet
                    742:                     // *lplf = lfDlg;
                    743: 
                    744:                     pFL = (LPFONTLIST)GlobalLock (hFonts);
                    745:                     if (pFL) {
                    746:                         if (GlobalFree (pFL->hList)) {
                    747:                             MessageBox (GetFocus(), "Failed To Free", "pFL->hList", MB_OK);
                    748:                         } else {
                    749:                             GlobalUnlock (hFonts);
                    750:                             if (GlobalFree (hFonts)) {
                    751:                                 MessageBox (GetFocus(), "Failed To Free", "hFonts", MB_OK);
                    752:                             }
                    753:                         }
                    754:                     }
                    755:                     EndDialog(hwnd, TRUE);
                    756:                     return (bDone);
                    757: 
                    758:                 case IDCANCEL:
                    759:                     pFL = (LPFONTLIST)GlobalLock (hFonts);
                    760:                     if (pFL) {
                    761:                         if (GlobalFree (pFL->hList)) {
                    762:                             MessageBox (GetFocus(), "Failed To Free", "pFL->hList", MB_OK);
                    763:                         } else {
                    764:                             GlobalUnlock (hFonts);
                    765:                             if (GlobalFree (hFonts)) {
                    766:                                 MessageBox (GetFocus(), "Failed To Free", "hFonts", MB_OK);
                    767:                             }
                    768:                         }
                    769:                     }
                    770:                     EndDialog(hwnd, TRUE);
                    771:                     return (TRUE);
                    772:             }
                    773:             break;
                    774:     }
                    775:     return (FALSE);
1.1       root      776: 
1.1.1.2   root      777:     lParam; // unreferenced formal parameter
1.1       root      778: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.