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 / fontdlg.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  9KB  |  374 lines

  1. /*++
  2.  
  3. Copyright (c) 1993-1997 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     fontdlg.c
  8.  
  9. Abstract:
  10.  
  11.     Font Editor interface to the common dialog Open File and Save File
  12.     functions.  Also, this routine displays and controls the font format
  13.     save dialog.
  14.  
  15.  
  16. --*/
  17.  
  18.  
  19. #include "windows.h"
  20. #include <windowsx.h>
  21. #include "fontedit.h"
  22. #include "commdlg.h"
  23.  
  24.  
  25. /* message box strings loaded in sample.c from the stringtable */
  26. extern CHAR szIFN[], szFNF[], szREF[], szSCC[], szEOF[], szECF[];
  27.  
  28. extern CHAR    szAppName [];
  29. extern CHAR    szExt [];
  30. extern CHAR    szExtDesc [];
  31.  
  32. extern CHAR    szNEWFONT [];
  33. extern CHAR    szFRO [];
  34.  
  35. extern INT iFontFormat;             /* Set to the id of current font format */
  36. extern BOOL fReadOnly;
  37.  
  38. CHAR szDlgMsg [MAX_STR_LEN+MAX_FNAME_LEN];
  39.  
  40. extern    CHAR szFilter[];
  41.  
  42. //
  43. // Local Function Prototypes.
  44. //
  45.  
  46. BOOL
  47. DlgCheckFormat (
  48.     HANDLE hInstance,       // app module instance handle
  49.     HWND   hWndParent       // window handle of parent window
  50.     );
  51.  
  52. BOOL APIENTRY
  53. DlgFnCheckFormat (
  54.     HWND   hDlg,
  55.     WORD   message,
  56.     WPARAM wParam,
  57.     LPARAM lParam
  58.     );
  59.  
  60. //
  61. // Functions.
  62. //
  63.  
  64. BOOL
  65. CommDlgOpen (
  66.     HWND   hWndParent,      /* window handle of parent window */
  67.     OFSTRUCT *pofsReOpenInfo,/* ptr to current file OFSTRUCT (->cBytes=0 if no
  68.                              * cur. file)*/
  69.     CHAR  *pszNewNameIn,    /* ptr to array which will get new file's name
  70.                              * (no path) */
  71.     CHAR  *pszExtIn,        /* ptr to current default extension */
  72.     CHAR  *pszFileNameOnly,    /* ptr to application name */
  73.     BOOL   fOpenType
  74.     )
  75. {
  76.     OPENFILENAME    ofTempOF;
  77.     HFILE            hFile;
  78.         CHAR            szBuf[_MAX_PATH];
  79.  
  80.     ofTempOF.lStructSize =        sizeof(OPENFILENAME);
  81.     ofTempOF.hwndOwner =        hWndParent;
  82.     ofTempOF.lpstrFilter =        szFilter;
  83.     ofTempOF.lpstrCustomFilter =    (LPSTR)NULL;
  84.     ofTempOF.nMaxCustFilter =    0L;
  85.     ofTempOF.nFilterIndex =        0L;
  86.     ofTempOF.lpstrFile =        pszNewNameIn;
  87.     ofTempOF.nMaxFile =        MAX_FNAME_LEN;
  88.     ofTempOF.lpstrFileTitle =    pszFileNameOnly;
  89.     ofTempOF.nMaxFileTitle =    MAX_FNAME_LEN;
  90.     ofTempOF.lpstrInitialDir =    (LPSTR)NULL;
  91.     ofTempOF.lpstrTitle =        (LPSTR)NULL;
  92.     ofTempOF.Flags =        OFN_SHOWHELP;
  93.     ofTempOF.nFileOffset =        0;
  94.     ofTempOF.nFileExtension =    0;
  95.     ofTempOF.lpstrDefExt =        pszExtIn;
  96.  
  97.     if (fOpenType == FONT_NEW)
  98.     {
  99.         if (MessageBox (hWndParent, (LPSTR)szNEWFONT, (LPSTR)szAppName,
  100.                 MB_OKCANCEL | MB_ICONEXCLAMATION | MB_APPLMODAL) == IDCANCEL)
  101.         {
  102.             return (FALSE);
  103.         }
  104.     }
  105.  
  106.         lstrcpy(szBuf,pszNewNameIn);
  107.     if (GetOpenFileName (&ofTempOF) == FALSE)
  108.     {
  109.             lstrcpy(pszNewNameIn,szBuf);
  110.         return (FALSE);
  111.     }
  112.  
  113.     CharUpper (pszNewNameIn);
  114.  
  115.     fReadOnly = FALSE;
  116.  
  117.     hFile = (HFILE)OpenFile (pszNewNameIn, pofsReOpenInfo, OF_READWRITE);
  118.  
  119.     if (hFile == (HFILE) -1) {
  120.  
  121.         hFile = (HFILE)OpenFile (pszNewNameIn, pofsReOpenInfo, OF_READ);
  122.  
  123.         if (hFile == (HFILE) -1) {
  124.  
  125.             DlgMergeStrings (szFNF, pszNewNameIn, szDlgMsg);
  126.  
  127.             MessageBox (hWndParent, szDlgMsg, szAppName,
  128.                     MB_OK | MB_ICONASTERISK | MB_APPLMODAL);
  129.  
  130.             return (FALSE);
  131.  
  132.         } else if (fOpenType != FONT_NEW) {
  133.             
  134.             BOOL    fResult;
  135.  
  136.             DlgMergeStrings (szFRO, pszNewNameIn, szDlgMsg);
  137.  
  138.             /* File Is Read Only */
  139.             fResult = MessageBox (hWndParent, szDlgMsg, szAppName,
  140.                     MB_OKCANCEL | MB_ICONEXCLAMATION | MB_APPLMODAL);
  141.  
  142.             /* Give them the chance to cancel right now. */
  143.             if (fResult == IDCANCEL) {
  144.  
  145.                 _lclose((HFILE)hFile);
  146.                 return (FALSE);
  147.             }
  148.  
  149.             fReadOnly = TRUE;
  150.         }
  151.     }
  152.  
  153.     _lclose((HFILE)hFile);
  154.  
  155.     return (TRUE);
  156. }
  157.  
  158. BOOL
  159. CommDlgSaveAs(
  160.     HANDLE hInstance,
  161.     HWND   hWndParent,      /* window handle of parent window */
  162.     OFSTRUCT *pofsReOpenInfo,/* ptr to current file OFSTRUCT (->cBytes=0 if no
  163.                              * cur. file)*/
  164.     CHAR  *pszNewNameIn,    /* ptr to array which will get new file's name
  165.                              * (no path) */
  166.     CHAR  *pszExtIn,        /* ptr to current default extension */
  167.     CHAR  *pszFileNameOnly  /* ptr to file name */
  168.     )
  169. {
  170.     OPENFILENAME    ofTempOF;
  171.     HFILE            hFile;
  172.         CHAR szBuf[_MAX_PATH];
  173.  
  174.     ofTempOF.lStructSize =        sizeof(OPENFILENAME);
  175.     ofTempOF.hwndOwner =        hWndParent;
  176.     ofTempOF.lpstrFilter =        szFilter;
  177.     ofTempOF.lpstrCustomFilter =    (LPSTR)NULL;
  178.     ofTempOF.nMaxCustFilter =    0L;
  179.     ofTempOF.nFilterIndex =        0L;
  180.     ofTempOF.lpstrFile =        pszNewNameIn;
  181.     ofTempOF.nMaxFile =        MAX_FNAME_LEN;
  182.     ofTempOF.lpstrFileTitle =    pszFileNameOnly;
  183.     ofTempOF.nMaxFileTitle =    MAX_FNAME_LEN;
  184.     ofTempOF.lpstrInitialDir =    (LPSTR)NULL;
  185.     ofTempOF.lpstrTitle =        (LPSTR)NULL;
  186.     ofTempOF.Flags =        OFN_SHOWHELP;
  187.     ofTempOF.nFileOffset =        0;
  188.     ofTempOF.nFileExtension =    0;
  189.     ofTempOF.lpstrDefExt =        pszExtIn;
  190.  
  191.     if (DlgCheckFormat (hInstance, hWndParent) == FALSE)
  192.     {
  193.         return (FALSE);
  194.     }
  195.  
  196.         // save lpstrFile. Because if GetSaveFileName returns 0(i.e. select
  197.         // [Cancel] in dialog, pointer to lpstrFile will be lost.
  198.         lstrcpy(szBuf,pszNewNameIn);
  199.  
  200.     if (GetSaveFileName (&ofTempOF) == FALSE)
  201.     {
  202.             lstrcpy(pszNewNameIn,szBuf);
  203.         return (FALSE);
  204.     }
  205.  
  206.     CharUpper (pszNewNameIn);
  207.  
  208.     hFile = (HFILE)OpenFile (pszNewNameIn, pofsReOpenInfo, OF_EXIST);
  209.  
  210.     if (hFile >= (HFILE) 0) /* already exists */
  211.     {
  212.         _lclose((HFILE)hFile);
  213.  
  214.         DlgMergeStrings (szREF, pszNewNameIn, szDlgMsg);
  215.  
  216.         if (MessageBox (hWndParent, (LPSTR)szDlgMsg, (LPSTR)pszNewNameIn,
  217.                 MB_YESNO | MB_DEFBUTTON2 | MB_ICONQUESTION | MB_APPLMODAL)
  218.                 == IDNO)
  219.         {
  220.             return (FALSE);
  221.         }
  222.  
  223.         hFile = (HFILE)OpenFile (pszNewNameIn, pofsReOpenInfo, OF_WRITE);
  224.  
  225.         if (hFile == (HFILE) -1)
  226.         {
  227.             DlgMergeStrings(szEOF, pszNewNameIn, szDlgMsg);
  228.  
  229.             MessageBox(hWndParent, (LPSTR)szDlgMsg, (LPSTR)pszNewNameIn,
  230.                     MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL);
  231.  
  232.             return (FALSE);
  233.         }
  234.  
  235.         _lclose((HFILE)hFile);
  236.  
  237.         return (TRUE);
  238.     }
  239.  
  240.     hFile = (HFILE)OpenFile (pszNewNameIn, pofsReOpenInfo, OF_CREATE);
  241.  
  242.     if (hFile == (HFILE) -1)
  243.     {
  244.         DlgMergeStrings(szECF, pszNewNameIn, szDlgMsg);
  245.  
  246.         MessageBox(hWndParent, (LPSTR)szDlgMsg, (LPSTR)pszNewNameIn,
  247.                 MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL);
  248.  
  249.         return (FALSE);
  250.     }
  251.  
  252.     _lclose((HFILE)hFile);
  253.  
  254.     return (TRUE);
  255.  
  256. } /* end dlgsaveas */
  257.  
  258.  
  259. /*=============================================================================
  260.  DLGMERGESTRINGS scans string1 for merge spec (%%). If found, insert string2 at
  261.  that point, and then append remainder of string1.  Result in string3.
  262. ==============================================================================*/
  263. BOOL
  264. DlgMergeStrings(
  265.     CHAR   *szSrc,
  266.     CHAR   *szMerge,
  267.     CHAR   *szDst
  268.     )
  269. {
  270.     CHAR *pchSrc;
  271.     CHAR *pchDst;
  272.  
  273.     pchSrc = szSrc;
  274.     pchDst = szDst;
  275.  
  276.     /* Find merge spec if there is one. */
  277.     while (!((*pchSrc == '%') && (*(pchSrc+1) == '%')))  {
  278.         *pchDst++ = *pchSrc;
  279.  
  280.         /* If we reach end of string before merge spec, just return. */
  281.         if (!*pchSrc++)
  282.             return FALSE;
  283.  
  284.     }
  285.     /* If merge spec found, insert sz2 there. (check for null merge string */
  286.     if (szMerge) {
  287.         while (*szMerge)
  288.             *pchDst++ = *szMerge++;
  289.  
  290.     }
  291.  
  292.     /* Jump over merge spec */
  293.     pchSrc++; pchSrc++;
  294.  
  295.  
  296.     /* Now append rest of Src String */
  297.     while (*pchDst++ = *pchSrc++);
  298.     return TRUE;
  299.  
  300. } /* end dlgmergestrings */
  301.  
  302.  
  303. BOOL
  304. DlgCheckFormat (
  305.     HANDLE hInstance,       // app module instance handle
  306.     HWND   hWndParent       // window handle of parent window
  307.     )
  308. {
  309.     //FARPROC  lpProc;
  310.     BOOL     fResult;
  311.  
  312.     fResult = DialogBox (hInstance, (LPSTR)MAKEINTRESOURCE (IDD_FORMAT),
  313.             //hWndParent, (WNDPROC)(lpProc = DlgFnCheckFormat));
  314.             hWndParent, (WNDPROC)DlgFnCheckFormat);
  315.  
  316.     //FreeProcInstance(lpProc);
  317.  
  318.     return (fResult);
  319.  
  320. } /* end dlgcheckformat */
  321.  
  322.  
  323. BOOL APIENTRY
  324. DlgFnCheckFormat (
  325.     HWND   hDlg,
  326.     WORD   message,
  327.     WPARAM wParam,
  328.     LPARAM lParam
  329.     )
  330. {
  331.  
  332.     switch (message)
  333.     {
  334.  
  335.         case WM_INITDIALOG:
  336.             CheckRadioButton (hDlg, ID_FORMAT2, ID_FORMAT3, iFontFormat);
  337.             break;
  338.  
  339.         case WM_COMMAND:
  340.  
  341.             switch (LOWORD(wParam))
  342.             {
  343.                 case IDOK:
  344.  
  345.                     EndDialog (hDlg, TRUE);
  346.                     break;
  347.  
  348.                 case IDCANCEL:
  349.                     EndDialog (hDlg, FALSE);
  350.                     break;
  351.  
  352.                 case ID_FORMAT2:
  353.                 case ID_FORMAT3:
  354.                     CheckRadioButton(hDlg, ID_FORMAT2,ID_FORMAT3,
  355.                             iFontFormat = LOWORD(wParam));
  356.                     break;
  357.  
  358.                 default:
  359.  
  360.                     return FALSE;
  361.  
  362.             } /* end switch wparam */
  363.             break;
  364.  
  365.         default:
  366.  
  367.             return FALSE;
  368.  
  369.     } /* end switch message */
  370.  
  371.     return TRUE;
  372.  
  373. } /* end dlgsaveasdlg */
  374.