Dear all,

Attached you find an update for my dual view patch.

Most important changes:
- This is a git diff
- On auto-update the setting is preserved instead of reset

-- 
Johan Commelin
diff --git a/callbacks.c b/callbacks.c
index 81a1183..29bea38 100644
--- a/callbacks.c
+++ b/callbacks.c
@@ -118,7 +118,35 @@ cb_pages_per_row_value_changed(girara_session_t* session, 
const char* UNUSED(nam
     pages_per_row = 1;
   }
 
-  page_widget_set_mode(zathura, pages_per_row);
+  unsigned int first_page_column = 1;
+  girara_setting_get(session, "first-page-column", &first_page_column);
+
+  page_widget_set_mode(zathura, pages_per_row, first_page_column);
+
+  if (zathura->document != NULL) {
+    unsigned int current_page = 
zathura_document_get_current_page_number(zathura->document);
+    page_set_delayed(zathura, current_page);
+  }
+}
+
+void
+cb_first_page_column_value_changed(girara_session_t* session, const char* 
UNUSED(name), girara_setting_type_t UNUSED(type), void* value, void* 
UNUSED(data))
+{
+  g_return_if_fail(value != NULL);
+  g_return_if_fail(session != NULL);
+  g_return_if_fail(session->global.data != NULL);
+  zathura_t* zathura = session->global.data;
+
+  int first_page_column = *(int*) value;
+
+  if (first_page_column < 1) {
+    first_page_column = 1;
+  }
+
+  unsigned int pages_per_row = 1;
+  girara_setting_get(session, "pages-per-row", &pages_per_row);
+
+  page_widget_set_mode(zathura, pages_per_row, first_page_column);
 
   if (zathura->document != NULL) {
     unsigned int current_page = 
zathura_document_get_current_page_number(zathura->document);
diff --git a/callbacks.h b/callbacks.h
index a7062ea..527b5e9 100644
--- a/callbacks.h
+++ b/callbacks.h
@@ -47,6 +47,18 @@ void cb_view_vadjustment_value_changed(GtkAdjustment 
*adjustment, gpointer data)
  */
 void cb_pages_per_row_value_changed(girara_session_t* session, const char* 
name,
     girara_setting_type_t type, void* value, void* data);
+/**
+ * This function gets called when the value of the "first-page-column"
+ * variable changes
+ *
+ * @param session The current girara session
+ * @param name The name of the row
+ * @param type The settings type
+ * @param value The value
+ * @param data Custom data
+ */
+void cb_first_page_column_value_changed(girara_session_t* session, const char* 
name,
+    girara_setting_type_t type, void* value, void* data);
 
 /**
  * Called when an index element is activated (e.g.: double click)
diff --git a/config.c b/config.c
index 516f139..c44d041 100644
--- a/config.c
+++ b/config.c
@@ -104,6 +104,8 @@ config_load_default(zathura_t* zathura)
   girara_setting_add(gsession, "page-padding",          &int_value,   INT,    
false, _("Padding between pages"),   cb_page_padding_changed, NULL);
   int_value = 1;
   girara_setting_add(gsession, "pages-per-row",         &int_value,   INT,    
false, _("Number of pages per row"), cb_pages_per_row_value_changed, NULL);
+  int_value = 1;
+  girara_setting_add(gsession, "first-page-column",     &int_value,   INT,    
false, _("Column of the first page"),cb_first_page_column_value_changed, NULL);
   float_value = 40;
   girara_setting_add(gsession, "scroll-step",           &float_value, FLOAT,  
false, _("Scroll step"),             NULL, NULL);
   int_value = 10;
diff --git a/database-plain.c b/database-plain.c
index 3003c68..dfc8776 100644
--- a/database-plain.c
+++ b/database-plain.c
@@ -21,6 +21,7 @@
 #define KEY_SCALE "scale"
 #define KEY_ROTATE "rotate"
 #define KEY_PAGES_PER_ROW "pages-per-row"
+#define KEY_FIRST_PAGE_COLUMN "first-page-column"
 #define KEY_POSITION_X "position-x"
 #define KEY_POSITION_Y "position-y"
 
@@ -387,8 +388,9 @@ plain_set_fileinfo(zathura_database_t* db, const char* 
file, zathura_fileinfo_t*
   g_key_file_set_string (priv->history, name, KEY_SCALE, tmp);
   g_free(tmp);
 
-  g_key_file_set_integer(priv->history, name, KEY_ROTATE,        
file_info->rotation);
-  g_key_file_set_integer(priv->history, name, KEY_PAGES_PER_ROW, 
file_info->pages_per_row);
+  g_key_file_set_integer(priv->history, name, KEY_ROTATE,            
file_info->rotation);
+  g_key_file_set_integer(priv->history, name, KEY_PAGES_PER_ROW,     
file_info->pages_per_row);
+  g_key_file_set_integer(priv->history, name, KEY_FIRST_PAGE_COLUMN, 
file_info->first_page_column);
 
   tmp = g_strdup_printf("%f", file_info->position_x);
   g_key_file_set_string(priv->history,  name, KEY_POSITION_X, tmp);
@@ -424,10 +426,11 @@ plain_get_fileinfo(zathura_database_t* db, const char* 
file, zathura_fileinfo_t*
     return false;
   }
 
-  file_info->current_page  = g_key_file_get_integer(priv->history, name, 
KEY_PAGE, NULL);
-  file_info->page_offset   = g_key_file_get_integer(priv->history, name, 
KEY_OFFSET, NULL);
-  file_info->rotation      = g_key_file_get_integer(priv->history, name, 
KEY_ROTATE, NULL);
-  file_info->pages_per_row = g_key_file_get_integer(priv->history, name, 
KEY_PAGES_PER_ROW, NULL);
+  file_info->current_page      = g_key_file_get_integer(priv->history, name, 
KEY_PAGE, NULL);
+  file_info->page_offset       = g_key_file_get_integer(priv->history, name, 
KEY_OFFSET, NULL);
+  file_info->rotation          = g_key_file_get_integer(priv->history, name, 
KEY_ROTATE, NULL);
+  file_info->pages_per_row     = g_key_file_get_integer(priv->history, name, 
KEY_PAGES_PER_ROW, NULL);
+  file_info->first_page_column = g_key_file_get_integer(priv->history, name, 
KEY_FIRST_PAGE_COLUMN, NULL);
 
   char* scale_string = g_key_file_get_string(priv->history, name, KEY_SCALE, 
NULL);
   if (scale_string != NULL) {
diff --git a/database-sqlite.c b/database-sqlite.c
index b8f3edb..56969e2 100644
--- a/database-sqlite.c
+++ b/database-sqlite.c
@@ -120,6 +120,7 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
       "scale FLOAT,"
       "rotation INTEGER,"
       "pages_per_row INTEGER,"
+      "first_page_column INTEGER,"
       "position_x FLOAT,"
       "position_y FLOAT"
       ");";
@@ -129,6 +130,9 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
     "ALTER TABLE fileinfo ADD COLUMN position_x FLOAT;"
     "ALTER TABLE fileinfo ADD COLUMN position_y FLOAT;";
 
+  static const char SQL_FILEINFO_ALTER2[] =
+    "ALTER TABLE fileinfo ADD COLUMN first_page_column INTEGER;";
+
   sqlite3* session = NULL;
   if (sqlite3_open(path, &session) != SQLITE_OK) {
     girara_error("Could not open database: %s\n", path);
@@ -155,6 +159,14 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
     }
   }
 
+  data_type = NULL;
+  if (sqlite3_table_column_metadata(session, NULL, "fileinfo", 
"first_page_column", &data_type, NULL, NULL, NULL, NULL) != SQLITE_OK) {
+    girara_debug("old database table layout detected; updating ...");
+    if (sqlite3_exec(session, SQL_FILEINFO_ALTER2, NULL, 0, NULL) != 
SQLITE_OK) {
+      girara_warning("failed to update database table layout");
+    }
+  }
+
   priv->session = session;
 }
 
@@ -299,21 +311,22 @@ sqlite_set_fileinfo(zathura_database_t* db, const char* 
file,
   zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
 
   static const char SQL_FILEINFO_SET[] =
-    "REPLACE INTO fileinfo (file, page, offset, scale, rotation, 
pages_per_row, position_x, position_y) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
+    "REPLACE INTO fileinfo (file, page, offset, scale, rotation, 
pages_per_row, first_page_column, position_x, position_y) VALUES (?, ?, ?, ?, 
?, ?, ?, ?, ?);";
 
   sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_FILEINFO_SET);
   if (stmt == NULL) {
     return false;
   }
 
-  if (sqlite3_bind_text(stmt,   1, file, -1, NULL)           != SQLITE_OK ||
-      sqlite3_bind_int(stmt,    2, file_info->current_page)  != SQLITE_OK ||
-      sqlite3_bind_int(stmt,    3, file_info->page_offset)   != SQLITE_OK ||
-      sqlite3_bind_double(stmt, 4, file_info->scale)         != SQLITE_OK ||
-      sqlite3_bind_int(stmt,    5, file_info->rotation)      != SQLITE_OK ||
-      sqlite3_bind_int(stmt,    6, file_info->pages_per_row) != SQLITE_OK ||
-      sqlite3_bind_double(stmt, 7, file_info->position_x)    != SQLITE_OK ||
-      sqlite3_bind_double(stmt, 8, file_info->position_y)    != SQLITE_OK) {
+  if (sqlite3_bind_text(stmt,   1, file, -1, NULL)               != SQLITE_OK 
||
+      sqlite3_bind_int(stmt,    2, file_info->current_page)      != SQLITE_OK 
||
+      sqlite3_bind_int(stmt,    3, file_info->page_offset)       != SQLITE_OK 
||
+      sqlite3_bind_double(stmt, 4, file_info->scale)             != SQLITE_OK 
||
+      sqlite3_bind_int(stmt,    5, file_info->rotation)          != SQLITE_OK 
||
+      sqlite3_bind_int(stmt,    6, file_info->pages_per_row)     != SQLITE_OK 
||
+      sqlite3_bind_int(stmt,    7, file_info->first_page_column) != SQLITE_OK 
||
+      sqlite3_bind_double(stmt, 8, file_info->position_x)        != SQLITE_OK 
||
+      sqlite3_bind_double(stmt, 9, file_info->position_y)        != SQLITE_OK) 
{
     sqlite3_finalize(stmt);
     girara_error("Failed to bind arguments.");
     return false;
@@ -336,7 +349,7 @@ sqlite_get_fileinfo(zathura_database_t* db, const char* 
file,
   zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
 
   static const char SQL_FILEINFO_GET[] =
-    "SELECT page, offset, scale, rotation, pages_per_row, position_x, 
position_y FROM fileinfo WHERE file = ?;";
+    "SELECT page, offset, scale, rotation, pages_per_row, first_page_column, 
position_x, position_y FROM fileinfo WHERE file = ?;";
 
   sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_FILEINFO_GET);
   if (stmt == NULL) {
@@ -355,13 +368,14 @@ sqlite_get_fileinfo(zathura_database_t* db, const char* 
file,
     return false;
   }
 
-  file_info->current_page  = sqlite3_column_int(stmt, 0);
-  file_info->page_offset   = sqlite3_column_int(stmt, 1);
-  file_info->scale         = sqlite3_column_double(stmt, 2);
-  file_info->rotation      = sqlite3_column_int(stmt, 3);
-  file_info->pages_per_row = sqlite3_column_int(stmt, 4);
-  file_info->position_x    = sqlite3_column_double(stmt, 5);
-  file_info->position_y    = sqlite3_column_double(stmt, 6);
+  file_info->current_page      = sqlite3_column_int(stmt, 0);
+  file_info->page_offset       = sqlite3_column_int(stmt, 1);
+  file_info->scale             = sqlite3_column_double(stmt, 2);
+  file_info->rotation          = sqlite3_column_int(stmt, 3);
+  file_info->pages_per_row     = sqlite3_column_int(stmt, 4);
+  file_info->first_page_column = sqlite3_column_int(stmt, 5);
+  file_info->position_x        = sqlite3_column_double(stmt, 6);
+  file_info->position_y        = sqlite3_column_double(stmt, 7);
 
   sqlite3_finalize(stmt);
 
diff --git a/database.h b/database.h
index d2cd624..5161b35 100644
--- a/database.h
+++ b/database.h
@@ -15,6 +15,7 @@ typedef struct zathura_fileinfo_s {
   double scale;
   unsigned int rotation;
   unsigned int pages_per_row;
+  unsigned int first_page_column;
   double position_x;
   double position_y;
 } zathura_fileinfo_t;
diff --git a/shortcuts.c b/shortcuts.c
index e877202..320572d 100644
--- a/shortcuts.c
+++ b/shortcuts.c
@@ -62,6 +62,9 @@ sc_adjust_window(girara_session_t* session, 
girara_argument_t* argument,
   unsigned int pages_per_row = 1;
   girara_setting_get(session, "pages-per-row", &pages_per_row);
 
+  unsigned int first_page_column = 1;
+  girara_setting_get(session, "first-page-column", &first_page_column);
+
   if (zathura->ui.page_widget == NULL || zathura->document == NULL) {
     goto error_ret;
   }
@@ -900,12 +903,16 @@ sc_toggle_fullscreen(girara_session_t* session, 
girara_argument_t*
 
   static bool fullscreen = false;
   static int pages_per_row = 1;
+  static int first_page_column = 1;
   static double zoom = 1.0;
 
   if (fullscreen == true) {
     /* reset pages per row */
     girara_setting_set(session, "pages-per-row", &pages_per_row);
 
+    /* reset first page column */
+    girara_setting_set(session, "first-page-column", &first_page_column);
+
     /* show status bar */
     gtk_widget_show(GTK_WIDGET(session->gtk.statusbar));
 
@@ -920,6 +927,9 @@ sc_toggle_fullscreen(girara_session_t* session, 
girara_argument_t*
     /* backup pages per row */
     girara_setting_get(session, "pages-per-row", &pages_per_row);
 
+    /* backup first page column */
+    girara_setting_get(session, "first-page-column", &first_page_column);
+
     /* set single view */
     int int_value = 1;
     girara_setting_set(session, "pages-per-row", &int_value);
diff --git a/zathura.c b/zathura.c
index 06d7278..2d37493 100644
--- a/zathura.c
+++ b/zathura.c
@@ -494,7 +494,7 @@ document_open(zathura_t* zathura, const char* path, const 
char* password)
   unsigned int number_of_pages = 
zathura_document_get_number_of_pages(document);
 
   /* read history file */
-  zathura_fileinfo_t file_info = { 0, 0, 1, 0, 1, 0, 0 };
+  zathura_fileinfo_t file_info = { 0, 0, 1, 0, 1, 1, 0, 0 };
   bool known_file = zathura_db_get_fileinfo(zathura->database, file_path, 
&file_info);
 
   /* set page offset */
@@ -622,14 +622,18 @@ document_open(zathura_t* zathura, const char* path, const 
char* password)
 
   /* view mode */
   int pages_per_row = 1;
+  int first_page_column = 1;
   if (file_info.pages_per_row > 0) {
     pages_per_row = file_info.pages_per_row;
+    first_page_column = file_info.first_page_column;
   } else {
     girara_setting_get(zathura->ui.session, "pages-per-row", &pages_per_row);
+    girara_setting_get(zathura->ui.session, "first-page-column", 
&first_page_column);
   }
 
   girara_setting_set(zathura->ui.session, "pages-per-row", &pages_per_row);
-  page_widget_set_mode(zathura, pages_per_row);
+  girara_setting_set(zathura->ui.session, "first-page-column", 
&first_page_column);
+  page_widget_set_mode(zathura, pages_per_row, first_page_column);
 
   girara_set_view(zathura->ui.session, zathura->ui.page_widget_alignment);
 
@@ -750,13 +754,14 @@ document_close(zathura_t* zathura, bool keep_monitor)
   /* store file information */
   const char* path = zathura_document_get_path(zathura->document);
 
-  zathura_fileinfo_t file_info = { 0, 0, 1, 0, 1, 0, 0 };
+  zathura_fileinfo_t file_info = { 0, 0, 1, 0, 1, 1, 0, 0 };
   file_info.current_page = 
zathura_document_get_current_page_number(zathura->document);
   file_info.page_offset  = zathura_document_get_page_offset(zathura->document);
   file_info.scale        = zathura_document_get_scale(zathura->document);
   file_info.rotation     = zathura_document_get_rotation(zathura->document);
 
   girara_setting_get(zathura->ui.session, "pages-per-row", 
&(file_info.pages_per_row));
+  girara_setting_get(zathura->ui.session, "first-page-column", 
&(file_info.first_page_column));
 
   /* get position */
   GtkScrolledWindow *window = 
GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view);
@@ -884,13 +889,22 @@ statusbar_page_number_update(zathura_t* zathura)
 }
 
 void
-page_widget_set_mode(zathura_t* zathura, unsigned int pages_per_row)
+page_widget_set_mode(zathura_t* zathura, unsigned int pages_per_row, unsigned 
int first_page_column)
 {
   /* show at least one page */
   if (pages_per_row == 0) {
     pages_per_row = 1;
   }
 
+       /* ensure: 0 < first_page_column <= pages_per_row */
+       if (first_page_column < 1) {
+               first_page_column = 1;
+       }
+
+       if (first_page_column > pages_per_row) {
+               first_page_column = ((first_page_column - 1) % pages_per_row) + 
1;
+       }
+
   if (zathura->document == NULL) {
     return;
   }
@@ -898,11 +912,11 @@ page_widget_set_mode(zathura_t* zathura, unsigned int 
pages_per_row)
   gtk_container_foreach(GTK_CONTAINER(zathura->ui.page_widget), 
remove_page_from_table, (gpointer)0);
 
   unsigned int number_of_pages     = 
zathura_document_get_number_of_pages(zathura->document);
-  gtk_table_resize(GTK_TABLE(zathura->ui.page_widget), ceil(number_of_pages / 
pages_per_row), pages_per_row);
+  gtk_table_resize(GTK_TABLE(zathura->ui.page_widget), ceil((number_of_pages + 
first_page_column - 1) / pages_per_row), pages_per_row);
   for (unsigned int i = 0; i < number_of_pages; i++)
   {
-    int x = i % pages_per_row;
-    int y = i / pages_per_row;
+    int x = (i + first_page_column - 1) % pages_per_row;
+    int y = (i + first_page_column - 1) / pages_per_row;
 
     zathura_page_t* page   = zathura_document_get_page(zathura->document, i);
     GtkWidget* page_widget = zathura_page_get_widget(zathura, page);
diff --git a/zathura.h b/zathura.h
index 9e40dcb..1233af9 100644
--- a/zathura.h
+++ b/zathura.h
@@ -194,8 +194,9 @@ bool position_set_delayed(zathura_t* zathura, double 
position_x, double position
  *
  * @param zathura The zathura session
  * @param pages_per_row Number of shown pages per row
+ * @param first_page_column Column on which first page start
  */
-void page_widget_set_mode(zathura_t* zathura, unsigned int pages_per_row);
+void page_widget_set_mode(zathura_t* zathura, unsigned int pages_per_row, 
unsigned int first_page_column);
 
 /**
  * Updates the page number in the statusbar. Note that 1 will be added to the
_______________________________________________
zathura mailing list
zathura@lists.pwmt.org
http://lists.pwmt.org/mailman/listinfo/zathura

Reply via email to