2010-01-14 02:13:59 +03:00
|
|
|
//
|
|
|
|
// Simple test of the Fl_Native_File_Chooser.
|
|
|
|
//
|
2016-09-13 03:59:04 +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:
|
|
|
|
//
|
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
|
|
|
//
|
|
|
|
#include <stdio.h>
|
2020-07-01 19:03:10 +03:00
|
|
|
#include <string.h> /* strstr() */
|
2010-01-14 02:13:59 +03:00
|
|
|
#include <FL/Fl.H>
|
2020-07-01 19:03:10 +03:00
|
|
|
#include <FL/fl_ask.H> /* fl_beep() */
|
2010-01-14 02:13:59 +03:00
|
|
|
#include <FL/Fl_Window.H>
|
|
|
|
#include <FL/Fl_Button.H>
|
|
|
|
#include <FL/Fl_Input.H>
|
2016-09-05 19:56:10 +03:00
|
|
|
#include <FL/Fl_Multiline_Input.H>
|
2011-01-01 23:27:07 +03:00
|
|
|
#include <FL/Fl_Box.H>
|
2010-01-14 02:13:59 +03:00
|
|
|
#include <FL/Fl_Native_File_Chooser.H>
|
2016-09-05 19:56:10 +03:00
|
|
|
#include <FL/Fl_Help_View.H>
|
2017-10-17 03:28:56 +03:00
|
|
|
#include <FL/Fl_Simple_Terminal.H>
|
|
|
|
|
|
|
|
#define TERMINAL_HEIGHT 120
|
2010-01-14 02:13:59 +03:00
|
|
|
|
|
|
|
// GLOBALS
|
|
|
|
Fl_Input *G_filename = NULL;
|
2016-09-05 19:56:10 +03:00
|
|
|
Fl_Multiline_Input *G_filter = NULL;
|
2017-10-17 03:28:56 +03:00
|
|
|
Fl_Simple_Terminal *G_tty = NULL;
|
2010-01-14 02:13:59 +03:00
|
|
|
|
2013-08-16 03:01:15 +04:00
|
|
|
void PickFile_CB(Fl_Widget*, void*) {
|
2010-01-14 02:13:59 +03:00
|
|
|
// Create native chooser
|
|
|
|
Fl_Native_File_Chooser native;
|
|
|
|
native.title("Pick a file");
|
|
|
|
native.type(Fl_Native_File_Chooser::BROWSE_FILE);
|
2016-09-13 03:59:04 +03:00
|
|
|
native.filter(G_filter->value());
|
2010-01-14 02:13:59 +03:00
|
|
|
native.preset_file(G_filename->value());
|
|
|
|
// Show native chooser
|
|
|
|
switch ( native.show() ) {
|
2020-07-01 19:03:10 +03:00
|
|
|
case -1: G_tty->printf("ERROR: %s\n", native.errmsg()); break; // ERROR
|
|
|
|
case 1: G_tty->printf("*** CANCEL\n"); fl_beep(); break; // CANCEL
|
|
|
|
default: // PICKED FILE
|
2010-01-14 02:13:59 +03:00
|
|
|
if ( native.filename() ) {
|
|
|
|
G_filename->value(native.filename());
|
2020-07-01 19:03:10 +03:00
|
|
|
G_tty->printf("filename='%s'\n", native.filename());
|
2010-01-14 02:13:59 +03:00
|
|
|
} else {
|
2020-07-01 19:03:10 +03:00
|
|
|
G_filename->value("NULL");
|
|
|
|
G_tty->printf("filename='(null)'\n");
|
2010-01-14 02:13:59 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-16 03:01:15 +04:00
|
|
|
void PickDir_CB(Fl_Widget*, void*) {
|
|
|
|
// Create native chooser
|
|
|
|
Fl_Native_File_Chooser native;
|
|
|
|
native.title("Pick a Directory");
|
|
|
|
native.directory(G_filename->value());
|
|
|
|
native.type(Fl_Native_File_Chooser::BROWSE_DIRECTORY);
|
|
|
|
// Show native chooser
|
|
|
|
switch ( native.show() ) {
|
2020-07-01 19:03:10 +03:00
|
|
|
case -1: G_tty->printf("ERROR: %s\n", native.errmsg()); break; // ERROR
|
|
|
|
case 1: G_tty->printf("*** CANCEL\n"); fl_beep(); break; // CANCEL
|
|
|
|
default: // PICKED DIR
|
2013-08-16 03:01:15 +04:00
|
|
|
if ( native.filename() ) {
|
|
|
|
G_filename->value(native.filename());
|
2020-07-01 19:03:10 +03:00
|
|
|
G_tty->printf("dirname='%s'\n", native.filename());
|
2013-08-16 03:01:15 +04:00
|
|
|
} else {
|
2020-07-01 19:03:10 +03:00
|
|
|
G_filename->value("NULL");
|
|
|
|
G_tty->printf("dirname='(null)'\n");
|
2013-08-16 03:01:15 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-14 02:13:59 +03:00
|
|
|
int main(int argc, char **argv) {
|
2010-01-14 21:42:13 +03:00
|
|
|
//// For a nicer looking browser under linux, call Fl_File_Icon::load_system_icons();
|
2010-01-14 02:13:59 +03:00
|
|
|
//// (If you do this, you'll need to link with fltk_images)
|
2010-01-14 21:42:13 +03:00
|
|
|
//// NOTE: If you do not load the system icons, the file chooser will still work, but
|
|
|
|
//// no icons will be shown. However, this means you do not need to link in the
|
|
|
|
//// fltk_images library, potentially reducing the size of your executable.
|
|
|
|
//// Loading the system icons is not required by the OSX or Windows native file choosers.
|
2018-02-09 17:39:42 +03:00
|
|
|
#if !defined(_WIN32) && !defined(__APPLE__)
|
2010-01-14 21:42:13 +03:00
|
|
|
Fl_File_Icon::load_system_icons();
|
2010-01-14 22:57:08 +03:00
|
|
|
#endif
|
2010-01-14 02:13:59 +03:00
|
|
|
|
2011-01-01 23:27:07 +03:00
|
|
|
int argn = 1;
|
|
|
|
#ifdef __APPLE__
|
|
|
|
// OS X may add the process number as the first argument - ignore
|
2020-08-09 05:10:57 +03:00
|
|
|
if (argc>argn && strncmp(argv[argn], "-psn_", 5)==0) ++argn;
|
2011-01-01 23:27:07 +03:00
|
|
|
#endif
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2020-08-09 05:10:57 +03:00
|
|
|
// Parse preset filename (if any)
|
|
|
|
char *filename = 0;
|
|
|
|
if ( argc>argn && argv[argn][0] != '-' ) {
|
|
|
|
filename = argv[argn++];
|
|
|
|
}
|
|
|
|
|
2017-10-17 03:28:56 +03:00
|
|
|
Fl_Window *win = new Fl_Window(640, 400+TERMINAL_HEIGHT, "Native File Chooser Test");
|
2016-09-16 23:29:13 +03:00
|
|
|
win->size_range(win->w(), win->h(), 0, 0);
|
2010-01-14 02:13:59 +03:00
|
|
|
win->begin();
|
|
|
|
{
|
2017-10-17 03:28:56 +03:00
|
|
|
G_tty = new Fl_Simple_Terminal(0,400,win->w(),TERMINAL_HEIGHT);
|
|
|
|
|
2016-09-05 19:56:10 +03:00
|
|
|
int x = 80, y = 10;
|
|
|
|
G_filename = new Fl_Input(x, y, win->w()-80-10, 25, "Filename");
|
2020-08-09 05:10:57 +03:00
|
|
|
G_filename->value(filename ? filename : ".");
|
2010-01-14 02:13:59 +03:00
|
|
|
G_filename->tooltip("Default filename");
|
2016-09-05 19:56:10 +03:00
|
|
|
|
|
|
|
y += G_filename->h() + 10;
|
|
|
|
G_filter = new Fl_Multiline_Input(x, y, G_filename->w(), 100, "Filter");
|
|
|
|
G_filter->value("Text\t*.txt\n"
|
|
|
|
"C Files\t*.{cxx,h,c,cpp}\n"
|
|
|
|
"Tars\t*.{tar,tar.gz}\n"
|
2020-07-01 19:03:10 +03:00
|
|
|
"Apps\t*.app");
|
2016-09-05 19:56:10 +03:00
|
|
|
G_filter->tooltip("Filter to be used for browser.\n"
|
|
|
|
"An empty string may be used.\n");
|
|
|
|
|
2016-09-16 23:29:13 +03:00
|
|
|
y += G_filter->h() + 10;
|
|
|
|
Fl_Help_View *view = new Fl_Help_View(x, y, G_filename->w(), 200);
|
2016-09-05 19:56:10 +03:00
|
|
|
view->box(FL_FLAT_BOX);
|
|
|
|
view->color(win->color());
|
|
|
|
#define TAB "<Tab>"
|
|
|
|
view->textfont(FL_HELVETICA);
|
|
|
|
view->textsize(10);
|
2016-09-05 22:31:13 +03:00
|
|
|
view->value("The Filter can be one or more filter patterns, one per line.\n"
|
2020-07-01 19:03:10 +03:00
|
|
|
"Patterns can be:<ul>\n"
|
|
|
|
" <li>A single wildcard (e.g. <tt>\"*.txt\"</tt>)</li>\n"
|
|
|
|
" <li>Multiple wildcards (e.g. <tt>\"*.{cxx,h,H}\"</tt>)</li>\n"
|
|
|
|
" <li>A descriptive name followed by a " TAB " and a wildcard (e.g. <tt>\"Text Files" TAB "*.txt\"</tt>)</li>\n"
|
|
|
|
"</ul>\n"
|
2016-09-13 03:59:04 +03:00
|
|
|
"In the above \"Filter\" field, you can use <b><font color=#55f face=Courier>Ctrl-I</font></b> to enter " TAB " characters as needed.<br>\n"
|
2020-07-01 19:03:10 +03:00
|
|
|
"Example:<pre>\n"
|
|
|
|
"\n"
|
|
|
|
" Text<font color=#55f><Ctrl-I></font>*.txt\n"
|
|
|
|
" C Files<font color=#55f><Ctrl-I></font>*.{cxx,h,c,cpp}\n"
|
|
|
|
" Tars<font color=#55f><Ctrl-I></font>*.{tar,tar.gz}\n"
|
|
|
|
" Apps<font color=#55f><Ctrl-I></font>*.app\n"
|
|
|
|
"</pre>\n");
|
2016-09-05 19:56:10 +03:00
|
|
|
|
2017-10-17 03:28:56 +03:00
|
|
|
Fl_Button *but = new Fl_Button(win->w()-x-10, win->h()-TERMINAL_HEIGHT-25-10, 80, 25, "Pick File");
|
2013-08-16 03:01:15 +04:00
|
|
|
but->callback(PickFile_CB);
|
2016-09-05 19:56:10 +03:00
|
|
|
|
2017-10-17 03:28:56 +03:00
|
|
|
Fl_Button *butdir = new Fl_Button(but->x()-x-10, win->h()-TERMINAL_HEIGHT-25-10, 80, 25, "Pick Dir");
|
2013-08-16 03:01:15 +04:00
|
|
|
butdir->callback(PickDir_CB);
|
2016-09-05 19:56:10 +03:00
|
|
|
|
2016-09-13 03:59:04 +03:00
|
|
|
win->resizable(G_filter);
|
2010-01-14 02:13:59 +03:00
|
|
|
}
|
|
|
|
win->end();
|
2020-08-09 05:10:57 +03:00
|
|
|
// Pass show() remaining args we haven't already parsed..
|
|
|
|
{
|
|
|
|
char **args = argv+(argn-1);
|
|
|
|
int nargs = argc-(argn-1);
|
|
|
|
win->show(nargs, args);
|
|
|
|
}
|
2010-01-14 02:13:59 +03:00
|
|
|
return(Fl::run());
|
|
|
|
}
|