1 | // |
---|
2 | // "$Id$" |
---|
3 | // |
---|
4 | // File chooser widget for the Fast Light Tool Kit (FLTK). |
---|
5 | // |
---|
6 | // Copyright 1998-2005 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. |
---|
12 | // |
---|
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. |
---|
17 | // |
---|
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. |
---|
22 | // |
---|
23 | // Please report all bugs and problems on the following page: |
---|
24 | // |
---|
25 | // http://www.fltk.org/str.php |
---|
26 | // |
---|
27 | |
---|
28 | #include "flstring.h" |
---|
29 | #include <FL/filename.H> |
---|
30 | #include <FL/Fl_File_Chooser.H> |
---|
31 | #include <FL/fl_ask.H> |
---|
32 | |
---|
33 | |
---|
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; |
---|
37 | |
---|
38 | |
---|
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()); |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | // Set the file chooser callback |
---|
47 | void fl_file_chooser_callback(void (*cb)(const char*)) { |
---|
48 | current_callback = cb; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | // Set the "OK" button label |
---|
53 | void fl_file_chooser_ok_label(const char *l) { |
---|
54 | if (l) current_label = l; |
---|
55 | else current_label = fl_ok; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | // |
---|
60 | // 'fl_file_chooser()' - Show a file chooser dialog and get a filename. |
---|
61 | // |
---|
62 | |
---|
63 | char * // O - Filename or NULL |
---|
64 | fl_file_chooser(const char *message, // I - Message in titlebar |
---|
65 | const char *pat, // I - Filename pattern |
---|
66 | const char *fname, // I - Initial filename selection |
---|
67 | int relative) { // I - 0 for absolute path |
---|
68 | static char retname[1024]; // Returned filename |
---|
69 | |
---|
70 | if (!fc) { |
---|
71 | if (!fname || !*fname) fname = "."; |
---|
72 | |
---|
73 | fc = new Fl_File_Chooser(fname, pat, Fl_File_Chooser::CREATE, message); |
---|
74 | fc->callback(callback, 0); |
---|
75 | } else { |
---|
76 | fc->type(Fl_File_Chooser::CREATE); |
---|
77 | // see, if we use the same pattern between calls |
---|
78 | char same_pattern = 0; |
---|
79 | const char *fcf = fc->filter(); |
---|
80 | if ( fcf && pat && strcmp(fcf, pat)==0) |
---|
81 | same_pattern = 1; |
---|
82 | else if ( (fcf==0L || *fcf==0) && (pat==0L || *pat==0) ) |
---|
83 | same_pattern = 1; |
---|
84 | // now set the pattern to the new pattern (even if they are the same) |
---|
85 | fc->filter(pat); |
---|
86 | fc->label(message); |
---|
87 | |
---|
88 | if (!fname) { // null pointer reuses same filename if pattern didn't change |
---|
89 | if (!same_pattern && fc->value()) { |
---|
90 | // if pattern is different, remove name but leave old directory: |
---|
91 | strlcpy(retname, fc->value(), sizeof(retname)); |
---|
92 | |
---|
93 | char *p = strrchr(retname, '/'); |
---|
94 | |
---|
95 | if (p) { |
---|
96 | // If the filename is "/foo", then the directory will be "/", not |
---|
97 | // ""... |
---|
98 | if (p == retname) |
---|
99 | retname[1] = '\0'; |
---|
100 | else |
---|
101 | *p = '\0'; |
---|
102 | } |
---|
103 | // Set the directory... |
---|
104 | fc->value(retname); |
---|
105 | } else { |
---|
106 | // re-use the previously selected name |
---|
107 | } |
---|
108 | } else if (!*fname) { // empty filename reuses directory with empty name |
---|
109 | const char *fcv = fc->value(); |
---|
110 | if (fcv) |
---|
111 | strlcpy(retname, fc->value(), sizeof(retname)); |
---|
112 | else |
---|
113 | *retname = 0; |
---|
114 | const char *n = fl_filename_name(retname); |
---|
115 | if (n) *((char*)n) = 0; |
---|
116 | fc->value(""); |
---|
117 | fc->directory(retname); |
---|
118 | } else { |
---|
119 | fc->value(fname); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | fc->ok_label(current_label); |
---|
124 | fc->show(); |
---|
125 | |
---|
126 | while (fc->shown()) |
---|
127 | Fl::wait(); |
---|
128 | |
---|
129 | if (fc->value() && relative) { |
---|
130 | fl_filename_relative(retname, sizeof(retname), fc->value()); |
---|
131 | |
---|
132 | return retname; |
---|
133 | } else if (fc->value()) return (char *)fc->value(); |
---|
134 | else return 0; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | // |
---|
139 | // 'fl_dir_chooser()' - Show a file chooser dialog and get a directory. |
---|
140 | // |
---|
141 | |
---|
142 | char * // O - Directory or NULL |
---|
143 | fl_dir_chooser(const char *message, // I - Message for titlebar |
---|
144 | const char *fname, // I - Initial directory name |
---|
145 | int relative) // I - 0 for absolute |
---|
146 | { |
---|
147 | static char retname[1024]; // Returned directory name |
---|
148 | |
---|
149 | if (!fc) { |
---|
150 | if (!fname || !*fname) fname = "."; |
---|
151 | |
---|
152 | fc = new Fl_File_Chooser(fname, "*", Fl_File_Chooser::CREATE | |
---|
153 | Fl_File_Chooser::DIRECTORY, message); |
---|
154 | fc->callback(callback, 0); |
---|
155 | } else { |
---|
156 | fc->type(Fl_File_Chooser::CREATE | Fl_File_Chooser::DIRECTORY); |
---|
157 | fc->filter("*"); |
---|
158 | if (fname && *fname) fc->value(fname); |
---|
159 | fc->label(message); |
---|
160 | } |
---|
161 | |
---|
162 | fc->show(); |
---|
163 | |
---|
164 | while (fc->shown()) |
---|
165 | Fl::wait(); |
---|
166 | |
---|
167 | if (fc->value() && relative) { |
---|
168 | fl_filename_relative(retname, sizeof(retname), fc->value()); |
---|
169 | |
---|
170 | return retname; |
---|
171 | } else if (fc->value()) return (char *)fc->value(); |
---|
172 | else return 0; |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | // |
---|
177 | // End of "$Id$". |
---|
178 | // |
---|