diff --git a/gtk/tools/gtk-builder-tool-simplify.c b/gtk/tools/gtk-builder-tool-simplify.c index 76dc3ea840..3266fcf0ff 100644 --- a/gtk/tools/gtk-builder-tool-simplify.c +++ b/gtk/tools/gtk-builder-tool-simplify.c @@ -183,6 +183,38 @@ needs_explicit_setting (GParamSpec *pspec, return found; } +static gboolean +keep_for_rewrite (const char *class_name, + const char *prop_name, + gboolean packing) +{ + struct _Prop { + const char *class; + const char *property; + gboolean packing; + } props[] = { + { "GtkActionBar", "pack-type", 1 }, + { "GtkHeaderBar", "pack-type", 1 }, + { "GtkPopoverMenu", "submenu", 1 }, + }; + gboolean found; + gint k; + + found = FALSE; + for (k = 0; k < G_N_ELEMENTS (props); k++) + { + if (strcmp (class_name, props[k].class) == 0 && + strcmp (prop_name, props[k].property) == 0 && + packing == props[k].packing) + { + found = TRUE; + break; + } + } + + return found; +} + static gboolean is_pcdata_element (Element *element) { @@ -323,6 +355,23 @@ value_is_default (MyParserData *data, return ret; } +static gboolean +has_attribute (Element *elt, + const char *name, + const char *value) +{ + int i; + + for (i = 0; elt->attribute_names[i]; i++) + { + if (strcmp (elt->attribute_names[i], name) == 0 && + (value == NULL || strcmp (elt->attribute_values[i], value) == 0)) + return TRUE; + } + + return FALSE; +} + static const char * get_attribute_value (Element *element, const char *name) @@ -426,9 +475,7 @@ property_can_be_omitted (Element *element, property_name = (const gchar *)element->attribute_values[i]; } - if ((strcmp (class_name, "GtkActionBar") == 0 || - strcmp (class_name, "GtkHeaderBar") == 0) && - strcmp (property_name, "pack-type") == 0) + if (keep_for_rewrite (class_name, property_name, packing)) return FALSE; /* keep, will be rewritten */ if (translatable) @@ -646,23 +693,6 @@ rewrite_assistant (Element *element, element->children = new_children; } -static gboolean -has_attribute (Element *elt, - const char *name, - const char *value) -{ - int i; - - for (i = 0; elt->attribute_names[i]; i++) - { - if (strcmp (elt->attribute_names[i], name) == 0 && - (value == NULL || strcmp (elt->attribute_values[i], value) == 0)) - return TRUE; - } - - return FALSE; -} - static Element * rewrite_notebook_page (Element *child, Element *tab, MyParserData *data) { @@ -792,10 +822,18 @@ rewrite_pack_type_child (Element *element, { pack_type = elt2; elt->children = g_list_remove (elt->children, pack_type); + if (elt->children == NULL) + { + element->children = g_list_remove (element->children, elt); + free_element (elt); + } break; } } } + + if (pack_type) + break; } if (pack_type) @@ -837,6 +875,81 @@ rewrite_pack_type (Element *element, } } +static void +rewrite_popover_menu_child (Element *element, + MyParserData *data) +{ + Element *submenu = NULL; + Element *object = NULL; + GList *l, *ll; + + if (!g_str_equal (element->element_name, "child")) + return; + + for (l = element->children; l; l = l->next) + { + Element *elt = l->data; + + if (g_str_equal (elt->element_name, "object")) + object = elt; + + if (g_str_equal (elt->element_name, "packing")) + { + for (ll = elt->children; ll; ll = ll->next) + { + Element *elt2 = ll->data; + + if (g_str_equal (elt2->element_name, "property") && + has_attribute (elt2, "name", "submenu")) + { + submenu = elt2; + elt->children = g_list_remove (elt->children, submenu); + if (elt->children == NULL) + { + element->children = g_list_remove (element->children, elt); + free_element (elt); + } + break; + } + } + } + + if (submenu) + break; + } + + if (submenu) + { + Element *elt; + + elt = g_new0 (Element, 1); + elt->parent = element; + elt->element_name = g_strdup ("property"); + elt->attribute_names = g_new0 (char *, 2); + elt->attribute_names[0] = g_strdup ("name"); + elt->attribute_values = g_new0 (char *, 2); + elt->attribute_values[0] = g_strdup ("name"); + elt->data = g_strdup (submenu->data); + + object->children = g_list_prepend (object->children, elt); + + free_element (submenu); + } +} +static void +rewrite_popover_menu (Element *element, + MyParserData *data) +{ + GList *l; + + for (l = element->children; l; l = l->next) + { + Element *elt = l->data; + if (g_str_equal (elt->element_name, "child")) + rewrite_popover_menu_child (elt, data); + } +} + static gboolean simplify_element (Element *element, MyParserData *data) @@ -892,6 +1005,10 @@ simplify_element (Element *element, g_str_equal (get_class_name (element), "GtkHeaderBar"))) rewrite_pack_type (element, data); + if (g_str_equal (element->element_name, "object") && + g_str_equal (get_class_name (element), "GtkPopoverMenu")) + rewrite_popover_menu (element, data); + if (g_str_equal (element->element_name, "property") && property_has_been_removed (element, data)) return TRUE;