From fcb58887fa6edbaabce3201f6e4855ece96f9b72 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 15 Feb 2019 15:46:51 -0500 Subject: [PATCH] Make emoji completion work for GtkText GtkEntry will stop using it directly in the following commits. --- gtk/gtkemojicompletion.c | 66 +++++++++++++++++++++------------------- gtk/gtkemojicompletion.h | 4 +-- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/gtk/gtkemojicompletion.c b/gtk/gtkemojicompletion.c index b3c62278e8..3c8796f920 100644 --- a/gtk/gtkemojicompletion.c +++ b/gtk/gtkemojicompletion.c @@ -19,7 +19,8 @@ #include "gtkemojicompletion.h" -#include "gtkentryprivate.h" +#include "gtktextprivate.h" +#include "gtkeditable.h" #include "gtkbox.h" #include "gtkcssprovider.h" #include "gtklistbox.h" @@ -28,6 +29,7 @@ #include "gtkintl.h" #include "gtkprivate.h" #include "gtkgesturelongpress.h" +#include "gtkeventcontrollerkey.h" #include "gtkflowbox.h" #include "gtkstack.h" @@ -35,7 +37,7 @@ struct _GtkEmojiCompletion { GtkPopover parent_instance; - GtkEntry *entry; + GtkText *entry; char *text; guint length; guint offset; @@ -54,7 +56,7 @@ struct _GtkEmojiCompletionClass { }; static void connect_signals (GtkEmojiCompletion *completion, - GtkEntry *entry); + GtkText *text); static void disconnect_signals (GtkEmojiCompletion *completion); static int populate_completion (GtkEmojiCompletion *completion, const char *text, @@ -86,7 +88,7 @@ update_completion (GtkEmojiCompletion *completion) n_matches = 0; - text = gtk_entry_get_text (GTK_ENTRY (completion->entry)); + text = gtk_editable_get_text (GTK_EDITABLE (completion->entry)); length = strlen (text); if (length > 0) @@ -125,7 +127,8 @@ next: } static void -entry_changed (GtkEntry *entry, GtkEmojiCompletion *completion) +changed_cb (GtkText *text, + GtkEmojiCompletion *completion) { update_completion (completion); } @@ -143,9 +146,9 @@ emoji_activated (GtkWidget *row, g_signal_handler_block (completion->entry, completion->changed_id); - length = g_utf8_strlen (gtk_entry_get_text (completion->entry), -1); - gtk_entry_set_positions (completion->entry, length - completion->length, length); - gtk_entry_enter_text (completion->entry, emoji); + length = g_utf8_strlen (gtk_editable_get_text (GTK_EDITABLE (completion->entry)), -1); + gtk_editable_select_region (GTK_EDITABLE (completion->entry), length - completion->length, length); + gtk_text_enter_text (completion->entry, emoji); g_signal_handler_unblock (completion->entry, completion->changed_id); } @@ -167,7 +170,6 @@ child_activated (GtkFlowBox *box, { GtkEmojiCompletion *completion = data; - g_print ("child activated\n"); emoji_activated (GTK_WIDGET (child), completion); } @@ -300,11 +302,11 @@ move_active_variation (GtkEmojiCompletion *completion, } static gboolean -entry_key_press (GtkEventControllerKey *key, - guint keyval, - guint keycode, - GdkModifierType modifiers, - GtkEmojiCompletion *completion) +key_press_cb (GtkEventControllerKey *key, + guint keyval, + guint keycode, + GdkModifierType modifiers, + GtkEmojiCompletion *completion) { if (!gtk_widget_get_visible (GTK_WIDGET (completion))) return FALSE; @@ -368,27 +370,27 @@ entry_key_press (GtkEventControllerKey *key, } static gboolean -entry_focus_out (GtkWidget *entry, - GParamSpec *pspec, - GtkEmojiCompletion *completion) +focus_out_cb (GtkWidget *text, + GParamSpec *pspec, + GtkEmojiCompletion *completion) { - if (!gtk_widget_has_focus (entry)) + if (!gtk_widget_has_focus (text)) gtk_popover_popdown (GTK_POPOVER (completion)); return FALSE; } static void connect_signals (GtkEmojiCompletion *completion, - GtkEntry *entry) + GtkText *entry) { GtkEventController *key_controller; completion->entry = g_object_ref (entry); - key_controller = gtk_entry_get_key_controller (entry); + key_controller = gtk_text_get_key_controller (entry); - completion->changed_id = g_signal_connect (entry, "changed", G_CALLBACK (entry_changed), completion); - g_signal_connect (key_controller, "key-pressed", G_CALLBACK (entry_key_press), completion); - g_signal_connect (entry, "notify::has-focus", G_CALLBACK (entry_focus_out), completion); + g_signal_connect (key_controller, "key-pressed", G_CALLBACK (key_press_cb), completion); + completion->changed_id = g_signal_connect (entry, "changed", G_CALLBACK (changed_cb), completion); + g_signal_connect (entry, "notify::has-focus", G_CALLBACK (focus_out_cb), completion); } static void @@ -396,11 +398,11 @@ disconnect_signals (GtkEmojiCompletion *completion) { GtkEventController *key_controller; - key_controller = gtk_entry_get_key_controller (completion->entry); + key_controller = gtk_text_get_key_controller (completion->entry); - g_signal_handlers_disconnect_by_func (completion->entry, entry_changed, completion); - g_signal_handlers_disconnect_by_func (key_controller, entry_key_press, completion); - g_signal_handlers_disconnect_by_func (completion->entry, entry_focus_out, completion); + g_signal_handlers_disconnect_by_func (completion->entry, changed_cb, completion); + g_signal_handlers_disconnect_by_func (key_controller, key_press_cb, completion); + g_signal_handlers_disconnect_by_func (completion->entry, focus_out_cb, completion); g_clear_object (&completion->entry); } @@ -554,8 +556,8 @@ add_emoji (GtkWidget *list, static int populate_completion (GtkEmojiCompletion *completion, - const char *text, - guint offset) + const char *text, + guint offset) { GList *children, *l; guint n_matches; @@ -657,15 +659,15 @@ gtk_emoji_completion_class_init (GtkEmojiCompletionClass *klass) } GtkWidget * -gtk_emoji_completion_new (GtkEntry *entry) +gtk_emoji_completion_new (GtkText *text) { GtkEmojiCompletion *completion; completion = GTK_EMOJI_COMPLETION (g_object_new (GTK_TYPE_EMOJI_COMPLETION, - "relative-to", entry, + "relative-to", text, NULL)); - connect_signals (completion, entry); + connect_signals (completion, text); return GTK_WIDGET (completion); } diff --git a/gtk/gtkemojicompletion.h b/gtk/gtkemojicompletion.h index ff7cb1fa63..620c1395a1 100644 --- a/gtk/gtkemojicompletion.h +++ b/gtk/gtkemojicompletion.h @@ -21,7 +21,7 @@ #error "Only can be included directly." #endif -#include +#include G_BEGIN_DECLS @@ -36,6 +36,6 @@ typedef struct _GtkEmojiCompletion GtkEmojiCompletion; typedef struct _GtkEmojiCompletionClass GtkEmojiCompletionClass; GType gtk_emoji_completion_get_type (void) G_GNUC_CONST; -GtkWidget *gtk_emoji_completion_new (GtkEntry *entry); +GtkWidget *gtk_emoji_completion_new (GtkText *text); G_END_DECLS