[gtk/stack-fixes: 4/8] stack switcher: Use the selection model
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/stack-fixes: 4/8] stack switcher: Use the selection model
- Date: Sat, 9 Feb 2019 23:18:55 +0000 (UTC)
commit eb0b1c55d826701db642f7684f3f04162a47affc
Author: Matthias Clasen <mclasen redhat com>
Date: Fri Feb 8 20:38:01 2019 -0500
stack switcher: Use the selection model
Make GtkStackSwitcher and GtkStack communicate via
the selection model that GtkStack now exposes.
gtk/gtkstackswitcher.c | 273 +++++++++++++++++--------------------------------
1 file changed, 93 insertions(+), 180 deletions(-)
---
diff --git a/gtk/gtkstackswitcher.c b/gtk/gtkstackswitcher.c
index ab9d91b27d..1be3696606 100644
--- a/gtk/gtkstackswitcher.c
+++ b/gtk/gtkstackswitcher.c
@@ -29,6 +29,7 @@
#include "gtkwidgetprivate.h"
#include "gtktypebuiltins.h"
#include "gtkimage.h"
+#include "gtkselectionmodel.h"
/**
* SECTION:gtkstackswitcher
@@ -66,6 +67,7 @@ typedef struct _GtkStackSwitcherPrivate GtkStackSwitcherPrivate;
struct _GtkStackSwitcherPrivate
{
GtkStack *stack;
+ GtkSelectionModel *pages;
GHashTable *buttons;
gboolean in_child_changed;
GtkWidget *switch_button;
@@ -89,11 +91,9 @@ gtk_stack_switcher_init (GtkStackSwitcher *switcher)
priv = gtk_stack_switcher_get_instance_private (switcher);
- priv->stack = NULL;
priv->buttons = g_hash_table_new (g_direct_hash, g_direct_equal);
context = gtk_widget_get_style_context (GTK_WIDGET (switcher));
- gtk_style_context_add_class (context, "stack-switcher");
gtk_style_context_add_class (context, GTK_STYLE_CLASS_LINKED);
gtk_orientable_set_orientation (GTK_ORIENTABLE (switcher), GTK_ORIENTATION_HORIZONTAL);
@@ -103,18 +103,17 @@ gtk_stack_switcher_init (GtkStackSwitcher *switcher)
}
static void
-on_button_clicked (GtkWidget *widget,
+on_button_clicked (GtkWidget *button,
GtkStackSwitcher *self)
{
- GtkWidget *child;
- GtkStackSwitcherPrivate *priv;
-
- priv = gtk_stack_switcher_get_instance_private (self);
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
if (!priv->in_child_changed)
{
- child = g_object_get_data (G_OBJECT (widget), "stack-child");
- gtk_stack_set_visible_child (priv->stack, child);
+ guint index;
+
+ index = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (button), "child-index"));
+ gtk_selection_model_select_item (priv->pages, index, TRUE);
}
}
@@ -164,12 +163,11 @@ update_button (GtkStackSwitcher *self,
GtkWidget *widget,
GtkWidget *button)
{
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
gchar *title;
gchar *icon_name;
gboolean needs_attention;
- GtkStackSwitcherPrivate *priv;
GtkStyleContext *context;
- priv = gtk_stack_switcher_get_instance_private (self);
g_object_get (gtk_stack_get_page (priv->stack, widget),
"title", &title,
@@ -196,72 +194,21 @@ on_visible_updated (GtkWidget *widget,
GParamSpec *pspec,
GtkStackSwitcher *self)
{
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
GtkWidget *button;
- GtkStackSwitcherPrivate *priv;
-
- priv = gtk_stack_switcher_get_instance_private (self);
-
- button = g_hash_table_lookup (priv->buttons, widget);
- update_button (self, widget, button);
-}
-
-static void
-on_title_icon_updated (GtkStackPage *page,
- GParamSpec *pspec,
- GtkStackSwitcher *self)
-{
- GtkWidget *widget;
- GtkWidget *button;
- GtkStackSwitcherPrivate *priv;
-
- priv = gtk_stack_switcher_get_instance_private (self);
- widget = gtk_stack_page_get_child (page);
button = g_hash_table_lookup (priv->buttons, widget);
update_button (self, widget, button);
}
static void
-on_position_updated (GtkStackPage *page,
- GParamSpec *pspec,
- GtkStackSwitcher *self)
-{
- GtkWidget *widget;
- GtkWidget *button;
- gint position;
- GtkStackSwitcherPrivate *priv;
-
- priv = gtk_stack_switcher_get_instance_private (self);
-
- widget = gtk_stack_page_get_child (page);
- button = g_hash_table_lookup (priv->buttons, widget);
-
- gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
- "position", &position,
- NULL);
-
- 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
-on_needs_attention_updated (GtkStackPage *page,
- GParamSpec *pspec,
- GtkStackSwitcher *self)
+on_page_updated (GtkStackPage *page,
+ GParamSpec *pspec,
+ GtkStackSwitcher *self)
{
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
GtkWidget *widget;
GtkWidget *button;
- GtkStackSwitcherPrivate *priv;
-
- priv = gtk_stack_switcher_get_instance_private (self);
widget = gtk_stack_page_get_child (page);
button = g_hash_table_lookup (priv->buttons, widget);
@@ -271,9 +218,7 @@ on_needs_attention_updated (GtkStackPage *page,
static void
remove_switch_timer (GtkStackSwitcher *self)
{
- GtkStackSwitcherPrivate *priv;
-
- priv = gtk_stack_switcher_get_instance_private (self);
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
if (priv->switch_timer)
{
@@ -286,11 +231,9 @@ static gboolean
gtk_stack_switcher_switch_timeout (gpointer data)
{
GtkStackSwitcher *self = data;
- GtkStackSwitcherPrivate *priv;
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
GtkWidget *button;
- priv = gtk_stack_switcher_get_instance_private (self);
-
priv->switch_timer = 0;
button = priv->switch_button;
@@ -309,14 +252,12 @@ gtk_stack_switcher_drag_motion (GtkWidget *widget,
gint y)
{
GtkStackSwitcher *self = GTK_STACK_SWITCHER (widget);
- GtkStackSwitcherPrivate *priv;
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
GtkWidget *button;
GHashTableIter iter;
gpointer value;
gboolean retval = FALSE;
- priv = gtk_stack_switcher_get_instance_private (self);
-
button = NULL;
g_hash_table_iter_init (&iter, priv->buttons);
while (g_hash_table_iter_next (&iter, NULL, &value))
@@ -355,160 +296,132 @@ gtk_stack_switcher_drag_leave (GtkWidget *widget,
}
static void
-add_child (GtkWidget *widget,
+add_child (guint position,
GtkStackSwitcher *self)
{
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
GtkWidget *button;
- GList *group;
- GtkStackSwitcherPrivate *priv;
+ GtkWidget *widget;
+ gboolean selected;
GtkStackPage *page;
- priv = gtk_stack_switcher_get_instance_private (self);
-
- button = gtk_radio_button_new (NULL);
-
+ button = gtk_toggle_button_new ();
gtk_widget_set_focus_on_click (button, FALSE);
- gtk_check_button_set_draw_indicator (GTK_CHECK_BUTTON (button), FALSE);
- page = gtk_stack_get_page (GTK_STACK (priv->stack), widget);
+ widget = g_list_model_get_item (G_LIST_MODEL (priv->pages), position);
update_button (self, widget, button);
- group = gtk_container_get_children (GTK_CONTAINER (self));
- if (group != NULL)
- {
- gtk_radio_button_join_group (GTK_RADIO_BUTTON (button), GTK_RADIO_BUTTON (group->data));
- g_list_free (group);
- }
-
gtk_container_add (GTK_CONTAINER (self), button);
- g_object_set_data (G_OBJECT (button), "stack-child", widget);
+ g_object_set_data (G_OBJECT (button), "child-index", GUINT_TO_POINTER (position));
+ selected = gtk_selection_model_is_selected (priv->pages, position);
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), selected);
+
+ page = gtk_stack_get_page (GTK_STACK (priv->stack), widget);
g_signal_connect (button, "clicked", G_CALLBACK (on_button_clicked), self);
g_signal_connect (widget, "notify::visible", G_CALLBACK (on_visible_updated), self);
- g_signal_connect (page, "notify::title", G_CALLBACK (on_title_icon_updated), self);
- g_signal_connect (page, "notify::icon-name", G_CALLBACK (on_title_icon_updated), self);
- g_signal_connect (page, "notify::position", G_CALLBACK (on_position_updated), self);
- g_signal_connect (page, "notify::needs-attention", G_CALLBACK (on_needs_attention_updated), self);
+ g_signal_connect (page, "notify", G_CALLBACK (on_page_updated), self);
g_hash_table_insert (priv->buttons, widget, button);
+
+ g_object_unref (widget);
}
static void
-remove_child (GtkWidget *widget,
- GtkStackSwitcher *self)
+populate_switcher (GtkStackSwitcher *self)
{
- GtkWidget *button;
- GtkStackSwitcherPrivate *priv;
-
- priv = gtk_stack_switcher_get_instance_private (self);
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
+ guint i;
- if (priv->stack)
- {
- GtkStackPage *page = gtk_stack_get_page (priv->stack, widget);
- if (page)
- {
- g_signal_handlers_disconnect_by_func (page, on_title_icon_updated, self);
- g_signal_handlers_disconnect_by_func (page, on_position_updated, self);
- g_signal_handlers_disconnect_by_func (page, on_needs_attention_updated, self);
- }
- g_signal_handlers_disconnect_by_func (widget, on_visible_updated, self);
- }
- button = g_hash_table_lookup (priv->buttons, widget);
- gtk_container_remove (GTK_CONTAINER (self), button);
- g_hash_table_remove (priv->buttons, widget);
+ for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (priv->pages)); i++)
+ add_child (i, self);
}
static void
-populate_switcher (GtkStackSwitcher *self)
+clear_switcher (GtkStackSwitcher *self)
{
- GtkStackSwitcherPrivate *priv;
- GtkWidget *widget, *button;
-
- priv = gtk_stack_switcher_get_instance_private (self);
- gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)add_child, self);
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (self);
+ GHashTableIter iter;
+ GtkWidget *widget;
+ GtkWidget *button;
- widget = gtk_stack_get_visible_child (priv->stack);
- if (widget)
+ g_hash_table_iter_init (&iter, priv->buttons);
+ while (g_hash_table_iter_next (&iter, (gpointer *)&widget, (gpointer *)&button))
{
- button = g_hash_table_lookup (priv->buttons, widget);
- priv->in_child_changed = TRUE;
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
- priv->in_child_changed = FALSE;
+ gtk_container_remove (GTK_CONTAINER (self), button);
+ g_hash_table_iter_remove (&iter);
+ if (priv->stack)
+ {
+ GtkStackPage *page = gtk_stack_get_page (priv->stack, widget);
+ if (page)
+ g_signal_handlers_disconnect_by_func (page, on_page_updated, self);
+ g_signal_handlers_disconnect_by_func (widget, on_visible_updated, self);
+ }
}
}
static void
-clear_switcher (GtkStackSwitcher *self)
+items_changed_cb (GListModel *model,
+ guint position,
+ guint removed,
+ guint added,
+ GtkStackSwitcher *switcher)
{
- GtkStackSwitcherPrivate *priv;
-
- priv = gtk_stack_switcher_get_instance_private (self);
- gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)remove_child, self);
+ clear_switcher (switcher);
+ populate_switcher (switcher);
}
static void
-on_child_changed (GtkWidget *widget,
- GParamSpec *pspec,
- GtkStackSwitcher *self)
+selection_changed_cb (GtkSelectionModel *model,
+ guint position,
+ guint n_items,
+ GtkStackSwitcher *switcher)
{
- GtkWidget *child;
- GtkWidget *button;
- GtkStackSwitcherPrivate *priv;
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (switcher);
+ guint i;
- priv = gtk_stack_switcher_get_instance_private (self);
+ priv->in_child_changed = TRUE;
- child = gtk_stack_get_visible_child (GTK_STACK (widget));
- button = g_hash_table_lookup (priv->buttons, child);
- if (button != NULL)
+ for (i = position; i < position + n_items; i++)
{
- priv->in_child_changed = TRUE;
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
- priv->in_child_changed = FALSE;
- }
-}
+ GtkWidget *child;
+ GtkWidget *button;
+ gboolean selected;
-static void
-on_stack_child_added (GtkContainer *container,
- GtkWidget *widget,
- GtkStackSwitcher *self)
-{
- add_child (widget, self);
-}
+ child = g_list_model_get_item (G_LIST_MODEL (priv->pages), i);
+ if (child == NULL)
+ continue;
-static void
-on_stack_child_removed (GtkContainer *container,
- GtkWidget *widget,
- GtkStackSwitcher *self)
-{
- remove_child (widget, self);
+ button = g_hash_table_lookup (priv->buttons, child);
+ if (button)
+ {
+ selected = gtk_selection_model_is_selected (priv->pages, i);
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), selected);
+ }
+ g_object_unref (child);
+ }
+
+ priv->in_child_changed = FALSE;
}
static void
disconnect_stack_signals (GtkStackSwitcher *switcher)
{
- GtkStackSwitcherPrivate *priv;
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (switcher);
- priv = gtk_stack_switcher_get_instance_private (switcher);
- g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_added, switcher);
- g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_removed, switcher);
- g_signal_handlers_disconnect_by_func (priv->stack, on_child_changed, switcher);
+ g_signal_handlers_disconnect_by_func (priv->pages, items_changed_cb, switcher);
+ g_signal_handlers_disconnect_by_func (priv->pages, selection_changed_cb, switcher);
g_signal_handlers_disconnect_by_func (priv->stack, disconnect_stack_signals, switcher);
}
static void
connect_stack_signals (GtkStackSwitcher *switcher)
{
- GtkStackSwitcherPrivate *priv;
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (switcher);
- priv = gtk_stack_switcher_get_instance_private (switcher);
- g_signal_connect_after (priv->stack, "add",
- G_CALLBACK (on_stack_child_added), switcher);
- g_signal_connect_after (priv->stack, "remove",
- G_CALLBACK (on_stack_child_removed), switcher);
- g_signal_connect (priv->stack, "notify::visible-child",
- G_CALLBACK (on_child_changed), switcher);
- g_signal_connect_swapped (priv->stack, "destroy",
- G_CALLBACK (disconnect_stack_signals), switcher);
+ g_signal_connect (priv->pages, "items-changed", G_CALLBACK (items_changed_cb), switcher);
+ g_signal_connect (priv->pages, "selection-changed", G_CALLBACK (selection_changed_cb), switcher);
+ g_signal_connect_swapped (priv->stack, "destroy", G_CALLBACK (disconnect_stack_signals), switcher);
}
/**
@@ -537,10 +450,12 @@ gtk_stack_switcher_set_stack (GtkStackSwitcher *switcher,
disconnect_stack_signals (switcher);
clear_switcher (switcher);
g_clear_object (&priv->stack);
+ g_clear_object (&priv->pages);
}
if (stack)
{
priv->stack = g_object_ref (stack);
+ priv->pages = gtk_stack_get_pages (stack);
populate_switcher (switcher);
connect_stack_signals (switcher);
}
@@ -627,9 +542,7 @@ static void
gtk_stack_switcher_finalize (GObject *object)
{
GtkStackSwitcher *switcher = GTK_STACK_SWITCHER (object);
- GtkStackSwitcherPrivate *priv;
-
- priv = gtk_stack_switcher_get_instance_private (switcher);
+ GtkStackSwitcherPrivate *priv = gtk_stack_switcher_get_instance_private (switcher);
g_hash_table_destroy (priv->buttons);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]