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