gtk-demo: Stop using ::key-press-event in font explorer

Use a key controller instead. Note that this currently
breaks the handling of Enter, since we activate key bindings
twice, causing us to switch to the label and back.
This commit is contained in:
Matthias Clasen 2018-01-31 12:42:43 +01:00 committed by Carlos Garnacho
parent 8d396991db
commit 918d552472
4 changed files with 25 additions and 18 deletions

View File

@ -133,7 +133,6 @@
Разъяренный чтец эгоистично бьёт пятью жердями шустрого фехтовальщика. Наш банк вчера же выплатил Ф.Я. Эйхгольду комиссию за ценные вещи. Эх, чужак, общий съём цен шляп (юфть) вдрызг! В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!
Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός</property>
<signal name="key-press-event" handler="entry_key_press"/>
<signal name="activate" handler="stop_edit"/>
<property name="valign">start</property>
<property name="width-chars">50</property>

View File

@ -1669,12 +1669,12 @@ stop_edit (void)
}
static gboolean
entry_key_press (GtkEntry *entry, GdkEventKey *event)
entry_key_press (GtkEventController *controller,
guint keyval,
guint keycode,
GdkModifierType modifiers,
GtkEntry *entry)
{
guint keyval;
gdk_event_get_keyval ((GdkEvent *)event, &keyval);
if (keyval == GDK_KEY_Escape)
{
gtk_entry_set_text (GTK_ENTRY (entry), text);
@ -1694,6 +1694,7 @@ do_font_features (GtkWidget *do_widget)
{
GtkBuilder *builder;
GtkWidget *feature_list;
GtkEventController *controller;
builder = gtk_builder_new_from_resource ("/font_features/font-features.ui");
@ -1703,7 +1704,6 @@ do_font_features (GtkWidget *do_widget)
gtk_builder_add_callback_symbol (builder, "reset", reset_features);
gtk_builder_add_callback_symbol (builder, "stop_edit", G_CALLBACK (stop_edit));
gtk_builder_add_callback_symbol (builder, "toggle_edit", G_CALLBACK (toggle_edit));
gtk_builder_add_callback_symbol (builder, "entry_key_press", G_CALLBACK (entry_key_press));
gtk_builder_connect_signals (builder, NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
@ -1718,6 +1718,10 @@ do_font_features (GtkWidget *do_widget)
entry = GTK_WIDGET (gtk_builder_get_object (builder, "entry"));
edit_toggle = GTK_WIDGET (gtk_builder_get_object (builder, "edit_toggle"));
controller = gtk_event_controller_key_new (entry);
g_object_set_data_full (G_OBJECT (entry), "controller", controller, g_object_unref);
g_signal_connect (controller, "key-pressed", G_CALLBACK (entry_key_press), entry);
add_check_group (feature_list, _("Kerning"), (const char *[]){ "kern", NULL });
add_check_group (feature_list, _("Ligatures"), (const char *[]){ "liga",
"dlig",

View File

@ -105,14 +105,14 @@ follow_if_link (GtkWidget *text_view,
/* Links can be activated by pressing Enter.
*/
static gboolean
key_press_event (GtkWidget *text_view,
GdkEventKey *event)
key_pressed (GtkEventController *controller,
guint keyval,
guint keycode,
GdkModifierType modifiers,
GtkWidget *text_view)
{
GtkTextIter iter;
GtkTextBuffer *buffer;
guint keyval;
gdk_event_get_keyval ((GdkEvent *)event, &keyval);
switch (keyval)
{
@ -128,7 +128,7 @@ key_press_event (GtkWidget *text_view,
break;
}
return FALSE;
return GDK_EVENT_PROPAGATE;
}
static void set_cursor_if_appropriate (GtkTextView *text_view,
@ -241,6 +241,7 @@ do_hypertext (GtkWidget *do_widget)
GtkWidget *view;
GtkWidget *sw;
GtkTextBuffer *buffer;
GtkEventController *controller;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Hypertext");
@ -255,8 +256,9 @@ do_hypertext (GtkWidget *do_widget)
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view), GTK_WRAP_WORD);
gtk_text_view_set_left_margin (GTK_TEXT_VIEW (view), 20);
gtk_text_view_set_right_margin (GTK_TEXT_VIEW (view), 20);
g_signal_connect (view, "key-press-event",
G_CALLBACK (key_press_event), NULL);
controller = gtk_event_controller_key_new (view);
g_object_set_data_full (G_OBJECT (view), "controller", controller, g_object_unref);
g_signal_connect (controller, "key-pressed", G_CALLBACK (key_pressed), view);
g_signal_connect (view, "event",
G_CALLBACK (event_cb), NULL);

View File

@ -31,7 +31,10 @@ window_key_press_event_cb (GtkWidget *widget,
GdkEvent *event,
GtkSearchBar *bar)
{
return gtk_search_bar_handle_event (bar, event);
if (gdk_event_get_event_type (event) == GDK_KEY_PRESS)
return gtk_search_bar_handle_event (bar, event);
return GDK_EVENT_PROPAGATE;
}
static void
@ -99,8 +102,7 @@ do_search_entry2 (GtkWidget *do_widget)
gtk_box_pack_start (GTK_BOX (vbox), searchbar);
/* Hook the search bar to key presses */
g_signal_connect (window, "key-press-event",
G_CALLBACK (window_key_press_event_cb), searchbar);
g_signal_connect (window, "event", G_CALLBACK (window_key_press_event_cb), searchbar);
/* Help */
label = gtk_label_new ("Start Typing to search");