2 * Copyright (C) 2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtktreednd.h"
24 gtk_tree_drag_source_get_type (void)
26 static GType our_type = 0;
30 static const GTypeInfo our_info =
32 sizeof (GtkTreeDragSourceIface), /* class_size */
34 NULL, /* base_finalize */
36 NULL, /* class_finalize */
37 NULL, /* class_data */
43 our_type = g_type_register_static (G_TYPE_INTERFACE, "GtkTreeDragSource",
52 gtk_tree_drag_dest_get_type (void)
54 static GType our_type = 0;
58 static const GTypeInfo our_info =
60 sizeof (GtkTreeDragDestIface), /* class_size */
62 NULL, /* base_finalize */
64 NULL, /* class_finalize */
65 NULL, /* class_data */
71 our_type = g_type_register_static (G_TYPE_INTERFACE, "GtkTreeDragDest", &our_info, 0);
78 * gtk_tree_drag_source_row_draggable:
79 * @drag_source: a #GtkTreeDragSource
80 * @path: row on which user is initiating a drag
82 * Asks the #GtkTreeDragSource whether a particular row can be used as
83 * the source of a DND operation. If the source doesn't implement
84 * this interface, the row is assumed draggable.
86 * Return value: %TRUE if the row can be dragged
89 gtk_tree_drag_source_row_draggable (GtkTreeDragSource *drag_source,
92 GtkTreeDragSourceIface *iface = GTK_TREE_DRAG_SOURCE_GET_IFACE (drag_source);
94 g_return_val_if_fail (path != NULL, FALSE);
96 if (iface->row_draggable)
97 return (* iface->row_draggable) (drag_source, path);
104 * gtk_tree_drag_source_drag_data_delete:
105 * @drag_source: a #GtkTreeDragSource
106 * @path: row that was being dragged
108 * Asks the #GtkTreeDragSource to delete the row at @path, because
109 * it was moved somewhere else via drag-and-drop. Returns %FALSE
110 * if the deletion fails because @path no longer exists, or for
111 * some model-specific reason. Should robustly handle a @path no
112 * longer found in the model!
114 * Return value: %TRUE if the row was successfully deleted
117 gtk_tree_drag_source_drag_data_delete (GtkTreeDragSource *drag_source,
120 GtkTreeDragSourceIface *iface = GTK_TREE_DRAG_SOURCE_GET_IFACE (drag_source);
122 g_return_val_if_fail (iface->drag_data_delete != NULL, FALSE);
123 g_return_val_if_fail (path != NULL, FALSE);
125 return (* iface->drag_data_delete) (drag_source, path);
129 * gtk_tree_drag_source_drag_data_get:
130 * @drag_source: a #GtkTreeDragSource
131 * @path: row that was dragged
132 * @selection_data: a #GtkSelectionData to fill with data from the dragged row
134 * Asks the #GtkTreeDragSource to fill in @selection_data with a
135 * representation of the row at @path. @selection_data->target gives
136 * the required type of the data. Should robustly handle a @path no
137 * longer found in the model!
139 * Return value: %TRUE if data of the required type was provided
142 gtk_tree_drag_source_drag_data_get (GtkTreeDragSource *drag_source,
144 GtkSelectionData *selection_data)
146 GtkTreeDragSourceIface *iface = GTK_TREE_DRAG_SOURCE_GET_IFACE (drag_source);
148 g_return_val_if_fail (iface->drag_data_get != NULL, FALSE);
149 g_return_val_if_fail (path != NULL, FALSE);
150 g_return_val_if_fail (selection_data != NULL, FALSE);
152 return (* iface->drag_data_get) (drag_source, path, selection_data);
156 * gtk_tree_drag_dest_drag_data_received:
157 * @drag_dest: a #GtkTreeDragDest
158 * @dest: row to drop in front of
159 * @selection_data: data to drop
161 * Asks the #GtkTreeDragDest to insert a row before the path @dest,
162 * deriving the contents of the row from @selection_data. If @dest is
163 * outside the tree so that inserting before it is impossible, %FALSE
164 * will be returned. Also, %FALSE may be returned if the new row is
165 * not created for some model-specific reason. Should robustly handle
166 * a @dest no longer found in the model!
168 * Return value: whether a new row was created before position @dest
171 gtk_tree_drag_dest_drag_data_received (GtkTreeDragDest *drag_dest,
173 GtkSelectionData *selection_data)
175 GtkTreeDragDestIface *iface = GTK_TREE_DRAG_DEST_GET_IFACE (drag_dest);
177 g_return_val_if_fail (iface->drag_data_received != NULL, FALSE);
178 g_return_val_if_fail (dest != NULL, FALSE);
179 g_return_val_if_fail (selection_data != NULL, FALSE);
181 return (* iface->drag_data_received) (drag_dest, dest, selection_data);
186 * gtk_tree_drag_dest_drop_possible:
187 * @drag_dest: a #GtkTreeDragDest
188 * @dest_path: destination row
189 * @selection_data: the data being dragged
191 * Determines whether a drop is possible before the given @dest_path,
192 * at the same depth as @dest_path. i.e., can we drop the data in
193 * @selection_data at that location. @dest_path does not have to
194 * exist; the return value will almost certainly be %FALSE if the
195 * parent of @dest_path doesn't exist, though.
197 * Return value: %TRUE if a drop is possible before @dest_path
200 gtk_tree_drag_dest_row_drop_possible (GtkTreeDragDest *drag_dest,
201 GtkTreePath *dest_path,
202 GtkSelectionData *selection_data)
204 GtkTreeDragDestIface *iface = GTK_TREE_DRAG_DEST_GET_IFACE (drag_dest);
206 g_return_val_if_fail (iface->row_drop_possible != NULL, FALSE);
207 g_return_val_if_fail (selection_data != NULL, FALSE);
208 g_return_val_if_fail (dest_path != NULL, FALSE);
210 return (* iface->row_drop_possible) (drag_dest, dest_path, selection_data);
213 typedef struct _TreeRowData TreeRowData;
222 * gtk_tree_set_row_drag_data:
223 * @selection_data: some #GtkSelectionData
224 * @tree_model: a #GtkTreeModel
225 * @path: a row in @tree_model
227 * Sets selection data of target type %GTK_TREE_MODEL_ROW. Normally used
228 * in a drag_data_get handler.
230 * Return value: %TRUE if the #GtkSelectionData had the proper target type to allow us to set a tree row
233 gtk_tree_set_row_drag_data (GtkSelectionData *selection_data,
234 GtkTreeModel *tree_model,
242 g_return_val_if_fail (selection_data != NULL, FALSE);
243 g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
244 g_return_val_if_fail (path != NULL, FALSE);
246 if (selection_data->target != gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
249 path_str = gtk_tree_path_to_string (path);
251 len = strlen (path_str);
253 /* the old allocate-end-of-struct-to-hold-string trick */
254 struct_size = sizeof (TreeRowData) + len + 1 -
255 (sizeof (TreeRowData) - G_STRUCT_OFFSET (TreeRowData, path));
257 trd = g_malloc (struct_size);
259 strcpy (trd->path, path_str);
263 trd->model = tree_model;
265 gtk_selection_data_set (selection_data,
266 gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE),
277 * gtk_tree_get_row_drag_data:
278 * @selection_data: a #GtkSelectionData
279 * @tree_model: a #GtkTreeModel
280 * @path: row in @tree_model
282 * Obtains a @tree_model and @path from selection data of target type
283 * %GTK_TREE_MODEL_ROW. Normally called from a drag_data_received handler.
284 * This function can only be used if @selection_data originates from the same
285 * process that's calling this function, because a pointer to the tree model
286 * is being passed around. If you aren't in the same process, then you'll
287 * get memory corruption. In the #GtkTreeDragDest drag_data_received handler,
288 * you can assume that selection data of type %GTK_TREE_MODEL_ROW is
289 * in from the current process. The returned path must be freed with
290 * gtk_tree_path_free().
292 * Return value: %TRUE if @selection_data had target type %GTK_TREE_MODEL_ROW and
296 gtk_tree_get_row_drag_data (GtkSelectionData *selection_data,
297 GtkTreeModel **tree_model,
302 g_return_val_if_fail (selection_data != NULL, FALSE);
310 if (selection_data->target != gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
313 trd = (void*) selection_data->data;
316 *tree_model = trd->model;
319 *path = gtk_tree_path_new_from_string (trd->path);