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.

306 lines
10KB

  1. //
  2. // "$Id: Fl_Native_File_Chooser.H 8380 2011-02-06 10:07:28Z manolo $"
  3. //
  4. // FLTK native OS file chooser widget
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. // Copyright 2004 Greg Ercolano.
  8. //
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Library General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. // Library General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU Library General Public
  20. // License along with this library; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. // USA.
  23. //
  24. // Please report all bugs and problems on the following page:
  25. //
  26. // http://www.fltk.org/str.php
  27. //
  28. /** \file
  29. Fl_Native_File_Chooser widget. */
  30. /**
  31. \class Fl_Native_File_Chooser
  32. This class lets an FLTK application easily and consistently access
  33. the operating system's native file chooser. Some operating systems
  34. have very complex and specific file choosers that many users want
  35. access to specifically, instead of FLTK's default file chooser(s).
  36. In cases where there is no native file browser, FLTK's own file browser
  37. is used instead.
  38. To use this widget correctly, use the following include in your code:
  39. \code
  40. #include <FL/Fl_Native_File_Chooser.H>
  41. \endcode
  42. Do not include the other Fl_Native_File_Choser_XXX.H files in your code;
  43. those are platform specific files that will be included automatically
  44. depending on your build platform.
  45. The following example shows how to pick a single file:
  46. \code
  47. // Create and post the local native file chooser
  48. #include <FL/Fl_Native_File_Chooser.H>
  49. [..]
  50. Fl_Native_File_Chooser fnfc;
  51. fnfc.title("Pick a file");
  52. fnfc.type(Fl_Native_File_Chooser::BROWSE_FILE);
  53. fnfc.filter("Text\t*.txt\n"
  54. "C Files\t*.{cxx,h,c}");
  55. fnfc.directory("/var/tmp"); // default directory to use
  56. // Show native chooser
  57. switch ( fnfc.show() ) {
  58. case -1: printf("ERROR: %s\n", fnfc.errmsg()); break; // ERROR
  59. case 1: printf("CANCEL\n"); break; // CANCEL
  60. default: printf("PICKED: %s\n", fnfc.filename()); break; // FILE CHOSEN
  61. }
  62. \endcode
  63. <B>Platform Specific Caveats</B>
  64. - Under X windows, it's best if you call Fl_File_Icon::load_system_icons()
  65. at the start of main(), to enable the nicer looking file browser widgets.
  66. Use the static public attributes of class Fl_File_Chooser to localize
  67. the browser.
  68. - Some operating systems support certain OS specific options; see
  69. Fl_Native_File_Chooser::options() for a list.
  70. \image html Fl_Native_File_Chooser.png "The Fl_Native_File_Chooser on different platforms."
  71. \image latex Fl_Native_File_Chooser.png "The Fl_Native_File_Chooser on different platforms" width=14cm
  72. */
  73. #ifndef FL_NATIVE_FILE_CHOOSER_H
  74. #define FL_NATIVE_FILE_CHOOSER_H
  75. /* \file
  76. Fl_Native_File_Chooser widget. */
  77. // Use Windows' chooser
  78. #ifdef WIN32
  79. // #define _WIN32_WINNT 0x0501 // needed for OPENFILENAME's 'FlagsEx'
  80. #include <stdio.h>
  81. #include <stdlib.h> // malloc
  82. #include <windows.h>
  83. #include <commdlg.h> // OPENFILENAME, GetOpenFileName()
  84. #include <shlobj.h> // BROWSEINFO, SHBrowseForFolder()
  85. #endif
  86. // Use Apple's chooser
  87. #ifdef __APPLE__
  88. #include <FL/filename.H>
  89. #define MAXFILTERS 80
  90. #endif
  91. // All else falls back to FLTK's own chooser
  92. #if ! defined(__APPLE__) && !defined(WIN32)
  93. #include <FL/Fl_File_Chooser.H>
  94. #include <unistd.h> // _POSIX_NAME_MAX
  95. #endif
  96. /**
  97. This class lets an FLTK application easily and consistently access
  98. the operating system's native file chooser. Some operating systems
  99. have very complex and specific file choosers that many users want
  100. access to specifically, instead of FLTK's default file chooser(s).
  101. In cases where there is no native file browser, FLTK's own file browser
  102. is used instead.
  103. To use this widget, use the following include in your code:
  104. \code
  105. #include <FL/Fl_Native_File_Chooser.H>
  106. \endcode
  107. The following example shows how to pick a single file:
  108. \code
  109. // Create and post the local native file chooser
  110. #include <FL/Fl_Native_File_Chooser.H>
  111. [..]
  112. Fl_Native_File_Chooser fnfc;
  113. fnfc.title("Pick a file");
  114. fnfc.type(Fl_Native_File_Chooser::BROWSE_FILE);
  115. fnfc.filter("Text\t*.txt\n"
  116. "C Files\t*.{cxx,h,c}");
  117. fnfc.directory("/var/tmp"); // default directory to use
  118. // Show native chooser
  119. switch ( fnfc.show() ) {
  120. case -1: printf("ERROR: %s\n", fnfc.errmsg()); break; // ERROR
  121. case 1: printf("CANCEL\n"); break; // CANCEL
  122. default: printf("PICKED: %s\n", fnfc.filename()); break; // FILE CHOSEN
  123. }
  124. \endcode
  125. <B>Platform Specific Caveats</B>
  126. - Under X windows, it's best if you call Fl_File_Icon::load_system_icons()
  127. at the start of main(), to enable the nicer looking file browser widgets.
  128. Use the static public attributes of class Fl_File_Chooser to localize
  129. the browser.
  130. - Some operating systems support certain OS specific options; see
  131. Fl_Native_File_Chooser::options() for a list.
  132. \image html Fl_Native_File_Chooser.png "The Fl_Native_File_Chooser on different platforms."
  133. \image latex Fl_Native_File_Chooser.png "The Fl_Native_File_Chooser on different platforms" width=14cm
  134. */
  135. class FL_EXPORT Fl_Native_File_Chooser {
  136. public:
  137. enum Type {
  138. BROWSE_FILE = 0, ///< browse files (lets user choose one file)
  139. BROWSE_DIRECTORY, ///< browse directories (lets user choose one directory)
  140. BROWSE_MULTI_FILE, ///< browse files (lets user choose multiple files)
  141. BROWSE_MULTI_DIRECTORY, ///< browse directories (lets user choose multiple directories)
  142. BROWSE_SAVE_FILE, ///< browse to save a file
  143. BROWSE_SAVE_DIRECTORY ///< browse to save a directory
  144. };
  145. enum Option {
  146. NO_OPTIONS = 0x0000, ///< no options enabled
  147. SAVEAS_CONFIRM = 0x0001, ///< Show native 'Save As' overwrite confirm dialog (if supported)
  148. NEW_FOLDER = 0x0002, ///< Show 'New Folder' icon (if supported)
  149. PREVIEW = 0x0004 ///< enable preview mode
  150. };
  151. /** Localizable message */
  152. static const char *file_exists_message;
  153. public:
  154. Fl_Native_File_Chooser(int val=BROWSE_FILE);
  155. ~Fl_Native_File_Chooser();
  156. // Public methods
  157. void type(int);
  158. int type() const;
  159. void options(int);
  160. int options() const;
  161. int count() const;
  162. const char *filename() const;
  163. const char *filename(int i) const;
  164. void directory(const char *val);
  165. const char *directory() const;
  166. void title(const char *);
  167. const char* title() const;
  168. const char *filter() const;
  169. void filter(const char *);
  170. int filters() const;
  171. void filter_value(int i);
  172. int filter_value() const;
  173. void preset_file(const char*);
  174. const char* preset_file() const;
  175. const char *errmsg() const;
  176. int show();
  177. #ifdef WIN32
  178. private:
  179. int _btype; // kind-of browser to show()
  180. int _options; // general options
  181. OPENFILENAMEW _ofn; // GetOpenFileName() & GetSaveFileName() struct
  182. BROWSEINFO _binf; // SHBrowseForFolder() struct
  183. char **_pathnames; // array of pathnames
  184. int _tpathnames; // total pathnames
  185. char *_directory; // default pathname to use
  186. char *_title; // title for window
  187. char *_filter; // user-side search filter
  188. char *_parsedfilt; // filter parsed for Windows dialog
  189. int _nfilters; // number of filters parse_filter counted
  190. char *_preset_file; // the file to preselect
  191. char *_errmsg; // error message
  192. // Private methods
  193. void errmsg(const char *msg);
  194. void clear_pathnames();
  195. void set_single_pathname(const char *s);
  196. void add_pathname(const char *s);
  197. void FreePIDL(ITEMIDLIST *pidl);
  198. void ClearOFN();
  199. void ClearBINF();
  200. void Win2Unix(char *s);
  201. void Unix2Win(char *s);
  202. int showfile();
  203. static int CALLBACK Dir_CB(HWND win, UINT msg, LPARAM param, LPARAM data);
  204. int showdir();
  205. void parse_filter(const char *);
  206. void clear_filters();
  207. void add_filter(const char *, const char *);
  208. #endif
  209. #ifdef __APPLE__
  210. private:
  211. int _btype; // kind-of browser to show()
  212. int _options; // general options
  213. void *_panel;
  214. char **_pathnames; // array of pathnames
  215. int _tpathnames; // total pathnames
  216. char *_directory; // default pathname to use
  217. char *_title; // title for window
  218. char *_preset_file; // the 'save as' filename
  219. char *_filter; // user-side search filter, eg:
  220. // C Files\t*.[ch]\nText Files\t*.txt"
  221. char *_filt_names; // filter names (tab delimited)
  222. // eg. "C Files\tText Files"
  223. char *_filt_patt[MAXFILTERS];
  224. // array of filter patterns, eg:
  225. // _filt_patt[0]="*.{cxx,h}"
  226. // _filt_patt[1]="*.txt"
  227. int _filt_total; // parse_filter() # of filters loaded
  228. int _filt_value; // index of the selected filter
  229. char *_errmsg; // error message
  230. // Private methods
  231. void errmsg(const char *msg);
  232. void clear_pathnames();
  233. void set_single_pathname(const char *s);
  234. int get_saveas_basename(void);
  235. void clear_filters();
  236. void add_filter(const char *, const char *);
  237. void parse_filter(const char *from);
  238. int post();
  239. #endif
  240. #if ! defined(__APPLE__) && !defined(WIN32)
  241. private:
  242. int _btype; // kind-of browser to show()
  243. int _options; // general options
  244. int _nfilters;
  245. char *_filter; // user supplied filter
  246. char *_parsedfilt; // parsed filter
  247. int _filtvalue; // selected filter
  248. char *_preset_file;
  249. char *_prevvalue; // Returned filename
  250. char *_directory;
  251. char *_errmsg; // error message
  252. Fl_File_Chooser *_file_chooser;
  253. // Private methods
  254. void errmsg(const char *msg);
  255. int type_fl_file(int);
  256. void parse_filter();
  257. void keeplocation();
  258. int exist_dialog();
  259. #endif
  260. };
  261. #endif /*FL_NATIVE_FILE_CHOOSER_H*/
  262. //
  263. // End of "$Id: Fl_Native_File_Chooser.H 8380 2011-02-06 10:07:28Z manolo $".
  264. //