diff --git a/src/dialogs.c b/src/dialogs.c index bf4e1e2ac..a4a9c7e7a 100644 --- a/src/dialogs.c +++ b/src/dialogs.c @@ -70,6 +70,19 @@ void dialogs_show_open_file () g_signal_connect((gpointer)app->open_filesel, "selection-changed", G_CALLBACK(on_file_open_selection_changed), NULL); } + + // set dialog directory to the current file's directory, if present + { + gchar *initdir = utils_get_current_file_dir(); + + if (initdir != NULL) + { + gtk_file_chooser_set_current_folder( + GTK_FILE_CHOOSER(app->open_filesel), initdir); + g_free(initdir); + } + } + gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(app->open_filesel)); /* We make sure the dialog is visible. */ gtk_window_present(GTK_WINDOW(app->open_filesel)); diff --git a/src/utils.c b/src/utils.c index 31506fc99..fa0aab059 100644 --- a/src/utils.c +++ b/src/utils.c @@ -2119,3 +2119,26 @@ void utils_treeviews_showhide(void) else gtk_widget_show(app->treeview_notebook); } + + +/* Get directory from current file in the notebook. + * Returns dir string that should be freed or NULL, depending on whether current file is valid. + * (thanks to Nick Treleaven for this patch) */ +gchar *utils_get_current_file_dir() +{ + gint cur_idx = document_get_cur_idx(); + + if (cur_idx >= 0) // if valid page found + { + // get current filename + const gchar *cur_fname = doc_list[cur_idx].file_name; + + if (cur_fname != NULL) + { + // get folder part from current filename + return g_path_get_dirname(cur_fname); // returns "." if no path + } + } + + return NULL; // no file open +} diff --git a/src/utils.h b/src/utils.h index d9dee3ed3..bf4c52d0d 100644 --- a/src/utils.h +++ b/src/utils.h @@ -199,4 +199,6 @@ gint utils_get_int_from_hexcolor(const gchar *hex); void utils_treeviews_showhide(void); +gchar *utils_get_current_file_dir(); + #endif