diff --git a/shortcuts.c b/shortcuts.c index 28cf0bf..ac1429f 100644 --- a/shortcuts.c +++ b/shortcuts.c @@ -72,6 +72,22 @@ sc_goto(girara_session_t* session, girara_argument_t* argument) bool sc_navigate(girara_session_t* session, girara_argument_t* argument) { + if(!session || !argument || !Zathura.document) { + return false; + } + + unsigned int number_of_pages = Zathura.document->number_of_pages; + unsigned int new_page = Zathura.document->current_page_number; + + if(argument->n == NEXT) { + new_page = (new_page + 1) % number_of_pages; + } + else if(argument->n == PREVIOUS) { + new_page = (new_page + number_of_pages - 1) % number_of_pages; + } + + page_set(new_page); + return false; } diff --git a/zathura.c b/zathura.c index d89c74e..dd456c4 100644 --- a/zathura.c +++ b/zathura.c @@ -51,6 +51,60 @@ init_zathura() return true; } +bool +document_open(const char* path, const char* password) +{ + if(!path) { + return false; + } + + zathura_document_t* document = zathura_document_open(path, password); + + if(!document) { + return false; + } + + Zathura.document = document; + + if(!page_set(0)) { + return false; + } + + return true; +} + +bool +document_close() +{ + if(!Zathura.document) { + return false; + } + + zathura_document_free(Zathura.document); + + return true; +} + +bool +page_set(unsigned int page_id) +{ + if(!Zathura.document) { + return false; + } + + if(page_id >= Zathura.document->number_of_pages) { + return false; + } + + Zathura.document->current_page_number = page_id; + + char* page_number = g_strdup_printf("[%d/%d]", page_id + 1, Zathura.document->number_of_pages); + girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.page_number, page_number); + g_free(page_number); + + return true; +} + /* main function */ int main(int argc, char* argv[]) { @@ -64,21 +118,10 @@ int main(int argc, char* argv[]) } if(argc > 1) { - zathura_document_t* document = zathura_document_open(argv[1], NULL); - - if(!document) { + if(!document_open(argv[1], NULL)) { + printf("error: could not open document\n"); return -1; } - - zathura_page_t* page = zathura_page_get(document, 0); - - if(!page) { - zathura_document_free(document); - return -2; - } - - zathura_page_free(page); - zathura_document_free(document); } gdk_threads_enter(); diff --git a/zathura.h b/zathura.h index ba8d3ec..132b3b2 100644 --- a/zathura.h +++ b/zathura.h @@ -6,6 +6,8 @@ #include #include +#include "ft/document.h" + enum { NEXT, PREVIOUS, LEFT, RIGHT, UP, DOWN, BOTTOM, TOP, HIDE, HIGHLIGHT, DELETE_LAST_WORD, DELETE_LAST_CHAR, DEFAULT, ERROR, WARNING, NEXT_GROUP, PREVIOUS_GROUP, ZOOM_IN, ZOOM_OUT, ZOOM_ORIGINAL, ZOOM_SPECIFIC, FORWARD, @@ -23,7 +25,7 @@ enum { NEXT, PREVIOUS, LEFT, RIGHT, UP, DOWN, BOTTOM, TOP, HIDE, HIGHLIGHT, struct { - struct + struct { girara_session_t* session; /**> girara interface session */ @@ -34,6 +36,8 @@ struct girara_statusbar_item_t* page_number; /**> page number statusbar entry */ } statusbar; } UI; + + zathura_document_t* document; /**> The current document */ } Zathura; /** @@ -44,4 +48,28 @@ struct bool init_zathura(); +/** + * Opens a file + * + * @param path The path to the file + * @param password The password of the file + * + * @return If no error occured true, otherwise false, is returned. + */ +bool document_open(const char* path, const char* password); + +/** + * Closes the current opened document + * + * @return If no error occured true, otherwise false, is returned. + */ +bool document_close(); + +/** + * Opens the page with the given number + * + * @return If no error occured true, otherwise false, is returned. + */ +bool page_set(unsigned int page_id); + #endif // ZATHURA_H