[gtk+] GtkBuilder: pick up named submenus



commit a991bb9927fd9073bbd47b530c3828a8a5a6c617
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Dec 14 00:13:32 2011 -0500

    GtkBuilder: pick up named submenus
    
    When parsing GMenu xml, use the facility for pick up named submenus.
    Note that <section id='foo'>...</section> is _not_ a named submenu.
    It is a named item with an unnamed submenu :-(

 gtk/gtkbuilderparser.c |   40 ++++++++++++++++++++++++++++------------
 gtk/tests/builder.c    |    6 ++++--
 2 files changed, 32 insertions(+), 14 deletions(-)
---
diff --git a/gtk/gtkbuilderparser.c b/gtk/gtkbuilderparser.c
index 253b4fd..6d36140 100644
--- a/gtk/gtkbuilderparser.c
+++ b/gtk/gtkbuilderparser.c
@@ -428,6 +428,14 @@ free_object_info (ObjectInfo *info)
 }
 
 static void
+free_menu_info (MenuInfo *info)
+{
+  g_free (info->id);
+  g_hash_table_unref (info->objects);
+  g_slice_free (MenuInfo, info);
+}
+
+static void
 parse_child (ParserData   *data,
              const gchar  *element_name,
              const gchar **names,
@@ -834,20 +842,20 @@ parse_menu (GMarkupParseContext  *context,
 {
   gchar *id;
   ParserData *data = user_data;
-  ObjectInfo *object_info;
+  MenuInfo *menu_info;
 
   if (!g_markup_collect_attributes (element_name, names, values, error,
                                     G_MARKUP_COLLECT_STRING, "id", &id,
                                     G_MARKUP_COLLECT_INVALID))
     return FALSE;
 
-  object_info = g_slice_new0 (ObjectInfo);
-  object_info->class_name = g_strdup ("GMenu");
-  object_info->id = g_strdup (id);
-  object_info->tag.name = element_name;
-  state_push (data, object_info);
+  menu_info = g_slice_new0 (MenuInfo);
+  menu_info->tag.name = element_name;
+  menu_info->id = g_strdup (id);
+  menu_info->objects = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
+  state_push (data, menu_info);
 
-  g_menu_markup_parser_start_menu (context, data->domain, NULL);
+  g_menu_markup_parser_start_menu (context, data->domain, menu_info->objects);
 
   return TRUE;
 }
@@ -987,15 +995,23 @@ end_element (GMarkupParseContext *context,
     }
   else if (strcmp (element_name, "menu") == 0)
     {
-      ObjectInfo *object_info;
+      MenuInfo *menu_info;
       GObject *menu;
+      GHashTableIter iter;
+      const gchar *id;
 
-      object_info = state_pop_info (data, ObjectInfo);
+      menu_info = state_pop_info (data, MenuInfo);
       menu = (GObject*)g_menu_markup_parser_end_menu (context);
-      _gtk_builder_add_object (data->builder, object_info->id, menu);
+      _gtk_builder_add_object (data->builder, menu_info->id, menu);
       g_object_unref (menu);
 
-      free_object_info (object_info);
+      g_hash_table_iter_init (&iter, menu_info->objects);
+      while (g_hash_table_iter_next (&iter, (gpointer*)&id, (gpointer*)&menu))
+        {
+          _gtk_builder_add_object (data->builder, id, menu);
+        }
+
+      free_menu_info (menu_info);
     }
   else if (data->requested_objects && !data->inside_requested_object)
     {
@@ -1142,7 +1158,7 @@ free_info (CommonInfo *info)
   else if (strcmp (info->tag.name, "requires") == 0)
     _free_requires_info ((RequiresInfo *)info, NULL);
   else if (strcmp (info->tag.name, "menu") == 0)
-    free_object_info ((ObjectInfo *)info);
+    free_menu_info ((MenuInfo *)info);
   else
     g_assert_not_reached ();
 }
diff --git a/gtk/tests/builder.c b/gtk/tests/builder.c
index ba356fb..0a3b6f6 100644
--- a/gtk/tests/builder.c
+++ b/gtk/tests/builder.c
@@ -2592,14 +2592,14 @@ test_gmenu (void)
     "      <item label='Copy' action='copy'/>"
     "      <item label='Paste' action='paste'/>"
     "    </section>"
-    "    <section>"
+    "    <item><link name='section' id='blargh'>"
     "      <item label='Bold' action='bold'/>"
     "      <submenu label='Language'>"
     "        <item label='Latin' action='lang' target='latin'/>"
     "        <item label='Greek' action='lang' target='greek'/>"
     "        <item label='Urdu'  action='lang' target='urdu'/>"
     "      </submenu>"
-    "    </section>"
+    "    </link></item>"
     "  </menu>"
     "</interface>";
 
@@ -2608,6 +2608,8 @@ test_gmenu (void)
   g_assert (GTK_IS_WINDOW (obj));
   obj1 = gtk_builder_get_object (builder, "edit-menu");
   g_assert (G_IS_MENU_MODEL (obj1));
+  obj1 = gtk_builder_get_object (builder, "blargh");
+  g_assert (G_IS_MENU_MODEL (obj1));
   g_object_unref (builder);
 }
 



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