1 /* gtktreednd.c
2 * Copyright (C) 2001 Red Hat, Inc.
3 *
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.
8 *
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.
13 *
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.
18 */
19
20 #include "config.h"
21 #include <string.h>
22 #include "gtktreednd.h"
23 #include "gtkintl.h"
24 #include "gtkalias.h"
25
26 GType
gtk_tree_drag_source_get_type(void)27 gtk_tree_drag_source_get_type (void)
28 {
29 static GType our_type = 0;
30
31 if (!our_type)
32 {
33 const GTypeInfo our_info =
34 {
35 sizeof (GtkTreeDragSourceIface), /* class_size */
36 NULL, /* base_init */
37 NULL, /* base_finalize */
38 NULL,
39 NULL, /* class_finalize */
40 NULL, /* class_data */
41 0,
42 0, /* n_preallocs */
43 NULL
44 };
45
46 our_type = g_type_register_static (G_TYPE_INTERFACE,
47 I_("GtkTreeDragSource"),
48 &our_info, 0);
49 }
50
51 return our_type;
52 }
53
54
55 GType
gtk_tree_drag_dest_get_type(void)56 gtk_tree_drag_dest_get_type (void)
57 {
58 static GType our_type = 0;
59
60 if (!our_type)
61 {
62 const GTypeInfo our_info =
63 {
64 sizeof (GtkTreeDragDestIface), /* class_size */
65 NULL, /* base_init */
66 NULL, /* base_finalize */
67 NULL,
68 NULL, /* class_finalize */
69 NULL, /* class_data */
70 0,
71 0, /* n_preallocs */
72 NULL
73 };
74
75 our_type = g_type_register_static (G_TYPE_INTERFACE, I_("GtkTreeDragDest"), &our_info, 0);
76 }
77
78 return our_type;
79 }
80
81 /**
82 * gtk_tree_drag_source_row_draggable:
83 * @drag_source: a #GtkTreeDragSource
84 * @path: row on which user is initiating a drag
85 *
86 * Asks the #GtkTreeDragSource whether a particular row can be used as
87 * the source of a DND operation. If the source doesn't implement
88 * this interface, the row is assumed draggable.
89 *
90 * Return value: %TRUE if the row can be dragged
91 **/
92 gboolean
gtk_tree_drag_source_row_draggable(GtkTreeDragSource * drag_source,GtkTreePath * path)93 gtk_tree_drag_source_row_draggable (GtkTreeDragSource *drag_source,
94 GtkTreePath *path)
95 {
96 GtkTreeDragSourceIface *iface = GTK_TREE_DRAG_SOURCE_GET_IFACE (drag_source);
97
98 g_return_val_if_fail (path != NULL, FALSE);
99
100 if (iface->row_draggable)
101 return (* iface->row_draggable) (drag_source, path);
102 else
103 return TRUE;
104 /* Returning TRUE if row_draggable is not implemented is a fallback.
105 Interface implementations such as GtkTreeStore and GtkListStore really should
106 implement row_draggable. */
107 }
108
109
110 /**
111 * gtk_tree_drag_source_drag_data_delete:
112 * @drag_source: a #GtkTreeDragSource
113 * @path: row that was being dragged
114 *
115 * Asks the #GtkTreeDragSource to delete the row at @path, because
116 * it was moved somewhere else via drag-and-drop. Returns %FALSE
117 * if the deletion fails because @path no longer exists, or for
118 * some model-specific reason. Should robustly handle a @path no
119 * longer found in the model!
120 *
121 * Return value: %TRUE if the row was successfully deleted
122 **/
123 gboolean
gtk_tree_drag_source_drag_data_delete(GtkTreeDragSource * drag_source,GtkTreePath * path)124 gtk_tree_drag_source_drag_data_delete (GtkTreeDragSource *drag_source,
125 GtkTreePath *path)
126 {
127 GtkTreeDragSourceIface *iface = GTK_TREE_DRAG_SOURCE_GET_IFACE (drag_source);
128
129 g_return_val_if_fail (iface->drag_data_delete != NULL, FALSE);
130 g_return_val_if_fail (path != NULL, FALSE);
131
132 return (* iface->drag_data_delete) (drag_source, path);
133 }
134
135 /**
136 * gtk_tree_drag_source_drag_data_get:
137 * @drag_source: a #GtkTreeDragSource
138 * @path: row that was dragged
139 * @selection_data: (out): a #GtkSelectionData to fill with data
140 * from the dragged row
141 *
142 * Asks the #GtkTreeDragSource to fill in @selection_data with a
143 * representation of the row at @path. @selection_data->target gives
144 * the required type of the data. Should robustly handle a @path no
145 * longer found in the model!
146 *
147 * Return value: %TRUE if data of the required type was provided
148 **/
149 gboolean
gtk_tree_drag_source_drag_data_get(GtkTreeDragSource * drag_source,GtkTreePath * path,GtkSelectionData * selection_data)150 gtk_tree_drag_source_drag_data_get (GtkTreeDragSource *drag_source,
151 GtkTreePath *path,
152 GtkSelectionData *selection_data)
153 {
154 GtkTreeDragSourceIface *iface = GTK_TREE_DRAG_SOURCE_GET_IFACE (drag_source);
155
156 g_return_val_if_fail (iface->drag_data_get != NULL, FALSE);
157 g_return_val_if_fail (path != NULL, FALSE);
158 g_return_val_if_fail (selection_data != NULL, FALSE);
159
160 return (* iface->drag_data_get) (drag_source, path, selection_data);
161 }
162
163 /**
164 * gtk_tree_drag_dest_drag_data_received:
165 * @drag_dest: a #GtkTreeDragDest
166 * @dest: row to drop in front of
167 * @selection_data: data to drop
168 *
169 * Asks the #GtkTreeDragDest to insert a row before the path @dest,
170 * deriving the contents of the row from @selection_data. If @dest is
171 * outside the tree so that inserting before it is impossible, %FALSE
172 * will be returned. Also, %FALSE may be returned if the new row is
173 * not created for some model-specific reason. Should robustly handle
174 * a @dest no longer found in the model!
175 *
176 * Return value: whether a new row was created before position @dest
177 **/
178 gboolean
gtk_tree_drag_dest_drag_data_received(GtkTreeDragDest * drag_dest,GtkTreePath * dest,GtkSelectionData * selection_data)179 gtk_tree_drag_dest_drag_data_received (GtkTreeDragDest *drag_dest,
180 GtkTreePath *dest,
181 GtkSelectionData *selection_data)
182 {
183 GtkTreeDragDestIface *iface = GTK_TREE_DRAG_DEST_GET_IFACE (drag_dest);
184
185 g_return_val_if_fail (iface->drag_data_received != NULL, FALSE);
186 g_return_val_if_fail (dest != NULL, FALSE);
187 g_return_val_if_fail (selection_data != NULL, FALSE);
188
189 return (* iface->drag_data_received) (drag_dest, dest, selection_data);
190 }
191
192
193 /**
194 * gtk_tree_drag_dest_row_drop_possible:
195 * @drag_dest: a #GtkTreeDragDest
196 * @dest_path: destination row
197 * @selection_data: the data being dragged
198 *
199 * Determines whether a drop is possible before the given @dest_path,
200 * at the same depth as @dest_path. i.e., can we drop the data in
201 * @selection_data at that location. @dest_path does not have to
202 * exist; the return value will almost certainly be %FALSE if the
203 * parent of @dest_path doesn't exist, though.
204 *
205 * Return value: %TRUE if a drop is possible before @dest_path
206 **/
207 gboolean
gtk_tree_drag_dest_row_drop_possible(GtkTreeDragDest * drag_dest,GtkTreePath * dest_path,GtkSelectionData * selection_data)208 gtk_tree_drag_dest_row_drop_possible (GtkTreeDragDest *drag_dest,
209 GtkTreePath *dest_path,
210 GtkSelectionData *selection_data)
211 {
212 GtkTreeDragDestIface *iface = GTK_TREE_DRAG_DEST_GET_IFACE (drag_dest);
213
214 g_return_val_if_fail (iface->row_drop_possible != NULL, FALSE);
215 g_return_val_if_fail (selection_data != NULL, FALSE);
216 g_return_val_if_fail (dest_path != NULL, FALSE);
217
218 return (* iface->row_drop_possible) (drag_dest, dest_path, selection_data);
219 }
220
221 typedef struct _TreeRowData TreeRowData;
222
223 struct _TreeRowData
224 {
225 GtkTreeModel *model;
226 gchar path[4];
227 };
228
229 /**
230 * gtk_tree_set_row_drag_data:
231 * @selection_data: some #GtkSelectionData
232 * @tree_model: a #GtkTreeModel
233 * @path: a row in @tree_model
234 *
235 * Sets selection data of target type %GTK_TREE_MODEL_ROW. Normally used
236 * in a drag_data_get handler.
237 *
238 * Return value: %TRUE if the #GtkSelectionData had the proper target type to allow us to set a tree row
239 **/
240 gboolean
gtk_tree_set_row_drag_data(GtkSelectionData * selection_data,GtkTreeModel * tree_model,GtkTreePath * path)241 gtk_tree_set_row_drag_data (GtkSelectionData *selection_data,
242 GtkTreeModel *tree_model,
243 GtkTreePath *path)
244 {
245 TreeRowData *trd;
246 gchar *path_str;
247 gint len;
248 gint struct_size;
249
250 g_return_val_if_fail (selection_data != NULL, FALSE);
251 g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
252 g_return_val_if_fail (path != NULL, FALSE);
253
254 if (selection_data->target != gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
255 return FALSE;
256
257 path_str = gtk_tree_path_to_string (path);
258
259 len = strlen (path_str);
260
261 /* the old allocate-end-of-struct-to-hold-string trick */
262 struct_size = sizeof (TreeRowData) + len + 1 -
263 (sizeof (TreeRowData) - G_STRUCT_OFFSET (TreeRowData, path));
264
265 trd = g_malloc (struct_size);
266
267 strcpy (trd->path, path_str);
268
269 g_free (path_str);
270
271 trd->model = tree_model;
272
273 gtk_selection_data_set (selection_data,
274 gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"),
275 8, /* bytes */
276 (void*)trd,
277 struct_size);
278
279 g_free (trd);
280
281 return TRUE;
282 }
283
284 /**
285 * gtk_tree_get_row_drag_data:
286 * @selection_data: a #GtkSelectionData
287 * @tree_model: (out): a #GtkTreeModel
288 * @path: (out): row in @tree_model
289 *
290 * Obtains a @tree_model and @path from selection data of target type
291 * %GTK_TREE_MODEL_ROW. Normally called from a drag_data_received handler.
292 * This function can only be used if @selection_data originates from the same
293 * process that's calling this function, because a pointer to the tree model
294 * is being passed around. If you aren't in the same process, then you'll
295 * get memory corruption. In the #GtkTreeDragDest drag_data_received handler,
296 * you can assume that selection data of type %GTK_TREE_MODEL_ROW is
297 * in from the current process. The returned path must be freed with
298 * gtk_tree_path_free().
299 *
300 * Return value: %TRUE if @selection_data had target type %GTK_TREE_MODEL_ROW and
301 * is otherwise valid
302 **/
303 gboolean
gtk_tree_get_row_drag_data(GtkSelectionData * selection_data,GtkTreeModel ** tree_model,GtkTreePath ** path)304 gtk_tree_get_row_drag_data (GtkSelectionData *selection_data,
305 GtkTreeModel **tree_model,
306 GtkTreePath **path)
307 {
308 TreeRowData *trd;
309
310 g_return_val_if_fail (selection_data != NULL, FALSE);
311
312 if (tree_model)
313 *tree_model = NULL;
314
315 if (path)
316 *path = NULL;
317
318 if (selection_data->target != gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
319 return FALSE;
320
321 if (selection_data->length < 0)
322 return FALSE;
323
324 trd = (void*) selection_data->data;
325
326 if (tree_model)
327 *tree_model = trd->model;
328
329 if (path)
330 *path = gtk_tree_path_new_from_string (trd->path);
331
332 return TRUE;
333 }
334
335 #define __GTK_TREE_DND_C__
336 #include "gtkaliasdef.c"
337