Fix #512: The "Back" button does not operate correctly
authorColin Clark <colin.clark@cclark.uk>
Mon, 18 Sep 2017 11:00:54 +0000 (12:00 +0100)
committerColin Clark <colin.clark@cclark.uk>
Mon, 18 Sep 2017 11:00:54 +0000 (12:00 +0100)
https://github.com/BestImageViewer/geeqie/issues/512

Include a Forward button also

src/history_list.c
src/history_list.h
src/layout.c
src/layout_util.c

index 505c499..e1fafbf 100644 (file)
 #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
index bbf4b28..da10b8e 100644 (file)
@@ -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);
 
index 692a2ae..7d1fbf7 100644 (file)
@@ -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);
 
index 9707d18..8665d7b 100644 (file)
@@ -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 =
 "      <menuitem action='LastImage'/>"
 "      <separator/>"
 "      <menuitem action='Back'/>"
+"      <menuitem action='Forward'/>"
 "      <menuitem action='Up'/>"
 "      <menuitem action='Home'/>"
 "      <separator/>"
@@ -2145,6 +2142,7 @@ static const gchar *menu_ui_description =
 "  <toolbar name='ToolBar'>"
 "    <toolitem action='Thumbnails'/>"
 "    <toolitem action='Back'/>"
+"    <toolitem action='Forward'/>"
 "    <toolitem action='Up'/>"
 "    <toolitem action='Home'/>"
 "    <toolitem action='Refresh'/>"