+typedef struct _CountingData CountingData;
+struct _CountingData {
+ GtkWidget *widget;
+ gboolean found;
+ guint before;
+ guint after;
+};
+
+static void
+count_widget_position (GtkWidget *widget,
+ gpointer data)
+{
+ CountingData *count = data;
+
+ if (!gtk_widget_get_visible (widget))
+ return;
+
+ if (count->widget == widget)
+ count->found = TRUE;
+ else if (count->found)
+ count->after++;
+ else
+ count->before++;
+}
+
+static gint
+gtk_box_get_visible_position (GtkBox *box,
+ GtkWidget *child)
+{
+ CountingData count = { child, FALSE, 0, 0 };
+
+ /* foreach iterates in visible order */
+ gtk_container_foreach (GTK_CONTAINER (box),
+ count_widget_position,
+ &count);
+
+ /* the child wasn't found, it's likely an internal child of some
+ * subclass, return -1 to indicate that there is no sibling relation
+ * to the regular box children
+ */
+ if (!count.found)
+ return -1;
+
+ if (box->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
+ gtk_widget_get_direction (GTK_WIDGET (box)) == GTK_TEXT_DIR_RTL)
+ return count.after;
+ else
+ return count.before;
+}
+
+static GtkWidgetPath *
+gtk_box_get_path_for_child (GtkContainer *container,
+ GtkWidget *child)
+{
+ GtkWidgetPath *path, *sibling_path;
+ GtkBox *box;
+ GtkBoxPrivate *private;
+ GList *list, *children;
+
+ box = GTK_BOX (container);
+ private = box->priv;
+
+ path = _gtk_widget_create_path (GTK_WIDGET (container));
+
+ if (gtk_widget_get_visible (child))
+ {
+ gint position;
+
+ sibling_path = gtk_widget_path_new ();
+
+ /* get_children works in visible order */
+ children = gtk_container_get_children (container);
+ if (private->orientation == GTK_ORIENTATION_HORIZONTAL &&
+ gtk_widget_get_direction (GTK_WIDGET (box)) == GTK_TEXT_DIR_RTL)
+ children = g_list_reverse (children);
+
+ for (list = children; list; list = list->next)
+ {
+ if (!gtk_widget_get_visible (list->data))
+ continue;
+
+ gtk_widget_path_append_for_widget (sibling_path, list->data);
+ }
+
+ g_list_free (children);
+
+ position = gtk_box_get_visible_position (box, child);
+
+ if (position >= 0)
+ gtk_widget_path_append_with_siblings (path, sibling_path, position);
+ else
+ gtk_widget_path_append_for_widget (path, child);
+
+ gtk_widget_path_unref (sibling_path);
+ }
+ else
+ gtk_widget_path_append_for_widget (path, child);
+
+ return path;
+}
+
+static void
+gtk_box_invalidate_order_foreach (GtkWidget *widget)
+{
+ _gtk_widget_invalidate_style_context (widget, GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_SIBLING_POSITION);
+}
+
+static void
+gtk_box_invalidate_order (GtkBox *box)
+{
+ gtk_container_foreach (GTK_CONTAINER (box),
+ (GtkCallback) gtk_box_invalidate_order_foreach,
+ NULL);
+}
+
+static void
+gtk_box_direction_changed (GtkWidget *widget,
+ GtkTextDirection previous_direction)
+{
+ gtk_box_invalidate_order (GTK_BOX (widget));
+}
+
+static void
+box_child_visibility_notify_cb (GObject *obj,
+ GParamSpec *pspec,
+ gpointer user_data)
+{
+ GtkBox *box = user_data;
+
+ gtk_box_invalidate_order (box);
+}
+