/*--- DEMO.C ----------------------------- Listing 1 ------------
 * Lists available fonts under Windows and shows approxi-
 * mately what the printer versions look like. Requires SDK.
 * Compiles under MSC 5.0-6.00A
 *
 * by Michael Young. Object code may be used freely. Source 
 * code may be used freely if authorship and publication are
 * caknowledged.
 *-------------------------------------------------------------*/
#define _WINDOWS
#include <windows.h>
#include <stdio.h>
#include <string.h>

#define IDC_BOLD         100
#define IDC_FONTLIST     110
#define IDC_ITALIC       120
#define IDC_PRNNAME      130
#define IDC_SIZELIST     140
#define IDC_UNDERLINE    150

HWND HBold;
HFONT HFont = NULL;
HWND HFontList;
HANDLE HInst;
HWND HItalic;
HDC HPrnIC = NULL;
HWND HSizeList;
HWND HUnderline;
HDC HWinDC = NULL;
HWND HWindow;
int FontSize;
LOGFONT LF;
int NumSizes;
char PrinterName [32];
RECT Rect;
int SizeList [25];
TEXTMETRIC TM;
int VectorFont;
static int VectorSizes [] = { 6*20,  8*20, 10*20, 12*20, 14*20,
                             18*20, 24*20, 30*20, 36*20, 48*20};

int FAR PASCAL EnumFaceNames (LPLOGFONT PtrLF,
                              LPTEXTMETRIC PtrTM,
                              short FontType,
                              LPSTR PtrData);
int FAR PASCAL EnumSizes     (LPLOGFONT PtrLF,
                              LPTEXTMETRIC PtrTM,
                              short FontType,
                              LPSTR PtrData);
void GetPrnIC (void);
BOOL InitInstance (HANDLE hInstance, int nCmdShow);
BOOL InitProgram (HANDLE hInstance);
long FAR PASCAL MainWndProc (HWND hWnd,unsigned message,
                             WORD wParam,LONG lParam);

int PASCAL WinMain
     (HANDLE hInstance,
     HANDLE hPrevInstance,
     LPSTR lpCmdLine,
     int nCmdShow)
     {
     MSG Msg;

     if (!hPrevInstance)
          if (!InitProgram (hInstance))
               return (FALSE);
     if (!InitInstance (hInstance, nCmdShow))
          return (FALSE);
     while (GetMessage
          (&Msg,
          NULL,
          NULL,
          NULL))
          {
          TranslateMessage (&Msg);
          DispatchMessage (&Msg);
          }
     return (Msg.wParam);
     } /* end WinMain */

int FAR PASCAL EnumFaceNames (LPLOGFONT PtrLF,LPTEXTMETRIC PtrTM,
                              short FontType,LPSTR PtrData)
     {
     SendMessage
          (HFontList,
          LB_ADDSTRING,
          NULL,
          (DWORD)(LPSTR)PtrLF->lfFaceName);
     return (1);
     } /* end EnumFaceNames */

int FAR PASCAL EnumSizes (LPLOGFONT PtrLF,LPTEXTMETRIC PtrTM,
                            short FontType,LPSTR PtrData)
     {
     int i;
     int PointSize;

     LF = *PtrLF;
     if (!(FontType & RASTER_FONTTYPE))
          {
          VectorFont = 1;
          return (0);
          }
     if (NumSizes >= 25)
          return (0);
     PointSize = (PtrTM->tmHeight -
                  PtrTM->tmInternalLeading + 10) / 20;
     PointSize = PointSize * 20 + 9;
     for (i = 0; i < NumSizes; ++i)
          {
          if (SizeList [i] == PointSize)
               return (1);
          if (SizeList [i] > PointSize)
               {
               memmove (SizeList + i + 1,SizeList + i,
                        (NumSizes - i) * sizeof (int));
               break;
               }
          }
     SizeList [i] = PointSize;
     ++NumSizes;
     return (1);
     } /* end EnumSizes */

void GetPrnIC (void)
     {
     char PrnInfo [80];
     char *PtrPrnDriver;
     char *PtrPrnName;
     char *PtrPrnPort;
     int Result;

     Result = GetProfileString
          ("windows",
          "device",
          (LPSTR)"",
          PrnInfo,
          sizeof (PrnInfo));
     if (Result == 0 || !PrnInfo [0])
          return;
     PtrPrnName = strtok (PrnInfo,",");
     PtrPrnDriver = strtok (NULL,", ");
     PtrPrnPort = strtok (NULL,", ");
     if (!PtrPrnName || !PtrPrnDriver || !PtrPrnPort)
          return;
     strcpy (PrinterName,PtrPrnName);
     HPrnIC = CreateIC
          (PtrPrnDriver,
          PtrPrnName,
          PtrPrnPort,
          NULL);
     if (HPrnIC == NULL)
          return;
     SetMapMode (HPrnIC,MM_ANISOTROPIC);
     SetWindowExt (HPrnIC,1440,1440);
     SetViewportExt
          (HPrnIC,
          GetDeviceCaps (HPrnIC,LOGPIXELSX),
          GetDeviceCaps (HPrnIC,LOGPIXELSY));
     return;
     } /* end GetPrnIC */

BOOL InitInstance
     (HANDLE hInstance,
     int hCmdShow)
     {
     LONG DlgBaseUnits;
     HDC HWinDC;
     int Index;
     FARPROC ProcAddr;

     HInst = hInstance;
     DlgBaseUnits = GetDialogBaseUnits ();
     Rect.left = (5 * LOWORD (DlgBaseUnits)) / 4;
     Rect.top = (100 * HIWORD (DlgBaseUnits)) / 8;
     Rect.right = (130 * LOWORD (DlgBaseUnits)) / 4;
     Rect.bottom = (142 * HIWORD (DlgBaseUnits)) / 8;
     HWindow = CreateWindow
          ("DemoClass",            /* Class name passed
                                      to RegisterClass. */
          "Printer Font Viewer",   /* Text for window title. */
          WS_BORDER      |         /* Window style: */
          WS_CAPTION     |
          WS_MINIMIZEBOX |
          WS_SYSMENU     |
          WS_VISIBLE,
          CW_USEDEFAULT,      /* Horz. position: use default. */
          CW_USEDEFAULT,      /* Vert. position: use default. */
          (130 * LOWORD (DlgBaseUnits)) / 4, /* Window width. */
          (142 * HIWORD (DlgBaseUnits)) / 8, /* Window height.*/
          NULL,               /* Parent: none. */
          NULL,               /* Menu: use class menu. */
          HInst,              /* Instance to be associated
                                 with window. */
          NULL);              /* Pointer value passed
                                 to window: none. */
     if (!HWindow)
          return (FALSE);
     HWinDC = GetDC (HWindow);
     SetMapMode
          (HWinDC,
          MM_ANISOTROPIC);
     SetWindowExt (HWinDC,1440,1440);
     SetViewportExt
          (HWinDC,
          GetDeviceCaps (HWinDC,LOGPIXELSX),
          GetDeviceCaps (HWinDC,LOGPIXELSY));
     SetViewportOrg
          (HWinDC,
          (5 * LOWORD (DlgBaseUnits)) / 4,
          (100 * HIWORD (DlgBaseUnits)) / 8);
     ReleaseDC (HWindow,HWinDC);
     ShowWindow
          (HWindow,
          hCmdShow);
     GetPrnIC ();
     GetTextMetrics (HPrnIC,&TM);
     FontSize = (TM.tmHeight -
                    TM.tmInternalLeading + 10) / 20;
     FontSize = FontSize * 20 + 9;
     GetTextFace (HPrnIC,LF_FACESIZE,LF.lfFaceName);
     CreateWindow
          ("STATIC",
          "Printer:",
          WS_CHILD   |
          WS_VISIBLE |
          SS_LEFT,
          (5 * LOWORD (DlgBaseUnits)) / 4,
          (5 * HIWORD (DlgBaseUnits)) / 8,
          (25 * LOWORD (DlgBaseUnits)) / 4,
          (8 * HIWORD (DlgBaseUnits)) / 8,
          HWindow,
          -1,
          HInst,
          NULL);
     CreateWindow
          ("STATIC",
          PrinterName,
          WS_CHILD   |
          WS_VISIBLE |
          SS_LEFT,
          (31 * LOWORD (DlgBaseUnits)) / 4,
          (5 * HIWORD (DlgBaseUnits)) / 8,
          (96 * LOWORD (DlgBaseUnits)) / 4,
          (8 * HIWORD (DlgBaseUnits)) / 8,
          HWindow,
          IDC_PRNNAME,
          HInst,
          NULL);
     CreateWindow
          ("STATIC",
          "Typeface:",
          WS_CHILD   |
          WS_VISIBLE |
          SS_LEFT,
          (5 * LOWORD (DlgBaseUnits)) / 4,
          (17 * HIWORD (DlgBaseUnits)) / 8,
          (35 * LOWORD (DlgBaseUnits)) / 4,
          (8 * HIWORD (DlgBaseUnits)) / 8,
          HWindow,
          -1,
          HInst,
          NULL);
     CreateWindow
          ("STATIC",
          "Size:",
          WS_CHILD   |
          WS_VISIBLE |
          SS_LEFT,
          (88 * LOWORD (DlgBaseUnits)) / 4,
          (17 * HIWORD (DlgBaseUnits)) / 8,
          (27 * LOWORD (DlgBaseUnits)) / 4,
          (8 * HIWORD (DlgBaseUnits)) / 8,
          HWindow,
          -1,
          HInst,
          NULL);
     HFontList = CreateWindow
          ("LISTBOX",
          "",
          WS_CHILD |
          WS_VSCROLL |
          WS_VISIBLE |
          LBS_STANDARD,
          (5 * LOWORD (DlgBaseUnits)) / 4,
          (28 * HIWORD (DlgBaseUnits)) / 8,
          (76 * LOWORD (DlgBaseUnits)) / 4,
          (57 * HIWORD (DlgBaseUnits)) / 8,
          HWindow,
          IDC_FONTLIST,
          HInst,
          NULL);
     HSizeList = CreateWindow
          ("LISTBOX",
          "",
          WS_CHILD   |
          WS_VSCROLL |
          WS_VISIBLE |
          WS_BORDER  |
          LBS_NOTIFY,
          (88 * LOWORD (DlgBaseUnits)) / 4,
          (28 * HIWORD (DlgBaseUnits)) / 8,
          (36 * LOWORD (DlgBaseUnits)) / 4,
          (57 * HIWORD (DlgBaseUnits)) / 8,
          HWindow,
          IDC_SIZELIST,
          HInst,
          NULL);
     HBold = CreateWindow
          ("BUTTON",
          "Bold",
          WS_CHILD   |
          WS_VISIBLE |
          BS_AUTOCHECKBOX,
          (5 * LOWORD (DlgBaseUnits)) / 4,
          (88 * HIWORD (DlgBaseUnits)) / 8,
          (28 * LOWORD (DlgBaseUnits)) / 4,
          (12 * HIWORD (DlgBaseUnits)) / 8,
          HWindow,
          IDC_BOLD,
          HInst,
          NULL);
     SendMessage
          (HBold,
          BM_SETCHECK,
          TM.tmWeight == 700,
          NULL);
     HItalic = CreateWindow
          ("BUTTON",
          "Italic",
          WS_CHILD   |
          WS_VISIBLE |
          BS_AUTOCHECKBOX,
          (43 * LOWORD (DlgBaseUnits)) / 4,
          (88 * HIWORD (DlgBaseUnits)) / 8,
          (28 * LOWORD (DlgBaseUnits)) / 4,
          (12 * HIWORD (DlgBaseUnits)) / 8,
          HWindow,
          IDC_ITALIC,
          HInst,
          NULL);
     SendMessage
          (HItalic,
          BM_SETCHECK,
          TM.tmItalic,
          NULL);
     HUnderline = CreateWindow
          ("BUTTON",
          "Underline",
          WS_CHILD   |
          WS_VISIBLE |
          BS_AUTOCHECKBOX,
          (82 * LOWORD (DlgBaseUnits)) / 4,
          (88 * HIWORD (DlgBaseUnits)) / 8,
          (43 * LOWORD (DlgBaseUnits)) / 4,
          (12 * HIWORD (DlgBaseUnits)) / 8,
          HWindow,
          IDC_UNDERLINE,
          HInst,
          NULL);
     SendMessage
          (HUnderline,
          BM_SETCHECK,
          TM.tmUnderlined,
          NULL);
     ProcAddr = MakeProcInstance (EnumFaceNames,HInst);
     EnumFonts (HPrnIC,NULL,ProcAddr,NULL);
     FreeProcInstance (ProcAddr);
     Index = SendMessage
          (HFontList,
          LB_FINDSTRING,
          -1,
          (DWORD)(LPSTR)LF.lfFaceName);
     SendMessage
          (HFontList,
          LB_SETCURSEL,
          Index,
          NULL);
     SendMessage
          (HWindow,
          WM_COMMAND,
          IDC_FONTLIST,
          MAKELONG (0,LBN_SELCHANGE));
     return (TRUE);
     } /* end InitInstance */

BOOL InitProgram
     (HANDLE hInstance)
     {
     WNDCLASS WC;

                  /* Style: private device context. */
     WC.style = CS_OWNDC;
                  /* Window function. */
     WC.lpfnWndProc = MainWndProc;
                  /* Extra data for class: none. */
     WC.cbClsExtra = 0;
                  /* Extra data for instance: none. */
     WC.cbWndExtra = 0;
                  /* Program instance that owns class */
     WC.hInstance = hInstance;
                  /* Load/assign class icon.*/
     WC.hIcon = LoadIcon (NULL, IDI_APPLICATION);
                  /* Load/assign cl. cursor.*/
     WC.hCursor = LoadCursor (NULL, IDC_ARROW);
                  /* Background brush. */
     WC.hbrBackground = GetStockObject (WHITE_BRUSH);
                  /* Default class menu: none. */
     WC.lpszMenuName = NULL;
                  /* Name of window class. */
     WC.lpszClassName = "DemoClass";
                  /* Register window class/return code*/
     return (RegisterClass (&WC));
     } /* end InitProgram */

long FAR PASCAL MainWndProc
     (HWND hWnd,
     WORD wMsg,
     WORD wParam,
     LONG lParam)
{
    char Buffer [LF_FACESIZE];
    int Checked;
    HFONT HFontOld;
    HDC HWinDC;
    int i;
    int IdxDefault;
    int Index;
    PAINTSTRUCT PaintStruct;
    int PrevPoints;
    FARPROC ProcAddr;

    switch (wMsg)
    {
    case WM_COMMAND:
        switch (wParam)
        {
        case IDC_BOLD:
            switch (HIWORD (lParam))
            {
            case BN_CLICKED:
                Checked = SendMessage (HBold,BM_GETCHECK,
                                       NULL,NULL);
                LF.lfWeight = Checked ? 700 : 400;
                if (HFont != NULL)
                    DeleteObject (HFont);
                HFont = CreateFontIndirect (&LF);
                InvalidateRect (HWindow,&Rect,TRUE);
                UpdateWindow (HWindow);
                return (NULL);
            default:
                return (DefWindowProc (hWnd,wMsg,wParam,lParam));
            }
        case IDC_FONTLIST:
            switch (HIWORD (lParam))
            {
            case LBN_SELCHANGE:
                Index = SendMessage
                            (HFontList,
                            LB_GETCURSEL,
                            0,
                            0L);
                SendMessage
                    (HFontList,
                    LB_GETTEXT,
                    Index,
                    (DWORD)(LPSTR)Buffer);
                SendMessage
                    (HSizeList,
                    LB_RESETCONTENT,
                    0,
                    0L);
                NumSizes = 0;
                VectorFont = 0;
                ProcAddr = MakeProcInstance (EnumSizes,HInst);
                EnumFonts (HPrnIC, Buffer, ProcAddr,NULL);
                FreeProcInstance (ProcAddr);
                LF.lfWidth = 0;
                Checked = SendMessage (HBold,
                            BM_GETCHECK,NULL,NULL);
                LF.lfWeight = Checked ? 700 : 400;
                Checked = SendMessage (HItalic,
                            BM_GETCHECK,NULL,NULL);
                LF.lfItalic = Checked;
                Checked = SendMessage (HUnderline,
                            BM_GETCHECK,NULL,NULL);
                LF.lfUnderline = Checked;
                if (VectorFont)
                {
                    memcpy (SizeList,VectorSizes,
                                sizeof (VectorSizes));
                    NumSizes = sizeof (VectorSizes) /
                                sizeof (int);
                }
                PrevPoints = 0;
                IdxDefault = 0;
                for (i = 0; i < NumSizes; ++i)
                {
                    sprintf (Buffer,"%d",SizeList [i] / 20);
                    SendMessage
                        (HSizeList,
                        LB_ADDSTRING,
                        NULL,
                        (DWORD)(LPSTR)Buffer);
                    if (SizeList [i] > PrevPoints &&
                        SizeList [i] <= FontSize)
                    {
                        PrevPoints = SizeList [i];
                        IdxDefault = i;
                    }
                }
                SendMessage
                    (HSizeList,
                    LB_SETCURSEL,
                    IdxDefault,
                    NULL);
                SendMessage (HWindow,WM_COMMAND,IDC_SIZELIST,
                             MAKELONG (0,LBN_SELCHANGE));
                return (NULL);
            default:
                return (DefWindowProc (hWnd,wMsg,
                                       wParam,lParam));
            }
        case IDC_ITALIC:
            switch (HIWORD (lParam))
            {
            case BN_CLICKED:
                Checked = SendMessage (HItalic,BM_GETCHECK,
                                        NULL,NULL);
                LF.lfItalic = Checked;
                if (HFont != NULL)
                    DeleteObject (HFont);
                HFont = CreateFontIndirect (&LF);
                InvalidateRect (HWindow,&Rect,TRUE);
                UpdateWindow (HWindow);
                return (NULL);
            default:
                return (DefWindowProc (hWnd,wMsg,wParam,lParam));
            }
        case IDC_SIZELIST:
            switch (HIWORD(lParam))
            {
                case LBN_SELCHANGE:
                    Index = SendMessage (HSizeList,
                                LB_GETCURSEL,
                                0,
                                0L);
                    FontSize = SizeList [Index];
                    LF.lfHeight = -SizeList [Index];
                    if (HFont != NULL)
                        DeleteObject (HFont);
                    HFont = CreateFontIndirect (&LF);
                    InvalidateRect (HWindow,&Rect,TRUE);
                    UpdateWindow (HWindow);
                    return (NULL);
                default:
                    return (DefWindowProc (hWnd, wMsg,
                            wParam, lParam));
            }
        case IDC_UNDERLINE:
            switch (HIWORD (lParam))
            {
                case BN_CLICKED:
                    Checked = SendMessage (HUnderline,
                                BM_GETCHECK,NULL,NULL);
                    LF.lfUnderline = Checked;
                    if (HFont != NULL)
                        DeleteObject (HFont);
                    HFont = CreateFontIndirect (&LF);
                    InvalidateRect (HWindow,&Rect,TRUE);
                    UpdateWindow (HWindow);
                    return (NULL);
                default:
                    return (DefWindowProc (hWnd,wMsg,
                                wParam,lParam));
            }
        default:
            return (DefWindowProc (hWnd, wMsg, wParam, lParam));
        }
    case WM_DESTROY:
        PostQuitMessage (0);
        return (NULL);
    case WM_PAINT:
        HWinDC = BeginPaint (HWindow, &PaintStruct);
        HFontOld = SelectObject (HWinDC,HFont);
        TextOut
            (HWinDC,
            0,
            0,
            "Abcdefghijklmnopqrstuvwxyz",
            26);
        SelectObject (HWinDC,HFontOld);
        EndPaint (HWindow,&PaintStruct);
        return (NULL);
    default:
        return (DefWindowProc (hWnd, wMsg, wParam, lParam));
    } /* end switch */
} /* end MainWndProc */