FLTK logo

[master] f0af606 - Save fl_file_chooser directory on cancel (STR 3225) (#110)

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] f0af606 - Save fl_file_chooser directory on cancel (STR 3225) (#110) "engelsman" Nov 02, 2020  
 
commit f0af606708a5ede2bb99880c9ffb68f29a2439c0
Author:     engelsman <engelsman@users.noreply.github.com>
AuthorDate: Mon Nov 2 11:38:30 2020 +0100
Commit:     GitHub <noreply@github.com>
CommitDate: Mon Nov 2 11:38:30 2020 +0100

    Save fl_file_chooser directory on cancel (STR 3225) (#110)
    
    * save fl_file_chooser directory on cancel (STR3225)
    
    by default, the fl_file_chooser() starts in the current directory
    if no filename is given, but the directory is reset to the "system
    directory" if the dialog is cancelled without choosing a file.
    This patch saves and restores the directory if no file is chosen.
    
    See also https://www.fltk.org/str.php?L3225
    
    * add fl_file_chooser() reinitialization comments

 src/fl_file_dir.cxx | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)

diff --git src/fl_file_dir.cxx src/fl_file_dir.cxx
index dc740f9..680f555 100644
--- src/fl_file_dir.cxx
+++ src/fl_file_dir.cxx
@@ -93,11 +93,16 @@ fl_file_chooser(const char *message,    // I - Message in titlebar
   static char   retname[FL_PATH_MAX];           // Returned filename
 
   if (!fc) {
+    // first time, so create file chooser, remember pointer,
+    // and if no filename given, set it to current directory
+
     if (!fname || !*fname) fname = ".";
 
     fc = new Fl_File_Chooser(fname, pat, Fl_File_Chooser::CREATE, message);
     fc->callback(callback, 0);
   } else {
+    // file chooser exists, so check old/new directory, pattern, filename
+
     fc->type(Fl_File_Chooser::CREATE);
     // see, if we use the same pattern between calls
     char same_pattern = 0;
@@ -110,16 +115,16 @@ fl_file_chooser(const char *message,    // I - Message in titlebar
     fc->filter(pat);
     fc->label(message);
 
-    if (!fname) { // null pointer reuses same filename if pattern didn't change
+    if (!fname) {
+      // new filename is null, so reuse old one if pattern unchanged
+
       if (!same_pattern && fc->value()) {
         // if pattern is different, remove name but leave old directory:
         strlcpy(retname, fc->value(), sizeof(retname));
 
         char *p = strrchr(retname, '/');
-
         if (p) {
-          // If the filename is "/foo", then the directory will be "/", not
-          // ""...
+          // If old filename as "/foo", then directory will be "/", not ""
           if (p == retname)
             retname[1] = '\0';
           else
@@ -130,7 +135,9 @@ fl_file_chooser(const char *message,    // I - Message in titlebar
       } else {
         // re-use the previously selected name
       }
-    } else if (!*fname) { // empty filename reuses directory with empty name
+    } else if (!*fname) {
+      // new filename is empty, so reuse old directory with empty file
+
       const char *fcv = fc->value();
       if (fcv)
         strlcpy(retname, fc->value(), sizeof(retname));
@@ -138,12 +145,22 @@ fl_file_chooser(const char *message,    // I - Message in titlebar
         *retname = 0;
       const char *n = fl_filename_name(retname);
       if (n) *((char*)n) = 0;
-      fc->value("");
-      fc->directory(retname);
+
+      // retname is either old directory, or empty if user cancelled
+      if (*retname) {
+        fc->value("");
+        fc->directory(retname); // reset old directory
+      } else {
+        char dirsave[FL_PATH_MAX];
+        strlcpy(dirsave, fc->directory(), sizeof(dirsave));
+        fc->value("");          // also resets directory to "system default"
+        fc->directory(dirsave); // so reset directory back where we were
+      }
     } else {
        fc->value(fname);
     }
   }
+  // end [re]initialization
 
   fc->ok_label(current_label);
   popup(fc);
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.