home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / WRITEPAD.PAK / FILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.7 KB  |  228 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: file.c
  9. //
  10. //  PURPOSE: File manipulation functions.
  11. //
  12. //  FUNCTIONS:
  13. //    CmdFileSave   - Save the current document.
  14. //    Open          - Open a file based on a file name and readonly attribute.
  15. //    QuerySaveFile - Save the current document if necessary & the user 
  16. //                      agrees.
  17. //    SetNewBuffer  - Create a new edit buffer.
  18. //    SaveFile      - Save the text from the editor control to the current 
  19. //                      file.
  20. //    SaveAs        - Save the edit text to the specified file.
  21. //
  22. //  COMMENTS:
  23. //
  24.  
  25.  
  26. #include <windows.h>            // required for all Windows applications
  27. #include "globals.h"            // prototypes specific to this application
  28. #include "resource.h"
  29. #include "rtf.h"
  30.  
  31. void SetNewBuffer(HANDLE);
  32. BOOL SaveFile(HWND);
  33.  
  34. char szFName[256];
  35.  
  36. //
  37. //  FUNCTION: CmdFileSave(HWND, WORD, WORD, HWND)
  38. //
  39. //  PURPOSE: Save the current document.
  40. //
  41. //  PARAMETERS:
  42. //    hwnd     - The window.
  43. //    wCommand - IDM_FILESAVE (unused)
  44. //    wNotify  - Notification number (unused)
  45. //    hwndCtrl - NULL (unused)
  46. //
  47. //  RETURN VALUE:
  48. //    Always returns 0 - command handled.
  49. //
  50. //  COMMENTS:
  51. //
  52. //
  53.  
  54. #pragma argsused
  55. LRESULT CmdFileSave(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  56. {
  57.     // If there is no filename, use the saveas command to get one.
  58.     //   Otherwise, save the file using the current filename.
  59.  
  60.     if (!GetProp(GetActiveMDIChild(), "FilenameSet"))
  61.     {
  62.         CmdFileSaveAs(hwnd, wCommand, 0, hwndCtrl);
  63.     }
  64.     else
  65.     {
  66.         // If the document has not been modified, don't bother saving it.
  67.         if (SendMessage(GetEditWindow(NULL), EM_GETMODIFY, FALSE, 0L))
  68.             SaveFile(hwnd);
  69.     }
  70.  
  71.     return 0;
  72. }
  73.  
  74.  
  75. //
  76. //  FUNCTION: Open(LPSTR, BOOL, HWND)
  77. //
  78. //  PURPOSE: Open a file based on a file name and readonly attribute.
  79. //
  80. //  PARAMETERS:
  81. //    lsz - The full path name of the file to be opened.
  82. //    fReadOnly - Read only attribute of the file.
  83. //    hwnd - The window into which the file is being opened.
  84. //
  85. //  RETURN VALUE:
  86. //    NONE
  87. //
  88. //  COMMENTS:
  89. //
  90. //
  91.  
  92. #pragma argsused
  93. VOID Open(LPSTR lsz, BOOL fReadOnly, HWND hwnd)
  94. {
  95.      RTF_Open(lsz, GetEditWindow(NULL));
  96.  
  97.      SendMessage(GetEditWindow(NULL), EM_SETREADONLY, fReadOnly, 0L);
  98.      lstrcpy(GetFName(), lsz);
  99.      SetProp(GetActiveMDIChild(), "FilenameSet", (HANDLE)1);
  100. }
  101.  
  102. //
  103. //  FUNCTION: QuerySaveFile(HWND)
  104. //
  105. //  PURPOSE: Save the current document if necessary & the user agrees.
  106. //
  107. //  PARAMETERS:
  108. //    hwnd - The document window.
  109. //
  110. //  RETURN VALUE:
  111. //    TRUE  - The file has been saved, or any changes can be thrown out.
  112. //    FALSE - The use has changed his/her mind, don't continue the
  113. //      operation that would throw away this document.
  114. //
  115. //  COMMENTS:
  116. //
  117. //
  118.  
  119. BOOL QuerySaveFile(HWND hwnd)
  120. {
  121.     int  idResponse;
  122.     char rgch[256];
  123.  
  124.     if (SendMessage(GetEditWindow(NULL), EM_GETMODIFY, 0, 0L))
  125.     {
  126.         wsprintf(rgch, "Save current changes? %s", GetFName());
  127.         idResponse = MessageBox(hwnd, 
  128.                                 rgch, 
  129.                                 szAppName,
  130.                                 MB_YESNOCANCEL | MB_ICONEXCLAMATION);
  131.  
  132.         if (idResponse == IDYES)            // Save changes
  133.         {
  134.             // Make sure there is a filename to save to
  135.             if (!GetProp(GetActiveMDIChild(), "FilenameSet"))
  136.                 return (BOOL)CmdFileSaveAs(hwnd, IDM_FILESAVEAS, 0, NULL);
  137.             else
  138.                 SaveFile(hwnd);
  139.         }
  140.         else if (idResponse == IDCANCEL)    // Cancel entire operation
  141.             return FALSE;
  142.     }
  143.  
  144.     return TRUE;                            // Proceed
  145. }
  146.  
  147.  
  148. //
  149. //  FUNCTION: SetNewBuffer(HWND, HANDLE, LPSTR)
  150. //
  151. //  PURPOSE: Create a new edit buffer.
  152. //
  153. //  PARAMETERS:
  154. //    hwnd    - Handle to the main window.
  155. //    hsz     - Local handle to the text (may be NULL).
  156. //    szTitle - Title to display for the user.
  157. //
  158. //  RETURN VALUE:
  159. //    None.
  160. //
  161. //  COMMENTS:
  162. //
  163.  
  164. void SetNewBuffer(HANDLE hsz)
  165. {
  166.     if (!hsz)                    // Allocates a buffer if none exists
  167.         hsz = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, 1);
  168.  
  169.     // Updates the buffer and displays new buffer
  170.     SetEditText(GetEditWindow(NULL), hsz);
  171.  
  172.     SendMessage(GetEditWindow(NULL), EM_SETMODIFY, FALSE, 0L);
  173. }
  174.  
  175.  
  176. //
  177. //  FUNCTION: SaveFile(HWND)
  178. //
  179. //  PURPOSE: Save the text from the editor control to the current file.
  180. //
  181. //  PARAMETERS:
  182. //    hwnd - The main application window.
  183. //
  184. //  RETURN VALUE:
  185. //    TRUE - Sucessfully saved the file.
  186. //    FALSE - Unable to save the file.
  187. //
  188. //  COMMENTS:
  189. //
  190.  
  191. #pragma argsused
  192. BOOL SaveFile(HWND hwnd)
  193. {
  194.      LPSTR szFileName;
  195.  
  196.      szFileName = GetFName();
  197.  
  198.     return RTF_Save(szFileName,GetEditWindow(NULL));
  199. }
  200.  
  201.  
  202. //
  203. //  FUNCTION: SaveAs(LPSTR, LPSTR, HWND)
  204. //
  205. //  PURPOSE: Save the edit text to the specified file.
  206. //
  207. //  PARAMETERS:
  208. //    lsz             - The fully qualified path of the file to save.
  209. //    lszCurrentName  - The current name of the file; used to restore 
  210. //                      the caption if saving fails
  211. //    hwnd            - The main application window.
  212. //
  213. //  RETURN VALUE:
  214. //    NONE
  215. //
  216. //  COMMENTS:
  217. //
  218. //
  219.  
  220. #pragma argsused
  221. VOID SaveAs(LPSTR lsz, LPSTR lszCurrentName, HWND hwnd)
  222. {
  223.      if (!SaveFile(hwnd))
  224.           SetWindowText(hwnd, lszCurrentName);
  225.      else
  226.         SetProp(GetActiveMDIChild(), "FilenameSet", (HANDLE)1);
  227. }
  228.