[gspell/wip/gtk4: 11/11] button-press-event -> GtkGesture conversion



commit 758facdccec0b7a0b257d0b5357483e2bcb38c26
Author: Timm Bäder <mail baedert org>
Date:   Sat Mar 10 15:32:46 2018 +0100

    button-press-event -> GtkGesture conversion
    
    First attempt. Also first problems.

 gspell/gspell-entry.c                      |   32 +++++++++++++++--------
 gspell/gspell-inline-checker-text-buffer.c |   39 +++++++++++++--------------
 2 files changed, 40 insertions(+), 31 deletions(-)
---
diff --git a/gspell/gspell-entry.c b/gspell/gspell-entry.c
index 9032466..6cc4bd5 100644
--- a/gspell/gspell-entry.c
+++ b/gspell/gspell-entry.c
@@ -597,25 +597,30 @@ popup_menu_cb (GtkEntry    *gtk_entry,
        return FALSE;
 }
 
-static gboolean
-button_press_event_cb (GtkEntry       *gtk_entry,
-                      GdkEventButton *event,
-                      GspellEntry    *gspell_entry)
+static void
+multipress_gesture_pressed_cb (GtkGestureMultiPress *gesture,
+                              double                x,
+                              double                y,
+                              GspellEntry          *gspell_entry)
 {
+       GtkEntry *gtk_entry;
        guint button;
+       GdkEventSequence *sequence;
+       const GdkEvent *event;
 
-       gdk_event_get_button ((GdkEvent *)event, &button);
+       gtk_entry = GTK_ENTRY (gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture)));
+       sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
+       event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
+       button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture));
 
        if (button == GDK_BUTTON_SECONDARY)
        {
                gspell_entry->popup_char_position =
-                       _gspell_entry_utils_get_char_position_at_event (gtk_entry, event);
+                       _gspell_entry_utils_get_char_position_at_event (gtk_entry, (GdkEventButton *)event);
        }
 
        _gspell_current_word_policy_cursor_moved (gspell_entry->current_word_policy);
        recheck_all (gspell_entry);
-
-       return GDK_EVENT_PROPAGATE;
 }
 
 static void
@@ -870,6 +875,8 @@ static void
 set_entry (GspellEntry *gspell_entry,
           GtkEntry    *gtk_entry)
 {
+       GtkGesture *multipress_gesture;
+
        g_return_if_fail (GTK_IS_ENTRY (gtk_entry));
 
        g_assert (gspell_entry->entry == NULL);
@@ -897,9 +904,12 @@ set_entry (GspellEntry *gspell_entry,
                          G_CALLBACK (popup_menu_cb),
                          gspell_entry);
 
-       g_signal_connect (gtk_entry,
-                         "button-press-event",
-                         G_CALLBACK (button_press_event_cb),
+       multipress_gesture = gtk_gesture_multi_press_new (GTK_WIDGET (gtk_entry));
+       /* TODO: We leak the gesture for now but the Gesture API is supposed to change soon so that
+        *       may not be a long-lasting problem. */
+       g_signal_connect (multipress_gesture,
+                         "pressed",
+                         G_CALLBACK (multipress_gesture_pressed_cb),
                          gspell_entry);
 
        /* connect_after, so when menu items are prepended, they have more
diff --git a/gspell/gspell-inline-checker-text-buffer.c b/gspell/gspell-inline-checker-text-buffer.c
index b6b9c99..e142826 100644
--- a/gspell/gspell-inline-checker-text-buffer.c
+++ b/gspell/gspell-inline-checker-text-buffer.c
@@ -997,36 +997,34 @@ language_notify_cb (GspellChecker                 *checker,
  * Here, we do NOT move the cursor to the location of the clicked-upon word
  * since that prevents the use of edit functions on the context menu.
  */
-static gboolean
-button_press_event_cb (GtkTextView                   *view,
-                      GdkEventButton                *event,
-                      GspellInlineCheckerTextBuffer *spell)
+static void
+multipress_gesture_pressed_cb (GtkGestureMultiPress          *gesture,
+                              double                         x,
+                              double                         y,
+                              GspellInlineCheckerTextBuffer *spell)
 {
+       GtkTextView *view;
        guint button;
 
-       gdk_event_get_button ((GdkEvent *)event, &button);
+       view = GTK_TEXT_VIEW (gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture)));
+       button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture));
 
        if (button == GDK_BUTTON_SECONDARY)
        {
                GtkTextBuffer *buffer = gtk_text_view_get_buffer (view);
                GtkTextIter iter;
-               double event_x, event_y;
-               gint x;
-               gint y;
-
-               gdk_event_get_coords ((GdkEvent *)event, &event_x, &event_y);
+               int buffer_x;
+               int buffer_y;
 
                gtk_text_view_window_to_buffer_coords (view,
                                                       GTK_TEXT_WINDOW_TEXT,
-                                                      event_x, event_y,
-                                                      &x, &y);
+                                                      x, y,
+                                                      &buffer_x, &buffer_y);
 
-               gtk_text_view_get_iter_at_location (view, &iter, x, y);
+               gtk_text_view_get_iter_at_location (view, &iter, buffer_x, buffer_y);
 
                gtk_text_buffer_move_mark (buffer, spell->mark_click, &iter);
        }
-
-       return GDK_EVENT_PROPAGATE;
 }
 
 /* Move the insert mark before popping up the menu, otherwise it
@@ -1414,16 +1412,17 @@ void
 _gspell_inline_checker_text_buffer_attach_view (GspellInlineCheckerTextBuffer *spell,
                                                GtkTextView                   *view)
 {
+       GtkGesture *multipress_gesture;
+
        g_return_if_fail (GSPELL_IS_INLINE_CHECKER_TEXT_BUFFER (spell));
        g_return_if_fail (GTK_IS_TEXT_VIEW (view));
        g_return_if_fail (gtk_text_view_get_buffer (view) == spell->buffer);
        g_return_if_fail (g_slist_find (spell->views, view) == NULL);
 
-       g_signal_connect_object (view,
-                                "button-press-event",
-                                G_CALLBACK (button_press_event_cb),
-                                spell,
-                                0);
+       multipress_gesture = gtk_gesture_multi_press_new (GTK_WIDGET (view));
+       /* TODO: We leak the gesture for now but the Gesture API is supposed to change soon so that
+        *       may not be a long-lasting problem. */
+       g_signal_connect (multipress_gesture, "pressed", G_CALLBACK (multipress_gesture_pressed_cb), spell);
 
        g_signal_connect_object (view,
                                 "popup-menu",


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