Make emoji completion work for GtkText

GtkEntry will stop using it directly in the following commits.
This commit is contained in:
Matthias Clasen 2019-02-15 15:46:51 -05:00
parent 1801bf6d83
commit fcb58887fa
2 changed files with 36 additions and 34 deletions

View File

@ -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,7 +302,7 @@ move_active_variation (GtkEmojiCompletion *completion,
}
static gboolean
entry_key_press (GtkEventControllerKey *key,
key_press_cb (GtkEventControllerKey *key,
guint keyval,
guint keycode,
GdkModifierType modifiers,
@ -368,27 +370,27 @@ entry_key_press (GtkEventControllerKey *key,
}
static gboolean
entry_focus_out (GtkWidget *entry,
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);
}
@ -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);
}

View File

@ -21,7 +21,7 @@
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gtk/gtkentry.h>
#include <gtk/gtktext.h>
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