]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktreeselection.c
small doc fixes.
[~andy/gtk] / gtk / gtktreeselection.c
index 7236376f171d4f2459a1468d098c2ccc8834fe46..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;
 }
@@ -334,7 +336,7 @@ gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
 /**
  * gtk_tree_selection_get_selected:
  * @selection: A #GtkTreeSelection.
- * @model: A pointer set to the #GtkTreeModel, or NULL.
+ * @model: A pointer to set to the #GtkTreeModel, or NULL.
  * @iter: The #GtkTreeIter, or NULL.
  *
  * Sets @iter to the currently selected node if @selection is set to
@@ -403,13 +405,199 @@ gtk_tree_selection_get_selected (GtkTreeSelection  *selection,
   return retval;
 }
 
+/**
+ * gtk_tree_selection_get_selected_rows:
+ * @selection: A #GtkTreeSelection.
+ * @model: A pointer to set to the #GtkTreeModel, or NULL.
+ *
+ * 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 #GtkTreeRowReference<!-- -->s.
+ * To do this, you can use gtk_tree_row_reference_new_proxy().
+ *
+ * To free the return value, use:
+ * <informalexample><programlisting>
+ * g_list_foreach (list, gtk_tree_path_free, NULL);
+ * g_list_free (list);
+ * </programlisting></informalexample>
+ *
+ * Return value: A #GList containing a #GtkTreePath for each selected row.
+ **/
+GList *
+gtk_tree_selection_get_selected_rows (GtkTreeSelection   *selection,
+                                      GtkTreeModel      **model)
+{
+  GList *list = NULL;
+  GtkRBTree *tree = NULL;
+  GtkRBNode *node = NULL;
+  GtkTreePath *path;
+
+  g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
+  g_return_val_if_fail (selection->tree_view != NULL, NULL);
+  g_return_val_if_fail (selection->tree_view->priv->model != NULL, NULL);
+
+  if (selection->tree_view->priv->tree == NULL ||
+      selection->tree_view->priv->tree->root == NULL)
+    return NULL;
+
+  if (model)
+    *model = selection->tree_view->priv->model;
+
+  if (selection->type == GTK_SELECTION_NONE)
+    return NULL;
+  else if (selection->type != GTK_SELECTION_MULTIPLE)
+    {
+      GtkTreeIter iter;
+
+      if (gtk_tree_selection_get_selected (selection, NULL, &iter))
+        {
+         GtkTreePath *path;
+
+         path = gtk_tree_model_get_path (*model, &iter);
+         list = g_list_append (list, path);
+
+         return list;
+       }
+
+      return NULL;
+    }
+
+  tree = selection->tree_view->priv->tree;
+  node = selection->tree_view->priv->tree->root;
+
+  while (node->left != tree->nil)
+    node = node->left;
+  path = gtk_tree_path_new_first ();
+
+  do
+    {
+      if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
+       list = g_list_append (list, gtk_tree_path_copy (path));
+
+      if (node->children)
+        {
+         tree = node->children;
+         node = tree->root;
+
+         while (node->left != tree->nil)
+           node = node->left;
+
+         gtk_tree_path_append_index (path, 0);
+       }
+      else
+        {
+         gboolean done = FALSE;
+
+         do
+           {
+             node = _gtk_rbtree_next (tree, node);
+             if (node != NULL)
+               {
+                 done = TRUE;
+                 gtk_tree_path_next (path);
+               }
+             else
+               {
+                 node = tree->parent_node;
+                 tree = tree->parent_tree;
+
+                 if (!tree)
+                   {
+                     gtk_tree_path_free (path);
+                     return list;
+                   }
+
+                 gtk_tree_path_up (path);
+               }
+           }
+         while (!done);
+       }
+    }
+  while (TRUE);
+
+  gtk_tree_path_free (path);
+
+  return list;
+}
+
+static void
+gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
+                                              GtkRBNode *node,
+                                              gpointer   data)
+{
+  gint *count = (gint *)data;
+
+  if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
+    (*count)++;
+
+  if (node->children)
+    _gtk_rbtree_traverse (node->children, node->children->root,
+                         G_PRE_ORDER,
+                         gtk_tree_selection_count_selected_rows_helper, data);
+}
+
+/**
+ * gtk_tree_selection_count_selected_rows:
+ * @selection: A #GtkTreeSelection.
+ *
+ * Returns the number of rows that have been selected in @tree.
+ *
+ * Return value: The number of rows selected.
+ **/
+gint
+gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
+{
+  gint count = 0;
+  GtkRBTree *tree;
+  GtkRBNode *node;
+
+  g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
+  g_return_val_if_fail (selection->tree_view != NULL, 0);
+  g_return_val_if_fail (selection->tree_view->priv->model != NULL, 0);
+
+  if (selection->tree_view->priv->tree == NULL ||
+      selection->tree_view->priv->tree->root == NULL)
+    return 0;
+
+  if (selection->type == GTK_SELECTION_SINGLE ||
+      selection->type == GTK_SELECTION_BROWSE)
+    {
+      if (gtk_tree_selection_get_selected (selection, NULL, NULL))
+       return 1;
+      else
+       return 0;
+    }
+
+  tree = selection->tree_view->priv->tree;
+  node = selection->tree_view->priv->tree->root;
+
+  _gtk_rbtree_traverse (selection->tree_view->priv->tree,
+                        selection->tree_view->priv->tree->root,
+                       G_PRE_ORDER,
+                       gtk_tree_selection_count_selected_rows_helper,
+                       &count);
+
+  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.
  * @func: The function to call for each selected node.
  * @data: user data to pass to the function.
  *
- * Calls a function for each selected node.
+ * Calls a function for each selected node. Note that you cannot modify
+ * the tree or selection from within this function. As a result,
+ * gtk_tree_selection_get_selected_rows() might be more useful.
  **/
 void
 gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
@@ -421,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);
@@ -449,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,
@@ -458,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;
@@ -470,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
        {
@@ -499,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");
 }
 
 /**
@@ -689,7 +926,7 @@ gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
  * @selection: A #GtkTreeSelection
  * @iter: A valid #GtkTreeIter
  * 
- * Returns %TRUE if the row pointed to by @path is currently selected.
+ * Returns %TRUE if the row at @iter is currently selected.
  * 
  * Return value: %TRUE, if @iter is selected
  **/
@@ -789,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
@@ -892,11 +1129,18 @@ 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
+{
+  RANGE_SELECT,
+  RANGE_UNSELECT
+};
+
 static gint
-gtk_tree_selection_real_select_range (GtkTreeSelection *selection,
+gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
+                                      gint              mode,
                                      GtkTreePath      *start_path,
                                      GtkTreePath      *end_path)
 {
@@ -941,7 +1185,7 @@ gtk_tree_selection_real_select_range (GtkTreeSelection *selection,
 
   do
     {
-      dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, TRUE);
+      dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
 
       if (start_node == end_node)
        break;
@@ -985,14 +1229,35 @@ gtk_tree_selection_select_range (GtkTreeSelection *selection,
   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
   g_return_if_fail (selection->tree_view != NULL);
 
-  if (gtk_tree_selection_real_select_range (selection, start_path, end_path))
-    g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
+  if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
+    g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
+}
+
+/**
+ * gtk_tree_selection_unselect_range:
+ * @selection: A #GtkTreeSelection.
+ * @start_path: The initial node of the range.
+ * @end_path: The initial node of the range.
+ *
+ * Unselects a range of nodes, determined by @start_path and @end_path
+ * inclusive.
+ **/
+void
+gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
+                                   GtkTreePath      *start_path,
+                                  GtkTreePath      *end_path)
+{
+  g_return_if_fail (GTK_IS_TREE_SELECTION (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 (selection, tree_selection_signals[CHANGED], 0);
 }
 
 /* Called internally by gtktreeview.c It handles actually selecting the tree.
  */
 
-/**
+/*
  * docs about the 'override_browse_mode', we set this flag when we want to
  * unset select the node and override the select browse mode behaviour (that is
  * 'one node should *always* be selected').
@@ -1074,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);
                }
@@ -1114,7 +1382,8 @@ _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
       else if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
        {
          dirty = gtk_tree_selection_real_unselect_all (selection);
-         dirty |= gtk_tree_selection_real_select_range (selection,
+         dirty |= gtk_tree_selection_real_modify_range (selection,
+                                                         RANGE_SELECT,
                                                         anchor_path,
                                                         path);
        }
@@ -1136,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!