[gtk/stack-fixes: 3/8] stack: Provide a selection model
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/stack-fixes: 3/8] stack: Provide a selection model
- Date: Sat, 9 Feb 2019 23:18:50 +0000 (UTC)
commit b39558938dcf8d7eb8f92db76676db24381b45e1
Author: Matthias Clasen <mclasen redhat com>
Date: Fri Feb 8 20:36:58 2019 -0500
stack: Provide a selection model
Make GtkStack expose a selection model for its children.
The model can be used to track changes to the children
and the visible child, and to change what child is
visible.
gtk/gtkstack.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
gtk/gtkstack.h | 5 ++++
2 files changed, 90 insertions(+)
---
diff --git a/gtk/gtkstack.c b/gtk/gtkstack.c
index 338199c319..a473cc2a09 100644
--- a/gtk/gtkstack.c
+++ b/gtk/gtkstack.c
@@ -30,6 +30,7 @@
#include "gtksettingsprivate.h"
#include "gtksnapshot.h"
#include "gtkwidgetprivate.h"
+#include "gtksingleselection.h"
#include "a11y/gtkstackaccessible.h"
#include "a11y/gtkstackaccessibleprivate.h"
#include <math.h>
@@ -138,6 +139,8 @@ typedef struct {
GtkStackTransitionType active_transition_type;
+ GtkSelectionModel *pages;
+
} GtkStackPrivate;
static void gtk_stack_buildable_interface_init (GtkBuildableIface *iface);
@@ -2294,3 +2297,85 @@ gtk_stack_init (GtkStack *stack)
priv->transition_duration = 200;
priv->transition_type = GTK_STACK_TRANSITION_TYPE_NONE;
}
+
+static gboolean
+transform_to (GBinding *binding,
+ const GValue *from_value,
+ GValue *to_value,
+ gpointer user_data)
+{
+ GtkStack *stack = user_data;
+ GtkStackPrivate *priv = gtk_stack_get_instance_private (stack);
+ GtkWidget *child;
+ guint i;
+ guint pos = GTK_INVALID_LIST_POSITION;
+
+ child = g_value_get_object (from_value);
+ for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (priv->pages)); i++)
+ {
+ GtkWidget *widget;
+ widget = g_list_model_get_item (G_LIST_MODEL (priv->pages), i);
+ if (widget == child)
+ pos = i;
+ g_object_unref (widget);
+ if (pos != GTK_INVALID_LIST_POSITION)
+ break;
+ }
+
+ g_value_set_uint (to_value, pos);
+ return TRUE;
+}
+
+static gboolean
+transform_from (GBinding *binding,
+ const GValue *from_value,
+ GValue *to_value,
+ gpointer user_data)
+{
+ GtkStack *stack = user_data;
+ GtkStackPrivate *priv = gtk_stack_get_instance_private (stack);
+ guint selected;
+
+ selected = g_value_get_uint (from_value);
+ if (selected < g_list_model_get_n_items (G_LIST_MODEL (priv->pages)))
+ {
+ GtkWidget *child;
+ child = g_list_model_get_item (G_LIST_MODEL (priv->pages), selected);
+ g_value_set_object (to_value, child);
+ g_object_unref (child);
+ return TRUE;
+ }
+ else
+ return FALSE;
+}
+
+/**
+ * gtk_stack_get_pages:
+ * @stack: a #GtkStack
+ *
+ * Returns a #GListModel that contains the children of the stack,
+ * and can be used to keep and up-to-date view. The model also
+ * implements #GtkSelectionModel and can be used to track and
+ * modify the visible child.
+ *
+ * Returns: (transfer full): a #GtkSelectionModel for the stack's children
+ */
+GtkSelectionModel *
+gtk_stack_get_pages (GtkStack *stack)
+{
+ GtkStackPrivate *priv = gtk_stack_get_instance_private (stack);
+
+ g_return_val_if_fail (GTK_IS_STACK (stack), NULL);
+
+ if (priv->pages)
+ return g_object_ref (priv->pages);
+
+ priv->pages = GTK_SELECTION_MODEL (gtk_single_selection_new (gtk_widget_observe_children (GTK_WIDGET
(stack))));
+ g_object_add_weak_pointer (G_OBJECT (priv->pages), (gpointer *)&priv->pages);
+ g_object_bind_property_full (stack, "visible-child",
+ priv->pages, "selected",
+ G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE,
+ transform_to, transform_from, stack, NULL);
+
+ return priv->pages;
+}
diff --git a/gtk/gtkstack.h b/gtk/gtkstack.h
index 6379328041..658ef8bb6a 100644
--- a/gtk/gtkstack.h
+++ b/gtk/gtkstack.h
@@ -27,6 +27,7 @@
#endif
#include <gtk/gtkcontainer.h>
+#include <gtk/gtkselectionmodel.h>
G_BEGIN_DECLS
@@ -155,6 +156,10 @@ void gtk_stack_set_interpolate_size (GtkStack *stack,
gboolean interpolate_size);
GDK_AVAILABLE_IN_ALL
gboolean gtk_stack_get_interpolate_size (GtkStack *stack);
+
+GDK_AVAILABLE_IN_ALL
+GtkSelectionModel * gtk_stack_get_pages (GtkStack *stack);
+
G_END_DECLS
#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]