[gtk+/wip/gmenu: 3/7] Add code for dynamic changes



commit 2757989bfc8092a6f511f36f22f392205d5b6ab3
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Nov 1 00:30:31 2011 -0400

    Add code for dynamic changes
    
    Add and remove items and actions - we don't update
    the menus properly yet.

 tests/testgmenu.c |   98 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 98 insertions(+), 0 deletions(-)
---
diff --git a/tests/testgmenu.c b/tests/testgmenu.c
index 029863b..9dd3ca4 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)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]