filedlg.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: filedlg.h
  3. // Purpose: interface of wxFileDialog
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. enum
  8. {
  9. wxFD_OPEN = 0x0001,
  10. wxFD_SAVE = 0x0002,
  11. wxFD_OVERWRITE_PROMPT = 0x0004,
  12. wxFD_FILE_MUST_EXIST = 0x0010,
  13. wxFD_MULTIPLE = 0x0020,
  14. wxFD_CHANGE_DIR = 0x0080,
  15. wxFD_PREVIEW = 0x0100
  16. };
  17. #define wxFD_DEFAULT_STYLE wxFD_OPEN
  18. /**
  19. Default wildcard string used in wxFileDialog corresponding to all files.
  20. It is defined as "*.*" under MSW and OS/2 and "*" everywhere else.
  21. */
  22. const char wxFileSelectorDefaultWildcardStr[];
  23. /**
  24. @class wxFileDialog
  25. This class represents the file chooser dialog.
  26. The path and filename are distinct elements of a full file pathname.
  27. If path is ::wxEmptyString, the current directory will be used.
  28. If filename is ::wxEmptyString, no default filename will be supplied.
  29. The wildcard determines what files are displayed in the file selector,
  30. and file extension supplies a type extension for the required filename.
  31. The typical usage for the open file dialog is:
  32. @code
  33. void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
  34. {
  35. if (...current content has not been saved...)
  36. {
  37. if (wxMessageBox(_("Current content has not been saved! Proceed?"), _("Please confirm"),
  38. wxICON_QUESTION | wxYES_NO, this) == wxNO )
  39. return;
  40. //else: proceed asking to the user the new file to open
  41. }
  42. wxFileDialog
  43. openFileDialog(this, _("Open XYZ file"), "", "",
  44. "XYZ files (*.xyz)|*.xyz", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
  45. if (openFileDialog.ShowModal() == wxID_CANCEL)
  46. return; // the user changed idea...
  47. // proceed loading the file chosen by the user;
  48. // this can be done with e.g. wxWidgets input streams:
  49. wxFileInputStream input_stream(openFileDialog.GetPath());
  50. if (!input_stream.IsOk())
  51. {
  52. wxLogError("Cannot open file '%s'.", openFileDialog.GetPath());
  53. return;
  54. }
  55. ...
  56. }
  57. @endcode
  58. The typical usage for the save file dialog is instead somewhat simpler:
  59. @code
  60. void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
  61. {
  62. wxFileDialog
  63. saveFileDialog(this, _("Save XYZ file"), "", "",
  64. "XYZ files (*.xyz)|*.xyz", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
  65. if (saveFileDialog.ShowModal() == wxID_CANCEL)
  66. return; // the user changed idea...
  67. // save the current contents in the file;
  68. // this can be done with e.g. wxWidgets output streams:
  69. wxFileOutputStream output_stream(saveFileDialog.GetPath());
  70. if (!output_stream.IsOk())
  71. {
  72. wxLogError("Cannot save current contents in file '%s'.", saveFileDialog.GetPath());
  73. return;
  74. }
  75. ...
  76. }
  77. @endcode
  78. @remarks
  79. All implementations of the wxFileDialog provide a wildcard filter. Typing a filename
  80. containing wildcards (*, ?) in the filename text item, and clicking on Ok, will
  81. result in only those files matching the pattern being displayed.
  82. The wildcard may be a specification for multiple types of file with a description
  83. for each, such as:
  84. @code
  85. "BMP and GIF files (*.bmp;*.gif)|*.bmp;*.gif|PNG files (*.png)|*.png"
  86. @endcode
  87. It must be noted that wildcard support in the native Motif file dialog is quite
  88. limited: only one file type is supported, and it is displayed without the
  89. descriptive test; "BMP files (*.bmp)|*.bmp" is displayed as "*.bmp", and both
  90. "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif" and "Image files|*.bmp;*.gif"
  91. are errors.
  92. @beginStyleTable
  93. @style{wxFD_DEFAULT_STYLE}
  94. Equivalent to @c wxFD_OPEN.
  95. @style{wxFD_OPEN}
  96. This is an open dialog; usually this means that the default
  97. button's label of the dialog is "Open". Cannot be combined with @c wxFD_SAVE.
  98. @style{wxFD_SAVE}
  99. This is a save dialog; usually this means that the default button's
  100. label of the dialog is "Save". Cannot be combined with @c wxFD_OPEN.
  101. @style{wxFD_OVERWRITE_PROMPT}
  102. For save dialog only: prompt for a confirmation if a file will be
  103. overwritten.
  104. @style{wxFD_FILE_MUST_EXIST}
  105. For open dialog only: the user may only select files that actually
  106. exist. Notice that under OS X the file dialog with @c wxFD_OPEN
  107. style always behaves as if this style was specified, because it is
  108. impossible to choose a file that doesn't exist from a standard OS X
  109. file dialog.
  110. @style{wxFD_MULTIPLE}
  111. For open dialog only: allows selecting multiple files.
  112. @style{wxFD_CHANGE_DIR}
  113. Change the current working directory (when the dialog is dismissed)
  114. to the directory where the file(s) chosen by the user are.
  115. @style{wxFD_PREVIEW}
  116. Show the preview of the selected files (currently only supported by
  117. wxGTK).
  118. @endStyleTable
  119. @library{wxcore}
  120. @category{cmndlg}
  121. @see @ref overview_cmndlg_file, ::wxFileSelector()
  122. */
  123. class wxFileDialog : public wxDialog
  124. {
  125. public:
  126. /**
  127. Constructor. Use ShowModal() to show the dialog.
  128. @param parent
  129. Parent window.
  130. @param message
  131. Message to show on the dialog.
  132. @param defaultDir
  133. The default directory, or the empty string.
  134. @param defaultFile
  135. The default filename, or the empty string.
  136. @param wildcard
  137. A wildcard, such as "*.*" or "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
  138. Note that the native Motif dialog has some limitations with respect to
  139. wildcards; see the Remarks section above.
  140. @param style
  141. A dialog style. See @c wxFD_* styles for more info.
  142. @param pos
  143. Dialog position. Not implemented.
  144. @param size
  145. Dialog size. Not implemented.
  146. @param name
  147. Dialog name. Not implemented.
  148. */
  149. wxFileDialog(wxWindow* parent,
  150. const wxString& message = wxFileSelectorPromptStr,
  151. const wxString& defaultDir = wxEmptyString,
  152. const wxString& defaultFile = wxEmptyString,
  153. const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
  154. long style = wxFD_DEFAULT_STYLE,
  155. const wxPoint& pos = wxDefaultPosition,
  156. const wxSize& size = wxDefaultSize,
  157. const wxString& name = wxFileDialogNameStr);
  158. /**
  159. Destructor.
  160. */
  161. virtual ~wxFileDialog();
  162. /**
  163. Returns the path of the file currently selected in dialog.
  164. Notice that this file is not necessarily going to be accepted by the
  165. user, so calling this function mostly makes sense from an update UI
  166. event handler of a custom file dialog extra control to update its state
  167. depending on the currently selected file.
  168. Currently this function is fully implemented under GTK and MSW and
  169. always returns an empty string elsewhere.
  170. @since 2.9.5
  171. @return The path of the currently selected file or an empty string if
  172. nothing is selected.
  173. @see SetExtraControlCreator()
  174. */
  175. virtual wxString GetCurrentlySelectedFilename() const;
  176. /**
  177. Returns the default directory.
  178. */
  179. virtual wxString GetDirectory() const;
  180. /**
  181. If functions SetExtraControlCreator() and ShowModal() were called,
  182. returns the extra window. Otherwise returns @NULL.
  183. @since 2.9.0
  184. */
  185. wxWindow* GetExtraControl() const;
  186. /**
  187. Returns the default filename.
  188. */
  189. virtual wxString GetFilename() const;
  190. /**
  191. Fills the array @a filenames with the names of the files chosen.
  192. This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
  193. use GetFilename() for the others.
  194. Note that under Windows, if the user selects shortcuts, the filenames
  195. include paths, since the application cannot determine the full path
  196. of each referenced file by appending the directory containing the shortcuts
  197. to the filename.
  198. */
  199. virtual void GetFilenames(wxArrayString& filenames) const;
  200. /**
  201. Returns the index into the list of filters supplied, optionally, in the
  202. wildcard parameter.
  203. Before the dialog is shown, this is the index which will be used when the
  204. dialog is first displayed.
  205. After the dialog is shown, this is the index selected by the user.
  206. */
  207. virtual int GetFilterIndex() const;
  208. /**
  209. Returns the message that will be displayed on the dialog.
  210. */
  211. virtual wxString GetMessage() const;
  212. /**
  213. Returns the full path (directory and filename) of the selected file.
  214. */
  215. virtual wxString GetPath() const;
  216. /**
  217. Fills the array @a paths with the full paths of the files chosen.
  218. This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
  219. use GetPath() for the others.
  220. */
  221. virtual void GetPaths(wxArrayString& paths) const;
  222. /**
  223. Returns the file dialog wildcard.
  224. */
  225. virtual wxString GetWildcard() const;
  226. /**
  227. Sets the default directory.
  228. */
  229. virtual void SetDirectory(const wxString& directory);
  230. /**
  231. The type of function used as an argument for SetExtraControlCreator().
  232. @since 2.9.0
  233. */
  234. typedef wxWindow *(*ExtraControlCreatorFunction)(wxWindow*);
  235. /**
  236. Customize file dialog by adding extra window, which is typically placed
  237. below the list of files and above the buttons.
  238. SetExtraControlCreator() can be called only once, before calling ShowModal().
  239. The @c creator function should take pointer to parent window (file dialog)
  240. and should return a window allocated with operator new.
  241. Supported platforms: wxGTK, wxMSW, wxUniv.
  242. @since 2.9.0
  243. */
  244. bool SetExtraControlCreator(ExtraControlCreatorFunction creator);
  245. /**
  246. Sets the default filename.
  247. In wxGTK this will have little effect unless a default directory has previously been set.
  248. */
  249. virtual void SetFilename(const wxString& setfilename);
  250. /**
  251. Sets the default filter index, starting from zero.
  252. */
  253. virtual void SetFilterIndex(int filterIndex);
  254. /**
  255. Sets the message that will be displayed on the dialog.
  256. */
  257. virtual void SetMessage(const wxString& message);
  258. /**
  259. Sets the path (the combined directory and filename that will be returned when
  260. the dialog is dismissed).
  261. */
  262. virtual void SetPath(const wxString& path);
  263. /**
  264. Sets the wildcard, which can contain multiple file types, for example:
  265. "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
  266. Note that the native Motif dialog has some limitations with respect to
  267. wildcards; see the Remarks section above.
  268. */
  269. virtual void SetWildcard(const wxString& wildCard);
  270. /**
  271. Shows the dialog, returning @c wxID_OK if the user pressed OK, and @c wxID_CANCEL
  272. otherwise.
  273. */
  274. virtual int ShowModal();
  275. };
  276. // ============================================================================
  277. // Global functions/macros
  278. // ============================================================================
  279. /** @addtogroup group_funcmacro_dialog */
  280. //@{
  281. /**
  282. Pops up a file selector box. In Windows, this is the common file selector
  283. dialog. In X, this is a file selector box with the same functionality. The
  284. path and filename are distinct elements of a full file pathname. If path
  285. is empty, the current directory will be used. If filename is empty, no
  286. default filename will be supplied. The wildcard determines what files are
  287. displayed in the file selector, and file extension supplies a type
  288. extension for the required filename. Flags may be a combination of
  289. wxFD_OPEN, wxFD_SAVE, wxFD_OVERWRITE_PROMPT or wxFD_FILE_MUST_EXIST.
  290. @note wxFD_MULTIPLE can only be used with wxFileDialog and not here since
  291. this function only returns a single file name.
  292. Both the Unix and Windows versions implement a wildcard filter. Typing a
  293. filename containing wildcards (*, ?) in the filename text item, and
  294. clicking on Ok, will result in only those files matching the pattern being
  295. displayed.
  296. The wildcard may be a specification for multiple types of file with a
  297. description for each, such as:
  298. @code
  299. "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif"
  300. @endcode
  301. The application must check for an empty return value (the user pressed
  302. Cancel). For example:
  303. @code
  304. wxString filename = wxFileSelector("Choose a file to open");
  305. if ( !filename.empty() )
  306. {
  307. // work with the file
  308. ...
  309. }
  310. //else: cancelled by user
  311. @endcode
  312. @header{wx/filedlg.h}
  313. */
  314. wxString wxFileSelector(const wxString& message,
  315. const wxString& default_path = wxEmptyString,
  316. const wxString& default_filename = wxEmptyString,
  317. const wxString& default_extension = wxEmptyString,
  318. const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
  319. int flags = 0,
  320. wxWindow* parent = NULL,
  321. int x = wxDefaultCoord,
  322. int y = wxDefaultCoord);
  323. /**
  324. An extended version of wxFileSelector
  325. */
  326. wxString wxFileSelectorEx(const wxString& message = wxFileSelectorPromptStr,
  327. const wxString& default_path = wxEmptyString,
  328. const wxString& default_filename = wxEmptyString,
  329. int *indexDefaultExtension = NULL,
  330. const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
  331. int flags = 0,
  332. wxWindow *parent = NULL,
  333. int x = wxDefaultCoord,
  334. int y = wxDefaultCoord);
  335. /**
  336. Ask for filename to load
  337. */
  338. wxString wxLoadFileSelector(const wxString& what,
  339. const wxString& extension,
  340. const wxString& default_name = wxEmptyString,
  341. wxWindow *parent = NULL);
  342. /**
  343. Ask for filename to save
  344. */
  345. wxString wxSaveFileSelector(const wxString& what,
  346. const wxString& extension,
  347. const wxString& default_name = wxEmptyString,
  348. wxWindow *parent = NULL);
  349. //@}