changeset 2609:5beaa1da4f14

Fix #512: The "Back" button does not operate correctly https://github.com/BestImageViewer/geeqie/issues/512 Include a Forward button also
author Colin Clark <colin.clark@cclark.uk>
date Mon, 18 Sep 2017 12:00:54 +0100
parents 62da56d27277
children 34ca0d97d79d
files src/history_list.c src/history_list.h src/layout.c src/layout_util.c
diffstat 4 files changed, 101 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/history_list.c	Sun Sep 17 15:53:47 2017 +0100
+++ b/src/history_list.c	Mon Sep 18 12:00:54 2017 +0100
@@ -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
--- a/src/history_list.h	Sun Sep 17 15:53:47 2017 +0100
+++ b/src/history_list.h	Mon Sep 18 12:00:54 2017 +0100
@@ -36,6 +36,10 @@
 
 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);
 
--- a/src/layout.c	Sun Sep 17 15:53:47 2017 +0100
+++ b/src/layout.c	Mon Sep 18 12:00:54 2017 +0100
@@ -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 @@
 		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);
 
--- a/src/layout_util.c	Sun Sep 17 15:53:47 2017 +0100
+++ b/src/layout_util.c	Mon Sep 18 12:00:54 2017 +0100
@@ -1467,25 +1467,20 @@
 {
 	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++;
-		}
-
-	if (!path) return;
-
-	/* Open previous path */
-	dir_fd = file_data_new_dir(path);
+
+	/* Obtain previous path */
+	dir_fd = file_data_new_dir(history_chain_back());
+	layout_set_fd(lw, dir_fd);
+	file_data_unref(dir_fd);
+}
+
+static void layout_menu_forward_cb(GtkAction *action, gpointer data)
+{
+	LayoutWindow *lw = data;
+	FileData *dir_fd;
+
+	/* 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 @@
   { "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 @@
 "      <menuitem action='LastImage'/>"
 "      <separator/>"
 "      <menuitem action='Back'/>"
+"      <menuitem action='Forward'/>"
 "      <menuitem action='Up'/>"
 "      <menuitem action='Home'/>"
 "      <separator/>"
@@ -2145,6 +2142,7 @@
 "  <toolbar name='ToolBar'>"
 "    <toolitem action='Thumbnails'/>"
 "    <toolitem action='Back'/>"
+"    <toolitem action='Forward'/>"
 "    <toolitem action='Up'/>"
 "    <toolitem action='Home'/>"
 "    <toolitem action='Refresh'/>"