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