* Boston, MA 02111-1307, USA.
*/
+#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);
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)
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),
if (selection->destroy)
{
- GtkDestroyNotify d = selection->destroy;
+ GDestroyNotify d = selection->destroy;
selection->destroy = NULL;
d (selection->user_data);
}
/* chain parent_class' handler */
- G_OBJECT_CLASS (parent_class)->finalize (object);
+ G_OBJECT_CLASS (gtk_tree_selection_parent_class)->finalize (object);
}
/**
node,
tree,
anchor_path,
- 0,
+ 0,
FALSE);
if (anchor_path)
gtk_tree_path_free (anchor_path);
gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
GtkTreeSelectionFunc func,
gpointer data,
- GtkDestroyNotify destroy)
+ GDestroyNotify destroy)
{
g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
g_return_if_fail (func != NULL);
if (selection->destroy)
{
- GtkDestroyNotify d = selection->destroy;
+ GDestroyNotify d = selection->destroy;
selection->destroy = NULL;
d (selection->user_data);
selection->destroy = destroy;
}
+/**
+ * gtk_tree_selection_get_select_function:
+ * @selection: A #GtkTreeSelection.
+ *
+ * Returns the current selection function.
+ *
+ * Return value: The function.
+ *
+ * Since: GSEAL-branch
+ **/
+GtkTreeSelectionFunc
+gtk_tree_selection_get_select_function (GtkTreeSelection *selection)
+{
+ g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
+
+ return selection->user_func;
+}
+
/**
* gtk_tree_selection_get_user_data:
* @selection: A #GtkTreeSelection.
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;
* 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 do this, you can use gtk_tree_row_reference_new().
*
* 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.
*
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 (model)
+ *model = selection->tree_view->priv->model;
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)
do
{
if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
- list = g_list_append (list, gtk_tree_path_copy (path));
+ list = g_list_prepend (list, gtk_tree_path_copy (path));
if (node->children)
{
if (!tree)
{
gtk_tree_path_free (path);
- return list;
+
+ goto done;
}
gtk_tree_path_up (path);
gtk_tree_path_free (path);
- return list;
+ done:
+ return g_list_reverse (list);
}
static void
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;
}
- 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,
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 ||
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 */
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);
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");
}
/**
{
GtkRBNode *node;
GtkRBTree *tree;
- GdkModifierType state = 0;
gboolean ret;
+ GtkTreeSelectMode mode = 0;
g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
g_return_if_fail (selection->tree_view != NULL);
return;
if (selection->type == GTK_SELECTION_MULTIPLE)
- state = GDK_CONTROL_MASK;
+ mode = GTK_TREE_SELECT_MODE_TOGGLE;
_gtk_tree_selection_internal_select_node (selection,
node,
tree,
path,
- state,
+ mode,
FALSE);
}
node,
tree,
path,
- GDK_CONTROL_MASK,
+ GTK_TREE_SELECT_MODE_TOGGLE,
TRUE);
}
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,
* gtk_tree_selection_select_all:
* @selection: A #GtkTreeSelection.
*
- * Selects all the nodes. @selection is must be set to #GTK_SELECTION_MULTIPLE
+ * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
* mode.
**/
void
{
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))
{
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);
}
* @end_path: The final node of the range.
*
* Selects a range of nodes, determined by @start_path and @end_path inclusive.
+ * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
**/
void
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);
{
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);
}
+gboolean
+_gtk_tree_selection_row_is_selectable (GtkTreeSelection *selection,
+ GtkRBNode *node,
+ GtkTreePath *path)
+{
+ GtkTreeIter iter;
+ gboolean sensitive = FALSE;
+
+ if (!gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path))
+ sensitive = TRUE;
+
+ if (!sensitive && selection->tree_view->priv->row_separator_func)
+ {
+ /* never allow separators to be selected */
+ if ((* selection->tree_view->priv->row_separator_func) (selection->tree_view->priv->model,
+ &iter,
+ selection->tree_view->priv->row_separator_data))
+ 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),
+ selection->user_data);
+ else
+ return TRUE;
+}
+
+
/* Called internally by gtktreeview.c It handles actually selecting the tree.
*/
GtkRBNode *node,
GtkRBTree *tree,
GtkTreePath *path,
- GdkModifierType state,
+ GtkTreeSelectMode mode,
gboolean override_browse_mode)
{
gint flags;
else if (selection->type == GTK_SELECTION_SINGLE &&
anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
{
- if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
+ if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
{
dirty = gtk_tree_selection_real_unselect_all (selection);
}
{
/* We only want to select the new node if we can unselect the old one,
* and we can select the new one. */
- if (selection->user_func)
- {
- if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
- GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
- selection->user_data))
- dirty = TRUE;
- }
- else
- {
- dirty = TRUE;
- }
+ 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
}
else if (selection->type == GTK_SELECTION_MULTIPLE)
{
- if (((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK) && (anchor_path == NULL))
+ if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
+ && (anchor_path == NULL))
{
if (selection->tree_view->priv->anchor)
gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
}
- else if ((state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_SHIFT_MASK|GDK_CONTROL_MASK))
+ else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
{
gtk_tree_selection_select_range (selection,
anchor_path,
path);
}
- else if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
+ else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
{
flags = node->flags;
if (selection->tree_view->priv->anchor)
else
dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
}
- else if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
+ else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
{
dirty = gtk_tree_selection_real_unselect_all (selection);
dirty |= gtk_tree_selection_real_modify_range (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!
GtkRBNode *node,
gboolean select)
{
- gboolean selected = FALSE;
+ gboolean toggle = FALSE;
GtkTreePath *path = NULL;
select = !! select;
if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
{
path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
- if (selection->user_func)
- {
- if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
- GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
- selection->user_data))
- selected = TRUE;
- }
- else
- selected = TRUE;
+ 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;
return FALSE;
}
+#define __GTK_TREE_SELECTION_C__
+#include "gtkaliasdef.c"