home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 9A < prev    next >
Encoding:
Text File  |  1991-10-30  |  15.1 KB  |  475 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  MODULE    : MpFile.c                                                   *
  4.  *                                                                         *
  5.  *  PURPOSE   : Contains the code for File I/O for Multipad.               *
  6.  *                                                                         *
  7.  *  FUNCTIONS : AlreadyOpen   - Determines if a file is already open.      *
  8.  *                                                                         *
  9.  *              AddFile       - Creates a new MDI window and, if specified,*
  10.  *                loads a file into it.                      *
  11.  *                                                                         *
  12.  *              LoadFile      - Loads a file into a MDI window.            *
  13.  *                                                                         *
  14.  *              ReadFile      - Calls File/Open dialog and appropriately   *
  15.  *                              responds to the user's input.              *
  16.  *                                                                         *
  17.  *              SaveFile      - Saves the contents of a MDI window's edit  *
  18.  *                              control to a file.                         *
  19.  *                                                                         *
  20.  *              SetSaveFrom   - Formats the "Save 'file' to" string.       *
  21.  *                                                                         *
  22.  *              SaveAsDlgProc - Dialog function for the File/SaveAs dialog.*
  23.  *                                                                         *
  24.  *              ChangeFile    - Calls File/SaveAs dialog.                  *
  25.  *                                                                         *
  26.  ***************************************************************************/
  27. #include "multipad.h"
  28.  
  29. VOID NEAR PASCAL GetFileName(PSTR);
  30.  
  31. OFSTRUCT    of;
  32. /****************************************************************************
  33.  *                                                                          *
  34.  *  FUNCTION   : AlreadyOpen(szFile)                                        *
  35.  *                                                                          *
  36.  *  PURPOSE    : Checks to see if the file described by the string pointed  *
  37.  *               to by 'szFile' is already open.                            *
  38.  *                                                                          *
  39.  *  RETURNS    : a handle to the described file's window if that file is    *
  40.  *               already open;  NULL otherwise.                             *
  41.  *                                                                          *
  42.  ****************************************************************************/
  43.  
  44. HWND AlreadyOpen(char *szFile)
  45. {
  46.     int     iDiff;
  47.     HWND    hwndCheck;
  48.     char    szChild[64];
  49.     LPSTR   lpChild, lpFile;
  50.     int     wFileTemp;
  51.  
  52.     /* Open the file with the OF_PARSE flag to obtain the fully qualified
  53.      * pathname in the OFSTRUCT structure.
  54.      */
  55.     wFileTemp = OpenFile ((LPSTR)szFile, (LPOFSTRUCT)&of, OF_PARSE);
  56.     if (! wFileTemp)
  57.     return(NULL);
  58.     _lclose (wFileTemp);
  59.  
  60.     /* Check each MDI child window in Multipad */
  61.     for (   hwndCheck = GetWindow(hwndMDIClient, GW_CHILD);
  62.         hwndCheck;
  63.         hwndCheck = GetWindow(hwndCheck, GW_HWNDNEXT)   ) {
  64.     /* Initialization  for comparison */
  65.     lpChild = szChild;
  66.     lpFile = AnsiUpper((LPSTR) of.szPathName);
  67.     iDiff = 0;
  68.  
  69.     /* Skip icon title windows */
  70.     if (GetWindow(hwndCheck, GW_OWNER))
  71.         continue;
  72.  
  73.     /* Get current child window's name */
  74.     GetWindowText(hwndCheck, lpChild, 64);
  75.  
  76.     /* Compare window name with given name */
  77.     while ((*lpChild) && (*lpFile) && (!iDiff)){
  78.         if (*lpChild++ != *lpFile++)
  79.         iDiff = 1;
  80.     }
  81.  
  82.     /* If the two names matched, the file is already   */
  83.     /* open -- return handle to matching child window. */
  84.     if (!iDiff)
  85.         return(hwndCheck);
  86.     }
  87.     /* No match found -- file is not open -- return NULL handle */
  88.     return(NULL);
  89. }
  90.  
  91. /****************************************************************************
  92.  *                                        *
  93.  *  FUNCTION   : AddFile (lpName)                        *
  94.  *                                        *
  95.  *  PURPOSE    : Creates a new MDI window. If the lpName parameter is not   *
  96.  *         NULL, it loads a file into the window.             *
  97.  *                                        *
  98.  *  RETURNS    : HWND  - A handle to the new window.                *
  99.  *                                        *
  100.  ****************************************************************************/
  101.  
  102. HWND FAR PASCAL AddFile(pName)
  103. char * pName;
  104. {
  105.     HWND hwnd;
  106.  
  107.     char        sz[160];
  108.     MDICREATESTRUCT mcs;
  109.  
  110.     if (!pName) {
  111.     /* The pName parameter is NULL -- load the "Untitled" string from */
  112.     /* STRINGTABLE and set the title field of the MDI CreateStruct.    */
  113.     LoadString (hInst, IDS_UNTITLED, sz, sizeof(sz));
  114.     mcs.szTitle = sz;
  115.     }
  116.     else
  117.     /* Title the window with the fully qualified pathname obtained by
  118.      * calling OpenFile() with the OF_PARSE flag (in function
  119.      * AlreadyOpen(), which is called before AddFile().
  120.      */
  121.     mcs.szTitle = of.szPathName;
  122.  
  123.     mcs.szClass = szChild;
  124.     mcs.hOwner    = hInst;
  125.  
  126.     /* Use the default size for the window */
  127.     mcs.x = mcs.cx = CW_USEDEFAULT;
  128.     mcs.y = mcs.cy = CW_USEDEFAULT;
  129.  
  130.     /* Set the style DWORD of the window to default */
  131.     mcs.style = styleDefault;
  132.  
  133.     /* tell the MDI Client to create the child */
  134.     hwnd = SendMessage (hwndMDIClient,
  135.                   WM_MDICREATE,
  136.                   0,
  137.                   (long)&mcs);
  138.  
  139.     /* Did we get a file? Read it into the window */
  140.     if (pName){
  141.     if (!LoadFile(hwnd, pName)){
  142.         /* File couldn't be loaded -- close window */
  143.         SendMessage(hwndMDIClient, WM_MDIDESTROY, (WORD) hwnd, 0L);
  144.     }
  145.     }
  146.  
  147.     return hwnd;
  148. }
  149.  
  150. /****************************************************************************
  151.  *                                        *
  152.  *  FUNCTION   : LoadFile (lpName)                        *
  153.  *                                        *
  154.  *  PURPOSE    : Given the handle to a MDI window and a filename, reads the *
  155.  *         file into the window's edit control child.                 *
  156.  *                                        *
  157.  *  RETURNS    : TRUE  - If file is sucessfully loaded.             *
  158.  *         FALSE - Otherwise.                        *
  159.  *                                        *
  160.  ****************************************************************************/
  161.  
  162. int FAR PASCAL LoadFile (hwnd, pName)
  163. HWND hwnd;
  164. char * pName;
  165. {
  166.     WORD   wLength;
  167.     HANDLE hT;
  168.     LP48STR  lpB;
  169.     HWND   hwndEdit;
  170.     int    fh;
  171.  
  172.     hwndEdit = GetWindowWord (hwnd, GWW_HWNDEDIT);
  173.  
  174.     /* The file has a title, so reset the UNTITLED flag. */
  175.     SetWindowWord(hwnd, GWW_UNTITLED, FALSE);
  176.  
  177.     fh = _lopen (pName, 0);
  178.  
  179.     /* Make sure file has been opened correctly */
  180.     if ( fh < 0 )
  181.     goto error;
  182.  
  183.     /* Find the length of the file */
  184.     wLength = (WORD)_llseek (fh, 0L, 2);
  185.     _llseek (fh, 0L, 0);
  186.  
  187.     /* Attempt to reallocate the edit control's buffer to the file size */
  188.     hT = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
  189.     if (LocalReAlloc(hT, wLength+1, LHND) == NULL) {
  190.     /* Couldn't reallocate to new size -- error */
  191.     _lclose (fh);
  192.     goto error;
  193.     }
  194.  
  195.     /* read the file into the buffer */
  196.     if (wLength != _lread (fh, (lpB = LocalLock (hT)), wLength))
  197.     MPError (hwnd, MB_OK|MB_ICONHAND, IDS_CANTREAD, (LPSTR)pName);
  198.  
  199.     /* Zero terminate the edit buffer */
  200.     lpB[wLength] = 0;
  201.     LocalUnlock (hT);
  202.  
  203.     SendMessage (hwndEdit, EM_SETHANDLE, hT, 0L);
  204.     _lclose (fh);
  205.  
  206.     return TRUE;
  207.  
  208. error:
  209.     /* Report the error and quit */
  210.     MPError(hwnd, MB_OK | MB_ICONHAND, IDS_CANTOPEN, (LPSTR)pName);
  211.     return FALSE;
  212. }
  213.  
  214. /****************************************************************************
  215.  *                                                                          *
  216.  *  FUNCTION   : ReadFile(hwnd)                                             *
  217.  *                                                                          *
  218.  *  PURPOSE    : Called in response to a File/Open menu selection. It asks  *
  219.  *               the user for a file name and responds appropriately.       *
  220.  *                                                                          *
  221.  ****************************************************************************/
  222.  
  223. VOID FAR PASCAL ReadFile(HWND hwnd)
  224. {
  225.     char    szFile[128];
  226.     HWND    hwndFile;
  227.  
  228.     GetFileName (szFile);
  229.  
  230.     /* If the result is not the empty string -- take appropriate action */
  231.     if (*szFile) {
  232.     /* Is file already open?? */
  233.     if ((hwndFile = AlreadyOpen(szFile)) != 0) {
  234.         /* Yes -- bring the file's window to the top */
  235.         BringWindowToTop(hwndFile);
  236.     }
  237.     else {
  238.         /* No -- make a new window and load file into it */
  239.         AddFile(szFile);
  240.     }
  241.     }
  242. }
  243.  
  244. /****************************************************************************
  245.  *                                        *
  246.  *  FUNCTION   : SaveFile (hwnd)                        *
  247.  *                                        *
  248.  *  PURPOSE    : Saves contents of current edit control to disk.        *
  249.  *                                        *
  250.  ****************************************************************************/
  251.  
  252. VOID FAR PASCAL SaveFile( hwnd )
  253.  
  254. HWND hwnd;
  255. {
  256.     HANDLE   hT;
  257.     LP48STR   lpT;
  258.     char     szFile[128];
  259.     WORD     cch;
  260.     int      fh;
  261.     OFSTRUCT of;
  262.     HWND     hwndEdit;
  263.  
  264.     hwndEdit = GetWindowWord ( hwnd, GWW_HWNDEDIT);
  265.     GetWindowText (hwnd, szFile, sizeof(szFile));
  266.  
  267.     /* If there is no extension (control is 'Untitled') add .TXT as extension */
  268.     for (cch = FALSE, lpT = szFile; *lpT; lpT++)
  269.     switch (*lpT){
  270.         case '.':
  271.          cch = TRUE;
  272.          break;
  273.  
  274.         case '\\':
  275.         case ':' :
  276.          cch = FALSE;
  277.          break;
  278.     }
  279.     if (!cch)
  280.     LoadString (hInst, IDS_ADDEXT, lpT, lpT - (LPSTR)szFile);
  281.  
  282.     fh = OpenFile (szFile, &of, OF_WRITE | OF_CREATE);
  283.  
  284.     /* If file could not be opened, quit */
  285.     if (fh < 0){
  286.     MPError (hwnd, MB_OK | MB_ICONHAND, IDS_CANTCREATE, (LPSTR)szFile);
  287.     return;
  288.     }
  289.  
  290.     /* Find out the length of the text in the edit control */
  291.     cch = GetWindowTextLength (hwndEdit);
  292.  
  293.     /* Obtain a handle to the text buffer */
  294.     hT    = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
  295.     lpT = LocalLock (hT);
  296.  
  297.     /* Write out the contents of the buffer to the file. */
  298.     if (cch != _lwrite (fh, lpT, cch))
  299.     MPError (hwnd, MB_OK | MB_ICONHAND, IDS_CANTWRITE, (LPSTR)szFile);
  300.  
  301.     /* Clean up */
  302.     LocalUnlock (hT);
  303.     SendMessage (hwndEdit, EM_SETHANDLE, hT, 0L);
  304.  
  305.     _lclose (fh);
  306.  
  307.     return;
  308. }
  309.  
  310. /****************************************************************************
  311.  *                                        *
  312.  *  FUNCTION   : SetSaveFrom ()                         *
  313.  *                                        *
  314.  *  PURPOSE    : Formats the "Save 'file' to .." string.            *
  315.  *                                        *
  316.  ****************************************************************************/
  317.  
  318. VOID NEAR PASCAL SetSaveFrom ( hwnd , psz)
  319. HWND hwnd;
  320. PSTR psz;
  321. {
  322.     char szFmt[32];
  323.     char szText[160];
  324.  
  325.     /* The text string in the .RC file contains the format string... */
  326.     GetDlgItemText( hwnd, IDD_SAVEFROM, szFmt, sizeof(szFmt));
  327.  
  328.     /* NOTE: this (LPSTR) cast MUST be here... wsprintf() is a cdecl
  329.      * (C calling conventions) function with varying args... there is
  330.      * no way for the compiler to know that all strings must be LPSTR's
  331.      * and do the conversion, so we have to be careful about wsprintf()'s.
  332.      */
  333.     wsprintf ( szText, szFmt, (LPSTR)psz);
  334.  
  335.     /* set the text in the static control */
  336.     SetDlgItemText (hwnd, IDD_SAVEFROM, szText);
  337. }
  338.  
  339. /****************************************************************************
  340.  *                                        *
  341.  *  FUNCTION   : SaveAsDlgProc(hwnd, message, wParam, lParam)            *
  342.  *                                        *
  343.  *  PURPOSE    : Dialog function File/SaveAs. It waits for a filename, and  *
  344.  *         then calls SaveFile or cancels the operation.            *
  345.  *                                        *
  346.  ****************************************************************************/
  347.  
  348. BOOL FAR PASCAL SaveAsDlgProc( hwnd, message, wParam, lParam)
  349. HWND hwnd;
  350. WORD message;
  351. WORD wParam;
  352. LONG lParam;
  353. {
  354.     char   sz[64];
  355.     char   *pch;
  356.     BOOL   fExt;
  357.     HWND   hwndSave;
  358.  
  359.     switch (message){
  360.  
  361.     case WM_INITDIALOG:
  362.  
  363.         /* Identify the window whose contents we're saving */
  364.         hwndSave = LOWORD (lParam);
  365.  
  366.         /* Set it's name in the property list */
  367.         SetProp (hwnd, PROP_FILENAME, hwndSave);
  368.  
  369.         GetWindowText (hwndSave, sz, sizeof(sz));
  370.  
  371.         /* Set the save from string... */
  372.         SetSaveFrom (hwnd,sz);
  373.  
  374.         /* Generate a filename complete with extension */
  375.         AnsiUpper (sz);
  376.         for (fExt = FALSE, pch = sz; *pch; pch++)
  377.         if (*pch == '.')
  378.             fExt = TRUE;
  379.         else if (*pch == '\\')
  380.             fExt = FALSE;
  381.         if (!fExt)
  382.         LoadString (hInst, IDS_ADDEXT, (LPSTR)pch, pch - sz);
  383.  
  384.         /* Display the filename in the edit control */
  385.         SetDlgItemText (hwnd, IDD_SAVETO, sz);
  386.  
  387.         /* Select the entire range of text */
  388.         SendDlgItemMessage (hwnd, IDD_SAVETO, EM_SETSEL, 0, MAKELONG (0,100));
  389.  
  390.         DlgDirList (hwnd, "*.*", IDD_DIRS, IDD_PATH, ATTR_DIRS);
  391.  
  392.         /* enable OK butto iff edit control is nonempty */
  393.         if (!*sz)
  394.         EnableWindow (GetDlgItem (hwnd, IDOK), FALSE);
  395.         break;
  396.  
  397.     case WM_COMMAND:
  398.         switch (wParam){
  399.         case IDCANCEL:
  400.             /* Abort operation */
  401.             EndDialog(hwnd,1);
  402.             break;
  403.  
  404.         case IDOK:
  405.            /*  Just change the title of the MDI child. The calling
  406.             *  function of ChangeFile(), which uses the title text
  407.             *  for the filename, will do the actual save.
  408.             */
  409.             hwndSave = GetProp (hwnd, PROP_FILENAME);
  410.             GetDlgItemText (hwnd, IDD_SAVETO, sz, sizeof(sz));
  411.             AnsiUpper ((LPSTR)sz);
  412.             SetWindowText (hwndSave, sz);
  413.             EndDialog (hwnd, 0);
  414.             break;
  415.  
  416.         case IDD_SAVETO:
  417.            /* If the edit control changes, check to see if its empty.
  418.             * enable OK if it contains something
  419.             */
  420.             if (HIWORD (lParam) != EN_CHANGE)
  421.             return FALSE;
  422.             EnableWindow (GetDlgItem (hwnd, IDOK),
  423.             (WORD) SendDlgItemMessage (hwnd,
  424.                            IDD_SAVETO,
  425.                            WM_GETTEXTLENGTH,
  426.                            0,
  427.                            0L));
  428.             break;
  429.  
  430.         case IDD_DIRS:
  431.             if (HIWORD(lParam)==LBN_DBLCLK){
  432.             char szT[64];
  433.  
  434.             DlgDirSelect ( hwnd, szT, IDD_DIRS);
  435.             lstrcat ( szT, "*.*");
  436.             DlgDirList (hwnd, szT, IDD_DIRS, IDD_PATH, ATTR_DIRS);
  437.             break;
  438.             }
  439.             return FALSE;
  440.  
  441.         default:
  442.             return FALSE;
  443.         }
  444.  
  445.     default:
  446.         return FALSE;
  447.     }
  448.     return TRUE;
  449. }
  450.  
  451. /****************************************************************************
  452.  *                                        *
  453.  *  FUNCTION   : ChangeFile (hwnd)                        *
  454.  *                                        *
  455.  *  PURPOSE    : Invokes the File/SaveAs dialog.                *
  456.  *                                        *
  457.  *  RETURNS    : TRUE  - if user selected OK or NO.                *
  458.  *         FALSE - otherwise.                        *
  459.  *                                        *
  460.  ****************************************************************************/
  461.  
  462. BOOL FAR PASCAL ChangeFile (hwnd)
  463. HWND hwnd;
  464. {
  465.     FARPROC lpfn;
  466.     int     i;
  467.  
  468.     lpfn = MakeProcInstance (SaveAsDlgProc, hInst);
  469.     i = DialogBoxParam (hInst, IDD_SAVEAS, hwnd, lpfn, MAKELONG (hwnd, 0));
  470.     FreeProcInstance (lpfn);
  471.     if (!i)
  472.     SetWindowWord (hwnd, GWW_UNTITLED, 0);
  473.     return !i;
  474. }
  475.