|
fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation) ![]() |
00001 // 00002 // "$Id: fl_file_dir.cxx 8063 2010-12-19 21:20:10Z matt $" 00003 // 00004 // File chooser widget for the Fast Light Tool Kit (FLTK). 00005 // 00006 // Copyright 1998-2010 by Bill Spitzak and others. 00007 // 00008 // This library is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU Library General Public 00010 // License as published by the Free Software Foundation; either 00011 // version 2 of the License, or (at your option) any later version. 00012 // 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 // Library General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU Library General Public 00019 // License along with this library; if not, write to the Free Software 00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00021 // USA. 00022 // 00023 // Please report all bugs and problems on the following page: 00024 // 00025 // http://www.fltk.org/str.php 00026 // 00027 00028 #include "flstring.h" 00029 #include <FL/filename.H> 00030 #include <FL/Fl_File_Chooser.H> 00031 #include <FL/fl_ask.H> 00032 00033 00034 static Fl_File_Chooser *fc = (Fl_File_Chooser *)0; 00035 static void (*current_callback)(const char*) = 0; 00036 static const char *current_label = fl_ok; 00037 00038 00039 // Do a file chooser callback... 00040 static void callback(Fl_File_Chooser *, void*) { 00041 if (current_callback && fc->value()) 00042 (*current_callback)(fc->value()); 00043 } 00044 00052 void fl_file_chooser_callback(void (*cb)(const char*)) { 00053 current_callback = cb; 00054 } 00055 00056 00061 void fl_file_chooser_ok_label(const char *l) { 00062 if (l) current_label = l; 00063 else current_label = fl_ok; 00064 } 00065 00078 char * // O - Filename or NULL 00079 fl_file_chooser(const char *message, // I - Message in titlebar 00080 const char *pat, // I - Filename pattern 00081 const char *fname, // I - Initial filename selection 00082 int relative) { // I - 0 for absolute path 00083 static char retname[FL_PATH_MAX]; // Returned filename 00084 00085 if (!fc) { 00086 if (!fname || !*fname) fname = "."; 00087 00088 fc = new Fl_File_Chooser(fname, pat, Fl_File_Chooser::CREATE, message); 00089 fc->callback(callback, 0); 00090 } else { 00091 fc->type(Fl_File_Chooser::CREATE); 00092 // see, if we use the same pattern between calls 00093 char same_pattern = 0; 00094 const char *fcf = fc->filter(); 00095 if ( fcf && pat && strcmp(fcf, pat)==0) 00096 same_pattern = 1; 00097 else if ( (fcf==0L || *fcf==0) && (pat==0L || *pat==0) ) 00098 same_pattern = 1; 00099 // now set the pattern to the new pattern (even if they are the same) 00100 fc->filter(pat); 00101 fc->label(message); 00102 00103 if (!fname) { // null pointer reuses same filename if pattern didn't change 00104 if (!same_pattern && fc->value()) { 00105 // if pattern is different, remove name but leave old directory: 00106 strlcpy(retname, fc->value(), sizeof(retname)); 00107 00108 char *p = strrchr(retname, '/'); 00109 00110 if (p) { 00111 // If the filename is "/foo", then the directory will be "/", not 00112 // ""... 00113 if (p == retname) 00114 retname[1] = '\0'; 00115 else 00116 *p = '\0'; 00117 } 00118 // Set the directory... 00119 fc->value(retname); 00120 } else { 00121 // re-use the previously selected name 00122 } 00123 } else if (!*fname) { // empty filename reuses directory with empty name 00124 const char *fcv = fc->value(); 00125 if (fcv) 00126 strlcpy(retname, fc->value(), sizeof(retname)); 00127 else 00128 *retname = 0; 00129 const char *n = fl_filename_name(retname); 00130 if (n) *((char*)n) = 0; 00131 fc->value(""); 00132 fc->directory(retname); 00133 } else { 00134 fc->value(fname); 00135 } 00136 } 00137 00138 fc->ok_label(current_label); 00139 fc->show(); 00140 00141 while (fc->shown()) 00142 Fl::wait(); 00143 00144 if (fc->value() && relative) { 00145 fl_filename_relative(retname, sizeof(retname), fc->value()); 00146 00147 return retname; 00148 } else if (fc->value()) return (char *)fc->value(); 00149 else return 0; 00150 } 00151 00161 char * // O - Directory or NULL 00162 fl_dir_chooser(const char *message, // I - Message for titlebar 00163 const char *fname, // I - Initial directory name 00164 int relative) // I - 0 for absolute 00165 { 00166 static char retname[FL_PATH_MAX]; // Returned directory name 00167 00168 if (!fc) { 00169 if (!fname || !*fname) fname = "."; 00170 00171 fc = new Fl_File_Chooser(fname, "*", Fl_File_Chooser::CREATE | 00172 Fl_File_Chooser::DIRECTORY, message); 00173 fc->callback(callback, 0); 00174 } else { 00175 fc->type(Fl_File_Chooser::CREATE | Fl_File_Chooser::DIRECTORY); 00176 fc->filter("*"); 00177 if (fname && *fname) fc->value(fname); 00178 fc->label(message); 00179 } 00180 00181 fc->show(); 00182 00183 while (fc->shown()) 00184 Fl::wait(); 00185 00186 if (fc->value() && relative) { 00187 fl_filename_relative(retname, sizeof(retname), fc->value()); 00188 00189 return retname; 00190 } else if (fc->value()) return (char *)fc->value(); 00191 else return 0; 00192 } 00196 // 00197 // End of "$Id: fl_file_dir.cxx 8063 2010-12-19 21:20:10Z matt $". 00198 //