gtktreednd.c 11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* gtktreednd.c
 * Copyright (C) 2001  Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
#include "config.h"
21 22
#include <string.h>
#include "gtktreednd.h"
23
#include "gtkintl.h"
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

/**
 * SECTION:gtktreednd
 * @Short_description: Interfaces for drag-and-drop support in GtkTreeView
 * @Title: GtkTreeView drag-and-drop
 *
 * GTK+ supports Drag-and-Drop in tree views with a high-level and a low-level
 * API.
 *
 * The low-level API consists of the GTK+ DND API, augmented by some treeview
 * utility functions: gtk_tree_view_set_drag_dest_row(),
 * gtk_tree_view_get_drag_dest_row(), gtk_tree_view_get_dest_row_at_pos(),
 * gtk_tree_view_create_row_drag_icon(), gtk_tree_set_row_drag_data() and
 * gtk_tree_get_row_drag_data(). This API leaves a lot of flexibility, but
 * nothing is done automatically, and implementing advanced features like
 * hover-to-open-rows or autoscrolling on top of this API is a lot of work.
 *
 * On the other hand, if you write to the high-level API, then all the
 * bookkeeping of rows is done for you, as well as things like hover-to-open
 * and auto-scroll, but your models have to implement the
 * #GtkTreeDragSource and #GtkTreeDragDest interfaces.
 */


49 50 51 52 53 54 55
GType
gtk_tree_drag_source_get_type (void)
{
  static GType our_type = 0;

  if (!our_type)
    {
56
      const GTypeInfo our_info =
57 58 59 60 61 62 63 64 65 66 67 68
      {
        sizeof (GtkTreeDragSourceIface), /* class_size */
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	NULL,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	0,
	0,              /* n_preallocs */
	NULL
      };

Matthias Clasen's avatar
Matthias Clasen committed
69 70
      our_type = g_type_register_static (G_TYPE_INTERFACE, 
					 I_("GtkTreeDragSource"),
Manish Singh's avatar
Manish Singh committed
71
					 &our_info, 0);
72 73 74 75 76 77 78 79 80 81 82 83 84
    }
  
  return our_type;
}


GType
gtk_tree_drag_dest_get_type (void)
{
  static GType our_type = 0;

  if (!our_type)
    {
85
      const GTypeInfo our_info =
86 87 88 89 90 91 92 93 94 95 96 97
      {
        sizeof (GtkTreeDragDestIface), /* class_size */
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	NULL,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	0,
	0,              /* n_preallocs */
	NULL
      };

98
      our_type = g_type_register_static (G_TYPE_INTERFACE, I_("GtkTreeDragDest"), &our_info, 0);
99 100 101 102 103
    }
  
  return our_type;
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/**
 * gtk_tree_drag_source_row_draggable:
 * @drag_source: a #GtkTreeDragSource
 * @path: row on which user is initiating a drag
 * 
 * Asks the #GtkTreeDragSource whether a particular row can be used as
 * the source of a DND operation. If the source doesn't implement
 * this interface, the row is assumed draggable.
 *
 * Return value: %TRUE if the row can be dragged
 **/
gboolean
gtk_tree_drag_source_row_draggable (GtkTreeDragSource *drag_source,
                                    GtkTreePath       *path)
{
  GtkTreeDragSourceIface *iface = GTK_TREE_DRAG_SOURCE_GET_IFACE (drag_source);

  g_return_val_if_fail (path != NULL, FALSE);

  if (iface->row_draggable)
    return (* iface->row_draggable) (drag_source, path);
  else
    return TRUE;
Murray Cumming's avatar
Murray Cumming committed
127 128 129
    /* Returning TRUE if row_draggable is not implemented is a fallback.
       Interface implementations such as GtkTreeStore and GtkListStore really should
       implement row_draggable. */
130 131
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

/**
 * gtk_tree_drag_source_drag_data_delete:
 * @drag_source: a #GtkTreeDragSource
 * @path: row that was being dragged
 * 
 * Asks the #GtkTreeDragSource to delete the row at @path, because
 * it was moved somewhere else via drag-and-drop. Returns %FALSE
 * if the deletion fails because @path no longer exists, or for
 * some model-specific reason. Should robustly handle a @path no
 * longer found in the model!
 * 
 * Return value: %TRUE if the row was successfully deleted
 **/
gboolean
gtk_tree_drag_source_drag_data_delete (GtkTreeDragSource *drag_source,
                                       GtkTreePath       *path)
{
  GtkTreeDragSourceIface *iface = GTK_TREE_DRAG_SOURCE_GET_IFACE (drag_source);

  g_return_val_if_fail (iface->drag_data_delete != NULL, FALSE);
  g_return_val_if_fail (path != NULL, FALSE);

  return (* iface->drag_data_delete) (drag_source, path);
}

/**
 * gtk_tree_drag_source_drag_data_get:
 * @drag_source: a #GtkTreeDragSource
 * @path: row that was dragged
162 163
 * @selection_data: (out): a #GtkSelectionData to fill with data
 *                  from the dragged row
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
 * 
 * Asks the #GtkTreeDragSource to fill in @selection_data with a
 * representation of the row at @path. @selection_data->target gives
 * the required type of the data.  Should robustly handle a @path no
 * longer found in the model!
 * 
 * Return value: %TRUE if data of the required type was provided 
 **/
gboolean
gtk_tree_drag_source_drag_data_get    (GtkTreeDragSource *drag_source,
                                       GtkTreePath       *path,
                                       GtkSelectionData  *selection_data)
{
  GtkTreeDragSourceIface *iface = GTK_TREE_DRAG_SOURCE_GET_IFACE (drag_source);

  g_return_val_if_fail (iface->drag_data_get != NULL, FALSE);
  g_return_val_if_fail (path != NULL, FALSE);
  g_return_val_if_fail (selection_data != NULL, FALSE);

  return (* iface->drag_data_get) (drag_source, path, selection_data);
}

/**
 * gtk_tree_drag_dest_drag_data_received:
 * @drag_dest: a #GtkTreeDragDest
 * @dest: row to drop in front of
 * @selection_data: data to drop
 * 
 * Asks the #GtkTreeDragDest to insert a row before the path @dest,
 * deriving the contents of the row from @selection_data. If @dest is
 * outside the tree so that inserting before it is impossible, %FALSE
 * will be returned. Also, %FALSE may be returned if the new row is
 * not created for some model-specific reason.  Should robustly handle
 * a @dest no longer found in the model!
 * 
 * Return value: whether a new row was created before position @dest
 **/
gboolean
gtk_tree_drag_dest_drag_data_received (GtkTreeDragDest  *drag_dest,
                                       GtkTreePath      *dest,
                                       GtkSelectionData *selection_data)
{
  GtkTreeDragDestIface *iface = GTK_TREE_DRAG_DEST_GET_IFACE (drag_dest);

  g_return_val_if_fail (iface->drag_data_received != NULL, FALSE);
  g_return_val_if_fail (dest != NULL, FALSE);
  g_return_val_if_fail (selection_data != NULL, FALSE);

  return (* iface->drag_data_received) (drag_dest, dest, selection_data);
}

215 216

/**
217
 * gtk_tree_drag_dest_row_drop_possible:
218 219
 * @drag_dest: a #GtkTreeDragDest
 * @dest_path: destination row
220
 * @selection_data: the data being dragged
221 222
 * 
 * Determines whether a drop is possible before the given @dest_path,
223 224
 * at the same depth as @dest_path. i.e., can we drop the data in
 * @selection_data at that location. @dest_path does not have to
225 226 227 228 229 230 231
 * exist; the return value will almost certainly be %FALSE if the
 * parent of @dest_path doesn't exist, though.
 * 
 * Return value: %TRUE if a drop is possible before @dest_path
 **/
gboolean
gtk_tree_drag_dest_row_drop_possible (GtkTreeDragDest   *drag_dest,
232 233
                                      GtkTreePath       *dest_path,
				      GtkSelectionData  *selection_data)
234 235 236 237
{
  GtkTreeDragDestIface *iface = GTK_TREE_DRAG_DEST_GET_IFACE (drag_dest);

  g_return_val_if_fail (iface->row_drop_possible != NULL, FALSE);
238
  g_return_val_if_fail (selection_data != NULL, FALSE);
239 240
  g_return_val_if_fail (dest_path != NULL, FALSE);

241
  return (* iface->row_drop_possible) (drag_dest, dest_path, selection_data);
242 243
}

244 245 246 247 248 249 250 251 252
typedef struct _TreeRowData TreeRowData;

struct _TreeRowData
{
  GtkTreeModel *model;
  gchar path[4];
};

/**
253
 * gtk_tree_set_row_drag_data:
254 255 256 257 258 259 260 261 262 263
 * @selection_data: some #GtkSelectionData
 * @tree_model: a #GtkTreeModel
 * @path: a row in @tree_model
 * 
 * Sets selection data of target type %GTK_TREE_MODEL_ROW. Normally used
 * in a drag_data_get handler.
 * 
 * Return value: %TRUE if the #GtkSelectionData had the proper target type to allow us to set a tree row
 **/
gboolean
264 265 266
gtk_tree_set_row_drag_data (GtkSelectionData *selection_data,
			    GtkTreeModel     *tree_model,
			    GtkTreePath      *path)
267 268 269 270 271 272 273 274 275 276
{
  TreeRowData *trd;
  gchar *path_str;
  gint len;
  gint struct_size;
  
  g_return_val_if_fail (selection_data != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
  g_return_val_if_fail (path != NULL, FALSE);

277
  if (gtk_selection_data_get_target (selection_data) != gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
278 279 280 281 282 283 284 285 286 287 288 289 290
    return FALSE;
  
  path_str = gtk_tree_path_to_string (path);

  len = strlen (path_str);

  /* the old allocate-end-of-struct-to-hold-string trick */
  struct_size = sizeof (TreeRowData) + len + 1 -
    (sizeof (TreeRowData) - G_STRUCT_OFFSET (TreeRowData, path));

  trd = g_malloc (struct_size); 

  strcpy (trd->path, path_str);
Jonathan Blandford's avatar
Jonathan Blandford committed
291 292

  g_free (path_str);
293 294 295 296
  
  trd->model = tree_model;
  
  gtk_selection_data_set (selection_data,
297
                          gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"),
298 299 300 301 302 303 304 305 306 307
                          8, /* bytes */
                          (void*)trd,
                          struct_size);

  g_free (trd);
  
  return TRUE;
}

/**
308
 * gtk_tree_get_row_drag_data:
309
 * @selection_data: a #GtkSelectionData
310 311
 * @tree_model: (out): a #GtkTreeModel
 * @path: (out): row in @tree_model
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
 * 
 * Obtains a @tree_model and @path from selection data of target type
 * %GTK_TREE_MODEL_ROW. Normally called from a drag_data_received handler.
 * This function can only be used if @selection_data originates from the same
 * process that's calling this function, because a pointer to the tree model
 * is being passed around. If you aren't in the same process, then you'll
 * get memory corruption. In the #GtkTreeDragDest drag_data_received handler,
 * you can assume that selection data of type %GTK_TREE_MODEL_ROW is
 * in from the current process. The returned path must be freed with
 * gtk_tree_path_free().
 * 
 * Return value: %TRUE if @selection_data had target type %GTK_TREE_MODEL_ROW and
 *  is otherwise valid
 **/
gboolean
327 328 329
gtk_tree_get_row_drag_data (GtkSelectionData  *selection_data,
			    GtkTreeModel     **tree_model,
			    GtkTreePath      **path)
330 331 332 333 334 335 336 337 338 339
{
  TreeRowData *trd;
  
  g_return_val_if_fail (selection_data != NULL, FALSE);  

  if (tree_model)
    *tree_model = NULL;

  if (path)
    *path = NULL;
340 341

  if (gtk_selection_data_get_target (selection_data) != gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
342 343
    return FALSE;

344
  if (gtk_selection_data_get_length (selection_data) < 0)
345 346
    return FALSE;

347
  trd = (void*) gtk_selection_data_get_data (selection_data);
348 349 350 351 352 353 354 355 356

  if (tree_model)
    *tree_model = trd->model;

  if (path)
    *path = gtk_tree_path_new_from_string (trd->path);
  
  return TRUE;
}