2 // "$Id: fl_file_dir.cxx 8345 2011-01-31 18:04:09Z manolo $"
4 // File chooser widget for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
29 #include <FL/filename.H>
30 #include <FL/Fl_File_Chooser.H>
31 #include <FL/fl_ask.H>
34 static Fl_File_Chooser
*fc
= (Fl_File_Chooser
*)0;
35 static void (*current_callback
)(const char*) = 0;
36 static const char *current_label
= fl_ok
;
39 // Do a file chooser callback...
40 static void callback(Fl_File_Chooser
*, void*) {
41 if (current_callback
&& fc
->value())
42 (*current_callback
)(fc
->value());
45 /** \addtogroup group_comdlg
49 Set the file chooser callback
50 \note \#include <FL/Fl_File_Chooser.H>
51 \relates Fl_File_Chooser
53 void fl_file_chooser_callback(void (*cb
)(const char*)) {
54 current_callback
= cb
;
59 Set the "OK" button label
60 \note \#include <FL/Fl_File_Chooser.H>
61 \relates Fl_File_Chooser
63 void fl_file_chooser_ok_label(const char *l
) {
64 if (l
) current_label
= l
;
65 else current_label
= fl_ok
;
69 Shows a file chooser dialog and gets a filename.
70 \note \#include <FL/Fl_File_Chooser.H>
71 \image html Fl_File_Chooser.jpg
72 \image latex Fl_File_Chooser.jpg "Fl_File_Chooser" width=12cm
73 \param[in] message text in title bar
74 \param[in] pat filename pattern filter
75 \param[in] fname initial/default filename selection
76 \param[in] relative 0 for absolute path name, relative path name otherwise
77 \return the user selected filename, in absolute or relative format
78 or NULL if user cancels
79 \relates Fl_File_Chooser
81 char * // O - Filename or NULL
82 fl_file_chooser(const char *message
, // I - Message in titlebar
83 const char *pat
, // I - Filename pattern
84 const char *fname
, // I - Initial filename selection
85 int relative
) { // I - 0 for absolute path
86 static char retname
[FL_PATH_MAX
]; // Returned filename
89 if (!fname
|| !*fname
) fname
= ".";
91 fc
= new Fl_File_Chooser(fname
, pat
, Fl_File_Chooser::CREATE
, message
);
92 fc
->callback(callback
, 0);
94 fc
->type(Fl_File_Chooser::CREATE
);
95 // see, if we use the same pattern between calls
96 char same_pattern
= 0;
97 const char *fcf
= fc
->filter();
98 if ( fcf
&& pat
&& strcmp(fcf
, pat
)==0)
100 else if ( (fcf
==0L || *fcf
==0) && (pat
==0L || *pat
==0) )
102 // now set the pattern to the new pattern (even if they are the same)
106 if (!fname
) { // null pointer reuses same filename if pattern didn't change
107 if (!same_pattern
&& fc
->value()) {
108 // if pattern is different, remove name but leave old directory:
109 strlcpy(retname
, fc
->value(), sizeof(retname
));
111 char *p
= strrchr(retname
, '/');
114 // If the filename is "/foo", then the directory will be "/", not
121 // Set the directory...
124 // re-use the previously selected name
126 } else if (!*fname
) { // empty filename reuses directory with empty name
127 const char *fcv
= fc
->value();
129 strlcpy(retname
, fc
->value(), sizeof(retname
));
132 const char *n
= fl_filename_name(retname
);
133 if (n
) *((char*)n
) = 0;
135 fc
->directory(retname
);
141 fc
->ok_label(current_label
);
147 if (fc
->value() && relative
) {
148 fl_filename_relative(retname
, sizeof(retname
), fc
->value());
151 } else if (fc
->value()) return (char *)fc
->value();
155 /** Shows a file chooser dialog and gets a directory.
156 \note \#include <FL/Fl_File_Chooser.H>
157 \param[in] message title bar text
158 \param[in] fname initial/default directory name
159 \param[in] relative 0 for absolute path return, relative otherwise
160 \return the directory path string chosen by the user or NULL if user cancels
161 \relates Fl_File_Chooser
163 char * // O - Directory or NULL
164 fl_dir_chooser(const char *message
, // I - Message for titlebar
165 const char *fname
, // I - Initial directory name
166 int relative
) // I - 0 for absolute
168 static char retname
[FL_PATH_MAX
]; // Returned directory name
171 if (!fname
|| !*fname
) fname
= ".";
173 fc
= new Fl_File_Chooser(fname
, "*", Fl_File_Chooser::CREATE
|
174 Fl_File_Chooser::DIRECTORY
, message
);
175 fc
->callback(callback
, 0);
177 fc
->type(Fl_File_Chooser::CREATE
| Fl_File_Chooser::DIRECTORY
);
179 if (fname
&& *fname
) fc
->value(fname
);
188 if (fc
->value() && relative
) {
189 fl_filename_relative(retname
, sizeof(retname
), fc
->value());
192 } else if (fc
->value()) return (char *)fc
->value();
199 // End of "$Id: fl_file_dir.cxx 8345 2011-01-31 18:04:09Z manolo $".