[gtksourceview/wip/chergert/gsv-gtk4: 113/125] view: use GtkEventControllerKey



commit 59fc9f0aeb93014f5f9c5d0b53ece99326174205
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jan 15 15:27:52 2020 -0800

    view: use GtkEventControllerKey
    
    We no longer have a key-press-event to use so we need to instead rely
    on the GtkEventControllerKey for dispatch of the key events. A good
    side-effect of this is that Input Method filtering should happen before
    we get to dispatching our handler.

 gtksourceview/gtksourceview.c | 57 +++++++++++++++++++++++--------------------
 1 file changed, 30 insertions(+), 27 deletions(-)
---
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index 790806dc..b2b3e8c1 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -281,8 +281,6 @@ static void           gtk_source_view_move_lines           (GtkSourceView
                                                             gboolean                 down);
 static void           gtk_source_view_move_words           (GtkSourceView           *view,
                                                             gint                     step);
-static gboolean       gtk_source_view_key_press_event      (GtkWidget               *widget,
-                                                            GdkEventKey             *event);
 static gint           calculate_real_tab_width             (GtkSourceView           *view,
                                                             guint                    tab_size,
                                                             gchar                    c);
@@ -302,6 +300,11 @@ static void           gtk_source_view_draw_layer           (GtkTextView
 static MarkCategory  *mark_category_new                    (GtkSourceMarkAttributes *attributes,
                                                             gint                     priority);
 static void           mark_category_free                   (MarkCategory            *category);
+static gboolean       gtk_source_view_key_pressed                        (GtkSourceView           *view,
+                                                                          guint                    keyval,
+                                                                          guint                    keycode,
+                                                                          guint                    state,
+                                                                          GtkEventControllerKey   
*controller);
 static void           gtk_source_view_queue_draw                         (GtkSourceView           *view);
 static gboolean       gtk_source_view_drag_drop                          (GtkDropTarget           *dest,
                                                                           GdkDrop                 *drop,
@@ -492,7 +495,6 @@ gtk_source_view_class_init (GtkSourceViewClass *klass)
        object_class->get_property = gtk_source_view_get_property;
        object_class->set_property = gtk_source_view_set_property;
 
-       widget_class->key_press_event = gtk_source_view_key_press_event;
        widget_class->draw = gtk_source_view_draw;
        widget_class->style_updated = gtk_source_view_style_updated;
 
@@ -1284,6 +1286,7 @@ gtk_source_view_init (GtkSourceView *view)
 {
        GtkSourceViewPrivate *priv = gtk_source_view_get_instance_private (view);
        GtkStyleContext *context;
+       GtkEventController *key;
        GdkContentFormats *formats;
        GtkDropTarget *dest;
 
@@ -1313,6 +1316,14 @@ gtk_source_view_init (GtkSourceView *view)
                                                             (GDestroyNotify) g_free,
                                                             (GDestroyNotify) mark_category_free);
 
+       key = gtk_event_controller_key_new ();
+       gtk_event_controller_set_propagation_phase (key, GTK_PHASE_CAPTURE);
+       g_signal_connect_swapped (key,
+                                 "key-pressed",
+                                 G_CALLBACK (gtk_source_view_key_pressed),
+                                 view);
+       gtk_widget_add_controller (GTK_WIDGET (view), g_steal_pointer (&key));
+
        formats = gdk_content_formats_new (dnd_targets, G_N_ELEMENTS (dnd_targets));
        dest = gtk_drop_target_new (formats, GDK_ACTION_COPY);
        g_signal_connect (dest, "drag-drop", G_CALLBACK (gtk_source_view_drag_drop), view);
@@ -3907,10 +3918,12 @@ do_ctrl_backspace (GtkSourceView *view)
 }
 
 static gboolean
-gtk_source_view_key_press_event (GtkWidget   *widget,
-                                 GdkEventKey *event)
+gtk_source_view_key_pressed (GtkSourceView         *view,
+                            guint                  keyval,
+                            guint                  keycode,
+                            guint                  state,
+                            GtkEventControllerKey *controller)
 {
-       GtkSourceView *view = GTK_SOURCE_VIEW (widget);
        GtkSourceViewPrivate *priv = gtk_source_view_get_instance_private (view);
        GtkTextBuffer *buf;
        GtkTextIter cur;
@@ -3919,21 +3932,21 @@ gtk_source_view_key_press_event (GtkWidget   *widget,
        gint key;
        gboolean editable;
 
-       buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
+       buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
 
-       editable = gtk_text_view_get_editable (GTK_TEXT_VIEW (widget));
+       editable = gtk_text_view_get_editable (GTK_TEXT_VIEW (view));
 
        /* Be careful when testing for modifier state equality:
         * caps lock, num lock,etc need to be taken into account */
        modifiers = gtk_accelerator_get_default_mod_mask ();
 
-       key = event->keyval;
+       key = keyval;
 
        mark = gtk_text_buffer_get_insert (buf);
        gtk_text_buffer_get_iter_at_mark (buf, &cur, mark);
 
        if ((key == GDK_KEY_Return || key == GDK_KEY_KP_Enter) &&
-           !(event->state & GDK_SHIFT_MASK) &&
+           !(state & GDK_SHIFT_MASK) &&
            priv->auto_indent)
        {
                /* Auto-indent means that when you press ENTER at the end of a
@@ -3948,15 +3961,6 @@ gtk_source_view_key_press_event (GtkWidget   *widget,
 
                if (indent != NULL)
                {
-                       /* Allow input methods to internally handle a key press event.
-                        * If this function returns TRUE, then no further processing should be done
-                        * for this keystroke. */
-                       if (gtk_text_view_im_context_filter_keypress (GTK_TEXT_VIEW (view), event))
-                       {
-                               g_free (indent);
-                               return GDK_EVENT_STOP;
-                       }
-
                        /* Delete any selected text to preserve behavior without auto-indent */
                        gtk_text_buffer_delete_selection (buf,
                                                          TRUE,
@@ -3973,8 +3977,7 @@ gtk_source_view_key_press_event (GtkWidget   *widget,
                        gtk_text_buffer_insert (buf, &cur, indent, strlen (indent));
                        g_free (indent);
                        gtk_text_buffer_end_user_action (buf);
-                       gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (widget),
-                                                           mark);
+                       gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (view), mark);
                        return GDK_EVENT_STOP;
                }
        }
@@ -3983,8 +3986,8 @@ gtk_source_view_key_press_event (GtkWidget   *widget,
         * with shift+tab key is GDK_ISO_Left_Tab (yay! on win32 and mac too!)
         */
        if ((key == GDK_KEY_Tab || key == GDK_KEY_KP_Tab || key == GDK_KEY_ISO_Left_Tab) &&
-           ((event->state & modifiers) == 0 ||
-            (event->state & modifiers) == GDK_SHIFT_MASK) &&
+           ((state & modifiers) == 0 ||
+            (state & modifiers) == GDK_SHIFT_MASK) &&
            editable &&
            gtk_text_view_get_accepts_tab (GTK_TEXT_VIEW (view)))
        {
@@ -3996,7 +3999,7 @@ gtk_source_view_key_press_event (GtkWidget   *widget,
                if (priv->indent_on_tab)
                {
                        /* shift+tab: always unindent */
-                       if (event->state & GDK_SHIFT_MASK)
+                       if (state & GDK_SHIFT_MASK)
                        {
                                _gtk_source_buffer_save_and_clear_selection (GTK_SOURCE_BUFFER (buf));
                                gtk_source_view_unindent_lines (view, &s, &e);
@@ -4025,14 +4028,14 @@ gtk_source_view_key_press_event (GtkWidget   *widget,
 
        if (key == GDK_KEY_BackSpace)
        {
-               if ((event->state & modifiers) == 0)
+               if ((state & modifiers) == 0)
                {
                        if (priv->smart_backspace && do_smart_backspace (view))
                        {
                                return GDK_EVENT_STOP;
                        }
                }
-               else if ((event->state & modifiers) == GDK_CONTROL_MASK)
+               else if ((state & modifiers) == GDK_CONTROL_MASK)
                {
                        if (do_ctrl_backspace (view))
                        {
@@ -4041,7 +4044,7 @@ gtk_source_view_key_press_event (GtkWidget   *widget,
                }
        }
 
-       return GTK_WIDGET_CLASS (gtk_source_view_parent_class)->key_press_event (widget, event);
+       return GDK_EVENT_PROPAGATE;
 }
 
 /**


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