1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 11:32:48 -05:00

Improve the align_next_to mechanism.

Various alignments I want to do in the host CA box have shown up
deficiencies in this system, so I've reworked it a bit.

Firstly, you can now specify more than two controls to be tied
together with an align_next_to (e.g. multiple checkboxes alongside
something else).

Secondly, as well as forcing the controls to be the same height as
each other, the layout algorithm will also move the later controls
further _downward_, so that their top y positions also line up. Until
now that hasn't been necessary, because they lined up already.

In the GTK implementation of this via the Columns class, I've renamed
'columns_force_same_height' to 'columns_align_next_to', and similarly
for some of the internal fields, since the latter change makes the
previous names a misnomer.

In the Windows implementation, I found it most convenient to set this
up by following a linked list of align_next_to fields backwards. But
it won't always be convenient to initialise them that way, so I've
also written a crude normaliser that will rewrite those links into a
canonical form. But I only call that on Windows; it's unnecessary in
GTK, where the Columns class provides plenty of per-widget extra
storage so I just keep each alignment class as a circular list.
This commit is contained in:
Simon Tatham
2022-05-02 14:37:11 +01:00
parent a5717f5ac2
commit b5ab90143a
7 changed files with 162 additions and 56 deletions

View File

@ -332,7 +332,6 @@ static void columns_remove(GtkContainer *container, GtkWidget *widget)
ColumnsChild *child;
GtkWidget *childw;
GList *children;
bool was_visible;
g_return_if_fail(container != NULL);
g_return_if_fail(IS_COLUMNS(container));
@ -346,23 +345,28 @@ static void columns_remove(GtkContainer *container, GtkWidget *widget)
if (child->widget != widget)
continue;
was_visible = gtk_widget_get_visible(widget);
bool need_layout = false;
if (gtk_widget_get_visible(widget))
need_layout = true;
gtk_widget_unparent(widget);
cols->children = g_list_remove_link(cols->children, children);
g_list_free(children);
if (child->same_height_as) {
g_return_if_fail(child->same_height_as->same_height_as == child);
child->same_height_as->same_height_as = NULL;
if (gtk_widget_get_visible(child->same_height_as->widget))
gtk_widget_queue_resize(GTK_WIDGET(container));
}
/* Unlink this widget from its valign list, and if anything
* else on the list is still visible, ensure we recompute our
* layout */
for (ColumnsChild *ch = child->valign_next; ch != child;
ch = ch->valign_next)
if (gtk_widget_get_visible(ch->widget))
need_layout = true;
child->valign_next->valign_prev = child->valign_prev;
child->valign_prev->valign_next = child->valign_next;
if (cols->vexpand == child)
cols->vexpand = NULL;
g_free(child);
if (was_visible)
if (need_layout)
gtk_widget_queue_resize(GTK_WIDGET(container));
break;
}
@ -465,7 +469,8 @@ void columns_add(Columns *cols, GtkWidget *child,
childdata->colstart = colstart;
childdata->colspan = colspan;
childdata->force_left = false;
childdata->same_height_as = NULL;
childdata->valign_next = childdata;
childdata->valign_prev = childdata;
childdata->percentages = NULL;
cols->children = g_list_append(cols->children, childdata);
@ -516,7 +521,7 @@ void columns_force_left_align(Columns *cols, GtkWidget *widget)
gtk_widget_queue_resize(GTK_WIDGET(cols));
}
void columns_force_same_height(Columns *cols, GtkWidget *cw1, GtkWidget *cw2)
void columns_align_next_to(Columns *cols, GtkWidget *cw1, GtkWidget *cw2)
{
ColumnsChild *child1, *child2;
@ -530,8 +535,13 @@ void columns_force_same_height(Columns *cols, GtkWidget *cw1, GtkWidget *cw2)
child2 = columns_find_child(cols, cw2);
g_return_if_fail(child2 != NULL);
child1->same_height_as = child2;
child2->same_height_as = child1;
ColumnsChild *child1prev = child1->valign_prev;
ColumnsChild *child2prev = child2->valign_prev;
child1prev->valign_next = child2;
child2->valign_prev = child1prev;
child2prev->valign_next = child1;
child1->valign_prev = child2prev;
if (gtk_widget_get_visible(cw1) || gtk_widget_get_visible(cw2))
gtk_widget_queue_resize(GTK_WIDGET(cols));
}
@ -843,8 +853,9 @@ static gint columns_compute_height(Columns *cols, widget_dim_fn_t get_height)
continue;
childheight = get_height(child);
if (child->same_height_as) {
gint childheight2 = get_height(child->same_height_as);
for (ColumnsChild *ch = child->valign_next; ch != child;
ch = ch->valign_next) {
gint childheight2 = get_height(ch);
if (childheight < childheight2)
childheight = childheight2;
}
@ -902,6 +913,11 @@ static void columns_alloc_vert(Columns *cols, gint ourheight,
colypos = g_new(gint, 1);
colypos[0] = 0;
for (children = cols->children;
children && (child = children->data);
children = children->next)
child->visited = false;
for (children = cols->children;
children && (child = children->data);
children = children->next) {
@ -922,14 +938,19 @@ static void columns_alloc_vert(Columns *cols, gint ourheight,
if (!gtk_widget_get_visible(child->widget))
continue;
int ymin = 0;
realheight = get_height(child);
if (child == cols->vexpand)
realheight += vexpand_extra;
fakeheight = realheight;
if (child->same_height_as) {
gint childheight2 = get_height(child->same_height_as);
for (ColumnsChild *ch = child->valign_next; ch != child;
ch = ch->valign_next) {
gint childheight2 = get_height(ch);
if (fakeheight < childheight2)
fakeheight = childheight2;
if (ch->visited && ymin < ch->y)
ymin = ch->y;
}
colspan = child->colspan ? child->colspan : ncols-child->colstart;
@ -943,13 +964,14 @@ static void columns_alloc_vert(Columns *cols, gint ourheight,
{
int topy, boty;
topy = 0;
topy = ymin;
for (i = 0; i < colspan; i++) {
if (topy < colypos[child->colstart+i])
topy = colypos[child->colstart+i];
}
child->y = topy + fakeheight/2 - realheight/2;
child->h = realheight;
child->visited = true;
boty = topy + fakeheight + cols->spacing;
for (i = 0; i < colspan; i++) {
colypos[child->colstart+i] = boty;

View File

@ -43,11 +43,17 @@ struct ColumnsChild_tag {
GtkWidget *widget;
gint colstart, colspan;
bool force_left; /* for recalcitrant GtkLabels */
ColumnsChild *same_height_as;
/* Otherwise, this entry represents a change in the column setup. */
gint ncols;
gint *percentages;
gint x, y, w, h; /* used during an individual size computation */
/* Circularly linked list of children that are vertically aligned
* with each other. */
ColumnsChild *valign_next, *valign_prev;
/* Temporary space used within some methods */
bool visited;
};
GType columns_get_type(void);
@ -57,7 +63,7 @@ void columns_add(Columns *cols, GtkWidget *child,
gint colstart, gint colspan);
void columns_taborder_last(Columns *cols, GtkWidget *child);
void columns_force_left_align(Columns *cols, GtkWidget *child);
void columns_force_same_height(Columns *cols, GtkWidget *ch1, GtkWidget *ch2);
void columns_align_next_to(Columns *cols, GtkWidget *ch1, GtkWidget *ch2);
void columns_vexpand(Columns *cols, GtkWidget *child);
#ifdef __cplusplus

View File

@ -2109,8 +2109,7 @@ GtkWidget *layout_ctrls(
columns_add(COLUMNS(container), label, 0, 1);
columns_force_left_align(COLUMNS(container), label);
columns_add(COLUMNS(container), w, 1, 1);
columns_force_same_height(COLUMNS(container),
label, w);
columns_align_next_to(COLUMNS(container), label, w);
}
gtk_widget_show(label);
gtk_widget_show(w);
@ -2163,7 +2162,7 @@ GtkWidget *layout_ctrls(
columns_add(COLUMNS(w), ww, 1, 1);
gtk_widget_show(ww);
columns_force_same_height(COLUMNS(w), uc->entry, uc->button);
columns_align_next_to(COLUMNS(w), uc->entry, uc->button);
g_signal_connect(G_OBJECT(uc->entry), "key_press_event",
G_CALLBACK(editbox_key), dp);
@ -2456,8 +2455,7 @@ GtkWidget *layout_ctrls(
columns_add(COLUMNS(container), label, 0, 1);
columns_force_left_align(COLUMNS(container), label);
columns_add(COLUMNS(container), w, 1, 1);
columns_force_same_height(COLUMNS(container),
label, w);
columns_align_next_to(COLUMNS(container), label, w);
}
gtk_widget_show(label);
gtk_widget_show(w);
@ -2520,19 +2518,10 @@ GtkWidget *layout_ctrls(
if (left)
columns_force_left_align(cols, w);
if (ctrl->align_next_to) {
/*
* Implement align_next_to by simply forcing the two
* controls to have the same height of size allocation. At
* least for the controls we're currently doing this with,
* the GTK layout system will automatically vertically
* centre each control within its allocation, which will
* get the two controls aligned alongside each other
* reasonably well.
*/
struct uctrl *uc2 = dlg_find_byctrl(
dp, ctrl->align_next_to);
assert(uc2);
columns_force_same_height(cols, w, uc2->toplevel);
columns_align_next_to(cols, w, uc2->toplevel);
#if GTK_CHECK_VERSION(3, 10, 0)
/* Slightly nicer to align baselines than just vertically