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