[gtk+/wip/matthiasc/kill-event-signals: 33/62] gtk-demo: Port the drawingarea demo to a gesture



commit ff5505fcdfbdb621bf9bba7885717a696b557f9e
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jan 1 10:09:31 2018 -0500

    gtk-demo: Port the drawingarea demo to a gesture
    
    Use a drag gesture here, just like in the drawing example.

 demos/gtk-demo/drawingarea.c |   67 ++++++++++++++++++++---------------------
 1 files changed, 33 insertions(+), 34 deletions(-)
---
diff --git a/demos/gtk-demo/drawingarea.c b/demos/gtk-demo/drawingarea.c
index 6ae5253..8f15f89 100644
--- a/demos/gtk-demo/drawingarea.c
+++ b/demos/gtk-demo/drawingarea.c
@@ -89,42 +89,38 @@ draw_brush (GtkWidget *widget,
   gtk_widget_queue_draw_area (widget, update_rect.x, update_rect.y, update_rect.width, update_rect.height);
 }
 
-static gboolean
-scribble_button_press_event (GtkWidget      *widget,
-                             GdkEventButton *event,
-                             gpointer        data)
-{
-  double x, y;
-  guint button;
-
-  gdk_event_get_button ((GdkEvent *)event, &button);
-  gdk_event_get_coords ((GdkEvent *)event, &x, &y);
+static double start_x;
+static double start_y;
 
-  if (button == GDK_BUTTON_PRIMARY)
-    draw_brush (widget, x, y);
+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);
 }
 
-static gboolean
-scribble_motion_notify_event (GtkWidget      *widget,
-                              GdkEventMotion *event,
-                              gpointer        data)
+static void
+drag_update (GtkGestureDrag *gesture,
+             double          x,
+             double          y,
+             GtkWidget      *area)
 {
-  double x, y;
-  GdkModifierType state;
-
-  gdk_event_get_state ((GdkEvent *)event, &state);
-  gdk_event_get_coords ((GdkEvent *)event, &x, &y);
-
-  if (state & GDK_BUTTON1_MASK)
-    draw_brush (widget, x, y);
-
-  /* We've handled it, stop processing */
-  return TRUE;
+  draw_brush (area, start_x + x, start_y + y);
 }
 
+static void
+drag_end (GtkGestureDrag *gesture,
+          double          x,
+          double          y,
+          GtkWidget      *area)
+{
+  draw_brush (area, start_x + x, start_y + y);
+}
 
 static void
 checkerboard_draw (GtkDrawingArea *da,
@@ -189,6 +185,7 @@ do_drawingarea (GtkWidget *do_widget)
   GtkWidget *vbox;
   GtkWidget *da;
   GtkWidget *label;
+  GtkGesture *drag;
 
   if (!window)
     {
@@ -247,11 +244,13 @@ do_drawingarea (GtkWidget *do_widget)
       g_signal_connect (da, "size-allocate",
                         G_CALLBACK (scribble_size_allocate), NULL);
 
-      /* Event signals */
-      g_signal_connect (da, "motion-notify-event",
-                        G_CALLBACK (scribble_motion_notify_event), NULL);
-      g_signal_connect (da, "button-press-event",
-                        G_CALLBACK (scribble_button_press_event), NULL);
+      drag = gtk_gesture_drag_new (da);
+      gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (drag), GDK_BUTTON_PRIMARY);
+      g_object_set_data_full (G_OBJECT (da), "drag", drag, g_object_unref);
+
+      g_signal_connect (drag, "drag-begin", G_CALLBACK (drag_begin), da);
+      g_signal_connect (drag, "drag-update", G_CALLBACK (drag_update), da);
+      g_signal_connect (drag, "drag-begin", G_CALLBACK (drag_end), da);
     }
 
   if (!gtk_widget_get_visible (window))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]