summaryrefslogtreecommitdiff
path: root/gtk/tools/gtk-builder-tool-simplify.c
diff options
context:
space:
mode:
Diffstat (limited to 'gtk/tools/gtk-builder-tool-simplify.c')
-rw-r--r--gtk/tools/gtk-builder-tool-simplify.c287
1 files changed, 267 insertions, 20 deletions
diff --git a/gtk/tools/gtk-builder-tool-simplify.c b/gtk/tools/gtk-builder-tool-simplify.c
index 76dc3ea840..e447ac447d 100644
--- a/gtk/tools/gtk-builder-tool-simplify.c
+++ b/gtk/tools/gtk-builder-tool-simplify.c
@@ -184,6 +184,42 @@ needs_explicit_setting (GParamSpec *pspec,
}
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 },
+ { "GtkToolbar", "expand", 1 },
+ { "GtkToolbar", "homogeneous", 1 },
+ { "GtkPaned", "resize", 1 },
+ { "GtkPaned", "shrink", 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)
{
/* elements that can contain text */
@@ -323,6 +359,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 +479,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 +697,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 +826,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 +879,195 @@ rewrite_pack_type (Element *element,
}
}
+static void
+rewrite_child_prop_to_prop_child (Element *element,
+ MyParserData *data,
+ const char *child_prop,
+ const char *prop)
+{
+ Element *object = NULL;
+ Element *replaced = 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", child_prop))
+ {
+ replaced = elt2;
+ elt->children = g_list_remove (elt->children, replaced);
+ if (elt->children == NULL)
+ {
+ element->children = g_list_remove (element->children, elt);
+ free_element (elt);
+ }
+ break;
+ }
+ }
+ }
+
+ if (replaced)
+ break;
+ }
+
+ if (replaced)
+ {
+ 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 (prop);
+ elt->data = g_strdup (replaced->data);
+
+ object->children = g_list_prepend (object->children, elt);
+
+ free_element (replaced);
+ }
+}
+
+static void
+rewrite_child_prop_to_prop (Element *element,
+ MyParserData *data,
+ const char *child_prop,
+ const char *prop)
+{
+ GList *l;
+
+ for (l = element->children; l; l = l->next)
+ {
+ Element *elt = l->data;
+ if (g_str_equal (elt->element_name, "child"))
+ rewrite_child_prop_to_prop_child (elt, data, child_prop, prop);
+ }
+}
+
+static void
+rewrite_paned_child (Element *element,
+ MyParserData *data,
+ Element *child,
+ const char *suffix)
+{
+ Element *resize = NULL;
+ Element *shrink = NULL;
+ GList *l, *ll;
+
+ for (l = child->children; l; l = l->next)
+ {
+ Element *elt = l->data;
+
+ 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", "resize"))
+ resize = elt2;
+ if (g_str_equal (elt2->element_name, "property") &&
+ has_attribute (elt2, "name", "shrink"))
+ shrink = elt2;
+ }
+ if (resize)
+ elt->children = g_list_remove (elt->children, resize);
+ if (shrink)
+ elt->children = g_list_remove (elt->children, shrink);
+ if (elt->children == NULL)
+ {
+ child->children = g_list_remove (child->children, elt);
+ free_element (elt);
+ }
+ }
+
+ if (resize || shrink)
+ break;
+ }
+
+ if (resize)
+ {
+ 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_strconcat ("resize-", suffix, NULL);
+ elt->data = g_strdup (resize->data);
+
+ element->children = g_list_prepend (element->children, elt);
+
+ free_element (resize);
+ }
+
+ if (shrink)
+ {
+ 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_strconcat ("shrink-", suffix, NULL);
+ elt->data = g_strdup (shrink->data);
+
+ element->children = g_list_prepend (element->children, elt);
+
+ free_element (shrink);
+ }
+}
+
+static void
+rewrite_paned (Element *element,
+ MyParserData *data)
+{
+ Element *child1 = NULL;
+ Element *child2 = NULL;
+ GList *l;
+
+ for (l = element->children; l; l = l->next)
+ {
+ Element *elt = l->data;
+
+ if (g_str_equal (elt->element_name, "child"))
+ {
+ if (child1 == NULL)
+ child1 = elt;
+ else if (child2 == NULL)
+ child2 = elt;
+ else
+ break;
+ }
+ }
+
+ if (child1)
+ rewrite_paned_child (element, data, child1, "child1");
+
+ if (child2)
+ rewrite_paned_child (element, data, child2, "child2");
+}
+
static gboolean
simplify_element (Element *element,
MyParserData *data)
@@ -892,6 +1123,22 @@ 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_child_prop_to_prop (element, data, "submenu", "name");
+
+ if (g_str_equal (element->element_name, "object") &&
+ g_str_equal (get_class_name (element), "GtkToolbar"))
+ rewrite_child_prop_to_prop (element, data, "expand", "expand-item");
+
+ if (g_str_equal (element->element_name, "object") &&
+ g_str_equal (get_class_name (element), "GtkToolbar"))
+ rewrite_child_prop_to_prop (element, data, "homogeneous", "homogeneous");
+
+ if (g_str_equal (element->element_name, "object") &&
+ g_str_equal (get_class_name (element), "GtkPaned"))
+ rewrite_paned (element, data);
+
if (g_str_equal (element->element_name, "property") &&
property_has_been_removed (element, data))
return TRUE;