From: H.G.Muller Date: Sun, 21 Dec 2014 22:09:25 +0000 (+0100) Subject: Let file selecor remember last used directory (GTK) X-Git-Url: http://winboard.nl/cgi-bin?a=commitdiff_plain;h=a928758cb579f00f54c499d90dc750d323f43223;p=xboard.git Let file selecor remember last used directory (GTK) The file selector now starts in the directory that was last used to load a file of the type we are now browsing for. (Supported types: pgn, fen, trn, bin, png.) --- diff --git a/dialogs.c b/dialogs.c index 29003f5..e932247 100644 --- a/dialogs.c +++ b/dialogs.c @@ -2967,6 +2967,34 @@ DirSelProc (int n, int sel) } void +StartDir (char *filter, char *newName) +{ + static char *gamesDir, *trnDir, *imgDir, *bookDir; + static char curDir[MSG_SIZ]; + char **res = NULL; + if(!filter || !*filter) return; + if(strstr(filter, "pgn")) res = &gamesDir; else + if(strstr(filter, "bin")) res = &bookDir; else + if(strstr(filter, "png")) res = &imgDir; else + if(strstr(filter, "trn")) res = &trnDir; else + if(strstr(filter, "fen")) res = &appData.positionDir; + if(res) { + if(newName) { + char *p, *q; + if(*newName) { + ASSIGN(*res, newName); + for(p=*res; q=strchr(p, '/');) p = q + 1; *p = NULLCHAR; + } + if(*curDir) chdir(curDir); + *curDir = NULLCHAR; + } else { + getcwd(curDir, MSG_SIZ); + if(*res && **res) chdir(*res); + } + } +} + +void Browse (DialogClass dlg, char *label, char *proposed, char *ext, Boolean pathFlag, char *mode, char **name, FILE **fp) { int j=0; diff --git a/dialogs.h b/dialogs.h index 9c00356..36cd212 100644 --- a/dialogs.h +++ b/dialogs.h @@ -169,6 +169,7 @@ void SetScroll P((Option *opt, float f)); void AddHandler P((Option *opt, DialogClass dlg, int nr)); void SendText P((int n)); void DisplayLogos P((Option *left, Option *right)); +void StartDir P((char *filter, char *newName)); void Browse P((DialogClass dlg, char *label, char *proposed, char *ext, Boolean pathFlag, char *mode, char **name, FILE **fp)); void FileNamePopUpWrapper P((char *label, char *def, char *filter, FileProc proc, diff --git a/gtk/xboard.c b/gtk/xboard.c index ef35f2a..3588c9d 100644 --- a/gtk/xboard.c +++ b/gtk/xboard.c @@ -2319,10 +2319,7 @@ void FileNamePopUpWrapper(label, def, filter, proc, pathFlag, openMode, name, fp char *cp; char curDir[MSG_SIZ]; - if(def && *def && def[strlen(def)-1] == '/') { - getcwd(curDir, MSG_SIZ); - chdir(def); - } + StartDir(filter, NULL); // change to start directory for this file type /* make a copy of the filter string, so that strtok can work with it*/ cp = strdup(filter); @@ -2396,8 +2393,10 @@ void FileNamePopUpWrapper(label, def, filter, proc, pathFlag, openMode, name, fp ASSIGN(*name, filename); ScheduleDelayedEvent(DelayedLoad, 50); } + StartDir(filter, filename); g_free (filename); - }; + } + else StartDir(filter, ""); gtk_widget_destroy (dialog); ModeHighlight(); diff --git a/gtk/xoptions.c b/gtk/xoptions.c index 67675ff..27a6a57 100644 --- a/gtk/xoptions.c +++ b/gtk/xoptions.c @@ -1016,7 +1016,9 @@ void BrowseGTK(GtkWidget *widget, gpointer gdata) gtkfilter = gtk_file_filter_new(); gtkfilter_all = gtk_file_filter_new(); - char fileext[MSG_SIZ]; + char fileext[MSG_SIZ], *filter = currentOption[opt_i].textValue; + + StartDir(filter, NULL); // change to start directory for this file type /* select file or folder depending on option_type */ if (currentOption[opt_i].type == PathName) @@ -1060,9 +1062,10 @@ void BrowseGTK(GtkWidget *widget, gpointer gdata) filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); entry = currentOption[opt_i].handle; gtk_entry_set_text (GTK_ENTRY (entry), filename); + StartDir(filter, filename); // back to original, and remember this one g_free (filename); - } + else StartDir(filter, ""); // change back to original directory gtk_widget_destroy (dialog); dialog = NULL; } diff --git a/menus.c b/menus.c index 653d978..a92a152 100644 --- a/menus.c +++ b/menus.c @@ -105,6 +105,7 @@ extern char *getenv(); char *gameCopyFilename, *gamePasteFilename; Boolean saveSettingsOnExit; char *settingsFileName; +char gamesDir[MSG_SIZ], positionsDir[MSG_SIZ], textureDir[MSG_SIZ], bookDir[MSG_SIZ], piecesDir[MSG_SIZ]; static int LoadGamePopUp (FILE *f, int gameNumber, char *title) @@ -173,12 +174,10 @@ ReloadPositionProc () void LoadPositionProc() { - static char buf[MSG_SIZ]; if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) { Reset(FALSE, TRUE); } - snprintf(buf, MSG_SIZ, "%s/", appData.positionDir); - FileNamePopUp(_("Load position file name?"), buf, ".fen .epd .pos", LoadPosition, "rb"); + FileNamePopUp(_("Load position file name?"), "", ".fen .epd .pos", LoadPosition, "rb"); } void