Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
8.3 KiB

  1. /*
  2. +-------------------------------------------------------------------------+
  3. | MDI Text File Viewer - File IO Routines |
  4. +-------------------------------------------------------------------------+
  5. | (c) Copyright 1994 |
  6. | Microsoft Corp. |
  7. | All rights reserved |
  8. | |
  9. | Program : [mpfile.c] |
  10. | Programmer : Arthur Hanson |
  11. | Original Program Date : [Jul 27, 1993 |
  12. | Last Update : [Jul 30, 1993] Time : 18:30 |
  13. | |
  14. | Version: 0.10 |
  15. | |
  16. | Description: |
  17. | |
  18. | History: |
  19. | arth Jul 27, 1993 0.10 Original Version. |
  20. | |
  21. +-------------------------------------------------------------------------+
  22. */
  23. #include "LogView.h"
  24. #include <fcntl.h>
  25. #include <SYS\types.h>
  26. #include <SYS\stat.h>
  27. #include <io.h>
  28. #include <string.h>
  29. VOID APIENTRY GetFileName(HWND hwnd, PSTR);
  30. OFSTRUCT of;
  31. /*+-------------------------------------------------------------------------+
  32. | AlreadyOpen() |
  33. | |
  34. | Checks to see if a file is already opened. Returns a handle to |
  35. | the file's window if it is opened, otherwise NULL. |
  36. | |
  37. +-------------------------------------------------------------------------+*/
  38. HWND AlreadyOpen(CHAR *szFile) {
  39. INT iDiff;
  40. HWND hwndCheck;
  41. CHAR szChild[64];
  42. LPSTR lpChild, lpFile;
  43. HFILE wFileTemp;
  44. // Open the file with the OF_PARSE flag to obtain the fully qualified
  45. // pathname in the OFSTRUCT structure.
  46. wFileTemp = OpenFile((LPSTR)szFile, (LPOFSTRUCT)&of, OF_PARSE);
  47. if (! wFileTemp)
  48. return(NULL);
  49. _lclose(wFileTemp);
  50. // Check each MDI child window in LogView
  51. for ( hwndCheck = GetWindow(hwndMDIClient, GW_CHILD);
  52. hwndCheck;
  53. hwndCheck = GetWindow(hwndCheck, GW_HWNDNEXT) ) {
  54. // Initialization for comparison
  55. lpChild = szChild;
  56. lpFile = (LPSTR)AnsiUpper((LPSTR) of.szPathName);
  57. iDiff = 0;
  58. // Skip icon title windows
  59. if (GetWindow(hwndCheck, GW_OWNER))
  60. continue;
  61. // Get current child window's name
  62. GetWindowText(hwndCheck, lpChild, 64);
  63. // Compare window name with given name
  64. while ((*lpChild) && (*lpFile) && (!iDiff)) {
  65. if (*lpChild++ != *lpFile++)
  66. iDiff = 1;
  67. }
  68. // If the two names matched, the file is already open - return handle to matching
  69. // child window.
  70. if (!iDiff)
  71. return(hwndCheck);
  72. }
  73. // No match found -- file is not open -- return NULL handle
  74. return(NULL);
  75. } // AlreadyOpen
  76. /*+-------------------------------------------------------------------------+
  77. | AddFile() |
  78. | |
  79. | Create a new MDI Window, and loads specified file into Window. |
  80. | |
  81. +-------------------------------------------------------------------------+*/
  82. HWND APIENTRY AddFile(CHAR * pName) {
  83. HWND hwnd;
  84. CHAR sz[160];
  85. MDICREATESTRUCT mcs;
  86. if (!pName) {
  87. // The pName parameter is NULL -- load the "Untitled" string from STRINGTABLE
  88. // and set the title field of the MDI CreateStruct.
  89. LoadString (hInst, IDS_UNTITLED, sz, sizeof(sz));
  90. mcs.szTitle = (LPSTR)sz;
  91. }
  92. else
  93. // Title the window with the fully qualified pathname obtained by calling
  94. // OpenFile() with the OF_PARSE flag (in function AlreadyOpen(), which is called
  95. // before AddFile().
  96. mcs.szTitle = pName;
  97. mcs.szClass = szChild;
  98. mcs.hOwner = hInst;
  99. // Use the default size for the window
  100. mcs.x = mcs.cx = CW_USEDEFAULT;
  101. mcs.y = mcs.cy = CW_USEDEFAULT;
  102. // Set the style DWORD of the window to default
  103. mcs.style = 0L;
  104. // tell the MDI Client to create the child
  105. hwnd = (HWND)SendMessage (hwndMDIClient,
  106. WM_MDICREATE,
  107. 0,
  108. (LONG_PTR)(LPMDICREATESTRUCT)&mcs);
  109. // Did we get a file? Read it into the window
  110. if (pName){
  111. if (!LoadFile(hwnd, pName)){
  112. // File couldn't be loaded -- close window
  113. SendMessage(hwndMDIClient, WM_MDIDESTROY, (DWORD_PTR) hwnd, 0L);
  114. }
  115. }
  116. return hwnd;
  117. } // AddFile
  118. /*+-------------------------------------------------------------------------+
  119. | LoadFile() |
  120. | |
  121. | Loads file into specified MDI window's edit control. |
  122. | |
  123. +-------------------------------------------------------------------------+*/
  124. INT APIENTRY LoadFile ( HWND hwnd, CHAR * pName) {
  125. LONG wLength;
  126. HANDLE hT;
  127. LPSTR lpB;
  128. HWND hwndEdit;
  129. HFILE fh;
  130. OFSTRUCT of;
  131. hwndEdit = (HWND)GetWindowLong (hwnd, GWL_HWNDEDIT);
  132. // The file has a title, so reset the UNTITLED flag.
  133. SetWindowWord(hwnd, GWW_UNTITLED, FALSE);
  134. fh = OpenFile(pName, &of, OF_READ); // JAP was 0, which is OF_READ)
  135. // Make sure file has been opened correctly
  136. if ( fh < 0 )
  137. goto error;
  138. // Find the length of the file
  139. wLength = (DWORD)_llseek(fh, 0L, 2);
  140. _llseek(fh, 0L, 0);
  141. // Attempt to reallocate the edit control's buffer to the file size
  142. hT = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
  143. if (LocalReAlloc(hT, wLength+1, LHND) == NULL) {
  144. // Couldn't reallocate to new size -- error
  145. _lclose(fh);
  146. goto error;
  147. }
  148. // read the file into the buffer
  149. if (wLength != (LONG)_lread(fh, (lpB = (LPSTR)LocalLock (hT)), (UINT)wLength))
  150. MPError (hwnd, MB_OK|MB_ICONHAND, IDS_CANTREAD, (LPSTR)pName);
  151. // Zero terminate the edit buffer
  152. lpB[wLength] = 0;
  153. LocalUnlock (hT);
  154. SendMessage (hwndEdit, EM_SETHANDLE, (UINT_PTR)hT, 0L);
  155. _lclose(fh);
  156. return TRUE;
  157. error:
  158. // Report the error and quit
  159. MPError(hwnd, MB_OK | MB_ICONHAND, IDS_CANTOPEN, (LPSTR)pName);
  160. return FALSE;
  161. } // LoadFile
  162. /*+-------------------------------------------------------------------------+
  163. | MyReadFile() |
  164. | |
  165. | Asks user for a filename. |
  166. | |
  167. +-------------------------------------------------------------------------+*/
  168. VOID APIENTRY MyReadFile(HWND hwnd) {
  169. CHAR szFile[128];
  170. HWND hwndFile;
  171. GetFileName (hwnd, szFile);
  172. // If the result is not the empty string -- take appropriate action
  173. if (*szFile) {
  174. // Is file already open??
  175. if (hwndFile = AlreadyOpen(szFile)) {
  176. // Yes -- bring the file's window to the top
  177. BringWindowToTop(hwndFile);
  178. }
  179. else {
  180. // No -- make a new window and load file into it
  181. AddFile(szFile);
  182. }
  183. }
  184. UNREFERENCED_PARAMETER(hwnd);
  185. } // MyReadFile