Fl_Native_File_Chooser.H 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // "$Id: Fl_Native_File_Chooser.H 11681 2016-04-23 06:27:31Z manolo $"
  3. //
  4. // FLTK native OS file chooser widget
  5. //
  6. // Copyright 1998-2016 by Bill Spitzak and others.
  7. // Copyright 2004 Greg Ercolano.
  8. //
  9. // This library is free software. Distribution and use rights are outlined in
  10. // the file "COPYING" which should have been included with this file. If this
  11. // file is missing or damaged, see the license at:
  12. //
  13. // http://www.fltk.org/COPYING.php
  14. //
  15. // Please report all bugs and problems on the following page:
  16. //
  17. // http://www.fltk.org/str.php
  18. //
  19. /** \file
  20. Fl_Native_File_Chooser widget. */
  21. /* Implementation note:
  22. class Fl_Native_File_Chooser <== public API used by applications
  23. class Fl_Native_File_Chooser_Driver <== virtual API that a platform may implement
  24. this API has a do-nothing default implementation
  25. class Fl_Native_File_Chooser_FLTK_Driver <== this API implementation is the default FLTK file chooser
  26. class Fl_GTK_Native_File_Chooser_Driver <== this API implementation runs a GTK file chooser
  27. it is determined at run-time if the GTK dynamic libraries are available
  28. class Fl_Quartz_Native_File_Chooser_Driver <== this API implementation runs a Mac OS X file chooser
  29. class Fl_WinAPI_Native_File_Chooser_Driver <== this API implementation runs a MSWindows file chooser
  30. Each platform must implement the constructor of the Fl_Native_File_Chooser class.
  31. This particular implementation:
  32. Fl_Native_File_Chooser::Fl_Native_File_Chooser(int val) {
  33. platform_fnfc = new Fl_Native_File_Chooser_FLTK_Driver(val);
  34. }
  35. can be used by any platform.
  36. No more code is required. The cross-platform Fl_Native_File_Chooser_FLTK.cxx file must be compiled in libfltk,
  37. and the default FLTK file chooser will be used.
  38. This other implementation:
  39. Fl_Native_File_Chooser::Fl_Native_File_Chooser(int val) {
  40. platform_fnfc = 0;
  41. }
  42. can be used by a platform that needs no file chooser.
  43. */
  44. #ifndef FL_NATIVE_FILE_CHOOSER_H
  45. #define FL_NATIVE_FILE_CHOOSER_H
  46. #include <FL/Fl_Export.H>
  47. #include <FL/Fl_File_Chooser.H>
  48. class Fl_Native_File_Chooser_Driver;
  49. /**
  50. This class lets an FLTK application easily and consistently access
  51. the operating system's native file chooser. Some operating systems
  52. have very complex and specific file choosers that many users want
  53. access to specifically, instead of FLTK's default file chooser(s).
  54. In cases where there is no native file browser, FLTK's own file browser
  55. is used instead.
  56. To use this widget, use the following include in your code:
  57. \code
  58. #include <FL/Fl_Native_File_Chooser.H>
  59. \endcode
  60. The following example shows how to pick a single file:
  61. \code
  62. // Create and post the local native file chooser
  63. #include <FL/Fl_Native_File_Chooser.H>
  64. [..]
  65. Fl_Native_File_Chooser fnfc;
  66. fnfc.title("Pick a file");
  67. fnfc.type(Fl_Native_File_Chooser::BROWSE_FILE);
  68. fnfc.filter("Text\t*.txt\n"
  69. "C Files\t*.{cxx,h,c}");
  70. fnfc.directory("/var/tmp"); // default directory to use
  71. // Show native chooser
  72. switch ( fnfc.show() ) {
  73. case -1: printf("ERROR: %s\n", fnfc.errmsg()); break; // ERROR
  74. case 1: printf("CANCEL\n"); break; // CANCEL
  75. default: printf("PICKED: %s\n", fnfc.filename()); break; // FILE CHOSEN
  76. }
  77. \endcode
  78. The Fl_Native_File_Chooser widget transmits UTF-8 encoded filenames to its user. It is
  79. recommended to open files that may have non-ASCII names with the fl_fopen() or
  80. fl_open() utility functions that handle these names in a cross-platform way
  81. (whereas the standard fopen()/open() functions fail on the MSWindows platform
  82. to open files with a non-ASCII name).
  83. <B>Platform Specific Caveats</B>
  84. - Under X windows, and if Fl::OPTION_FNFC_USES_GTK has not been switched off,
  85. the widget attempts to use standard GTK file chooser dialogs if they are
  86. available at run-time on the platform, and falls back to use FLTK's Fl_File_Chooser if they are not.
  87. In the latter case, it's best if you call Fl_File_Icon::load_system_icons()
  88. at the start of main(), to enable the nicer looking file browser widgets.
  89. Use the static public attributes of class Fl_File_Chooser to localize
  90. the browser.
  91. - Some operating systems support certain OS specific options; see
  92. Fl_Native_File_Chooser::options() for a list.
  93. \image html Fl_Native_File_Chooser.png "The Fl_Native_File_Chooser on different platforms."
  94. \image latex Fl_Native_File_Chooser.png "The Fl_Native_File_Chooser on different platforms" width=14cm
  95. */
  96. class FL_EXPORT Fl_Native_File_Chooser {
  97. private:
  98. Fl_Native_File_Chooser_Driver *platform_fnfc;
  99. public:
  100. enum Type {
  101. BROWSE_FILE = 0, ///< browse files (lets user choose one file)
  102. BROWSE_DIRECTORY, ///< browse directories (lets user choose one directory)
  103. BROWSE_MULTI_FILE, ///< browse files (lets user choose multiple files)
  104. BROWSE_MULTI_DIRECTORY, ///< browse directories (lets user choose multiple directories)
  105. BROWSE_SAVE_FILE, ///< browse to save a file
  106. BROWSE_SAVE_DIRECTORY ///< browse to save a directory
  107. };
  108. enum Option {
  109. NO_OPTIONS = 0x0000, ///< no options enabled
  110. SAVEAS_CONFIRM = 0x0001, ///< Show native 'Save As' overwrite confirm dialog
  111. NEW_FOLDER = 0x0002, ///< Show 'New Folder' icon (if supported)
  112. PREVIEW = 0x0004, ///< enable preview mode (if supported)
  113. USE_FILTER_EXT = 0x0008 ///< Chooser filter pilots the output file extension (if supported)
  114. };
  115. static const char *file_exists_message;
  116. /**
  117. The constructor. Internally allocates the native widgets.
  118. Optional \p val presets the type of browser this will be,
  119. which can also be changed with type().
  120. */
  121. Fl_Native_File_Chooser(int val = BROWSE_FILE); // each platorm implements it
  122. ~Fl_Native_File_Chooser();
  123. void type(int t);
  124. int type() const ;
  125. void options(int o);
  126. int options() const;
  127. int count() const;
  128. const char *filename() const ;
  129. const char *filename(int i) const ;
  130. void directory(const char *val) ;
  131. const char *directory() const;
  132. void title(const char *t);
  133. const char* title() const;
  134. const char *filter() const ;
  135. void filter(const char *f);
  136. int filters() const ;
  137. void filter_value(int i) ;
  138. int filter_value() const ;
  139. void preset_file(const char*f) ;
  140. const char* preset_file() const;
  141. const char *errmsg() const ;
  142. int show() ;
  143. };
  144. /** Represents the interface between FLTK and a native file chooser.
  145. This class is only for internal use by the FLTK library.
  146. A platform that wants to provide a native file chooser implements all virtual methods
  147. of this class. Each platform supported by FLTK must also implement the constructor of the
  148. Fl_Native_File_Chooser class.
  149. */
  150. class Fl_Native_File_Chooser_Driver {
  151. protected:
  152. static void chrcat(char *s, char c);
  153. static char *strapp(char *s, const char *val);
  154. static char *strfree(char *val);
  155. static char *strnew(const char *val);
  156. public:
  157. Fl_Native_File_Chooser_Driver(int val) {}
  158. virtual ~Fl_Native_File_Chooser_Driver() {}
  159. virtual void type(int t) {}
  160. virtual int type() const {return 0;}
  161. virtual void options(int o) {}
  162. virtual int options() const {return 0;}
  163. virtual int count() const {return 0;}
  164. virtual const char *filename() const {return 0;}
  165. virtual const char *filename(int i) const {return 0;}
  166. virtual void directory(const char *val) {}
  167. virtual const char *directory() const {return 0;}
  168. virtual void title(const char *t) {}
  169. virtual const char* title() const {return 0;}
  170. virtual const char *filter() const {return 0;}
  171. virtual void filter(const char *f) {}
  172. virtual int filters() const {return 0;}
  173. virtual void filter_value(int i) {}
  174. virtual int filter_value() const {return 0;}
  175. virtual void preset_file(const char*f) {}
  176. virtual const char* preset_file() const {return 0;}
  177. virtual const char *errmsg() const {return 0;}
  178. virtual int show() {return 1;}
  179. };
  180. /** A cross-platform implementation of Fl_Native_File_Chooser_Driver.
  181. This implementation uses a Fl_File_Chooser object as file chooser.
  182. Any platform can support the Fl_Native_File_Chooser class by implementing
  183. its constructor as follows:
  184. \code
  185. Fl_Native_File_Chooser::Fl_Native_File_Chooser(int type) {
  186. platform_fnfc = new Fl_Native_File_Chooser_FLTK_Driver(type);
  187. }
  188. \endcode
  189. */
  190. class Fl_Native_File_Chooser_FLTK_Driver : public Fl_Native_File_Chooser_Driver {
  191. private:
  192. void errmsg(const char *msg);
  193. int type_fl_file(int val);
  194. int exist_dialog();
  195. void parse_filter();
  196. protected:
  197. int _btype; // kind-of browser to show()
  198. int _options; // general options
  199. int _nfilters;
  200. char *_filter; // user supplied filter
  201. char *_parsedfilt; // parsed filter
  202. int _filtvalue; // selected filter
  203. char *_preset_file;
  204. char *_prevvalue; // Returned filename
  205. char *_directory;
  206. char *_errmsg; // error message
  207. Fl_File_Chooser *_file_chooser;
  208. public:
  209. Fl_Native_File_Chooser_FLTK_Driver(int val);
  210. virtual ~Fl_Native_File_Chooser_FLTK_Driver();
  211. virtual void type(int t);
  212. virtual int type() const ;
  213. virtual void options(int o);
  214. virtual int options() const;
  215. virtual int count() const;
  216. virtual const char *filename() const ;
  217. virtual const char *filename(int i) const ;
  218. virtual void directory(const char *val) ;
  219. virtual const char *directory() const;
  220. virtual void title(const char *t);
  221. virtual const char* title() const;
  222. virtual const char *filter() const ;
  223. virtual void filter(const char *f);
  224. virtual int filters() const ;
  225. virtual void filter_value(int i) ;
  226. virtual int filter_value() const ;
  227. virtual void preset_file(const char*f) ;
  228. virtual const char* preset_file() const;
  229. virtual const char *errmsg() const ;
  230. virtual int show() ;
  231. };
  232. #endif /*FL_NATIVE_FILE_CHOOSER_H*/
  233. //
  234. // End of "$Id: Fl_Native_File_Chooser.H 11681 2016-04-23 06:27:31Z manolo $".
  235. //