diff options
author | Ernestas Kulik <ernestask@gnome.org> | 2018-05-16 11:26:07 +0300 |
---|---|---|
committer | Ernestas Kulik <ernestask@gnome.org> | 2018-05-22 16:52:49 +0300 |
commit | 390b6519b1db28a25174d8314fc0d99222091264 (patch) | |
tree | 182b6b7f1be300a863c292821579bd5f70e74db5 /src/nautilus-notebook.c | |
parent | c9180d6065300508eb9505d5ca0be226b2e58afa (diff) | |
download | nautilus-390b6519b1db28a25174d8314fc0d99222091264.tar.gz |
notebook: Use gesture for button press events
Diffstat (limited to 'src/nautilus-notebook.c')
-rw-r--r-- | src/nautilus-notebook.c | 88 |
1 files changed, 57 insertions, 31 deletions
diff --git a/src/nautilus-notebook.c b/src/nautilus-notebook.c index e86b15a8a..f58647f14 100644 --- a/src/nautilus-notebook.c +++ b/src/nautilus-notebook.c @@ -56,17 +56,33 @@ static guint signals[LAST_SIGNAL]; struct _NautilusNotebook { GtkNotebook parent_instance; + + GtkGesture *multi_press_gesture; }; G_DEFINE_TYPE (NautilusNotebook, nautilus_notebook, GTK_TYPE_NOTEBOOK); static void +nautilus_notebook_dispose (GObject *object) +{ + NautilusNotebook *notebook; + + notebook = NAUTILUS_NOTEBOOK (object); + + g_clear_object (¬ebook->multi_press_gesture); + + G_OBJECT_CLASS (nautilus_notebook_parent_class)->dispose (object); +} + +static void nautilus_notebook_class_init (NautilusNotebookClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass); GtkNotebookClass *notebook_class = GTK_NOTEBOOK_CLASS (klass); + object_class->dispose = nautilus_notebook_dispose; + container_class->remove = nautilus_notebook_remove; notebook_class->insert_page = nautilus_notebook_insert_page; @@ -97,7 +113,6 @@ find_tab_num_at_pos (NautilusNotebook *notebook, { GtkWidget *tab; gint max_x, max_y; - gint x_root, y_root; tab = gtk_notebook_get_tab_label (nb, page); g_return_val_if_fail (tab != NULL, -1); @@ -108,12 +123,10 @@ find_tab_num_at_pos (NautilusNotebook *notebook, continue; } - gdk_window_get_origin (gtk_widget_get_window (tab), - &x_root, &y_root); gtk_widget_get_allocation (tab, &allocation); - max_x = x_root + allocation.x + allocation.width; - max_y = y_root + allocation.y + allocation.height; + max_x = allocation.x + allocation.width; + max_y = allocation.y + allocation.height; if (abs_x <= max_x && abs_y <= max_y) { @@ -125,45 +138,53 @@ find_tab_num_at_pos (NautilusNotebook *notebook, return AFTER_ALL_TABS; } -static gboolean -button_press_cb (NautilusNotebook *notebook, - GdkEventButton *event, - gpointer data) +static void +button_press_cb (GtkGestureMultiPress *gesture, + gint n_press, + gdouble x, + gdouble y, + gpointer user_data) { + guint button; + GdkEventSequence *sequence; + const GdkEvent *event; + GtkWidget *widget; + NautilusNotebook *notebook; int tab_clicked; + GdkModifierType state; - tab_clicked = find_tab_num_at_pos (notebook, event->x_root, event->y_root); + button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture)); + sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture)); + event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence); + widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture)); + notebook = NAUTILUS_NOTEBOOK (widget); + tab_clicked = find_tab_num_at_pos (notebook, x, y); - if (event->type == GDK_BUTTON_PRESS && - event->button == GDK_BUTTON_SECONDARY && - (event->state & gtk_accelerator_get_default_mod_mask ()) == 0) + gdk_event_get_state (event, &state); + + if (n_press != 1) { - if (tab_clicked == -1) - { - /* consume event, so that we don't pop up the context menu when - * the mouse if not over a tab label - */ - return GDK_EVENT_STOP; - } + return; + } + if (tab_clicked == -1) + { + return; + } + + if (button == GDK_BUTTON_SECONDARY && + (state & gtk_accelerator_get_default_mod_mask ()) == 0) + { /* switch to the page the mouse is over, but don't consume the event */ gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), tab_clicked); } - else if (event->type == GDK_BUTTON_PRESS && - event->button == GDK_BUTTON_MIDDLE) + else if (button == GDK_BUTTON_MIDDLE) { GtkWidget *slot; - if (tab_clicked == -1) - { - return GDK_EVENT_PROPAGATE; - } - slot = gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), tab_clicked); g_signal_emit (notebook, signals[TAB_CLOSE_REQUEST], 0, slot); } - - return GDK_EVENT_PROPAGATE; } static void @@ -173,8 +194,13 @@ nautilus_notebook_init (NautilusNotebook *notebook) gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE); gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE); - g_signal_connect (notebook, "button-press-event", - (GCallback) button_press_cb, NULL); + notebook->multi_press_gesture = gtk_gesture_multi_press_new (GTK_WIDGET (notebook)); + + gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (notebook->multi_press_gesture), + GTK_PHASE_CAPTURE); + gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (notebook->multi_press_gesture), 0); + + g_signal_connect (notebook->multi_press_gesture, "pressed", G_CALLBACK (button_press_cb), NULL); } gboolean |