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.

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