[gnome-builder/gnome-builder-3-18] libide: add helper to synthesize gdk event keys



commit ebd1984d7eb92bb3ac8b5f087bf1cc10bc48af3c
Author: Christian Hergert <chergert redhat com>
Date:   Sat Oct 10 17:27:32 2015 -0400

    libide: add helper to synthesize gdk event keys
    
    This is sort of hackish, but necessary do to how we do indenters today.

 libide/Makefile.am    |    2 +
 libide/util/ide-gdk.c |   90 +++++++++++++++++++++++++++++++++++++++++++++++++
 libide/util/ide-gdk.h |   31 +++++++++++++++++
 3 files changed, 123 insertions(+), 0 deletions(-)
---
diff --git a/libide/Makefile.am b/libide/Makefile.am
index 0011a57..64ff833 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -234,6 +234,8 @@ libide_1_0_la_SOURCES = \
        util/ide-cairo.h \
        util/ide-doc-seq.c \
        util/ide-doc-seq.h \
+       util/ide-gdk.c \
+       util/ide-gdk.h \
        util/ide-line-reader.c \
        util/ide-line-reader.h \
        util/ide-pango.c \
diff --git a/libide/util/ide-gdk.c b/libide/util/ide-gdk.c
new file mode 100644
index 0000000..dfd6f0f
--- /dev/null
+++ b/libide/util/ide-gdk.c
@@ -0,0 +1,90 @@
+/* ide-gdk.c
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+
+#include "ide-gdk.h"
+
+GdkEventKey *
+ide_gdk_synthesize_event_key (GdkWindow *window,
+                              gunichar   ch)
+{
+  GdkDisplay *display;
+  GdkDeviceManager *device_manager;
+  GdkDevice *client_pointer;
+  GdkEvent *ev;
+  GdkKeymapKey *keys = NULL;
+  gint n_keys = 0;
+  gchar str[8] = { 0 };
+
+  g_assert (window != NULL);
+  g_assert (GDK_IS_WINDOW (window));
+
+  g_unichar_to_utf8 (ch, str);
+
+  ev = gdk_event_new (GDK_KEY_PRESS);
+  ev->key.window = g_object_ref (window);
+  ev->key.send_event = TRUE;
+  ev->key.time = gtk_get_current_event_time ();
+  ev->key.state = 0;
+  ev->key.hardware_keycode = 0;
+  ev->key.group = 0;
+  ev->key.is_modifier = 0;
+
+  switch (ch)
+    {
+    case '\n':
+      ev->key.keyval = GDK_KEY_Return;
+      ev->key.string = g_strdup ("\n");
+      ev->key.length = 1;
+      break;
+
+    case '\e':
+      ev->key.keyval = GDK_KEY_Escape;
+      ev->key.string = g_strdup ("");
+      ev->key.length = 0;
+      break;
+
+    default:
+      ev->key.keyval = gdk_unicode_to_keyval (ch);
+      ev->key.length = strlen (str);
+      ev->key.string = g_strdup (str);
+      break;
+    }
+
+  gdk_keymap_get_entries_for_keyval (gdk_keymap_get_default (),
+                                     ev->key.keyval,
+                                     &keys,
+                                     &n_keys);
+
+  if (n_keys > 0)
+    {
+      ev->key.hardware_keycode = keys [0].keycode;
+      ev->key.group = keys [0].group;
+      if (keys [0].level == 1)
+        ev->key.state |= GDK_SHIFT_MASK;
+      g_free (keys);
+    }
+
+  display = gdk_window_get_display (ev->any.window);
+  device_manager = gdk_display_get_device_manager (display);
+  client_pointer = gdk_device_manager_get_client_pointer (device_manager);
+  gdk_event_set_device (ev, gdk_device_get_associated_device (client_pointer));
+
+  return &ev->key;
+}
diff --git a/libide/util/ide-gdk.h b/libide/util/ide-gdk.h
new file mode 100644
index 0000000..d20868a
--- /dev/null
+++ b/libide/util/ide-gdk.h
@@ -0,0 +1,31 @@
+/* ide-gdk.h
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_GDK_H
+#define IDE_GDK_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+GdkEventKey *ide_gdk_synthesize_event_key (GdkWindow *window,
+                                           gunichar   ch);
+
+G_END_DECLS
+
+#endif /* IDE_GDK_H */


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