NTK 1.3.0
Fl_Native_File_Chooser_FLTK.cxx
1// "$Id: Fl_Native_File_Chooser_FLTK.cxx 8282 2011-01-16 18:26:51Z manolo $"
2//
3// FLTK native OS file chooser widget
4//
5// Copyright 1998-2010 by Bill Spitzak and others.
6// Copyright 2004 Greg Ercolano.
7// API changes + filter improvements by Nathan Vander Wilt 2005
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Library General Public
11// License as published by the Free Software Foundation; either
12// version 2 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Library General Public License for more details.
18//
19// You should have received a copy of the GNU Library General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22// USA.
23//
24// Please report all bugs and problems to:
25//
26// http://www.fltk.org/str.php
27//
28
30#include <FL/Fl_File_Icon.H>
31#define FLTK_CHOOSER_SINGLE Fl_File_Chooser::SINGLE
32#define FLTK_CHOOSER_DIRECTORY Fl_File_Chooser::DIRECTORY
33#define FLTK_CHOOSER_MULTI Fl_File_Chooser::MULTI
34#define FLTK_CHOOSER_CREATE Fl_File_Chooser::CREATE
35
36#include "Fl_Native_File_Chooser_common.cxx"
37#include <sys/stat.h>
38#include <string.h>
39
55 _btype = val;
56 _options = NO_OPTIONS;
57 _filter = NULL;
58 _filtvalue = 0;
59 _parsedfilt = NULL;
60 _preset_file = NULL;
61 _prevvalue = NULL;
62 _directory = NULL;
63 _errmsg = NULL;
64 _file_chooser = new Fl_File_Chooser(NULL, NULL, 0, NULL);
65 type(val); // do this after _file_chooser created
66 _nfilters = 0;
67}
68
74 delete _file_chooser;
75 _filter = strfree(_filter);
76 _parsedfilt = strfree(_parsedfilt);
77 _preset_file = strfree(_preset_file);
78 _prevvalue = strfree(_prevvalue);
79 _directory = strfree(_directory);
80 _errmsg = strfree(_errmsg);
81}
82
83// PRIVATE: SET ERROR MESSAGE
84void Fl_Native_File_Chooser::errmsg(const char *msg) {
85 _errmsg = strfree(_errmsg);
86 _errmsg = strnew(msg);
87}
88
89// PRIVATE: translate Native types to Fl_File_Chooser types
90int Fl_Native_File_Chooser::type_fl_file(int val) {
91 switch (val) {
92 case BROWSE_FILE:
93 return(FLTK_CHOOSER_SINGLE);
95 return(FLTK_CHOOSER_SINGLE | FLTK_CHOOSER_DIRECTORY);
97 return(FLTK_CHOOSER_MULTI);
99 return(FLTK_CHOOSER_DIRECTORY | FLTK_CHOOSER_MULTI);
100 case BROWSE_SAVE_FILE:
101 return(FLTK_CHOOSER_SINGLE | FLTK_CHOOSER_CREATE);
103 return(FLTK_CHOOSER_DIRECTORY | FLTK_CHOOSER_MULTI | FLTK_CHOOSER_CREATE);
104 default:
105 return(FLTK_CHOOSER_SINGLE);
106 }
107}
108
113 _btype = val;
114 _file_chooser->type(type_fl_file(val));
115}
116
121 return(_btype);
122}
123
138 _options = val;
139}
140
145 return(_options);
146}
147
156 // FILTER
157 if ( _parsedfilt ) {
158 _file_chooser->filter(_parsedfilt);
159 }
160
161 // FILTER VALUE
162 // Set this /after/ setting the filter
163 //
164 _file_chooser->filter_value(_filtvalue);
165
166 // DIRECTORY
167 if ( _directory && _directory[0] ) {
168 _file_chooser->directory(_directory);
169 } else {
170 _file_chooser->directory(_prevvalue);
171 }
172
173 // PRESET FILE
174 if ( _preset_file ) {
175 _file_chooser->value(_preset_file);
176 }
177
178 // OPTIONS: PREVIEW
179 _file_chooser->preview( (options() & PREVIEW) ? 1 : 0);
180
181 // OPTIONS: NEW FOLDER
182 if ( options() & NEW_FOLDER )
183 _file_chooser->type(_file_chooser->type() | FLTK_CHOOSER_CREATE); // on
184
185 // SHOW
186 _file_chooser->show();
187
188 // BLOCK WHILE BROWSER SHOWN
189 while ( _file_chooser->shown() ) {
190 Fl::wait();
191 }
192
193 if ( _file_chooser->value() && _file_chooser->value()[0] ) {
194 _prevvalue = strfree(_prevvalue);
195 _prevvalue = strnew(_file_chooser->value());
196 _filtvalue = _file_chooser->filter_value(); // update filter value
197
198 // HANDLE SHOWING 'SaveAs' CONFIRM
199 if ( options() & SAVEAS_CONFIRM && type() == BROWSE_SAVE_FILE ) {
200 struct stat buf;
201 if ( stat(_file_chooser->value(), &buf) != -1 ) {
202 if ( buf.st_mode & S_IFREG ) { // Regular file + exists?
203 if ( exist_dialog() == 0 ) {
204 return(1);
205 }
206 }
207 }
208 }
209 }
210
211 if ( _file_chooser->count() ) return(0);
212 else return(1);
213}
214
221 return(_errmsg ? _errmsg : "No error");
222}
223
231 if ( _file_chooser->count() > 0 ) return(_file_chooser->value());
232 return("");
233}
234
249const char* Fl_Native_File_Chooser::filename(int i) const {
250 if ( i < _file_chooser->count() )
251 return(_file_chooser->value(i+1)); // convert fltk 1 based to our 0 based
252 return("");
253}
254
260void Fl_Native_File_Chooser::title(const char *val) {
261 _file_chooser->label(val);
262}
263
268const char *Fl_Native_File_Chooser::title() const {
269 return(_file_chooser->label());
270}
271
290void Fl_Native_File_Chooser::filter(const char *val) {
291 _filter = strfree(_filter);
292 _filter = strnew(val);
293 parse_filter();
294}
295
301 return(_filter);
302}
303
308 return(_nfilters);
309}
310
319 _filtvalue = val;
320}
321
327 return(_filtvalue);
328}
329
344 return(_file_chooser->count());
345}
346
353 _directory = strfree(_directory);
354 _directory = strnew(val);
355}
356
361 return(_directory);
362}
363
364// PRIVATE: Convert our filter format to fltk's chooser format
365// FROM TO (FLTK)
366// ------------------------- --------------------------
367// "*.cxx" "*.cxx Files(*.cxx)"
368// "C Files\t*.{cxx,h}" "C Files(*.{cxx,h})"
369// "C Files\t*.{cxx,h}\nText Files\t*.txt" "C Files(*.{cxx,h})\tText Files(*.txt)"
370//
371// Returns a modified version of the filter that the caller is responsible
372// for freeing with strfree().
373//
374void Fl_Native_File_Chooser::parse_filter() {
375 _parsedfilt = strfree(_parsedfilt); // clear previous parsed filter (if any)
376 _nfilters = 0;
377 char *in = _filter;
378 if ( !in ) return;
379
380 int has_name = strchr(in, '\t') ? 1 : 0;
381
382 char mode = has_name ? 'n' : 'w'; // parse mode: n=title, w=wildcard
383 char wildcard[1024] = ""; // parsed wildcard
384 char name[1024] = "";
385
386 // Parse filter user specified
387 for ( ; 1; in++ ) {
388 /*** DEBUG
389 printf("WORKING ON '%c': mode=<%c> name=<%s> wildcard=<%s>\n",
390 *in, mode, name, wildcard);
391 ***/
392
393 switch (*in) {
394 // FINISHED PARSING NAME?
395 case '\t':
396 if ( mode != 'n' ) goto regchar;
397 mode = 'w';
398 break;
399 // ESCAPE NEXT CHAR
400 case '\\':
401 ++in;
402 goto regchar;
403 // FINISHED PARSING ONE OF POSSIBLY SEVERAL FILTERS?
404 case '\r':
405 case '\n':
406 case '\0':
407 // APPEND NEW FILTER TO LIST
408 if ( wildcard[0] ) {
409 // OUT: "name(wild)\tname(wild)"
410 char comp[2048];
411 sprintf(comp, "%s%.511s(%.511s)", ((_parsedfilt)?"\t":""),
412 name, wildcard);
413 _parsedfilt = strapp(_parsedfilt, comp);
414 _nfilters++;
415 //DEBUG printf("DEBUG: PARSED FILT NOW <%s>\n", _parsedfilt);
416 }
417 // RESET
418 wildcard[0] = name[0] = '\0';
419 mode = strchr(in, '\t') ? 'n' : 'w';
420 // DONE?
421 if ( *in == '\0' ) return; // done
422 else continue; // not done yet, more filters
423
424 // Parse all other chars
425 default: // handle all non-special chars
426 regchar: // handle regular char
427 switch ( mode ) {
428 case 'n': chrcat(name, *in); continue;
429 case 'w': chrcat(wildcard, *in); continue;
430 }
431 break;
432 }
433 }
434 //NOTREACHED
435}
436
444 _preset_file = strfree(_preset_file);
445 _preset_file = strnew(val);
446}
447
452 return(_preset_file);
453}
454
455
456int Fl_Native_File_Chooser::exist_dialog() {
457 return(fl_choice("%s", fl_cancel, fl_ok, NULL, file_exists_message));
458}
459
460//
461// End of "$Id: Fl_Native_File_Chooser_FLTK.cxx 8282 2011-01-16 18:26:51Z manolo $".
462//
Fl_Native_File_Chooser widget.
The Fl_File_Chooser widget displays a standard file selection dialog that supports various selection ...
Definition Fl_File_Chooser.H:50
static const char * file_exists_message
Localizable message.
Definition Fl_Native_File_Chooser.H:175
int filter_value() const
Returns which filter value was last selected by the user.
Definition Fl_Native_File_Chooser_FLTK.cxx:326
const char * filter() const
Returns the filter string last set.
Definition Fl_Native_File_Chooser_FLTK.cxx:300
const char * errmsg() const
Returns a system dependent error message for the last method that failed.
Definition Fl_Native_File_Chooser_FLTK.cxx:220
~Fl_Native_File_Chooser()
Destructor.
Definition Fl_Native_File_Chooser_FLTK.cxx:73
const char * preset_file() const
Get the preset filename.
Definition Fl_Native_File_Chooser_FLTK.cxx:451
const char * filename() const
Return the filename the user choose.
Definition Fl_Native_File_Chooser_FLTK.cxx:230
Fl_Native_File_Chooser(int val=BROWSE_FILE)
The constructor.
Definition Fl_Native_File_Chooser_FLTK.cxx:45
int filters() const
Gets how many filters were available, not including "All Files".
Definition Fl_Native_File_Chooser_FLTK.cxx:307
@ BROWSE_DIRECTORY
browse directories (lets user choose one directory)
Definition Fl_Native_File_Chooser.H:162
@ BROWSE_MULTI_FILE
browse files (lets user choose multiple files)
Definition Fl_Native_File_Chooser.H:163
@ BROWSE_SAVE_FILE
browse to save a file
Definition Fl_Native_File_Chooser.H:165
@ BROWSE_SAVE_DIRECTORY
browse to save a directory
Definition Fl_Native_File_Chooser.H:166
@ BROWSE_MULTI_DIRECTORY
browse directories (lets user choose multiple directories)
Definition Fl_Native_File_Chooser.H:164
@ BROWSE_FILE
browse files (lets user choose one file)
Definition Fl_Native_File_Chooser.H:161
int show()
Post the chooser's dialog.
Definition Fl_Native_File_Chooser_FLTK.cxx:155
@ NEW_FOLDER
Show 'New Folder' icon (if supported)
Definition Fl_Native_File_Chooser.H:171
@ NO_OPTIONS
no options enabled
Definition Fl_Native_File_Chooser.H:169
@ PREVIEW
enable preview mode
Definition Fl_Native_File_Chooser.H:172
@ SAVEAS_CONFIRM
Show native 'Save As' overwrite confirm dialog (if supported)
Definition Fl_Native_File_Chooser.H:170
const char * title() const
Get the title of the file chooser's dialog window.
Definition Fl_Native_File_Chooser_FLTK.cxx:268
const char * directory() const
Returns the current preset directory() value.
Definition Fl_Native_File_Chooser_FLTK.cxx:360
int options() const
Gets the platform specific Fl_Native_File_Chooser::Option flags.
Definition Fl_Native_File_Chooser_FLTK.cxx:144
int type() const
Gets the current Fl_Native_File_Chooser::Type of browser.
Definition Fl_Native_File_Chooser_FLTK.cxx:120
int count() const
Returns the number of filenames (or directory names) the user selected.
Definition Fl_Native_File_Chooser_FLTK.cxx:343
static int wait()
Waits until "something happens" and then returns.
Definition Fl.cxx:605
FL_EXPORT const char * fl_ok
string pointer used in common dialogs, you can change it to another language
Definition fl_ask.cxx:269
FL_EXPORT const char * fl_cancel
string pointer used in common dialogs, you can change it to another language
Definition fl_ask.cxx:270