]> Pileus Git - ~andy/gtk/blob - gtk/gtktreednd.c
adapt to handle PangoColor
[~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 /**
78  * gtk_tree_drag_source_drag_data_delete:
79  * @drag_source: a #GtkTreeDragSource
80  * @path: row that was being dragged
81  * 
82  * Asks the #GtkTreeDragSource to delete the row at @path, because
83  * it was moved somewhere else via drag-and-drop. Returns %FALSE
84  * if the deletion fails because @path no longer exists, or for
85  * some model-specific reason. Should robustly handle a @path no
86  * longer found in the model!
87  * 
88  * Return value: %TRUE if the row was successfully deleted
89  **/
90 gboolean
91 gtk_tree_drag_source_drag_data_delete (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 (iface->drag_data_delete != NULL, FALSE);
97   g_return_val_if_fail (path != NULL, FALSE);
98
99   return (* iface->drag_data_delete) (drag_source, path);
100 }
101
102 /**
103  * gtk_tree_drag_source_drag_data_get:
104  * @drag_source: a #GtkTreeDragSource
105  * @path: row that was dragged
106  * @selection_data: a #GtkSelectionData to fill with data from the dragged row
107  * 
108  * Asks the #GtkTreeDragSource to fill in @selection_data with a
109  * representation of the row at @path. @selection_data->target gives
110  * the required type of the data.  Should robustly handle a @path no
111  * longer found in the model!
112  * 
113  * Return value: %TRUE if data of the required type was provided 
114  **/
115 gboolean
116 gtk_tree_drag_source_drag_data_get    (GtkTreeDragSource *drag_source,
117                                        GtkTreePath       *path,
118                                        GtkSelectionData  *selection_data)
119 {
120   GtkTreeDragSourceIface *iface = GTK_TREE_DRAG_SOURCE_GET_IFACE (drag_source);
121
122   g_return_val_if_fail (iface->drag_data_get != NULL, FALSE);
123   g_return_val_if_fail (path != NULL, FALSE);
124   g_return_val_if_fail (selection_data != NULL, FALSE);
125
126   return (* iface->drag_data_get) (drag_source, path, selection_data);
127 }
128
129 /**
130  * gtk_tree_drag_dest_drag_data_received:
131  * @drag_dest: a #GtkTreeDragDest
132  * @dest: row to drop in front of
133  * @selection_data: data to drop
134  * 
135  * Asks the #GtkTreeDragDest to insert a row before the path @dest,
136  * deriving the contents of the row from @selection_data. If @dest is
137  * outside the tree so that inserting before it is impossible, %FALSE
138  * will be returned. Also, %FALSE may be returned if the new row is
139  * not created for some model-specific reason.  Should robustly handle
140  * a @dest no longer found in the model!
141  * 
142  * Return value: whether a new row was created before position @dest
143  **/
144 gboolean
145 gtk_tree_drag_dest_drag_data_received (GtkTreeDragDest  *drag_dest,
146                                        GtkTreePath      *dest,
147                                        GtkSelectionData *selection_data)
148 {
149   GtkTreeDragDestIface *iface = GTK_TREE_DRAG_DEST_GET_IFACE (drag_dest);
150
151   g_return_val_if_fail (iface->drag_data_received != NULL, FALSE);
152   g_return_val_if_fail (dest != NULL, FALSE);
153   g_return_val_if_fail (selection_data != NULL, FALSE);
154
155   return (* iface->drag_data_received) (drag_dest, dest, selection_data);
156 }
157
158 typedef struct _TreeRowData TreeRowData;
159
160 struct _TreeRowData
161 {
162   GtkTreeModel *model;
163   gchar path[4];
164 };
165
166 /**
167  * gtk_selection_data_set_tree_row:
168  * @selection_data: some #GtkSelectionData
169  * @tree_model: a #GtkTreeModel
170  * @path: a row in @tree_model
171  * 
172  * Sets selection data of target type %GTK_TREE_MODEL_ROW. Normally used
173  * in a drag_data_get handler.
174  * 
175  * Return value: %TRUE if the #GtkSelectionData had the proper target type to allow us to set a tree row
176  **/
177 gboolean
178 gtk_selection_data_set_tree_row (GtkSelectionData *selection_data,
179                                  GtkTreeModel     *tree_model,
180                                  GtkTreePath      *path)
181 {
182   TreeRowData *trd;
183   gchar *path_str;
184   gint len;
185   gint struct_size;
186   
187   g_return_val_if_fail (selection_data != NULL, FALSE);
188   g_return_val_if_fail (GTK_IS_TREE_MODEL (tree_model), FALSE);
189   g_return_val_if_fail (path != NULL, FALSE);
190
191   if (selection_data->target != gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
192     return FALSE;
193   
194   path_str = gtk_tree_path_to_string (path);
195
196   len = strlen (path_str);
197
198   /* the old allocate-end-of-struct-to-hold-string trick */
199   struct_size = sizeof (TreeRowData) + len + 1 -
200     (sizeof (TreeRowData) - G_STRUCT_OFFSET (TreeRowData, path));
201
202   trd = g_malloc (struct_size); 
203
204   strcpy (trd->path, path_str);
205   
206   trd->model = tree_model;
207   
208   gtk_selection_data_set (selection_data,
209                           gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE),
210                           8, /* bytes */
211                           (void*)trd,
212                           struct_size);
213
214   g_free (trd);
215   
216   return TRUE;
217 }
218
219 /**
220  * gtk_selection_data_get_tree_row:
221  * @selection_data: a #GtkSelectionData
222  * @tree_model: a #GtkTreeModel
223  * @path: row in @tree_model
224  * 
225  * Obtains a @tree_model and @path from selection data of target type
226  * %GTK_TREE_MODEL_ROW. Normally called from a drag_data_received handler.
227  * This function can only be used if @selection_data originates from the same
228  * process that's calling this function, because a pointer to the tree model
229  * is being passed around. If you aren't in the same process, then you'll
230  * get memory corruption. In the #GtkTreeDragDest drag_data_received handler,
231  * you can assume that selection data of type %GTK_TREE_MODEL_ROW is
232  * in from the current process. The returned path must be freed with
233  * gtk_tree_path_free().
234  * 
235  * Return value: %TRUE if @selection_data had target type %GTK_TREE_MODEL_ROW and
236  *  is otherwise valid
237  **/
238 gboolean
239 gtk_selection_data_get_tree_row (GtkSelectionData *selection_data,
240                                  GtkTreeModel    **tree_model,
241                                  GtkTreePath     **path)
242 {
243   TreeRowData *trd;
244   
245   g_return_val_if_fail (selection_data != NULL, FALSE);  
246
247   if (tree_model)
248     *tree_model = NULL;
249
250   if (path)
251     *path = NULL;
252   
253   if (selection_data->target != gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
254     return FALSE;
255
256   trd = (void*) selection_data->data;
257
258   if (tree_model)
259     *tree_model = trd->model;
260
261   if (path)
262     *path = gtk_tree_path_new_from_string (trd->path);
263   
264   return TRUE;
265 }