2010-01-14 02:13:59 +03:00
|
|
|
//
|
|
|
|
// FLTK native OS file chooser widget
|
|
|
|
//
|
2016-04-05 14:25:35 +03:00
|
|
|
// Copyright 1998-2016 by Bill Spitzak and others.
|
2010-01-14 02:13:59 +03:00
|
|
|
// Copyright 2004 Greg Ercolano.
|
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
2010-01-14 02:13:59 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2010-01-14 02:13:59 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2010-01-14 02:13:59 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2010-01-14 02:13:59 +03:00
|
|
|
//
|
|
|
|
|
2011-02-06 13:07:28 +03:00
|
|
|
/** \file
|
2011-02-05 20:45:00 +03:00
|
|
|
Fl_Native_File_Chooser widget. */
|
|
|
|
|
2016-04-05 14:25:35 +03:00
|
|
|
/* Implementation note:
|
2018-02-09 17:39:42 +03:00
|
|
|
|
2016-04-05 14:25:35 +03:00
|
|
|
class Fl_Native_File_Chooser <== public API used by applications
|
2018-02-09 17:39:42 +03:00
|
|
|
|
2016-04-05 14:25:35 +03:00
|
|
|
class Fl_Native_File_Chooser_Driver <== virtual API that a platform may implement
|
|
|
|
this API has a do-nothing default implementation
|
2018-02-09 17:39:42 +03:00
|
|
|
|
2016-04-05 14:25:35 +03:00
|
|
|
class Fl_Native_File_Chooser_FLTK_Driver <== this API implementation is the default FLTK file chooser
|
2016-04-16 11:37:15 +03:00
|
|
|
class Fl_GTK_Native_File_Chooser_Driver <== this API implementation runs a GTK file chooser
|
2021-12-07 13:24:35 +03:00
|
|
|
class Fl_Kdialog_Native_File_Chooser_Driver <== this API implementation runs a KDE file chooser
|
2016-04-05 14:25:35 +03:00
|
|
|
it is determined at run-time if the GTK dynamic libraries are available
|
2021-12-07 13:24:35 +03:00
|
|
|
and the KDE file chooser runs under the KDE desktop
|
2018-02-09 17:39:42 +03:00
|
|
|
|
2016-04-16 11:37:15 +03:00
|
|
|
class Fl_Quartz_Native_File_Chooser_Driver <== this API implementation runs a Mac OS X file chooser
|
2016-01-04 01:54:29 +03:00
|
|
|
|
2018-02-09 17:39:42 +03:00
|
|
|
class Fl_WinAPI_Native_File_Chooser_Driver <== this API implementation runs a Windows file chooser
|
|
|
|
|
|
|
|
|
2016-04-05 14:25:35 +03:00
|
|
|
Each platform must implement the constructor of the Fl_Native_File_Chooser class.
|
|
|
|
This particular implementation:
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2016-04-05 14:25:35 +03:00
|
|
|
Fl_Native_File_Chooser::Fl_Native_File_Chooser(int val) {
|
|
|
|
platform_fnfc = new Fl_Native_File_Chooser_FLTK_Driver(val);
|
|
|
|
}
|
2020-07-01 19:03:10 +03:00
|
|
|
|
|
|
|
can be used by any platform.
|
2016-04-05 14:25:35 +03:00
|
|
|
No more code is required. The cross-platform Fl_Native_File_Chooser_FLTK.cxx file must be compiled in libfltk,
|
|
|
|
and the default FLTK file chooser will be used.
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2016-04-16 11:37:15 +03:00
|
|
|
This other implementation:
|
2016-04-16 10:55:51 +03:00
|
|
|
Fl_Native_File_Chooser::Fl_Native_File_Chooser(int val) {
|
|
|
|
platform_fnfc = 0;
|
|
|
|
}
|
|
|
|
can be used by a platform that needs no file chooser.
|
2016-04-05 14:25:35 +03:00
|
|
|
*/
|
2010-01-14 02:13:59 +03:00
|
|
|
|
2016-04-05 14:25:35 +03:00
|
|
|
#ifndef FL_NATIVE_FILE_CHOOSER_H
|
|
|
|
#define FL_NATIVE_FILE_CHOOSER_H
|
2016-01-04 01:54:29 +03:00
|
|
|
|
2016-04-05 14:25:35 +03:00
|
|
|
#include <FL/Fl_Export.H>
|
|
|
|
#include <FL/Fl_File_Chooser.H>
|
2011-02-06 01:35:51 +03:00
|
|
|
|
2016-04-05 14:25:35 +03:00
|
|
|
class Fl_Native_File_Chooser_Driver;
|
2011-02-06 01:35:51 +03:00
|
|
|
|
|
|
|
/**
|
2020-07-01 19:03:10 +03:00
|
|
|
This class lets an FLTK application easily and consistently access
|
|
|
|
the operating system's native file chooser. Some operating systems
|
|
|
|
have very complex and specific file choosers that many users want
|
|
|
|
access to specifically, instead of FLTK's default file chooser(s).
|
|
|
|
|
2011-02-06 01:35:51 +03:00
|
|
|
In cases where there is no native file browser, FLTK's own file browser
|
|
|
|
is used instead.
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2011-02-06 01:35:51 +03:00
|
|
|
To use this widget, use the following include in your code:
|
|
|
|
\code
|
|
|
|
#include <FL/Fl_Native_File_Chooser.H>
|
|
|
|
\endcode
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2011-02-06 01:35:51 +03:00
|
|
|
The following example shows how to pick a single file:
|
|
|
|
\code
|
|
|
|
// Create and post the local native file chooser
|
|
|
|
#include <FL/Fl_Native_File_Chooser.H>
|
|
|
|
[..]
|
|
|
|
Fl_Native_File_Chooser fnfc;
|
|
|
|
fnfc.title("Pick a file");
|
|
|
|
fnfc.type(Fl_Native_File_Chooser::BROWSE_FILE);
|
|
|
|
fnfc.filter("Text\t*.txt\n"
|
|
|
|
"C Files\t*.{cxx,h,c}");
|
|
|
|
fnfc.directory("/var/tmp"); // default directory to use
|
|
|
|
// Show native chooser
|
|
|
|
switch ( fnfc.show() ) {
|
|
|
|
case -1: printf("ERROR: %s\n", fnfc.errmsg()); break; // ERROR
|
|
|
|
case 1: printf("CANCEL\n"); break; // CANCEL
|
|
|
|
default: printf("PICKED: %s\n", fnfc.filename()); break; // FILE CHOSEN
|
|
|
|
}
|
|
|
|
\endcode
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2012-10-19 13:23:05 +04:00
|
|
|
The Fl_Native_File_Chooser widget transmits UTF-8 encoded filenames to its user. It is
|
2012-10-19 15:23:51 +04:00
|
|
|
recommended to open files that may have non-ASCII names with the fl_fopen() or
|
2018-02-09 17:39:42 +03:00
|
|
|
fl_open() utility functions that handle these names in a cross-platform way
|
|
|
|
(whereas the standard fopen()/open() functions fail on the Windows platform
|
2012-10-19 15:23:51 +04:00
|
|
|
to open files with a non-ASCII name).
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2011-02-06 01:35:51 +03:00
|
|
|
<B>Platform Specific Caveats</B>
|
2022-12-19 17:23:20 +03:00
|
|
|
|
2022-12-13 20:37:48 +03:00
|
|
|
- Under X11/Wayland, what dialog opens is determined as follows:
|
|
|
|
-# If command \p zenity is available at run-time and if \p Fl::option(OPTION_FNFC_USES_ZENITY) is
|
|
|
|
not turned off, the \p zenity -based dialog opens. This is expected to be more appropriate
|
|
|
|
than other dialog forms for sandboxed apps.
|
|
|
|
-# Else if the app runs under the KDE desktop and if command \p kdialog is available at run-time
|
|
|
|
and if the library was not built with <tt>cmake -DOPTION_USE_KDIALOG=Off</tt>, the
|
|
|
|
\p kdialog -based dialog opens.
|
|
|
|
-# Else if the GTK library is available at run-time on the computer and if \p Fl::option(OPTION_FNFC_USES_GTK) is
|
|
|
|
not turned off, the GTK-styled dialog opens. Call fl_register_images() to add a "Preview" button to this dialog.
|
|
|
|
Use the static public attributes of class Fl_File_Chooser to localize the browser.
|
|
|
|
-# Otherwise, FLTK's own dialog produced by the Fl_File_Chooser class opens.
|
|
|
|
Call fl_register_images() to add a "Preview" button to it.
|
|
|
|
It's best if you also call Fl_File_Icon::load_system_icons()
|
2011-02-06 01:35:51 +03:00
|
|
|
at the start of main(), to enable the nicer looking file browser widgets.
|
2022-12-13 20:37:48 +03:00
|
|
|
Use the static public attributes of class Fl_File_Chooser to localize the browser.
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
- Some operating systems support certain OS specific options; see
|
2011-02-06 01:35:51 +03:00
|
|
|
Fl_Native_File_Chooser::options() for a list.
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2011-02-06 01:35:51 +03:00
|
|
|
\image html Fl_Native_File_Chooser.png "The Fl_Native_File_Chooser on different platforms."
|
|
|
|
\image latex Fl_Native_File_Chooser.png "The Fl_Native_File_Chooser on different platforms" width=14cm
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2011-02-06 01:35:51 +03:00
|
|
|
*/
|
|
|
|
class FL_EXPORT Fl_Native_File_Chooser {
|
2016-04-05 14:25:35 +03:00
|
|
|
private:
|
|
|
|
Fl_Native_File_Chooser_Driver *platform_fnfc;
|
2011-02-06 01:35:51 +03:00
|
|
|
public:
|
|
|
|
enum Type {
|
2020-07-01 19:03:10 +03:00
|
|
|
BROWSE_FILE = 0, ///< browse files (lets user choose one file)
|
|
|
|
BROWSE_DIRECTORY, ///< browse directories (lets user choose one directory)
|
|
|
|
BROWSE_MULTI_FILE, ///< browse files (lets user choose multiple files)
|
|
|
|
BROWSE_MULTI_DIRECTORY, ///< browse directories (lets user choose multiple directories)
|
|
|
|
BROWSE_SAVE_FILE, ///< browse to save a file
|
|
|
|
BROWSE_SAVE_DIRECTORY ///< browse to save a directory
|
2011-02-06 01:35:51 +03:00
|
|
|
};
|
|
|
|
enum Option {
|
2020-07-01 19:03:10 +03:00
|
|
|
NO_OPTIONS = 0x0000, ///< no options enabled
|
|
|
|
SAVEAS_CONFIRM = 0x0001, ///< Show native 'Save As' overwrite confirm dialog
|
|
|
|
NEW_FOLDER = 0x0002, ///< Show 'New Folder' icon (if supported)
|
|
|
|
PREVIEW = 0x0004, ///< enable preview mode (if supported)
|
|
|
|
USE_FILTER_EXT = 0x0008 ///< Chooser filter pilots the output file extension (if supported)
|
2011-02-06 01:35:51 +03:00
|
|
|
};
|
|
|
|
static const char *file_exists_message;
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2016-04-05 14:25:35 +03:00
|
|
|
/**
|
|
|
|
The constructor. Internally allocates the native widgets.
|
|
|
|
Optional \p val presets the type of browser this will be,
|
|
|
|
which can also be changed with type().
|
|
|
|
*/
|
|
|
|
Fl_Native_File_Chooser(int val = BROWSE_FILE); // each platorm implements it
|
2011-02-06 01:35:51 +03:00
|
|
|
~Fl_Native_File_Chooser();
|
2014-06-07 16:01:59 +04:00
|
|
|
void type(int t);
|
|
|
|
int type() const ;
|
|
|
|
void options(int o);
|
2011-02-06 01:35:51 +03:00
|
|
|
int options() const;
|
|
|
|
int count() const;
|
2014-06-07 16:01:59 +04:00
|
|
|
const char *filename() const ;
|
|
|
|
const char *filename(int i) const ;
|
|
|
|
void directory(const char *val) ;
|
2011-02-06 01:35:51 +03:00
|
|
|
const char *directory() const;
|
2014-06-07 16:01:59 +04:00
|
|
|
void title(const char *t);
|
2011-02-06 01:35:51 +03:00
|
|
|
const char* title() const;
|
2014-06-07 16:01:59 +04:00
|
|
|
const char *filter() const ;
|
|
|
|
void filter(const char *f);
|
|
|
|
int filters() const ;
|
|
|
|
void filter_value(int i) ;
|
|
|
|
int filter_value() const ;
|
|
|
|
void preset_file(const char*f) ;
|
2011-02-06 01:35:51 +03:00
|
|
|
const char* preset_file() const;
|
2014-06-07 16:01:59 +04:00
|
|
|
const char *errmsg() const ;
|
|
|
|
int show() ;
|
2016-04-05 14:25:35 +03:00
|
|
|
};
|
2016-01-04 01:54:29 +03:00
|
|
|
|
2018-06-23 19:47:40 +03:00
|
|
|
/**
|
2018-06-23 23:50:22 +03:00
|
|
|
\cond DriverDev
|
|
|
|
\addtogroup DriverDeveloper
|
|
|
|
\{
|
2018-06-23 19:47:40 +03:00
|
|
|
*/
|
|
|
|
|
2016-04-23 09:27:31 +03:00
|
|
|
/** Represents the interface between FLTK and a native file chooser.
|
|
|
|
This class is only for internal use by the FLTK library.
|
|
|
|
A platform that wants to provide a native file chooser implements all virtual methods
|
|
|
|
of this class. Each platform supported by FLTK must also implement the constructor of the
|
|
|
|
Fl_Native_File_Chooser class.
|
|
|
|
*/
|
2016-04-05 14:25:35 +03:00
|
|
|
class Fl_Native_File_Chooser_Driver {
|
|
|
|
protected:
|
|
|
|
static void chrcat(char *s, char c);
|
|
|
|
static char *strapp(char *s, const char *val);
|
|
|
|
static char *strfree(char *val);
|
|
|
|
static char *strnew(const char *val);
|
|
|
|
public:
|
2021-12-09 10:09:44 +03:00
|
|
|
Fl_Native_File_Chooser_Driver(int) {}
|
2016-04-05 14:25:35 +03:00
|
|
|
virtual ~Fl_Native_File_Chooser_Driver() {}
|
2021-12-09 10:09:44 +03:00
|
|
|
virtual void type(int) {}
|
2016-04-05 14:25:35 +03:00
|
|
|
virtual int type() const {return 0;}
|
2021-12-09 10:09:44 +03:00
|
|
|
virtual void options(int) {}
|
2016-04-05 14:25:35 +03:00
|
|
|
virtual int options() const {return 0;}
|
|
|
|
virtual int count() const {return 0;}
|
|
|
|
virtual const char *filename() const {return 0;}
|
2021-12-09 10:09:44 +03:00
|
|
|
virtual const char *filename(int) const {return 0;}
|
|
|
|
virtual void directory(const char *) {}
|
2016-04-05 14:25:35 +03:00
|
|
|
virtual const char *directory() const {return 0;}
|
2021-12-09 10:09:44 +03:00
|
|
|
virtual void title(const char *) {}
|
2016-04-05 14:25:35 +03:00
|
|
|
virtual const char* title() const {return 0;}
|
|
|
|
virtual const char *filter() const {return 0;}
|
2021-12-09 10:09:44 +03:00
|
|
|
virtual void filter(const char *) {}
|
2016-04-05 14:25:35 +03:00
|
|
|
virtual int filters() const {return 0;}
|
2021-12-09 10:09:44 +03:00
|
|
|
virtual void filter_value(int) {}
|
2016-04-05 14:25:35 +03:00
|
|
|
virtual int filter_value() const {return 0;}
|
2021-12-09 10:09:44 +03:00
|
|
|
virtual void preset_file(const char*) {}
|
2016-04-05 14:25:35 +03:00
|
|
|
virtual const char* preset_file() const {return 0;}
|
|
|
|
virtual const char *errmsg() const {return 0;}
|
|
|
|
virtual int show() {return 1;}
|
2014-06-07 16:01:59 +04:00
|
|
|
};
|
2013-06-06 16:11:08 +04:00
|
|
|
|
2016-04-23 09:27:31 +03:00
|
|
|
/** A cross-platform implementation of Fl_Native_File_Chooser_Driver.
|
|
|
|
This implementation uses a Fl_File_Chooser object as file chooser.
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2016-04-23 09:27:31 +03:00
|
|
|
Any platform can support the Fl_Native_File_Chooser class by implementing
|
|
|
|
its constructor as follows:
|
|
|
|
\code
|
|
|
|
Fl_Native_File_Chooser::Fl_Native_File_Chooser(int type) {
|
|
|
|
platform_fnfc = new Fl_Native_File_Chooser_FLTK_Driver(type);
|
|
|
|
}
|
|
|
|
\endcode
|
|
|
|
*/
|
2016-04-05 14:25:35 +03:00
|
|
|
class Fl_Native_File_Chooser_FLTK_Driver : public Fl_Native_File_Chooser_Driver {
|
|
|
|
private:
|
|
|
|
void errmsg(const char *msg);
|
|
|
|
int type_fl_file(int val);
|
|
|
|
int exist_dialog();
|
|
|
|
void parse_filter();
|
2014-06-07 16:01:59 +04:00
|
|
|
protected:
|
2020-07-01 19:03:10 +03:00
|
|
|
int _btype; // kind-of browser to show()
|
|
|
|
int _options; // general options
|
2014-06-07 16:01:59 +04:00
|
|
|
int _nfilters;
|
2020-07-01 19:03:10 +03:00
|
|
|
char *_filter; // user supplied filter
|
|
|
|
char *_parsedfilt; // parsed filter
|
|
|
|
int _filtvalue; // selected filter
|
2014-06-07 16:01:59 +04:00
|
|
|
char *_preset_file;
|
2020-07-01 19:03:10 +03:00
|
|
|
char *_prevvalue; // Returned filename
|
2014-06-07 16:01:59 +04:00
|
|
|
char *_directory;
|
2020-07-01 19:03:10 +03:00
|
|
|
char *_errmsg; // error message
|
2014-06-07 16:01:59 +04:00
|
|
|
Fl_File_Chooser *_file_chooser;
|
2016-04-05 14:25:35 +03:00
|
|
|
public:
|
|
|
|
Fl_Native_File_Chooser_FLTK_Driver(int val);
|
|
|
|
virtual ~Fl_Native_File_Chooser_FLTK_Driver();
|
2022-12-30 21:14:36 +03:00
|
|
|
void type(int t) FL_OVERRIDE;
|
|
|
|
int type() const FL_OVERRIDE;
|
|
|
|
void options(int o) FL_OVERRIDE;
|
|
|
|
int options() const FL_OVERRIDE;
|
|
|
|
int count() const FL_OVERRIDE;
|
|
|
|
const char *filename() const FL_OVERRIDE;
|
|
|
|
const char *filename(int i) const FL_OVERRIDE;
|
|
|
|
void directory(const char *val) FL_OVERRIDE;
|
|
|
|
const char *directory() const FL_OVERRIDE;
|
|
|
|
void title(const char *t) FL_OVERRIDE;
|
|
|
|
const char* title() const FL_OVERRIDE;
|
|
|
|
const char *filter() const FL_OVERRIDE;
|
|
|
|
void filter(const char *f) FL_OVERRIDE;
|
|
|
|
int filters() const FL_OVERRIDE;
|
|
|
|
void filter_value(int i) FL_OVERRIDE;
|
|
|
|
int filter_value() const FL_OVERRIDE;
|
|
|
|
void preset_file(const char*f) FL_OVERRIDE;
|
|
|
|
const char* preset_file() const FL_OVERRIDE;
|
|
|
|
const char *errmsg() const FL_OVERRIDE;
|
|
|
|
int show() FL_OVERRIDE;
|
2011-02-06 01:35:51 +03:00
|
|
|
};
|
|
|
|
|
2018-06-23 19:47:40 +03:00
|
|
|
/**
|
2018-06-23 23:50:22 +03:00
|
|
|
\}
|
|
|
|
\endcond
|
2018-06-23 19:47:40 +03:00
|
|
|
*/
|
|
|
|
|
2010-01-14 02:13:59 +03:00
|
|
|
|
|
|
|
#endif /*FL_NATIVE_FILE_CHOOSER_H*/
|