diff options
author | Matthias Clasen <mclasen@redhat.com> | 2018-01-01 10:00:37 -0500 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2018-01-02 18:14:13 -0500 |
commit | c9267e70a9781d55a6a97a781ca3882ecf69cba3 (patch) | |
tree | 8f5e4c39357ce7f9538f7dadd2277998d2656144 | |
parent | bb568a51d4108bb5eb12f0b6f58b09fdec13e05c (diff) | |
download | gtk+-c9267e70a9781d55a6a97a781ca3882ecf69cba3.tar.gz |
Port drawing example to gestures
Use a drag and a multi-press gesture.
-rw-r--r-- | examples/drawing.c | 106 |
1 files changed, 50 insertions, 56 deletions
diff --git a/examples/drawing.c b/examples/drawing.c index 8712bdf47d..c1f64b7e54 100644 --- a/examples/drawing.c +++ b/examples/drawing.c @@ -77,64 +77,48 @@ draw_brush (GtkWidget *widget, gtk_widget_queue_draw_area (widget, x - 3, y - 3, 6, 6); } -/* Handle button press events by either drawing a rectangle - * or clearing the surface, depending on which button was pressed. - * The ::button-press signal handler receives a GdkEventButton - * struct which contains this information. - */ -static gboolean -button_press_event_cb (GtkWidget *widget, - GdkEventButton *event, - gpointer data) -{ - guint button; - double x, y; - - /* paranoia check, in case we haven't gotten a configure event */ - if (surface == NULL) - return FALSE; +static double start_x; +static double start_y; - gdk_event_get_button ((GdkEvent *)event, &button); - gdk_event_get_coords ((GdkEvent *)event, &x, &y); - - if (button == GDK_BUTTON_PRIMARY) - { - draw_brush (widget, x, y); - } - else if (button == GDK_BUTTON_SECONDARY) - { - clear_surface (); - gtk_widget_queue_draw (widget); - } +static void +drag_begin (GtkGestureDrag *gesture, + double x, + double y, + GtkWidget *area) +{ + start_x = x; + start_y = y; - /* We've handled the event, stop processing */ - return TRUE; + draw_brush (area, x, y); } -/* Handle motion events by continuing to draw if button 1 is - * still held down. The ::motion-notify signal handler receives - * a GdkEventMotion struct which contains this information. - */ -static gboolean -motion_notify_event_cb (GtkWidget *widget, - GdkEventMotion *event, - gpointer data) +static void +drag_update (GtkGestureDrag *gesture, + double x, + double y, + GtkWidget *area) { - double x, y; - GdkModifierType state; - - /* paranoia check, in case we haven't gotten a configure event */ - if (surface == NULL) - return FALSE; - - gdk_event_get_state ((GdkEvent *)event, &state); - gdk_event_get_coords ((GdkEvent *)event, &x, &y); + draw_brush (area, start_x + x, start_y + y); +} - if (state & GDK_BUTTON1_MASK) - draw_brush (widget, x, y); +static void +drag_end (GtkGestureDrag *gesture, + double x, + double y, + GtkWidget *area) +{ + draw_brush (area, start_x + x, start_y + y); +} - /* We've handled it, stop processing */ - return TRUE; +static void +pressed (GtkGestureMultiPress *gesture, + int n_press, + double x, + double y, + GtkWidget *area) +{ + clear_surface (); + gtk_widget_queue_draw (area); } static void @@ -151,6 +135,8 @@ activate (GtkApplication *app, GtkWidget *window; GtkWidget *frame; GtkWidget *drawing_area; + GtkGesture *drag; + GtkGesture *press; window = gtk_application_window_new (app); gtk_window_set_title (GTK_WINDOW (window), "Drawing Area"); @@ -172,11 +158,19 @@ activate (GtkApplication *app, g_signal_connect_after (drawing_area, "size-allocate", G_CALLBACK (size_allocate_cb), NULL); - /* Event signals */ - g_signal_connect (drawing_area, "motion-notify-event", - G_CALLBACK (motion_notify_event_cb), NULL); - g_signal_connect (drawing_area, "button-press-event", - G_CALLBACK (button_press_event_cb), NULL); + drag = gtk_gesture_drag_new (drawing_area); + gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (drag), GDK_BUTTON_PRIMARY); + g_object_set_data_full (G_OBJECT (drawing_area), "drag", drag, g_object_unref); + + g_signal_connect (drag, "drag-begin", G_CALLBACK (drag_begin), drawing_area); + g_signal_connect (drag, "drag-update", G_CALLBACK (drag_update), drawing_area); + g_signal_connect (drag, "drag-begin", G_CALLBACK (drag_end), drawing_area); + + press = gtk_gesture_multi_press_new (drawing_area); + gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (press), GDK_BUTTON_SECONDARY); + g_object_set_data_full (G_OBJECT (drawing_area), "press", press, g_object_unref); + + g_signal_connect (press, "pressed", G_CALLBACK (pressed), drawing_area); gtk_widget_show (window); } |