diff options
-rw-r--r-- | src/find-toolbar.c | 124 |
1 files changed, 112 insertions, 12 deletions
diff --git a/src/find-toolbar.c b/src/find-toolbar.c index 2e6e4ab..d61fc8e 100644 --- a/src/find-toolbar.c +++ b/src/find-toolbar.c @@ -88,6 +88,106 @@ set_property(GObject *object, guint property_id, const GValue *value, } static void +search(MqFindToolbar *find_toolbar, gboolean forward) +{ + guint32 find_options; + + find_options = WEBKIT_FIND_OPTIONS_WRAP_AROUND; + if (!find_toolbar->match_case) { + find_options |= WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE; + } + if (!forward) { + find_options |= WEBKIT_FIND_OPTIONS_BACKWARDS; + } + webkit_find_controller_search(find_toolbar->find_controller, + gtk_entry_get_text(GTK_ENTRY(find_toolbar->search_entry)), + find_options, G_MAXUINT); + find_toolbar->searching = TRUE; +} + +static void +search_finished(MqFindToolbar *find_toolbar) +{ + find_toolbar->searching = FALSE; + webkit_find_controller_search_finish(find_toolbar->find_controller); +} + +static void +hide(MqFindToolbar *find_toolbar) +{ + gtk_revealer_set_reveal_child(GTK_REVEALER(find_toolbar), + FALSE); + gtk_label_set_text(GTK_LABEL(find_toolbar->matches_label), NULL); + search_finished(find_toolbar); +} + +static void +search_changed_cb(GtkSearchEntry G_GNUC_UNUSED *entry, + MqFindToolbar *find_toolbar) +{ + search(find_toolbar, TRUE); +} + +static gboolean +search_key_press_event_cb(GtkSearchEntry G_GNUC_UNUSED *entry, + GdkEventKey *event, MqFindToolbar *find_toolbar) +{ + switch (event->keyval) { + case GDK_KEY_Escape: + hide(find_toolbar); + return TRUE; + case GDK_KEY_Return: + case GDK_KEY_KP_Enter: + case GDK_KEY_ISO_Enter: + search(find_toolbar, + !(event->state & GDK_SHIFT_MASK)); + return TRUE; + default: + return FALSE; + } +} + +static void +prev_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqFindToolbar *find_toolbar) +{ + /* Calling this method before webkit_find_controller_search() or + * webkit_find_controller_count_matches() is a programming error. */ + if (find_toolbar->searching) { + webkit_find_controller_search_previous( + find_toolbar->find_controller); + } else { + search(find_toolbar, FALSE); + } +} + +static void +next_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqFindToolbar *find_toolbar) +{ + /* Calling this method before webkit_find_controller_search() or + * webkit_find_controller_count_matches() is a programming error. */ + if (find_toolbar->searching) { + webkit_find_controller_search_next( + find_toolbar->find_controller); + } else { + search(find_toolbar, TRUE); + } +} + +static void +match_case_toggled_cb(GtkToggleButton *toggle_button, + MqFindToolbar *find_toolbar) +{ + find_toolbar->match_case = gtk_toggle_button_get_active(toggle_button); + search(find_toolbar, TRUE); +} + +static void +close_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqFindToolbar *find_toolbar) +{ + hide(find_toolbar); +} + +static void mq_find_toolbar_class_init(MqFindToolbarClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS(klass); @@ -117,34 +217,34 @@ mq_find_toolbar_init(MqFindToolbar *find_toolbar) /* Search entry */ find_toolbar->search_entry = gtk_search_entry_new(); -// g_signal_connect(find_toolbar->search_entry, "search-changed", -// G_CALLBACK(find_search_changed_cb), find_toolbar); -// g_signal_connect(find_toolbar->search_entry, "key-press-event", -// G_CALLBACK(find_search_key_press_event_cb), find_toolbar); + g_signal_connect(find_toolbar->search_entry, "search-changed", + G_CALLBACK(search_changed_cb), find_toolbar); + g_signal_connect(find_toolbar->search_entry, "key-press-event", + G_CALLBACK(search_key_press_event_cb), find_toolbar); /* Previous button */ prev_button = gtk_button_new_from_icon_name("go-up", GTK_ICON_SIZE_BUTTON); gtk_widget_set_tooltip_text(prev_button, "Find previous occurrence"); gtk_widget_set_can_focus(prev_button, FALSE); -// g_signal_connect(prev_button, "clicked", -// G_CALLBACK(find_prev_clicked_cb), find_toolbar); + g_signal_connect(prev_button, "clicked", + G_CALLBACK(prev_clicked_cb), find_toolbar); /* Next button */ next_button = gtk_button_new_from_icon_name("go-down", GTK_ICON_SIZE_BUTTON); gtk_widget_set_tooltip_text(next_button, "Find next occurrence"); gtk_widget_set_can_focus(next_button, FALSE); -// g_signal_connect(next_button, "clicked", -// G_CALLBACK(find_next_clicked_cb), find_toolbar); + g_signal_connect(next_button, "clicked", + G_CALLBACK(next_clicked_cb), find_toolbar); /* Case sensitivity button */ match_case_button = gtk_toggle_button_new_with_label("Match case"); gtk_widget_set_tooltip_text(match_case_button, "Search with case sensitivity"); gtk_widget_set_can_focus(match_case_button, FALSE); -// g_signal_connect(match_case_button, "toggled", -// G_CALLBACK(find_match_case_toggled_cb), find_toolbar); + g_signal_connect(match_case_button, "toggled", + G_CALLBACK(match_case_toggled_cb), find_toolbar); /* Matches label */ find_toolbar->matches_label = gtk_label_new(NULL); @@ -155,8 +255,8 @@ mq_find_toolbar_init(MqFindToolbar *find_toolbar) gtk_button_set_relief(GTK_BUTTON(close_button), GTK_RELIEF_NONE); gtk_widget_set_tooltip_text(close_button, "Close find bar"); gtk_widget_set_can_focus(close_button, FALSE); -// g_signal_connect(close_button, "clicked", -// G_CALLBACK(find_close_clicked_cb), find_toolbar); + g_signal_connect(close_button, "clicked", + G_CALLBACK(close_clicked_cb), find_toolbar); /* Box */ box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); |