+
+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 guint
+gtk_toolbar_get_visible_position (GtkToolbar *toolbar,
+ GtkWidget *child)
+{
+ CountingData count = { child, FALSE, 0, 0 };
+
+ if (child == (GtkWidget*)toolbar->priv->highlight_tool_item)
+ return 0;
+
+ /* foreach iterates in visible order */
+ gtk_container_forall (GTK_CONTAINER (toolbar),
+ count_widget_position,
+ &count);
+
+ g_assert (count.found);
+
+ if (toolbar->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
+ gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
+ return count.after;
+
+ return count.before;
+}
+
+static void
+add_widget_to_path (gpointer data,
+ gpointer user_data)
+{
+ GtkWidget *widget = data;
+ GtkWidgetPath *path = user_data;
+
+ if (gtk_widget_get_visible (widget))
+ gtk_widget_path_append_for_widget (path, widget);
+}
+
+static GtkWidgetPath *
+gtk_toolbar_get_path_for_child (GtkContainer *container,
+ GtkWidget *child)
+{
+ GtkWidgetPath *path;
+ GtkToolbar *toolbar;
+ GtkToolbarPrivate *priv;
+ GtkWidgetPath *sibling_path;
+ gint vis_index;
+ GList *children;
+
+ toolbar = GTK_TOOLBAR (container);
+ priv = toolbar->priv;
+
+ /* build a path for all the visible children;
+ * get_children works in visible order
+ */
+ sibling_path = gtk_widget_path_new ();
+ children = _gtk_container_get_all_children (container);
+
+ if (priv->orientation != GTK_ORIENTATION_HORIZONTAL ||
+ gtk_widget_get_direction (GTK_WIDGET (toolbar)) != GTK_TEXT_DIR_RTL)
+ children = g_list_reverse (children);
+
+ g_list_foreach (children, add_widget_to_path, sibling_path);
+ g_list_free (children);
+
+ path = _gtk_widget_create_path (GTK_WIDGET (container));
+ if (gtk_widget_get_visible (child))
+ {
+ vis_index = gtk_toolbar_get_visible_position (toolbar, child);
+
+ if (vis_index < gtk_widget_path_length (sibling_path))
+ gtk_widget_path_append_with_siblings (path,
+ sibling_path,
+ vis_index);
+ else
+ gtk_widget_path_append_for_widget (path, child);
+ }
+ else
+ gtk_widget_path_append_for_widget (path, child);
+
+ gtk_widget_path_unref (sibling_path);
+ return path;
+}
+
+static void
+gtk_toolbar_invalidate_order_foreach (GtkWidget *widget)
+{
+ _gtk_widget_invalidate_style_context (widget, GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_SIBLING_POSITION);
+}
+
+static void
+gtk_toolbar_invalidate_order (GtkToolbar *toolbar)
+{
+ gtk_container_forall (GTK_CONTAINER (toolbar),
+ (GtkCallback) gtk_toolbar_invalidate_order_foreach,
+ NULL);
+}
+
+static void
+gtk_toolbar_direction_changed (GtkWidget *widget,
+ GtkTextDirection previous_direction)
+{
+ GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->direction_changed (widget, previous_direction);
+
+ gtk_toolbar_invalidate_order (GTK_TOOLBAR (widget));
+}
+