Commit cbf5b49c authored by Matthias Clasen's avatar Matthias Clasen

Add an example for accelerators with manual image menuitems

This is somewhat tricky to work out, so put some example
code here for future reference. The tricky part is that
GtkAccelLabel tries to be smart about hiding the accel
if there's not enough space, so we have to make sure to
pack the label with expand=TRUE and set align=GTK_ALIGN_FILL,
or things won't work.
parent b3725a45
...@@ -10,8 +10,10 @@ int main (int argc, char **argv) ...@@ -10,8 +10,10 @@ int main (int argc, char **argv)
GtkWidget *menu; GtkWidget *menu;
GtkWidget *item; GtkWidget *item;
GtkWidget *box; GtkWidget *box;
GtkWidget *label;
GtkAction *action1; GtkAction *action1;
GtkAction *action2; GtkAction *action2;
GtkAccelGroup *accel_group;
gtk_init (&argc, &argv); gtk_init (&argc, &argv);
...@@ -21,6 +23,9 @@ int main (int argc, char **argv) ...@@ -21,6 +23,9 @@ int main (int argc, char **argv)
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
accel_group = gtk_accel_group_new ();
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
grid = gtk_grid_new (); grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid); gtk_container_add (GTK_CONTAINER (window), grid);
...@@ -58,6 +63,9 @@ int main (int argc, char **argv) ...@@ -58,6 +63,9 @@ int main (int argc, char **argv)
gtk_container_add (GTK_CONTAINER (grid), button); gtk_container_add (GTK_CONTAINER (grid), button);
menu = gtk_menu_new (); menu = gtk_menu_new ();
gtk_menu_set_accel_group (GTK_MENU (menu), accel_group);
gtk_menu_set_accel_path (GTK_MENU (menu), "<menu>/TEST");
gtk_menu_button_set_popup (GTK_MENU_BUTTON (button), menu); gtk_menu_button_set_popup (GTK_MENU_BUTTON (button), menu);
/* plain old stock menuitem */ /* plain old stock menuitem */
...@@ -65,7 +73,7 @@ int main (int argc, char **argv) ...@@ -65,7 +73,7 @@ int main (int argc, char **argv)
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
/* gtk_image_menu_item_set_always_show_image still works */ /* gtk_image_menu_item_set_always_show_image still works */
item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLOSE, NULL); item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLOSE, accel_group);
gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE); gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
...@@ -74,9 +82,17 @@ int main (int argc, char **argv) ...@@ -74,9 +82,17 @@ int main (int argc, char **argv)
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12); box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
gtk_container_add (GTK_CONTAINER (item), box); gtk_container_add (GTK_CONTAINER (item), box);
gtk_container_add (GTK_CONTAINER (box), gtk_image_new_from_icon_name ("edit-clear", GTK_ICON_SIZE_MENU)); gtk_container_add (GTK_CONTAINER (box), gtk_image_new_from_icon_name ("edit-clear", GTK_ICON_SIZE_MENU));
gtk_container_add (GTK_CONTAINER (box), gtk_label_new_with_mnemonic ("_Clear")); label = gtk_accel_label_new ("C_lear");
gtk_label_set_use_underline (GTK_LABEL (label), TRUE);
gtk_label_set_xalign (GTK_LABEL (label), 0.0);
gtk_widget_set_halign (label, GTK_ALIGN_FILL);
gtk_widget_add_accelerator (item, "activate", accel_group,
gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), item);
gtk_box_pack_end (GTK_BOX (box), label, TRUE, TRUE, 0);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
/* GtkAction-backed menuitem */ /* GtkAction-backed menuitem */
item = gtk_image_menu_item_new (); item = gtk_image_menu_item_new ();
gtk_activatable_set_related_action (GTK_ACTIVATABLE (item), action1); gtk_activatable_set_related_action (GTK_ACTIVATABLE (item), action1);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment