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