Leaked source code of windows server 2003
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.

253 lines
7.2 KiB

  1. /*
  2. * File Open/Create dialogs
  3. *
  4. */
  5. /*
  6. * these dialog functions exist because they were written and
  7. * used before the commmon dialogs existed.
  8. *
  9. * they have now been reduced to just calls to the common file dialog
  10. * functions
  11. */
  12. /*---includes-----------------------------------------------------------*/
  13. #include <precomp.h>
  14. #include "gutilsrc.h"
  15. /*--functions----------------------------------------------------------*/
  16. /*
  17. * gfile_open
  18. * dialog asking the user to select an existing file to open.
  19. *
  20. * parameters
  21. *
  22. * prompt - message to user indicating purpose of file
  23. * (to be displayed somewhere in dialog box.
  24. *
  25. * ext - default file extension if user enters file without
  26. * extension.
  27. *
  28. * spec - default file spec (eg *.*)
  29. *
  30. * pszFull - buffer where full filename (including path) is returned.
  31. *
  32. * cchMax - size of pszFull buffer.
  33. *
  34. * fn - buffer where filename (just final element) is returned.
  35. *
  36. * returns - true if file selected and exists (tested with OF_EXIST).
  37. * FALSE if dialog cancelled. If user selects a file that we cannot
  38. * open, we complain and restart the dialog.
  39. *
  40. * if TRUE is returned, the file will have been successfully opened,
  41. * for reading and then closed again.
  42. */
  43. BOOL
  44. FAR
  45. PASCAL
  46. gfile_open(
  47. HWND hwnd,
  48. LPSTR prompt,
  49. LPSTR ext,
  50. LPSTR spec,
  51. LPSTR pszFull,
  52. int cchMax,
  53. LPSTR fn
  54. )
  55. {
  56. OPENFILENAME ofn;
  57. char achFilters[MAX_PATH];
  58. char szTmp[MAX_PATH * 2] = {0};
  59. HANDLE fh;
  60. if (!pszFull)
  61. {
  62. pszFull = szTmp;
  63. cchMax = sizeof(szTmp) / sizeof(szTmp[0]);
  64. }
  65. if (cchMax < 1)
  66. return FALSE;
  67. /* build filter-pair buffer to contain one pair - the spec filter,
  68. * twice (one of the pair should be the filter, the second should be
  69. * the title of the filter - we don't have a title so we use the
  70. * filter both times.
  71. */
  72. _snprintf(achFilters, (sizeof(achFilters)/sizeof(achFilters[0])) - 1, "%s%c%s", spec, 0, spec);
  73. /*
  74. * initialise arguments to dialog proc
  75. */
  76. memset(&ofn, 0, sizeof(ofn));
  77. // GetOpenFileName ang GetSaveFileName unfortunately
  78. // validate the size of the structue. So we need to lie to
  79. // the function if we were built for >=Win2000 and
  80. // running on an earlier OS
  81. #if (_WIN32_WINNT >= 0x0500)
  82. ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
  83. #else
  84. ofn.lStructSize = sizeof(OPENFILENAME);
  85. #endif
  86. ofn.hwndOwner = hwnd;
  87. ofn.hInstance = NULL;
  88. ofn.lpstrFilter = achFilters;
  89. ofn.lpstrCustomFilter = NULL;
  90. ofn.nMaxCustFilter = 0;
  91. ofn.nFilterIndex = 1; // first filter pair in list
  92. pszFull[0] = '\0';
  93. ofn.lpstrFile = pszFull; // we need to get the full path to open
  94. ofn.nMaxFile = cchMax;
  95. ofn.lpstrFileTitle = fn; // return final elem of name here
  96. ofn.nMaxFileTitle = 13; // assume just big enough for 8.3+null
  97. ofn.lpstrInitialDir = NULL;
  98. ofn.lpstrTitle = prompt; // dialog title is good place for prompt text
  99. ofn.Flags = OFN_FILEMUSTEXIST |
  100. OFN_HIDEREADONLY |
  101. OFN_PATHMUSTEXIST;
  102. ofn.lpstrDefExt = ext;
  103. /*
  104. * loop until the user cancels, or selects a file that we can open
  105. */
  106. do {
  107. if (!GetOpenFileName(&ofn)) {
  108. return(FALSE);
  109. }
  110. fh = CreateFile(pszFull, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  111. if (fh == INVALID_HANDLE_VALUE) {
  112. if (MessageBox(NULL, "File Could Not Be Opened", "File Open",
  113. MB_OKCANCEL|MB_ICONSTOP) == IDCANCEL) {
  114. return(FALSE);
  115. }
  116. }
  117. } while (fh == INVALID_HANDLE_VALUE);
  118. CloseHandle(fh);
  119. return(TRUE);
  120. }
  121. /*
  122. * gfile_new
  123. * dialog asking the user to name a file for writing to.
  124. *
  125. * parameters
  126. *
  127. * prompt - message to user indicating purpose of file
  128. * (to be displayed somewhere in dialog box.
  129. *
  130. * ext - default file extension if user enters file without
  131. * extension.
  132. *
  133. * spec - default file spec (eg *.*)
  134. *
  135. * pszFull - buffer where full filename (including path) is returned.
  136. *
  137. * cchMax - size of pszFull buffer.
  138. *
  139. * fn - buffer where filename (just final element) is returned.
  140. *
  141. * returns - true if file selected and exists (tested with OF_EXIST).
  142. * FALSE if dialog cancelled. If user selects a file that we cannot
  143. * open, we complain and restart the dialog.
  144. *
  145. * if TRUE is returned, the file will have been successfully
  146. * created and opened for writing and then closed again.
  147. */
  148. BOOL
  149. FAR
  150. PASCAL
  151. gfile_new(
  152. LPSTR prompt,
  153. LPSTR ext,
  154. LPSTR spec,
  155. LPSTR pszFull,
  156. int cchMax,
  157. LPSTR fn
  158. )
  159. {
  160. OPENFILENAME ofn;
  161. char achFilters[MAX_PATH] = {0};
  162. char szTmp[MAX_PATH * 2];
  163. HANDLE fh;
  164. if (!pszFull)
  165. {
  166. pszFull = szTmp;
  167. cchMax = sizeof(szTmp) / sizeof(szTmp[0]);
  168. }
  169. if (cchMax < 1)
  170. return FALSE;
  171. /* build filter-pair buffer to contain one pair - the spec filter,
  172. * twice (one of the pair should be the filter, the second should be
  173. * the title of the filter - we don't have a title so we use the
  174. * filter both times. remember double null at end of list of strings.
  175. */
  176. _snprintf(achFilters, (sizeof(achFilters)/sizeof(achFilters[0])) - 1, "%s%c%s", spec, 0, spec);
  177. /*
  178. * initialise arguments to dialog proc
  179. */
  180. memset(&ofn, 0, sizeof(ofn));
  181. // GetOpenFileName ang GetSaveFileName unfortunately
  182. // validate the size of the structue. So we need to lie to
  183. // the function if we were built for >=Win2000 and
  184. // running on an earlier OS
  185. #if (_WIN32_WINNT >= 0x0500)
  186. ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
  187. #else
  188. ofn.lStructSize = sizeof(OPENFILENAME);
  189. #endif
  190. ofn.hwndOwner = NULL;
  191. ofn.hInstance = NULL;
  192. ofn.lpstrFilter = achFilters;
  193. ofn.lpstrCustomFilter = NULL;
  194. ofn.nMaxCustFilter = 0;
  195. ofn.nFilterIndex = 1; // first filter pair in list
  196. pszFull[0] = '\0';
  197. ofn.lpstrFile = pszFull; // we need to get the full path to open
  198. ofn.nMaxFile = cchMax;
  199. ofn.lpstrFileTitle = fn; // return final elem of name here
  200. ofn.nMaxFileTitle = 13; // assume just big enough for 8.3+null
  201. ofn.lpstrInitialDir = NULL;
  202. ofn.lpstrTitle = prompt; // dialog title is good place for prompt text
  203. ofn.Flags = OFN_HIDEREADONLY;
  204. ofn.lpstrDefExt = ext;
  205. /*
  206. * loop until the user cancels, or selects a file that we can create/write
  207. */
  208. do {
  209. if (!GetSaveFileName(&ofn)) {
  210. return(FALSE);
  211. }
  212. fh = CreateFile(pszFull, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
  213. if (fh == INVALID_HANDLE_VALUE) {
  214. if (MessageBox(NULL, "File Could Not Be Created", "File Open",
  215. MB_OKCANCEL|MB_ICONSTOP) == IDCANCEL) {
  216. return(FALSE);
  217. }
  218. }
  219. } while (fh == INVALID_HANDLE_VALUE);
  220. CloseHandle(fh);
  221. return(TRUE);
  222. }