themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / fl_file_dir.cxx
bloba96070a12f3388dc3ec5bbb7bb6048b90b3e51c8
1 //
2 // "$Id: fl_file_dir.cxx 8345 2011-01-31 18:04:09Z manolo $"
3 //
4 // File chooser widget for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
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
21 // USA.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 #include "flstring.h"
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
46 @{ */
48 /**
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;
58 /**
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;
68 /**
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
88 if (!fc) {
89 if (!fname || !*fname) fname = ".";
91 fc = new Fl_File_Chooser(fname, pat, Fl_File_Chooser::CREATE, message);
92 fc->callback(callback, 0);
93 } else {
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)
99 same_pattern = 1;
100 else if ( (fcf==0L || *fcf==0) && (pat==0L || *pat==0) )
101 same_pattern = 1;
102 // now set the pattern to the new pattern (even if they are the same)
103 fc->filter(pat);
104 fc->label(message);
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, '/');
113 if (p) {
114 // If the filename is "/foo", then the directory will be "/", not
115 // ""...
116 if (p == retname)
117 retname[1] = '\0';
118 else
119 *p = '\0';
121 // Set the directory...
122 fc->value(retname);
123 } else {
124 // re-use the previously selected name
126 } else if (!*fname) { // empty filename reuses directory with empty name
127 const char *fcv = fc->value();
128 if (fcv)
129 strlcpy(retname, fc->value(), sizeof(retname));
130 else
131 *retname = 0;
132 const char *n = fl_filename_name(retname);
133 if (n) *((char*)n) = 0;
134 fc->value("");
135 fc->directory(retname);
136 } else {
137 fc->value(fname);
141 fc->ok_label(current_label);
142 fc->show();
144 while (fc->shown())
145 Fl::wait();
147 if (fc->value() && relative) {
148 fl_filename_relative(retname, sizeof(retname), fc->value());
150 return retname;
151 } else if (fc->value()) return (char *)fc->value();
152 else return 0;
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
170 if (!fc) {
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);
176 } else {
177 fc->type(Fl_File_Chooser::CREATE | Fl_File_Chooser::DIRECTORY);
178 fc->filter("*");
179 if (fname && *fname) fc->value(fname);
180 fc->label(message);
183 fc->show();
185 while (fc->shown())
186 Fl::wait();
188 if (fc->value() && relative) {
189 fl_filename_relative(retname, sizeof(retname), fc->value());
191 return retname;
192 } else if (fc->value()) return (char *)fc->value();
193 else return 0;
195 /** @} */
199 // End of "$Id: fl_file_dir.cxx 8345 2011-01-31 18:04:09Z manolo $".