| 1 |
// ---------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// fileselect.cxx -- file selector front end |
| 4 |
// |
| 5 |
// Copyright (C) 2008-2009 |
| 6 |
// Stelios Bounanos, M0GLD |
| 7 |
// Dave Freese, 2015 |
| 8 |
// |
| 9 |
// This file is part of fldigi. |
| 10 |
// |
| 11 |
// Fldigi is free software: you can redistribute it and/or modify |
| 12 |
// it under the terms of the GNU General Public License as published by |
| 13 |
// the Free Software Foundation, either version 3 of the License, or |
| 14 |
// (at your option) any later version. |
| 15 |
// |
| 16 |
// Fldigi is distributed in the hope that it will be useful, |
| 17 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
// GNU General Public License for more details. |
| 20 |
// |
| 21 |
// You should have received a copy of the GNU General Public License |
| 22 |
// along with fldigi. If not, see <http://www.gnu.org/licenses/>. |
| 23 |
// ---------------------------------------------------------------------------- |
| 24 |
|
| 25 |
#include <string> |
| 26 |
#include <cstdlib> |
| 27 |
#include <libgen.h> |
| 28 |
|
| 29 |
#include <FL/fl_ask.H> |
| 30 |
#include <FL/Fl_Native_File_Chooser.H> |
| 31 |
|
| 32 |
#include "config.h" |
| 33 |
|
| 34 |
#include "fileselect.h" |
| 35 |
#include "debug.h" |
| 36 |
|
| 37 |
/** |
| 38 |
\class Fl_Native_File_Chooser |
| 39 |
|
| 40 |
This class lets an FLTK application easily and consistently access |
| 41 |
the operating system's native file chooser. Some operating systems |
| 42 |
have very complex and specific file choosers that many users want |
| 43 |
access to specifically, instead of FLTK's default file chooser(s). |
| 44 |
|
| 45 |
In cases where there is no native file browser, FLTK's own file browser |
| 46 |
is used instead. |
| 47 |
|
| 48 |
To use this widget correctly, use the following include in your code: |
| 49 |
\code |
| 50 |
#include <FL/Fl_Native_File_Chooser.H> |
| 51 |
\endcode |
| 52 |
Do not include the other Fl_Native_File_Choser_XXX.H files in your code; |
| 53 |
those are platform specific files that will be included automatically |
| 54 |
depending on your build platform. |
| 55 |
|
| 56 |
The following example shows how to pick a single file: |
| 57 |
\code |
| 58 |
// Create and post the local native file chooser |
| 59 |
#include <FL/Fl_Native_File_Chooser.H> |
| 60 |
[..] |
| 61 |
Fl_Native_File_Chooser fnfc; |
| 62 |
fnfc.title("Pick a file"); |
| 63 |
fnfc.type(Fl_Native_File_Chooser::BROWSE_FILE); |
| 64 |
fnfc.filter("Text\t*.txt\n" |
| 65 |
"C Files\t*.{cxx,h,c}"); |
| 66 |
fnfc.directory("/var/tmp"); // default directory to use |
| 67 |
// Show native chooser |
| 68 |
switch ( fnfc.show() ) { |
| 69 |
case -1: printf("ERROR: %s\n", fnfc.errmsg()); break; // ERROR |
| 70 |
case 1: printf("CANCEL\n"); break; // CANCEL |
| 71 |
default: printf("PICKED: %s\n", fnfc.filename()); break; // FILE CHOSEN |
| 72 |
} |
| 73 |
\endcode |
| 74 |
|
| 75 |
<B>Platform Specific Caveats</B> |
| 76 |
|
| 77 |
- Under X windows, it's best if you call Fl_File_Icon::load_system_icons() |
| 78 |
at the start of main(), to enable the nicer looking file browser widgets. |
| 79 |
Use the static public attributes of class Fl_File_Chooser to localize |
| 80 |
the browser. |
| 81 |
- Some operating systems support certain OS specific options; see |
| 82 |
Fl_Native_File_Chooser::options() for a list. |
| 83 |
|
| 84 |
\image html Fl_Native_File_Chooser.png "The Fl_Native_File_Chooser on different platforms." |
| 85 |
\image latex Fl_Native_File_Chooser.png "The Fl_Native_File_Chooser on different platforms" width=14cm |
| 86 |
|
| 87 |
enum Type { |
| 88 |
BROWSE_FILE = 0, ///< browse files (lets user choose one file) |
| 89 |
BROWSE_DIRECTORY, ///< browse directories (lets user choose one directory) |
| 90 |
BROWSE_MULTI_FILE, ///< browse files (lets user choose multiple files) |
| 91 |
BROWSE_MULTI_DIRECTORY, ///< browse directories (lets user choose multiple directories) |
| 92 |
BROWSE_SAVE_FILE, ///< browse to save a file |
| 93 |
BROWSE_SAVE_DIRECTORY ///< browse to save a directory |
| 94 |
}; |
| 95 |
enum Option { |
| 96 |
NO_OPTIONS = 0x0000, ///< no options enabled |
| 97 |
SAVEAS_CONFIRM = 0x0001, ///< Show native 'Save As' overwrite confirm dialog (if supported) |
| 98 |
NEW_FOLDER = 0x0002, ///< Show 'New Folder' icon (if supported) |
| 99 |
PREVIEW = 0x0004 ///< enable preview mode |
| 100 |
}; |
| 101 |
|
| 102 |
IMPORTANT NOTICE: |
| 103 |
|
| 104 |
The filter type must be terminated with a '\n' on OS X or the application crashes with a Bus timeout |
| 105 |
|
| 106 |
*/ |
| 107 |
|
| 108 |
bool trx_inhibit = false; |
| 109 |
|
| 110 |
namespace FSEL { |
| 111 |
|
| 112 |
void create(void) {}; |
| 113 |
void destroy(void) {}; |
| 114 |
|
| 115 |
std::string filename, stitle, sfilter, sdef, sdirectory; |
| 116 |
char dirbuf[FL_PATH_MAX + 1] = ""; |
| 117 |
char msg[400]; |
| 118 |
|
| 119 |
// use this function for testing on garbage OS, aka Windows |
| 120 |
/* |
| 121 |
void pfile (const char *dir, const char *fname, const char *filt) { |
| 122 |
char fn[FL_PATH_MAX+1]; |
| 123 |
#ifdef __WIN32__ |
| 124 |
fl_filename_expand(fn, sizeof(fn) -1, "$USERPROFILE/"); |
| 125 |
#else |
| 126 |
fl_filename_expand(fn, sizeof(fn) -1, "$HOME/"); |
| 127 |
#endif |
| 128 |
strcat(fn, "pfile.txt"); |
| 129 |
FILE *f = fl_fopen(fn, "a"); |
| 130 |
fprintf(f,"\ |
| 131 |
dir: %s\n\ |
| 132 |
file: %s\n\ |
| 133 |
filter: %s\n", dir, fname, filt); |
| 134 |
fclose(f); |
| 135 |
} |
| 136 |
*/ |
| 137 |
|
| 138 |
void dosfname(std::string &s) |
| 139 |
{ |
| 140 |
for (size_t i = 0; i < s.length(); i++) |
| 141 |
if (s[i] == '/') s[i] = '\\'; |
| 142 |
} |
| 143 |
|
| 144 |
const char* select(const char* title, const char* filter, const char* def, int* fsel) |
| 145 |
{ |
| 146 |
if (strlen(dirbuf) == 0) { |
| 147 |
#ifdef __WIN32__ |
| 148 |
fl_filename_expand(dirbuf, sizeof(dirbuf) -1, "$USERPROFILE/"); |
| 149 |
#else |
| 150 |
fl_filename_expand(dirbuf, sizeof(dirbuf) -1, "$HOME/"); |
| 151 |
#endif |
| 152 |
} |
| 153 |
|
| 154 |
size_t p = 0; |
| 155 |
Fl_Native_File_Chooser native; |
| 156 |
|
| 157 |
stitle.clear(); |
| 158 |
sfilter.clear(); |
| 159 |
sdef.clear(); |
| 160 |
sdirectory.clear(); |
| 161 |
|
| 162 |
if (title) stitle.assign(title); |
| 163 |
if (filter) sfilter.assign(filter); |
| 164 |
|
| 165 |
if (def) { |
| 166 |
sdef.assign(def); |
| 167 |
if (!sdef.empty()) { |
| 168 |
p = sdef.length() - 1; |
| 169 |
if ((sdef[p] == '/') || (sdef[p] == '\\')) sdef.append("fname"); |
| 170 |
} |
| 171 |
sdirectory.assign(sdef); |
| 172 |
p = sdirectory.rfind(fl_filename_name(sdef.c_str())); |
| 173 |
sdirectory.erase(p); |
| 174 |
} |
| 175 |
if (sdirectory.empty()) { |
| 176 |
sdirectory.assign(dirbuf); |
| 177 |
} |
| 178 |
if (sdef.empty()) { |
| 179 |
sdef.assign(sdirectory); |
| 180 |
sdef.append("temp"); |
| 181 |
} |
| 182 |
|
| 183 |
if (!sfilter.empty()) { |
| 184 |
if (sfilter[sfilter.length()-1] != '\n') sfilter += '\n'; |
| 185 |
native.filter(sfilter.c_str()); |
| 186 |
} |
| 187 |
native.title(stitle.c_str()); |
| 188 |
#if __WIN32__ |
| 189 |
dosfname(sdef); |
| 190 |
dosfname(sdirectory); |
| 191 |
#endif |
| 192 |
if (!sdef.empty()) native.preset_file(sdef.c_str()); |
| 193 |
if (!sdirectory.empty()) native.directory(sdirectory.c_str()); |
| 194 |
|
| 195 |
native.type(Fl_Native_File_Chooser::BROWSE_FILE); |
| 196 |
native.options(Fl_Native_File_Chooser::PREVIEW); |
| 197 |
|
| 198 |
// pfile(sdirectory.c_str(), sdef.c_str(), sfilter.c_str()); |
| 199 |
|
| 200 |
filename.clear(); |
| 201 |
|
| 202 |
trx_inhibit = true; |
| 203 |
|
| 204 |
switch ( native.show() ) { |
| 205 |
case -1: // ERROR |
| 206 |
LOG_ERROR("ERROR: %s\n", native.errmsg()); // Error fall through |
| 207 |
case 1: // CANCEL |
| 208 |
filename = ""; |
| 209 |
break; |
| 210 |
default: |
| 211 |
if ( native.filename() ) { |
| 212 |
filename = native.filename(); |
| 213 |
} else { |
| 214 |
filename = ""; |
| 215 |
} |
| 216 |
break; |
| 217 |
} |
| 218 |
|
| 219 |
trx_inhibit = false; |
| 220 |
|
| 221 |
if (fsel) |
| 222 |
*fsel = native.filter_value(); |
| 223 |
|
| 224 |
return filename.c_str(); |
| 225 |
} |
| 226 |
|
| 227 |
const char* saveas(const char* title, const char* filter, const char* def, int* fsel) |
| 228 |
{ |
| 229 |
if (strlen(dirbuf) == 0) { |
| 230 |
#ifdef __WIN32__ |
| 231 |
fl_filename_expand(dirbuf, sizeof(dirbuf) -1, "$USERPROFILE/"); |
| 232 |
#else |
| 233 |
fl_filename_expand(dirbuf, sizeof(dirbuf) -1, "$HOME/"); |
| 234 |
#endif |
| 235 |
} |
| 236 |
|
| 237 |
size_t p = 0; |
| 238 |
Fl_Native_File_Chooser native; |
| 239 |
|
| 240 |
stitle.clear(); |
| 241 |
sfilter.clear(); |
| 242 |
sdef.clear(); |
| 243 |
sdirectory.clear(); |
| 244 |
|
| 245 |
if (title) stitle.assign(title); |
| 246 |
if (filter) sfilter.assign(filter); |
| 247 |
|
| 248 |
if (def) { |
| 249 |
sdef.assign(def); |
| 250 |
if (!sdef.empty()) { |
| 251 |
p = sdef.length() - 1; |
| 252 |
if ((sdef[p] == '/') || (sdef[p] == '\\')) sdef.append("fname"); |
| 253 |
} |
| 254 |
sdirectory.assign(sdef); |
| 255 |
p = sdirectory.rfind(fl_filename_name(sdef.c_str())); |
| 256 |
sdirectory.erase(p); |
| 257 |
} |
| 258 |
if (sdirectory.empty()) { |
| 259 |
sdirectory.assign(dirbuf); |
| 260 |
} |
| 261 |
if (sdef.empty()) { |
| 262 |
sdef.assign(sdirectory); |
| 263 |
sdef.append("temp"); |
| 264 |
} |
| 265 |
|
| 266 |
if (!sfilter.empty()) { |
| 267 |
if (sfilter[sfilter.length()-1] != '\n') sfilter += '\n'; |
| 268 |
native.filter(sfilter.c_str()); |
| 269 |
} |
| 270 |
native.title(stitle.c_str()); |
| 271 |
#if __WIN32__ |
| 272 |
dosfname(sdef); |
| 273 |
dosfname(sdirectory); |
| 274 |
#endif |
| 275 |
if (!sdef.empty()) native.preset_file(sdef.c_str()); |
| 276 |
if (!sdirectory.empty()) native.directory(sdirectory.c_str()); |
| 277 |
native.type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE); |
| 278 |
native.options(Fl_Native_File_Chooser::NEW_FOLDER | Fl_Native_File_Chooser::SAVEAS_CONFIRM); |
| 279 |
|
| 280 |
// pfile(sdirectory.c_str(), sdef.c_str(), sfilter.c_str()); |
| 281 |
|
| 282 |
filename.clear(); |
| 283 |
|
| 284 |
trx_inhibit = true; |
| 285 |
|
| 286 |
switch ( native.show() ) { |
| 287 |
case -1: // ERROR |
| 288 |
LOG_ERROR("ERROR: %s\n", native.errmsg()); |
| 289 |
break; |
| 290 |
case 1: // CANCEL |
| 291 |
filename = ""; |
| 292 |
break; |
| 293 |
default: |
| 294 |
if ( native.filename() ) { |
| 295 |
filename = native.filename(); |
| 296 |
} else { |
| 297 |
filename = ""; |
| 298 |
} |
| 299 |
break; |
| 300 |
} |
| 301 |
|
| 302 |
trx_inhibit = false; |
| 303 |
|
| 304 |
if (fsel) |
| 305 |
*fsel = native.filter_value(); |
| 306 |
|
| 307 |
return filename.c_str(); |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
const char* dir_select(const char* title, const char* filter, const char* def) |
| 312 |
{ |
| 313 |
Fl_Native_File_Chooser native; |
| 314 |
|
| 315 |
stitle.clear(); |
| 316 |
sfilter.clear(); |
| 317 |
sdef.clear(); |
| 318 |
if (title) stitle.assign(title); |
| 319 |
if (filter) sfilter.assign(filter); |
| 320 |
if (def) sdef.assign(def); |
| 321 |
if (!sfilter.empty() && sfilter[sfilter.length()-1] != '\n') sfilter += '\n'; |
| 322 |
|
| 323 |
if (!stitle.empty()) native.title(stitle.c_str()); |
| 324 |
native.type(Fl_Native_File_Chooser::BROWSE_DIRECTORY); |
| 325 |
if (!sfilter.empty()) native.filter(sfilter.c_str()); |
| 326 |
native.options(Fl_Native_File_Chooser::NEW_FOLDER);//NO_OPTIONS); |
| 327 |
#if __WIN32__ |
| 328 |
dosfname(sdef); |
| 329 |
#endif |
| 330 |
if (!sdef.empty()) { |
| 331 |
native.directory(sdef.c_str()); |
| 332 |
sdirectory = sdef; |
| 333 |
} else |
| 334 |
sdirectory.clear(); |
| 335 |
|
| 336 |
filename.clear(); |
| 337 |
|
| 338 |
trx_inhibit = true; |
| 339 |
|
| 340 |
switch ( native.show() ) { |
| 341 |
case -1: // ERROR |
| 342 |
LOG_ERROR("ERROR: %s\n", native.errmsg()); |
| 343 |
break; |
| 344 |
case 1: // CANCEL |
| 345 |
filename = ""; |
| 346 |
break; |
| 347 |
default: |
| 348 |
if ( native.filename() ) { |
| 349 |
filename = native.filename(); |
| 350 |
} else { |
| 351 |
filename = ""; |
| 352 |
} |
| 353 |
break; |
| 354 |
} |
| 355 |
|
| 356 |
trx_inhibit = false; |
| 357 |
|
| 358 |
return filename.c_str(); |
| 359 |
} |
| 360 |
|
| 361 |
} // FSEL |