From 776d9f6b72bcca6de52a53b33285b38b5e75ec85 Mon Sep 17 00:00:00 2001 From: Colin Clark Date: Mon, 18 Sep 2017 12:00:54 +0100 Subject: [PATCH] Fix #512: The "Back" button does not operate correctly https://github.com/BestImageViewer/geeqie/issues/512 Include a Forward button also --- src/history_list.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++ src/history_list.h | 4 +++ src/layout.c | 7 ++++- src/layout_util.c | 30 +++++++++---------- 4 files changed, 98 insertions(+), 17 deletions(-) diff --git a/src/history_list.c b/src/history_list.c index 505c4991..e1fafbf6 100644 --- a/src/history_list.c +++ b/src/history_list.c @@ -24,6 +24,80 @@ #include "secure_save.h" #include "ui_fileops.h" + +/* + *----------------------------------------------------------------------------- + * Implements a history chain. Used by the Back and Forward toolbar buttons. + * Selecting any folder appends the path to the end of the chain. + * Pressing the Back and Forward buttons moves along the chain, but does + * not make additions to the chain. + * The chain always increases and is deleted at the end of the session + *----------------------------------------------------------------------------- + */ + +static GList *history_chain = NULL; +static guint chain_index = G_MAXUINT; +static gboolean nav_button = FALSE; /** Used to prevent the nav buttons making entries to the chain **/ + +const gchar *history_chain_back() +{ + nav_button = TRUE; + + chain_index = chain_index > 0 ? chain_index - 1 : 0; + + return g_list_nth_data(history_chain, chain_index); +} + +const gchar *history_chain_forward() +{ + nav_button= TRUE; + guint last = g_list_length(history_chain) - 1; + + chain_index = chain_index < last ? chain_index + 1 : last; + + return g_list_nth_data(history_chain, chain_index); +} + +/** + * @brief Appends a path to the history chain + * @param path Path selected + * + * Each time the user selects a new path it is appended to the chain + * except when it is identical to the current last entry + * The pointer is always moved to the end of the chain + */ +void history_chain_append_end(const gchar *path) +{ + GList *work; + + if (!nav_button) + { + if(chain_index == G_MAXUINT) + { + history_chain = g_list_append (history_chain, g_strdup(path)); + chain_index = 0; + } + else + { + work = g_list_last(history_chain); + if (g_strcmp0(work->data , path) != 0) + { + history_chain = g_list_append (history_chain, g_strdup(path)); + chain_index = g_list_length(history_chain) - 1; + DEBUG_3("%d %s", chain_index, path); + } + else + { + chain_index = g_list_length(history_chain) - 1; + } + } + } + else + { + nav_button = FALSE; + } +} + /* *----------------------------------------------------------------------------- * history lists diff --git a/src/history_list.h b/src/history_list.h index bbf4b284..da10b8e2 100644 --- a/src/history_list.h +++ b/src/history_list.h @@ -36,6 +36,10 @@ void history_list_item_remove(const gchar *key, const gchar *path); const gchar *history_list_find_last_path_by_key(const gchar *key); +const gchar *history_chain_back(); +const gchar *history_chain_forward(); +void history_chain_append_end(const gchar *path); + /* the returned GList is internal, don't free it */ GList *history_list_get_by_key(const gchar *key); diff --git a/src/layout.c b/src/layout.c index 692a2ae6..7d1fbf7f 100644 --- a/src/layout.c +++ b/src/layout.c @@ -25,6 +25,7 @@ #include "color-man.h" #include "filedata.h" #include "histogram.h" +#include "history_list.h" #include "image.h" #include "image-overlay.h" #include "layout_config.h" @@ -1064,7 +1065,11 @@ gboolean layout_set_fd(LayoutWindow *lw, FileData *fd) if (isfile(fd->path)) have_file = TRUE; } - if (lw->path_entry) tab_completion_append_to_history(lw->path_entry, lw->dir_fd->path); + if (lw->path_entry) + { + history_chain_append_end(lw->dir_fd->path); + tab_completion_append_to_history(lw->path_entry, lw->dir_fd->path); + } layout_sync_path(lw); layout_list_sync_sort(lw); diff --git a/src/layout_util.c b/src/layout_util.c index 9707d18b..8665d7be 100644 --- a/src/layout_util.c +++ b/src/layout_util.c @@ -1467,25 +1467,20 @@ static void layout_menu_back_cb(GtkAction *action, gpointer data) { LayoutWindow *lw = data; FileData *dir_fd; - gchar *path = NULL; - GList *list = history_list_get_by_key("path_list"); - gint n = 0; - while (list) - { - if (n == 1) { - /* Previous path from history */ - path = (gchar *)list->data; - break; - } - list = list->next; - n++; - } + /* Obtain previous path */ + dir_fd = file_data_new_dir(history_chain_back()); + layout_set_fd(lw, dir_fd); + file_data_unref(dir_fd); +} - if (!path) return; +static void layout_menu_forward_cb(GtkAction *action, gpointer data) +{ + LayoutWindow *lw = data; + FileData *dir_fd; - /* Open previous path */ - dir_fd = file_data_new_dir(path); + /* Obtain next path */ + dir_fd = file_data_new_dir(history_chain_forward()); layout_set_fd(lw, dir_fd); file_data_unref(dir_fd); } @@ -1736,6 +1731,7 @@ static GtkActionEntry menu_entries[] = { { "NextImageAlt2", GTK_STOCK_GO_DOWN, N_("_Next Image"), "KP_Page_Down", N_("Next Image"), CB(layout_menu_image_next_cb) }, { "LastImage", GTK_STOCK_GOTO_BOTTOM, N_("_Last Image"), "End", N_("Last Image"), CB(layout_menu_image_last_cb) }, { "Back", GTK_STOCK_GO_BACK, N_("_Back"), NULL, N_("Back"), CB(layout_menu_back_cb) }, + { "Forward", GTK_STOCK_GO_FORWARD, N_("_Forward"), NULL, N_("Forward"), CB(layout_menu_forward_cb) }, { "Home", GTK_STOCK_HOME, N_("_Home"), NULL, N_("Home"), CB(layout_menu_home_cb) }, { "Up", GTK_STOCK_GO_UP, N_("_Up"), NULL, N_("Up"), CB(layout_menu_up_cb) }, @@ -1949,6 +1945,7 @@ static const gchar *menu_ui_description = " " " " " " +" " " " " " " " @@ -2145,6 +2142,7 @@ static const gchar *menu_ui_description = " " " " " " +" " " " " " " " -- 2.20.1