home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol12n07.zip / FNTPRN.ZIP / FNTSRC.ZIP / FNTPRN.C < prev    next >
Text File  |  1993-03-16  |  16KB  |  357 lines

  1. // Contents copyright (c) 1993 John Deurbrouck
  2. /*
  3. **  Includes
  4. */
  5. #include<windows.h>
  6. #include<commdlg.h>
  7. #include<stdlib.h>
  8. #include<ctype.h>
  9. #include<string.h>
  10. #include"controls.h"
  11. #include"enumfont.h"
  12.  
  13. /*
  14. **  Defines
  15. */
  16. #define IDD_GETGOING 200
  17. #define IDM_ABOUT 1
  18. #define IDM_HELP 2
  19.  
  20. /*
  21. **  Global Variables
  22. */
  23. int OldLfHeight;
  24. HCURSOR hHourGlass;                     // global so call LoadCursor() just once
  25.  
  26. /*
  27. **  Function Prototypes
  28. */
  29. long FAR PASCAL _export WndProc(HWND,UINT,UINT,LONG);
  30. static void show_default_text_settings(HWND hwndDesc);
  31.  
  32. /*
  33. **  Function Definitions
  34. */
  35. #pragma argsused
  36. int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow){
  37.     MSG msg;
  38.     HWND hwnd;
  39.     WNDCLASS wndclass;
  40.     static LPSTR appname="FntPrn";
  41.     instance=hInstance;
  42.     hHourGlass=LoadCursor(NULL,IDC_WAIT);
  43.     // verify we're in Win 3.1 or better
  44.     {
  45.         DWORD dwVersion=GetVersion();
  46.         unsigned int winver=((unsigned int)LOBYTE(LOWORD(dwVersion)))<<8;
  47.         winver|=(unsigned int)HIBYTE(LOWORD(dwVersion));
  48.         if(winver<0x30A){
  49.             MessageBox(NULL,"FntPrn requires Windows 3.1 or later",
  50.              "ERROR",MB_OK|MB_ICONSTOP);
  51.             return 1;
  52.         }
  53.     }
  54.     if(!hPrevInstance){
  55.         wndclass.style=CS_HREDRAW|CS_VREDRAW;
  56.         wndclass.lpfnWndProc=WndProc;
  57.         wndclass.cbClsExtra=0;
  58.         wndclass.cbWndExtra=DLGWINDOWEXTRA;
  59.         wndclass.hInstance=hInstance;
  60.         wndclass.hIcon=LoadIcon(hInstance,"FntPrn2");
  61.         wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
  62.         wndclass.hbrBackground=COLOR_WINDOW+1;
  63.         wndclass.lpszMenuName=NULL;
  64.         wndclass.lpszClassName=appname;
  65.         RegisterClass(&wndclass);
  66.     }
  67.     hwnd=CreateDialog(hInstance,appname,0,NULL);
  68.     if(!hwnd){
  69.         MessageBox(NULL,"Could not create window","ERROR",MB_OK|MB_ICONSTOP);
  70.         return 1;
  71.     }
  72.     hwndDialog=hwnd;
  73.     // add new options to System Menu
  74.     {
  75.         HMENU hMenu;
  76.         hMenu=GetSystemMenu(hwnd,FALSE);
  77.         if(hMenu!=NULL){
  78.             EnableMenuItem(hMenu,SC_MAXIMIZE,MF_BYCOMMAND|MF_GRAYED);
  79.             EnableMenuItem(hMenu,SC_SIZE,MF_BYCOMMAND|MF_GRAYED);
  80.             AppendMenu(hMenu,MF_SEPARATOR,0,NULL);
  81.             AppendMenu(hMenu,MF_STRING,IDM_ABOUT,"&About...");
  82.             AppendMenu(hMenu,MF_STRING,IDM_HELP,"&Help...");
  83.         }
  84.     }
  85.     // get dialog box initialized
  86.     PostMessage(hwnd,WM_COMMAND,IDD_GETGOING,0L);
  87.     ShowWindow(hwnd,nCmdShow);
  88.     while(GetMessage(&msg,NULL,0,0)){
  89.         // next line intercepts ESC key so IsDialogMessage() can't exit on it
  90.         if((msg.message==WM_KEYDOWN&&msg.wParam==VK_ESCAPE)||(!IsDialogMessage(hwnd,&msg))){
  91.             TranslateMessage (&msg) ;
  92.             DispatchMessage (&msg) ;
  93.         }
  94.     }
  95.     return msg.wParam ;
  96. }
  97.  
  98. // this is a dialog box procedure but is also the main window
  99. long FAR PASCAL _export WndProc(HWND hwnd,UINT message,UINT wParam,LONG lParam){
  100.     static HWND hwndSampletext,hwndJustification,hwndSize,hwndDesc;
  101.     HCURSOR hSaveCursor;
  102.     switch(message){
  103.     case WM_KEYDOWN:
  104.         if(wParam==VK_ESCAPE)return 1;
  105.         break;
  106.     case WM_SYSCOMMAND:
  107.         switch(wParam){
  108.         case IDM_ABOUT:
  109.             MessageBox(hwndDialog,
  110.             "FntPrn 1.0\n"
  111.             "Font Printing Utility\n"
  112.             "Copyright ⌐ 1993 John Deurbrouck\n\n"
  113.             "First Published in PC Magazine April 13, 1993",
  114.             "FontPrint",MB_OK);
  115.             SetFocus(hwndSortorder);
  116.             return 1;
  117.         case IDM_HELP:
  118.             MessageBox(hwndDialog,
  119.             "FntPrn helps you browse your fonts.\n\n"
  120.             "You choose which fonts to see and how to sort them (Fonts section), "
  121.             "what you want to see in each font (Sample Text section), "
  122.             "and how you want the font names to look (Description Formatting section).\n\n"
  123.             "FntPrn creates a file (FNTPRN.WRI by default) "
  124.             "and optionally starts Write so you can browse or print the report.\n\n"
  125.             "See PC Magazine, April 13, 1993 (Vol. 12 No. 7) for more details.",
  126.             "FontPrint Help",MB_OK);
  127.             SetFocus(hwndSortorder);
  128.             return 1;
  129.         }
  130.         break;
  131.     case WM_COMMAND:
  132.         switch(wParam){
  133.         case IDD_GETGOING:
  134.             // do this instead of WM_INITDIALOG since child windows
  135.             // won't have been created by that time
  136.             hwndSortorder=GetDlgItem(hwnd,IDD_SORTORDER);
  137.             SendMessage(hwndSortorder,CB_INSERTSTRING,0,(LONG)(LPSTR)"Alphabetical");
  138.             SendMessage(hwndSortorder,CB_INSERTSTRING,1,(LONG)(LPSTR)"Height");
  139.             SendMessage(hwndSortorder,CB_INSERTSTRING,2,(LONG)(LPSTR)"Width");
  140.             SendMessage(hwndSortorder,CB_INSERTSTRING,3,(LONG)(LPSTR)"Proportion (Height/Width)");
  141.             SendMessage(hwndSortorder,CB_INSERTSTRING,4,(LONG)(LPSTR)"Weight");
  142.             SendMessage(hwndSortorder,CB_INSERTSTRING,5,(LONG)(LPSTR)"Font Family");
  143.             hwndSampletext=GetDlgItem(hwnd,IDD_SAMPLETEXT);
  144.             SendMessage(hwndSampletext,CB_INSERTSTRING,0,(LONG)(LPSTR)"\"AENOPS abefglmoqsty 801\"");
  145.             SendMessage(hwndSampletext,CB_INSERTSTRING,1,(LONG)(LPSTR)"Text Paragraph");
  146.             SendMessage(hwndSampletext,CB_INSERTSTRING,2,(LONG)(LPSTR)"Alphabet");
  147.             SendMessage(hwndSampletext,CB_INSERTSTRING,3,(LONG)(LPSTR)"\"Sample Text\"");
  148.             hwndJustification=GetDlgItem(hwnd,IDD_JUSTIFICATION);
  149.             SendMessage(hwndJustification,CB_INSERTSTRING,0,(LONG)(LPSTR)"Justified");
  150.             SendMessage(hwndJustification,CB_INSERTSTRING,1,(LONG)(LPSTR)"Left");
  151.             SendMessage(hwndJustification,CB_INSERTSTRING,2,(LONG)(LPSTR)"Center");
  152.             SendMessage(hwndJustification,CB_INSERTSTRING,3,(LONG)(LPSTR)"Right");
  153.             hwndSize=GetDlgItem(hwnd,IDD_SIZE);
  154.             hwndDesc=GetDlgItem(hwnd,IDD_SHOWDESCRIPTION);
  155.             // fall through to set defaults...
  156.         case IDD_DEFAULTS:
  157.             SendMessage(hwndSortorder,CB_SETCURSEL,0,0L);
  158.             SendMessage(hwndSampletext,CB_SETCURSEL,0,0L);
  159.             SendMessage(hwndJustification,CB_SETCURSEL,1,0L);
  160.             CheckRadioButton(hwnd,IDD_PITCHBOTH,IDD_PITCHVARIABLE,IDD_PITCHBOTH);
  161.             CheckRadioButton(hwnd,IDD_DEVICEPRINTER,IDD_DEVICESCREEN,IDD_DEVICEPRINTER);
  162.             CheckDlgButton(hwnd,IDD_SYNTHESIS,1);
  163.             CheckDlgButton(hwnd,IDD_TRUETYPEONLY,0);
  164.             CheckDlgButton(hwnd,IDD_NOVELTY,1);
  165.             CheckDlgButton(hwnd,IDD_MODERN,1);
  166.             CheckDlgButton(hwnd,IDD_ROMAN,1);
  167.             CheckDlgButton(hwnd,IDD_SANSSERIF,1);
  168.             CheckDlgButton(hwnd,IDD_SCRIPT,1);
  169.             CheckDlgButton(hwnd,IDD_OTHERFONTS,1);
  170.             CheckDlgButton(hwnd,IDD_REGULAR,1);
  171.             CheckDlgButton(hwnd,IDD_ITALIC,1);
  172.             CheckDlgButton(hwnd,IDD_BOLD,1);
  173.             CheckDlgButton(hwnd,IDD_BOLDITALIC,1);
  174.             CheckDlgButton(hwnd,IDD_LAUNCHWRITE,1);
  175.             SendMessage(hwndSize,WM_SETTEXT,0,(LONG)(LPSTR)"18");
  176.             default_font="Arial";
  177.             default_ffid=FF_SWISS;
  178.             default_pointsize=16;
  179.             default_just=JUST_LEFT;
  180.             default_bolditalic=0;
  181.             {                           // adjust points to logical units
  182.                 HDC hdc=CreateIC("DISPLAY",NULL,NULL,NULL);
  183.                 if(hdc==NULL)OldLfHeight=10;
  184.                 else{
  185.                     OldLfHeight=MulDiv(-(default_pointsize/2),
  186.                      GetDeviceCaps(hdc,LOGPIXELSY),72);
  187.                     DeleteDC(hdc);
  188.                 }
  189.             }
  190.             show_default_text_settings(hwndDesc);
  191.             SetFocus(hwndSortorder);
  192.             return 1;
  193.         case IDOK:                      // make the file
  194.             for(;;){
  195.                 SetCapture(hwnd);
  196.                 if(hHourGlass!=NULL)hSaveCursor=SetCursor(hHourGlass);
  197.                 switch(SendMessage(hwndSortorder,CB_GETCURSEL,0,0L)){
  198.                 default:
  199.                 case 0: sortorder=ALPHA; break;
  200.                 case 1: sortorder=HEIGHT; break;
  201.                 case 2: sortorder=WIDTH; break;
  202.                 case 3: sortorder=NARROWNESS; break;
  203.                 case 4: sortorder=WEIGHT; break;
  204.                 case 5: sortorder=FONTTYPE; break;
  205.                 }
  206.                 if(IsDlgButtonChecked(hwnd,IDD_PITCHFIXED))monospace=MONO;
  207.                 else if(IsDlgButtonChecked(hwnd,IDD_PITCHVARIABLE))monospace=VAR;
  208.                 else monospace=VARMONO;
  209.                 use_printer_context=IsDlgButtonChecked(hwnd,IDD_DEVICEPRINTER)?1:0;
  210.                 allow_synthesis=IsDlgButtonChecked(hwnd,IDD_SYNTHESIS)?1:0;
  211.                 truetype_only=IsDlgButtonChecked(hwnd,IDD_TRUETYPEONLY)?1:0;
  212.                 incl_regular=IsDlgButtonChecked(hwnd,IDD_REGULAR)?1:0;
  213.                 incl_bold=IsDlgButtonChecked(hwnd,IDD_BOLD)?1:0;
  214.                 incl_italic=IsDlgButtonChecked(hwnd,IDD_ITALIC)?1:0;
  215.                 incl_bolditalic=IsDlgButtonChecked(hwnd,IDD_BOLDITALIC)?1:0;
  216.                 if(!(incl_regular+incl_bold+incl_italic+incl_bolditalic)){
  217.                     MessageBox(hwnd,"Must select at least one style","Error",MB_OK);
  218.                     SetFocus(GetDlgItem(hwnd,IDD_REGULAR));
  219.                     break;
  220.                 }
  221.                 incl_novelty=IsDlgButtonChecked(hwnd,IDD_NOVELTY)?1:0;
  222.                 incl_modern=IsDlgButtonChecked(hwnd,IDD_MODERN)?1:0;
  223.                 incl_roman=IsDlgButtonChecked(hwnd,IDD_ROMAN)?1:0;
  224.                 incl_sanserif=IsDlgButtonChecked(hwnd,IDD_SANSSERIF)?1:0;
  225.                 incl_script=IsDlgButtonChecked(hwnd,IDD_SCRIPT)?1:0;
  226.                 incl_other=IsDlgButtonChecked(hwnd,IDD_OTHERFONTS)?1:0;
  227.                 if(!(incl_novelty+incl_modern+incl_roman+incl_sanserif+
  228.                 incl_script+incl_other)){
  229.                     MessageBox(hwnd,"Must select at least one font family","Error",MB_OK);
  230.                     SetFocus(GetDlgItem(hwnd,IDD_ROMAN));
  231.                     break;
  232.                 }
  233.                 switch(SendMessage(hwndSampletext,CB_GETCURSEL,0,0L)){
  234.                 default:
  235.                 case 0: sample_text_ptr="AENOPS abefglmoqsty 801\r\n"; break;
  236.                 case 1: sample_text_ptr="\tThis sample text paragraph is designed to "
  237.                     "demonstrate the readability and appearance of this font in "
  238.                     "full text paragraphs. For best results, import this into a word "
  239.                     "processor and set up a three-column format.\r\n"; break;
  240.                 case 2: sample_text_ptr="ABCDEFGHIJKLMNOPQRSTUVWXYZ "
  241.                     "abcdefghijklmnopqrstuvwxyz 1234567890 "
  242.                     "`~!@#$%^&*()_+-=[]{}\\|;:'\",.<>/?\r\n"; break;
  243.                 case 3: sample_text_ptr="Sample Text\r\n"; break;
  244.                 }
  245.                 sample_text_length=lstrlen(sample_text_ptr);
  246.                 for(;;){
  247.                     char buf[10],*ptr;
  248.                     int success;
  249.                     sample_text_pointsize=0;
  250.                     if(SendMessage(hwndSize,WM_GETTEXTLENGTH,0,0L)>3)break;
  251.                     SendMessage(hwndSize,WM_GETTEXT,10,(LPARAM)(LPSTR)buf);
  252.                     ptr=buf;
  253.                     success=1;
  254.                     while(*ptr){
  255.                         if(!isdigit(*ptr))success=0;
  256.                         ptr++;
  257.                     }
  258.                     if(success)sample_text_pointsize=atoi(buf);
  259.                     break;
  260.                 }
  261.                 if(sample_text_pointsize<6||sample_text_pointsize>72){
  262.                     MessageBox(hwnd,"Legal sizes are from 6 to 72","Error",MB_OK);
  263.                     SetFocus(hwndSize);
  264.                     break;
  265.                 }
  266.                 sample_text_pointsize*=2;
  267.                 switch(SendMessage(hwndJustification,CB_GETCURSEL,0,0L)){
  268.                 default:
  269.                 case 0: sample_text_just=JUST_JUST; break;
  270.                 case 1: sample_text_just=JUST_LEFT; break;
  271.                 case 2: sample_text_just=JUST_CENTER; break;
  272.                 case 3: sample_text_just=JUST_RIGHT; break;
  273.                 }
  274.                 launchwrite=IsDlgButtonChecked(hwnd,IDD_LAUNCHWRITE)?1:0;
  275.                 generate_file_from_options();
  276.                 break;
  277.             }
  278.             if(hHourGlass!=NULL)SetCursor(hSaveCursor);
  279.             ReleaseCapture();
  280.             return 1;
  281.         case IDCANCEL:                  // bail out
  282.             PostMessage(hwnd,WM_SYSCOMMAND,SC_CLOSE,0L);
  283.             return 1;
  284.         case IDD_SETDESCRIPTION:        // will do ChooseFont()
  285.             {
  286.                 static char picked_face[LF_FACESIZE],picked_style[LF_FACESIZE];
  287.                 char stylestring[LF_FACESIZE];
  288.                 CHOOSEFONT cf;
  289.                 LOGFONT lf;
  290.                 memset(&cf,0,sizeof(cf));
  291.                 memset(&lf,0,sizeof(lf));
  292.                 cf.lStructSize=sizeof(cf);
  293.                 cf.hwndOwner=hwnd;
  294.                 cf.lpLogFont=&lf;
  295.                 cf.Flags=CF_ANSIONLY|CF_INITTOLOGFONTSTRUCT|
  296.                  CF_LIMITSIZE|CF_USESTYLE|CF_SCREENFONTS;
  297.                 cf.lpszStyle=(LPSTR)&stylestring;
  298.                 cf.nFontType=REGULAR_FONTTYPE;
  299.                 cf.nSizeMin=6;
  300.                 cf.nSizeMax=72;
  301.                 if(default_font!=(LPSTR)&picked_face){
  302.                     lstrcpy(picked_face,default_font);
  303.                     lstrcpy(picked_style,"Regular");
  304.                 }
  305.                 lstrcpy(lf.lfFaceName,picked_face);
  306.                 lf.lfHeight=OldLfHeight;
  307.                 lstrcpy(stylestring,picked_style);
  308.                 if(!default_just)cf.nFontType=REGULAR_FONTTYPE;
  309.                 if(default_just&ITALIC_BIT){
  310.                     lf.lfItalic=1;
  311.                     cf.nFontType|=ITALIC_FONTTYPE;
  312.                 }
  313.                 if(default_just&BOLD_BIT){
  314.                     lf.lfWeight=FW_BOLD;
  315.                     cf.nFontType|=BOLD_FONTTYPE;
  316.                 }
  317.                 else lf.lfWeight=FW_NORMAL;
  318.                 lf.lfPitchAndFamily=default_ffid;
  319.                 if(ChooseFont(&cf)){
  320.                     lstrcpy(picked_face,lf.lfFaceName);
  321.                     default_font=picked_face;
  322.                     default_ffid=lf.lfPitchAndFamily;
  323.                     default_pointsize=cf.iPointSize/5;
  324.                     default_bolditalic=lf.lfItalic?ITALIC_BIT:0;
  325.                     OldLfHeight=lf.lfHeight;
  326.                     lstrcpy(picked_style,stylestring);
  327.                     if(lf.lfWeight>FW_NORMAL)default_bolditalic|=BOLD_BIT;
  328.                     show_default_text_settings(hwndDesc);
  329.                 }
  330.             }
  331.             SetFocus(hwndSortorder);
  332.             return 1;
  333.         }
  334.         break;
  335.     case WM_DESTROY:
  336.         PostQuitMessage(0);
  337.         return 0;
  338.     }
  339.     return DefWindowProc(hwnd,message,wParam,lParam);
  340. }
  341. // puts description line's formatting into static text
  342. // control on dialog box
  343. static void show_default_text_settings(HWND hwndDesc){
  344.     char buf[LF_FACESIZE+100],*boldit;
  345.     if(default_bolditalic&BOLD_BIT){
  346.         if(default_bolditalic&ITALIC_BIT)boldit="Bold Italic";
  347.         else boldit="Bold";
  348.     }
  349.     else{
  350.         if(default_bolditalic&ITALIC_BIT)boldit="Italic";
  351.         else boldit="Regular";
  352.     }
  353.     wsprintf(buf,"%s, %d pt, %s",
  354.         (LPSTR)default_font,default_pointsize/2,(LPSTR)boldit);
  355.     SendMessage(hwndDesc,WM_SETTEXT,0,(LONG)(LPSTR)buf);
  356. }
  357.