diff --git a/gtk/tools/gtk-builder-tool-simplify.c b/gtk/tools/gtk-builder-tool-simplify.c index 6bd0810069..1fc1987f51 100644 --- a/gtk/tools/gtk-builder-tool-simplify.c +++ b/gtk/tools/gtk-builder-tool-simplify.c @@ -141,8 +141,24 @@ typedef enum { PROP_KIND_OBJECT, PROP_KIND_PACKING, PROP_KIND_CELL_PACKING, + PROP_KIND_LAYOUT } PropKind; +static PropKind +get_prop_kind (Element *element) +{ + g_assert (g_str_equal (element->element_name, "property")); + + if (g_str_equal (element->parent->element_name, "packing")) + return PROP_KIND_PACKING; + else if (g_str_equal (element->parent->element_name, "layout")) + return PROP_KIND_LAYOUT; + else if (g_str_equal (element->parent->element_name, "cell-packing")) + return PROP_KIND_CELL_PACKING; + else + return PROP_KIND_OBJECT; +} + /* A number of properties unfortunately can't be omitted even * if they are nominally set to their default value. In many * cases, this is due to subclasses not overriding the default @@ -165,6 +181,8 @@ needs_explicit_setting (GParamSpec *pspec, { "GtkRadioButton", "draw-indicator", PROP_KIND_OBJECT }, { "GtkWidget", "hexpand", PROP_KIND_OBJECT }, { "GtkWidget", "vexpand", PROP_KIND_OBJECT }, + { "GtkGrid", "top-attach", PROP_KIND_LAYOUT }, + { "GtkGrid", "left-attach", PROP_KIND_LAYOUT }, }; gboolean found; gint k; @@ -179,6 +197,7 @@ needs_explicit_setting (GParamSpec *pspec, strcmp (pspec->name, props[k].property) == 0 && kind == props[k].kind) { +g_print ("explicit: %s::%s\n", class_name, pspec->name); found = TRUE; break; } @@ -210,6 +229,10 @@ keep_for_rewrite (const char *class_name, { "GtkGrid", "top-attach", PROP_KIND_PACKING }, { "GtkGrid", "width", PROP_KIND_PACKING }, { "GtkGrid", "height", PROP_KIND_PACKING }, + { "GtkStack", "name", PROP_KIND_PACKING }, + { "GtkStack", "title", PROP_KIND_PACKING }, + { "GtkStack", "icon-name", PROP_KIND_PACKING }, + { "GtkStack", "needs-attention", PROP_KIND_PACKING }, }; gboolean found; gint k; @@ -225,6 +248,7 @@ keep_for_rewrite (const char *class_name, strcmp (canonical_name, props[k].property) == 0 && kind == props[k].kind) { +g_print ("keep for rewrite: %s::%s\n", class_name, canonical_name); found = TRUE; break; } @@ -309,6 +333,18 @@ canonicalize_key (gchar *key) } } +static struct { + const char *class; + const char *layout_manager; +} layout_managers[] = { + { "GtkBox", "GtkBoxLayout" }, + { "GtkGrid", "GtkGridLayout" }, + { "GtkFixed", "GtkFixedLayout" }, + { "GtkFileChooserButton", "GtkBinLayout" }, + { "GtkFileChooserWidget", "GtkBinLayout" }, + { "GtkOverlay", "GtkOverlayLayout" } +}; + static GParamSpec * get_property_pspec (MyParserData *data, const gchar *class_name, @@ -336,9 +372,11 @@ get_property_pspec (MyParserData *data, case PROP_KIND_OBJECT: pspec = g_object_class_find_property (class, canonical_name); break; + case PROP_KIND_PACKING: pspec = NULL; break; + case PROP_KIND_CELL_PACKING: { GObjectClass *cell_class; @@ -349,6 +387,40 @@ get_property_pspec (MyParserData *data, g_type_class_unref (cell_class); } break; + + case PROP_KIND_LAYOUT: + { + int i; + const char *layout_manager = NULL; + + pspec = NULL; + + for (i = 0; i < G_N_ELEMENTS (layout_managers); i++) + { + if (g_str_equal (layout_managers[i].class, class_name)) + { + layout_manager = layout_managers[i].layout_manager; + break; + } + } + + if (layout_manager) + { + GtkLayoutManagerClass *layout_manager_class; + + layout_manager_class = GTK_LAYOUT_MANAGER_CLASS (g_type_class_ref (g_type_from_name (layout_manager))); + if (layout_manager_class->layout_child_type != G_TYPE_INVALID) + { + GObjectClass *layout_child_class; + layout_child_class = g_type_class_ref (layout_manager_class->layout_child_type); + pspec = g_object_class_find_property (layout_child_class, canonical_name); + g_type_class_unref (layout_child_class); + } + g_type_class_unref (layout_manager_class); + } + } + break; + default: g_assert_not_reached (); } @@ -492,11 +564,7 @@ property_is_boolean (Element *element, int i; PropKind kind; - if (g_str_equal (element->parent->element_name, "packing")) - kind = PROP_KIND_PACKING; - else - kind = PROP_KIND_OBJECT; - + kind = get_prop_kind (element); class_name = get_class_name (element); property_name = ""; @@ -526,13 +594,7 @@ property_can_be_omitted (Element *element, GParamSpec *pspec; PropKind kind; - if (g_str_equal (element->parent->element_name, "packing")) - kind = PROP_KIND_PACKING; - else if (g_str_equal (element->parent->element_name, "cell-packing")) - kind = PROP_KIND_CELL_PACKING; - else - kind = PROP_KIND_OBJECT; - + kind = get_prop_kind (element); class_name = get_class_name (element); property_name = ""; value_string = element->data; @@ -550,7 +612,7 @@ property_can_be_omitted (Element *element, property_name = (const gchar *)element->attribute_values[i]; } - if (data->convert3to4 && + if (data->convert3to4 && keep_for_rewrite (class_name, property_name, kind)) return FALSE; /* keep, will be rewritten */ @@ -567,9 +629,10 @@ property_can_be_omitted (Element *element, const char *kind_str[] = { "", "Packing ", - "Cell " + "Cell ", + "Layout " }; - + g_printerr (_("%s: %sproperty %s::%s not found\n"), data->input_filename, kind_str[kind], class_name, property_name); return FALSE; @@ -609,10 +672,7 @@ property_has_been_removed (Element *element, gint i, k; PropKind kind; - if (g_str_equal (element->parent->element_name, "packing")) - kind = PROP_KIND_PACKING; - else - kind = PROP_KIND_OBJECT; + kind = get_prop_kind (element); class_name = get_class_name (element); property_name = ""; @@ -1268,6 +1328,7 @@ rewrite_grid_layout (Element *element, } } +/* returns TRUE to remove the element from the parent */ static gboolean simplify_element (Element *element, MyParserData *data) @@ -1304,58 +1365,6 @@ simplify_element (Element *element, property_can_be_omitted (element, data)) return TRUE; - if (data->convert3to4) - { - if (g_str_equal (element->element_name, "object") && - g_str_equal (get_class_name (element), "GtkStack")) - rewrite_stack (element, data); - - 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"))) - 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, "object") && - g_str_equal (get_class_name (element), "GtkOverlay")) - rewrite_layout_props (element, data); - - if (g_str_equal (element->element_name, "object") && - g_str_equal (get_class_name (element), "GtkGrid")) - rewrite_grid_layout (element, data); - - if (g_str_equal (element->element_name, "object") && - g_str_equal (get_class_name (element), "GtkFixed")) - rewrite_layout_props (element, data); - - if (g_str_equal (element->element_name, "property") && - property_has_been_removed (element, data)) - return TRUE; - } - return FALSE; } @@ -1365,6 +1374,83 @@ simplify_tree (MyParserData *data) simplify_element (data->root, data); } +static gboolean +rewrite_element (Element *element, + MyParserData *data) +{ + GList *l; + + l = element->children; + while (l) + { + GList *next = l->next; + Element *child = l->data; + if (rewrite_element (child, data)) + { + element->children = g_list_remove (element->children, child); + free_element (child); + } + l = next; + } + + if (g_str_equal (element->element_name, "object") && + g_str_equal (get_class_name (element), "GtkStack")) + rewrite_stack (element, data); + + 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"))) + 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, "object") && + g_str_equal (get_class_name (element), "GtkOverlay")) + rewrite_layout_props (element, data); + + if (g_str_equal (element->element_name, "object") && + g_str_equal (get_class_name (element), "GtkGrid")) + rewrite_grid_layout (element, data); + + if (g_str_equal (element->element_name, "object") && + g_str_equal (get_class_name (element), "GtkFixed")) + rewrite_layout_props (element, data); + + if (g_str_equal (element->element_name, "property") && + property_has_been_removed (element, data)) + return TRUE; + + return FALSE; +} + +static void +rewrite_tree (MyParserData *data) +{ + rewrite_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 @@ -1433,8 +1519,7 @@ enhance_element (Element *element, static void enhance_tree (MyParserData *data) { - if (data->convert3to4) - enhance_element (data->root, data); + enhance_element (data->root, data); } static void @@ -1538,7 +1623,11 @@ simplify_file (const char *filename, data.builder = gtk_builder_new (); - enhance_tree (&data); + if (data.convert3to4) + { + enhance_tree (&data); + rewrite_tree (&data); + } simplify_tree (&data); dump_tree (&data); diff --git a/testsuite/tools/meson.build b/testsuite/tools/meson.build index 74a79901a9..4e6a605056 100644 --- a/testsuite/tools/meson.build +++ b/testsuite/tools/meson.build @@ -5,7 +5,7 @@ bash = find_program('bash', required : false) if bash.found() test_env = environment() - foreach t : ['simplify', 'settings'] + foreach t : ['simplify', 'simplify-3to4', 'settings'] if get_option('install-tests') configure_file(output: t, input: '@0@.in'.format(t), @@ -44,4 +44,5 @@ if get_option('install-tests') endforeach install_subdir('simplify-data', install_dir: testexecdir) + install_subdir('simplify-data-3to4', install_dir: testexecdir) endif diff --git a/testsuite/tools/simplify-3to4.in b/testsuite/tools/simplify-3to4.in new file mode 100755 index 0000000000..3c0a515762 --- /dev/null +++ b/testsuite/tools/simplify-3to4.in @@ -0,0 +1,31 @@ +#! /bin/bash + +GTK_BUILDER_TOOL=${GTK_BUILDER_TOOL:-gtk-builder-tool} +TEST_DATA_DIR=${TEST_DATA_DIR:-./simplify-data-3to4} +TEST_RESULT_DIR=${TEST_RESULT_DIR:-/tmp} + +shopt -s nullglob +TESTS=( "$TEST_DATA_DIR"/*.ui ) + +echo "1..${#TESTS[*]}" + +I=1 +for t in ${TESTS[*]}; do + name=$(basename $t .ui) + expected="$TEST_DATA_DIR/$name.expected" + result="$TEST_RESULT_DIR/$name.out" + diff="$TEST_RESULT_DIR/$name.diff" + ref="$TEST_RESULT_DIR/$name.ref" + + $GTK_BUILDER_TOOL simplify --3to4 $t 2>/dev/null >$result + + if diff -u "$expected" "$result" > "$diff"; then + echo "ok $I $name" + rm "$diff" + else + echo "not ok $I $name" + cp "$expected" "$ref" + fi + + I=$((I+1)) +done diff --git a/testsuite/tools/simplify-data-3to4/assistant.expected b/testsuite/tools/simplify-data-3to4/assistant.expected new file mode 100644 index 0000000000..5b81816063 --- /dev/null +++ b/testsuite/tools/simplify-data-3to4/assistant.expected @@ -0,0 +1,28 @@ + + + + popup + + + Page 1 + + + Button 1 + 1 + + + + + + + Page 2 + + + Button 2 + 1 + + + + + + diff --git a/testsuite/tools/simplify-data-3to4/assistant.ui b/testsuite/tools/simplify-data-3to4/assistant.ui new file mode 100644 index 0000000000..aed2044489 --- /dev/null +++ b/testsuite/tools/simplify-data-3to4/assistant.ui @@ -0,0 +1,28 @@ + + + + + False + popup + + + Button 1 + True + True + + + Page 1 + + + + + Button 2 + True + True + + + Page 2 + + + + diff --git a/testsuite/tools/simplify-data-3to4/grid.expected b/testsuite/tools/simplify-data-3to4/grid.expected new file mode 100644 index 0000000000..cda31e5900 --- /dev/null +++ b/testsuite/tools/simplify-data-3to4/grid.expected @@ -0,0 +1,110 @@ + + + + popup + + + + + Hello World! + 1 + 1 + 1 + + + + + Hello World! + 1 + 1 + + 1 + + + + + + Hello World! + 1 + 1 + + 2 + + + + + + Hello World! + 1 + button3 + 1 + 1 + + 3 + + + + + + 1 + 1 + 1 + + 4 + + + + + + 1 + 1 + + + Test switch + + + + 5 + + + + + + 1 + 1 + Text Button + + 1 + + + + + + 1 + 1 + center + center + + 1 + 1 + + + + + + 1 + 1 + center + center + + 2 + 1 + 2 + 2 + + + + + + + diff --git a/testsuite/tools/simplify-data-3to4/grid.ui b/testsuite/tools/simplify-data-3to4/grid.ui new file mode 100644 index 0000000000..be79df3ce4 --- /dev/null +++ b/testsuite/tools/simplify-data-3to4/grid.ui @@ -0,0 +1,138 @@ + + + + + False + popup + + + True + + + Hello World! + True + True + True + True + + + 0 + 0 + + + + + Hello World! + True + False + True + True + + + 1 + 0 + + + + + Hello World! + True + False + True + True + + + 2 + 0 + + + + + Hello World! + True + True + button3 + True + True + + + 3 + 0 + + + + + True + True + True + True + + + 4 + 0 + + + + + True + False + True + True + + + Test switch + + + + + 5 + 0 + + + + + + True + True + True + Text Button + + + 0 + 1 + + + + + True + True + True + center + center + + + 1 + 1 + 1 + 1 + + + + + True + True + True + center + center + + + 2 + 1 + 2 + 2 + + + + + + diff --git a/testsuite/tools/simplify-data-3to4/notebook.expected b/testsuite/tools/simplify-data-3to4/notebook.expected new file mode 100644 index 0000000000..60ac5bcc4f --- /dev/null +++ b/testsuite/tools/simplify-data-3to4/notebook.expected @@ -0,0 +1,38 @@ + + + + popup + + + + + + + Yes + + + + + Tab 1 + + + + + + + + + No + + + + + Tab 2 + + + + + + + + diff --git a/testsuite/tools/simplify-data-3to4/notebook.ui b/testsuite/tools/simplify-data-3to4/notebook.ui new file mode 100644 index 0000000000..46cc7378a2 --- /dev/null +++ b/testsuite/tools/simplify-data-3to4/notebook.ui @@ -0,0 +1,37 @@ + + + + + False + popup + + + True + + + Yes + True + + + + + Tab 1 + True + + + + + No + True + + + + + Tab 2 + True + + + + + + diff --git a/testsuite/tools/simplify-data-3to4/stack.expected b/testsuite/tools/simplify-data-3to4/stack.expected new file mode 100644 index 0000000000..f6321c9563 --- /dev/null +++ b/testsuite/tools/simplify-data-3to4/stack.expected @@ -0,0 +1,43 @@ + + + + popup + + + + + stack1 + + + + + + + page1 + Page 1 + 1 + + + Yes + + + + + + + page2 + Page 2 + page2-icon + + + No + + + + + + + + + + diff --git a/testsuite/tools/simplify-data-3to4/stack.ui b/testsuite/tools/simplify-data-3to4/stack.ui new file mode 100644 index 0000000000..feb092eaad --- /dev/null +++ b/testsuite/tools/simplify-data-3to4/stack.ui @@ -0,0 +1,46 @@ + + + + + False + popup + + + True + + + True + stack1 + + + + + True + + + Yes + True + + + page1 + Page 1 + 1 + + + + + No + True + + + page2 + Page 2 + page2-icon + + + + + + + + diff --git a/testsuite/tools/simplify-data-3to4/test1.expected b/testsuite/tools/simplify-data-3to4/test1.expected new file mode 100644 index 0000000000..0ddbcae8f5 --- /dev/null +++ b/testsuite/tools/simplify-data-3to4/test1.expected @@ -0,0 +1,10 @@ + + + + + + 0 + + + + diff --git a/testsuite/tools/simplify.in b/testsuite/tools/simplify.in index 642b50f0e6..814f803091 100755 --- a/testsuite/tools/simplify.in +++ b/testsuite/tools/simplify.in @@ -7,7 +7,7 @@ TEST_RESULT_DIR=${TEST_RESULT_DIR:-/tmp} shopt -s nullglob TESTS=( "$TEST_DATA_DIR"/*.ui ) -echo "1..${#TESTS}" +echo "1..${#TESTS[*]}" I=1 for t in ${TESTS[*]}; do