You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1697 lines
46KB

  1. //
  2. // "$Id: Fl_File_Chooser2.cxx 8785 2011-06-06 16:11:22Z manolo $"
  3. //
  4. // More Fl_File_Chooser routines.
  5. //
  6. // Copyright 1999-2011 by Michael Sweet.
  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. // fabien: ATTENTION: Only Out Of Source Gen. because cxx/H files are autogenerated by fluid.
  28. /** \defgroup group_comdlg Common Dialogs classes and functions
  29. @{
  30. */
  31. /** \class Fl_File_Chooser
  32. The Fl_File_Chooser widget displays a standard file selection
  33. dialog that supports various selection modes.
  34. \image html Fl_File_Chooser.jpg
  35. \image latex Fl_File_Chooser.jpg "Fl_File_Chooser" width=12cm
  36. The Fl_File_Chooser class also exports several static values
  37. that may be used to localize or customize the appearance of all file chooser
  38. dialogs:
  39. <CENTER><TABLE BORDER="1">
  40. <TR>
  41. <TH>Member</TH>
  42. <TH>Default value</TH>
  43. </TR>
  44. <TR>
  45. <TD>add_favorites_label</TD>
  46. <TD>"Add to Favorites"</TD>
  47. </TR>
  48. <TR>
  49. <TD>all_files_label</TD>
  50. <TD>"All Files (*)"</TD>
  51. </TR>
  52. <TR>
  53. <TD>custom_filter_label</TD>
  54. <TD>"Custom Filter"</TD>
  55. </TR>
  56. <TR>
  57. <TD>existing_file_label</TD>
  58. <TD>"Please choose an existing file!"</TD>
  59. </TR>
  60. <TR>
  61. <TD>favorites_label</TD>
  62. <TD>"Favorites"</TD>
  63. </TR>
  64. <TR>
  65. <TD>filename_label</TD>
  66. <TD>"Filename:"</TD>
  67. </TR>
  68. <TR>
  69. <TD>filesystems_label</TD>
  70. <TD>"My Computer" (WIN32)<BR>
  71. "File Systems" (all others)</TD>
  72. </TR>
  73. <TR>
  74. <TD>hidden_label</TD>
  75. <TD>"Show hidden files:"</TD>
  76. </TR>
  77. <TR>
  78. <TD>manage_favorites_label</TD>
  79. <TD>"Manage Favorites"</TD>
  80. </TR>
  81. <TR>
  82. <TD>new_directory_label</TD>
  83. <TD>"New Directory?"</TD>
  84. </TR>
  85. <TR>
  86. <TD>new_directory_tooltip</TD>
  87. <TD>"Create a new directory."</TD>
  88. </TR>
  89. <TR>
  90. <TD>preview_label</TD>
  91. <TD>"Preview"</TD>
  92. </TR>
  93. <TR>
  94. <TD>save_label</TD>
  95. <TD>"Save"</TD>
  96. </TR>
  97. <TR>
  98. <TD>show_label</TD>
  99. <TD>"Show:"</TD>
  100. </TR>
  101. <TR>
  102. <TD>sort</TD>
  103. <TD>fl_numericsort</TD>
  104. </TR>
  105. </TABLE></CENTER>
  106. The Fl_File_Chooser::sort member specifies the sort function that is
  107. used when loading the contents of a directory and can be customized
  108. at run-time.
  109. The Fl_File_Chooser class also exports the Fl_File_Chooser::newButton
  110. and Fl_File_Chooser::previewButton widgets so that application developers
  111. can control their appearance and use. For more complex customization,
  112. consider copying the FLTK file chooser code and changing it accordingly.
  113. */
  114. /** @} */
  115. /** \fn Fl_File_Chooser::Fl_File_Chooser(const char *pathname, const char *pattern, int type, const char *title)
  116. The constructor creates the Fl_File_Chooser dialog shown.
  117. The pathname argument can be a directory name or a
  118. complete file name (in which case the corresponding file is highlighted
  119. in the list and in the filename input field.)
  120. The pattern argument can be a NULL
  121. string or "*" to list all files, or it can be a
  122. series of descriptions and filter strings separated by tab
  123. characters (\\t). The format of filters is either
  124. "Description text (patterns)" or just "patterns". A file chooser
  125. that provides filters for HTML and image files might look like:
  126. \code
  127. "HTML Files (*.html)\tImage Files (*.{bmp,gif,jpg,png})"
  128. \endcode
  129. The file chooser will automatically add the "All Files (*)"
  130. pattern to the end of the string you pass if you do not provide
  131. one. The first filter in the string is the default filter.
  132. See the FLTK documentation on fl_filename_match()
  133. for the kinds of pattern strings that are supported.
  134. The type argument can be one of the following:
  135. \li \c SINGLE - allows the user to select a single, existing file.
  136. \li \c MULTI - allows the user to select one or more existing files.
  137. \li \c CREATE - allows the user to select a single, existing file or
  138. specify a new filename.
  139. \li \c DIRECTORY - allows the user to select a single, existing directory.
  140. The title argument is used to set the title bar text for the
  141. Fl_File_Chooser window.
  142. */
  143. /** \var Fl_File_Chooser::newButton
  144. The "new directory" button is exported so that application developers
  145. can control the appearance and use.
  146. */
  147. /** \var Fl_File_Chooser::previewButton
  148. The "preview" button is exported so that application developers can
  149. control the appearance and use.
  150. */
  151. /** \fn Fl_File_Chooser::~Fl_File_Chooser()
  152. Destroys the widget and frees all memory used by it.*/
  153. /** \fn void Fl_File_Chooser::color(Fl_Color c)
  154. Sets the background color of the Fl_File_Browser list.*/
  155. /** \fn Fl_Color Fl_File_Chooser::color()
  156. Gets the background color of the Fl_File_Browser list.*/
  157. /** \fn int Fl_File_Chooser::count()
  158. Returns the number of selected files.*/
  159. /** \fn void Fl_File_Chooser::directory(const char *pathname)
  160. Sets the current directory.*/
  161. /** \fn const char *Fl_File_Chooser::directory()
  162. Gets the current directory.*/
  163. /** \fn void Fl_File_Chooser::filter(const char *pattern)
  164. Sets or gets the current filename filter patterns. The filter
  165. patterns use fl_filename_match().
  166. Multiple patterns can be used by separating them with tabs, like
  167. <tt>"*.jpg\t*.png\t*.gif\t*"</tt>. In addition, you can provide
  168. human-readable labels with the patterns inside parenthesis, like
  169. <tt>"JPEG Files (*.jpg)\tPNG Files (*.png)\tGIF Files (*.gif)\tAll Files (*)"
  170. </tt>.
  171. Use filter(NULL) to show all files.
  172. */
  173. /** \fn const char *Fl_File_Chooser::filter()
  174. See void filter(const char *pattern)*/
  175. /** \fn void Fl_File_Chooser::filter_value(int f)
  176. Sets the current filename filter selection.*/
  177. /** \fn int Fl_File_Chooser::filter_value()
  178. Gets the current filename filter selection.*/
  179. /** \fn void Fl_File_Chooser::hide()
  180. Hides the Fl_File_Chooser window.*/
  181. /** \fn void Fl_File_Chooser::iconsize(uchar s)
  182. Sets the size of the icons in the Fl_File_Browser. By
  183. default the icon size is set to 1.5 times the textsize().
  184. */
  185. /** \fn uchar Fl_File_Chooser::iconsize()
  186. Gets the size of the icons in the Fl_File_Browser. By
  187. default the icon size is set to 1.5 times the textsize().
  188. */
  189. /** \fn void Fl_File_Chooser::label(const char *l)
  190. Sets the title bar text for the Fl_File_Chooser.*/
  191. /** \fn const char *Fl_File_Chooser::label()
  192. Gets the title bar text for the Fl_File_Chooser.*/
  193. /** \fn void Fl_File_Chooser::ok_label(const char *l)
  194. Sets the label for the "ok" button in the Fl_File_Chooser.
  195. */
  196. /** \fn const char *Fl_File_Chooser::ok_label()
  197. Gets the label for the "ok" button in the Fl_File_Chooser.
  198. */
  199. /** \fn int Fl_File_Chooser::preview() const
  200. Returns the current state of the preview box. */
  201. /** \fn void Fl_File_Chooser::rescan()
  202. Reloads the current directory in the Fl_File_Browser.*/
  203. /** \fn void Fl_File_Chooser::show()
  204. Shows the Fl_File_Chooser window.*/
  205. /** \fn void Fl_File_Chooser::textcolor(Fl_Color c)
  206. Sets the current Fl_File_Browser text color.*/
  207. /** \fn Fl_Color Fl_File_Chooser::textcolor()
  208. Gets the current Fl_File_Browser text color.*/
  209. /** \fn void Fl_File_Chooser::textfont(Fl_Font f)
  210. Sets the current Fl_File_Browser text font.*/
  211. /** \fn Fl_Font Fl_File_Chooser::textfont()
  212. Gets the current Fl_File_Browser text font.*/
  213. /** \fn void Fl_File_Chooser::textsize(Fl_Fontsize s)
  214. Sets the current Fl_File_Browser text size.*/
  215. /** \fn Fl_Fontsize Fl_File_Chooser::textsize()
  216. Gets the current Fl_File_Browser text size.*/
  217. /** \fn void Fl_File_Chooser::type(int t)
  218. Sets the current type of Fl_File_Chooser.*/
  219. /** \fn int Fl_File_Chooser::type()
  220. Gets the current type of Fl_File_Chooser.*/
  221. /** \fn void Fl_File_Chooser::value(const char *pathname)
  222. Sets the current value of the selected file.
  223. */
  224. /** \fn const char *Fl_File_Chooser::value(int f)
  225. Gets the current value of the selected file(s).
  226. \p f is a \c 1-based index into a list of
  227. file names. The number of selected files is returned by
  228. Fl_File_Chooser::count().
  229. This sample code loops through all selected files:
  230. \code
  231. // Get list of filenames user selected from a MULTI chooser
  232. for ( int t=1; t<=chooser->count(); t++ ) {
  233. const char *filename = chooser->value(t);
  234. ...
  235. }
  236. \endcode
  237. */
  238. /** \fn int Fl_File_Chooser::visible()
  239. Returns 1 if the Fl_File_Chooser window is visible.*/
  240. /** \fn Fl_Widget* Fl_File_Chooser::add_extra(Fl_Widget*)
  241. Adds extra widget at the bottom of Fl_File_Chooser window.
  242. Returns pointer for previous extra widget or NULL if not set previously.
  243. If argument is NULL only remove previous extra widget.
  244. \note Fl_File_Chooser does \b not delete extra widget in destructor!
  245. To prevent memory leakage, don't forget to delete unused extra widgets
  246. */
  247. /** \fn int Fl_File_Chooser::shown()
  248. Returns non-zero if the file chooser main window show() has been called (but not hide()
  249. see Fl_Window::shown()
  250. */
  251. /** \fn void Fl_File_Chooser::callback(void (*cb)(Fl_File_Chooser *, void *), void *d = 0)
  252. Sets the file chooser callback cb and associated data d */
  253. /** \fn void Fl_File_Chooser::user_data(void *d)
  254. Sets the file chooser user data d */
  255. /** \fn void * Fl_File_Chooser::user_data() const
  256. Gets the file chooser user data d */
  257. /** \fn Fl_File_Browser* Fl_File_Chooser::browser()
  258. returns a pointer to the underlying Fl_File_Browser object */
  259. // *** END OF OUT OF SOURCE DOC ***
  260. // Contents:
  261. //
  262. // Fl_File_Chooser::count() - Return the number of selected files.
  263. // Fl_File_Chooser::directory() - Set the directory in the file chooser.
  264. // Fl_File_Chooser::filter() - Set the filter(s) for the chooser.
  265. // Fl_File_Chooser::newdir() - Make a new directory.
  266. // Fl_File_Chooser::value() - Return a selected filename.
  267. // Fl_File_Chooser::rescan() - Rescan the current directory.
  268. // Fl_File_Chooser::favoritesButtonCB() - Handle favorites selections.
  269. // Fl_File_Chooser::fileListCB() - Handle clicks (and double-clicks)
  270. // in the Fl_File_Browser.
  271. // Fl_File_Chooser::fileNameCB() - Handle text entry in the FileBrowser.
  272. // Fl_File_Chooser::showChoiceCB() - Handle show selections.
  273. // compare_dirnames() - Compare two directory names.
  274. // quote_pathname() - Quote a pathname for a menu.
  275. // unquote_pathname() - Unquote a pathname from a menu.
  276. //
  277. // Fl_File_Chooser::add_extra() - add extra widget at the bottom, return pointer
  278. // to previous extra widget or NULL if none,
  279. // If argument is NULL extra widget removed.
  280. // NOTE! file chooser does't delete extra widget in
  281. // destructor! To prevent memory leakage don't forget
  282. // delete unused extra widgets by yourself.
  283. //
  284. //
  285. // Include necessary headers.
  286. //
  287. #include <FL/Fl_File_Chooser.H>
  288. #include <FL/filename.H>
  289. #include <FL/fl_ask.H>
  290. #include <FL/x.H>
  291. #include <FL/Fl_Shared_Image.H>
  292. #include <FL/fl_draw.H>
  293. #include <stdio.h>
  294. #include <stdlib.h>
  295. #include "flstring.h"
  296. #include <errno.h>
  297. #include <sys/types.h>
  298. #include <sys/stat.h>
  299. #if defined(WIN32) && ! defined (__CYGWIN__)
  300. # include <direct.h>
  301. # include <io.h>
  302. // Visual C++ 2005 incorrectly displays a warning about the use of POSIX APIs
  303. // on Windows, which is supposed to be POSIX compliant...
  304. # define access _access
  305. # define mkdir _mkdir
  306. // Apparently Borland C++ defines DIRECTORY in <direct.h>, which
  307. // interfers with the Fl_File_Icon enumeration of the same name.
  308. # ifdef DIRECTORY
  309. # undef DIRECTORY
  310. # endif // DIRECTORY
  311. #else
  312. # include <unistd.h>
  313. # include <pwd.h>
  314. #endif /* WIN32 */
  315. //
  316. // File chooser label strings and sort function...
  317. //
  318. Fl_Preferences Fl_File_Chooser::prefs_(Fl_Preferences::USER, "fltk.org", "filechooser");
  319. const char *Fl_File_Chooser::add_favorites_label = "Add to Favorites";
  320. const char *Fl_File_Chooser::all_files_label = "All Files (*)";
  321. const char *Fl_File_Chooser::custom_filter_label = "Custom Filter";
  322. const char *Fl_File_Chooser::existing_file_label = "Please choose an existing file!";
  323. const char *Fl_File_Chooser::favorites_label = "Favorites";
  324. const char *Fl_File_Chooser::filename_label = "Filename:";
  325. #ifdef WIN32
  326. const char *Fl_File_Chooser::filesystems_label = "My Computer";
  327. #else
  328. const char *Fl_File_Chooser::filesystems_label = "File Systems";
  329. #endif // WIN32
  330. const char *Fl_File_Chooser::manage_favorites_label = "Manage Favorites";
  331. const char *Fl_File_Chooser::new_directory_label = "New Directory?";
  332. const char *Fl_File_Chooser::new_directory_tooltip = "Create a new directory.";
  333. const char *Fl_File_Chooser::preview_label = "Preview";
  334. const char *Fl_File_Chooser::save_label = "Save";
  335. const char *Fl_File_Chooser::show_label = "Show:";
  336. const char *Fl_File_Chooser::hidden_label = "Show hidden files";
  337. Fl_File_Sort_F *Fl_File_Chooser::sort = fl_numericsort;
  338. //
  339. // Local functions...
  340. //
  341. static int compare_dirnames(const char *a, const char *b);
  342. static void quote_pathname(char *, const char *, int);
  343. static void unquote_pathname(char *, const char *, int);
  344. //
  345. // 'Fl_File_Chooser::count()' - Return the number of selected files.
  346. //
  347. int // O - Number of selected files
  348. Fl_File_Chooser::count() {
  349. int i; // Looping var
  350. int fcount; // Number of selected files
  351. const char *filename; // Filename in input field or list
  352. filename = fileName->value();
  353. if (!(type_ & MULTI)) {
  354. // Check to see if the file name input field is blank...
  355. if (!filename || !filename[0]) return 0;
  356. else return 1;
  357. }
  358. for (i = 1, fcount = 0; i <= fileList->size(); i ++)
  359. if (fileList->selected(i)) {
  360. // See if this file is a directory...
  361. // matt: why would we do that? It is perfectly legal to select multiple
  362. // directories in a DIR chooser. They are visually selected and value(i)
  363. // returns all of them as expected
  364. //filename = (char *)fileList->text(i);
  365. //if (filename[strlen(filename) - 1] != '/')
  366. fcount ++;
  367. }
  368. if (fcount) return fcount;
  369. else if (!filename || !filename[0]) return 0;
  370. else return 1;
  371. }
  372. //
  373. // 'Fl_File_Chooser::directory()' - Set the directory in the file chooser.
  374. //
  375. void
  376. Fl_File_Chooser::directory(const char *d)// I - Directory to change to
  377. {
  378. char *dirptr; // Pointer into directory
  379. // printf("Fl_File_Chooser::directory(\"%s\")\n", d == NULL ? "(null)" : d);
  380. // NULL == current directory
  381. if (d == NULL)
  382. d = ".";
  383. #ifdef WIN32
  384. // See if the filename contains backslashes...
  385. char *slash; // Pointer to slashes
  386. char fixpath[FL_PATH_MAX]; // Path with slashes converted
  387. if (strchr(d, '\\')) {
  388. // Convert backslashes to slashes...
  389. strlcpy(fixpath, d, sizeof(fixpath));
  390. for (slash = strchr(fixpath, '\\'); slash; slash = strchr(slash + 1, '\\'))
  391. *slash = '/';
  392. d = fixpath;
  393. }
  394. #endif // WIN32
  395. if (d[0] != '\0')
  396. {
  397. // Make the directory absolute...
  398. #if (defined(WIN32) && ! defined(__CYGWIN__))|| defined(__EMX__)
  399. if (d[0] != '/' && d[0] != '\\' && d[1] != ':')
  400. #else
  401. if (d[0] != '/' && d[0] != '\\')
  402. #endif /* WIN32 || __EMX__ */
  403. fl_filename_absolute(directory_, d);
  404. else
  405. strlcpy(directory_, d, sizeof(directory_));
  406. // Strip any trailing slash...
  407. dirptr = directory_ + strlen(directory_) - 1;
  408. if ((*dirptr == '/' || *dirptr == '\\') && dirptr > directory_)
  409. *dirptr = '\0';
  410. // See if we have a trailing .. or . in the filename...
  411. dirptr = directory_ + strlen(directory_) - 3;
  412. if (dirptr >= directory_ && strcmp(dirptr, "/..") == 0) {
  413. // Yes, we have "..", so strip the trailing path...
  414. *dirptr = '\0';
  415. while (dirptr > directory_) {
  416. if (*dirptr == '/') break;
  417. dirptr --;
  418. }
  419. if (dirptr >= directory_ && *dirptr == '/')
  420. *dirptr = '\0';
  421. } else if ((dirptr + 1) >= directory_ && strcmp(dirptr + 1, "/.") == 0) {
  422. // Strip trailing "."...
  423. dirptr[1] = '\0';
  424. }
  425. }
  426. else
  427. directory_[0] = '\0';
  428. if (shown()) {
  429. // Rescan the directory...
  430. rescan();
  431. }
  432. }
  433. //
  434. // 'Fl_File_Chooser::favoritesButtonCB()' - Handle favorites selections.
  435. //
  436. void
  437. Fl_File_Chooser::favoritesButtonCB()
  438. {
  439. int v; // Current selection
  440. char pathname[FL_PATH_MAX], // Pathname
  441. menuname[FL_PATH_MAX]; // Menu name
  442. v = favoritesButton->value();
  443. if (!v) {
  444. // Add current directory to favorites...
  445. if (getenv("HOME")) v = favoritesButton->size() - 5;
  446. else v = favoritesButton->size() - 4;
  447. sprintf(menuname, "favorite%02d", v);
  448. prefs_.set(menuname, directory_);
  449. prefs_.flush();
  450. quote_pathname(menuname, directory_, sizeof(menuname));
  451. favoritesButton->add(menuname);
  452. if (favoritesButton->size() > 104) {
  453. ((Fl_Menu_Item *)favoritesButton->menu())[0].deactivate();
  454. }
  455. } else if (v == 1) {
  456. // Manage favorites...
  457. favoritesCB(0);
  458. } else if (v == 2) {
  459. // Filesystems/My Computer
  460. directory("");
  461. } else {
  462. unquote_pathname(pathname, favoritesButton->text(v), sizeof(pathname));
  463. directory(pathname);
  464. }
  465. }
  466. //
  467. // 'Fl_File_Chooser::favoritesCB()' - Handle favorites dialog.
  468. //
  469. void
  470. Fl_File_Chooser::favoritesCB(Fl_Widget *w)
  471. // I - Widget
  472. {
  473. int i; // Looping var
  474. char name[32], // Preference name
  475. pathname[1024]; // Directory in list
  476. if (!w) {
  477. // Load the favorites list...
  478. favList->clear();
  479. favList->deselect();
  480. for (i = 0; i < 100; i ++) {
  481. // Get favorite directory 0 to 99...
  482. sprintf(name, "favorite%02d", i);
  483. prefs_.get(name, pathname, "", sizeof(pathname));
  484. // Stop on the first empty favorite...
  485. if (!pathname[0]) break;
  486. // Add the favorite to the list...
  487. favList->add(pathname,
  488. Fl_File_Icon::find(pathname, Fl_File_Icon::DIRECTORY));
  489. }
  490. favUpButton->deactivate();
  491. favDeleteButton->deactivate();
  492. favDownButton->deactivate();
  493. favOkButton->deactivate();
  494. favWindow->hotspot(favList);
  495. favWindow->show();
  496. } else if (w == favList) {
  497. i = favList->value();
  498. if (i) {
  499. if (i > 1) favUpButton->activate();
  500. else favUpButton->deactivate();
  501. favDeleteButton->activate();
  502. if (i < favList->size()) favDownButton->activate();
  503. else favDownButton->deactivate();
  504. } else {
  505. favUpButton->deactivate();
  506. favDeleteButton->deactivate();
  507. favDownButton->deactivate();
  508. }
  509. } else if (w == favUpButton) {
  510. i = favList->value();
  511. favList->insert(i - 1, favList->text(i), favList->data(i));
  512. favList->remove(i + 1);
  513. favList->select(i - 1);
  514. if (i == 2) favUpButton->deactivate();
  515. favDownButton->activate();
  516. favOkButton->activate();
  517. } else if (w == favDeleteButton) {
  518. i = favList->value();
  519. favList->remove(i);
  520. if (i > favList->size()) i --;
  521. favList->select(i);
  522. if (i < favList->size()) favDownButton->activate();
  523. else favDownButton->deactivate();
  524. if (i > 1) favUpButton->activate();
  525. else favUpButton->deactivate();
  526. if (!i) favDeleteButton->deactivate();
  527. favOkButton->activate();
  528. } else if (w == favDownButton) {
  529. i = favList->value();
  530. favList->insert(i + 2, favList->text(i), favList->data(i));
  531. favList->remove(i);
  532. favList->select(i + 1);
  533. if ((i + 1) == favList->size()) favDownButton->deactivate();
  534. favUpButton->activate();
  535. favOkButton->activate();
  536. } else if (w == favOkButton) {
  537. // Copy the new list over...
  538. for (i = 0; i < favList->size(); i ++) {
  539. // Set favorite directory 0 to 99...
  540. sprintf(name, "favorite%02d", i);
  541. prefs_.set(name, favList->text(i + 1));
  542. }
  543. // Clear old entries as necessary...
  544. for (; i < 100; i ++) {
  545. // Clear favorite directory 0 to 99...
  546. sprintf(name, "favorite%02d", i);
  547. prefs_.get(name, pathname, "", sizeof(pathname));
  548. if (pathname[0]) prefs_.set(name, "");
  549. else break;
  550. }
  551. update_favorites();
  552. prefs_.flush();
  553. favWindow->hide();
  554. }
  555. }
  556. //
  557. // 'Fl_File_Chooser::fileListCB()' - Handle clicks (and double-clicks) in the
  558. // Fl_File_Browser.
  559. //
  560. void
  561. Fl_File_Chooser::fileListCB()
  562. {
  563. char *filename, // New filename
  564. pathname[FL_PATH_MAX]; // Full pathname to file
  565. filename = (char *)fileList->text(fileList->value());
  566. if (!filename)
  567. return;
  568. if (!directory_[0]) {
  569. strlcpy(pathname, filename, sizeof(pathname));
  570. } else if (strcmp(directory_, "/") == 0) {
  571. snprintf(pathname, sizeof(pathname), "/%s", filename);
  572. } else {
  573. snprintf(pathname, sizeof(pathname), "%s/%s", directory_, filename);
  574. }
  575. if (Fl::event_clicks()) {
  576. #if (defined(WIN32) && ! defined(__CYGWIN__)) || defined(__EMX__)
  577. if ((strlen(pathname) == 2 && pathname[1] == ':') ||
  578. _fl_filename_isdir_quick(pathname))
  579. #else
  580. if (_fl_filename_isdir_quick(pathname))
  581. #endif /* WIN32 || __EMX__ */
  582. {
  583. // Change directories...
  584. directory(pathname);
  585. // Reset the click count so that a click in the same spot won't
  586. // be treated as a triple-click. We use a value of -1 because
  587. // the next click will increment click count to 0, which is what
  588. // we really want...
  589. Fl::event_clicks(-1);
  590. }
  591. else
  592. {
  593. // Hide the window - picked the file...
  594. window->hide();
  595. if (callback_) (*callback_)(this, data_);
  596. }
  597. }
  598. else
  599. {
  600. // Check if the user clicks on a directory when picking files;
  601. // if so, make sure only that item is selected...
  602. filename = pathname + strlen(pathname) - 1;
  603. if ((type_ & MULTI) && !(type_ & DIRECTORY)) {
  604. if (*filename == '/') {
  605. // Clicked on a directory, deselect everything else...
  606. int i = fileList->value();
  607. fileList->deselect();
  608. fileList->select(i);
  609. } else {
  610. // Clicked on a file - see if there are other directories selected...
  611. int i;
  612. const char *temp;
  613. for (i = 1; i <= fileList->size(); i ++) {
  614. if (i != fileList->value() && fileList->selected(i)) {
  615. temp = fileList->text(i);
  616. temp += strlen(temp) - 1;
  617. if (*temp == '/') break; // Yes, selected directory
  618. }
  619. }
  620. if (i <= fileList->size()) {
  621. i = fileList->value();
  622. fileList->deselect();
  623. fileList->select(i);
  624. }
  625. }
  626. }
  627. // Strip any trailing slash from the directory name...
  628. if (*filename == '/') *filename = '\0';
  629. // puts("Setting fileName from fileListCB...");
  630. fileName->value(pathname);
  631. // Update the preview box...
  632. Fl::remove_timeout((Fl_Timeout_Handler)previewCB, this);
  633. Fl::add_timeout(1.0, (Fl_Timeout_Handler)previewCB, this);
  634. // Do any callback that is registered...
  635. if (callback_) (*callback_)(this, data_);
  636. // Activate the OK button as needed...
  637. if (!_fl_filename_isdir_quick(pathname) || (type_ & DIRECTORY))
  638. okButton->activate();
  639. else
  640. okButton->deactivate();
  641. }
  642. }
  643. //
  644. // 'Fl_File_Chooser::fileNameCB()' - Handle text entry in the FileBrowser.
  645. //
  646. void
  647. Fl_File_Chooser::fileNameCB()
  648. {
  649. char *filename, // New filename
  650. *slash, // Pointer to trailing slash
  651. pathname[FL_PATH_MAX], // Full pathname to file
  652. matchname[FL_PATH_MAX]; // Matching filename
  653. int i, // Looping var
  654. min_match, // Minimum number of matching chars
  655. max_match, // Maximum number of matching chars
  656. num_files, // Number of files in directory
  657. first_line; // First matching line
  658. const char *file; // File from directory
  659. // puts("fileNameCB()");
  660. // printf("Event: %s\n", fl_eventnames[Fl::event()]);
  661. // Get the filename from the text field...
  662. filename = (char *)fileName->value();
  663. if (!filename || !filename[0]) {
  664. okButton->deactivate();
  665. return;
  666. }
  667. // Expand ~ and $ variables as needed...
  668. if (strchr(filename, '~') || strchr(filename, '$')) {
  669. fl_filename_expand(pathname, sizeof(pathname), filename);
  670. filename = pathname;
  671. value(pathname);
  672. }
  673. // Make sure we have an absolute path...
  674. #if (defined(WIN32) && !defined(__CYGWIN__)) || defined(__EMX__)
  675. if (directory_[0] != '\0' && filename[0] != '/' &&
  676. filename[0] != '\\' &&
  677. !(isalpha(filename[0] & 255) && (!filename[1] || filename[1] == ':'))) {
  678. #else
  679. if (directory_[0] != '\0' && filename[0] != '/') {
  680. #endif /* WIN32 || __EMX__ */
  681. fl_filename_absolute(pathname, sizeof(pathname), filename);
  682. value(pathname);
  683. fileName->mark(fileName->position()); // no selection after expansion
  684. } else if (filename != pathname) {
  685. // Finally, make sure that we have a writable copy...
  686. strlcpy(pathname, filename, sizeof(pathname));
  687. }
  688. filename = pathname;
  689. // Now process things according to the key pressed...
  690. if (Fl::event_key() == FL_Enter || Fl::event_key() == FL_KP_Enter) {
  691. // Enter pressed - select or change directory...
  692. #if (defined(WIN32) && ! defined(__CYGWIN__)) || defined(__EMX__)
  693. if ((isalpha(pathname[0] & 255) && pathname[1] == ':' && !pathname[2]) ||
  694. (_fl_filename_isdir_quick(pathname) &&
  695. compare_dirnames(pathname, directory_))) {
  696. #else
  697. if (_fl_filename_isdir_quick(pathname) &&
  698. compare_dirnames(pathname, directory_)) {
  699. #endif /* WIN32 || __EMX__ */
  700. directory(pathname);
  701. } else if ((type_ & CREATE) || access(pathname, 0) == 0) {
  702. if (!_fl_filename_isdir_quick(pathname) || (type_ & DIRECTORY)) {
  703. // Update the preview box...
  704. update_preview();
  705. // Do any callback that is registered...
  706. if (callback_) (*callback_)(this, data_);
  707. // Hide the window to signal things are done...
  708. window->hide();
  709. }
  710. } else {
  711. // File doesn't exist, so beep at and alert the user...
  712. fl_alert("%s",existing_file_label);
  713. }
  714. }
  715. else if (Fl::event_key() != FL_Delete &&
  716. Fl::event_key() != FL_BackSpace) {
  717. // Check to see if the user has entered a directory...
  718. if ((slash = strrchr(pathname, '/')) == NULL)
  719. slash = strrchr(pathname, '\\');
  720. if (!slash) return;
  721. // Yes, change directories if necessary...
  722. *slash++ = '\0';
  723. filename = slash;
  724. #if defined(WIN32) || defined(__EMX__)
  725. if (strcasecmp(pathname, directory_) &&
  726. (pathname[0] || strcasecmp("/", directory_))) {
  727. #else
  728. if (strcmp(pathname, directory_) &&
  729. (pathname[0] || strcasecmp("/", directory_))) {
  730. #endif // WIN32 || __EMX__
  731. int p = fileName->position();
  732. int m = fileName->mark();
  733. directory(pathname);
  734. if (filename[0]) {
  735. char tempname[FL_PATH_MAX];
  736. snprintf(tempname, sizeof(tempname), "%s/%s", directory_, filename);
  737. fileName->value(tempname);
  738. strlcpy(pathname, tempname, sizeof(pathname));
  739. }
  740. fileName->position(p, m);
  741. }
  742. // Other key pressed - do filename completion as possible...
  743. num_files = fileList->size();
  744. min_match = strlen(filename);
  745. max_match = min_match + 1;
  746. first_line = 0;
  747. for (i = 1; i <= num_files && max_match > min_match; i ++) {
  748. file = fileList->text(i);
  749. #if (defined(WIN32) && ! defined(__CYGWIN__)) || defined(__EMX__)
  750. if (strncasecmp(filename, file, min_match) == 0) {
  751. #else
  752. if (strncmp(filename, file, min_match) == 0) {
  753. #endif // WIN32 || __EMX__
  754. // OK, this one matches; check against the previous match
  755. if (!first_line) {
  756. // First match; copy stuff over...
  757. strlcpy(matchname, file, sizeof(matchname));
  758. max_match = strlen(matchname);
  759. // Strip trailing /, if any...
  760. if (matchname[max_match - 1] == '/') {
  761. max_match --;
  762. matchname[max_match] = '\0';
  763. }
  764. // And then make sure that the item is visible
  765. fileList->topline(i);
  766. first_line = i;
  767. } else {
  768. // Succeeding match; compare to find maximum string match...
  769. while (max_match > min_match)
  770. #if (defined(WIN32) && ! defined(__CYGWIN__)) || defined(__EMX__)
  771. if (strncasecmp(file, matchname, max_match) == 0)
  772. #else
  773. if (strncmp(file, matchname, max_match) == 0)
  774. #endif // WIN32 || __EMX__
  775. break;
  776. else
  777. max_match --;
  778. // Truncate the string as needed...
  779. matchname[max_match] = '\0';
  780. }
  781. }
  782. }
  783. // If we have any matches, add them to the input field...
  784. if (first_line > 0 && min_match == max_match &&
  785. max_match == (int)strlen(fileList->text(first_line))) {
  786. // This is the only possible match...
  787. fileList->deselect(0);
  788. fileList->select(first_line);
  789. fileList->redraw();
  790. } else if (max_match > min_match && first_line) {
  791. // Add the matching portion...
  792. fileName->replace(filename - pathname, filename - pathname + min_match,
  793. matchname);
  794. // Highlight it with the cursor at the end of the selection so
  795. // s/he can press the right arrow to accept the selection
  796. // (Tab and End also do this for both cases.)
  797. fileName->position(filename - pathname + max_match,
  798. filename - pathname + min_match);
  799. } else if (max_match == 0) {
  800. fileList->deselect(0);
  801. fileList->redraw();
  802. }
  803. // See if we need to enable the OK button...
  804. if (((type_ & CREATE) || !access(fileName->value(), 0)) &&
  805. (!fl_filename_isdir(fileName->value()) || (type_ & DIRECTORY))) {
  806. okButton->activate();
  807. } else {
  808. okButton->deactivate();
  809. }
  810. } else {
  811. // FL_Delete or FL_BackSpace
  812. fileList->deselect(0);
  813. fileList->redraw();
  814. if (((type_ & CREATE) || !access(fileName->value(), 0)) &&
  815. (!fl_filename_isdir(fileName->value()) || (type_ & DIRECTORY))) {
  816. okButton->activate();
  817. } else {
  818. okButton->deactivate();
  819. }
  820. }
  821. }
  822. //
  823. // 'Fl_File_Chooser::filter()' - Set the filter(s) for the chooser.
  824. //
  825. void
  826. Fl_File_Chooser::filter(const char *p) // I - Pattern(s)
  827. {
  828. char *copyp, // Copy of pattern
  829. *start, // Start of pattern
  830. *end; // End of pattern
  831. int allfiles; // Do we have a "*" pattern?
  832. char temp[FL_PATH_MAX]; // Temporary pattern string
  833. // Make sure we have a pattern...
  834. if (!p || !*p) p = "*";
  835. // Copy the pattern string...
  836. copyp = strdup(p);
  837. // Separate the pattern string as necessary...
  838. showChoice->clear();
  839. for (start = copyp, allfiles = 0; start && *start; start = end) {
  840. end = strchr(start, '\t');
  841. if (end) *end++ = '\0';
  842. if (strcmp(start, "*") == 0) {
  843. showChoice->add(all_files_label);
  844. allfiles = 1;
  845. } else {
  846. quote_pathname(temp, start, sizeof(temp));
  847. showChoice->add(temp);
  848. if (strstr(start, "(*)") != NULL) allfiles = 1;
  849. }
  850. }
  851. free(copyp);
  852. if (!allfiles) showChoice->add(all_files_label);
  853. showChoice->add(custom_filter_label);
  854. showChoice->value(0);
  855. showChoiceCB();
  856. }
  857. //
  858. // 'Fl_File_Chooser::newdir()' - Make a new directory.
  859. //
  860. void
  861. Fl_File_Chooser::newdir()
  862. {
  863. const char *dir; // New directory name
  864. char pathname[FL_PATH_MAX]; // Full path of directory
  865. // Get a directory name from the user
  866. if ((dir = fl_input("%s", NULL, new_directory_label)) == NULL)
  867. return;
  868. // Make it relative to the current directory as needed...
  869. #if (defined(WIN32) && ! defined (__CYGWIN__)) || defined(__EMX__)
  870. if (dir[0] != '/' && dir[0] != '\\' && dir[1] != ':')
  871. #else
  872. if (dir[0] != '/' && dir[0] != '\\')
  873. #endif /* WIN32 || __EMX__ */
  874. snprintf(pathname, sizeof(pathname), "%s/%s", directory_, dir);
  875. else
  876. strlcpy(pathname, dir, sizeof(pathname));
  877. // Create the directory; ignore EEXIST errors...
  878. #if defined(WIN32) && ! defined (__CYGWIN__)
  879. if (mkdir(pathname))
  880. #else
  881. if (mkdir(pathname, 0777))
  882. #endif /* WIN32 */
  883. if (errno != EEXIST)
  884. {
  885. fl_alert("%s", strerror(errno));
  886. return;
  887. }
  888. // Show the new directory...
  889. directory(pathname);
  890. }
  891. /** Enable or disable the preview tile. 1 = enable preview, 0 = disable preview. */
  892. void Fl_File_Chooser::preview(int e)
  893. {
  894. previewButton->value(e);
  895. prefs_.set("preview", e);
  896. prefs_.flush();
  897. Fl_Group *p = previewBox->parent();
  898. if (e) {
  899. int w = p->w() * 2 / 3;
  900. fileList->resize(fileList->x(), fileList->y(),
  901. w, fileList->h());
  902. previewBox->resize(fileList->x()+w, previewBox->y(),
  903. p->w()-w, previewBox->h());
  904. previewBox->show();
  905. update_preview();
  906. } else {
  907. fileList->resize(fileList->x(), fileList->y(),
  908. p->w(), fileList->h());
  909. previewBox->resize(p->x()+p->w(), previewBox->y(),
  910. 0, previewBox->h());
  911. previewBox->hide();
  912. }
  913. p->init_sizes();
  914. fileList->parent()->redraw();
  915. }
  916. //
  917. // 'Fl_File_Chooser::previewCB()' - Timeout handler for the preview box.
  918. //
  919. void
  920. Fl_File_Chooser::previewCB(Fl_File_Chooser *fc) { // I - File chooser
  921. fc->update_preview();
  922. }
  923. //
  924. // 'Fl_File_Chooser::rescan()' - Rescan the current directory.
  925. //
  926. void
  927. Fl_File_Chooser::rescan()
  928. {
  929. char pathname[FL_PATH_MAX]; // New pathname for filename field
  930. // Clear the current filename
  931. strlcpy(pathname, directory_, sizeof(pathname));
  932. if (pathname[0] && pathname[strlen(pathname) - 1] != '/') {
  933. strlcat(pathname, "/", sizeof(pathname));
  934. }
  935. // puts("Setting fileName in rescan()");
  936. fileName->value(pathname);
  937. if (type_ & DIRECTORY)
  938. okButton->activate();
  939. else
  940. okButton->deactivate();
  941. // Build the file list...
  942. fileList->load(directory_, sort);
  943. #ifndef WIN32
  944. if (!showHiddenButton->value()) remove_hidden_files();
  945. #endif
  946. // Update the preview box...
  947. update_preview();
  948. }
  949. //
  950. /**
  951. Rescan the current directory without clearing the filename,
  952. then select the file if it is in the list
  953. */
  954. void Fl_File_Chooser::rescan_keep_filename()
  955. {
  956. // if no filename was set, this is likely a diretory browser
  957. const char *fn = fileName->value();
  958. if (!fn || !*fn || fn[strlen(fn) - 1]=='/') {
  959. rescan();
  960. return;
  961. }
  962. int i;
  963. char pathname[FL_PATH_MAX]; // New pathname for filename field
  964. strlcpy(pathname, fn, sizeof(pathname));
  965. // Build the file list...
  966. fileList->load(directory_, sort);
  967. #ifndef WIN32
  968. if (!showHiddenButton->value()) remove_hidden_files();
  969. #endif
  970. // Update the preview box...
  971. update_preview();
  972. // and select the chosen file
  973. char found = 0;
  974. char *slash = strrchr(pathname, '/');
  975. if (slash)
  976. slash++;
  977. else
  978. slash = pathname;
  979. for (i = 1; i <= fileList->size(); i ++)
  980. #if defined(WIN32) || defined(__EMX__)
  981. if (strcasecmp(fileList->text(i), slash) == 0) {
  982. #else
  983. if (strcmp(fileList->text(i), slash) == 0) {
  984. #endif // WIN32 || __EMX__
  985. fileList->topline(i);
  986. fileList->select(i);
  987. found = 1;
  988. break;
  989. }
  990. // update OK button activity
  991. if (found || type_ & CREATE)
  992. okButton->activate();
  993. else
  994. okButton->deactivate();
  995. }
  996. //
  997. // 'Fl_File_Chooser::showChoiceCB()' - Handle show selections.
  998. //
  999. void
  1000. Fl_File_Chooser::showChoiceCB()
  1001. {
  1002. const char *item, // Selected item
  1003. *patstart; // Start of pattern
  1004. char *patend; // End of pattern
  1005. char temp[FL_PATH_MAX]; // Temporary string for pattern
  1006. item = showChoice->text(showChoice->value());
  1007. if (strcmp(item, custom_filter_label) == 0) {
  1008. if ((item = fl_input("%s", pattern_, custom_filter_label)) != NULL) {
  1009. strlcpy(pattern_, item, sizeof(pattern_));
  1010. quote_pathname(temp, item, sizeof(temp));
  1011. showChoice->add(temp);
  1012. showChoice->value(showChoice->size() - 2);
  1013. }
  1014. } else if ((patstart = strchr(item, '(')) == NULL) {
  1015. strlcpy(pattern_, item, sizeof(pattern_));
  1016. } else {
  1017. strlcpy(pattern_, patstart + 1, sizeof(pattern_));
  1018. if ((patend = strrchr(pattern_, ')')) != NULL) *patend = '\0';
  1019. }
  1020. fileList->filter(pattern_);
  1021. if (shown()) {
  1022. // Rescan the directory...
  1023. rescan_keep_filename();
  1024. }
  1025. }
  1026. //
  1027. // 'Fl_File_Chooser::update_favorites()' - Update the favorites menu.
  1028. //
  1029. void
  1030. Fl_File_Chooser::update_favorites()
  1031. {
  1032. int i; // Looping var
  1033. char pathname[FL_PATH_MAX], // Pathname
  1034. menuname[2048]; // Menu name
  1035. const char *home; // Home directory
  1036. favoritesButton->clear();
  1037. favoritesButton->add("bla");
  1038. favoritesButton->clear();
  1039. favoritesButton->add(add_favorites_label, FL_ALT + 'a', 0);
  1040. favoritesButton->add(manage_favorites_label, FL_ALT + 'm', 0, 0, FL_MENU_DIVIDER);
  1041. favoritesButton->add(filesystems_label, FL_ALT + 'f', 0);
  1042. if ((home = getenv("HOME")) != NULL) {
  1043. quote_pathname(menuname, home, sizeof(menuname));
  1044. favoritesButton->add(menuname, FL_ALT + 'h', 0);
  1045. }
  1046. for (i = 0; i < 100; i ++) {
  1047. sprintf(menuname, "favorite%02d", i);
  1048. prefs_.get(menuname, pathname, "", sizeof(pathname));
  1049. if (!pathname[0]) break;
  1050. quote_pathname(menuname, pathname, sizeof(menuname));
  1051. if (i < 10) favoritesButton->add(menuname, FL_ALT + '0' + i, 0);
  1052. else favoritesButton->add(menuname);
  1053. }
  1054. if (i == 100) ((Fl_Menu_Item *)favoritesButton->menu())[0].deactivate();
  1055. }
  1056. //
  1057. // 'Fl_File_Chooser::update_preview()' - Update the preview box...
  1058. //
  1059. void
  1060. Fl_File_Chooser::update_preview()
  1061. {
  1062. const char *filename; // Current filename
  1063. const char *newlabel = 0; // New label text
  1064. Fl_Shared_Image *image = 0, // New image
  1065. *oldimage; // Old image
  1066. int pbw, pbh; // Width and height of preview box
  1067. int w, h; // Width and height of preview image
  1068. int set = 0; // Set this flag as soon as a decent preview is found
  1069. if (!previewButton->value()) return;
  1070. filename = value();
  1071. if (filename == NULL) {
  1072. // no file name at all, so we have an empty preview
  1073. set = 1;
  1074. } else if (fl_filename_isdir(filename)) {
  1075. // filename is a directory, show a folder icon
  1076. newlabel = "@fileopen";
  1077. set = 1;
  1078. } else {
  1079. struct stat s;
  1080. if (fl_stat(filename, &s)==0) {
  1081. if ((s.st_mode&S_IFMT)!=S_IFREG) {
  1082. // this is no regular file, probably some kind of device
  1083. newlabel = "@-3refresh"; // a cross
  1084. set = 1;
  1085. } else if (s.st_size==0) {
  1086. // this file is emty
  1087. newlabel = "<empty file>";
  1088. set = 1;
  1089. } else {
  1090. // if this file is an image, try to load it
  1091. window->cursor(FL_CURSOR_WAIT);
  1092. Fl::check();
  1093. image = Fl_Shared_Image::get(filename);
  1094. if (image) {
  1095. window->cursor(FL_CURSOR_DEFAULT);
  1096. Fl::check();
  1097. set = 1;
  1098. }
  1099. }
  1100. }
  1101. }
  1102. oldimage = (Fl_Shared_Image *)previewBox->image();
  1103. if (oldimage) oldimage->release();
  1104. previewBox->image(0);
  1105. if (!set) {
  1106. FILE *fp;
  1107. int bytes;
  1108. char *ptr;
  1109. if (filename) fp = fl_fopen(filename, "rb");
  1110. else fp = NULL;
  1111. if (fp != NULL) {
  1112. // Try reading the first 1k of data for a label...
  1113. bytes = fread(preview_text_, 1, sizeof(preview_text_) - 1, fp);
  1114. preview_text_[bytes] = '\0';
  1115. fclose(fp);
  1116. } else {
  1117. // Assume we can't read any data...
  1118. preview_text_[0] = '\0';
  1119. }
  1120. window->cursor(FL_CURSOR_DEFAULT);
  1121. Fl::check();
  1122. // Scan the buffer for printable UTF8 chars...
  1123. for (ptr = preview_text_; *ptr; ptr++) {
  1124. uchar c = uchar(*ptr);
  1125. if ( (c&0x80)==0 ) {
  1126. if (!isprint(c&255) && !isspace(c&255)) break;
  1127. } else if ( (c&0xe0)==0xc0 ) {
  1128. if (ptr[1] && (ptr[1]&0xc0)!=0x80) break;
  1129. ptr++;
  1130. } else if ( (c&0xf0)==0xe0 ) {
  1131. if (ptr[1] && (ptr[1]&0xc0)!=0x80) break;
  1132. ptr++;
  1133. if (ptr[1] && (ptr[1]&0xc0)!=0x80) break;
  1134. ptr++;
  1135. } else if ( (c&0xf8)==0xf0 ) {
  1136. if (ptr[1] && (ptr[1]&0xc0)!=0x80) break;
  1137. ptr++;
  1138. if (ptr[1] && (ptr[1]&0xc0)!=0x80) break;
  1139. ptr++;
  1140. if (ptr[1] && (ptr[1]&0xc0)!=0x80) break;
  1141. ptr++;
  1142. }
  1143. }
  1144. // *ptr && (isprint(*ptr & 255) || isspace(*ptr & 255));
  1145. // ptr ++);
  1146. // Scan the buffer for printable characters in 8 bit
  1147. if (*ptr || ptr == preview_text_) {
  1148. for (ptr = preview_text_;
  1149. *ptr && (isprint(*ptr & 255) || isspace(*ptr & 255));
  1150. ptr ++);
  1151. }
  1152. if (*ptr || ptr == preview_text_) {
  1153. // Non-printable file, just show a big ?...
  1154. previewBox->label(filename ? "?" : 0);
  1155. previewBox->align(FL_ALIGN_CLIP);
  1156. previewBox->labelsize(75);
  1157. previewBox->labelfont(FL_HELVETICA);
  1158. } else {
  1159. // Show the first 1k of text...
  1160. int size = previewBox->h() / 20;
  1161. if (size < 6) size = 6;
  1162. else if (size > FL_NORMAL_SIZE) size = FL_NORMAL_SIZE;
  1163. previewBox->label(preview_text_);
  1164. previewBox->align((Fl_Align)(FL_ALIGN_CLIP | FL_ALIGN_INSIDE |
  1165. FL_ALIGN_LEFT | FL_ALIGN_TOP));
  1166. previewBox->labelsize(size);
  1167. previewBox->labelfont(FL_COURIER);
  1168. }
  1169. } else if (image) {
  1170. pbw = previewBox->w() - 20;
  1171. pbh = previewBox->h() - 20;
  1172. if (image->w() > pbw || image->h() > pbh) {
  1173. w = pbw;
  1174. h = w * image->h() / image->w();
  1175. if (h > pbh) {
  1176. h = pbh;
  1177. w = h * image->w() / image->h();
  1178. }
  1179. oldimage = (Fl_Shared_Image *)image->copy(w, h);
  1180. previewBox->image((Fl_Image *)oldimage);
  1181. image->release();
  1182. } else {
  1183. previewBox->image((Fl_Image *)image);
  1184. }
  1185. previewBox->align(FL_ALIGN_CLIP);
  1186. previewBox->label(0);
  1187. } else if (newlabel) {
  1188. previewBox->label(newlabel);
  1189. previewBox->align(FL_ALIGN_CLIP);
  1190. previewBox->labelsize(newlabel[0]=='@'?75:12);
  1191. previewBox->labelfont(FL_HELVETICA);
  1192. }
  1193. previewBox->redraw();
  1194. }
  1195. //
  1196. // 'Fl_File_Chooser::value()' - Return a selected filename.
  1197. //
  1198. const char * // O - Filename or NULL
  1199. Fl_File_Chooser::value(int f) // I - File number
  1200. {
  1201. int i; // Looping var
  1202. int fcount; // Number of selected files
  1203. const char *name; // Current filename
  1204. static char pathname[FL_PATH_MAX]; // Filename + directory
  1205. name = fileName->value();
  1206. if (!(type_ & MULTI)) {
  1207. // Return the filename in the filename field...
  1208. if (!name || !name[0]) return NULL;
  1209. else return name;
  1210. }
  1211. // Return a filename from the list...
  1212. for (i = 1, fcount = 0; i <= fileList->size(); i ++)
  1213. if (fileList->selected(i)) {
  1214. // See if this file is a selected file/directory...
  1215. name = fileList->text(i);
  1216. fcount ++;
  1217. if (fcount == f) {
  1218. if (directory_[0]) {
  1219. snprintf(pathname, sizeof(pathname), "%s/%s", directory_, name);
  1220. } else {
  1221. strlcpy(pathname, name, sizeof(pathname));
  1222. }
  1223. return pathname;
  1224. }
  1225. }
  1226. // If nothing is selected, use the filename field...
  1227. if (!name || !name[0]) return NULL;
  1228. else return name;
  1229. }
  1230. //
  1231. // 'Fl_File_Chooser::value()' - Set the current filename.
  1232. //
  1233. void
  1234. Fl_File_Chooser::value(const char *filename)
  1235. // I - Filename + directory
  1236. {
  1237. int i, // Looping var
  1238. fcount; // Number of items in list
  1239. char *slash; // Directory separator
  1240. char pathname[FL_PATH_MAX]; // Local copy of filename
  1241. // printf("Fl_File_Chooser::value(\"%s\")\n", filename == NULL ? "(null)" : filename);
  1242. // See if the filename is the "My System" directory...
  1243. if (filename == NULL || !filename[0]) {
  1244. // Yes, just change the current directory...
  1245. directory(filename);
  1246. fileName->value("");
  1247. okButton->deactivate();
  1248. return;
  1249. }
  1250. #ifdef WIN32
  1251. // See if the filename contains backslashes...
  1252. char fixpath[FL_PATH_MAX]; // Path with slashes converted
  1253. if (strchr(filename, '\\')) {
  1254. // Convert backslashes to slashes...
  1255. strlcpy(fixpath, filename, sizeof(fixpath));
  1256. for (slash = strchr(fixpath, '\\'); slash; slash = strchr(slash + 1, '\\'))
  1257. *slash = '/';
  1258. filename = fixpath;
  1259. }
  1260. #endif // WIN32
  1261. // See if there is a directory in there...
  1262. fl_filename_absolute(pathname, sizeof(pathname), filename);
  1263. if ((slash = strrchr(pathname, '/')) != NULL) {
  1264. // Yes, change the display to the directory...
  1265. if (!fl_filename_isdir(pathname)) *slash++ = '\0';
  1266. directory(pathname);
  1267. if (*slash == '/') slash = pathname;
  1268. } else {
  1269. directory(".");
  1270. slash = pathname;
  1271. }
  1272. // Set the input field to the absolute path...
  1273. if (slash > pathname) slash[-1] = '/';
  1274. fileName->value(pathname);
  1275. fileName->position(0, strlen(pathname));
  1276. okButton->activate();
  1277. // Then find the file in the file list and select it...
  1278. fcount = fileList->size();
  1279. fileList->deselect(0);
  1280. fileList->redraw();
  1281. for (i = 1; i <= fcount; i ++)
  1282. #if defined(WIN32) || defined(__EMX__)
  1283. if (strcasecmp(fileList->text(i), slash) == 0) {
  1284. #else
  1285. if (strcmp(fileList->text(i), slash) == 0) {
  1286. #endif // WIN32 || __EMX__
  1287. // printf("Selecting line %d...\n", i);
  1288. fileList->topline(i);
  1289. fileList->select(i);
  1290. break;
  1291. }
  1292. }
  1293. void Fl_File_Chooser::show()
  1294. {
  1295. window->hotspot(fileList);
  1296. window->show();
  1297. Fl::flush();
  1298. fl_cursor(FL_CURSOR_WAIT);
  1299. rescan_keep_filename();
  1300. fl_cursor(FL_CURSOR_DEFAULT);
  1301. fileName->take_focus();
  1302. #ifdef WIN32
  1303. showHiddenButton->hide();
  1304. #endif
  1305. }
  1306. void Fl_File_Chooser::showHidden(int value)
  1307. {
  1308. if (value) {
  1309. fileList->load(directory());
  1310. } else {
  1311. remove_hidden_files();
  1312. fileList->redraw();
  1313. }
  1314. }
  1315. void Fl_File_Chooser::remove_hidden_files()
  1316. {
  1317. int count = fileList->size();
  1318. for(int num = count; num >= 1; num--) {
  1319. const char *p = fileList->text(num);
  1320. if (*p == '.' && strcmp(p, "../") != 0) fileList->remove(num);
  1321. }
  1322. fileList->topline(1);
  1323. }
  1324. //
  1325. // 'compare_dirnames()' - Compare two directory names.
  1326. //
  1327. static int
  1328. compare_dirnames(const char *a, const char *b) {
  1329. int alen, blen;
  1330. // Get length of each string...
  1331. alen = strlen(a) - 1;
  1332. blen = strlen(b) - 1;
  1333. if (alen < 0 || blen < 0) return alen - blen;
  1334. // Check for trailing slashes...
  1335. if (a[alen] != '/') alen ++;
  1336. if (b[blen] != '/') blen ++;
  1337. // If the lengths aren't the same, then return the difference...
  1338. if (alen != blen) return alen - blen;
  1339. // Do a comparison of the first N chars (alen == blen at this point)...
  1340. #ifdef WIN32
  1341. return strncasecmp(a, b, alen);
  1342. #else
  1343. return strncmp(a, b, alen);
  1344. #endif // WIN32
  1345. }
  1346. //
  1347. // 'quote_pathname()' - Quote a pathname for a menu.
  1348. //
  1349. static void
  1350. quote_pathname(char *dst, // O - Destination string
  1351. const char *src, // I - Source string
  1352. int dstsize) // I - Size of destination string
  1353. {
  1354. dstsize --;
  1355. while (*src && dstsize > 1) {
  1356. if (*src == '\\') {
  1357. // Convert backslash to forward slash...
  1358. *dst++ = '\\';
  1359. *dst++ = '/';
  1360. src ++;
  1361. } else {
  1362. if (*src == '/') *dst++ = '\\';
  1363. *dst++ = *src++;
  1364. }
  1365. }
  1366. *dst = '\0';
  1367. }
  1368. //
  1369. // 'unquote_pathname()' - Unquote a pathname from a menu.
  1370. //
  1371. static void
  1372. unquote_pathname(char *dst, // O - Destination string
  1373. const char *src, // I - Source string
  1374. int dstsize) // I - Size of destination string
  1375. {
  1376. dstsize --;
  1377. while (*src && dstsize > 1) {
  1378. if (*src == '\\') src ++;
  1379. *dst++ = *src++;
  1380. }
  1381. *dst = '\0';
  1382. }
  1383. //
  1384. // End of "$Id: Fl_File_Chooser2.cxx 8785 2011-06-06 16:11:22Z manolo $".
  1385. //