#include "gtktreeselection.h"
#include "gtktreeprivate.h"
#include "gtkrbtree.h"
+#include "gtkmarshalers.h"
#include "gtksignal.h"
static void gtk_tree_selection_init (GtkTreeSelection *selection);
GTK_RUN_FIRST,
GTK_CLASS_TYPE (object_class),
GTK_SIGNAL_OFFSET (GtkTreeSelectionClass, changed),
- gtk_marshal_VOID__VOID,
+ _gtk_marshal_VOID__VOID,
GTK_TYPE_NONE, 0);
}
static void
gtk_tree_selection_init (GtkTreeSelection *selection)
{
- selection->type = GTK_TREE_SELECTION_SINGLE;
+ selection->type = GTK_SELECTION_SINGLE;
}
static void
* @type: The selection mode
*
* Sets the selection mode of the @selection. If the previous type was
- * #GTK_TREE_SELECTION_MULTI and @type is #GTK_TREE_SELECTION_SINGLE, then
- * the anchor is kept selected, if it was previously selected.
+ * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
+ * previously selected.
**/
void
-gtk_tree_selection_set_mode (GtkTreeSelection *selection,
- GtkTreeSelectionMode type)
+gtk_tree_selection_set_mode (GtkTreeSelection *selection,
+ GtkSelectionMode type)
{
+ GtkTreeSelectionFunc tmp_func;
g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
if (selection->type == type)
return;
- if (type == GTK_TREE_SELECTION_SINGLE)
+
+ if (type == GTK_SELECTION_NONE)
+ {
+ gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
+ /* We do this so that we unconditionally unset all rows
+ */
+ tmp_func = selection->user_func;
+ selection->user_func = NULL;
+ gtk_tree_selection_unselect_all (selection);
+ selection->user_func = tmp_func;
+ }
+ else if (type == GTK_SELECTION_SINGLE ||
+ type == GTK_SELECTION_BROWSE)
{
GtkRBTree *tree = NULL;
GtkRBNode *node = NULL;
gint selected = FALSE;
+ GtkTreePath *anchor_path = NULL;
if (selection->tree_view->priv->anchor)
{
- GtkTreePath *anchor_path;
-
anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
if (anchor_path)
if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
selected = TRUE;
-
- gtk_tree_path_free (anchor_path);
}
}
- /* FIXME: if user_func is set, then it needs to unconditionally unselect
- * all.
+
+ /* We do this so that we unconditionally unset all rows
*/
+ tmp_func = selection->user_func;
+ selection->user_func = NULL;
gtk_tree_selection_unselect_all (selection);
+ selection->user_func = tmp_func;
- /* FIXME are we properly emitting the selection_changed signal here? */
if (node && selected)
- GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_IS_SELECTED);
+ _gtk_tree_selection_internal_select_node (selection,
+ node,
+ tree,
+ anchor_path,
+ 0);
+ if (anchor_path)
+ gtk_tree_path_free (anchor_path);
}
+
selection->type = type;
}
*
* Return value: the current selection mode
**/
-GtkTreeSelectionMode
+GtkSelectionMode
gtk_tree_selection_get_mode (GtkTreeSelection *selection)
{
- g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_TREE_SELECTION_SINGLE);
+ g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
return selection->type;
}
*
* Sets the selection function. If set, this function is called before any node
* is selected or unselected, giving some control over which nodes are selected.
+ * The select function should return %TRUE if the state of the node may be toggled,
+ * and %FALSE if the state of the node should be left unchanged.
**/
void
gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
return selection->user_data;
}
-GtkTreeView*
+/**
+ * gtk_tree_selection_get_tree_view:
+ * @selection: A #GtkTreeSelection
+ *
+ * Returns the tree view associated with @selection.
+ *
+ * Return value: A #GtkTreeView
+ **/
+GtkTreeView *
gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
{
g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
* @iter: The #GtkTreeIter, or NULL.
*
* Sets @iter to the currently selected node if @selection is set to
- * #GTK_TREE_SELECTION_SINGLE. @iter may be NULL if you just want to test if
- * @selection has any selected nodes. @model is filled with the current model
- * as a convenience. This function will not work if you use @selection is
- * #GTK_TREE_SELECTION_MULTI.
+ * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
+ * just want to test if @selection has any selected nodes. @model is filled
+ * with the current model as a convenience. This function will not work if you
+ * use @selection is #GTK_SELECTION_MULTIPLE.
*
* Return value: TRUE, if there is a selected node.
**/
gboolean retval;
g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
- g_return_val_if_fail (selection->type == GTK_TREE_SELECTION_SINGLE, 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);
selection->tree_view->priv->tree->root == NULL)
return;
- if (selection->type == GTK_TREE_SELECTION_SINGLE)
+ if (selection->type == GTK_SELECTION_SINGLE ||
+ selection->type == GTK_SELECTION_BROWSE)
{
if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
{
if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
return;
- if (selection->type == GTK_TREE_SELECTION_MULTI)
+ if (selection->type == GTK_SELECTION_MULTIPLE)
state = GDK_CONTROL_MASK;
_gtk_tree_selection_internal_select_node (selection,
gtk_tree_path_free (path);
}
+/**
+ * gtk_tree_selection_path_is_selected:
+ * @selection: A #GtkTreeSelection.
+ * @path: A #GtkTreePath to check selection on.
+ *
+ * Returns %TRUE if the row pointed to by @path is currently selected. If @path
+ * does not point to a valid location, %FALSE is returned
+ *
+ * Return value: %TRUE if @path is selected.
+ **/
+gboolean
+gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
+ GtkTreePath *path)
+{
+ GtkRBNode *node;
+ GtkRBTree *tree;
+
+ 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);
+
+ _gtk_tree_view_find_node (selection->tree_view,
+ path,
+ &tree,
+ &node);
+
+ if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
+ return FALSE;
+
+ return TRUE;
+}
+
+/**
+ * gtk_tree_selection_iter_is_selected:
+ * @selection: A #GtkTreeSelection
+ * @iter: A valid #GtkTreeIter
+ *
+ * Returns %TRUE if the row pointed to by @path is currently selected.
+ *
+ * Return value: %TRUE, if @iter is selected
+ **/
+gboolean
+gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
+ GtkTreeIter *iter)
+{
+ GtkTreePath *path;
+ gboolean retval;
+
+ g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
+ g_return_val_if_fail (iter != NULL, FALSE);
+ g_return_val_if_fail (selection->tree_view != NULL, FALSE);
+ g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
+
+ path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
+ if (path == NULL)
+ return FALSE;
+
+ retval = gtk_tree_selection_path_is_selected (selection, path);
+ gtk_tree_path_free (path);
+
+ return retval;
+}
+
+
/* Wish I was in python, right now... */
struct _TempTuple {
GtkTreeSelection *selection;
* gtk_tree_selection_select_all:
* @selection: A #GtkTreeSelection.
*
- * Selects all the nodes. @selection is must be set to
- * #GTK_TREE_SELECTION_MULTI mode.
+ * Selects all the nodes. @selection is must be set to #GTK_SELECTION_MULTIPLE
+ * mode.
**/
void
gtk_tree_selection_select_all (GtkTreeSelection *selection)
g_return_if_fail (selection->tree_view != NULL);
if (selection->tree_view->priv->tree == NULL)
return;
- g_return_if_fail (selection->type != GTK_TREE_SELECTION_MULTI);
+ 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);
{
struct _TempTuple *tuple;
- if (selection->type == GTK_TREE_SELECTION_SINGLE)
+ if (selection->type == GTK_SELECTION_SINGLE ||
+ selection->type == GTK_SELECTION_BROWSE)
{
GtkRBTree *tree = NULL;
GtkRBNode *node = NULL;
gint dirty = FALSE;
GtkTreePath *anchor_path = NULL;
+ if (selection->type == GTK_SELECTION_NONE)
+ return;
if (selection->tree_view->priv->anchor)
anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
- if (selection->type == GTK_TREE_SELECTION_SINGLE)
+ if (selection->type == GTK_SELECTION_SINGLE ||
+ selection->type == GTK_SELECTION_BROWSE)
{
/* Did we try to select the same node again? */
- if (anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
+ if (selection->type == GTK_SELECTION_SINGLE &&
+ anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
{
if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
{
}
}
}
- else if (selection->type == GTK_TREE_SELECTION_MULTI)
+ else if (selection->type == GTK_SELECTION_MULTIPLE)
{
if (((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK) && (anchor_path == NULL))
{
/* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
*/
-/* FIXME: user_func can screw up GTK_TREE_SELECTION_SINGLE. If it prevents
- * unselection of a node, it can keep more then one node selected.
- */
-/* Perhaps the correct solution is to prevent selecting the new node, if
- * we fail to unselect the old node.
- */
static gint
gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
GtkRBTree *tree,
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, selection->user_data))
+ 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