# [Fltk-2.x] NativeFileChooser

## class fltk::NativeFileChooser

### Include Files

      #include <fltk/NativeFileChooser.h>

Dowload latest source from here.

### Description

This class lets an FLTK2 application easily and consistently access the local 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 FLTK2's default file chooser(s).

### Methods

- NativeFileChooser
- ~NativeFileChooser
- count

- directory
- errmsg
- filename

- filter
- filter_value
- filters

- options
- preset_file
- show

- title
- type

#### fltk::NativeFileChooser::NativeFileChooser()
fltk::NativeFileChooser::NativeFileChooser(type)

The constructor for the fltk::NativeFileChooser.

If the optional `type` is not specified, BROWSE_FILE (browse to open a file) is assumed. The type can also be set later with type().

#### fltk::NativeFileChooser::~NativeFileChooser()

The destructor; destroys any resources allocated to this widget.

#### int fltk::NativeFileChooser::count() const

Returns the number of filenames (or directory names) the user selected. These are zero-indexed. See this example of how to retrieve multiple filenames.

#### void fltk::NativeFileChooser::directory(const char*)
const char* fltk::NativeFileChooser::directory() const;

Sets the directory with which to start the chooser. If NULL is passed, or if no directory is specified, the chooser will attempt to use the last non-cancelled folder.

#### const char *fltk::NativeFileChooser::errmsg() const

Returns a system dependent error message for the last method that failed. This message should at least be flagged to the user in a dialog box, or to some kind of error log.

#### const char *fltk::NativeFileChooser::filename() const
const char *fltk::NativeFileChooser::filename(int) const

The first form returns the single filename selected by the user from the browser. (In a multiple browser context, this returns the first filename the user selected).

The second form should be used to return multiple filenames, and is normally used inside a loop to retrieve all the files the user selected, eg:

            if ( chooser->show() == 0 ) {
                // HANDLE MULTIPLE FILENAMES
                for (int n = 0; n < chooser->count(); n++ ) {
          	        fprintf(stderr, "%d) '%s'\n", n, chooser->filename(n));
                }
    	}

You can preset the directory with directory() method, and the filename using the preset_file() method.

#### const char *fltk::NativeFileChooser::filter() const
void fltk::NativeFileChooser::filter(const char*)

Gets or sets the filename filters used for browsing. The default is NULL, which browses all files.

The filter string can be any of:

- A single wildcard (eg. "*.txt")
- Multiple wildcards (eg. "*.{cxx,h,H})
- A descriptive name followed by a '\t' and a wildcard (eg. "Text Files\t*.txt")
- A list of separate wildcards with a \n between each (eg. "*.{cxx,H}\n*.txt")
- A list of descriptive names and wildcards (eg. "C++ Files\t*.{cxx,H}\nTxt Files\t*.txt")

The format of each filter is a wildcard, or an optional user description followed by '\t' and the wildcard.

On most platforms, each filter is available to the user via a pulldown menu in the file chooser. The 'All Files' option is always available to the user.

#### void fltk::NativeFileChooser::filter_value(int)
int fltk::NativeFileChooser::filter_value()

The first form sets which filter will be initially selected.
The second returns which filter was chosen. This is only valid if the chooser returns success.

The first filter is indexed as 0. If filter_value()==filters(), then "All Files" was chosen. If filter_value() > filters(), then a custom filter was set.

#### int fltk::NativeFileChooser::filters()

Gets how many filters were available, not including "All Files"

#### void fltk::NativeFileChooser::options(int)
int fltk::NativeFileChooser::options()

Sets/gets the platform specific chooser options.

Some platforms have file choosers with specific functions that can be enabled/disabled via this method.

Flags can be ORed together to enable several features. Flags currently supported:

Flag | Description | Win | Mac | Other
`fltk::NativeFileChooser::SAVEAS_CONFIRM` | With a BROWSE_SAVEAS chooser, prompts a confirmation if file exists | Ignored | Used | Ignored
`fltk::NativeFileChooser::NEW_FOLDER` | Shows the 'New Folder' button | Ignored | Used | Used
`fltk::NativeFileChooser::PREVIEW` | Enables the 'Preview' mode by default | Ignored | Ignored | Used

#### void fltk::NativeFileChooser::preset_file(const char*)
const char* fltk::NativeFileChooser::preset_file()

Sets a default filename for the chooser. (Use directory() to set the default directory)
Mainly used to preset the filename for save dialogs, and on most platforms can be used for opening files as well.

#### int fltk::NativeFileChooser::show() const

Opens the current file chooser on the screen. This method will block until the user has chosen something, or cancelled.

Return value:

- 0 - user picked a file, filename() will have the result
- 1 - user hit 'cancel'
- -1 - failed; errmsg() has reason

#### const char *fltk::NativeFileChooser::title() const
void fltk::NativeFileChooser::title(const char*)

Gets or sets the title of the file chooser's window.

The default title varies according to the platform, so you are advised to set the title explicitly.

#### int fltk::NativeFileChooser::type() const
fltk::NativeFileChooser::type(int)

Gets or sets the current type of browser. Possible choices are:

Flag | Description
`fltk::NativeFileChooser::BROWSE_FILE` | Browse for a single file
`fltk::NativeFileChooser::BROWSE_DIRECTORY` | Browse for a single directory
`fltk::NativeFileChooser::BROWSE_MULTI_FILE` | Browse for multiple files
`fltk::NativeFileChooser::BROWSE_MULTI_DIRECTORY` | Browse for multiple directories (implementation varies)
`fltk::NativeFileChooser::BROWSE_SAVE_FILE` | Browse to save a single file
`fltk::NativeFileChooser::BROWSE_SAVE_DIRECTORY` | Browse for a directory, allowing creation

These may be changed in future versions of fltk::NativeFileChooser.

### Typical Usage

    #include <fltk/NativeFileChooser.h>
    :
    fltk::NativeFileChooser *chooser = new fltk::NativeFileChooser();
    chooser->type(fltk::NativeFileChooser::BROWSE_FILE);   // let user browse a single file
    chooser->title("Open a file");                           // optional title
    chooser->preset_file("/var/tmp/somefile.txt");           // optional filename preset
    chooser->filter("Text Files\t*.txt");                    // optional filter
    switch ( chooser->show() ) {
        case -1:    // ERROR
    	fprintf(stderr, "*** ERROR show() failed:%s\n", chooser->errmsg());
    	break;
        case 1:     // CANCEL
    	fprintf(stderr, "*** CANCEL\n");
    	break;
        default:    // USER PICKED A FILE
            fprintf(stderr, "Filename was '%s'\n", chooser->filename());
    	break;
    }

    // EXAMPLE 'SAVEAS' FILE BROWSER
    #include <fltk/NativeFileChooser.h>
    :
    fltk::NativeFileChooser *chooser = new fltk::NativeFileChooser();
    chooser->type(fltk::NativeFileChooser::BROWSE_SAVE_FILE);   // 'saveas' browser
    chooser->title("Save As..");                                  // optional title for chooser window
    chooser->directory("/var/tmp");                               // optional starting directory
    chooser->preset_file("untitled.txt");                         // optional default filename
    chooser->filter("Text Files\t*.txt");                         // optional filter
    switch ( chooser->show() ) {
        case -1:    // ERROR
    	fprintf(stderr, "*** ERROR show() failed:%s\n", chooser->errmsg());
    	break;
        case 1:     // CANCEL
    	fprintf(stderr, "*** CANCEL\n");
    	break;
        default:    // USER PICKED A FILE
            fprintf(stderr, "Filename was '%s'\n", chooser->filename());
    	break;
    }