diff --git a/zathura/callbacks.c b/zathura/callbacks.c index 6fa1cfc..4c23148 100644 --- a/zathura/callbacks.c +++ b/zathura/callbacks.c @@ -408,49 +408,42 @@ handle_link(GtkEntry* entry, girara_session_t* session, index = index - 1; } - /* set pages to draw links */ - bool invalid_index = true; + /* find the link*/ + zathura_link_t* link = NULL; 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) { + if (page == NULL || zathura_page_get_visibility(page) == false || eval == false) { continue; } - GtkWidget* page_widget = zathura_page_get_widget(zathura, page); - g_object_set(G_OBJECT(page_widget), "draw-links", FALSE, NULL); - - if (eval == FALSE) { - /* nothing to evaluate */ - continue; - } - - zathura_link_t* link = zathura_page_widget_link_get(ZATHURA_PAGE(page_widget), index); + link = zathura_page_widget_link_get(ZATHURA_PAGE(page_widget), index); if (link != NULL) { - invalid_index = false; - switch (action) { - case ZATHURA_LINK_ACTION_FOLLOW: - zathura_link_evaluate(zathura, link); - break; - case ZATHURA_LINK_ACTION_DISPLAY: - zathura_link_display(zathura, link); - break; - case ZATHURA_LINK_ACTION_COPY: { - GdkAtom* selection = get_selection(zathura); - if (selection == NULL) { - break; - } - - zathura_link_copy(zathura, link, selection); - g_free(selection); - break; - } - } + break; } } - if (eval == TRUE && invalid_index == true) { + if (eval == TRUE && link == NULL) { girara_notify(session, GIRARA_WARNING, _("Invalid index '%s' given."), input); + } else { + switch (action) { + case ZATHURA_LINK_ACTION_FOLLOW: + zathura_link_evaluate(zathura, link); + break; + case ZATHURA_LINK_ACTION_DISPLAY: + zathura_link_display(zathura, link); + break; + case ZATHURA_LINK_ACTION_COPY: { + GdkAtom* selection = get_selection(zathura); + if (selection == NULL) { + break; + } + + zathura_link_copy(zathura, link, selection); + g_free(selection); + break; + } + } } g_free(input); @@ -834,3 +827,24 @@ cb_gesture_zoom_scale_changed(GtkGestureZoom* UNUSED(self), gdouble scale, void* render_all(zathura); refresh_view(zathura); } + +void cb_hide_links(GtkWidget* widget, gpointer data) { + g_return_if_fail(widget != NULL); + g_return_if_fail(data != NULL); + + /* disconnect from signal */ + gulong handler_id = GPOINTER_TO_UINT(g_object_steal_data(G_OBJECT(widget), "handler_id")); + g_signal_handler_disconnect(G_OBJECT(widget), handler_id); + + zathura_t* zathura = data; + 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; + } + + GtkWidget* page_widget = zathura_page_get_widget(zathura, page); + g_object_set(G_OBJECT(page_widget), "draw-links", FALSE, NULL); + } +} diff --git a/zathura/callbacks.h b/zathura/callbacks.h index c8520ee..786a8e4 100644 --- a/zathura/callbacks.h +++ b/zathura/callbacks.h @@ -285,4 +285,12 @@ void cb_gesture_zoom_begin(GtkGesture* self, GdkEventSequence* sequence, void* d void cb_gesture_zoom_scale_changed(GtkGestureZoom* self, gdouble scale, void* data); +/** + * Clears all highlighted links when the inputbar gets closed + * + * @param GtkWidget* Inputbar widget + * @param data The zathura instance + */ +void cb_hide_links(GtkWidget* widget, gpointer data); + #endif // CALLBACKS_H diff --git a/zathura/shortcuts.c b/zathura/shortcuts.c index 2975db1..8836cd2 100644 --- a/zathura/shortcuts.c +++ b/zathura/shortcuts.c @@ -21,7 +21,7 @@ #include "adjustment.h" #include -/* Helper function; see sc_display_link and sc_follow. */ +/* Helper function for highlighting the links */ static bool draw_links(zathura_t* zathura) { @@ -55,6 +55,30 @@ draw_links(zathura_t* zathura) return show_links; } +/* Common code for sc_follow, sc_display_link and sc_copy_link */ +static bool +link_shortcuts(zathura_t* zathura, girara_callback_inputbar_activate_t callback, + const char* text) +{ + if (zathura->document == NULL || zathura->ui.session == NULL) { + return false; + } + + bool show_links = draw_links(zathura); + + /* ask for input */ + if (show_links == true) { + GtkWidget *inputbar = zathura->ui.session->gtk.inputbar; + gulong handler_id = g_signal_connect(inputbar, "hide", G_CALLBACK(cb_hide_links), zathura); + g_object_set_data(G_OBJECT(inputbar), "handler_id", GUINT_TO_POINTER(handler_id)); + + zathura_document_set_adjust_mode(zathura->document, ZATHURA_ADJUST_INPUTBAR); + girara_dialog(zathura->ui.session, text, FALSE, NULL, callback, zathura->ui.session); + } + + return false; +} + bool sc_abort(girara_session_t* session, girara_argument_t* UNUSED(argument), girara_event_t* UNUSED(event), unsigned int UNUSED(t)) @@ -133,21 +157,7 @@ sc_display_link(girara_session_t* session, girara_argument_t* UNUSED(argument), g_return_val_if_fail(session->global.data != NULL, false); zathura_t* zathura = session->global.data; - if (zathura->document == NULL || zathura->ui.session == NULL) { - return false; - } - - bool show_links = draw_links(zathura); - - /* ask for input */ - if (show_links) { - zathura_document_set_adjust_mode(zathura->document, ZATHURA_ADJUST_INPUTBAR); - girara_dialog(zathura->ui.session, "Display link:", FALSE, NULL, - cb_sc_display_link, - zathura->ui.session); - } - - return false; + return link_shortcuts(zathura, cb_sc_display_link, "Display Link: "); } bool @@ -158,21 +168,7 @@ sc_copy_link(girara_session_t* session, girara_argument_t* UNUSED(argument), g_return_val_if_fail(session->global.data != NULL, false); zathura_t* zathura = session->global.data; - if (zathura->document == NULL || zathura->ui.session == NULL) { - return false; - } - - bool show_links = draw_links(zathura); - - /* ask for input */ - if (show_links) { - zathura_document_set_adjust_mode(zathura->document, ZATHURA_ADJUST_INPUTBAR); - girara_dialog(zathura->ui.session, "Copy link:", FALSE, NULL, - cb_sc_copy_link, - zathura->ui.session); - } - - return false; + return link_shortcuts(zathura, cb_sc_copy_link, "Copy Link: "); } bool @@ -272,19 +268,7 @@ sc_follow(girara_session_t* session, girara_argument_t* UNUSED(argument), g_return_val_if_fail(session->global.data != NULL, false); zathura_t* zathura = session->global.data; - if (zathura->document == NULL || zathura->ui.session == NULL) { - return false; - } - - bool show_links = draw_links(zathura); - - /* ask for input */ - if (show_links == true) { - zathura_document_set_adjust_mode(zathura->document, ZATHURA_ADJUST_INPUTBAR); - girara_dialog(zathura->ui.session, "Follow link:", FALSE, NULL, cb_sc_follow, zathura->ui.session); - } - - return false; + return link_shortcuts(zathura, cb_sc_follow, "Follow Link: "); } bool