diff --git a/page_widget.c b/page_widget.c index 1e99e3e..2aeb627 100644 --- a/page_widget.c +++ b/page_widget.c @@ -42,6 +42,7 @@ 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 @@ -73,7 +74,7 @@ zathura_page_widget_class_init(ZathuraPageClass* class) g_object_class_install_property(object_class, PROP_LINKS_NUMBER, g_param_spec_int("number-of-links", "number-of-links", "Number of links", 0, INT_MAX, 0, G_PARAM_READABLE)); g_object_class_install_property(object_class, PROP_SEARCH_RESULTS, - g_param_spec_pointer("search-results", "search-results", "Set to the list of search results", G_PARAM_WRITABLE)); + g_param_spec_pointer("search-results", "search-results", "Set to the list of search results", G_PARAM_WRITABLE | G_PARAM_READABLE)); g_object_class_install_property(object_class, PROP_SEARCH_RESULTS_CURRENT, 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, @@ -89,6 +90,8 @@ zathura_page_widget_init(ZathuraPage* widget) priv->links = NULL; priv->links_got = false; priv->link_offset = 0; + priv->search_results = NULL; + priv->search_current = INT_MAX; g_static_mutex_init(&(priv->lock)); /* we want mouse events */ @@ -175,13 +178,13 @@ zathura_page_widget_set_property(GObject* object, guint prop_id, const GValue* v break; case PROP_SEARCH_RESULTS_CURRENT: { g_return_if_fail(priv->search_results != NULL); - if (priv->search_current < (signed) girara_list_size(priv->search_results)) { + 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); zathura_rectangle_t rectangle = recalc_rectangle(priv->page, *rect); redraw_rect(pageview, &rectangle); } int val = g_value_get_int(value); - if (val == -1) { + if (val < 0) { priv->search_current = girara_list_size(priv->search_results); } else { priv->search_current = val; @@ -212,6 +215,9 @@ zathura_page_widget_get_property(GObject* object, guint prop_id, GValue* value, case PROP_SEARCH_RESULTS_CURRENT: g_value_set_int(value, priv->search_results == NULL ? -1 : priv->search_current); break; + case PROP_SEARCH_RESULTS: + g_value_set_pointer(value, priv->search_results); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); } diff --git a/shortcuts.c b/shortcuts.c index 735456f..cd44e75 100644 --- a/shortcuts.c +++ b/shortcuts.c @@ -403,6 +403,9 @@ sc_search(girara_session_t* session, girara_argument_t* argument, const int cur_page = zathura->document->current_page_number; int diff = argument->n == FORWARD ? 1 : -1; + zathura_page_t* target_page = NULL; + int target_idx = 0; + 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]; @@ -419,10 +422,13 @@ sc_search(girara_session_t* session, girara_argument_t* argument, if (diff == 1 && current < num_search_results - 1) { /* the next result is on the same page */ - g_object_set(page->drawing_area, "search-current", current + 1, NULL); + target_page = page; + target_idx = current + 1; + // g_object_set(page->drawing_area, "search-current", current + 1, NULL); } else if (diff == -1 && current >= 1) { - g_object_set(page->drawing_area, "search-current", current - 1, NULL); - + // g_object_set(page->drawing_area, "search-current", current - 1, NULL); + target_page = page; + target_idx = current - 1; } else { /* the next result is on a different page */ g_object_set(page->drawing_area, "search-current", -1, NULL); @@ -432,9 +438,8 @@ sc_search(girara_session_t* session, girara_argument_t* argument, zathura_page_t* npage = zathura->document->pages[(ntmp + 2*num_pages) % num_pages]; g_object_get(npage->drawing_area, "search-length", &num_search_results, NULL); if (num_search_results != 0) { - int next = diff == 1 ? 0 : num_search_results - 1; - g_object_set(npage->drawing_area, "search-current", next, NULL); - page_set_delayed(zathura, npage->number); + target_page = npage; + target_idx = diff == 1 ? 0 : num_search_results - 1; break; } } @@ -442,6 +447,25 @@ sc_search(girara_session_t* session, girara_argument_t* argument, break; } + if (target_page != NULL) { + girara_list_t* results = NULL; + g_object_set(target_page->drawing_area, "search-current", target_idx, NULL); + g_object_get(target_page->drawing_area, "search-results", &results, NULL); + + zathura_rectangle_t* rect = girara_list_nth(results, target_idx); + zathura_rectangle_t rectangle = recalc_rectangle(target_page, *rect); + page_offset_t offset; + page_calculate_offset(target_page, &offset); + + GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view)); + GtkAdjustment* view_hadjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view)); + + int x = offset.x - gtk_adjustment_get_page_size(view_hadjustment) / 2 + rectangle.x1; + int y = offset.y - gtk_adjustment_get_page_size(view_vadjustment) / 2 + rectangle.y1; + gtk_adjustment_set_value(view_hadjustment, MAX(view_hadjustment->lower, MIN(view_hadjustment->upper - view_hadjustment->page_size, x))); + gtk_adjustment_set_value(view_vadjustment, MAX(view_vadjustment->lower, MIN(view_vadjustment->upper - view_vadjustment->page_size, y))); + } + return false; }