[gtk/stack-fixes: 6/13] Fix problems with the no-selection state



commit 9deb7f131835a111617ffe82c8920b8ced285cd5
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Feb 8 19:01:11 2019 -0500

    Fix problems with the no-selection state
    
    It turns out that GtkSingleSelection will sometimes have
    a selected value of 0, even when there is no child at that
    position (because the model is empty). Therefore, comparing
    the selected value to GTK_INVALID_LIST_POSITION is not
    sufficient. Use n_items <= selected instead.

 gtk/gtkstack.c         | 8 ++++----
 gtk/gtkstackswitcher.c | 4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)
---
diff --git a/gtk/gtkstack.c b/gtk/gtkstack.c
index 4c1a2471f6..99b6bd103c 100644
--- a/gtk/gtkstack.c
+++ b/gtk/gtkstack.c
@@ -2249,15 +2249,15 @@ transform_from (GBinding *binding,
   guint selected;
 
   selected = g_value_get_uint (from_value);
-  if (selected == GTK_INVALID_LIST_POSITION)
-    g_value_set_object (to_value, NULL);
-  else
+  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);
+      return TRUE;
     }
-  return TRUE;
+  else
+    return FALSE;
 }
 
 /**
diff --git a/gtk/gtkstackswitcher.c b/gtk/gtkstackswitcher.c
index 93bb85171b..0e37da9a7c 100644
--- a/gtk/gtkstackswitcher.c
+++ b/gtk/gtkstackswitcher.c
@@ -362,7 +362,7 @@ populate_switcher (GtkStackSwitcher *self)
     }
 
   selected = gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (priv->pages));
-  if (selected != GTK_INVALID_LIST_POSITION)
+  if (selected < g_list_model_get_n_items (G_LIST_MODEL (priv->pages)))
     {
       widget = g_list_model_get_item (G_LIST_MODEL (priv->pages), selected);
       button = g_hash_table_lookup (priv->buttons, widget);
@@ -418,7 +418,7 @@ selection_changed_cb (GtkSelectionModel *model,
   GtkWidget *button;
 
   selected = gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (model));
-  if (selected == GTK_INVALID_LIST_POSITION)
+  if (g_list_model_get_n_items (G_LIST_MODEL (model)) <= selected)
     return;
 
   child = g_list_model_get_item (G_LIST_MODEL (model), selected);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]