FLTK 1.3.9
Loading...
Searching...
No Matches
Fl_Native_File_Chooser_FLTK.cxx
1// "$Id$"
2//
3// FLTK native file chooser widget wrapper for GTK's GtkFileChooserDialog
4//
5// Copyright 1998-2014 by Bill Spitzak and others.
6// Copyright 2012 IMM
7//
8// This library is free software. Distribution and use rights are outlined in
9// the file "COPYING" which should have been included with this file. If this
10// file is missing or damaged, see the license at:
11//
12// http://www.fltk.org/COPYING.php
13//
14// Please report all bugs and problems to:
15//
16// http://www.fltk.org/str.php
17//
18
19#include <config.h>
21#include <FL/Fl_File_Icon.H>
22#define FLTK_CHOOSER_SINGLE Fl_File_Chooser::SINGLE
23#define FLTK_CHOOSER_DIRECTORY Fl_File_Chooser::DIRECTORY
24#define FLTK_CHOOSER_MULTI Fl_File_Chooser::MULTI
25#define FLTK_CHOOSER_CREATE Fl_File_Chooser::CREATE
26
27#include "Fl_Native_File_Chooser_common.cxx"
28#include "Fl_Native_File_Chooser_GTK.cxx"
29
30#include <sys/stat.h>
31#include <string.h>
32
33int Fl_Native_File_Chooser::have_looked_for_GTK_libs = 0;
34
41#if FLTK_ABI_VERSION <= 10302
42 _btype = val;
43 _options = NO_OPTIONS;
44 _filter = NULL;
45 _filtvalue = 0;
46 _parsedfilt = NULL;
47 _preset_file = NULL;
48 _prevvalue = NULL;
49 _directory = NULL;
50 _errmsg = NULL;
51#endif // FLTK_ABI_VERSION
52 if (have_looked_for_GTK_libs == 0) {
53 // First Time here, try to find the GTK libs if they are installed
54#if HAVE_DLSYM && HAVE_DLFCN_H
56 Fl_GTK_File_Chooser::probe_for_GTK_libs();
57 }
58#endif
59 have_looked_for_GTK_libs = -1;
60 }
61 // if we found all the GTK functions we need, we will use the GtkFileChooserDialog
62 if (Fl_GTK_File_Chooser::did_find_GTK_libs) _gtk_file_chooser = new Fl_GTK_File_Chooser(val);
63 else _x11_file_chooser = new Fl_FLTK_File_Chooser(val);
64}
65
71 delete _x11_file_chooser;
72}
73
77void Fl_Native_File_Chooser::type(int t) { return _x11_file_chooser->type(t); }
78
82int Fl_Native_File_Chooser::type() const { return _x11_file_chooser->type(); }
83
98void Fl_Native_File_Chooser::options(int o) { _x11_file_chooser->options(o); }
99
103int Fl_Native_File_Chooser::options() const { return _x11_file_chooser->options(); }
104
118int Fl_Native_File_Chooser::count() const { return _x11_file_chooser->count(); }
119
126const char *Fl_Native_File_Chooser::filename() const { return _x11_file_chooser->filename(); }
127
142const char *Fl_Native_File_Chooser::filename(int i) const { return _x11_file_chooser->filename(i); }
143
149void Fl_Native_File_Chooser::directory(const char *val) { _x11_file_chooser->directory(val); }
150
154const char *Fl_Native_File_Chooser::directory() const { return _x11_file_chooser->directory(); }
155
161void Fl_Native_File_Chooser::title(const char *t) { _x11_file_chooser->title(t); }
162
167const char* Fl_Native_File_Chooser::title() const { return _x11_file_chooser->title(); }
168
173const char *Fl_Native_File_Chooser::filter() const { return _x11_file_chooser->filter(); }
174
193void Fl_Native_File_Chooser::filter(const char *f) { _x11_file_chooser->filter(f); }
194
198int Fl_Native_File_Chooser::filters() const { return _x11_file_chooser->filters(); }
199
207void Fl_Native_File_Chooser::filter_value(int i) { _x11_file_chooser->filter_value(i); }
208
213int Fl_Native_File_Chooser::filter_value() const { return _x11_file_chooser->filter_value(); }
214
221void Fl_Native_File_Chooser::preset_file(const char* f) { _x11_file_chooser->preset_file(f); }
222
226const char* Fl_Native_File_Chooser::preset_file() const { return _x11_file_chooser->preset_file(); }
227
233const char *Fl_Native_File_Chooser::errmsg() const { return _x11_file_chooser->errmsg(); }
234
242int Fl_Native_File_Chooser::show() { return _x11_file_chooser->show(); }
243
244Fl_FLTK_File_Chooser::Fl_FLTK_File_Chooser(int val) {
245 _btype = 0;
246 _options = 0;
247 _filter = NULL;
248 _filtvalue = 0;
249 _parsedfilt = NULL;
250 _preset_file = NULL;
251 _prevvalue = NULL;
252 _directory = NULL;
253 _errmsg = NULL;
254 _file_chooser= NULL;
255 if (val >= 0) {
256 _file_chooser = new Fl_File_Chooser(NULL, NULL, 0, NULL);
257 type(val); // do this after _file_chooser created
258 }
259 _nfilters = 0;
260}
261
262Fl_FLTK_File_Chooser::~Fl_FLTK_File_Chooser() {
263 delete _file_chooser;
264 _file_chooser = NULL;
265 _filter = strfree(_filter);
266 _parsedfilt = strfree(_parsedfilt);
267 _preset_file = strfree(_preset_file);
268 _prevvalue = strfree(_prevvalue);
269 _directory = strfree(_directory);
270 _errmsg = strfree(_errmsg);
271}
272
273
274// PRIVATE: SET ERROR MESSAGE
275void Fl_FLTK_File_Chooser::errmsg(const char *msg) {
276 _errmsg = strfree(_errmsg);
277 _errmsg = strnew(msg);
278}
279
280// PRIVATE: translate Native types to Fl_File_Chooser types
281int Fl_FLTK_File_Chooser::type_fl_file(int val) {
282 switch (val) {
284 return(Fl_File_Chooser::SINGLE);
286 return(Fl_File_Chooser::SINGLE | Fl_File_Chooser::DIRECTORY);
288 return(Fl_File_Chooser::MULTI);
290 return(Fl_File_Chooser::DIRECTORY | Fl_File_Chooser::MULTI);
292 return(Fl_File_Chooser::SINGLE | Fl_File_Chooser::CREATE);
294 return(Fl_File_Chooser::DIRECTORY | Fl_File_Chooser::MULTI | Fl_File_Chooser::CREATE);
295 default:
296 return(Fl_File_Chooser::SINGLE);
297 }
298}
299
300void Fl_FLTK_File_Chooser::type(int val) {
301 _btype = val;
302 _file_chooser->type(type_fl_file(val));
303}
304
305int Fl_FLTK_File_Chooser::type() const {
306 return(_btype);
307}
308
309void Fl_FLTK_File_Chooser::options(int val) {
310 _options = val;
311}
312
313int Fl_FLTK_File_Chooser::options() const {
314 return(_options);
315}
316
317int Fl_FLTK_File_Chooser::show() {
318
319 // FILTER
320 if ( _parsedfilt ) {
321 _file_chooser->filter(_parsedfilt);
322 }
323
324 // FILTER VALUE
325 // Set this /after/ setting the filter
326 //
327 _file_chooser->filter_value(_filtvalue);
328
329 // DIRECTORY
330 if ( _directory && _directory[0] ) {
331 _file_chooser->directory(_directory);
332 } else {
333 _file_chooser->directory(_prevvalue);
334 }
335
336 // PRESET FILE
337 if ( _preset_file ) {
338 _file_chooser->value(_preset_file);
339 }
340
341 // OPTIONS: PREVIEW
342 _file_chooser->preview( (options() & Fl_Native_File_Chooser::PREVIEW) ? 1 : 0);
343
344 // OPTIONS: NEW FOLDER
345 if ( options() & Fl_Native_File_Chooser::NEW_FOLDER )
346 _file_chooser->type(_file_chooser->type() | Fl_File_Chooser::CREATE); // on
347
348 // SHOW
349 _file_chooser->show();
350
351 // BLOCK WHILE BROWSER SHOWN
352 while ( _file_chooser->shown() ) {
353 Fl::wait();
354 }
355
356 if ( _file_chooser->value() && _file_chooser->value()[0] ) {
357 _prevvalue = strfree(_prevvalue);
358 _prevvalue = strnew(_file_chooser->value());
359 _filtvalue = _file_chooser->filter_value(); // update filter value
360
361 // HANDLE SHOWING 'SaveAs' CONFIRM
363 struct stat buf;
364 if ( stat(_file_chooser->value(), &buf) != -1 ) {
365 if ( buf.st_mode & S_IFREG ) { // Regular file + exists?
366 if ( exist_dialog() == 0 ) {
367 return(1);
368 }
369 }
370 }
371 }
372 }
373
374 if ( _file_chooser->count() ) return(0);
375 else return(1);
376}
377
378const char *Fl_FLTK_File_Chooser::errmsg() const {
379 return(_errmsg ? _errmsg : "No error");
380}
381
382const char* Fl_FLTK_File_Chooser::filename() const {
383 if ( _file_chooser->count() > 0 ) {
384 return(_file_chooser->value());
385 }
386 return("");
387}
388
389const char* Fl_FLTK_File_Chooser::filename(int i) const {
390 if ( i < _file_chooser->count() )
391 return(_file_chooser->value(i+1)); // convert fltk 1 based to our 0 based
392 return("");
393}
394
395void Fl_FLTK_File_Chooser::title(const char *val) {
396 _file_chooser->label(val);
397}
398
399const char *Fl_FLTK_File_Chooser::title() const {
400 return(_file_chooser->label());
401}
402
403void Fl_FLTK_File_Chooser::filter(const char *val) {
404 _filter = strfree(_filter);
405 _filter = strnew(val);
406 parse_filter();
407}
408
409const char *Fl_FLTK_File_Chooser::filter() const {
410 return(_filter);
411}
412
413int Fl_FLTK_File_Chooser::filters() const {
414 return(_nfilters);
415}
416
417void Fl_FLTK_File_Chooser::filter_value(int val) {
418 _filtvalue = val;
419}
420
421int Fl_FLTK_File_Chooser::filter_value() const {
422 return _filtvalue;
423}
424
425int Fl_FLTK_File_Chooser::count() const {
426 return _file_chooser->count();
427}
428
429void Fl_FLTK_File_Chooser::directory(const char *val) {
430 _directory = strfree(_directory);
431 _directory = strnew(val);
432}
433
434const char *Fl_FLTK_File_Chooser::directory() const {
435 return _directory;
436}
437
438// PRIVATE: Convert our filter format to fltk's chooser format
439// FROM TO (FLTK)
440// ------------------------- --------------------------
441// "*.cxx" "*.cxx Files(*.cxx)"
442// "C Files\t*.{cxx,h}" "C Files(*.{cxx,h})"
443// "C Files\t*.{cxx,h}\nText Files\t*.txt" "C Files(*.{cxx,h})\tText Files(*.txt)"
444//
445// Returns a modified version of the filter that the caller is responsible
446// for freeing with strfree().
447//
448void Fl_FLTK_File_Chooser::parse_filter() {
449 _parsedfilt = strfree(_parsedfilt); // clear previous parsed filter (if any)
450 _nfilters = 0;
451 char *in = _filter;
452 if ( !in ) return;
453
454 int has_name = strchr(in, '\t') ? 1 : 0;
455
456 char mode = has_name ? 'n' : 'w'; // parse mode: n=title, w=wildcard
457 char wildcard[1024] = ""; // parsed wildcard
458 char name[1024] = "";
459
460 // Parse filter user specified
461 for ( ; 1; in++ ) {
462 /*** DEBUG
463 printf("WORKING ON '%c': mode=<%c> name=<%s> wildcard=<%s>\n",
464 *in, mode, name, wildcard);
465 ***/
466
467 switch (*in) {
468 // FINISHED PARSING NAME?
469 case '\t':
470 if ( mode != 'n' ) goto regchar;
471 mode = 'w';
472 break;
473 // ESCAPE NEXT CHAR
474 case '\\':
475 ++in;
476 goto regchar;
477 // FINISHED PARSING ONE OF POSSIBLY SEVERAL FILTERS?
478 case '\r':
479 case '\n':
480 case '\0':
481 // APPEND NEW FILTER TO LIST
482 if ( wildcard[0] ) {
483 // OUT: "name(wild)\tname(wild)"
484 char comp[2048];
485 sprintf(comp, "%s%.511s(%.511s)", ((_parsedfilt)?"\t":""),
486 name, wildcard);
487 _parsedfilt = strapp(_parsedfilt, comp);
488 _nfilters++;
489 //DEBUG printf("DEBUG: PARSED FILT NOW <%s>\n", _parsedfilt);
490 }
491 // RESET
492 wildcard[0] = name[0] = '\0';
493 mode = strchr(in, '\t') ? 'n' : 'w';
494 // DONE?
495 if ( *in == '\0' ) return; // done
496 else continue; // not done yet, more filters
497
498 // Parse all other chars
499 default: // handle all non-special chars
500 regchar: // handle regular char
501 switch ( mode ) {
502 case 'n': chrcat(name, *in); continue;
503 case 'w': chrcat(wildcard, *in); continue;
504 }
505 break;
506 }
507 }
508 //NOTREACHED
509}
510
511void Fl_FLTK_File_Chooser::preset_file(const char* val) {
512 _preset_file = strfree(_preset_file);
513 _preset_file = strnew(val);
514}
515
516const char* Fl_FLTK_File_Chooser::preset_file() const {
517 return _preset_file;
518}
519
520int Fl_FLTK_File_Chooser::exist_dialog() {
521 return fl_choice("%s", fl_cancel, fl_ok, NULL, Fl_Native_File_Chooser::file_exists_message);
522}
523
524//
525// End of "$Id$".
526//
Fl_Native_File_Chooser widget.
Definition Fl_Native_File_Chooser.H:259
The Fl_File_Chooser widget displays a standard file selection dialog that supports various selection ...
Definition Fl_File_Chooser.H:48
int shown()
Returns non-zero if the file chooser main window show() has been called (but not hide() see Fl_Window...
Definition Fl_File_Chooser.cxx:390
void show()
Shows the Fl_File_Chooser window.
Definition Fl_File_Chooser2.cxx:1597
int filter_value()
Gets the current filename filter selection.
Definition Fl_File_Chooser.cxx:348
void directory(const char *d)
Sets the current directory.
Definition Fl_File_Chooser2.cxx:458
const char * value(int f=1)
Gets the current value of the selected file(s).
Definition Fl_File_Chooser2.cxx:1477
int count()
Returns the number of selected files.
Definition Fl_File_Chooser2.cxx:421
void filter(const char *p)
Sets or gets the current filename filter patterns.
Definition Fl_File_Chooser2.cxx:1010
void type(int t)
Sets the current type of Fl_File_Chooser.
Definition Fl_File_Chooser.cxx:418
void preview(int e)
Enable or disable the preview tile.
Definition Fl_File_Chooser2.cxx:1097
void label(const char *l)
Sets the title bar text for the Fl_File_Chooser.
Definition Fl_File_Chooser.cxx:369
Definition Fl_Native_File_Chooser.H:302
static const char * file_exists_message
Localizable message.
Definition Fl_Native_File_Chooser.H:129
int filter_value() const
Returns which filter value was last selected by the user.
Definition Fl_Native_File_Chooser_FLTK.cxx:213
const char * filter() const
Returns the filter string last set.
Definition Fl_Native_File_Chooser_FLTK.cxx:173
const char * errmsg() const
Returns a system dependent error message for the last method that failed.
Definition Fl_Native_File_Chooser_FLTK.cxx:233
~Fl_Native_File_Chooser()
Destructor.
Definition Fl_Native_File_Chooser_FLTK.cxx:70
const char * preset_file() const
Get the preset filename.
Definition Fl_Native_File_Chooser_FLTK.cxx:226
const char * filename() const
Return the filename the user chose.
Definition Fl_Native_File_Chooser_FLTK.cxx:126
Fl_Native_File_Chooser(int val=BROWSE_FILE)
The constructor.
Definition Fl_Native_File_Chooser_FLTK.cxx:40
int filters() const
Gets how many filters were available, not including "All Files".
Definition Fl_Native_File_Chooser_FLTK.cxx:198
@ BROWSE_DIRECTORY
browse directories (lets user choose one directory)
Definition Fl_Native_File_Chooser.H:115
@ BROWSE_MULTI_FILE
browse files (lets user choose multiple files)
Definition Fl_Native_File_Chooser.H:116
@ BROWSE_SAVE_FILE
browse to save a file
Definition Fl_Native_File_Chooser.H:118
@ BROWSE_SAVE_DIRECTORY
browse to save a directory
Definition Fl_Native_File_Chooser.H:119
@ BROWSE_MULTI_DIRECTORY
browse directories (lets user choose multiple directories)
Definition Fl_Native_File_Chooser.H:117
@ BROWSE_FILE
browse files (lets user choose one file)
Definition Fl_Native_File_Chooser.H:114
int show()
Post the chooser's dialog.
Definition Fl_Native_File_Chooser_FLTK.cxx:242
@ NEW_FOLDER
Show 'New Folder' icon (if supported)
Definition Fl_Native_File_Chooser.H:124
@ NO_OPTIONS
no options enabled
Definition Fl_Native_File_Chooser.H:122
@ PREVIEW
enable preview mode (if supported)
Definition Fl_Native_File_Chooser.H:125
@ SAVEAS_CONFIRM
Show native 'Save As' overwrite confirm dialog.
Definition Fl_Native_File_Chooser.H:123
const char * title() const
Get the title of the file chooser's dialog window.
Definition Fl_Native_File_Chooser_FLTK.cxx:167
const char * directory() const
Returns the current preset directory() value.
Definition Fl_Native_File_Chooser_FLTK.cxx:154
int options() const
Gets the platform specific Fl_Native_File_Chooser::Option flags.
Definition Fl_Native_File_Chooser_FLTK.cxx:103
int type() const
Gets the current Fl_Native_File_Chooser::Type of browser.
Definition Fl_Native_File_Chooser_FLTK.cxx:82
int count() const
Returns the number of filenames (or directory names) the user selected.
Definition Fl_Native_File_Chooser_FLTK.cxx:118
static int wait()
Waits until "something happens" and then returns.
Definition Fl.cxx:670
@ OPTION_FNFC_USES_GTK
When switched on (default), Fl_Native_File_Chooser runs GTK file dialogs if the GTK library is availa...
Definition Fl.H:231
static bool option(Fl_Option opt)
FLTK library options management.
Definition Fl.cxx:2186
FL_EXPORT const char * fl_ok
string pointer used in common dialogs, you can change it to another language
Definition fl_ask.cxx:279
FL_EXPORT const char * fl_cancel
string pointer used in common dialogs, you can change it to another language
Definition fl_ask.cxx:280