From: Hugo Villeneuve Date: Sun, 8 Sep 2013 14:15:27 +0000 (-0400) Subject: Remember previous opened directory in file selection dialog X-Git-Tag: v2.0.0~173 X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=9a4763975619ad66d0bcbb3f2da127f45fe21422;p=emu8051.git Remember previous opened directory in file selection dialog --- diff --git a/src/common.h b/src/common.h index cf4a634..2fa6dcc 100644 --- a/src/common.h +++ b/src/common.h @@ -37,6 +37,8 @@ #define FIXED_FONT "monospace 12" +#define MAX_FILENAME_LENGTH 1024 + /* Common constants. */ #ifndef EXIT_SUCCESS # define EXIT_SUCCESS 0 diff --git a/src/filemenu.c b/src/filemenu.c index 560f4d9..1f3df9f 100644 --- a/src/filemenu.c +++ b/src/filemenu.c @@ -34,11 +34,37 @@ #define FILENAME_DESCRIPTION "Open Intel Hex file" +static char previous_folder[MAX_FILENAME_LENGTH + 1]; + +static void +remember_current_folder(GtkFileChooser *chooser) +{ + char *folder; + + folder = gtk_file_chooser_get_current_folder(chooser); + + if (folder != NULL) { + if (strlen(folder) >= MAX_FILENAME_LENGTH) { + /* Non-critical error */ + g_print("current folder name too long for buffer\n"); + } else { +#if defined(DEBUG) + g_print("current folder = %s\n", folder); +#endif + + strncpy(previous_folder, folder, MAX_FILENAME_LENGTH); + } + + g_free(folder); + } +} + void FileOpenEvent(GtkObject *object, gpointer data) { GtkWidget *file_dialog; - char *cwd; + char *dir; + char *cwd = NULL; #if defined(DEBUG) g_print("FileOpenEvent()\n"); @@ -50,10 +76,19 @@ FileOpenEvent(GtkObject *object, gpointer data) GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); - /* Opening file chooser to current working directory. */ - cwd = g_get_current_dir(); - gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_dialog), cwd); - g_free (cwd); + if (strlen(previous_folder) == 0) { + /* Opening file chooser to current working directory. */ + cwd = g_get_current_dir(); + dir = cwd; + } else { + /* Opening file chooser to previous opened directory. */ + dir = previous_folder; + } + + gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_dialog), dir); + + if (cwd) + g_free(cwd); if (gtk_dialog_run(GTK_DIALOG(file_dialog)) == GTK_RESPONSE_ACCEPT) { char *selected_file; @@ -66,6 +101,8 @@ FileOpenEvent(GtkObject *object, gpointer data) g_print("emugtk_File = %s\n", selected_file); #endif + remember_current_folder(GTK_FILE_CHOOSER(file_dialog)); + emugtk_new_file(selected_file); g_free(selected_file); }