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