diff options
author | Matthias Clasen <mclasen@redhat.com> | 2011-11-01 00:30:31 -0400 |
---|---|---|
committer | Ryan Lortie <desrt@desrt.ca> | 2011-12-19 12:45:50 -0500 |
commit | c62ed7e3a32b025c8ab96812df64f106c2a05161 (patch) | |
tree | 77f17933e81838d5e7ef3fd3add3b10b1cec85e5 /tests | |
parent | bfa53a9df55eeffef21995d634cd7fc76ffe90da (diff) | |
download | gtk+-c62ed7e3a32b025c8ab96812df64f106c2a05161.tar.gz |
Add code for dynamic changes
Add and remove items and actions - we don't update
the menus properly yet.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/testgmenu.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/tests/testgmenu.c b/tests/testgmenu.c index 029863b32e..9dd3ca463d 100644 --- a/tests/testgmenu.c +++ b/tests/testgmenu.c @@ -596,6 +596,8 @@ create_action_treeview (GActionGroup *group) gtk_list_store_set (values, &iter, 0, "greek", -1); gtk_list_store_append (values, &iter); gtk_list_store_set (values, &iter, 0, "urdu", -1); + gtk_list_store_append (values, &iter); + gtk_list_store_set (values, &iter, 0, "sumerian", -1); g_object_set (cell, "has-entry", FALSE, "model", values, @@ -610,6 +612,99 @@ create_action_treeview (GActionGroup *group) return tv; } +/* Dynamic menu changes {{{1 */ + +static void +toggle_sumerian (GtkToggleButton *button, gpointer data) +{ + GMenuModel *model; + gboolean adding; + GMenuModel *m; + + model = g_object_get_data (G_OBJECT (button), "model"); + + adding = gtk_toggle_button_get_active (button); + + m = g_menu_model_get_item_link (model, g_menu_model_get_n_items (model) - 1, G_MENU_LINK_SECTION); + m = g_menu_model_get_item_link (m, g_menu_model_get_n_items (m) - 1, G_MENU_LINK_SUBMENU); + if (adding) + g_menu_append (G_MENU (m), "Sumerian", "lang::sumerian"); + else + g_menu_remove (G_MENU (m), g_menu_model_get_n_items (m) - 1); +} + +static void +toggle_italic (GtkToggleButton *button, gpointer data) +{ + GMenuModel *model; + GActionGroup *group; + GSimpleAction *action; + gboolean adding; + GMenuModel *m; + GtkTreeView *tv = data; + GtkTreeModel *store; + GtkTreeIter iter; + + model = g_object_get_data (G_OBJECT (button), "model"); + group = g_object_get_data (G_OBJECT (button), "group"); + + store = gtk_tree_view_get_model (tv); + + adding = gtk_toggle_button_get_active (button); + + m = g_menu_model_get_item_link (model, g_menu_model_get_n_items (model) - 1, G_MENU_LINK_SECTION); + if (adding) + { + action = g_simple_action_new_stateful ("italic", NULL, g_variant_new_boolean (FALSE)); + g_simple_action_group_insert (G_SIMPLE_ACTION_GROUP (group), + G_ACTION (action)); + g_object_unref (action); + g_menu_insert (G_MENU (m), 1, "Italic", "italic"); + gtk_list_store_prepend (GTK_LIST_STORE (store), &iter); + gtk_list_store_set (GTK_LIST_STORE (store), &iter, + 0, "italic", + -1); + } + else + { + g_simple_action_group_remove (G_SIMPLE_ACTION_GROUP (group), "italic"); + g_menu_remove (G_MENU (m), 1); + gtk_tree_model_get_iter_first (store, &iter); + gtk_list_store_remove (GTK_LIST_STORE (store), &iter); + } +} + +static GtkWidget * +create_add_remove_buttons (GActionGroup *group, + GMenuModel *model, + GtkWidget *treeview) +{ + GtkWidget *box; + GtkWidget *button; + + box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6); + + button = gtk_check_button_new_with_label ("Add Italic"); + gtk_container_add (GTK_CONTAINER (box), button); + + g_object_set_data (G_OBJECT (button), "group", group); + g_object_set_data (G_OBJECT (button), "model", model); + + g_signal_connect (button, "toggled", + G_CALLBACK (toggle_italic), treeview); + + button = gtk_check_button_new_with_label ("Add Sumerian"); + gtk_container_add (GTK_CONTAINER (box), button); + + g_object_set_data (G_OBJECT (button), "group", group); + g_object_set_data (G_OBJECT (button), "model", model); + + g_signal_connect (button, "toggled", + G_CALLBACK (toggle_sumerian), NULL); + + return box; +} + /* The menu button {{{1 */ static void @@ -665,6 +760,7 @@ main (int argc, char *argv[]) GtkWidget *box; GtkWidget *button; GtkWidget *tv; + GtkWidget *buttons; GMenuModel *model; GActionGroup *group; GDBusConnection *bus; @@ -705,6 +801,8 @@ main (int argc, char *argv[]) tv = create_action_treeview (group); gtk_container_add (GTK_CONTAINER (box), tv); + buttons = create_add_remove_buttons (group, model, tv); + gtk_container_add (GTK_CONTAINER (box), buttons); } if (do_export) |