home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / multip.zip / MPFILE.C next >
C/C++ Source or Header  |  1990-01-04  |  11KB  |  395 lines

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