From 082ba6d7c847de3d046d4379d58ce016617eaada Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 7 May 2023 21:26:09 -0400 Subject: [PATCH 1/2] Introduce GTK_ALIGN_BASELINE_FILL/CENTER It turns out that the old behavior of GTK_ALIGN_BASELINE was actually used in libadwaita, so bring it back, and introduce a new GtkAlign value for the new behavior. --- demos/gtk-demo/bluroverlay.c | 9 ++++--- gtk/gtkboxlayout.c | 3 ++- gtk/gtkenums.h | 6 ++++- gtk/gtkflowbox.c | 3 ++- gtk/gtkgridlayout.c | 4 +++- gtk/gtkoverlay.c | 9 ++++--- gtk/gtkoverlaylayout.c | 3 ++- gtk/gtkpopover.c | 12 ++++++---- gtk/gtkwidget.c | 29 ++++++++++++++--------- tests/testbaseline2.c | 46 ++++++++++++++++++------------------ 10 files changed, 75 insertions(+), 49 deletions(-) diff --git a/demos/gtk-demo/bluroverlay.c b/demos/gtk-demo/bluroverlay.c index a4f1d450fd..c96ed78465 100644 --- a/demos/gtk-demo/bluroverlay.c +++ b/demos/gtk-demo/bluroverlay.c @@ -116,7 +116,8 @@ effective_align (GtkAlign align, return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_START : GTK_ALIGN_END; case GTK_ALIGN_FILL: case GTK_ALIGN_CENTER: - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: return align; } @@ -258,7 +259,8 @@ blur_overlay_get_child_position (BlurOverlay *overlay, case GTK_ALIGN_END: alloc->x += width - alloc->width; break; - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: g_assert_not_reached (); break; @@ -281,7 +283,8 @@ blur_overlay_get_child_position (BlurOverlay *overlay, case GTK_ALIGN_END: alloc->y += height - alloc->height; break; - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: g_assert_not_reached (); break; diff --git a/gtk/gtkboxlayout.c b/gtk/gtkboxlayout.c index 0b5c22a22a..a95a4ee3cb 100644 --- a/gtk/gtkboxlayout.c +++ b/gtk/gtkboxlayout.c @@ -885,7 +885,8 @@ gtk_box_layout_allocate (GtkLayoutManager *layout_manager, sizes[i].natural_size = child_size; if (self->orientation == GTK_ORIENTATION_HORIZONTAL && - gtk_widget_get_valign (child) == GTK_ALIGN_BASELINE) + (gtk_widget_get_valign (child) == GTK_ALIGN_BASELINE_FILL || + gtk_widget_get_valign (child) == GTK_ALIGN_BASELINE_CENTER)) { int child_allocation_width; int child_minimum_height, child_natural_height; diff --git a/gtk/gtkenums.h b/gtk/gtkenums.h index 815e25de55..c683e9d01e 100644 --- a/gtk/gtkenums.h +++ b/gtk/gtkenums.h @@ -42,6 +42,8 @@ G_BEGIN_DECLS * @GTK_ALIGN_CENTER: center natural width of widget inside the allocation * @GTK_ALIGN_BASELINE: align the widget according to the baseline. * See [class@Gtk.Widget]. + * @GTK_ALIGN_BASELINE_FILL: a different name for @GTK_ALIGN_BASELINE. Since 4.12 + * @GTK_ALIGN_BASELINE_CENTER: stretch to fill all space, but align the baseline. Since 4.12 * * Controls how a widget deals with extra space in a single dimension. * @@ -67,7 +69,9 @@ typedef enum GTK_ALIGN_START, GTK_ALIGN_END, GTK_ALIGN_CENTER, - GTK_ALIGN_BASELINE + GTK_ALIGN_BASELINE, + GTK_ALIGN_BASELINE_FILL = GTK_ALIGN_BASELINE, + GTK_ALIGN_BASELINE_CENTER, } GtkAlign; /** diff --git a/gtk/gtkflowbox.c b/gtk/gtkflowbox.c index a0ecc3d056..93c3f8ec46 100644 --- a/gtk/gtkflowbox.c +++ b/gtk/gtkflowbox.c @@ -1530,7 +1530,8 @@ get_offset_pixels (GtkAlign align, case GTK_ALIGN_END: offset = pixels; break; - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: g_assert_not_reached (); break; diff --git a/gtk/gtkgridlayout.c b/gtk/gtkgridlayout.c index 1e2a8b7bcc..fca1d2f215 100644 --- a/gtk/gtkgridlayout.c +++ b/gtk/gtkgridlayout.c @@ -1463,7 +1463,9 @@ allocate_child (GridRequest *request, attach = &grid_child->attach[orientation]; *position = lines->lines[attach->pos - lines->min].position; - if (attach->span == 1 && gtk_widget_get_valign (child) == GTK_ALIGN_BASELINE) + if (attach->span == 1 && + (gtk_widget_get_valign (child) == GTK_ALIGN_BASELINE_CENTER || + gtk_widget_get_valign (child) == GTK_ALIGN_BASELINE_FILL)) *baseline = lines->lines[attach->pos - lines->min].allocated_baseline; else *baseline = -1; diff --git a/gtk/gtkoverlay.c b/gtk/gtkoverlay.c index 43286c0624..6aed2e59f4 100644 --- a/gtk/gtkoverlay.c +++ b/gtk/gtkoverlay.c @@ -114,7 +114,8 @@ effective_align (GtkAlign align, return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_START : GTK_ALIGN_END; case GTK_ALIGN_FILL: case GTK_ALIGN_CENTER: - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: return align; } @@ -154,7 +155,8 @@ gtk_overlay_get_child_position (GtkOverlay *overlay, case GTK_ALIGN_END: alloc->x += width - alloc->width; break; - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: g_assert_not_reached (); break; @@ -177,7 +179,8 @@ gtk_overlay_get_child_position (GtkOverlay *overlay, case GTK_ALIGN_END: alloc->y += height - alloc->height; break; - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: g_assert_not_reached (); break; diff --git a/gtk/gtkoverlaylayout.c b/gtk/gtkoverlaylayout.c index 204babbf30..33f8de7a20 100644 --- a/gtk/gtkoverlaylayout.c +++ b/gtk/gtkoverlaylayout.c @@ -324,7 +324,8 @@ effective_align (GtkAlign align, return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_START : GTK_ALIGN_END; case GTK_ALIGN_FILL: case GTK_ALIGN_CENTER: - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: return align; } diff --git a/gtk/gtkpopover.c b/gtk/gtkpopover.c index f1025fc333..42e2b06384 100644 --- a/gtk/gtkpopover.c +++ b/gtk/gtkpopover.c @@ -505,7 +505,8 @@ create_popup_layout (GtkPopover *popover) case GTK_ALIGN_FILL: case GTK_ALIGN_CENTER: - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: parent_anchor = GDK_GRAVITY_WEST; surface_anchor = GDK_GRAVITY_EAST; @@ -529,7 +530,8 @@ create_popup_layout (GtkPopover *popover) case GTK_ALIGN_FILL: case GTK_ALIGN_CENTER: - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: parent_anchor = GDK_GRAVITY_EAST; surface_anchor = GDK_GRAVITY_WEST; @@ -553,7 +555,8 @@ create_popup_layout (GtkPopover *popover) case GTK_ALIGN_FILL: case GTK_ALIGN_CENTER: - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: parent_anchor = GDK_GRAVITY_NORTH; surface_anchor = GDK_GRAVITY_SOUTH; @@ -577,7 +580,8 @@ create_popup_layout (GtkPopover *popover) case GTK_ALIGN_FILL: case GTK_ALIGN_CENTER: - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: parent_anchor = GDK_GRAVITY_SOUTH; surface_anchor = GDK_GRAVITY_NORTH; diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c index d5542a3b3b..a13ce9711b 100644 --- a/gtk/gtkwidget.c +++ b/gtk/gtkwidget.c @@ -3749,7 +3749,8 @@ effective_align (GtkAlign align, return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_START : GTK_ALIGN_END; case GTK_ALIGN_FILL: case GTK_ALIGN_CENTER: - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_FILL: + case GTK_ALIGN_BASELINE_CENTER: default: return align; } @@ -3765,7 +3766,7 @@ adjust_for_align (GtkAlign align, { switch (align) { - case GTK_ALIGN_BASELINE: + case GTK_ALIGN_BASELINE_CENTER: if (*allocated_size > natural_size && nat_baseline > -1 && *allocated_baseline > -1) @@ -3773,8 +3774,18 @@ adjust_for_align (GtkAlign align, *allocated_pos = *allocated_baseline - nat_baseline; *allocated_size = MIN (*allocated_size, natural_size); *allocated_baseline = nat_baseline; + break; + } + G_GNUC_FALLTHROUGH; + + case GTK_ALIGN_CENTER: + if (*allocated_size > natural_size) + { + *allocated_pos += (*allocated_size - natural_size) / 2; + *allocated_size = MIN (*allocated_size, natural_size); } break; + case GTK_ALIGN_BASELINE_FILL: case GTK_ALIGN_FILL: default: /* change nothing */ @@ -3790,13 +3801,6 @@ adjust_for_align (GtkAlign align, *allocated_size = natural_size; } break; - case GTK_ALIGN_CENTER: - if (*allocated_size > natural_size) - { - *allocated_pos += (*allocated_size - natural_size) / 2; - *allocated_size = MIN (*allocated_size, natural_size); - } - break; } } @@ -3868,7 +3872,8 @@ gtk_widget_adjust_size_allocation (GtkWidget *widget, } out: - if (priv->valign != GTK_ALIGN_BASELINE) + if (priv->valign != GTK_ALIGN_BASELINE_FILL && + priv->valign != GTK_ALIGN_BASELINE_CENTER) *baseline = -1; } @@ -9444,8 +9449,10 @@ gtk_widget_get_halign (GtkWidget *widget) g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_ALIGN_FILL); - if (priv->halign == GTK_ALIGN_BASELINE) + if (priv->halign == GTK_ALIGN_BASELINE_FILL) return GTK_ALIGN_FILL; + else if (priv->halign == GTK_ALIGN_BASELINE_CENTER) + return GTK_ALIGN_CENTER; return priv->halign; } diff --git a/tests/testbaseline2.c b/tests/testbaseline2.c index c720cdf303..ebaed92753 100644 --- a/tests/testbaseline2.c +++ b/tests/testbaseline2.c @@ -221,7 +221,7 @@ baseline_widget_new (int above, int below, int across) "above", above, "below", below, "across", across, - "valign", GTK_ALIGN_BASELINE, + "valign", GTK_ALIGN_BASELINE_CENTER, NULL); } @@ -254,13 +254,13 @@ main (int argc, char *argv[]) gtk_widget_set_margin_bottom (box, 20); gtk_widget_set_margin_start (box, 20); gtk_widget_set_margin_end (box, 20); - gtk_widget_set_valign (box, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (box, GTK_ALIGN_BASELINE_CENTER); gtk_stack_add_titled (GTK_STACK (stack), box, "boxes", "Boxes"); box1 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0); gtk_widget_set_halign (box1, GTK_ALIGN_FILL); - gtk_widget_set_valign (box1, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (box1, GTK_ALIGN_BASELINE_CENTER); gtk_widget_set_hexpand (box1, TRUE); child = baseline_widget_new (20, 10, 20); @@ -279,7 +279,7 @@ main (int argc, char *argv[]) box1 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0); gtk_widget_set_halign (box1, GTK_ALIGN_FILL); - gtk_widget_set_valign (box1, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (box1, GTK_ALIGN_BASELINE_CENTER); gtk_widget_set_hexpand (box1, TRUE); child = baseline_widget_new (10, 15, 10); @@ -301,10 +301,10 @@ main (int argc, char *argv[]) gtk_widget_set_margin_bottom (box, 20); gtk_widget_set_margin_start (box, 20); gtk_widget_set_margin_end (box, 20); - gtk_widget_set_valign (box, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (box, GTK_ALIGN_BASELINE_CENTER); grid = gtk_grid_new (); - gtk_widget_set_valign (grid, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (grid, GTK_ALIGN_BASELINE_CENTER); gtk_widget_set_hexpand (grid, TRUE); child = baseline_widget_new (20, 10, 20); @@ -322,7 +322,7 @@ main (int argc, char *argv[]) gtk_box_append (GTK_BOX (box), grid); grid = gtk_grid_new (); - gtk_widget_set_valign (grid, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (grid, GTK_ALIGN_BASELINE_CENTER); gtk_widget_set_hexpand (grid, TRUE); child = baseline_widget_new (10, 15, 10); @@ -346,44 +346,44 @@ main (int argc, char *argv[]) gtk_widget_set_margin_bottom (box, 20); gtk_widget_set_margin_start (box, 20); gtk_widget_set_margin_end (box, 20); - gtk_widget_set_valign (box, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (box, GTK_ALIGN_BASELINE_CENTER); child = baseline_widget_new (60, 15, 20); gtk_box_append (GTK_BOX (box), child); child = gtk_label_new ("Label"); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_entry_new (); gtk_editable_set_text (GTK_EDITABLE (child), "Entry"); gtk_editable_set_width_chars (GTK_EDITABLE (child), 10); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_password_entry_new (); gtk_editable_set_text (GTK_EDITABLE (child), "Password"); gtk_editable_set_width_chars (GTK_EDITABLE (child), 10); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_spin_button_new_with_range (0, 100, 1); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_spin_button_new_with_range (0, 100, 1); gtk_orientable_set_orientation (GTK_ORIENTABLE (child), GTK_ORIENTATION_VERTICAL); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_switch_new (); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0, 100, 1); gtk_widget_set_size_request (child, 100, -1); //gtk_scale_add_mark (GTK_SCALE (child), 50, GTK_POS_BOTTOM, "50"); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); gtk_stack_add_titled (GTK_STACK (stack), box, "controls", "Controls"); @@ -393,7 +393,7 @@ main (int argc, char *argv[]) gtk_widget_set_margin_bottom (box, 20); gtk_widget_set_margin_start (box, 20); gtk_widget_set_margin_end (box, 20); - gtk_widget_set_valign (box, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (box, GTK_ALIGN_BASELINE_CENTER); child = baseline_widget_new (60, 15, 20); gtk_widget_set_hexpand (child, TRUE); @@ -401,40 +401,40 @@ main (int argc, char *argv[]) child = gtk_label_new ("Label"); gtk_widget_set_hexpand (child, TRUE); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_label_new ("Two\nlines"); gtk_widget_set_hexpand (child, TRUE); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_label_new ("Large"); gtk_widget_set_hexpand (child, TRUE); gtk_label_set_use_markup (GTK_LABEL (child), TRUE); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_label_new ("Huge"); gtk_widget_set_hexpand (child, TRUE); gtk_label_set_use_markup (GTK_LABEL (child), TRUE); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_label_new ("Underlined"); gtk_widget_set_hexpand (child, TRUE); gtk_label_set_use_markup (GTK_LABEL (child), TRUE); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_label_new ("♥️"); gtk_widget_set_hexpand (child, TRUE); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); child = gtk_image_new_from_icon_name ("edit-copy-symbolic"); gtk_widget_set_hexpand (child, TRUE); - gtk_widget_set_valign (child, GTK_ALIGN_BASELINE); + gtk_widget_set_valign (child, GTK_ALIGN_BASELINE_CENTER); gtk_box_append (GTK_BOX (box), child); gtk_stack_add_titled (GTK_STACK (stack), box, "labels", "Labels"); From 58a5de6f5d8d671649302c983c46d9a1e315b603 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 8 May 2023 07:00:34 -0400 Subject: [PATCH 2/2] testsuite: Handle duplicate enum values Our notify tests would fall over if there was a duplicate enum value (within the first 10 values). Make it handle that, by skipping the duplicate value. --- testsuite/gtk/notify.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/testsuite/gtk/notify.c b/testsuite/gtk/notify.c index 3f313ac435..01ee5f13d0 100644 --- a/testsuite/gtk/notify.c +++ b/testsuite/gtk/notify.c @@ -89,8 +89,13 @@ check_property (GObject *instance, GParamSpec *pspec) for (i = first; i < class->n_values; i++) { + /* skip duplicates */ + if (i > 0 && class->values[i].value == class->values[i - 1].value) + continue; + current_count = data.count + 1; g_object_set (instance, pspec->name, class->values[i].value, NULL); + assert_notifies (instance, pspec->name, data.count, current_count); if (current_count == 10) /* just test a few */