]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktreeselection.c
Display an error when we come to the root.
[~andy/gtk] / gtk / gtktreeselection.c
index b8ac48b8b8c20f105307734456f675f0db6b0963..e7aad5bff7de32c6ca01bbc240438405c076790f 100644 (file)
  */
 
 #include <config.h>
+#include <string.h>
 #include "gtktreeselection.h"
 #include "gtktreeprivate.h"
 #include "gtkrbtree.h"
 #include "gtkmarshalers.h"
-
-static void gtk_tree_selection_init              (GtkTreeSelection      *selection);
-static void gtk_tree_selection_class_init        (GtkTreeSelectionClass *class);
+#include "gtkintl.h"
+#include "gtkalias.h"
 
 static void gtk_tree_selection_finalize          (GObject               *object);
 static gint gtk_tree_selection_real_select_all   (GtkTreeSelection      *selection);
@@ -40,36 +40,9 @@ enum
   LAST_SIGNAL
 };
 
-static GObjectClass *parent_class = NULL;
 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
 
-GType
-gtk_tree_selection_get_type (void)
-{
-  static GType selection_type = 0;
-
-  if (!selection_type)
-    {
-      static const GTypeInfo selection_info =
-      {
-        sizeof (GtkTreeSelectionClass),
-       NULL,           /* base_init */
-       NULL,           /* base_finalize */
-        (GClassInitFunc) gtk_tree_selection_class_init,
-       NULL,           /* class_finalize */
-       NULL,           /* class_data */
-        sizeof (GtkTreeSelection),
-       0,              /* n_preallocs */
-        (GInstanceInitFunc) gtk_tree_selection_init
-      };
-
-      selection_type =
-       g_type_register_static (G_TYPE_OBJECT, "GtkTreeSelection",
-                               &selection_info, 0);
-    }
-
-  return selection_type;
-}
+G_DEFINE_TYPE (GtkTreeSelection, gtk_tree_selection, G_TYPE_OBJECT)
 
 static void
 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
@@ -77,13 +50,12 @@ gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
   GObjectClass *object_class;
 
   object_class = (GObjectClass*) class;
-  parent_class = g_type_class_peek_parent (class);
 
   object_class->finalize = gtk_tree_selection_finalize;
   class->changed = NULL;
 
   tree_selection_signals[CHANGED] =
-    g_signal_new ("changed",
+    g_signal_new (I_("changed"),
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
@@ -112,7 +84,7 @@ gtk_tree_selection_finalize (GObject *object)
     }
 
   /* chain parent_class' handler */
-  G_OBJECT_CLASS (parent_class)->finalize (object);
+  G_OBJECT_CLASS (gtk_tree_selection_parent_class)->finalize (object);
 }
 
 /**
@@ -362,7 +334,10 @@ gtk_tree_selection_get_selected (GtkTreeSelection  *selection,
   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
   g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
   g_return_val_if_fail (selection->tree_view != NULL, FALSE);
-  g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
+
+  /* Clear the iter */
+  if (iter)
+    memset (iter, 0, sizeof (GtkTreeIter));
 
   if (model)
     *model = selection->tree_view->priv->model;
@@ -437,7 +412,6 @@ gtk_tree_selection_get_selected_rows (GtkTreeSelection   *selection,
 
   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)
@@ -555,12 +529,9 @@ 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)
@@ -575,9 +546,6 @@ gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
        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,
@@ -615,13 +583,13 @@ gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
   GtkRBTree *tree;
   GtkRBNode *node;
   GtkTreeIter iter;
+  GtkTreeModel *model;
 
-  guint inserted_id, deleted_id, reordered_id;
-  gboolean stop = FALSE, has_next = TRUE, has_parent = TRUE;
+  gulong inserted_id, deleted_id, reordered_id, changed_id;
+  gboolean stop = 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);
 
   if (func == NULL ||
       selection->tree_view->priv->tree == NULL ||
@@ -647,75 +615,64 @@ gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
   while (node->left != tree->nil)
     node = node->left;
 
+  model = selection->tree_view->priv->model;
+  g_object_ref (model);
+
   /* connect to signals to monitor changes in treemodel */
-  inserted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
-                                          "row_inserted",
+  inserted_id = g_signal_connect_swapped (model, "row_inserted",
                                          G_CALLBACK (model_changed),
                                          &stop);
-  deleted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
-                                         "row_deleted",
+  deleted_id = g_signal_connect_swapped (model, "row_deleted",
                                         G_CALLBACK (model_changed),
                                         &stop);
-  reordered_id = g_signal_connect_swapped (selection->tree_view->priv->model,
-                                           "rows_reordered",
+  reordered_id = g_signal_connect_swapped (model, "rows_reordered",
                                           G_CALLBACK (model_changed),
                                           &stop);
+  changed_id = g_signal_connect_swapped (selection->tree_view, "notify::model",
+                                        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,
-                          &iter, path);
 
   do
     {
       if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
-       (* func) (selection->tree_view->priv->model, path, &iter, data);
+        {
+          gtk_tree_model_get_iter (model, &iter, path);
+         (* func) (model, path, &iter, data);
+        }
 
       if (stop)
        goto out;
 
       if (node->children)
        {
-         gboolean has_child;
-         GtkTreeIter tmp;
-
          tree = node->children;
          node = tree->root;
+
          while (node->left != tree->nil)
            node = node->left;
-         tmp = iter;
-         has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
-         gtk_tree_path_append_index (path, 0);
 
-         /* we do the sanity check at the bottom of this function */
-         if (!has_child)
-           goto out;
+         gtk_tree_path_append_index (path, 0);
        }
       else
        {
          gboolean done = FALSE;
+
          do
            {
              node = _gtk_rbtree_next (tree, node);
              if (node != NULL)
                {
-                 gboolean has_next;
-
-                 has_next = gtk_tree_model_iter_next (selection->tree_view->priv->model, &iter);
                  done = TRUE;
                  gtk_tree_path_next (path);
-
-                 /* we do the sanity check at the bottom of this function */
-                 if (!has_next)
-                   goto out;
                }
              else
                {
-                 gboolean has_parent;
-                 GtkTreeIter tmp_iter = iter;
-
                  node = tree->parent_node;
                  tree = tree->parent_tree;
+
                  if (tree == NULL)
                    {
                      /* we've run out of tree */
@@ -724,12 +681,7 @@ gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
                      goto out;
                    }
 
-                 has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
                  gtk_tree_path_up (path);
-
-                 /* we do the sanity check at the bottom of this function */
-                 if (!has_parent)
-                   goto out;
                }
            }
          while (!done);
@@ -741,24 +693,18 @@ 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);
+  g_signal_handler_disconnect (model, inserted_id);
+  g_signal_handler_disconnect (model, deleted_id);
+  g_signal_handler_disconnect (model, reordered_id);
+  g_signal_handler_disconnect (selection->tree_view, changed_id);
+  g_object_unref (model);
 
   /* 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_selected_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");
+    g_warning ("The model has been modified from within gtk_tree_selection_selected_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");
 }
 
 /**
@@ -915,7 +861,9 @@ gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
   g_return_val_if_fail (path != NULL, FALSE);
   g_return_val_if_fail (selection->tree_view != NULL, FALSE);
-  g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
+
+  if (selection->tree_view->priv->model == NULL)
+    return FALSE;
 
   ret = _gtk_tree_view_find_node (selection->tree_view,
                                  path,
@@ -1029,8 +977,10 @@ gtk_tree_selection_select_all (GtkTreeSelection *selection)
 {
   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
   g_return_if_fail (selection->tree_view != NULL);
-  if (selection->tree_view->priv->tree == NULL)
+
+  if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
     return;
+
   g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
 
   if (gtk_tree_selection_real_select_all (selection))
@@ -1130,12 +1080,10 @@ gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
 {
   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
   g_return_if_fail (selection->tree_view != NULL);
-  if (selection->tree_view->priv->tree == NULL)
+
+  if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
     return;
   
-  if (selection->tree_view->priv->tree == NULL)
-    return;
-
   if (gtk_tree_selection_real_unselect_all (selection))
     g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
 }
@@ -1253,6 +1201,7 @@ gtk_tree_selection_select_range (GtkTreeSelection *selection,
   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
   g_return_if_fail (selection->tree_view != NULL);
   g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
+  g_return_if_fail (selection->tree_view->priv->model != NULL);
 
   if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
     g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
@@ -1276,49 +1225,17 @@ gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
 {
   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);
 
   if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
     g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
 }
 
-static gboolean
-tree_column_is_sensitive (GtkTreeViewColumn *column,
-                         GtkTreeModel      *model,
-                         GtkTreeIter       *iter)
-{
-  GList *cells, *list;
-  gboolean sensitive;
-  gboolean visible;
-
-  gtk_tree_view_column_cell_set_cell_data (column, model,
-                                          iter, FALSE, FALSE);
-
-  cells = gtk_tree_view_column_get_cell_renderers (column);
-
-  list = cells;
-  while (list)
-    {
-      g_object_get (G_OBJECT (list->data), 
-                   "sensitive", &sensitive, 
-                   "visible", &visible,
-                   NULL);
-      
-      if (visible && sensitive)
-       break;
-
-      list = list->next;
-    }
-  g_list_free (cells);
-
-  return sensitive;
-}
-
-static gboolean
-row_is_selectable (GtkTreeSelection *selection,
-                  GtkRBNode        *node,
-                  GtkTreePath      *path)
+gboolean
+_gtk_tree_selection_row_is_selectable (GtkTreeSelection *selection,
+                                      GtkRBNode        *node,
+                                      GtkTreePath      *path)
 {
-  GList *list;
   GtkTreeIter iter;
   gboolean sensitive = FALSE;
 
@@ -1334,19 +1251,6 @@ row_is_selectable (GtkTreeSelection *selection,
        return FALSE;
     }
 
-  for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
-    {
-      GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
-
-      if (!column->visible)
-       continue;
-
-      sensitive = tree_column_is_sensitive (column, selection->tree_view->priv->model, &iter);
-    }
-
-  if (!sensitive)
-    return FALSE;
-
   if (selection->user_func)
     return (*selection->user_func) (selection, selection->tree_view->priv->model, path,
                                    GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
@@ -1405,7 +1309,7 @@ _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
            {
              /* We only want to select the new node if we can unselect the old one,
               * and we can select the new one. */
-             dirty = row_is_selectable (selection, node, path);
+             dirty = _gtk_tree_selection_row_is_selectable (selection, node, path);
 
              /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
               * unselect the new one
@@ -1502,7 +1406,14 @@ _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
     gtk_tree_path_free (anchor_path);
 
   if (dirty)
-    g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
+    g_signal_emit (selection, tree_selection_signals[CHANGED], 0);  
+}
+
+
+void 
+_gtk_tree_selection_emit_changed (GtkTreeSelection *selection)
+{
+  g_signal_emit (selection, tree_selection_signals[CHANGED], 0);  
 }
 
 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
@@ -1514,7 +1425,7 @@ gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
                                     GtkRBNode        *node,
                                     gboolean          select)
 {
-  gboolean selected = FALSE;
+  gboolean toggle = FALSE;
   GtkTreePath *path = NULL;
 
   select = !! select;
@@ -1522,11 +1433,11 @@ gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
   if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
     {
       path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
-      selected = row_is_selectable (selection, node, path);
+      toggle = _gtk_tree_selection_row_is_selectable (selection, node, path);
       gtk_tree_path_free (path);
     }
 
-  if (selected == TRUE)
+  if (toggle)
     {
       node->flags ^= GTK_RBNODE_IS_SELECTED;
 
@@ -1538,3 +1449,5 @@ gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
   return FALSE;
 }
 
+#define __GTK_TREE_SELECTION_C__
+#include "gtkaliasdef.c"