]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktreeselection.c
small doc fixes.
[~andy/gtk] / gtk / gtktreeselection.c
index 88df17bcbac6f92491885b6db4e304ee757c548a..b6b6adac80ab192405b10fa77132d1b9f9643363 100644 (file)
@@ -21,7 +21,6 @@
 #include "gtktreeprivate.h"
 #include "gtkrbtree.h"
 #include "gtkmarshalers.h"
-#include "gtksignal.h"
 
 static void gtk_tree_selection_init              (GtkTreeSelection      *selection);
 static void gtk_tree_selection_class_init        (GtkTreeSelectionClass *class);
@@ -43,10 +42,10 @@ enum
 static GObjectClass *parent_class = NULL;
 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
 
-GtkType
+GType
 gtk_tree_selection_get_type (void)
 {
-  static GtkType selection_type = 0;
+  static GType selection_type = 0;
 
   if (!selection_type)
     {
@@ -63,7 +62,9 @@ gtk_tree_selection_get_type (void)
         (GInstanceInitFunc) gtk_tree_selection_init
       };
 
-      selection_type = g_type_register_static (G_TYPE_OBJECT, "GtkTreeSelection", &selection_info, 0);
+      selection_type =
+       g_type_register_static (G_TYPE_OBJECT, "GtkTreeSelection",
+                               &selection_info, 0);
     }
 
   return selection_type;
@@ -81,12 +82,13 @@ gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
   class->changed = NULL;
 
   tree_selection_signals[CHANGED] =
-    gtk_signal_new ("changed",
-                   GTK_RUN_FIRST,
-                   GTK_CLASS_TYPE (object_class),
-                   GTK_SIGNAL_OFFSET (GtkTreeSelectionClass, changed),
-                   _gtk_marshal_VOID__VOID,
-                   GTK_TYPE_NONE, 0);
+    g_signal_new ("changed",
+                 G_OBJECT_CLASS_TYPE (object_class),
+                 G_SIGNAL_RUN_FIRST,
+                 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
+                 NULL, NULL,
+                 _gtk_marshal_VOID__VOID,
+                 G_TYPE_NONE, 0);
 }
 
 static void
@@ -125,7 +127,7 @@ _gtk_tree_selection_new (void)
 {
   GtkTreeSelection *selection;
 
-  selection = GTK_TREE_SELECTION (g_object_new (GTK_TYPE_TREE_SELECTION, NULL));
+  selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
 
   return selection;
 }
@@ -410,7 +412,7 @@ gtk_tree_selection_get_selected (GtkTreeSelection  *selection,
  *
  * Creates a list of path of all selected rows. Additionally, if you are
  * planning on modifying the model after calling this function, you may
- * want to convert the returned list into a list of #GtkTreeRowReferences.
+ * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
  * To do this, you can use gtk_tree_row_reference_new_proxy().
  *
  * To free the return value, use:
@@ -578,6 +580,15 @@ gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
   return count;
 }
 
+/* gtk_tree_selection_selected_foreach helper */
+static void
+model_changed (gpointer data)
+{
+  gboolean *stop = (gboolean *)data;
+
+  *stop = TRUE;
+}
+
 /**
  * gtk_tree_selection_selected_foreach:
  * @selection: A #GtkTreeSelection.
@@ -598,6 +609,9 @@ gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
   GtkRBNode *node;
   GtkTreeIter iter;
 
+  guint inserted_id, deleted_id, reordered_id;
+  gboolean stop = FALSE, has_next = FALSE, has_parent = FALSE;
+
   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
   g_return_if_fail (selection->tree_view != NULL);
   g_return_if_fail (selection->tree_view->priv->model != NULL);
@@ -626,6 +640,20 @@ gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
   while (node->left != tree->nil)
     node = node->left;
 
+  /* connect to signals to monitor changes in treemodel */
+  inserted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
+                                          "row_inserted",
+                                         G_CALLBACK (model_changed),
+                                         &stop);
+  deleted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
+                                         "row_deleted",
+                                        G_CALLBACK (model_changed),
+                                        &stop);
+  reordered_id = g_signal_connect_swapped (selection->tree_view->priv->model,
+                                           "rows_reordered",
+                                          G_CALLBACK (model_changed),
+                                          &stop);
+
   /* find the node internally */
   path = gtk_tree_path_new_first ();
   gtk_tree_model_get_iter (selection->tree_view->priv->model,
@@ -635,6 +663,10 @@ gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
     {
       if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
        (* func) (selection->tree_view->priv->model, path, &iter, data);
+
+      if (stop)
+       goto out;
+
       if (node->children)
        {
          gboolean has_child;
@@ -647,8 +679,10 @@ gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
          tmp = iter;
          has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
          gtk_tree_path_append_index (path, 0);
-         /* Sanity Check! */
-         TREE_VIEW_INTERNAL_ASSERT_VOID (has_child);
+
+         /* we do the sanity check at the bottom of this function */
+         if (has_child)
+           goto out;
        }
       else
        {
@@ -676,21 +710,47 @@ gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
                  tree = tree->parent_tree;
                  if (tree == NULL)
                    {
-                     gtk_tree_path_free (path);
                      /* we've run out of tree */
                      /* We're done with this function */
-                     return;
+
+                     goto out;
                    }
+
                  has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
                  gtk_tree_path_up (path);
-                 /* Sanity check */
-                 TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
+
+                 /* we do the sanity check at the bottom of this function */
+                 if (has_parent)
+                   goto out;
                }
            }
          while (!done);
        }
     }
   while (TRUE);
+
+out:
+  if (path)
+    gtk_tree_path_free (path);
+
+  g_signal_handler_disconnect (selection->tree_view->priv->model,
+                               inserted_id);
+  g_signal_handler_disconnect (selection->tree_view->priv->model,
+                               deleted_id);
+  g_signal_handler_disconnect (selection->tree_view->priv->model,
+                               reordered_id);
+
+  /* check if we have to spew a scary message */
+  if (has_next)
+    TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
+  if (has_parent)
+    TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
+  if (stop)
+    g_warning
+      ("The model has been modified from within gtk_tree_selection_foreach.\n"
+       "This function is for observing the selections of the tree only.  If\n"
+       "you are trying to get all selected items from the tree, try using\n"
+       "gtk_tree_selection_get_selected_rows instead.\n");
 }
 
 /**
@@ -966,7 +1026,7 @@ gtk_tree_selection_select_all (GtkTreeSelection *selection)
   g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
 
   if (gtk_tree_selection_real_select_all (selection))
-    g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
+    g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
 }
 
 static void
@@ -1069,7 +1129,7 @@ gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
     return;
 
   if (gtk_tree_selection_real_unselect_all (selection))
-    g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
+    g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
 }
 
 enum
@@ -1170,7 +1230,7 @@ gtk_tree_selection_select_range (GtkTreeSelection *selection,
   g_return_if_fail (selection->tree_view != NULL);
 
   if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
-    g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
+    g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
 }
 
 /**
@@ -1191,7 +1251,7 @@ gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
   g_return_if_fail (selection->tree_view != NULL);
 
   if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
-    g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
+    g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
 }
 
 /* Called internally by gtktreeview.c It handles actually selecting the tree.
@@ -1279,6 +1339,9 @@ _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
              if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
                {
                  dirty = TRUE;
+                 if (selection->tree_view->priv->anchor)
+                   gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
+
                  selection->tree_view->priv->anchor =
                    gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
                }
@@ -1342,7 +1405,7 @@ _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
     gtk_tree_path_free (anchor_path);
 
   if (dirty)
-    g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
+    g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
 }
 
 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!