2012-05-29 17:39:37 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2014-01-06 18:46:12 +00:00
|
|
|
#define INITIAL_HALIGN GTK_ALIGN_START
|
|
|
|
#define INITIAL_VALIGN GTK_ALIGN_START
|
|
|
|
|
|
|
|
static GList *menubuttons = NULL;
|
|
|
|
|
|
|
|
static void
|
|
|
|
horizontal_alignment_changed (GtkComboBox *box)
|
|
|
|
{
|
|
|
|
GtkAlign alignment = gtk_combo_box_get_active (box);
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
for (l = menubuttons; l != NULL; l = l->next) {
|
|
|
|
GtkMenu *popup = gtk_menu_button_get_popup (GTK_MENU_BUTTON (l->data));
|
|
|
|
if (popup != NULL)
|
|
|
|
gtk_widget_set_halign (GTK_WIDGET (popup), alignment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vertical_alignment_changed (GtkComboBox *box)
|
|
|
|
{
|
|
|
|
GtkAlign alignment = gtk_combo_box_get_active (box);
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
for (l = menubuttons; l != NULL; l = l->next) {
|
|
|
|
GtkMenu *popup = gtk_menu_button_get_popup (GTK_MENU_BUTTON (l->data));
|
|
|
|
if (popup != NULL)
|
|
|
|
gtk_widget_set_valign (GTK_WIDGET (popup), alignment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 17:39:37 +00:00
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
GtkWidget *window;
|
|
|
|
GtkWidget *button;
|
|
|
|
GtkWidget *grid;
|
|
|
|
GtkWidget *entry;
|
2014-01-06 18:46:12 +00:00
|
|
|
GtkWidget *label;
|
2014-04-29 01:53:03 +00:00
|
|
|
GtkWidget *check;
|
2014-01-06 18:46:12 +00:00
|
|
|
GtkWidget *combo;
|
2012-05-29 17:39:37 +00:00
|
|
|
GtkWidget *menu_widget;
|
2012-09-12 17:12:41 +00:00
|
|
|
GtkAccelGroup *accel_group;
|
2012-05-29 17:39:37 +00:00
|
|
|
guint i;
|
2014-01-06 18:46:12 +00:00
|
|
|
guint row = 0;
|
2012-05-29 17:39:37 +00:00
|
|
|
GMenu *menu;
|
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2012-05-29 17:39:37 +00:00
|
|
|
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtk_window_resize (GTK_WINDOW (window), 400, 300);
|
|
|
|
|
|
|
|
grid = gtk_grid_new ();
|
2014-01-06 18:11:22 +00:00
|
|
|
gtk_grid_set_row_spacing (GTK_GRID (grid), 12);
|
|
|
|
gtk_grid_set_column_spacing (GTK_GRID (grid), 12);
|
2012-05-29 17:39:37 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), grid);
|
|
|
|
|
2012-09-12 17:12:41 +00:00
|
|
|
accel_group = gtk_accel_group_new ();
|
|
|
|
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
|
|
|
|
|
2014-01-06 18:46:12 +00:00
|
|
|
/* horizontal alignment */
|
|
|
|
label = gtk_label_new ("Horizontal Alignment:");
|
|
|
|
gtk_widget_show (label);
|
|
|
|
gtk_grid_attach (GTK_GRID (grid), label, 0, row++, 1, 1);
|
|
|
|
|
|
|
|
combo = gtk_combo_box_text_new ();
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Fill");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Start");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "End");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Center");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Baseline");
|
|
|
|
gtk_combo_box_set_active (GTK_COMBO_BOX (combo), INITIAL_HALIGN);
|
|
|
|
gtk_widget_show (combo);
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), combo, label, GTK_POS_RIGHT, 1, 1);
|
|
|
|
g_signal_connect (G_OBJECT (combo), "changed",
|
|
|
|
G_CALLBACK (horizontal_alignment_changed), menubuttons);
|
|
|
|
|
|
|
|
/* vertical alignment */
|
|
|
|
label = gtk_label_new ("Vertical Alignment:");
|
|
|
|
gtk_widget_show (label);
|
|
|
|
gtk_grid_attach (GTK_GRID (grid), label, 0, row++, 1, 1);
|
|
|
|
|
|
|
|
combo = gtk_combo_box_text_new ();
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Fill");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Start");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "End");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Center");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Baseline");
|
|
|
|
gtk_combo_box_set_active (GTK_COMBO_BOX (combo), INITIAL_HALIGN);
|
|
|
|
gtk_widget_show (combo);
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), combo, label, GTK_POS_RIGHT, 1, 1);
|
|
|
|
g_signal_connect (G_OBJECT (combo), "changed",
|
|
|
|
G_CALLBACK (vertical_alignment_changed), menubuttons);
|
|
|
|
|
2012-05-29 17:39:37 +00:00
|
|
|
/* Button next to entry */
|
|
|
|
entry = gtk_entry_new ();
|
2014-01-06 18:46:12 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), entry, 0, row++, 1, 1);
|
2012-05-29 17:39:37 +00:00
|
|
|
button = gtk_menu_button_new ();
|
2014-01-06 18:46:12 +00:00
|
|
|
gtk_widget_set_halign (button, GTK_ALIGN_START);
|
|
|
|
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), button, entry, GTK_POS_RIGHT, 1, 1);
|
|
|
|
menubuttons = g_list_prepend (menubuttons, button);
|
2012-05-29 17:39:37 +00:00
|
|
|
|
|
|
|
/* Button with GtkMenu */
|
|
|
|
menu_widget = gtk_menu_new ();
|
testmenubutton: Fix inverted keynav in the GtkMenu
GtkMenu’s own keynav code, which actually bothers to account for the
layout of items, only happens if columns > 1. So, adding items to 1
column using a reverse loop meant they were placed in the Menu’s list of
children in that order, and because we only have 1 column, Menu passes
keynav up to MenuShell, which doesn’t adjust for the items’ positions.
‘Fix’ that here by adding items in the same order they’ll have when laid
out in the Menu, so keynav does what you’d expect, not the opposite. For
that, it’s simpler just to use gtk_container_add().
Let’s presume users are using add(), attach() with a non-inverted loop,
or attach() with arguments that create 2+ columns and so GtkMenu keynav.
2017-10-08 12:15:33 +00:00
|
|
|
for (i = 0; i < 5; ++i) {
|
2012-05-29 17:39:37 +00:00
|
|
|
GtkWidget *item;
|
|
|
|
|
testmenubutton: Fix inverted keynav in the GtkMenu
GtkMenu’s own keynav code, which actually bothers to account for the
layout of items, only happens if columns > 1. So, adding items to 1
column using a reverse loop meant they were placed in the Menu’s list of
children in that order, and because we only have 1 column, Menu passes
keynav up to MenuShell, which doesn’t adjust for the items’ positions.
‘Fix’ that here by adding items in the same order they’ll have when laid
out in the Menu, so keynav does what you’d expect, not the opposite. For
that, it’s simpler just to use gtk_container_add().
Let’s presume users are using add(), attach() with a non-inverted loop,
or attach() with arguments that create 2+ columns and so GtkMenu keynav.
2017-10-08 12:15:33 +00:00
|
|
|
if (i == 2) {
|
2013-06-24 21:31:22 +00:00
|
|
|
item = gtk_menu_item_new_with_mnemonic ("_Copy");
|
2012-09-12 17:12:41 +00:00
|
|
|
} else {
|
|
|
|
char *label;
|
|
|
|
|
testmenubutton: Fix inverted keynav in the GtkMenu
GtkMenu’s own keynav code, which actually bothers to account for the
layout of items, only happens if columns > 1. So, adding items to 1
column using a reverse loop meant they were placed in the Menu’s list of
children in that order, and because we only have 1 column, Menu passes
keynav up to MenuShell, which doesn’t adjust for the items’ positions.
‘Fix’ that here by adding items in the same order they’ll have when laid
out in the Menu, so keynav does what you’d expect, not the opposite. For
that, it’s simpler just to use gtk_container_add().
Let’s presume users are using add(), attach() with a non-inverted loop,
or attach() with arguments that create 2+ columns and so GtkMenu keynav.
2017-10-08 12:15:33 +00:00
|
|
|
label = g_strdup_printf ("Item _%d", i + 1);
|
2012-09-12 17:12:41 +00:00
|
|
|
item = gtk_menu_item_new_with_mnemonic (label);
|
|
|
|
g_free (label);
|
|
|
|
}
|
testmenubutton: Fix inverted keynav in the GtkMenu
GtkMenu’s own keynav code, which actually bothers to account for the
layout of items, only happens if columns > 1. So, adding items to 1
column using a reverse loop meant they were placed in the Menu’s list of
children in that order, and because we only have 1 column, Menu passes
keynav up to MenuShell, which doesn’t adjust for the items’ positions.
‘Fix’ that here by adding items in the same order they’ll have when laid
out in the Menu, so keynav does what you’d expect, not the opposite. For
that, it’s simpler just to use gtk_container_add().
Let’s presume users are using add(), attach() with a non-inverted loop,
or attach() with arguments that create 2+ columns and so GtkMenu keynav.
2017-10-08 12:15:33 +00:00
|
|
|
|
2012-09-12 17:12:41 +00:00
|
|
|
gtk_menu_item_set_use_underline (GTK_MENU_ITEM (item), TRUE);
|
testmenubutton: Fix inverted keynav in the GtkMenu
GtkMenu’s own keynav code, which actually bothers to account for the
layout of items, only happens if columns > 1. So, adding items to 1
column using a reverse loop meant they were placed in the Menu’s list of
children in that order, and because we only have 1 column, Menu passes
keynav up to MenuShell, which doesn’t adjust for the items’ positions.
‘Fix’ that here by adding items in the same order they’ll have when laid
out in the Menu, so keynav does what you’d expect, not the opposite. For
that, it’s simpler just to use gtk_container_add().
Let’s presume users are using add(), attach() with a non-inverted loop,
or attach() with arguments that create 2+ columns and so GtkMenu keynav.
2017-10-08 12:15:33 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (menu_widget), item);
|
2012-05-29 17:39:37 +00:00
|
|
|
}
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (menu_widget);
|
2012-05-29 17:39:37 +00:00
|
|
|
|
|
|
|
button = gtk_menu_button_new ();
|
2014-01-06 18:46:12 +00:00
|
|
|
gtk_widget_set_halign (button, GTK_ALIGN_START);
|
|
|
|
menubuttons = g_list_prepend (menubuttons, button);
|
2012-08-20 21:03:15 +00:00
|
|
|
gtk_menu_button_set_popup (GTK_MENU_BUTTON (button), menu_widget);
|
2014-01-06 18:46:12 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), button, 1, row++, 1, 1);
|
2012-05-29 17:39:37 +00:00
|
|
|
|
2014-04-29 01:53:03 +00:00
|
|
|
check = gtk_check_button_new_with_label ("Popover");
|
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), TRUE);
|
|
|
|
gtk_grid_attach (GTK_GRID (grid), check, 0, row, 1, 1);
|
|
|
|
|
2012-05-29 17:39:37 +00:00
|
|
|
/* Button with GMenuModel */
|
|
|
|
menu = g_menu_new ();
|
|
|
|
for (i = 5; i > 0; i--) {
|
|
|
|
char *label;
|
2013-09-09 02:37:50 +00:00
|
|
|
GMenuItem *item;
|
2012-09-10 16:38:35 +00:00
|
|
|
label = g_strdup_printf ("Item _%d", i);
|
2013-09-09 02:37:50 +00:00
|
|
|
item = g_menu_item_new (label, NULL);
|
|
|
|
if (i == 3)
|
|
|
|
g_menu_item_set_attribute (item, "icon", "s", "preferences-desktop-locale-symbolic");
|
2014-04-26 21:47:21 +00:00
|
|
|
g_menu_insert_item (menu, 0, item);
|
2013-09-09 02:37:50 +00:00
|
|
|
g_object_unref (item);
|
2012-05-29 17:39:37 +00:00
|
|
|
g_free (label);
|
|
|
|
}
|
2014-01-06 18:46:12 +00:00
|
|
|
|
2012-05-29 17:39:37 +00:00
|
|
|
button = gtk_menu_button_new ();
|
2014-04-29 01:53:03 +00:00
|
|
|
g_object_bind_property (check, "active", button, "use-popover", G_BINDING_SYNC_CREATE);
|
|
|
|
|
2014-01-06 18:46:12 +00:00
|
|
|
gtk_widget_set_halign (button, GTK_ALIGN_START);
|
|
|
|
menubuttons = g_list_prepend (menubuttons, button);
|
2012-05-29 17:39:37 +00:00
|
|
|
gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), G_MENU_MODEL (menu));
|
2014-01-06 18:46:12 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), button, 1, row++, 1, 1);
|
2012-05-29 17:39:37 +00:00
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2012-05-29 17:39:37 +00:00
|
|
|
|
|
|
|
gtk_main ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|