From 4c25d68777223d14a8031a78d7794996fbbfed15 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Sun, 6 May 2012 23:41:25 +0200 Subject: [PATCH] Restructure zathura_page_widget_private_s --- page-widget.c | 296 +++++++++++++++++++++++++++----------------------- 1 file changed, 159 insertions(+), 137 deletions(-) diff --git a/page-widget.c b/page-widget.c index 5299889..cf5dce8 100644 --- a/page-widget.c +++ b/page-widget.c @@ -16,27 +16,39 @@ G_DEFINE_TYPE(ZathuraPage, zathura_page_widget, GTK_TYPE_DRAWING_AREA) typedef struct zathura_page_widget_private_s { - zathura_page_t* page; - zathura_t* zathura; + zathura_page_t* page; /**< Page object */ + zathura_t* zathura; /**< Zathura object */ cairo_surface_t* surface; /**< Cairo surface */ + gint64 last_view; /**< Last time the page has been viewed */ GStaticMutex lock; /**< Lock */ - girara_list_t* links; /**< List of links on the page */ - bool links_got; /**< True if we already tried to retrieve the list of links */ - bool draw_links; /**< True if links should be drawn */ - unsigned int link_offset; /**< Offset to the links */ - unsigned int number_of_links; /**< Offset to the links */ - girara_list_t* search_results; /**< A list if there are search results that should be drawn */ - int search_current; /**< The index of the current search result */ - bool draw_search_results; /**< Draw search results */ - zathura_rectangle_t selection; /**< Region selected with the mouse */ + struct { - int x; - int y; - } selection_basepoint; - girara_list_t* images; /**< List of images on the page */ - bool images_got; /**< True if we already tried to retrieve the list of images */ - zathura_image_t* current_image; /**< Image data of selected image */ - gint64 last_view; + girara_list_t* list; /**< List of links on the page */ + bool retrieved; /**< True if we already tried to retrieve the list of links */ + bool draw; /**< True if links should be drawn */ + unsigned int offset; /**< Offset to the links */ + unsigned int n; /**< Number */ + } links; + + struct { + girara_list_t* list; /**< A list if there are search results that should be drawn */ + int current; /**< The index of the current search result */ + bool draw; /**< Draw search results */ + } search; + + struct { + girara_list_t* list; /**< List of images on the page */ + bool retrieved; /**< True if we already tried to retrieve the list of images */ + zathura_image_t* current; /**< Image data of selected image */ + } images; + + struct { + zathura_rectangle_t selection; /**< Region selected with the mouse */ + struct { + int x; /**< X coordinate */ + int y; /**< Y coordinate */ + } selection_basepoint; + } mouse; } zathura_page_widget_private_t; #define ZATHURA_PAGE_GET_PRIVATE(obj) \ @@ -68,11 +80,10 @@ enum properties_e PROP_DRAW_LINKS, PROP_LINKS_OFFSET, PROP_LINKS_NUMBER, - PROP_SEARCH_RESULT, PROP_SEARCH_RESULTS, PROP_SEARCH_RESULTS_LENGTH, PROP_SEARCH_RESULTS_CURRENT, - PROP_DRAW_SEARCH_RESULTS + PROP_DRAW_SEACH_RESULTS }; static void @@ -116,7 +127,7 @@ zathura_page_widget_class_init(ZathuraPageClass* class) g_param_spec_int("search-current", "search-current", "The current search result", -1, INT_MAX, 0, G_PARAM_WRITABLE | G_PARAM_READABLE)); g_object_class_install_property(object_class, PROP_SEARCH_RESULTS_LENGTH, g_param_spec_int("search-length", "search-length", "The number of search results", -1, INT_MAX, 0, G_PARAM_READABLE)); - g_object_class_install_property(object_class, PROP_DRAW_SEARCH_RESULTS, + g_object_class_install_property(object_class, PROP_DRAW_SEACH_RESULTS, g_param_spec_boolean("draw-search-results", "draw-search-results", "Set to true if search results should be drawn", FALSE, G_PARAM_WRITABLE)); } @@ -124,21 +135,29 @@ static void zathura_page_widget_init(ZathuraPage* widget) { zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget); - priv->page = NULL; - priv->surface = NULL; - priv->links = NULL; - priv->links_got = false; - priv->link_offset = 0; - priv->search_results = NULL; - priv->search_current = INT_MAX; - priv->selection.x1 = -1; - priv->selection_basepoint.x = -1; - priv->selection_basepoint.y = -1; - priv->images = NULL; - priv->images_got = false; - priv->current_image = NULL; - priv->last_view = g_get_real_time(); - priv->draw_search_results = true; + priv->page = NULL; + priv->surface = NULL; + priv->last_view = g_get_real_time(); + + priv->links.list = NULL; + priv->links.retrieved = false; + priv->links.draw = false; + priv->links.offset = 0; + priv->links.n = 0; + + priv->search.list = NULL; + priv->search.current = INT_MAX; + priv->search.draw = true; + + priv->images.list = NULL; + priv->images.retrieved = false; + priv->images.current = NULL; + + priv->mouse.selection.x1 = -1; + priv->mouse.selection.y1 = -1; + priv->mouse.selection_basepoint.x = -1; + priv->mouse.selection_basepoint.y = -1; + g_static_mutex_init(&(priv->lock)); /* we want mouse events */ @@ -164,12 +183,12 @@ zathura_page_widget_finalize(GObject* object) cairo_surface_destroy(priv->surface); } - if (priv->search_results != NULL) { - girara_list_free(priv->search_results); + if (priv->search.list != NULL) { + girara_list_free(priv->search.list); } - if (priv->links != NULL) { - girara_list_free(priv->links); + if (priv->links.list != NULL) { + girara_list_free(priv->links.list); } g_static_mutex_free(&(priv->lock)); @@ -191,60 +210,60 @@ zathura_page_widget_set_property(GObject* object, guint prop_id, const GValue* v priv->zathura = g_value_get_pointer(value); break; case PROP_DRAW_LINKS: - priv->draw_links = g_value_get_boolean(value); + priv->links.draw = g_value_get_boolean(value); /* get links */ - if (priv->draw_links == true && priv->links_got == false) { - priv->links = zathura_page_links_get(priv->page, NULL); - priv->links_got = true; - priv->number_of_links = (priv->links == NULL) ? 0 : girara_list_size(priv->links); + if (priv->links.draw == true && priv->links.retrieved == false) { + priv->links.list = zathura_page_links_get(priv->page, NULL); + priv->links.retrieved = true; + priv->links.n = (priv->links.list == NULL) ? 0 : girara_list_size(priv->links.list); } - if (priv->links_got == true && priv->links != NULL) { - GIRARA_LIST_FOREACH(priv->links, zathura_link_t*, iter, link) + if (priv->links.retrieved == true && priv->links.list != NULL) { + GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link) if (link != NULL) { zathura_rectangle_t rectangle = recalc_rectangle(priv->page, link->position); redraw_rect(pageview, &rectangle); } - GIRARA_LIST_FOREACH_END(priv->links, zathura_link_t*, iter, link); + GIRARA_LIST_FOREACH_END(priv->links.list, zathura_link_t*, iter, link); } break; case PROP_LINKS_OFFSET: - priv->link_offset = g_value_get_int(value); + priv->links.offset = g_value_get_int(value); break; case PROP_SEARCH_RESULTS: - if (priv->search_results != NULL && priv->draw_search_results) { - redraw_all_rects(pageview, priv->search_results); - girara_list_free(priv->search_results); + if (priv->search.list != NULL && priv->search.draw) { + redraw_all_rects(pageview, priv->search.list); + girara_list_free(priv->search.list); } - priv->search_results = g_value_get_pointer(value); - if (priv->search_results != NULL && priv->draw_search_results) { - priv->draw_links = false; - redraw_all_rects(pageview, priv->search_results); + priv->search.list = g_value_get_pointer(value); + if (priv->search.list != NULL && priv->search.draw) { + priv->links.draw = false; + redraw_all_rects(pageview, priv->search.list); } - priv->search_current = -1; + priv->search.current = -1; break; case PROP_SEARCH_RESULTS_CURRENT: { - g_return_if_fail(priv->search_results != NULL); - if (priv->search_current >= 0 && priv->search_current < (signed) girara_list_size(priv->search_results)) { - zathura_rectangle_t* rect = girara_list_nth(priv->search_results, priv->search_current); + g_return_if_fail(priv->search.list != NULL); + if (priv->search.current >= 0 && priv->search.current < (signed) girara_list_size(priv->search.list)) { + zathura_rectangle_t* rect = girara_list_nth(priv->search.list, priv->search.current); zathura_rectangle_t rectangle = recalc_rectangle(priv->page, *rect); redraw_rect(pageview, &rectangle); } int val = g_value_get_int(value); if (val < 0) { - priv->search_current = girara_list_size(priv->search_results); + priv->search.current = girara_list_size(priv->search.list); } else { - priv->search_current = val; - zathura_rectangle_t* rect = girara_list_nth(priv->search_results, priv->search_current); + priv->search.current = val; + zathura_rectangle_t* rect = girara_list_nth(priv->search.list, priv->search.current); zathura_rectangle_t rectangle = recalc_rectangle(priv->page, *rect); - if (priv->draw_search_results) { + if (priv->search.draw) { redraw_rect(pageview, &rectangle); } } break; } - case PROP_DRAW_SEARCH_RESULTS: - priv->draw_search_results = g_value_get_boolean(value); + case PROP_DRAW_SEACH_RESULTS: + priv->search.draw = g_value_get_boolean(value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); @@ -259,16 +278,16 @@ zathura_page_widget_get_property(GObject* object, guint prop_id, GValue* value, switch (prop_id) { case PROP_LINKS_NUMBER: - g_value_set_int(value, priv->number_of_links); + g_value_set_int(value, priv->links.n); break; case PROP_SEARCH_RESULTS_LENGTH: - g_value_set_int(value, priv->search_results == NULL ? 0 : girara_list_size(priv->search_results)); + g_value_set_int(value, priv->search.list == NULL ? 0 : girara_list_size(priv->search.list)); break; case PROP_SEARCH_RESULTS_CURRENT: - g_value_set_int(value, priv->search_results == NULL ? -1 : priv->search_current); + g_value_set_int(value, priv->search.list == NULL ? -1 : priv->search.current); break; case PROP_SEARCH_RESULTS: - g_value_set_pointer(value, priv->search_results); + g_value_set_pointer(value, priv->search.list); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); @@ -350,9 +369,9 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) g_free(font); /* draw links */ - if (priv->draw_links == true && priv->number_of_links != 0) { + if (priv->links.draw == true && priv->links.n != 0) { unsigned int link_counter = 0; - GIRARA_LIST_FOREACH(priv->links, zathura_link_t*, iter, link) + GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link) if (link != NULL) { zathura_rectangle_t rectangle = recalc_rectangle(priv->page, link->position); @@ -367,21 +386,21 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) cairo_set_source_rgba(cairo, 0, 0, 0, 1); cairo_set_font_size(cairo, 10); cairo_move_to(cairo, rectangle.x1 + 1, rectangle.y2 - 1); - char* link_number = g_strdup_printf("%i", priv->link_offset + ++link_counter); + char* link_number = g_strdup_printf("%i", priv->links.offset + ++link_counter); cairo_show_text(cairo, link_number); g_free(link_number); } - GIRARA_LIST_FOREACH_END(priv->links, zathura_link_t*, iter, link); + GIRARA_LIST_FOREACH_END(priv->links.list, zathura_link_t*, iter, link); } /* draw search results */ - if (priv->search_results != NULL && priv->draw_search_results == true) { + if (priv->search.list != NULL && priv->search.draw == true) { int idx = 0; - GIRARA_LIST_FOREACH(priv->search_results, zathura_rectangle_t*, iter, rect) + GIRARA_LIST_FOREACH(priv->search.list, zathura_rectangle_t*, iter, rect) zathura_rectangle_t rectangle = recalc_rectangle(priv->page, *rect); /* draw position */ - if (idx == priv->search_current) { + if (idx == priv->search.current) { GdkColor color = priv->zathura->ui.colors.highlight_color_active; cairo_set_source_rgba(cairo, color.red, color.green, color.blue, transparency); } else { @@ -392,14 +411,14 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) (rectangle.x2 - rectangle.x1), (rectangle.y2 - rectangle.y1)); cairo_fill(cairo); ++idx; - GIRARA_LIST_FOREACH_END(priv->search_results, zathura_rectangle_t*, iter, rect); + GIRARA_LIST_FOREACH_END(priv->search.list, zathura_rectangle_t*, iter, rect); } /* draw selection */ - if (priv->selection.y2 != -1 && priv->selection.x2 != -1) { + if (priv->mouse.selection.y2 != -1 && priv->mouse.selection.x2 != -1) { GdkColor color = priv->zathura->ui.colors.highlight_color; cairo_set_source_rgba(cairo, color.red, color.green, color.blue, transparency); - cairo_rectangle(cairo, priv->selection.x1, priv->selection.y1, - (priv->selection.x2 - priv->selection.x1), (priv->selection.y2 - priv->selection.y1)); + cairo_rectangle(cairo, priv->mouse.selection.x1, priv->mouse.selection.y1, + (priv->mouse.selection.x2 - priv->mouse.selection.x1), (priv->mouse.selection.y2 - priv->mouse.selection.y1)); cairo_fill(cairo); } } else { @@ -494,9 +513,9 @@ zathura_page_widget_link_get(ZathuraPage* widget, unsigned int index) zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget); g_return_val_if_fail(priv != NULL, NULL); - if (priv->links != NULL && index >= priv->link_offset && - girara_list_size(priv->links) > index - priv->link_offset) { - return girara_list_nth(priv->links, index - priv->link_offset); + if (priv->links.list != NULL && index >= priv->links.offset && + girara_list_size(priv->links.list) > index - priv->links.offset) { + return girara_list_nth(priv->links.list, index - priv->links.offset); } else { return NULL; } @@ -513,20 +532,22 @@ cb_zathura_page_widget_button_press_event(GtkWidget* widget, GdkEventButton* but if (button->button == 1) { /* left click */ if (button->type == GDK_BUTTON_PRESS) { /* start the selection */ - priv->selection_basepoint.x = button->x; - priv->selection_basepoint.y = button->y; - priv->selection.x1 = button->x; - priv->selection.y1 = button->y; - priv->selection.x2 = button->x; - priv->selection.y2 = button->y; + priv->mouse.selection_basepoint.x = button->x; + priv->mouse.selection_basepoint.y = button->y; + + priv->mouse.selection.x1 = button->x; + priv->mouse.selection.y1 = button->y; + priv->mouse.selection.x2 = button->x; + priv->mouse.selection.y2 = button->y; } else if (button->type == GDK_2BUTTON_PRESS || button->type == GDK_3BUTTON_PRESS) { /* abort the selection */ - priv->selection_basepoint.x = -1; - priv->selection_basepoint.y = -1; - priv->selection.x1 = -1; - priv->selection.y1 = -1; - priv->selection.x2 = -1; - priv->selection.y2 = -1; + priv->mouse.selection_basepoint.x = -1; + priv->mouse.selection_basepoint.y = -1; + + priv->mouse.selection.x1 = -1; + priv->mouse.selection.y1 = -1; + priv->mouse.selection.x2 = -1; + priv->mouse.selection.y2 = -1; } return true; @@ -550,27 +571,27 @@ cb_zathura_page_widget_button_release_event(GtkWidget* widget, GdkEventButton* b zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget); zathura_document_t* document = zathura_page_get_document(priv->page); - if (priv->selection.y2 == -1 && priv->selection.x2 == -1 ) { + if (priv->mouse.selection.y2 == -1 && priv->mouse.selection.x2 == -1 ) { /* simple single click */ /* get links */ - if (priv->links_got == false) { - priv->links = zathura_page_links_get(priv->page, NULL); - priv->links_got = true; - priv->number_of_links = (priv->links == NULL) ? 0 : girara_list_size(priv->links); + if (priv->links.retrieved == false) { + priv->links.list = zathura_page_links_get(priv->page, NULL); + priv->links.retrieved = true; + priv->links.n = (priv->links.list == NULL) ? 0 : girara_list_size(priv->links.list); } - if (priv->links != NULL && priv->number_of_links > 0) { - GIRARA_LIST_FOREACH(priv->links, zathura_link_t*, iter, link) + if (priv->links.list != NULL && priv->links.n > 0) { + GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link) zathura_rectangle_t rect = recalc_rectangle(priv->page, link->position); if (rect.x1 <= button->x && rect.x2 >= button->x && rect.y1 <= button->y && rect.y2 >= button->y) { zathura_link_evaluate(priv->zathura, link); } - GIRARA_LIST_FOREACH_END(priv->links, zathura_link_t*, iter, link); + GIRARA_LIST_FOREACH_END(priv->links.list, zathura_link_t*, iter, link); } } else { - redraw_rect(ZATHURA_PAGE(widget), &priv->selection); - zathura_rectangle_t tmp = priv->selection; + redraw_rect(ZATHURA_PAGE(widget), &priv->mouse.selection); + zathura_rectangle_t tmp = priv->mouse.selection; double scale = zathura_document_get_scale(document); tmp.x1 /= scale; @@ -596,12 +617,13 @@ cb_zathura_page_widget_button_release_event(GtkWidget* widget, GdkEventButton* b } } - priv->selection_basepoint.x = -1; - priv->selection_basepoint.y = -1; - priv->selection.x1 = -1; - priv->selection.y1 = -1; - priv->selection.x2 = -1; - priv->selection.y2 = -1; + priv->mouse.selection_basepoint.x = -1; + priv->mouse.selection_basepoint.y = -1; + + priv->mouse.selection.x1 = -1; + priv->mouse.selection.y1 = -1; + priv->mouse.selection.x2 = -1; + priv->mouse.selection.y2 = -1; return false; } @@ -616,25 +638,25 @@ cb_zathura_page_widget_motion_notify(GtkWidget* widget, GdkEventMotion* event) } zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget); - zathura_rectangle_t tmp = priv->selection; - if (event->x < priv->selection_basepoint.x) { + zathura_rectangle_t tmp = priv->mouse.selection; + if (event->x < priv->mouse.selection_basepoint.x) { tmp.x1 = event->x; - tmp.x2 = priv->selection_basepoint.x; + tmp.x2 = priv->mouse.selection_basepoint.x; } else { tmp.x2 = event->x; - tmp.x1 = priv->selection_basepoint.x; + tmp.x1 = priv->mouse.selection_basepoint.x; } - if (event->y < priv->selection_basepoint.y) { + if (event->y < priv->mouse.selection_basepoint.y) { tmp.y1 = event->y; - tmp.y2 = priv->selection_basepoint.y; + tmp.y2 = priv->mouse.selection_basepoint.y; } else { - tmp.y1 = priv->selection_basepoint.y; + tmp.y1 = priv->mouse.selection_basepoint.y; tmp.y2 = event->y; } - redraw_rect(ZATHURA_PAGE(widget), &priv->selection); + redraw_rect(ZATHURA_PAGE(widget), &priv->mouse.selection); redraw_rect(ZATHURA_PAGE(widget), &tmp); - priv->selection = tmp; + priv->mouse.selection = tmp; return false; } @@ -650,29 +672,29 @@ zathura_page_widget_popup_menu(GtkWidget* widget, GdkEventButton* event) return; #endif - if (priv->images_got == false) { - priv->images = zathura_page_images_get(priv->page, NULL); - priv->images_got = true; + if (priv->images.retrieved == false) { + priv->images.list = zathura_page_images_get(priv->page, NULL); + priv->images.retrieved = true; } - if (priv->images == NULL) { + if (priv->images.list == NULL) { return; } /* search for underlaying image */ zathura_image_t* image = NULL; - GIRARA_LIST_FOREACH(priv->images, zathura_image_t*, iter, image_it) + GIRARA_LIST_FOREACH(priv->images.list, zathura_image_t*, iter, image_it) zathura_rectangle_t rect = recalc_rectangle(priv->page, image_it->position); if (rect.x1 <= event->x && rect.x2 >= event->x && rect.y1 <= event->y && rect.y2 >= event->y) { image = image_it; } - GIRARA_LIST_FOREACH_END(priv->images, zathura_image_t*, iter, image_it); + GIRARA_LIST_FOREACH_END(priv->images.list, zathura_image_t*, iter, image_it); if (image == NULL) { return; } - priv->current_image = image; + priv->images.current = image; /* setup menu */ GtkWidget* menu = gtk_menu_new(); @@ -722,9 +744,9 @@ cb_menu_image_copy(GtkMenuItem* item, ZathuraPage* page) g_return_if_fail(item != NULL); g_return_if_fail(page != NULL); zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(page); - g_return_if_fail(priv->current_image != NULL); + g_return_if_fail(priv->images.current != NULL); - cairo_surface_t* surface = zathura_page_image_get_cairo(priv->page, priv->current_image, NULL); + cairo_surface_t* surface = zathura_page_image_get_cairo(priv->page, priv->images.current, NULL); if (surface == NULL) { return; } @@ -746,7 +768,7 @@ cb_menu_image_copy(GtkMenuItem* item, ZathuraPage* page) gtk_clipboard_set_image(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pixbuf); /* reset */ - priv->current_image = NULL; + priv->images.current = NULL; #endif } @@ -756,20 +778,20 @@ cb_menu_image_save(GtkMenuItem* item, ZathuraPage* page) g_return_if_fail(item != NULL); g_return_if_fail(page != NULL); zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(page); - g_return_if_fail(priv->current_image != NULL); - g_return_if_fail(priv->images != NULL); + g_return_if_fail(priv->images.current != NULL); + g_return_if_fail(priv->images.list != NULL); /* generate image identifier */ unsigned int page_id = zathura_page_get_index(priv->page) + 1; unsigned int image_id = 1; - GIRARA_LIST_FOREACH(priv->images, zathura_image_t*, iter, image_it) - if (image_it == priv->current_image) { + GIRARA_LIST_FOREACH(priv->images.list, zathura_image_t*, iter, image_it) + if (image_it == priv->images.current) { break; } image_id++; - GIRARA_LIST_FOREACH_END(priv->images, zathura_image_t*, iter, image_it); + GIRARA_LIST_FOREACH_END(priv->images.list, zathura_image_t*, iter, image_it); /* set command */ char* export_command = g_strdup_printf(":export image-p%d-%d ", page_id, image_id); @@ -778,7 +800,7 @@ cb_menu_image_save(GtkMenuItem* item, ZathuraPage* page) g_free(export_command); /* reset */ - priv->current_image = NULL; + priv->images.current = NULL; } void