Windows NT 4.0 source code leak
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.

213 lines
5.9 KiB

4 years ago
  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(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. ofn.lStructSize = sizeof(OPENFILENAME);
  67. ofn.hwndOwner = NULL;
  68. ofn.hInstance = NULL;
  69. ofn.lpstrFilter = achFilters;
  70. ofn.lpstrCustomFilter = NULL;
  71. ofn.nMaxCustFilter = 0;
  72. ofn.nFilterIndex = 1; // first filter pair in list
  73. achPath[0] = '\0';
  74. ofn.lpstrFile = achPath; // we need to get the full path to open
  75. ofn.nMaxFile = sizeof(achPath);
  76. ofn.lpstrFileTitle = fn; // return final elem of name here
  77. ofn.nMaxFileTitle = 13; // assume just big enough for 8.3+null
  78. ofn.lpstrInitialDir = NULL;
  79. ofn.lpstrTitle = prompt; // dialog title is good place for prompt text
  80. ofn.Flags = OFN_FILEMUSTEXIST |
  81. OFN_HIDEREADONLY |
  82. OFN_PATHMUSTEXIST;
  83. ofn.lpstrDefExt = ext;
  84. /*
  85. * loop until the user cancels, or selects a file that we can open
  86. */
  87. do {
  88. if (!GetOpenFileName(&ofn)) {
  89. return(FALSE);
  90. }
  91. fh = OpenFile(achPath, osp, OF_READ);
  92. if (fh == HFILE_ERROR) {
  93. if (MessageBox(NULL, "File Could Not Be Opened", "File Open",
  94. MB_OKCANCEL|MB_ICONSTOP) == IDCANCEL) {
  95. return(FALSE);
  96. }
  97. }
  98. } while (fh == HFILE_ERROR);
  99. _lclose(fh);
  100. return(TRUE);
  101. }
  102. /*
  103. * gfile_new
  104. * dialog asking the user to name a file for writing to.
  105. *
  106. * parameters
  107. *
  108. * prompt - message to user indicating purpose of file
  109. * (to be displayed somewhere in dialog box.
  110. *
  111. * ext - default file extension if user enters file without
  112. * extension.
  113. *
  114. * spec - default file spec (eg *.*)
  115. *
  116. * osp - OFSTRUCT representing file, if successfully created.
  117. *
  118. * fn - buffer where filename (just final element) is returned.
  119. *
  120. * returns - true if file selected and exists (tested with OF_EXIST).
  121. * FALSE if dialog cancelled. If user selects a file that we cannot
  122. * open, we complain and restart the dialog.
  123. *
  124. * if TRUE is returned, the file will have been successfully
  125. * created and opened for writing and then closed again.
  126. */
  127. BOOL FAR PASCAL
  128. gfile_new(LPSTR prompt, LPSTR ext, LPSTR spec, OFSTRUCT FAR *osp, LPSTR fn)
  129. {
  130. OPENFILENAME ofn;
  131. char achFilters[MAX_PATH];
  132. LPSTR chp;
  133. char achPath[MAX_PATH];
  134. int fh;
  135. /* build filter-pair buffer to contain one pair - the spec filter,
  136. * twice (one of the pair should be the filter, the second should be
  137. * the title of the filter - we don't have a title so we use the
  138. * filter both times. remember double null at end of list of strings.
  139. */
  140. lstrcpy(achFilters, spec); // filter + null
  141. chp = &achFilters[lstrlen(achFilters)+1]; //2nd string just after null
  142. lstrcpy(chp, spec); // filter name (+null)
  143. chp[lstrlen(chp)+1] = '\0'; // double null at end of list
  144. /*
  145. * initialise arguments to dialog proc
  146. */
  147. ofn.lStructSize = sizeof(OPENFILENAME);
  148. ofn.hwndOwner = NULL;
  149. ofn.hInstance = NULL;
  150. ofn.lpstrFilter = achFilters;
  151. ofn.lpstrCustomFilter = NULL;
  152. ofn.nMaxCustFilter = 0;
  153. ofn.nFilterIndex = 1; // first filter pair in list
  154. achPath[0] = '\0';
  155. ofn.lpstrFile = achPath; // we need to get the full path to open
  156. ofn.nMaxFile = sizeof(achPath);
  157. ofn.lpstrFileTitle = fn; // return final elem of name here
  158. ofn.nMaxFileTitle = 13; // assume just big enough for 8.3+null
  159. ofn.lpstrInitialDir = NULL;
  160. ofn.lpstrTitle = prompt; // dialog title is good place for prompt text
  161. ofn.Flags = OFN_HIDEREADONLY;
  162. ofn.lpstrDefExt = ext;
  163. /*
  164. * loop until the user cancels, or selects a file that we can create/write
  165. */
  166. do {
  167. if (!GetSaveFileName(&ofn)) {
  168. return(FALSE);
  169. }
  170. fh = OpenFile(achPath, osp, OF_CREATE|OF_READWRITE);
  171. if (fh == HFILE_ERROR) {
  172. if (MessageBox(NULL, "File Could Not Be Created", "File Open",
  173. MB_OKCANCEL|MB_ICONSTOP) == IDCANCEL) {
  174. return(FALSE);
  175. }
  176. }
  177. } while (fh == HFILE_ERROR);
  178. _lclose(fh);
  179. return(TRUE);
  180. }