[gtk+/gestures: 117/173] GtkButton: Port to GtkGesture



commit 07094acc8630c926d73be589f18df6f4a65d9f03
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu May 1 20:15:40 2014 -0400

    GtkButton: Port to GtkGesture
    
    Quick-and-dirty patch to use a GtkGestureSingle for both
    button press/release and touch event handling.

 gtk/gtkbutton.c |  110 ++++++++++++++++---------------------------------------
 1 files changed, 32 insertions(+), 78 deletions(-)
---
diff --git a/gtk/gtkbutton.c b/gtk/gtkbutton.c
index fe15499..1fcc191 100644
--- a/gtk/gtkbutton.c
+++ b/gtk/gtkbutton.c
@@ -119,12 +119,6 @@ static void gtk_button_style_updated (GtkWidget * widget);
 static void gtk_button_size_allocate (GtkWidget * widget,
                                      GtkAllocation * allocation);
 static gint gtk_button_draw (GtkWidget * widget, cairo_t *cr);
-static gint gtk_button_button_press (GtkWidget * widget,
-                                    GdkEventButton * event);
-static gint gtk_button_button_release (GtkWidget * widget,
-                                      GdkEventButton * event);
-static gboolean gtk_button_touch (GtkWidget     *widget,
-                                  GdkEventTouch *event);
 static gint gtk_button_grab_broken (GtkWidget * widget,
                                    GdkEventGrabBroken * event);
 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
@@ -225,9 +219,6 @@ gtk_button_class_init (GtkButtonClass *klass)
   widget_class->style_updated = gtk_button_style_updated;
   widget_class->size_allocate = gtk_button_size_allocate;
   widget_class->draw = gtk_button_draw;
-  widget_class->button_press_event = gtk_button_button_press;
-  widget_class->button_release_event = gtk_button_button_release;
-  widget_class->touch_event = gtk_button_touch;
   widget_class->grab_broken_event = gtk_button_grab_broken;
   widget_class->key_release_event = gtk_button_key_release;
   widget_class->enter_notify_event = gtk_button_enter_notify;
@@ -594,6 +585,30 @@ gtk_button_class_init (GtkButtonClass *klass)
 }
 
 static void
+gesture_begin_cb (GtkGesture *gesture,
+                  gpointer    sequence,
+                  GtkWidget  *widget)
+{
+  GtkButton *button = GTK_BUTTON (widget);
+  GtkButtonPrivate *priv = button->priv;
+
+  if (priv->focus_on_click && !gtk_widget_has_focus (widget))
+    gtk_widget_grab_focus (widget);
+ 
+  g_signal_emit (button, button_signals[PRESSED], 0);
+}
+
+static void
+gesture_end_cb (GtkGesture *gesture,
+                gpointer    sequence,
+                GtkWidget  *widget)
+{
+  GtkButton *button = GTK_BUTTON (widget);
+
+  g_signal_emit (button, button_signals[RELEASED], 0);
+}
+
+static void
 gtk_button_init (GtkButton *button)
 {
   GtkButtonPrivate *priv;
@@ -627,6 +642,14 @@ gtk_button_init (GtkButton *button)
 
   context = gtk_widget_get_style_context (GTK_WIDGET (button));
   gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
+
+  priv->gesture = GTK_GESTURE (g_object_new (GTK_TYPE_GESTURE_SINGLE, "widget", button, NULL));
+  gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (priv->gesture), FALSE);
+  gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (priv->gesture), GDK_BUTTON_PRIMARY);
+  g_signal_connect (priv->gesture, "begin", G_CALLBACK (gesture_begin_cb), button);
+  g_signal_connect (priv->gesture, "end", G_CALLBACK (gesture_end_cb), button);
+  gtk_widget_add_controller (GTK_WIDGET (button),
+                             GTK_EVENT_CONTROLLER (priv->gesture));
 }
 
 static void
@@ -1869,75 +1892,6 @@ gtk_button_draw (GtkWidget *widget,
   return FALSE;
 }
 
-static gboolean
-gtk_button_button_press (GtkWidget      *widget,
-                        GdkEventButton *event)
-{
-  GtkButton *button;
-  GtkButtonPrivate *priv;
-
-  if (event->type == GDK_BUTTON_PRESS)
-    {
-      button = GTK_BUTTON (widget);
-      priv = button->priv;
-
-      if (priv->focus_on_click && !gtk_widget_has_focus (widget))
-       gtk_widget_grab_focus (widget);
-
-      if (event->button == GDK_BUTTON_PRIMARY)
-        {
-          g_signal_emit (button, button_signals[PRESSED], 0);
-          return GDK_EVENT_STOP;
-        }
-    }
-  else if (event->type == GDK_2BUTTON_PRESS)
-    {
-      return GDK_EVENT_STOP;
-    }
-
-  return GDK_EVENT_PROPAGATE;
-}
-
-static gboolean
-gtk_button_button_release (GtkWidget      *widget,
-                          GdkEventButton *event)
-{
-  GtkButton *button;
-
-  if (event->button == GDK_BUTTON_PRIMARY)
-    {
-      button = GTK_BUTTON (widget);
-      g_signal_emit (button, button_signals[RELEASED], 0);
-      return GDK_EVENT_STOP;
-    }
-
-  return GDK_EVENT_PROPAGATE;
-}
-
-static gboolean
-gtk_button_touch (GtkWidget     *widget,
-                  GdkEventTouch *event)
-{
-  GtkButton *button = GTK_BUTTON (widget);
-  GtkButtonPrivate *priv = button->priv;
-
-  if (event->type == GDK_TOUCH_BEGIN)
-    {
-      if (priv->focus_on_click && !gtk_widget_has_focus (widget))
-        gtk_widget_grab_focus (widget);
-
-      g_signal_emit (button, button_signals[PRESSED], 0);
-      return GDK_EVENT_STOP;
-    }
-  else if (event->type == GDK_TOUCH_END)
-    {
-      g_signal_emit (button, button_signals[RELEASED], 0);
-      return GDK_EVENT_STOP;
-    }
-
-  return GDK_EVENT_PROPAGATE;
-}
-
 static void
 gtk_button_do_release (GtkButton *button,
                        gboolean   emit_clicked)


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