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.
This commit is contained in:
Matthias Clasen 2023-05-07 21:26:09 -04:00
parent 61ff647f71
commit 082ba6d7c8
10 changed files with 75 additions and 49 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;
/**

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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 ("<span size='large'>Large</span>");
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 ("<span size='xx-large'>Huge</span>");
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 ("<span underline='double'>Underlined</span>");
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");