[gtk+/wip/carlosg/key-event-controller] WIP: gtk: Add GtkEventControllerKey



commit d379e695977554e97b6f012d75397794c6653bfd
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Dec 11 19:21:38 2017 +0100

    WIP: gtk: Add GtkEventControllerKey
    
    This event controller is meant to replace usage from key-press/release-event
    handlers all through.

 gtk/gtk.h                   |    1 +
 gtk/gtkeventcontrollerkey.c |  138 +++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkeventcontrollerkey.h |   50 ++++++++++++++++
 gtk/meson.build             |    2 +
 4 files changed, 191 insertions(+), 0 deletions(-)
---
diff --git a/gtk/gtk.h b/gtk/gtk.h
index acd5b76..09d5268 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -93,6 +93,7 @@
 #include <gtk/gtkentrycompletion.h>
 #include <gtk/gtkenums.h>
 #include <gtk/gtkeventcontroller.h>
+#include <gtk/gtkeventcontrollerkey.h>
 #include <gtk/gtkeventcontrollermotion.h>
 #include <gtk/gtkeventcontrollerscroll.h>
 #include <gtk/gtkexpander.h>
diff --git a/gtk/gtkeventcontrollerkey.c b/gtk/gtkeventcontrollerkey.c
new file mode 100644
index 0000000..6f66a4f
--- /dev/null
+++ b/gtk/gtkeventcontrollerkey.c
@@ -0,0 +1,138 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2017, Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author(s): Carlos Garnacho <carlosg gnome org>
+ */
+
+#include "config.h"
+
+#include "gtkintl.h"
+#include "gtkprivate.h"
+#include "gtkwidget.h"
+#include "gtkeventcontrollerprivate.h"
+#include "gtkeventcontrollerkey.h"
+#include "gtkbindings.h"
+
+#include <gdk/gdk.h>
+
+struct _GtkEventControllerKey
+{
+  GtkEventController parent_instance;
+  GdkModifierType state;
+};
+
+struct _GtkEventControllerKeyClass
+{
+  GtkEventControllerClass parent_class;
+};
+
+enum {
+  KEY_PRESSED,
+  KEY_RELEASED,
+  MODIFIERS,
+  N_SIGNALS
+};
+
+static guint signals[N_SIGNALS] = { 0 };
+
+G_DEFINE_TYPE (GtkEventControllerKey, gtk_event_controller_key,
+               GTK_TYPE_EVENT_CONTROLLER)
+
+static gboolean
+gtk_event_controller_key_handle_event (GtkEventController *controller,
+                                       const GdkEvent     *event)
+{
+  GtkEventControllerKey *key = GTK_EVENT_CONTROLLER_KEY (controller);
+  GdkEventType event_type = gdk_event_get_event_type (event);
+  gboolean handled;
+  GdkModifierType state;
+  guint16 keycode;
+  guint keyval;
+
+  if (event_type != GDK_KEY_PRESS && event_type != GDK_KEY_RELEASE)
+    return FALSE;
+  if (!gdk_event_get_state (event, &state))
+    return FALSE;
+
+  if (state != key->state)
+    {
+      g_signal_emit (controller, signals[MODIFIERS], 0, state);
+      key->state = state;
+    }
+
+  gdk_event_get_keycode (event, &keycode);
+  gdk_event_get_keyval (event, &keyval);
+
+  if (event_type == GDK_KEY_PRESS)
+    {
+      g_signal_emit (controller, signals[KEY_PRESSED], 0,
+                     keyval, keycode, &handled);
+    }
+  else if (event_type == GDK_KEY_RELEASE)
+    {
+      g_signal_emit (controller, signals[KEY_RELEASED], 0,
+                     keyval, keycode, &handled);
+    }
+  else
+    handled = TRUE;
+
+  if (!handled)
+    handled = gtk_bindings_activate_event (G_OBJECT (gtk_event_controller_get_widget (controller)), event);
+
+  return handled;
+}
+
+static void
+gtk_event_controller_key_class_init (GtkEventControllerKeyClass *klass)
+{
+  GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
+
+  controller_class->handle_event = gtk_event_controller_key_handle_event;
+
+  signals[KEY_PRESSED] =
+    g_signal_new (I_("key-pressed"),
+                  GTK_TYPE_EVENT_CONTROLLER_KEY,
+                  G_SIGNAL_RUN_FIRST,
+                  0, _gtk_boolean_handled_accumulator, NULL, NULL,
+                  G_TYPE_BOOLEAN, 2, G_TYPE_UINT, G_TYPE_UINT);
+  signals[KEY_RELEASED] =
+    g_signal_new (I_("key-released"),
+                  GTK_TYPE_EVENT_CONTROLLER_KEY,
+                  G_SIGNAL_RUN_FIRST,
+                  0, NULL, NULL, NULL,
+                  G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
+  signals[MODIFIERS] =
+    g_signal_new (I_("modifiers"),
+                  GTK_TYPE_EVENT_CONTROLLER_KEY,
+                  G_SIGNAL_RUN_FIRST,
+                  0, NULL, NULL, NULL,
+                  G_TYPE_NONE, 1, G_TYPE_UINT);
+}
+
+static void
+gtk_event_controller_key_init (GtkEventControllerKey *controller)
+{
+}
+
+GtkEventController *
+gtk_event_controller_key_new (GtkWidget *widget)
+{
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
+
+  return g_object_new (GTK_TYPE_EVENT_CONTROLLER_KEY,
+                       "widget", widget,
+                       NULL);
+}
diff --git a/gtk/gtkeventcontrollerkey.h b/gtk/gtkeventcontrollerkey.h
new file mode 100644
index 0000000..1bfdfc6
--- /dev/null
+++ b/gtk/gtkeventcontrollerkey.h
@@ -0,0 +1,50 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2017, Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author(s): Carlos Garnacho <carlosg gnome org>
+ */
+
+#ifndef __GTK_EVENT_CONTROLLER_KEY_H__
+#define __GTK_EVENT_CONTROLLER_KEY_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gdk/gdk.h>
+#include <gtk/gtkeventcontroller.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_EVENT_CONTROLLER_KEY         (gtk_event_controller_key_get_type ())
+#define GTK_EVENT_CONTROLLER_KEY(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), 
GTK_TYPE_EVENT_CONTROLLER_KEY, GtkEventControllerKey))
+#define GTK_EVENT_CONTROLLER_KEY_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_EVENT_CONTROLLER_KEY, 
GtkEventControllerKeyClass))
+#define GTK_IS_EVENT_CONTROLLER_KEY(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), 
GTK_TYPE_EVENT_CONTROLLER_KEY))
+#define GTK_IS_EVENT_CONTROLLER_KEY_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_EVENT_CONTROLLER_KEY))
+#define GTK_EVENT_CONTROLLER_KEY_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), 
GTK_TYPE_EVENT_CONTROLLER_KEY, GtkEventControllerKeyClass))
+
+typedef struct _GtkEventControllerKey GtkEventControllerKey;
+typedef struct _GtkEventControllerKeyClass GtkEventControllerKeyClass;
+
+GDK_AVAILABLE_IN_3_92
+GType               gtk_event_controller_key_get_type  (void) G_GNUC_CONST;
+
+GDK_AVAILABLE_IN_3_92
+GtkEventController *gtk_event_controller_key_new (GtkWidget *widget);
+
+G_END_DECLS
+
+#endif /* __GTK_EVENT_CONTROLLER_KEY_H__ */
diff --git a/gtk/meson.build b/gtk/meson.build
index ff6ebe7..c6aa32a 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -149,6 +149,7 @@ gtk_public_sources = files([
   'gtkentrybuffer.c',
   'gtkentrycompletion.c',
   'gtkeventcontroller.c',
+  'gtkeventcontrollerkey.c',
   'gtkeventcontrollerlegacy.c',
   'gtkeventcontrollermotion.c',
   'gtkeventcontrollerscroll.c',
@@ -440,6 +441,7 @@ gtk_public_headers = files([
   'gtkentrycompletion.h',
   'gtkenums.h',
   'gtkeventcontroller.h',
+  'gtkeventcontrollerkey.h',
   'gtkeventcontrollerscroll.h',
   'gtkeventcontrollermotion.h',
   'gtkexpander.h',


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