testgtk: Port keyval example to shortcut controllers

This commit is contained in:
Emmanuele Bassi 2020-02-06 16:55:34 +00:00 committed by Matthias Clasen
parent 29ced09dc4
commit 1a55f59f23

View File

@ -1603,14 +1603,15 @@ create_listbox (GtkWidget *widget)
static GtkWidget * static GtkWidget *
accel_button_new (GtkAccelGroup *accel_group, accel_button_new (const gchar *text,
const gchar *text, const gchar *accel)
const gchar *accel)
{ {
guint keyval; guint keyval;
GdkModifierType modifiers; GdkModifierType modifiers;
GtkWidget *button; GtkWidget *button;
GtkWidget *label; GtkWidget *label;
GtkEventController *controller;
GtkShortcut *shortcut;
if (!gtk_accelerator_parse (accel, &keyval, &modifiers)) if (!gtk_accelerator_parse (accel, &keyval, &modifiers))
{ {
@ -1618,12 +1619,17 @@ accel_button_new (GtkAccelGroup *accel_group,
} }
button = gtk_button_new (); button = gtk_button_new ();
gtk_widget_add_accelerator (button, "activate", accel_group, controller = gtk_shortcut_controller_new ();
keyval, modifiers, GTK_ACCEL_VISIBLE | GTK_ACCEL_LOCKED); gtk_shortcut_controller_set_scope (GTK_SHORTCUT_CONTROLLER (controller), GTK_SHORTCUT_SCOPE_GLOBAL);
gtk_event_controller_set_propagation_phase (controller, GTK_PHASE_CAPTURE);
shortcut = gtk_shortcut_new (gtk_keyval_trigger_new (keyval, modifiers),
gtk_activate_action_new ());
gtk_shortcut_controller_add_shortcut (GTK_SHORTCUT_CONTROLLER (controller), shortcut);
g_object_unref (shortcut);
gtk_widget_add_controller (button, controller);
label = gtk_accel_label_new (text); label = gtk_accel_label_new (text);
gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), button); gtk_accel_label_set_accel (GTK_ACCEL_LABEL (label), keyval, modifiers);
gtk_widget_show (label);
gtk_container_add (GTK_CONTAINER (button), label); gtk_container_add (GTK_CONTAINER (button), label);
@ -1638,7 +1644,6 @@ create_key_lookup (GtkWidget *widget)
if (!window) if (!window)
{ {
GtkAccelGroup *accel_group = gtk_accel_group_new ();
GtkWidget *button; GtkWidget *button;
GtkWidget *content_area; GtkWidget *content_area;
@ -1653,8 +1658,6 @@ create_key_lookup (GtkWidget *widget)
*/ */
gtk_window_set_default_size (GTK_WINDOW (window), 300, -1); gtk_window_set_default_size (GTK_WINDOW (window), 300, -1);
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (window)); content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));
button = gtk_button_new_with_mnemonic ("Button 1 (_a)"); button = gtk_button_new_with_mnemonic ("Button 1 (_a)");
@ -1667,23 +1670,23 @@ create_key_lookup (GtkWidget *widget)
gtk_container_add (GTK_CONTAINER (content_area), button); gtk_container_add (GTK_CONTAINER (content_area), button);
button = gtk_button_new_with_mnemonic ("Button 6 (_b)"); button = gtk_button_new_with_mnemonic ("Button 6 (_b)");
gtk_container_add (GTK_CONTAINER (content_area), button); gtk_container_add (GTK_CONTAINER (content_area), button);
button = accel_button_new (accel_group, "Button 7", "<Alt><Shift>b"); button = accel_button_new ("Button 7", "<Alt><Shift>b");
gtk_container_add (GTK_CONTAINER (content_area), button); gtk_container_add (GTK_CONTAINER (content_area), button);
button = accel_button_new (accel_group, "Button 8", "<Alt>d"); button = accel_button_new ("Button 8", "<Alt>d");
gtk_container_add (GTK_CONTAINER (content_area), button); gtk_container_add (GTK_CONTAINER (content_area), button);
button = accel_button_new (accel_group, "Button 9", "<Alt>Cyrillic_ve"); button = accel_button_new ("Button 9", "<Alt>Cyrillic_ve");
gtk_container_add (GTK_CONTAINER (content_area), button); gtk_container_add (GTK_CONTAINER (content_area), button);
button = gtk_button_new_with_mnemonic ("Button 10 (_1)"); button = gtk_button_new_with_mnemonic ("Button 10 (_1)");
gtk_container_add (GTK_CONTAINER (content_area), button); gtk_container_add (GTK_CONTAINER (content_area), button);
button = gtk_button_new_with_mnemonic ("Button 11 (_!)"); button = gtk_button_new_with_mnemonic ("Button 11 (_!)");
gtk_container_add (GTK_CONTAINER (content_area), button); gtk_container_add (GTK_CONTAINER (content_area), button);
button = accel_button_new (accel_group, "Button 12", "<Super>a"); button = accel_button_new ("Button 12", "<Super>a");
gtk_container_add (GTK_CONTAINER (content_area), button); gtk_container_add (GTK_CONTAINER (content_area), button);
button = accel_button_new (accel_group, "Button 13", "<Hyper>a"); button = accel_button_new ("Button 13", "<Hyper>a");
gtk_container_add (GTK_CONTAINER (content_area), button); gtk_container_add (GTK_CONTAINER (content_area), button);
button = accel_button_new (accel_group, "Button 14", "<Meta>a"); button = accel_button_new ("Button 14", "<Meta>a");
gtk_container_add (GTK_CONTAINER (content_area), button); gtk_container_add (GTK_CONTAINER (content_area), button);
button = accel_button_new (accel_group, "Button 15", "<Shift><Mod4>b"); button = accel_button_new ("Button 15", "<Shift><Mod4>b");
gtk_container_add (GTK_CONTAINER (content_area), button); gtk_container_add (GTK_CONTAINER (content_area), button);
window_ptr = &window; window_ptr = &window;