[gtk/builder-tool-fixes] builder-tool: Handle default values changing



commit 09b6dbb7efcdea9f96d582cfd885abe74294e55a
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat May 4 15:59:08 2019 +0000

    builder-tool: Handle default values changing
    
    The default value of GtkWidget::visible changed
    from FALSE to TRUE from GTK 3 to 4. Make --3to4
    deal with this by ensuring the visible property
    is explicitly set, before simplifying.

 gtk/tools/gtk-builder-tool-simplify.c | 82 +++++++++++++++++++++++++++++++++--
 1 file changed, 78 insertions(+), 4 deletions(-)
---
diff --git a/gtk/tools/gtk-builder-tool-simplify.c b/gtk/tools/gtk-builder-tool-simplify.c
index 93c19e0a8c..6bd0810069 100644
--- a/gtk/tools/gtk-builder-tool-simplify.c
+++ b/gtk/tools/gtk-builder-tool-simplify.c
@@ -389,7 +389,8 @@ value_is_default (Element      *element,
           GType type = g_type_from_name (class_name);
           gboolean default_value;
 
-          if (g_type_is_a (type, GTK_TYPE_NATIVE))
+          if (g_type_is_a (type, GTK_TYPE_ROOT) ||
+              g_type_is_a (type, GTK_TYPE_POPOVER))
             default_value = FALSE;
           else
             default_value = TRUE;
@@ -1312,11 +1313,11 @@ simplify_element (Element      *element,
       if (g_str_equal (element->element_name, "object") &&
           g_str_equal (get_class_name (element), "GtkAssistant"))
         rewrite_assistant (element, data);
-          
+
       if (g_str_equal (element->element_name, "object") &&
           g_str_equal (get_class_name (element), "GtkNotebook"))
         rewrite_notebook (element, data);
-          
+
       if (g_str_equal (element->element_name, "object") &&
           (g_str_equal (get_class_name (element), "GtkActionBar") ||
            g_str_equal (get_class_name (element), "GtkHeaderBar")))
@@ -1364,6 +1365,78 @@ simplify_tree (MyParserData *data)
   simplify_element (data->root, data);
 }
 
+/* For properties which have changed their default
+ * value between 3 and 4, we make sure that their
+ * old default value is present in the tree before
+ * simplifying it.
+ *
+ * So far, this is just GtkWidget::visible,
+ * changing its default from 0 to 1.
+ */
+static void
+add_old_default_properties (Element      *element,
+                            MyParserData *data)
+{
+  const char *class_name;
+  GType type;
+
+  if (!g_str_equal (element->element_name, "object"))
+    return;
+
+  class_name = get_class_name (element);
+  type = g_type_from_name (class_name);
+  if (g_type_is_a (type, GTK_TYPE_WIDGET))
+    {
+      GList *l;
+      gboolean has_visible = FALSE;
+
+      for (l = element->children; l; l = l->next)
+        {
+          Element *prop = l->data;
+          const char *name = get_attribute_value (prop, "name");
+
+          if (g_str_equal (prop->element_name, "property") &&
+              g_str_equal (name, "visible"))
+            has_visible = TRUE;
+        }
+
+      if (!has_visible)
+        {
+          Element *new_prop = g_new0 (Element, 1);
+          new_prop->parent = element;
+          new_prop->element_name = g_strdup ("property");
+          new_prop->attribute_names = g_new0 (char *, 2);
+          new_prop->attribute_names[0] = g_strdup ("name");
+          new_prop->attribute_values = g_new0 (char *, 2);
+          new_prop->attribute_values[0] = g_strdup ("visible");
+          new_prop->data = g_strdup ("0");
+          element->children = g_list_prepend (element->children, new_prop);
+        }
+    }
+}
+
+static void
+enhance_element (Element      *element,
+                 MyParserData *data)
+{
+  GList *l;
+
+  add_old_default_properties (element, data);
+
+  for (l = element->children; l; l = l->next)
+    {
+      Element *child = l->data;
+      enhance_element (child, data);
+    }
+}
+
+static void
+enhance_tree (MyParserData *data)
+{
+  if (data->convert3to4)
+    enhance_element (data->root, data);
+}
+
 static void
 dump_element (Element *element,
               FILE    *output,
@@ -1405,7 +1478,7 @@ dump_element (Element *element,
       g_fprintf (output, "</%s>\n", element->element_name);
     }
   else
-    g_fprintf (output, "/>\n"); 
+    g_fprintf (output, "/>\n");
 }
 
 static void
@@ -1465,6 +1538,7 @@ simplify_file (const char *filename,
 
   data.builder = gtk_builder_new ();
 
+  enhance_tree (&data);
   simplify_tree (&data);
 
   dump_tree (&data);


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