Optional page offset mode

This commit is build on the idea of Renato Florentino to be able to
correct the page offset that some documents have. His patch, where
most of the code has been adapted to this commit ( thanks! ), made
it able to correct the current page and automatically calculate the
correct page offset. Now this patch will introduce a third mode to
the bcmd_goto command: You are now able to set the page directly,
calculate it over the offset or search the labels for it.
This commit is contained in:
Moritz Lipp 2010-05-05 20:05:06 +02:00
parent 5e45c8d608
commit 63c0312b57
2 changed files with 77 additions and 17 deletions

View file

@ -17,6 +17,7 @@ static const char BOOKMARK_FILE[] = "bookmarks";
/* bookmarks */ /* bookmarks */
static const char BM_PAGE_ENTRY[] = "page"; static const char BM_PAGE_ENTRY[] = "page";
static const char BM_PAGE_OFFSET[] = "offset";
/* look */ /* look */
static const char font[] = "monospace normal 9"; static const char font[] = "monospace normal 9";
@ -55,7 +56,7 @@ static const char DEFAULT_TEXT[] = "[No Name]";
#define SHOW_SCROLLBARS 0 #define SHOW_SCROLLBARS 0
#define ADJUST_OPEN ADJUST_BESTFIT #define ADJUST_OPEN ADJUST_BESTFIT
#define RECOLOR_OPEN 0 #define RECOLOR_OPEN 0
#define ENABLE_LABELS 1 #define GOTO_MODE GOTO_LABELS /* GOTO_DEFAULT, GOTO_LABELS, GOTO_OFFSET */
/* shortcuts */ /* shortcuts */
Shortcut shortcuts[] = { Shortcut shortcuts[] = {
@ -74,6 +75,7 @@ Shortcut shortcuts[] = {
{0, GDK_K, sc_navigate, NORMAL, { PREVIOUS } }, {0, GDK_K, sc_navigate, NORMAL, { PREVIOUS } },
{GDK_MOD1_MASK, GDK_Right, sc_navigate, NORMAL, { NEXT } }, {GDK_MOD1_MASK, GDK_Right, sc_navigate, NORMAL, { NEXT } },
{GDK_MOD1_MASK, GDK_Left, sc_navigate, NORMAL, { PREVIOUS } }, {GDK_MOD1_MASK, GDK_Left, sc_navigate, NORMAL, { PREVIOUS } },
{0, GDK_O, sc_switch_goto_mode, NORMAL, {0} },
{0, GDK_space, sc_navigate, NORMAL, { NEXT } }, {0, GDK_space, sc_navigate, NORMAL, { NEXT } },
{0, GDK_Escape, sc_abort, -1, {0} }, {0, GDK_Escape, sc_abort, -1, {0} },
{0, GDK_i, sc_change_mode, NORMAL, { INSERT } }, {0, GDK_i, sc_change_mode, NORMAL, { INSERT } },
@ -124,6 +126,7 @@ Command commands[] = {
{"bmark", "b", cmd_bookmark, 0, "Bookmark current page" }, {"bmark", "b", cmd_bookmark, 0, "Bookmark current page" },
{"blist", 0, cmd_open_bookmark, cc_bookmark, "List and open bookmark" }, {"blist", 0, cmd_open_bookmark, cc_bookmark, "List and open bookmark" },
{"close", "c", cmd_close, 0, "Close current file" }, {"close", "c", cmd_close, 0, "Close current file" },
{"coffset", 0, cmd_correct_offset, 0, "Correct page offset" },
{"delbmark", 0, cmd_delete_bookmark, cc_bookmark, "Bookmark current page" }, {"delbmark", 0, cmd_delete_bookmark, cc_bookmark, "Bookmark current page" },
{"export", "e", cmd_export, cc_export, "Export images or attached files" }, {"export", "e", cmd_export, cc_export, "Export images or attached files" },
{"info", "i", cmd_info, 0, "Show information about the document" }, {"info", "i", cmd_info, 0, "Show information about the document" },
@ -160,5 +163,5 @@ Setting settings[] = {
/* name, variable, type, render, description */ /* name, variable, type, render, description */
{"recolor", &(Zathura.Global.recolor), 'b', TRUE, "Invert the image"}, {"recolor", &(Zathura.Global.recolor), 'b', TRUE, "Invert the image"},
{"password", &(Zathura.PDF.password), 's', FALSE, "The password of the document"}, {"password", &(Zathura.PDF.password), 's', FALSE, "The password of the document"},
{"labels", &(Zathura.Global.enable_labels), 'b', FALSE, "Use labels for pages"}, {"offset", &(Zathura.PDF.page_offset), 'i', FALSE, "Optional page offset"},
}; };

View file

@ -27,7 +27,8 @@ enum { NEXT, PREVIOUS, LEFT, RIGHT, UP, DOWN,
ZOOM_IN, ZOOM_OUT, ZOOM_ORIGINAL, ZOOM_SPECIFIC, ZOOM_IN, ZOOM_OUT, ZOOM_ORIGINAL, ZOOM_SPECIFIC,
FORWARD, BACKWARD, ADJUST_BESTFIT, ADJUST_WIDTH, FORWARD, BACKWARD, ADJUST_BESTFIT, ADJUST_WIDTH,
ADJUST_NONE, CONTINUOUS, DELETE_LAST, ADD_MARKER, ADJUST_NONE, CONTINUOUS, DELETE_LAST, ADD_MARKER,
EVAL_MARKER, INDEX, EXPAND, COLLAPSE, SELECT }; EVAL_MARKER, INDEX, EXPAND, COLLAPSE, SELECT,
GOTO_DEFAULT, GOTO_LABELS, GOTO_OFFSET};
/* typedefs */ /* typedefs */
struct CElement struct CElement
@ -186,7 +187,7 @@ struct
int mode; int mode;
int viewing_mode; int viewing_mode;
gboolean recolor; gboolean recolor;
gboolean enable_labels; int goto_mode;
GtkLabel *status_text; GtkLabel *status_text;
GtkLabel *status_buffer; GtkLabel *status_buffer;
GtkLabel *status_state; GtkLabel *status_state;
@ -228,6 +229,7 @@ struct
char *password; char *password;
Page **pages; Page **pages;
int page_number; int page_number;
int page_offset;
int number_of_pages; int number_of_pages;
int scale; int scale;
int rotate; int rotate;
@ -291,6 +293,7 @@ void sc_recolor(Argument*);
void sc_rotate(Argument*); void sc_rotate(Argument*);
void sc_scroll(Argument*); void sc_scroll(Argument*);
void sc_search(Argument*); void sc_search(Argument*);
void sc_switch_goto_mode(Argument*);
void sc_navigate_index(Argument*); void sc_navigate_index(Argument*);
void sc_toggle_index(Argument*); void sc_toggle_index(Argument*);
void sc_toggle_inputbar(Argument*); void sc_toggle_inputbar(Argument*);
@ -307,6 +310,7 @@ void isc_string_manipulation(Argument*);
gboolean cmd_bookmark(int, char**); gboolean cmd_bookmark(int, char**);
gboolean cmd_open_bookmark(int, char**); gboolean cmd_open_bookmark(int, char**);
gboolean cmd_close(int, char**); gboolean cmd_close(int, char**);
gboolean cmd_correct_offset(int, char**);
gboolean cmd_delete_bookmark(int, char**); gboolean cmd_delete_bookmark(int, char**);
gboolean cmd_export(int, char**); gboolean cmd_export(int, char**);
gboolean cmd_info(int, char**); gboolean cmd_info(int, char**);
@ -412,7 +416,7 @@ init_zathura()
Zathura.Global.viewing_mode = NORMAL; Zathura.Global.viewing_mode = NORMAL;
Zathura.Global.recolor = RECOLOR_OPEN; Zathura.Global.recolor = RECOLOR_OPEN;
Zathura.Global.adjust_mode = ADJUST_OPEN; Zathura.Global.adjust_mode = ADJUST_OPEN;
Zathura.Global.enable_labels = ENABLE_LABELS; Zathura.Global.goto_mode = GOTO_MODE;
Zathura.State.filename = (char*) DEFAULT_TEXT; Zathura.State.filename = (char*) DEFAULT_TEXT;
Zathura.State.pages = ""; Zathura.State.pages = "";
@ -862,15 +866,21 @@ open_file(char* path, char* password)
/* start page */ /* start page */
int start_page = 0; int start_page = 0;
Zathura.PDF.page_offset = 0;
/* bookmarks */ /* bookmarks */
if(Zathura.Bookmarks.data) if(Zathura.Bookmarks.data && g_key_file_has_group(Zathura.Bookmarks.data, file))
{ {
/* get last opened page */ /* get last opened page */
if(g_key_file_has_group(Zathura.Bookmarks.data, file))
if(g_key_file_has_key(Zathura.Bookmarks.data, file, BM_PAGE_ENTRY, NULL)) if(g_key_file_has_key(Zathura.Bookmarks.data, file, BM_PAGE_ENTRY, NULL))
start_page = g_key_file_get_integer(Zathura.Bookmarks.data, file, BM_PAGE_ENTRY, NULL); start_page = g_key_file_get_integer(Zathura.Bookmarks.data, file, BM_PAGE_ENTRY, NULL);
/* get page offset */
if(g_key_file_has_key(Zathura.Bookmarks.data, file, BM_PAGE_OFFSET, NULL))
Zathura.PDF.page_offset = g_key_file_get_integer(Zathura.Bookmarks.data, file, BM_PAGE_OFFSET, NULL);
if((Zathura.PDF.page_offset != 0) && (Zathura.PDF.page_offset != GOTO_OFFSET))
Zathura.PDF.page_offset = GOTO_OFFSET;
/* open and read bookmark file */ /* open and read bookmark file */
gsize i = 0; gsize i = 0;
gsize number_of_keys = 0; gsize number_of_keys = 0;
@ -878,7 +888,7 @@ open_file(char* path, char* password)
for(i = 0; i < number_of_keys; i++) for(i = 0; i < number_of_keys; i++)
{ {
if(strcmp(keys[i], BM_PAGE_ENTRY)) if(strcmp(keys[i], BM_PAGE_ENTRY) && strcmp(keys[i], BM_PAGE_OFFSET))
{ {
Zathura.Bookmarks.bookmarks = realloc(Zathura.Bookmarks.bookmarks, Zathura.Bookmarks.bookmarks = realloc(Zathura.Bookmarks.bookmarks,
(Zathura.Bookmarks.number_of_bookmarks + 1) * sizeof(Bookmark)); (Zathura.Bookmarks.number_of_bookmarks + 1) * sizeof(Bookmark));
@ -916,16 +926,20 @@ update_status()
if( Zathura.PDF.document && Zathura.PDF.pages ) if( Zathura.PDF.document && Zathura.PDF.pages )
{ {
int page = Zathura.PDF.page_number; int page = Zathura.PDF.page_number;
if(Zathura.Global.enable_labels && Zathura.PDF.pages[page]->label) /*
if((Zathura.Global.goto_mode == GOTO_LABELS) && Zathura.PDF.pages[page]->label)
Zathura.State.pages = g_strdup_printf("[%s/%i]", Zathura.State.pages = g_strdup_printf("[%s/%i]",
Zathura.PDF.pages[page]->label, Zathura.PDF.number_of_pages); Zathura.PDF.pages[page]->label, Zathura.PDF.number_of_pages);
else else
*/
Zathura.State.pages = g_strdup_printf("[%i/%i]", page + 1, Zathura.PDF.number_of_pages); Zathura.State.pages = g_strdup_printf("[%i/%i]", page + 1, Zathura.PDF.number_of_pages);
} }
/* update state */ /* update state */
char* zoom_level = (Zathura.PDF.scale != 0) ? g_strdup_printf("%d%%", Zathura.PDF.scale) : ""; char* zoom_level = (Zathura.PDF.scale != 0) ? g_strdup_printf("%d%%", Zathura.PDF.scale) : "";
char* status_text = g_strdup_printf("%s %s", zoom_level, Zathura.State.pages); char* goto_mode = (Zathura.Global.goto_mode == GOTO_LABELS) ? "L" :
(Zathura.Global.goto_mode == GOTO_OFFSET) ? "O" : "D";
char* status_text = g_strdup_printf("%s [%s] %s", zoom_level, goto_mode, Zathura.State.pages);
gtk_label_set_markup((GtkLabel*) Zathura.Global.status_state, status_text); gtk_label_set_markup((GtkLabel*) Zathura.Global.status_state, status_text);
} }
@ -1440,6 +1454,25 @@ sc_search(Argument* argument)
g_static_mutex_unlock(&(Zathura.Lock.search_lock)); g_static_mutex_unlock(&(Zathura.Lock.search_lock));
} }
void
sc_switch_goto_mode(Argument* argument)
{
switch(Zathura.Global.goto_mode)
{
case GOTO_LABELS:
Zathura.Global.goto_mode = GOTO_OFFSET;
break;
case GOTO_OFFSET:
Zathura.Global.goto_mode = GOTO_DEFAULT;
break;
default:
Zathura.Global.goto_mode = GOTO_LABELS;
break;
}
update_status();
}
gboolean cb_index_row_activated(GtkTreeView* treeview, GtkTreePath* path, gboolean cb_index_row_activated(GtkTreeView* treeview, GtkTreePath* path,
GtkTreeViewColumn* column, gpointer user_data) GtkTreeViewColumn* column, gpointer user_data)
{ {
@ -2060,6 +2093,10 @@ cmd_close(int argc, char** argv)
g_key_file_set_integer(Zathura.Bookmarks.data, Zathura.PDF.file, g_key_file_set_integer(Zathura.Bookmarks.data, Zathura.PDF.file,
BM_PAGE_ENTRY, Zathura.PDF.page_number); BM_PAGE_ENTRY, Zathura.PDF.page_number);
/* set page offset */
g_key_file_set_integer(Zathura.Bookmarks.data, Zathura.PDF.file,
BM_PAGE_OFFSET, Zathura.PDF.page_offset);
/* save bookmarks */ /* save bookmarks */
int i; int i;
for(i = 0; i < Zathura.Bookmarks.number_of_bookmarks; i++) for(i = 0; i < Zathura.Bookmarks.number_of_bookmarks; i++)
@ -2077,7 +2114,6 @@ cmd_close(int argc, char** argv)
inotify_rm_watch(Zathura.Inotify.fd, Zathura.Inotify.wd); inotify_rm_watch(Zathura.Inotify.fd, Zathura.Inotify.wd);
Zathura.Inotify.wd = -1; Zathura.Inotify.wd = -1;
/*pthread_cancel(Zathura.Thread.inotify_thread);*/
/* reset values */ /* reset values */
free(Zathura.PDF.pages); free(Zathura.PDF.pages);
@ -2095,6 +2131,7 @@ cmd_close(int argc, char** argv)
Zathura.PDF.number_of_pages = 0; Zathura.PDF.number_of_pages = 0;
Zathura.PDF.scale = 0; Zathura.PDF.scale = 0;
Zathura.PDF.rotate = 0; Zathura.PDF.rotate = 0;
Zathura.PDF.page_offset = 0;
/* destroy index */ /* destroy index */
if(Zathura.UI.index) if(Zathura.UI.index)
@ -2121,6 +2158,24 @@ cmd_close(int argc, char** argv)
return TRUE; return TRUE;
} }
gboolean
cmd_correct_offset(int argc, char** argv)
{
if(!Zathura.PDF.document || argc == 0)
return TRUE;
Zathura.PDF.page_offset = (Zathura.PDF.page_number + 1) - atoi(argv[0]);
if(Zathura.PDF.page_offset != 0)
Zathura.Global.goto_mode = GOTO_OFFSET;
else
Zathura.Global.goto_mode = GOTO_MODE;
update_status();
return TRUE;
}
gboolean gboolean
cmd_delete_bookmark(int argc, char** argv) cmd_delete_bookmark(int argc, char** argv)
{ {
@ -2765,13 +2820,15 @@ bcmd_goto(char* buffer, Argument* argument)
char* id = g_strndup(buffer, b_length - 1); char* id = g_strndup(buffer, b_length - 1);
int pid = atoi(id); int pid = atoi(id);
if(Zathura.Global.enable_labels) if(Zathura.Global.goto_mode == GOTO_LABELS)
{ {
int i; int i;
for(i = 0; i < Zathura.PDF.number_of_pages; i++) for(i = 0; i < Zathura.PDF.number_of_pages; i++)
if(!strcmp(id, Zathura.PDF.pages[i]->label)) if(!strcmp(id, Zathura.PDF.pages[i]->label))
pid = Zathura.PDF.pages[i]->id; pid = Zathura.PDF.pages[i]->id;
} }
else if(Zathura.Global.goto_mode == GOTO_OFFSET)
pid += Zathura.PDF.page_offset;
set_page(pid - 1); set_page(pid - 1);
g_free(id); g_free(id);