Compare commits

..

1 Commits

Author SHA1 Message Date
Emmanuele Bassi
041495c710 Add a counterpart to gtk_widget_init_template()
Children added through a template still need to be unparented when the
widget is disposed. Instead of doing so manually, we should have a dual
of the gtk_widget_init_template() functions that clears all the bound
objects, and calls `gtk_widget_unparent()` on the direct children of the
widget.
2021-11-27 18:40:11 +00:00
8 changed files with 81 additions and 88 deletions

5
NEWS
View File

@@ -1,8 +1,3 @@
Overview of Changes
===================
* Rename git `master` branch to `main`
Overview of Changes in 4.5.0
============================

View File

@@ -981,7 +981,6 @@ change_tweak (GSimpleAction *action,
typedef struct {
guint32 tag;
float default_value;
GtkAdjustment *adjustment;
GtkWidget *label;
GtkWidget *scale;
@@ -1510,15 +1509,12 @@ add_font_variations (GtkFontChooserWidget *fontchooser,
char tag[5];
double value;
value = gtk_adjustment_get_value (axis->adjustment);
if (value == axis->default_value)
continue;
tag[0] = (axis->tag >> 24) & 0xff;
tag[1] = (axis->tag >> 16) & 0xff;
tag[2] = (axis->tag >> 8) & 0xff;
tag[3] = (axis->tag >> 0) & 0xff;
tag[4] = '\0';
value = gtk_adjustment_get_value (axis->adjustment);
g_string_append_printf (s, "%s%s=%s", sep, tag, g_ascii_dtostr (buf, sizeof(buf), value));
sep = ",";
}
@@ -1537,9 +1533,12 @@ adjustment_changed (GtkAdjustment *adjustment,
s = g_string_new ("");
add_font_variations (fontchooser, s);
font_desc = pango_font_description_new ();
pango_font_description_set_variations (font_desc, s->str);
gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
if (s->len > 0)
{
font_desc = pango_font_description_new ();
pango_font_description_set_variations (font_desc, s->str);
gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
}
g_string_free (s, TRUE);
@@ -1591,7 +1590,6 @@ add_axis (GtkFontChooserWidget *fontchooser,
axis = g_new (Axis, 1);
axis->tag = ax->tag;
axis->default_value = ax->default_value;
axis->fontchooser = GTK_WIDGET (fontchooser);
hb_ot_name_get_utf8 (hb_face, ax->name_id, HB_LANGUAGE_INVALID, &buffer_len, buffer);
@@ -2324,13 +2322,13 @@ gtk_font_chooser_widget_merge_font_desc (GtkFontChooserWidget *fontchooser
mask = pango_font_description_get_set_fields (font_desc);
/* sucky test, because we can't restrict the comparison to
/* sucky test, because we can't restrict the comparison to
* only the parts that actually do get merged */
if (pango_font_description_equal (font_desc, fontchooser->font_desc))
return;
pango_font_description_merge (fontchooser->font_desc, font_desc, TRUE);
if (mask & PANGO_FONT_MASK_SIZE)
{
double font_size = (double) pango_font_description_get_size (fontchooser->font_desc) / PANGO_SCALE;
@@ -2354,12 +2352,6 @@ gtk_font_chooser_widget_merge_font_desc (GtkFontChooserWidget *fontchooser
g_simple_action_set_enabled (G_SIMPLE_ACTION (fontchooser->tweak_action), has_tweak);
}
if (mask & PANGO_FONT_MASK_VARIATIONS)
{
if (pango_font_description_get_variations (fontchooser->font_desc)[0] == '\0')
pango_font_description_unset_fields (fontchooser->font_desc, PANGO_FONT_MASK_VARIANT);
}
gtk_font_chooser_widget_update_preview_attributes (fontchooser);
g_object_notify (G_OBJECT (fontchooser), "font");

View File

@@ -787,20 +787,28 @@ gtk_paned_buildable_add_child (GtkBuildable *buildable,
if (g_strcmp0 (type, "start") == 0)
{
gtk_paned_set_start_child (self, GTK_WIDGET (child));
gtk_paned_set_resize_start_child (self, FALSE);
gtk_paned_set_shrink_start_child (self, TRUE);
}
else if (g_strcmp0 (type, "end") == 0)
{
gtk_paned_set_end_child (self, GTK_WIDGET (child));
gtk_paned_set_resize_end_child (self, TRUE);
gtk_paned_set_shrink_end_child (self, TRUE);
}
else if (type == NULL && GTK_IS_WIDGET (child))
{
if (self->start_child == NULL)
{
gtk_paned_set_start_child (self, GTK_WIDGET (child));
gtk_paned_set_resize_start_child (self, FALSE);
gtk_paned_set_shrink_start_child (self, TRUE);
}
else if (self->end_child == NULL)
{
gtk_paned_set_end_child (self, GTK_WIDGET (child));
gtk_paned_set_resize_end_child (self, TRUE);
gtk_paned_set_shrink_end_child (self, TRUE);
}
else
g_warning ("GtkPaned only accepts two widgets as children");

View File

@@ -10938,6 +10938,67 @@ get_auto_child_hash (GtkWidget *widget,
return auto_child_hash;
}
/**
* gtk_widget_clear_template:
* @widget: the widget whose template has to be cleared
*
* Clears and finalizes child widgets defined in templates.
*
* This function must be called in the `dispose()` implementation
* of the widget class which assigned itself a template using
* [method@Gtk.WidgetClass.set_template], and called
* [method@Gtk.Widget.init_template] in its instance initialization
* function.
*
* Since: 4.6
*/
void
gtk_widget_clear_template (GtkWidget *widget)
{
g_return_if_fail (GTK_IS_WIDGET (widget));
GtkWidgetTemplate *template = GTK_WIDGET_GET_CLASS (widget)->priv->template;
g_return_if_fail (template != NULL);
GType class_type = G_OBJECT_TYPE (widget);
/* Build the automatic child data */
for (GSList *l = template->children; l; l = l->next)
{
AutomaticChildClass *child_class = l->data;
GHashTable *auto_child_hash = get_auto_child_hash (widget, class_type, FALSE);
if (auto_child_hash == NULL)
continue;
GObject *child = g_hash_table_lookup (auto_child_hash, child_class->name);
if (G_UNLIKELY (child == NULL))
{
g_critical ("Unable to retrieve child object '%s' from class "
"template for type '%s' while clearing a '%s'",
child_class->name, g_type_name (class_type), G_OBJECT_TYPE_NAME (widget));
break;
}
if (child_class->offset != 0)
{
gpointer field_p;
/* Nullify the field in the instance/private data */
field_p = G_STRUCT_MEMBER_P (widget, child_class->offset);
(* (gpointer *) field_p) = NULL;
}
/* Unparent direct children of the widget; objects inside the
* auto_child_hash get an additional reference upon insertion
*/
if (GTK_IS_WIDGET (child) && _gtk_widget_get_parent (GTK_WIDGET (child)) == widget)
gtk_widget_unparent (GTK_WIDGET (child));
g_hash_table_remove (auto_child_hash, child_class->name);
}
}
/**
* gtk_widget_init_template:
* @widget: a `GtkWidget`

View File

@@ -851,6 +851,8 @@ void gtk_widget_class_bind_template_child_full (GtkWidgetClass *
const char *name,
gboolean internal_child,
gssize struct_offset);
GDK_AVAILABLE_IN_4_6
void gtk_widget_clear_template (GtkWidget *widget);
GDK_AVAILABLE_IN_ALL
void gtk_widget_insert_action_group (GtkWidget *widget,

View File

@@ -4179,61 +4179,6 @@ update_realized_window_properties (GtkWindow *window)
}
}
/* NB: When orientation is VERTICAL, width/height are flipped.
* The code uses the terms nonetheless to make it more intuitive
* to understand.
*/
static void
gtk_window_compute_min_size (GtkWidget *window,
GtkOrientation orientation,
double ideal_ratio,
int *min_width,
int *min_height)
{
int start, end, mid, other;
double ratio;
/* start = min width, end = min width for min height (ie max width) */
gtk_widget_measure (window, orientation, -1, &start, NULL, NULL, NULL);
gtk_widget_measure (window, OPPOSITE_ORIENTATION (orientation), start, &other, NULL, NULL, NULL);
if ((double) start / other >= ideal_ratio)
{
*min_width = start;
*min_height = other;
return;
}
gtk_widget_measure (window, OPPOSITE_ORIENTATION (orientation), -1, &other, NULL, NULL, NULL);
gtk_widget_measure (window, orientation, other, &end, NULL, NULL, NULL);
if ((double) end / other <= ideal_ratio)
{
*min_width = end;
*min_height = other;
return;
}
while (start < end)
{
mid = (start + end) / 2;
gtk_widget_measure (window, OPPOSITE_ORIENTATION (orientation), mid, &other, NULL, NULL, NULL);
ratio = (double) mid / other;
if(ratio == ideal_ratio)
{
*min_width = mid;
*min_height = other;
return;
}
else if (ratio < ideal_ratio)
start = mid + 1;
else
end = mid - 1;
}
gtk_widget_measure (window, orientation, other, &start, NULL, NULL, NULL);
*min_width = start;
*min_height = other;
}
static void
gtk_window_compute_default_size (GtkWindow *window,
int cur_width,
@@ -4246,9 +4191,8 @@ gtk_window_compute_default_size (GtkWindow *window,
int *height)
{
GtkWidget *widget = GTK_WIDGET (window);
GtkSizeRequestMode request_mode = gtk_widget_get_request_mode (widget);
if (request_mode == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
{
int minimum, natural;
@@ -4268,8 +4212,6 @@ gtk_window_compute_default_size (GtkWindow *window,
if (cur_width <= 0)
cur_width = natural;
*width = MAX (minimum, MIN (max_width, cur_width));
gtk_window_compute_min_size (widget, GTK_ORIENTATION_VERTICAL, (double) *height / *width, min_height, min_width);
}
else /* GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH or CONSTANT_SIZE */
{
@@ -4292,9 +4234,6 @@ gtk_window_compute_default_size (GtkWindow *window,
cur_height = natural;
*height = MAX (minimum, MIN (max_height, cur_height));
if (request_mode != GTK_SIZE_REQUEST_CONSTANT_SIZE)
gtk_window_compute_min_size (widget, GTK_ORIENTATION_HORIZONTAL, (double) *width / *height, min_width, min_height);
}
}

View File

@@ -361,8 +361,6 @@ testsuite/reftests/window-height-for-width.ref.ui
testsuite/reftests/window-height-for-width.ui
testsuite/reftests/window-show-contents-on-map.ref.ui
testsuite/reftests/window-show-contents-on-map.ui
testsuite/reftests/wrapping-in-boxes-in-boxes.ref.ui
testsuite/reftests/wrapping-in-boxes-in-boxes.ui
testsuite/tools/simplify-data-3to4/assistant.ui
testsuite/tools/simplify-data-3to4/box.ui
testsuite/tools/simplify-data-3to4/grid.ui

View File

@@ -319,8 +319,6 @@ testsuite/reftests/window-height-for-width.ref.ui
testsuite/reftests/window-height-for-width.ui
testsuite/reftests/window-show-contents-on-map.ref.ui
testsuite/reftests/window-show-contents-on-map.ui
testsuite/reftests/wrapping-in-boxes-in-boxes.ref.ui
testsuite/reftests/wrapping-in-boxes-in-boxes.ui
testsuite/tools/simplify-data-3to4/assistant.ui
testsuite/tools/simplify-data-3to4/box.ui
testsuite/tools/simplify-data-3to4/grid.ui