From 334b4fbb959ed71c97b7aa98f4cb880d5b957ff1 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Tue, 27 Mar 2012 21:59:35 +0200 Subject: [PATCH] Introduce and use zathura_document wrapper functions --- bookmarks.c | 6 +- callbacks.c | 25 ++++--- commands.c | 19 +++-- config.c | 8 +- document.c | 197 ++++++++++++++++++++++++++++++++++++++++++-------- document.h | 128 +++++++++++++++++++++----------- internal.h | 18 +++++ page-widget.c | 25 ++++--- page.c | 94 ++++++++++-------------- plugin-api.h | 10 +-- plugin.h | 6 +- print.c | 19 +++-- render.c | 10 ++- shortcuts.c | 107 ++++++++++++++------------- types.h | 10 +++ utils.c | 49 +++++++------ zathura.c | 65 ++++++++++------- zathura.h | 10 +-- 18 files changed, 517 insertions(+), 289 deletions(-) diff --git a/bookmarks.c b/bookmarks.c index 5fc94d5..3d142a2 100644 --- a/bookmarks.c +++ b/bookmarks.c @@ -34,7 +34,8 @@ zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page) girara_list_append(zathura->bookmarks.bookmarks, bookmark); if (zathura->database != NULL) { - if (zathura_db_add_bookmark(zathura->database, zathura->document->file_path, bookmark) == false) { + const char* path = zathura_document_get_path(zathura->document); + if (zathura_db_add_bookmark(zathura->database, path, bookmark) == false) { girara_warning("Failed to add bookmark to database."); } } @@ -54,7 +55,8 @@ zathura_bookmark_remove(zathura_t* zathura, const gchar* id) } if (zathura->database != NULL) { - if (zathura_db_remove_bookmark(zathura->database, zathura->document->file_path, bookmark->id) == false) { + const char* path = zathura_document_get_path(zathura->document); + if (zathura_db_remove_bookmark(zathura->database, path, bookmark->id) == false) { girara_warning("Failed to remove bookmark from database."); } } diff --git a/callbacks.c b/callbacks.c index 70fe2aa..33e32f2 100644 --- a/callbacks.c +++ b/callbacks.c @@ -51,8 +51,7 @@ void cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpointer data) { zathura_t* zathura = data; - if (zathura == NULL || zathura->document == NULL || zathura->document->pages == NULL - || zathura->ui.page_widget == NULL) { + if (zathura == NULL || zathura->document == NULL || zathura->ui.page_widget == NULL) { return; } @@ -74,23 +73,26 @@ cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpoi center.y = (view_rect.height + 1) / 2; center.height = center.width = (2 * page_padding) + 1; + unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); + double scale = zathura_document_get_scale(zathura->document); + bool updated = false; /* find page that fits */ - for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++) { - zathura_page_t* page = zathura->document->pages[page_id]; + for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { + zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); GdkRectangle page_rect; GtkWidget* page_widget = zathura_page_get_widget(page); gtk_widget_translate_coordinates(page_widget, zathura->ui.session->gtk.view, 0, 0, &page_rect.x, &page_rect.y); - page_rect.width = zathura_page_get_width(page) * zathura->document->scale; - page_rect.height = zathura_page_get_height(page) * zathura->document->scale; + page_rect.width = zathura_page_get_width(page) * scale; + page_rect.height = zathura_page_get_height(page) * scale; if (gdk_rectangle_intersect(&view_rect, &page_rect, NULL) == TRUE) { zathura_page_set_visibility(page, true); if (zathura->global.update_page_number == true && updated == false && gdk_rectangle_intersect(¢er, &page_rect, NULL) == TRUE) { - zathura->document->current_page_number = page_id; + zathura_document_set_current_page_number(zathura->document, page_id); updated = true; } } else { @@ -119,7 +121,7 @@ cb_pages_per_row_value_changed(girara_session_t* session, const char* UNUSED(nam page_widget_set_mode(zathura, pages_per_row); if (zathura->document != NULL) { - unsigned int current_page = zathura->document->current_page_number; + unsigned int current_page = zathura_document_get_current_page_number(zathura->document); page_set_delayed(zathura, current_page); } } @@ -185,8 +187,9 @@ cb_sc_follow(GtkEntry* entry, girara_session_t* session) /* set pages to draw links */ bool invalid_index = true; - for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++) { - zathura_page_t* page = zathura->document->pages[page_id]; + unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); + for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { + zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); if (page == NULL || zathura_page_get_visibility(page) == false) { continue; } @@ -322,7 +325,7 @@ cb_view_resized(GtkWidget* UNUSED(widget), GtkAllocation* allocation, zathura_t* /* adjust only if the allocation changed */ if (width != allocation->width || height != allocation->height) { - girara_argument_t argument = { zathura->document->adjust_mode, NULL }; + girara_argument_t argument = { zathura_document_get_adjust_mode(zathura->document), NULL }; sc_adjust_window(zathura->ui.session, &argument, NULL, 0); width = allocation->width; diff --git a/commands.c b/commands.c index 80192bd..c688d2a 100644 --- a/commands.c +++ b/commands.c @@ -40,12 +40,12 @@ cmd_bookmark_create(girara_session_t* session, girara_list_t* argument_list) const char* bookmark_name = girara_list_nth(argument_list, 0); zathura_bookmark_t* bookmark = zathura_bookmark_get(zathura, bookmark_name); if (bookmark != NULL) { - bookmark->page = zathura->document->current_page_number + 1; + bookmark->page = zathura_document_get_current_page_number(zathura->document) + 1; girara_notify(session, GIRARA_INFO, _("Bookmark successfuly updated: %s"), bookmark_name); return true; } - bookmark = zathura_bookmark_add(zathura, bookmark_name, zathura->document->current_page_number + 1); + bookmark = zathura_bookmark_add(zathura, bookmark_name, zathura_document_get_current_page_number(zathura->document) + 1); if (bookmark == NULL) { girara_notify(session, GIRARA_ERROR, _("Could not create bookmark: %s"), bookmark_name); return false; @@ -304,8 +304,13 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu bool firsthit = true; zathura_error_t error = ZATHURA_ERROR_OK; - for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; ++page_id) { - zathura_page_t* page = zathura->document->pages[(page_id + zathura->document->current_page_number) % zathura->document->number_of_pages]; + + unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); + unsigned int current_page_number = zathura_document_get_current_page_number(zathura->document); + + for (unsigned int page_id = 0; page_id < number_of_pages; ++page_id) { + unsigned int index = (page_id + current_page_number) % number_of_pages; + zathura_page_t* page = zathura_document_get_page(zathura->document, index); if (page == NULL) { continue; } @@ -385,7 +390,7 @@ cmd_offset(girara_session_t* session, girara_list_t* argument_list) } /* no argument: take current page as offset */ - unsigned int page_offset = zathura->document->current_page_number; + unsigned int page_offset = zathura_document_get_current_page_number(zathura->document); /* retrieve offset from argument */ if (girara_list_size(argument_list) == 1) { @@ -399,8 +404,8 @@ cmd_offset(girara_session_t* session, girara_list_t* argument_list) } } - if (page_offset < zathura->document->number_of_pages) { - zathura->document->page_offset = page_offset; + if (page_offset < zathura_document_get_number_of_pages(zathura->document)) { + zathura_document_set_page_offset(zathura->document, page_offset); } return true; diff --git a/config.c b/config.c index 417cedf..ad51700 100644 --- a/config.c +++ b/config.c @@ -126,8 +126,8 @@ config_load_default(zathura_t* zathura) girara_shortcut_add(gsession, GDK_CONTROL_MASK, GDK_KEY_c, NULL, sc_abort, 0, 0, NULL); girara_shortcut_add(gsession, 0, GDK_KEY_Escape, NULL, sc_abort, 0, 0, NULL); - girara_shortcut_add(gsession, 0, GDK_KEY_a, NULL, sc_adjust_window, NORMAL, ADJUST_BESTFIT, NULL); - girara_shortcut_add(gsession, 0, GDK_KEY_s, NULL, sc_adjust_window, NORMAL, ADJUST_WIDTH, NULL); + girara_shortcut_add(gsession, 0, GDK_KEY_a, NULL, sc_adjust_window, NORMAL, ZATHURA_ADJUST_BESTFIT, NULL); + girara_shortcut_add(gsession, 0, GDK_KEY_s, NULL, sc_adjust_window, NORMAL, ZATHURA_ADJUST_WIDTH, NULL); girara_shortcut_add(gsession, 0, GDK_KEY_m, NULL, sc_change_mode, NORMAL, ADD_MARKER, NULL); girara_shortcut_add(gsession, 0, GDK_KEY_apostrophe, NULL, sc_change_mode, NORMAL, EVAL_MARKER, NULL); @@ -302,8 +302,8 @@ config_load_default(zathura_t* zathura) girara_argument_mapping_add(gsession, "specific", ZOOM_SPECIFIC); girara_argument_mapping_add(gsession, "top", TOP); girara_argument_mapping_add(gsession, "up", UP); - girara_argument_mapping_add(gsession, "best-fit", ADJUST_BESTFIT); - girara_argument_mapping_add(gsession, "width", ADJUST_WIDTH); + girara_argument_mapping_add(gsession, "best-fit", ZATHURA_ADJUST_BESTFIT); + girara_argument_mapping_add(gsession, "width", ZATHURA_ADJUST_WIDTH); girara_argument_mapping_add(gsession, "rotate-cw", ROTATE_CW); girara_argument_mapping_add(gsession, "rotate-ccw", ROTATE_CCW); } diff --git a/document.c b/document.c index 605f46f..997d045 100644 --- a/document.c +++ b/document.c @@ -32,6 +32,34 @@ static const size_t GT_MAX_READ = 1 << 16; static const gchar* guess_type(const char* path); +/** + * Document + */ +struct zathura_document_s +{ + char* file_path; /**< File path of the document */ + const char* password; /**< Password of the document */ + unsigned int current_page_number; /**< Current page number */ + unsigned int number_of_pages; /**< Number of pages */ + double scale; /**< Scale value */ + unsigned int rotate; /**< Rotation */ + void* data; /**< Custom data */ + zathura_adjust_mode_t adjust_mode; /**< Adjust mode (best-fit, width) */ + zathura_t* zathura; /**< Zathura instance */ + unsigned int page_offset; /**< Page offset */ + + /** + * Document pages + */ + zathura_page_t** pages; + + /** + * Used plugin + */ + zathura_plugin_t* plugin; +}; + + zathura_document_t* zathura_document_open(zathura_t* zathura, const char* path, const char* password) { @@ -94,8 +122,8 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password document->file_path = real_path; document->password = password; document->scale = 1.0; - document->zathura = zathura; document->plugin = plugin; + document->zathura = zathura; /* open document */ if (plugin->functions.document_open == NULL) { @@ -170,14 +198,14 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password /* apply open adjustment */ char* adjust_open = "best-fit"; - document->adjust_mode = ADJUST_BESTFIT; + document->adjust_mode = ZATHURA_ADJUST_BESTFIT; if (girara_setting_get(zathura->ui.session, "adjust-open", &(adjust_open)) == true) { if (g_strcmp0(adjust_open, "best-fit") == 0) { - document->adjust_mode = ADJUST_BESTFIT; + document->adjust_mode = ZATHURA_ADJUST_BESTFIT; } else if (g_strcmp0(adjust_open, "width") == 0) { - document->adjust_mode = ADJUST_WIDTH; + document->adjust_mode = ZATHURA_ADJUST_WIDTH; } else { - document->adjust_mode = ADJUST_NONE; + document->adjust_mode = ZATHURA_ADJUST_NONE; } g_free(adjust_open); } @@ -203,8 +231,7 @@ error_free: zathura_error_t zathura_document_free(zathura_document_t* document) { - if (document == NULL || document->plugin == NULL || document->zathura == NULL - || document->zathura->ui.session == NULL) { + if (document == NULL || document->plugin == NULL) { return ZATHURA_ERROR_INVALID_ARGUMENTS; } @@ -219,8 +246,6 @@ zathura_document_free(zathura_document_t* document) /* free document */ zathura_error_t error = ZATHURA_ERROR_OK; if (document->plugin->functions.document_free == NULL) { - girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); error = ZATHURA_ERROR_NOT_IMPLEMENTED; } else { error = document->plugin->functions.document_free(document, document->data); @@ -255,6 +280,16 @@ zathura_document_get_password(zathura_document_t* document) return document->password; } +zathura_page_t* +zathura_document_get_page(zathura_document_t* document, unsigned int index) +{ + if (document == NULL || document->pages == NULL || (document->number_of_pages <= index)) { + return NULL; + } + + return document->pages[index]; +} + void* zathura_document_get_data(zathura_document_t* document) { @@ -296,7 +331,7 @@ zathura_document_set_number_of_pages(zathura_document_t* document, unsigned int } unsigned int -zathura_document_get_current_page(zathura_document_t* document) +zathura_document_get_current_page_number(zathura_document_t* document) { if (document == NULL) { return 0; @@ -306,7 +341,7 @@ zathura_document_get_current_page(zathura_document_t* document) } void -zathura_document_set_current_page(zathura_document_t* document, unsigned int +zathura_document_set_current_page_number(zathura_document_t* document, unsigned int current_page) { if (document == NULL) { @@ -316,17 +351,110 @@ zathura_document_set_current_page(zathura_document_t* document, unsigned int document->current_page_number = current_page; } +double +zathura_document_get_scale(zathura_document_t* document) +{ + if (document == NULL) { + return 0; + } + + return document->scale; +} + +void +zathura_document_set_scale(zathura_document_t* document, double scale) +{ + if (document == NULL) { + return; + } + + document->scale = scale; +} + +unsigned int +zathura_document_get_rotation(zathura_document_t* document) +{ + if (document == NULL) { + return 0; + } + + return document->rotate; +} + +void +zathura_document_set_rotation(zathura_document_t* document, unsigned int rotation) +{ + if (document == NULL) { + return; + } + + document->rotate = rotation % 360; + + if (document->rotate > 0 && document->rotate < 90) { + document->rotate = 90; + } else if (document->rotate > 0 && document->rotate < 180) { + document->rotate = 180; + } else if (document->rotate > 0 && document->rotate < 270) { + document->rotate = 270; + } else { + document->rotate = 0; + } +} + +zathura_adjust_mode_t +zathura_document_get_adjust_mode(zathura_document_t* document) +{ + if (document == NULL) { + return ZATHURA_ADJUST_NONE; + } + + return document->adjust_mode; +} + +void +zathura_document_set_adjust_mode(zathura_document_t* document, zathura_adjust_mode_t mode) +{ + if (document == NULL) { + return; + } + + if (mode == ZATHURA_ADJUST_BESTFIT || mode == ZATHURA_ADJUST_WIDTH) { + document->adjust_mode = mode; + } else { + document->adjust_mode = ZATHURA_ADJUST_NONE; + } +} + +unsigned int +zathura_document_get_page_offset(zathura_document_t* document) +{ + if (document == NULL) { + return 0; + } + + return document->page_offset; +} + +void +zathura_document_set_page_offset(zathura_document_t* document, unsigned int page_offset) +{ + if (document == NULL) { + return; + } + + if (page_offset < document->number_of_pages) { + document->page_offset = page_offset; + } +} + zathura_error_t zathura_document_save_as(zathura_document_t* document, const char* path) { - if (document == NULL || document->plugin == NULL || path == NULL || - document->zathura == NULL || document->zathura->ui.session == NULL) { + if (document == NULL || document->plugin == NULL || path == NULL) { return ZATHURA_ERROR_UNKNOWN; } if (document->plugin->functions.document_save_as == NULL) { - girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); return ZATHURA_ERROR_NOT_IMPLEMENTED; } @@ -336,8 +464,7 @@ zathura_document_save_as(zathura_document_t* document, const char* path) girara_tree_node_t* zathura_document_index_generate(zathura_document_t* document, zathura_error_t* error) { - if (document == NULL || document->plugin == NULL || document->zathura == NULL - || document->zathura->ui.session == NULL) { + if (document == NULL || document->plugin == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } @@ -345,8 +472,6 @@ zathura_document_index_generate(zathura_document_t* document, zathura_error_t* e } if (document->plugin->functions.document_index_generate == NULL) { - girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } @@ -359,8 +484,7 @@ zathura_document_index_generate(zathura_document_t* document, zathura_error_t* e girara_list_t* zathura_document_attachments_get(zathura_document_t* document, zathura_error_t* error) { - if (document == NULL || document->plugin == NULL || document->zathura == NULL - || document->zathura->ui.session == NULL) { + if (document == NULL || document->plugin == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } @@ -368,8 +492,6 @@ zathura_document_attachments_get(zathura_document_t* document, zathura_error_t* } if (document->plugin->functions.document_attachments_get == NULL) { - girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } @@ -382,14 +504,11 @@ zathura_document_attachments_get(zathura_document_t* document, zathura_error_t* zathura_error_t zathura_document_attachment_save(zathura_document_t* document, const char* attachment, const char* file) { - if (document == NULL || document->plugin == NULL || document->zathura == NULL - || document->zathura->ui.session == NULL) { + if (document == NULL || document->plugin == NULL) { return ZATHURA_ERROR_INVALID_ARGUMENTS; } if (document->plugin->functions.document_attachment_save == NULL) { - girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); return ZATHURA_ERROR_NOT_IMPLEMENTED; } @@ -399,7 +518,7 @@ zathura_document_attachment_save(zathura_document_t* document, const char* attac char* zathura_document_meta_get(zathura_document_t* document, zathura_document_meta_t meta, zathura_error_t* error) { - if (document == NULL || document->plugin == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) { + if (document == NULL || document->plugin == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } @@ -410,8 +529,6 @@ zathura_document_meta_get(zathura_document_t* document, zathura_document_meta_t if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } - girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); return NULL; } @@ -493,3 +610,23 @@ guess_type(const char* path) g_strdelimit(out, "\n\r", '\0'); return out; } + +zathura_t* +zathura_document_get_zathura(zathura_document_t* document) +{ + if (document == NULL) { + return NULL; + } + + return document->zathura; +} + +zathura_plugin_t* +zathura_document_get_plugin(zathura_document_t* document) +{ + if (document == NULL) { + return NULL; + } + + return document->plugin; +} diff --git a/document.h b/document.h index 4c0a4c2..330a8ae 100644 --- a/document.h +++ b/document.h @@ -26,33 +26,6 @@ typedef enum zathura_document_meta_e ZATHURA_DOCUMENT_MODIFICATION_DATE /**< Modification data */ } zathura_document_meta_t; -/** - * Document - */ -struct zathura_document_s -{ - char* file_path; /**< File path of the document */ - const char* password; /**< Password of the document */ - unsigned int current_page_number; /**< Current page number */ - unsigned int number_of_pages; /**< Number of pages */ - double scale; /**< Scale value */ - unsigned int rotate; /**< Rotation */ - void* data; /**< Custom data */ - zathura_t* zathura; /** Zathura object */ - int adjust_mode; /**< Adjust mode (best-fit, width) */ - unsigned int page_offset; /**< Page offset */ - - /** - * Document pages - */ - zathura_page_t** pages; - - /** - * Used plugin - */ - zathura_plugin_t* plugin; -}; - /** * Open the document * @@ -64,6 +37,15 @@ struct zathura_document_s zathura_document_t* zathura_document_open(zathura_t* zathura, const char* path, const char* password); +/** + * Free the document + * + * @param document + * @return ZATHURA_ERROR_OK when no error occured, otherwise see + * zathura_error_t + */ +zathura_error_t zathura_document_free(zathura_document_t* document); + /** * Returns the path of the document * @@ -81,21 +63,13 @@ const char* zathura_document_get_path(zathura_document_t* document); const char* zathura_document_get_password(zathura_document_t* document); /** - * Free the document - * - * @param document - * @return ZATHURA_ERROR_OK when no error occured, otherwise see - * zathura_error_t - */ -zathura_error_t zathura_document_free(zathura_document_t* document); - -/** - * Returns the private data of the document + * Returns the page at the given index * * @param document The document - * @return The private data or NULL + * @param index The index of the page + * @return The page or NULL if an error occured */ -void* zathura_document_get_data(zathura_document_t* document); +zathura_page_t* zathura_document_get_page(zathura_document_t* document, unsigned int index); /** * Returns the number of pages @@ -120,7 +94,7 @@ void zathura_document_set_number_of_pages(zathura_document_t* document, unsigned * @param document The document * @return Current page */ -unsigned int zathura_document_get_current_page(zathura_document_t* document); +unsigned int zathura_document_get_current_page_number(zathura_document_t* document); /** * Sets the number of pages @@ -128,9 +102,81 @@ unsigned int zathura_document_get_current_page(zathura_document_t* document); * @param document The document * @param current_page The current page number */ -void zathura_document_set_current_page(zathura_document_t* document, unsigned +void zathura_document_set_current_page_number(zathura_document_t* document, unsigned int current_page); +/** + * Returns the current scale value of the document + * + * @param document The document + * @return The current scale value + */ +double zathura_document_get_scale(zathura_document_t* document); + +/** + * Sets the new scale value of the document + * + * @param document The document + * @param scale The new scale value + */ +void zathura_document_set_scale(zathura_document_t* document, double scale); + +/** + * Returns the rotation value of zathura (0..360) + * + * @param document The document + * @return The current rotation value + */ +unsigned int zathura_document_get_rotation(zathura_document_t* document); + +/** + * Sets the new rotation value + * + * @param document The document + * @param rotation The new rotation value + */ +void zathura_document_set_rotation(zathura_document_t* document, unsigned int rotation); + +/** + * Returns the adjust mode of the document + * + * @param document The document + * @return The adjust mode + */ +zathura_adjust_mode_t zathura_document_get_adjust_mode(zathura_document_t* document); + +/** + * Sets the new adjust mode of the document + * + * @param document The document + * @param mode The new adjust mode + */ +void zathura_document_set_adjust_mode(zathura_document_t* document, zathura_adjust_mode_t mode); + +/** + * Returns the page offset of the document + * + * @param document The document + * @return The page offset + */ +unsigned int zathura_document_get_page_offset(zathura_document_t* document); + +/** + * Sets the new page offset of the document + * + * @param document The document + * @param page_offset The new page offset + */ +void zathura_document_set_page_offset(zathura_document_t* document, unsigned int page_offset); + +/** + * Returns the private data of the document + * + * @param document The document + * @return The private data or NULL + */ +void* zathura_document_get_data(zathura_document_t* document); + /** * Sets the private data of the document * diff --git a/internal.h b/internal.h index fadacfb..488c27a 100644 --- a/internal.h +++ b/internal.h @@ -4,6 +4,7 @@ #define INTERNAL_H #include "zathura.h" +#include "plugin.h" /** * Zathura password dialog @@ -14,4 +15,21 @@ typedef struct zathura_password_dialog_info_s zathura_t* zathura; /**< Zathura session */ } zathura_password_dialog_info_t; +/** + * Returns the associated zathura instance + * TODO: Separate zathura_t completely from the document + * + * @param document The docment + * @return The associated zathura instance + */ +zathura_t* zathura_document_get_zathura(zathura_document_t* document); + +/** + * Returns the associated plugin + * + * @param document The document + * @return The plugin or NULL + */ +zathura_plugin_t* zathura_document_get_plugin(zathura_document_t* document); + #endif // INTERNAL_H diff --git a/page-widget.c b/page-widget.c index e43e2af..70798ae 100644 --- a/page-widget.c +++ b/page-widget.c @@ -179,7 +179,7 @@ zathura_page_widget_set_property(GObject* object, guint prop_id, const GValue* v case PROP_PAGE: priv->page = g_value_get_pointer(value); document = zathura_page_get_document(priv->page); - priv->zathura = document->zathura; + priv->zathura = zathura_document_get_zathura(document); break; case PROP_DRAW_LINKS: priv->draw_links = g_value_get_boolean(value); @@ -300,7 +300,8 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) if (priv->surface != NULL) { cairo_save(cairo); - switch (document->rotate) { + unsigned int rotation = zathura_document_get_rotation(document); + switch (rotation) { case 90: cairo_translate(cairo, page_width, 0); break; @@ -312,8 +313,8 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) break; } - if (document->rotate != 0) { - cairo_rotate(cairo, document->rotate * G_PI / 180.0); + if (rotation != 0) { + cairo_rotate(cairo, rotation * G_PI / 180.0); } cairo_set_source_surface(cairo, priv->surface, 0, 0); @@ -547,7 +548,7 @@ cb_zathura_page_widget_button_release_event(GtkWidget* widget, GdkEventButton* b && rect.y1 <= button->y && rect.y2 >= button->y) { switch (link->type) { case ZATHURA_LINK_TO_PAGE: - page_set_delayed(document->zathura, link->target.page_number); + page_set_delayed(priv->zathura, link->target.page_number); return false; case ZATHURA_LINK_EXTERNAL: girara_xdg_open(link->target.uri); @@ -562,10 +563,11 @@ cb_zathura_page_widget_button_release_event(GtkWidget* widget, GdkEventButton* b redraw_rect(ZATHURA_PAGE(widget), &priv->selection); zathura_rectangle_t tmp = priv->selection; - tmp.x1 /= document->scale; - tmp.x2 /= document->scale; - tmp.y1 /= document->scale; - tmp.y2 /= document->scale; + double scale = zathura_document_get_scale(document); + tmp.x1 /= scale; + tmp.x2 /= scale; + tmp.y1 /= scale; + tmp.y2 /= scale; char* text = zathura_page_get_text(priv->page, tmp, NULL); if (text != NULL) { @@ -574,10 +576,9 @@ cb_zathura_page_widget_button_release_event(GtkWidget* widget, GdkEventButton* b gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), text, -1); - if (priv->page != NULL && document != NULL && document->zathura != NULL) { - zathura_t* zathura = document->zathura; + if (priv->page != NULL && document != NULL && priv->zathura != NULL) { char* stripped_text = g_strdelimit(g_strdup(text), "\n\t\r\n", ' '); - girara_notify(zathura->ui.session, GIRARA_INFO, _("Copied selected text to clipbard: %s"), stripped_text); + girara_notify(priv->zathura->ui.session, GIRARA_INFO, _("Copied selected text to clipbard: %s"), stripped_text); g_free(stripped_text); } } diff --git a/page.c b/page.c index a77b4cc..8accf5c 100644 --- a/page.c +++ b/page.c @@ -9,6 +9,7 @@ #include "page-widget.h" #include "plugin.h" #include "utils.h" +#include "internal.h" struct zathura_page_s { double height; /**< Page height */ @@ -23,8 +24,7 @@ struct zathura_page_s { zathura_page_t* zathura_page_new(zathura_document_t* document, unsigned int index, zathura_error_t* error) { - if (document == NULL || document->plugin == NULL || document->zathura == NULL - || document->zathura->ui.session == NULL) { + if (document == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } @@ -47,16 +47,15 @@ zathura_page_new(zathura_document_t* document, unsigned int index, zathura_error } /* init plugin */ - if (document->plugin->functions.page_init == NULL) { + zathura_plugin_t* plugin = zathura_document_get_plugin(document); + if (plugin->functions.page_init == NULL) { if (error != NULL) { - girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } goto error_ret; } - zathura_error_t ret = document->plugin->functions.page_init(page); + zathura_error_t ret = plugin->functions.page_init(page); if (ret != ZATHURA_ERROR_OK) { if (error != NULL) { *error = ret; @@ -91,19 +90,17 @@ zathura_page_free(zathura_page_t* page) return ZATHURA_ERROR_INVALID_ARGUMENTS; } - if (page->document == NULL || page->document->plugin == NULL || - page->document->zathura == NULL || page->document->zathura->ui.session == NULL) { + if (page->document == NULL) { g_free(page); return ZATHURA_ERROR_INVALID_ARGUMENTS; } - if (page->document->plugin->functions.page_clear == NULL) { - girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); + zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); + if (plugin->functions.page_clear == NULL) { return ZATHURA_ERROR_NOT_IMPLEMENTED; } - zathura_error_t error = page->document->plugin->functions.page_clear(page, page->data); + zathura_error_t error = plugin->functions.page_clear(page, page->data); g_free(page); @@ -223,47 +220,43 @@ zathura_page_set_data(zathura_page_t* page, void* data) girara_list_t* zathura_page_search_text(zathura_page_t* page, const char* text, zathura_error_t* error) { - if (page == NULL || page->document == NULL || page->document->plugin == NULL || text == NULL || - page->document->zathura == NULL || page->document->zathura->ui.session == NULL) { + if (page == NULL || page->document == NULL || text == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } return NULL; } - if (page->document->plugin->functions.page_search_text == NULL) { - girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); + zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); + if (plugin->functions.page_search_text == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return page->document->plugin->functions.page_search_text(page, page->data, text, error); + return plugin->functions.page_search_text(page, page->data, text, error); } girara_list_t* zathura_page_links_get(zathura_page_t* page, zathura_error_t* error) { - if (page == NULL || page->document == NULL || page->document->plugin == NULL || page->document->zathura == NULL - || page->document->zathura->ui.session == NULL) { + if (page == NULL || page->document == NULL ) { if (error != NULL) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } return NULL; } - if (page->document->plugin->functions.page_links_get == NULL) { - girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); + zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); + if (plugin->functions.page_links_get == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return page->document->plugin->functions.page_links_get(page, page->data, error); + return plugin->functions.page_links_get(page, page->data, error); } zathura_error_t @@ -275,24 +268,22 @@ zathura_page_links_free(girara_list_t* UNUSED(list)) girara_list_t* zathura_page_form_fields_get(zathura_page_t* page, zathura_error_t* error) { - if (page == NULL || page->document == NULL || page->document->plugin == NULL || page->document->zathura == NULL - || page->document->zathura->ui.session == NULL) { + if (page == NULL || page->document == NULL ) { if (error != NULL) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } return NULL; } - if (page->document->plugin->functions.page_form_fields_get == NULL) { - girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); + zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); + if (plugin->functions.page_form_fields_get == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return page->document->plugin->functions.page_form_fields_get(page, page->data, error); + return plugin->functions.page_form_fields_get(page, page->data, error); } zathura_error_t @@ -304,87 +295,76 @@ zathura_page_form_fields_free(girara_list_t* UNUSED(list)) girara_list_t* zathura_page_images_get(zathura_page_t* page, zathura_error_t* error) { - if (page == NULL || page->document == NULL || page->document->plugin == NULL || page->document->zathura == NULL - || page->document->zathura->ui.session == NULL) { + if (page == NULL || page->document == NULL ) { if (error != NULL) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } return NULL; } - if (page->document->plugin->functions.page_images_get == NULL) { - girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); + zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); + if (plugin->functions.page_images_get == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return page->document->plugin->functions.page_images_get(page, page->data, error); + return plugin->functions.page_images_get(page, page->data, error); } cairo_surface_t* zathura_page_image_get_cairo(zathura_page_t* page, zathura_image_t* image, zathura_error_t* error) { - if (page == NULL || page->document == NULL || page->document->plugin == NULL || image == NULL || - page->document->zathura == NULL || page->document->zathura->ui.session == - NULL) { + if (page == NULL || page->document == NULL || image == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } return NULL; } - if (page->document->plugin->functions.page_image_get_cairo == NULL) { - girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); + zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); + if (plugin->functions.page_image_get_cairo == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return page->document->plugin->functions.page_image_get_cairo(page, page->data, image, error); + return plugin->functions.page_image_get_cairo(page, page->data, image, error); } char* zathura_page_get_text(zathura_page_t* page, zathura_rectangle_t rectangle, zathura_error_t* error) { - if (page == NULL || page->document == NULL || page->document->plugin == NULL - || page->document->zathura == NULL || page->document->zathura->ui.session - == NULL) { + if (page == NULL || page->document == NULL ) { if (error) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } return NULL; } - if (page->document->plugin->functions.page_get_text == NULL) { - girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); + zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); + if (plugin->functions.page_get_text == NULL) { if (error) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return page->document->plugin->functions.page_get_text(page, page->data, rectangle, error); + return plugin->functions.page_get_text(page, page->data, rectangle, error); } zathura_error_t zathura_page_render(zathura_page_t* page, cairo_t* cairo, bool printing) { - if (page == NULL || page->document->plugin == NULL || page->document == NULL || cairo == NULL || - page->document->zathura == NULL || page->document->zathura->ui.session == - NULL) { + if (page == NULL || page->document == NULL || cairo == NULL) { return ZATHURA_ERROR_INVALID_ARGUMENTS; } - if (page->document->plugin->functions.page_render_cairo == NULL) { - girara_notify(page->document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__); - girara_error("%s not implemented", __FUNCTION__); + zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); + if (plugin->functions.page_render_cairo == NULL) { return ZATHURA_ERROR_NOT_IMPLEMENTED; } - return page->document->plugin->functions.page_render_cairo(page, page->data, cairo, printing); + return plugin->functions.page_render_cairo(page, page->data, cairo, printing); } diff --git a/plugin-api.h b/plugin-api.h index c099d46..0256f4c 100644 --- a/plugin-api.h +++ b/plugin-api.h @@ -41,11 +41,11 @@ void zathura_plugin_add_mimetype(zathura_plugin_t* plugin, const char* mime_type * @param mimetypes a char array of mime types supported by the plugin */ #define ZATHURA_PLUGIN_REGISTER(plugin_name, major, minor, rev, register_functions, mimetypes) \ - unsigned int plugin_version_major() { return major; } \ - unsigned int plugin_version_minor() { return minor; } \ - unsigned int plugin_version_revision() { return rev; } \ - unsigned int plugin_api_version() { return ZATHURA_API_VERSION; } \ - unsigned int plugin_abi_version() { return ZATHURA_ABI_VERSION; } \ + unsigned int zathura_plugin_version_major() { return major; } \ + unsigned int zathura_plugin_version_minor() { return minor; } \ + unsigned int zathura_plugin_version_revision() { return rev; } \ + unsigned int zathura_plugin_api_version() { return ZATHURA_API_VERSION; } \ + unsigned int zathura_plugin_abi_version() { return ZATHURA_ABI_VERSION; } \ \ void zathura_plugin_register(zathura_plugin_t* plugin) \ { \ diff --git a/plugin.h b/plugin.h index 70bd3a4..039ca90 100644 --- a/plugin.h +++ b/plugin.h @@ -10,9 +10,9 @@ #include "version.h" #include "zathura.h" -#define PLUGIN_REGISTER_FUNCTION "plugin_register" -#define PLUGIN_API_VERSION_FUNCTION "plugin_api_version" -#define PLUGIN_ABI_VERSION_FUNCTION "plugin_abi_version" +#define PLUGIN_REGISTER_FUNCTION "zathura_plugin_register" +#define PLUGIN_API_VERSION_FUNCTION "zathura_plugin_api_version" +#define PLUGIN_ABI_VERSION_FUNCTION "zathura_plugin_abi_version" /** * Plugin mapping diff --git a/print.c b/print.c index 4f029e6..247352b 100644 --- a/print.c +++ b/print.c @@ -26,8 +26,8 @@ print(zathura_t* zathura) } gtk_print_operation_set_allow_async(print_operation, TRUE); - gtk_print_operation_set_n_pages(print_operation, zathura->document->number_of_pages); - gtk_print_operation_set_current_page(print_operation, zathura->document->current_page_number); + gtk_print_operation_set_n_pages(print_operation, zathura_document_get_number_of_pages(zathura->document)); + gtk_print_operation_set_current_page(print_operation, zathura_document_get_current_page_number(zathura->document)); /* print operation signals */ g_signal_connect(print_operation, "draw-page", G_CALLBACK(cb_print_draw_page), zathura); @@ -59,20 +59,23 @@ void cb_print_end(GtkPrintOperation* UNUSED(print_operation), GtkPrintContext* UNUSED(context), zathura_t* zathura) { - if (zathura == NULL || zathura->ui.session == NULL || zathura->document == NULL - || zathura->document->file_path == NULL) { + if (zathura == NULL || zathura->ui.session == NULL || zathura->document == NULL) { return; } - girara_statusbar_item_set_text(zathura->ui.session, - zathura->ui.statusbar.file, zathura->document->file_path); + const char* file_path = zathura_document_get_path(zathura->document); + + if (file_path != NULL) { + girara_statusbar_item_set_text(zathura->ui.session, + zathura->ui.statusbar.file, file_path); + } } void cb_print_draw_page(GtkPrintOperation* UNUSED(print_operation), GtkPrintContext* context, gint page_number, zathura_t* zathura) { - if (context == NULL || zathura == NULL || zathura->document->pages == NULL || + if (context == NULL || zathura == NULL || zathura->document == NULL || zathura->ui.session == NULL || zathura->ui.statusbar.file == NULL) { return; } @@ -85,7 +88,7 @@ cb_print_draw_page(GtkPrintOperation* UNUSED(print_operation), GtkPrintContext* /* render page */ cairo_t* cairo = gtk_print_context_get_cairo_context(context); - zathura_page_t* page = zathura->document->pages[page_number]; + zathura_page_t* page = zathura_document_get_page(zathura->document, page_number); if (cairo == NULL || page == NULL) { return; } diff --git a/render.c b/render.c index 7b18b86..53e806e 100644 --- a/render.c +++ b/render.c @@ -111,8 +111,9 @@ render(zathura_t* zathura, zathura_page_t* page) cairo_restore(cairo); cairo_save(cairo); - if (fabs(zathura->document->scale - 1.0f) > FLT_EPSILON) { - cairo_scale(cairo, zathura->document->scale, zathura->document->scale); + double scale = zathura_document_get_scale(zathura->document); + if (fabs(scale - 1.0f) > FLT_EPSILON) { + cairo_scale(cairo, scale, scale); } if (zathura_page_render(page, cairo, false) != ZATHURA_ERROR_OK) { @@ -177,8 +178,9 @@ render_all(zathura_t* zathura) } /* unmark all pages */ - for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++) { - zathura_page_t* page = zathura->document->pages[page_id]; + unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); + for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { + zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); unsigned int page_height = 0, page_width = 0; page_calc_height_width(page, &page_height, &page_width, true); diff --git a/shortcuts.c b/shortcuts.c index 3a0df7f..d1bcca1 100644 --- a/shortcuts.c +++ b/shortcuts.c @@ -20,12 +20,17 @@ static void readjust_view_after_zooming(zathura_t *zathura, float old_zoom) { + if (zathura == NULL || zathura->document == NULL) { + return; + } + 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; + double scale = zathura_document_get_scale(zathura->document); + gdouble valx = gtk_adjustment_get_value(hadjustment) / old_zoom * scale; + gdouble valy = gtk_adjustment_get_value(vadjustment) / old_zoom * scale; set_adjustment(hadjustment, valx); set_adjustment(vadjustment, valy); } @@ -39,8 +44,9 @@ sc_abort(girara_session_t* session, girara_argument_t* UNUSED(argument), zathura_t* zathura = session->global.data; if (zathura->document != NULL) { - for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; ++page_id) { - zathura_page_t* page = zathura->document->pages[page_id]; + unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); + for (unsigned int page_id = 0; page_id < number_of_pages; ++page_id) { + zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); if (page == NULL) { continue; } @@ -71,9 +77,9 @@ sc_adjust_window(girara_session_t* session, girara_argument_t* argument, goto error_ret; } - float old_zoom = zathura->document->scale; - zathura->document->adjust_mode = argument->n; - if (argument->n == ADJUST_NONE) { + float old_zoom = zathura_document_get_scale(zathura->document); + zathura_document_set_adjust_mode(zathura->document, argument->n); + if (argument->n == ZATHURA_ADJUST_NONE) { /* there is nothing todo */ goto error_ret; } @@ -88,12 +94,13 @@ sc_adjust_window(girara_session_t* session, girara_argument_t* argument, double total_width = 0; double max_height = 0; + unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); for (unsigned int page_id = 0; page_id < pages_per_row; page_id++) { - if (page_id == zathura->document->number_of_pages) { + if (page_id == number_of_pages) { break; } - zathura_page_t* page = zathura->document->pages[page_id]; + zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); if (page == NULL) { goto error_ret; } @@ -105,10 +112,10 @@ sc_adjust_window(girara_session_t* session, girara_argument_t* argument, total_width += zathura_page_get_width(page); } - if (argument->n == ADJUST_WIDTH) { - zathura->document->scale = width / total_width; - } else if (argument->n == ADJUST_BESTFIT) { - zathura->document->scale = height / max_height; + if (argument->n == ZATHURA_ADJUST_WIDTH) { + zathura_document_set_scale(zathura->document, width / total_width); + } else if (argument->n == ZATHURA_ADJUST_BESTFIT) { + zathura_document_set_scale(zathura->document, height / max_height); } else { goto error_ret; } @@ -159,22 +166,20 @@ sc_focus_inputbar(girara_session_t* session, girara_argument_t* argument, girara /* append filepath */ if (argument->n == APPEND_FILEPATH && zathura->document != NULL) { - char* file_path = g_strdup(zathura->document->file_path); - if (file_path == NULL) { - return false; - } + const char* file_path = zathura_document_get_path(zathura->document); + if (file_path == NULL) { + return false; + } - char* path = dirname(file_path); + char* path = dirname(g_strdup(file_path)); char* tmp = g_strdup_printf("%s%s/", (char*) argument->data, (g_strcmp0(path, "/") == 0) ? "" : path); if (tmp == NULL) { - g_free(file_path); return false; } gtk_entry_set_text(session->gtk.inputbar_entry, tmp); g_free(tmp); - g_free(file_path); } /* we save the X clipboard that will be clear by "grab_focus" */ @@ -207,8 +212,9 @@ sc_follow(girara_session_t* session, girara_argument_t* UNUSED(argument), /* set pages to draw links */ bool show_links = false; unsigned int page_offset = 0; - for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++) { - zathura_page_t* page = zathura->document->pages[page_id]; + unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); + for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { + zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); if (page == NULL) { continue; } @@ -249,15 +255,16 @@ sc_goto(girara_session_t* session, girara_argument_t* argument, girara_event_t* if (t != 0) { /* add offset */ - if (zathura->document->page_offset > 0) { - t += zathura->document->page_offset; + unsigned int page_offset = zathura_document_get_page_offset(zathura->document); + if (page_offset > 0) { + t += page_offset; } page_set(zathura, t - 1); } else if (argument->n == TOP) { page_set(zathura, 0); } else if (argument->n == BOTTOM) { - page_set(zathura, zathura->document->number_of_pages - 1); + page_set(zathura, zathura_document_get_number_of_pages(zathura->document) - 1); } return false; @@ -360,8 +367,8 @@ sc_navigate(girara_session_t* session, girara_argument_t* argument, g_return_val_if_fail(argument != NULL, false); g_return_val_if_fail(zathura->document != NULL, false); - int number_of_pages = zathura->document->number_of_pages; - int new_page = zathura->document->current_page_number; + int number_of_pages = zathura_document_get_number_of_pages(zathura->document); + int new_page = zathura_document_get_current_page_number(zathura->document); bool scroll_wrap = false; girara_setting_get(session, "scroll-wrap", &scroll_wrap); @@ -434,7 +441,7 @@ sc_rotate(girara_session_t* session, girara_argument_t* argument, zathura_t* zathura = session->global.data; g_return_val_if_fail(zathura->document != NULL, false); - unsigned int page_number = zathura->document->current_page_number; + unsigned int page_number = zathura_document_get_current_page_number(zathura->document); int angle = 90; if (argument != NULL && argument->n == ROTATE_CCW) { @@ -442,7 +449,8 @@ sc_rotate(girara_session_t* session, girara_argument_t* argument, } /* update rotate value */ - zathura->document->rotate = (zathura->document->rotate + angle) % 360; + unsigned int rotation = zathura_document_get_rotation(zathura->document); + zathura_document_set_rotation(zathura->document, (rotation + angle) % 360); /* render all pages again */ render_all(zathura); @@ -535,8 +543,8 @@ sc_search(girara_session_t* session, girara_argument_t* argument, g_return_val_if_fail(argument != NULL, false); g_return_val_if_fail(zathura->document != NULL, false); - const int num_pages = zathura->document->number_of_pages; - const int cur_page = zathura->document->current_page_number; + const int num_pages = zathura_document_get_number_of_pages(zathura->document); + const int cur_page = zathura_document_get_current_page_number(zathura->document); int diff = argument->n == FORWARD ? 1 : -1; zathura_page_t* target_page = NULL; @@ -544,7 +552,7 @@ sc_search(girara_session_t* session, girara_argument_t* argument, for (int page_id = 0; page_id < num_pages; ++page_id) { int tmp = cur_page + diff * page_id; - zathura_page_t* page = zathura->document->pages[(tmp + num_pages) % num_pages]; + zathura_page_t* page = zathura_document_get_page(zathura->document, (tmp + num_pages) % num_pages); if (page == NULL) { continue; } @@ -571,8 +579,8 @@ sc_search(girara_session_t* session, girara_argument_t* argument, for (int npage_id = 1; page_id < num_pages; ++npage_id) { int ntmp = cur_page + diff * (page_id + npage_id); - zathura_page_t* npage = zathura->document->pages[(ntmp + 2*num_pages) % num_pages]; - zathura->document->current_page_number = zathura_page_get_index(npage); + zathura_page_t* npage = zathura_document_get_page(zathura->document, (ntmp + 2*num_pages) % num_pages); + zathura_document_set_current_page_number(zathura->document, zathura_page_get_index(npage)); GtkWidget* npage_page_widget = zathura_page_get_widget(npage); g_object_get(npage_page_widget, "search-length", &num_search_results, NULL); if (num_search_results != 0) { @@ -848,9 +856,9 @@ sc_toggle_fullscreen(girara_session_t* session, girara_argument_t* gtk_window_unfullscreen(GTK_WINDOW(session->gtk.window)); /* reset scale */ - zathura->document->scale = zoom; + zathura_document_set_scale(zathura->document, zoom); render_all(zathura); - page_set_delayed(zathura, zathura->document->current_page_number); + page_set_delayed(zathura, zathura_document_get_current_page_number(zathura->document)); } else { /* backup pages per row */ girara_setting_get(session, "pages-per-row", &pages_per_row); @@ -860,10 +868,10 @@ sc_toggle_fullscreen(girara_session_t* session, girara_argument_t* girara_setting_set(session, "pages-per-row", &int_value); /* back up zoom */ - zoom = zathura->document->scale; + zoom = zathura_document_get_scale(zathura->document); /* adjust window */ - girara_argument_t argument = { ADJUST_BESTFIT, NULL }; + girara_argument_t argument = { ZATHURA_ADJUST_BESTFIT, NULL }; sc_adjust_window(session, &argument, NULL, 0); /* hide status and inputbar */ @@ -872,7 +880,7 @@ sc_toggle_fullscreen(girara_session_t* session, girara_argument_t* /* set full screen */ gtk_window_fullscreen(GTK_WINDOW(session->gtk.window)); - page_set_delayed(zathura, zathura->document->current_page_number); + page_set_delayed(zathura, zathura_document_get_current_page_number(zathura->document)); } fullscreen = fullscreen ? false : true; @@ -904,24 +912,24 @@ sc_zoom(girara_session_t* session, girara_argument_t* argument, girara_event_t* g_return_val_if_fail(argument != NULL, false); g_return_val_if_fail(zathura->document != NULL, false); - zathura->document->adjust_mode = ADJUST_NONE; + zathura_document_set_adjust_mode(zathura->document, ZATHURA_ADJUST_NONE); /* retreive zoom step value */ int value = 1; girara_setting_get(zathura->ui.session, "zoom-step", &value); float zoom_step = value / 100.0f; - float old_zoom = zathura->document->scale; + float old_zoom = zathura_document_get_scale(zathura->document); /* specify new zoom value */ if (argument->n == ZOOM_IN) { - zathura->document->scale += zoom_step; + zathura_document_set_scale(zathura->document, old_zoom + zoom_step); } else if (argument->n == ZOOM_OUT) { - zathura->document->scale -= zoom_step; + zathura_document_set_scale(zathura->document, old_zoom - zoom_step); } else if (argument->n == ZOOM_SPECIFIC) { - zathura->document->scale = t / 100.0f; + zathura_document_set_scale(zathura->document, t / 100.0f); } else { - zathura->document->scale = 1.0; + zathura_document_set_scale(zathura->document, 1.0); } /* zoom limitations */ @@ -933,10 +941,11 @@ sc_zoom(girara_session_t* session, girara_argument_t* argument, girara_event_t* float zoom_min = zoom_min_int * 0.01f; float zoom_max = zoom_max_int * 0.01f; - if (zathura->document->scale < zoom_min) { - zathura->document->scale = zoom_min; - } else if (zathura->document->scale > zoom_max) { - zathura->document->scale = zoom_max; + float scale = zathura_document_get_scale(zathura->document); + if (scale < zoom_min) { + zathura_document_set_scale(zathura->document, zoom_min); + } else if (scale > zoom_max) { + zathura_document_set_scale(zathura->document, zoom_max); } /* keep position */ diff --git a/types.h b/types.h index 13bf164..1a520c5 100644 --- a/types.h +++ b/types.h @@ -34,6 +34,16 @@ typedef struct zathura_image_buffer_s unsigned int rowstride; /**< Rowstride of the image */ } zathura_image_buffer_t; +/** + * Adjust mode + */ +typedef enum zathura_adjust_mode_e +{ + ZATHURA_ADJUST_NONE, /**< No adjustment */ + ZATHURA_ADJUST_BESTFIT, /**< Adjust to best-fit */ + ZATHURA_ADJUST_WIDTH /**< Adjust to width */ +} zathura_adjust_mode_t; + /** * Creates an image buffer * diff --git a/utils.c b/utils.c index d1842d2..642253a 100644 --- a/utils.c +++ b/utils.c @@ -11,6 +11,7 @@ #include "utils.h" #include "zathura.h" +#include "internal.h" #include "document.h" #include "page.h" #include "plugin.h" @@ -177,7 +178,7 @@ page_calculate_offset(zathura_page_t* page, page_offset_t* offset) g_return_if_fail(page != NULL); g_return_if_fail(offset != NULL); zathura_document_t* document = zathura_page_get_document(page); - zathura_t* zathura = document->zathura; + zathura_t* zathura = zathura_document_get_zathura(document); GtkWidget* widget = zathura_page_get_widget(page); g_return_if_fail(gtk_widget_translate_coordinates(widget, @@ -231,33 +232,34 @@ recalc_rectangle(zathura_page_t* page, zathura_rectangle_t rectangle) double page_height = zathura_page_get_height(page); double page_width = zathura_page_get_width(page); + double scale = zathura_document_get_scale(document); zathura_rectangle_t tmp; - switch (document->rotate) { + switch (zathura_document_get_rotation(document)) { case 90: - tmp.x1 = (page_height - rectangle.y2) * document->scale; - tmp.x2 = (page_height - rectangle.y1) * document->scale; - tmp.y1 = rectangle.x1 * document->scale; - tmp.y2 = rectangle.x2 * document->scale; + tmp.x1 = (page_height - rectangle.y2) * scale; + tmp.x2 = (page_height - rectangle.y1) * scale; + tmp.y1 = rectangle.x1 * scale; + tmp.y2 = rectangle.x2 * scale; break; case 180: - tmp.x1 = (page_width - rectangle.x2) * document->scale; - tmp.x2 = (page_width - rectangle.x1) * document->scale; - tmp.y1 = (page_height - rectangle.y2) * document->scale; - tmp.y2 = (page_height - rectangle.y1) * document->scale; + tmp.x1 = (page_width - rectangle.x2) * scale; + tmp.x2 = (page_width - rectangle.x1) * scale; + tmp.y1 = (page_height - rectangle.y2) * scale; + tmp.y2 = (page_height - rectangle.y1) * scale; break; case 270: - tmp.x1 = rectangle.y1 * document->scale; - tmp.x2 = rectangle.y2 * document->scale; - tmp.y1 = (page_width - rectangle.x2) * document->scale; - tmp.y2 = (page_width - rectangle.x1) * document->scale; + tmp.x1 = rectangle.y1 * scale; + tmp.x2 = rectangle.y2 * scale; + tmp.y1 = (page_width - rectangle.x2) * scale; + tmp.y2 = (page_width - rectangle.x1) * scale; break; default: - tmp.x1 = rectangle.x1 * document->scale; - tmp.x2 = rectangle.x2 * document->scale; - tmp.y1 = rectangle.y1 * document->scale; - tmp.y2 = rectangle.y2 * document->scale; + tmp.x1 = rectangle.x1 * scale; + tmp.x2 = rectangle.x2 * scale; + tmp.y1 = rectangle.y1 * scale; + tmp.y2 = rectangle.y2 * scale; } return tmp; @@ -286,12 +288,13 @@ page_calc_height_width(zathura_page_t* page, unsigned int* page_height, unsigned double height = zathura_page_get_height(page); double width = zathura_page_get_width(page); + double scale = zathura_document_get_scale(document); - if (rotate && document->rotate % 180) { - *page_width = ceil(height * document->scale); - *page_height = ceil(width * document->scale); + if (rotate && zathura_document_get_rotation(document) % 180) { + *page_width = ceil(height * scale); + *page_height = ceil(width * scale); } else { - *page_width = ceil(width * document->scale); - *page_height = ceil(height * document->scale); + *page_width = ceil(width * scale); + *page_height = ceil(height * scale); } } diff --git a/zathura.c b/zathura.c index fe6fc37..58f7a8f 100644 --- a/zathura.c +++ b/zathura.c @@ -444,7 +444,8 @@ document_open(zathura_t* zathura, const char* path, const char* password) } /* install file monitor */ - gchar* file_uri = g_filename_to_uri(document->file_path, NULL, NULL); + const char* file_path = zathura_document_get_path(document); + gchar* file_uri = g_filename_to_uri(file_path, NULL, NULL); if (file_uri == NULL) { goto error_free; } @@ -465,15 +466,15 @@ document_open(zathura_t* zathura, const char* path, const char* password) } if (zathura->file_monitor.file_path == NULL) { - zathura->file_monitor.file_path = g_strdup(document->file_path); + zathura->file_monitor.file_path = g_strdup(file_path); if (zathura->file_monitor.file_path == NULL) { goto error_free; } } - if (document->password != NULL) { + if (password != NULL) { g_free(zathura->file_monitor.password); - zathura->file_monitor.password = g_strdup(document->password); + zathura->file_monitor.password = g_strdup(password); if (zathura->file_monitor.password == NULL) { goto error_free; } @@ -496,8 +497,9 @@ document_open(zathura_t* zathura, const char* path, const char* password) } /* create blank pages */ - for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) { - zathura_page_t* page = document->pages[page_id]; + unsigned int number_of_pages = zathura_document_get_number_of_pages(document); + for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { + zathura_page_t* page = zathura_document_get_page(document, page_id); if (page != NULL) { GtkWidget* page_widget = zathura_page_get_widget(page); if (page_widget != NULL) { @@ -507,18 +509,18 @@ document_open(zathura_t* zathura, const char* path, const char* password) } /* bookmarks */ - zathura_bookmarks_load(zathura, zathura->document->file_path); + zathura_bookmarks_load(zathura, file_path); - page_set_delayed(zathura, document->current_page_number); + page_set_delayed(zathura, zathura_document_get_current_page_number(document)); cb_view_vadjustment_value_changed(NULL, zathura); /* update title */ - girara_set_window_title(zathura->ui.session, document->file_path); + girara_set_window_title(zathura->ui.session, file_path); free(file_uri); /* adjust window */ - girara_argument_t argument = { zathura->document->adjust_mode, NULL }; + girara_argument_t argument = { zathura_document_get_adjust_mode(document), NULL }; sc_adjust_window(zathura->ui.session, &argument, NULL, 0); return true; @@ -598,10 +600,15 @@ document_close(zathura_t* zathura, bool keep_monitor) } } + const char* path = zathura_document_get_path(zathura->document); + unsigned int current_page_number = zathura_document_get_current_page_number(zathura->document); + unsigned int page_offset = zathura_document_get_page_offset(zathura->document); + double scale = zathura_document_get_scale(zathura->document); + unsigned int rotation = zathura_document_get_rotation(zathura->document); + /* store last seen page */ - zathura_db_set_fileinfo(zathura->database, zathura->document->file_path, - zathura->document->current_page_number, zathura->document->page_offset, - zathura->document->scale, zathura->document->rotate); + zathura_db_set_fileinfo(zathura->database, path, current_page_number, + page_offset, scale, rotation); render_free(zathura->sync.render_thread); zathura->sync.render_thread = NULL; @@ -644,8 +651,8 @@ page_set_delayed_impl(gpointer data) bool page_set_delayed(zathura_t* zathura, unsigned int page_id) { - if (zathura == NULL || zathura->document == NULL || zathura->document->pages == NULL || - page_id >= zathura->document->number_of_pages) { + if (zathura == NULL || zathura->document == NULL || + (page_id >= zathura_document_get_number_of_pages(zathura->document))) { return false; } @@ -659,22 +666,18 @@ page_set_delayed(zathura_t* zathura, unsigned int page_id) bool page_set(zathura_t* zathura, unsigned int page_id) { - if (zathura == NULL || zathura->document == NULL || zathura->document->pages == NULL) { - goto error_out; - } - - if (page_id >= zathura->document->number_of_pages) { + if (zathura == NULL || zathura->document == NULL) { goto error_out; } /* render page */ - zathura_page_t* page = zathura->document->pages[page_id]; + zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); if (page == NULL) { goto error_out; } - zathura->document->current_page_number = page_id; + zathura_document_set_current_page_number(zathura->document, page_id); zathura->global.update_page_number = false; page_offset_t offset; @@ -701,8 +704,11 @@ statusbar_page_number_update(zathura_t* zathura) return; } + unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); + unsigned int current_page_number = zathura_document_get_current_page_number(zathura->document); + if (zathura->document != NULL) { - char* page_number_text = g_strdup_printf("[%d/%d]", zathura->document->current_page_number + 1, zathura->document->number_of_pages); + char* page_number_text = g_strdup_printf("[%d/%d]", current_page_number + 1, number_of_pages); girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.page_number, page_number_text); g_free(page_number_text); } else { @@ -724,13 +730,15 @@ 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); - gtk_table_resize(GTK_TABLE(zathura->ui.page_widget), ceil(zathura->document->number_of_pages / pages_per_row), pages_per_row); - for (unsigned int i = 0; i < zathura->document->number_of_pages; i++) + 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); + for (unsigned int i = 0; i < number_of_pages; i++) { int x = i % pages_per_row; int y = i / pages_per_row; - GtkWidget* page_widget = zathura_page_get_widget(zathura->document->pages[i]); + zathura_page_t* page = zathura_document_get_page(zathura->document, i); + GtkWidget* page_widget = zathura_page_get_widget(page); gtk_table_attach(GTK_TABLE(zathura->ui.page_widget), page_widget, x, x + 1, y, y + 1, GTK_SHRINK, GTK_SHRINK, 0, 0); } @@ -751,8 +759,9 @@ gboolean purge_pages(gpointer data) return TRUE; } - for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++) { - zathura_page_t* page = zathura->document->pages[page_id]; + unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document); + for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { + zathura_page_t* page = zathura_document_get_page(zathura->document, page_id); GtkWidget* page_widget = zathura_page_get_widget(page); zathura_page_widget_purge_unused(ZATHURA_PAGE(page_widget), threshold); } diff --git a/zathura.h b/zathura.h index fc56686..17d581c 100644 --- a/zathura.h +++ b/zathura.h @@ -11,11 +11,11 @@ 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, - BACKWARD, ADJUST_BESTFIT, ADJUST_WIDTH, ADJUST_NONE, CONTINUOUS, DELETE_LAST, - ADD_MARKER, EVAL_MARKER, EXPAND, EXPAND_ALL, COLLAPSE_ALL, COLLAPSE, SELECT, - GOTO_DEFAULT, GOTO_LABELS, GOTO_OFFSET, HALF_UP, HALF_DOWN, FULL_UP, - FULL_DOWN, HALF_LEFT, HALF_RIGHT, FULL_LEFT, FULL_RIGHT, NEXT_CHAR, PREVIOUS_CHAR, - DELETE_TO_LINE_START, APPEND_FILEPATH, ROTATE_CW, ROTATE_CCW }; + BACKWARD, CONTINUOUS, DELETE_LAST, ADD_MARKER, EVAL_MARKER, EXPAND, + EXPAND_ALL, COLLAPSE_ALL, COLLAPSE, SELECT, GOTO_DEFAULT, GOTO_LABELS, + GOTO_OFFSET, HALF_UP, HALF_DOWN, FULL_UP, FULL_DOWN, HALF_LEFT, HALF_RIGHT, + FULL_LEFT, FULL_RIGHT, NEXT_CHAR, PREVIOUS_CHAR, DELETE_TO_LINE_START, + APPEND_FILEPATH, ROTATE_CW, ROTATE_CCW }; /* forward declaration for types from document.h */ struct zathura_document_s;