box: Avoid position in the reorder api

Change the reorder api to insert after a sibling,
so that moving to first place becomes reorder (... NULL).

And add a insert_after api that can replace the common
container_add / reorder_after (... NULL) combination.

Update all callers.
This commit is contained in:
Matthias Clasen 2019-01-22 18:44:34 -05:00
parent 883d5d8584
commit f3f5a896de
17 changed files with 150 additions and 131 deletions

View File

@ -323,9 +323,10 @@ gtk_box_get_homogeneous
gtk_box_set_homogeneous
gtk_box_get_spacing
gtk_box_set_spacing
gtk_box_reorder_child
gtk_box_get_baseline_position
gtk_box_set_baseline_position
gtk_box_insert_child_after
gtk_box_reorder_child_after
<SUBSECTION Standard>
GTK_BOX
GTK_IS_BOX

View File

@ -210,8 +210,7 @@ gtk_action_bar_set_child_property (GtkContainer *container,
{
g_object_ref (child);
gtk_container_remove (GTK_CONTAINER (priv->start_box), child);
gtk_container_add (GTK_CONTAINER (priv->end_box), child);
gtk_box_reorder_child (GTK_BOX (priv->end_box), child, 0);
gtk_box_insert_child_after (GTK_BOX (priv->end_box), child, NULL);
g_object_unref (child);
}
}
@ -479,8 +478,7 @@ gtk_action_bar_pack_end (GtkActionBar *action_bar,
{
GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (action_bar);
gtk_container_add (GTK_CONTAINER (priv->end_box), child);
gtk_box_reorder_child (GTK_BOX (priv->end_box), child, 0);
gtk_box_insert_child_after (GTK_BOX (priv->end_box), child, NULL);
}
/**

View File

@ -1125,7 +1125,7 @@ gtk_assistant_init (GtkAssistant *assistant)
buttons = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
for (l = buttons; l; l = l->next)
gtk_box_reorder_child (GTK_BOX (priv->action_area), GTK_WIDGET (l->data), -1);
gtk_box_reorder_child_after (GTK_BOX (priv->action_area), GTK_WIDGET (l->data), NULL);
g_list_free (buttons);
}
@ -1644,6 +1644,7 @@ gtk_assistant_insert_page (GtkAssistant *assistant,
gint n_pages;
GtkStyleContext *context;
GtkWidget *box;
GtkWidget *sibling;
g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), 0);
g_return_val_if_fail (GTK_IS_WIDGET (page), 0);
@ -1686,10 +1687,18 @@ gtk_assistant_insert_page (GtkAssistant *assistant,
priv->pages = g_list_insert (priv->pages, page_info, position);
gtk_container_add (GTK_CONTAINER (priv->sidebar), page_info->regular_title);
gtk_container_add (GTK_CONTAINER (priv->sidebar), page_info->current_title);
gtk_box_reorder_child (GTK_BOX (priv->sidebar), page_info->regular_title, 2 * position);
gtk_box_reorder_child (GTK_BOX (priv->sidebar), page_info->current_title, 2 * position + 1);
if (position == 0)
sibling = NULL;
else
{
int i;
sibling = gtk_widget_get_first_child (priv->sidebar);
for (i = 1; i < 2 * position; i++)
sibling = gtk_widget_get_next_sibling (sibling);
}
gtk_box_insert_child_after (GTK_BOX (priv->sidebar), page_info->current_title, sibling);
gtk_box_insert_child_after (GTK_BOX (priv->sidebar), page_info->regular_title, sibling);
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_show (box);

View File

@ -386,9 +386,7 @@ gtk_button_box_set_child_secondary (GtkButtonBox *widget,
gtk_widget_child_notify (child, "secondary");
if (bbox->priv->layout_style == GTK_BUTTONBOX_EXPAND)
{
gtk_box_reorder_child (GTK_BOX (bbox), child, is_secondary ? 0 : -1);
}
gtk_box_reorder_child_after (GTK_BOX (bbox), child, is_secondary ? NULL : gtk_widget_get_last_child (GTK_WIDGET (bbox)));
if (gtk_widget_get_visible (GTK_WIDGET (widget)) &&
gtk_widget_get_visible (child))

View File

@ -35,8 +35,9 @@
* the children to influence their allocation.
*
* Use repeated calls to gtk_container_add() to pack widgets into a
* GtkBox from start to end. Use gtk_container_remove()
* to remove widgets from the GtkBox.
* GtkBox from start to end. Use gtk_container_remove() to remove widgets
* from the GtkBox. gtk_box_insert_child_after() can be used to add a child
* at a particular position.
*
* Use gtk_box_set_homogeneous() to specify whether or not all children
* of the GtkBox are forced to get the same amount of space.
@ -45,7 +46,7 @@
* minimally placed between all children in the GtkBox. Note that
* spacing is added between the children.
*
* Use gtk_box_reorder_child() to move a GtkBox child to a different
* Use gtk_box_reorder_child_after() to move a child to a different
* place in the box.
*
* # CSS nodes
@ -1047,75 +1048,6 @@ gtk_box_get_baseline_position (GtkBox *box)
return priv->baseline_pos;
}
/**
* gtk_box_reorder_child:
* @box: a #GtkBox
* @child: the #GtkWidget to move
* @position: the new position for @child in the list of children
* of @box, starting from 0. If negative, indicates the end of
* the list
*
* Moves @child to a new @position in the list of @box children.
*/
void
gtk_box_reorder_child (GtkBox *box,
GtkWidget *child,
gint position)
{
GtkWidget *widget;
g_return_if_fail (GTK_IS_BOX (box));
g_return_if_fail (GTK_IS_WIDGET (child));
widget = GTK_WIDGET (box);
if (position == 0)
{
gtk_widget_insert_after (child, widget, NULL);
gtk_css_node_insert_after (gtk_widget_get_css_node (widget),
gtk_widget_get_css_node (child),
NULL);
}
else if (position < 0)
{
gtk_widget_insert_before (child, widget, NULL);
gtk_css_node_insert_before (gtk_widget_get_css_node (widget),
gtk_widget_get_css_node (child),
NULL);
}
else
{
int i = 0;
int old_pos = -1;
GtkWidget *p;
GtkWidget *new_next_sibling = NULL;
for (p = _gtk_widget_get_first_child (widget);
p != NULL;
p = _gtk_widget_get_next_sibling (p))
{
if (p == child)
old_pos = i;
if (i == position + 1)
{
new_next_sibling = p;
}
i ++;
}
if (position == old_pos)
return;
gtk_widget_insert_before (child, widget, new_next_sibling);
gtk_css_node_insert_before (gtk_widget_get_css_node (widget),
gtk_widget_get_css_node (child),
new_next_sibling ? gtk_widget_get_css_node (new_next_sibling) : NULL);
}
}
static void
gtk_box_add (GtkContainer *container,
GtkWidget *child)
@ -1164,3 +1096,73 @@ _gtk_box_get_children (GtkBox *box)
return g_list_reverse (retval);
}
/**
* gtk_box_insert_child_after:
* @box: a #GtkBox
* @child: the #GtkWidget to insert
* @sibling: (nullable): the sibling to move @child after, or %NULL
*
* Inserts @child in the position after @sibling in the list
* of @box children. If @sibling is %NULL, insert @child at
* the first position.
*/
void
gtk_box_insert_child_after (GtkBox *box,
GtkWidget *child,
GtkWidget *sibling)
{
GtkWidget *widget = GTK_WIDGET (box);
g_return_if_fail (GTK_IS_BOX (box));
g_return_if_fail (GTK_IS_WIDGET (child));
g_return_if_fail (gtk_widget_get_parent (child) == NULL);
if (sibling)
{
g_return_if_fail (GTK_IS_WIDGET (sibling));
g_return_if_fail (gtk_widget_get_parent (sibling) == widget);
}
if (child == sibling)
return;
gtk_widget_insert_after (child, widget, sibling);
gtk_css_node_insert_after (gtk_widget_get_css_node (widget),
gtk_widget_get_css_node (child),
sibling ? gtk_widget_get_css_node (sibling) : NULL);
}
/**
* gtk_box_reorder_child_after:
* @box: a #GtkBox
* @child: the #GtkWidget to move, must be a child of @box
* @sibling: (nullable): the sibling to move @child after, or %NULL
*
* Moves @child to the position after @sibling in the list
* of @box children. If @sibling is %NULL, move @child to
* the first position.
*/
void
gtk_box_reorder_child_after (GtkBox *box,
GtkWidget *child,
GtkWidget *sibling)
{
GtkWidget *widget = GTK_WIDGET (box);
g_return_if_fail (GTK_IS_BOX (box));
g_return_if_fail (GTK_IS_WIDGET (child));
g_return_if_fail (gtk_widget_get_parent (child) == widget);
if (sibling)
{
g_return_if_fail (GTK_IS_WIDGET (sibling));
g_return_if_fail (gtk_widget_get_parent (sibling) == widget);
}
if (child == sibling)
return;
gtk_widget_insert_after (child, widget, sibling);
gtk_css_node_insert_after (gtk_widget_get_css_node (widget),
gtk_widget_get_css_node (child),
sibling ? gtk_widget_get_css_node (sibling) : NULL);
}

View File

@ -92,9 +92,15 @@ GDK_AVAILABLE_IN_ALL
GtkBaselinePosition gtk_box_get_baseline_position (GtkBox *box);
GDK_AVAILABLE_IN_ALL
void gtk_box_reorder_child (GtkBox *box,
void gtk_box_insert_child_after (GtkBox *box,
GtkWidget *child,
GtkWidget *sibling);
GDK_AVAILABLE_IN_ALL
void gtk_box_reorder_child_after (GtkBox *box,
GtkWidget *child,
gint position);
GtkWidget *sibling);
G_END_DECLS

View File

@ -745,8 +745,7 @@ add_custom_color (GtkColorChooserWidget *cc,
gtk_color_swatch_set_can_drop (GTK_COLOR_SWATCH (p), TRUE);
connect_custom_signals (p, cc);
gtk_container_add (GTK_CONTAINER (cc->priv->custom), p);
gtk_box_reorder_child (GTK_BOX (cc->priv->custom), p, 1);
gtk_box_insert_child_after (GTK_BOX (cc->priv->custom), p, gtk_widget_get_first_child (cc->priv->custom));
gtk_widget_show (p);
select_swatch (cc, GTK_COLOR_SWATCH (p));

View File

@ -1156,9 +1156,7 @@ gtk_combo_box_create_child (GtkComboBox *combo_box)
gtk_widget_set_hexpand (child, TRUE);
gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
gtk_container_add (GTK_CONTAINER (gtk_widget_get_parent (priv->arrow)),
priv->cell_view);
gtk_box_reorder_child (GTK_BOX (gtk_widget_get_parent (priv->arrow)), priv->cell_view, 0);
gtk_box_insert_child_after (GTK_BOX (gtk_widget_get_parent (priv->arrow)), priv->cell_view, NULL);
_gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
}
}
@ -1193,8 +1191,7 @@ gtk_combo_box_add (GtkContainer *container,
}
gtk_widget_set_hexpand (widget, TRUE);
gtk_container_add (GTK_CONTAINER (priv->box), widget);
gtk_box_reorder_child (GTK_BOX (priv->box), widget, 0);
gtk_box_insert_child_after (GTK_BOX (priv->box), widget, NULL);
_gtk_bin_set_child (GTK_BIN (container), widget);
if (priv->has_entry)

View File

@ -916,8 +916,7 @@ update_preview_widget_visibility (GtkFileChooserWidget *impl)
if (!priv->preview_label)
{
priv->preview_label = gtk_label_new (priv->preview_display_name);
gtk_container_add (GTK_CONTAINER (priv->preview_box), priv->preview_label);
gtk_box_reorder_child (GTK_BOX (priv->preview_box), priv->preview_label, 0);
gtk_box_insert_child_after (GTK_BOX (priv->preview_box), priv->preview_label, NULL);
gtk_label_set_ellipsize (GTK_LABEL (priv->preview_label), PANGO_ELLIPSIZE_MIDDLE);
gtk_widget_show (priv->preview_label);
}
@ -958,9 +957,6 @@ set_preview_widget (GtkFileChooserWidget *impl,
{
gtk_widget_show (priv->preview_widget);
gtk_container_add (GTK_CONTAINER (priv->preview_box), priv->preview_widget);
gtk_box_reorder_child (GTK_BOX (priv->preview_box),
priv->preview_widget,
(priv->use_preview_label && priv->preview_label) ? 1 : 0);
}
update_preview_widget_visibility (impl);
@ -2607,8 +2603,7 @@ save_widgets_create (GtkFileChooserWidget *impl)
gtk_label_set_mnemonic_widget (GTK_LABEL (widget), priv->location_entry);
priv->save_widgets = vbox;
gtk_container_add (GTK_CONTAINER (priv->box), priv->save_widgets);
gtk_box_reorder_child (GTK_BOX (priv->box), priv->save_widgets, 0);
gtk_box_insert_child_after (GTK_BOX (priv->box), priv->save_widgets, NULL);
gtk_widget_show (priv->save_widgets);
}

View File

@ -470,7 +470,7 @@ _gtk_header_bar_update_window_buttons (GtkHeaderBar *bar)
gtk_container_add (GTK_CONTAINER (box), separator);
if (i == 1)
gtk_box_reorder_child (GTK_BOX (box), separator, 0);
gtk_box_reorder_child_after (GTK_BOX (box), separator, NULL);
if (i == 0)
gtk_style_context_add_class (gtk_widget_get_style_context (box), GTK_STYLE_CLASS_LEFT);

View File

@ -134,10 +134,7 @@ gtk_menu_section_box_sync_separators (GtkMenuSectionBox *box,
return;
if (should_have_separator)
{
gtk_container_add (GTK_CONTAINER (box), box->separator);
gtk_box_reorder_child (GTK_BOX (box), box->separator, 0);
}
gtk_box_insert_child_after (GTK_BOX (box), box->separator, NULL);
else
gtk_container_remove (GTK_CONTAINER (box), box->separator);
}
@ -333,7 +330,17 @@ gtk_menu_section_box_insert_func (GtkMenuTrackerItem *item,
gtk_widget_set_halign (widget, GTK_ALIGN_FILL);
gtk_container_add (GTK_CONTAINER (box->item_box), widget);
gtk_box_reorder_child (GTK_BOX (box->item_box), widget, position);
if (position == 0)
gtk_box_reorder_child_after (GTK_BOX (box->item_box), widget, NULL);
else
{
GtkWidget *sibling = gtk_widget_get_first_child (GTK_WIDGET (box->item_box));
int i;
for (i = 1; i < position; i++)
sibling = gtk_widget_get_next_sibling (sibling);
gtk_box_reorder_child_after (GTK_BOX (box->item_box), widget, sibling);
}
gtk_menu_section_box_schedule_separator_sync (box);
}
@ -457,8 +464,7 @@ gtk_menu_section_box_new_submenu (GtkMenuTrackerItem *item,
g_object_set_data (G_OBJECT (button), "focus", focus);
g_object_set_data (G_OBJECT (focus), "focus", button);
gtk_container_add (GTK_CONTAINER (box), button);
gtk_box_reorder_child (GTK_BOX (box), button, 0);
gtk_box_insert_child_after (GTK_BOX (box), button, NULL);
g_signal_connect (focus, "clicked", G_CALLBACK (open_submenu), item);
g_signal_connect (button, "clicked", G_CALLBACK (close_submenu), item);

View File

@ -191,8 +191,7 @@ gtk_model_menu_item_set_icon (GtkModelMenuItem *item,
image = gtk_image_new_from_gicon (icon);
gtk_image_set_pixel_size (GTK_IMAGE (image), 16);
gtk_container_add (GTK_CONTAINER (child), image);
gtk_box_reorder_child (GTK_BOX (child), image, 0);
gtk_box_insert_child_after (GTK_BOX (child), image, NULL);
}
g_object_notify (G_OBJECT (item), "icon");

View File

@ -6204,7 +6204,7 @@ gtk_notebook_update_tab_pos (GtkNotebook *notebook)
gtk_widget_set_hexpand (priv->header_widget, TRUE);
gtk_widget_set_vexpand (priv->header_widget, FALSE);
if (priv->show_tabs)
gtk_box_reorder_child (GTK_BOX (priv->box), priv->header_widget, 0);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->header_widget, NULL);
gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box), GTK_ORIENTATION_VERTICAL);
gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->header_widget), GTK_ORIENTATION_HORIZONTAL);
@ -6216,7 +6216,7 @@ gtk_notebook_update_tab_pos (GtkNotebook *notebook)
gtk_widget_set_hexpand (priv->header_widget, TRUE);
gtk_widget_set_vexpand (priv->header_widget, FALSE);
if (priv->show_tabs)
gtk_box_reorder_child (GTK_BOX (priv->box), priv->header_widget, 1);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->header_widget, gtk_widget_get_last_child (priv->box));
gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box), GTK_ORIENTATION_VERTICAL);
gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->header_widget), GTK_ORIENTATION_HORIZONTAL);
@ -6228,7 +6228,7 @@ gtk_notebook_update_tab_pos (GtkNotebook *notebook)
gtk_widget_set_hexpand (priv->header_widget, FALSE);
gtk_widget_set_vexpand (priv->header_widget, TRUE);
if (priv->show_tabs)
gtk_box_reorder_child (GTK_BOX (priv->box), priv->header_widget, 0);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->header_widget, NULL);
gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box), GTK_ORIENTATION_HORIZONTAL);
gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->header_widget), GTK_ORIENTATION_VERTICAL);
@ -6240,7 +6240,7 @@ gtk_notebook_update_tab_pos (GtkNotebook *notebook)
gtk_widget_set_hexpand (priv->header_widget, FALSE);
gtk_widget_set_vexpand (priv->header_widget, TRUE);
if (priv->show_tabs)
gtk_box_reorder_child (GTK_BOX (priv->box), priv->header_widget, 1);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->header_widget, gtk_widget_get_last_child (priv->box));
gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box), GTK_ORIENTATION_HORIZONTAL);
gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->header_widget), GTK_ORIENTATION_VERTICAL);
@ -7147,10 +7147,11 @@ gtk_notebook_set_action_widget (GtkNotebook *notebook,
if (widget)
{
int pos = pack_type == GTK_PACK_START ? 0 : -1;
gtk_container_add (GTK_CONTAINER (priv->header_widget), widget);
gtk_box_reorder_child (GTK_BOX (priv->header_widget), widget, pos);
if (pack_type == GTK_PACK_START)
gtk_box_reorder_child_after (GTK_BOX (priv->header_widget), widget, NULL);
else
gtk_box_reorder_child_after (GTK_BOX (priv->header_widget), widget, gtk_widget_get_last_child (priv->header_widget));
gtk_widget_set_child_visible (widget, priv->show_tabs);
}

View File

@ -727,16 +727,13 @@ apply_orientation (GtkScaleButton *button,
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
gtk_box_reorder_child (GTK_BOX (priv->box), priv->scale, 0);
gtk_box_reorder_child (GTK_BOX (priv->box), priv->minus_button, 1);
gtk_box_reorder_child (GTK_BOX (priv->box), priv->plus_button, 2);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->plus_button, NULL);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->scale, NULL);
}
else
{
gtk_box_reorder_child (GTK_BOX (priv->box), priv->scale, 1);
gtk_box_reorder_child (GTK_BOX (priv->box), priv->minus_button, 2);
gtk_box_reorder_child (GTK_BOX (priv->box), priv->plus_button, 0);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->scale, NULL);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->plus_button, NULL);
}
gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->scale), orientation);

View File

@ -868,6 +868,7 @@ gtk_spin_button_init (GtkSpinButton *spin_button)
priv->box = gtk_box_new (priv->orientation, 0);
gtk_widget_set_parent (priv->box, GTK_WIDGET (spin_button));
priv->entry = gtk_entry_new ();
gtk_entry_set_width_chars (GTK_ENTRY (priv->entry), 0);
gtk_entry_set_max_width_chars (GTK_ENTRY (priv->entry), 0);
@ -1051,13 +1052,14 @@ gtk_spin_button_set_orientation (GtkSpinButton *spin,
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
{
/* Current orientation of the box is vertical! */
gtk_box_reorder_child (GTK_BOX (priv->box), priv->entry, 0);
gtk_box_reorder_child (GTK_BOX (priv->box), priv->down_button, 1);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->up_button, NULL);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->entry, NULL);
}
else
{
/* Current orientation of the box is horizontal! */
gtk_box_reorder_child (GTK_BOX (priv->box), priv->up_button, 0);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->entry, NULL);
gtk_box_reorder_child_after (GTK_BOX (priv->box), priv->up_button, NULL);
}
gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box), priv->orientation);

View File

@ -235,7 +235,16 @@ on_position_updated (GtkWidget *widget,
"position", &position,
NULL);
gtk_box_reorder_child (GTK_BOX (self), button, position);
if (position == 0)
gtk_box_reorder_child_after (GTK_BOX (self), button, NULL);
else
{
GtkWidget *sibling = gtk_widget_get_first_child (GTK_WIDGET (self));
int i;
for (i = 1; i < position; i++)
sibling = gtk_widget_get_next_sibling (sibling);
gtk_box_reorder_child_after (GTK_BOX (self), button, sibling);
}
}
static void

View File

@ -964,9 +964,9 @@ gtk_tree_view_column_update_button (GtkTreeViewColumn *tree_column)
* reverse things
*/
if (priv->xalign <= 0.5)
gtk_box_reorder_child (GTK_BOX (hbox), arrow, 1);
gtk_box_reorder_child_after (GTK_BOX (hbox), arrow, gtk_widget_get_last_child (hbox));
else
gtk_box_reorder_child (GTK_BOX (hbox), arrow, 0);
gtk_box_reorder_child_after (GTK_BOX (hbox), arrow, NULL);
if (priv->show_sort_indicator
|| (GTK_IS_TREE_SORTABLE (model) && priv->sort_column_id >= 0))