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.

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