From 358ef21bc3947ab63cc908922fddddae7c258fe5 Mon Sep 17 00:00:00 2001
From: Julian Orth <ju.orth@gmail.com>
Date: Tue, 29 May 2012 14:22:04 +0200
Subject: [PATCH] We added three options

1) zoom-center :: bool
   zoom-center = false

   If zoom-center = false nothing changes.
   If zoom-center = true zooming respects the horizontal center of the
   pageview. That is, the vertical line dividing the displayed part of
   the document doesn't move within the document.

   Reasoning: When viewing a pdf with a margin we usually want to hide the
   margin at both sides by zooming in.

2) scroll-hstep :: float
   scroll-hstep = -1

   If scroll-hstep < 0 nothing changes.
   If scroll-hstep >= 0 then scroll-hstep defines the scroll step in the
   horizontal direction.

   Reasoning: This allows us to make finer adjustments in the horizontal
   direction without the mouse and without loosing the ablility to
   navigate quickly through the document.

3) search-hadjust :: bool
   search-hadjust = true

   If search-hadjust = true nothing changes.
   If search-hadjust = false searching does not adjust the horizontal
   center of the document when showing search results.

   Reasoning: When viewing a pdf with a margin we usually want to hide
   the margin at both sides by zooming in. The horizontal adjustment
   sometimes hides parts of the document by moving the horizontal
   center.
---
 config.c        |    6 ++++++
 shortcuts.c     |   38 +++++++++++++++++++++++++++++++-------
 zathurarc.5.rst |   21 +++++++++++++++++++++
 3 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/config.c b/config.c
index 47a717f..5d27275 100644
--- a/config.c
+++ b/config.c
@@ -89,6 +89,8 @@ config_load_default(zathura_t* zathura)
   girara_setting_add(gsession, "pages-per-row",         &int_value,   INT,    false, _("Number of pages per row"), cb_pages_per_row_value_changed, NULL);
   float_value = 40;
   girara_setting_add(gsession, "scroll-step",           &float_value, FLOAT,  false, _("Scroll step"),             NULL, NULL);
+  float_value = -1;
+  girara_setting_add(gsession, "scroll-hstep",          &float_value, FLOAT,  false, _("Horizontal scroll step"),             NULL, NULL);
   int_value = 10;
   girara_setting_add(gsession, "zoom-min",              &int_value,   INT,    false, _("Zoom minimum"), NULL, NULL);
   int_value = 1000;
@@ -110,6 +112,10 @@ config_load_default(zathura_t* zathura)
   girara_setting_add(gsession, "recolor",                &bool_value,  BOOLEAN, false, _("Recolor pages"), cb_setting_recolor_change, NULL);
   bool_value = false;
   girara_setting_add(gsession, "scroll-wrap",            &bool_value,  BOOLEAN, false, _("Wrap scrolling"), NULL, NULL);
+  bool_value = false;
+  girara_setting_add(gsession, "zoom-center",            &bool_value,  BOOLEAN, false, _("Horizontally centered zoom"), NULL, NULL);
+  bool_value = true;
+  girara_setting_add(gsession, "search-hadjust",         &bool_value,  BOOLEAN, false, _("Center result horizontally"), NULL, NULL);
   float_value = 0.5;
   girara_setting_add(gsession, "highlight-transparency", &float_value, FLOAT,   false, _("Transparency for highlighting"), NULL, NULL);
   bool_value = true;
diff --git a/shortcuts.c b/shortcuts.c
index 42a8244..8cefbf1 100644
--- a/shortcuts.c
+++ b/shortcuts.c
@@ -18,13 +18,22 @@
 #include "page-widget.h"
 
 static void
-readjust_view_after_zooming(zathura_t *zathura, float old_zoom) {
+readjust_view_after_zooming(girara_session_t* session, zathura_t *zathura, float old_zoom) {
+  bool zoom_center = false;
+  girara_setting_get(session, "zoom-center", &zoom_center);
+
   GtkScrolledWindow *window = GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view);
   GtkAdjustment* vadjustment = gtk_scrolled_window_get_vadjustment(window);
   GtkAdjustment* hadjustment = gtk_scrolled_window_get_hadjustment(window);
 
   gdouble valx = gtk_adjustment_get_value(hadjustment) / old_zoom * zathura->document->scale;
   gdouble valy = gtk_adjustment_get_value(vadjustment) / old_zoom * zathura->document->scale;
+
+  if(zoom_center)
+  {
+    valx += gtk_adjustment_get_page_size(hadjustment) * (zathura->document->scale / old_zoom - 1) / 2;
+  }
+
   set_adjustment(hadjustment, valx);
   set_adjustment(vadjustment, valy);
 }
@@ -113,7 +122,7 @@ sc_adjust_window(girara_session_t* session, girara_argument_t* argument,
   }
 
   /* keep position */
-  readjust_view_after_zooming(zathura, old_zoom);
+  readjust_view_after_zooming(session, zathura, old_zoom);
 
   /* re-render all pages */
   render_all(zathura);
@@ -478,6 +487,12 @@ sc_scroll(girara_session_t* session, girara_argument_t* argument,
 
   float scroll_step = 40;
   girara_setting_get(session, "scroll-step", &scroll_step);
+  float scroll_hstep = -1;
+  girara_setting_get(session, "scroll-hstep", &scroll_hstep);
+  if(scroll_hstep < 0) 
+  {
+      scroll_hstep = scroll_step;
+  }
   int padding = 1;
   girara_setting_get(session, "page-padding", &padding);
 
@@ -501,10 +516,14 @@ sc_scroll(girara_session_t* session, girara_argument_t* argument,
       new_value = value + ((view_size + padding) / 2);
       break;
     case LEFT:
+      new_value = value - scroll_hstep;
+      break;
     case UP:
       new_value = value - scroll_step;
       break;
     case RIGHT:
+      new_value = value + scroll_hstep;
+      break;
     case DOWN:
       new_value = value + scroll_step;
       break;
@@ -582,9 +601,11 @@ sc_search(girara_session_t* session, girara_argument_t* argument,
   }
 
   if (target_page != NULL) {
+    bool search_hadjust = true;
     girara_list_t* results = NULL;
     g_object_set(target_page->drawing_area, "search-current", target_idx, NULL);
     g_object_get(target_page->drawing_area, "search-results", &results, NULL);
+    girara_setting_get(session, "search-hadjust", &search_hadjust);
 
     zathura_rectangle_t* rect = girara_list_nth(results, target_idx);
     zathura_rectangle_t rectangle = recalc_rectangle(target_page, *rect);
@@ -592,12 +613,15 @@ sc_search(girara_session_t* session, girara_argument_t* argument,
     page_calculate_offset(target_page, &offset);
 
     GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
-    GtkAdjustment* view_hadjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
-
-    int x = offset.x - gtk_adjustment_get_page_size(view_hadjustment) / 2 + rectangle.x1;
     int y = offset.y - gtk_adjustment_get_page_size(view_vadjustment) / 2 + rectangle.y1;
-    set_adjustment(view_hadjustment, x);
     set_adjustment(view_vadjustment, y);
+
+    if(search_hadjust)
+    {
+      GtkAdjustment* view_hadjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
+      int x = offset.x - gtk_adjustment_get_page_size(view_hadjustment) / 2 + rectangle.x1;
+      set_adjustment(view_hadjustment, x);
+    }
   }
 
   return false;
@@ -918,7 +942,7 @@ sc_zoom(girara_session_t* session, girara_argument_t* argument, girara_event_t*
   }
 
   /* keep position */
-  readjust_view_after_zooming(zathura, old_zoom);
+  readjust_view_after_zooming(session, zathura, old_zoom);
 
   render_all(zathura);
 
diff --git a/zathurarc.5.rst b/zathurarc.5.rst
index f6821db..41bb064 100644
--- a/zathurarc.5.rst
+++ b/zathurarc.5.rst
@@ -549,6 +549,13 @@ Defines if the "Loading..." text should be displayed if a page is rendered.
 * Value-type: Boolean
 * Default value: true
 
+scroll-hstep
+^^^^^^^^^^^^
+Defines the horizontal step size of scrolling by calling the scroll command once
+
+* Value-type: Float
+* Default value: -1
+
 scroll-step
 ^^^^^^^^^^^
 Defines the step size of scrolling by calling the scroll command once
@@ -563,6 +570,20 @@ Defines if the last/first page should be wrapped
 * Value-type: Boolean
 * Default value: false
 
+search-hadjust
+^^^^^^^^^^^^^^
+En/Disables horizontally centered search results
+
+* Value-type: Boolean
+* Default value: true
+
+zoom-center
+^^^^^^^^^^^
+En/Disables horizontally centered zooming
+
+* Value-type: Bool
+* Default value: False
+
 zoom-max
 ^^^^^^^^
 Defines the maximum percentage that the zoom level can be
-- 
1.7.10.2

