]> git.street.me.uk Git - andy/viking.git/blame - src/vikaggregatelayer.c
finished layer marshalling support
[andy/viking.git] / src / vikaggregatelayer.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "viking.h"
23#include "vikaggregatelayer_pixmap.h"
24
25#include <string.h>
26
27#define DISCONNECT_UPDATE_SIGNAL(vl, val) g_signal_handlers_disconnect_matched(vl, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, val)
28
29static VikAggregateLayer *aggregate_layer_copy ( VikAggregateLayer *val, gpointer vp );
0a6cab71
AF
30static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *len );
31static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
50a14534 32static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode );
70a23263 33static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path );
50a14534
EB
34
35VikLayerInterface vik_aggregate_layer_interface = {
36 "Aggregate",
37 &aggregatelayer_pixbuf,
38
39 NULL,
40 0,
41
42 NULL,
43 0,
44 NULL,
45 0,
46
47 (VikLayerFuncCreate) vik_aggregate_layer_create,
48 (VikLayerFuncRealize) vik_aggregate_layer_realize,
49 (VikLayerFuncPostRead) NULL,
50 (VikLayerFuncFree) vik_aggregate_layer_free,
51
52 (VikLayerFuncProperties) NULL,
53 (VikLayerFuncDraw) vik_aggregate_layer_draw,
54 (VikLayerFuncChangeCoordMode) aggregate_layer_change_coord_mode,
55
56 (VikLayerFuncAddMenuItems) NULL,
57 (VikLayerFuncSublayerAddMenuItems) NULL,
58
59 (VikLayerFuncSublayerRenameRequest) NULL,
60 (VikLayerFuncSublayerToggleVisible) NULL,
61
62 (VikLayerFuncCopy) aggregate_layer_copy,
0a6cab71
AF
63 (VikLayerFuncMarshall) aggregate_layer_marshall,
64 (VikLayerFuncUnmarshall) aggregate_layer_unmarshall,
50a14534
EB
65
66 (VikLayerFuncSetParam) NULL,
67 (VikLayerFuncGetParam) NULL,
68
69 (VikLayerFuncReadFileData) NULL,
70 (VikLayerFuncWriteFileData) NULL,
71
72 (VikLayerFuncCopyItem) NULL,
73 (VikLayerFuncPasteItem) NULL,
74 (VikLayerFuncFreeCopiedItem) NULL,
70a23263 75 (VikLayerFuncDragDropRequest) aggregate_layer_drag_drop_request,
50a14534
EB
76};
77
78struct _VikAggregateLayer {
79 VikLayer vl;
80 GList *children;
81};
82
83GType vik_aggregate_layer_get_type ()
84{
85 static GType val_type = 0;
86
87 if (!val_type)
88 {
89 static const GTypeInfo val_info =
90 {
91 sizeof (VikAggregateLayerClass),
92 NULL, /* base_init */
93 NULL, /* base_finalize */
94 NULL, /* class init */
95 NULL, /* class_finalize */
96 NULL, /* class_data */
97 sizeof (VikAggregateLayer),
98 0,
99 NULL /* instance init */
100 };
101 val_type = g_type_register_static ( VIK_LAYER_TYPE, "VikAggregateLayer", &val_info, 0 );
102 }
103
104 return val_type;
105}
106
107VikAggregateLayer *vik_aggregate_layer_create (VikViewport *vp)
108{
109 VikAggregateLayer *rv = vik_aggregate_layer_new ();
110 vik_layer_rename ( VIK_LAYER(rv), vik_aggregate_layer_interface.name );
111 return rv;
112}
113
114static VikAggregateLayer *aggregate_layer_copy ( VikAggregateLayer *val, gpointer vp )
115{
116 VikAggregateLayer *rv = vik_aggregate_layer_new ();
117 VikLayer *child_layer;
118 GList *child = val->children;
119 while ( child )
120 {
121 child_layer = vik_layer_copy ( VIK_LAYER(child->data), vp );
0a6cab71 122 if ( child_layer ) {
50a14534
EB
123 rv->children = g_list_append ( rv->children, child_layer );
124 g_signal_connect_swapped ( G_OBJECT(child_layer), "update", G_CALLBACK(vik_layer_emit_update), rv );
0a6cab71 125 }
50a14534
EB
126 child = child->next;
127 }
128 return rv;
129}
130
0a6cab71
AF
131static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *datalen )
132{
133 GList *child = val->children;
134 VikLayer *child_layer;
135 guint8 *ld;
136 gint ll;
137 GByteArray* b = g_byte_array_new ();
138 gint len;
139
140#define alm_append(obj, sz) \
141 len = (sz); \
142 g_byte_array_append ( b, (guint8 *)&len, sizeof(len) ); \
143 g_byte_array_append ( b, (guint8 *)(obj), len );
144
145 vik_layer_marshall_params(VIK_LAYER(val), &ld, &ll);
146 alm_append(ld, ll);
147 g_free(ld);
148
149 while (child) {
150 child_layer = VIK_LAYER(child->data);
151 vik_layer_marshall ( child_layer, &ld, &ll );
152 if (ld) {
153 alm_append(ld, ll);
154 g_free(ld);
155 }
156 child = child->next;
157 }
158 *data = b->data;
159 *datalen = b->len;
160 g_byte_array_free(b, FALSE);
161#undef alm_append
162}
163
164static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
165{
166#define alm_size (*(gint *)data)
167#define alm_next \
168 len -= sizeof(gint) + alm_size; \
169 data += sizeof(gint) + alm_size;
170
171 VikAggregateLayer *rv = vik_aggregate_layer_new();
172 VikLayer *child_layer;
173
174 vik_layer_unmarshall_params ( VIK_LAYER(rv), data+sizeof(gint), alm_size, vvp );
175 alm_next;
176
177 while (len>0) {
178 child_layer = vik_layer_unmarshall ( data + sizeof(gint), alm_size, vvp );
179 if (child_layer) {
180 rv->children = g_list_append ( rv->children, child_layer );
181 g_signal_connect_swapped ( G_OBJECT(child_layer), "update", G_CALLBACK(vik_layer_emit_update), rv );
182 }
183 alm_next;
184 }
185 // g_print("aggregate_layer_unmarshall ended with len=%d\n", len);
186 return rv;
187#undef alm_size
188#undef alm_next
189}
190
50a14534
EB
191VikAggregateLayer *vik_aggregate_layer_new ()
192{
193 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( g_object_new ( VIK_AGGREGATE_LAYER_TYPE, NULL ) );
194 vik_layer_init ( VIK_LAYER(val), VIK_LAYER_AGGREGATE );
195 val->children = NULL;
196 return val;
197}
198
199void vik_aggregate_layer_insert_layer ( VikAggregateLayer *val, VikLayer *l, GtkTreeIter *replace_iter )
200{
50a14534 201 GtkTreeIter iter;
e673b75f 202
50a14534
EB
203 if ( VIK_LAYER(val)->realized )
204 {
205 vik_treeview_insert_layer ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), &iter, l->name, val, l, l->type, l->type, replace_iter );
206 if ( ! l->visible )
207 vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE );
208 vik_layer_realize ( l, VIK_LAYER(val)->vt, &iter );
209
210 if ( val->children == NULL )
211 vik_treeview_expand ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter) );
212 }
50a14534 213
e673b75f
AF
214 if (replace_iter) {
215 GList *theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, replace_iter ) );
216 val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone)+1 );
217 } else {
218 val->children = g_list_append ( val->children, l );
219 }
50a14534
EB
220 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update), val );
221}
222
223void vik_aggregate_layer_add_layer ( VikAggregateLayer *val, VikLayer *l )
224{
225 GtkTreeIter iter;
226
227 if ( VIK_LAYER(val)->realized )
228 {
229 vik_treeview_add_layer ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), &iter, l->name, val, l, l->type, l->type);
230 if ( ! l->visible )
231 vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE );
232 vik_layer_realize ( l, VIK_LAYER(val)->vt, &iter );
233
234 if ( val->children == NULL )
235 vik_treeview_expand ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter) );
236 }
237
238 val->children = g_list_append ( val->children, l );
239 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update), val );
240}
241
242void vik_aggregate_layer_move_layer ( VikAggregateLayer *val, GtkTreeIter *child_iter, gboolean up )
243{
244 GList *theone, *first, *second;
245 vik_treeview_move_item ( VIK_LAYER(val)->vt, child_iter, up );
246
247 theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, child_iter ) );
248
249 g_assert ( theone != NULL );
250
251 /* the old switcheroo */
252 if ( up && theone->next )
253 {
254 first = theone;
255 second = theone->next;
256 }
257 else if ( !up && theone->prev )
258 {
259 first = theone->prev;
260 second = theone;
261 }
262 else
263 return;
264
265 first->next = second->next;
266 second->prev = first->prev;
267 first->prev = second;
268 second->next = first;
269
270 /* second is now first */
271
272 if ( second->prev )
273 second->prev->next = second;
274 if ( first->next )
275 first->next->prev = first;
276
277 if ( second->prev == NULL )
278 val->children = second;
279}
280
281void vik_aggregate_layer_draw ( VikAggregateLayer *val, gpointer data )
282{
283 g_list_foreach ( val->children, (GFunc)(vik_layer_draw), data );
284}
285
286static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode )
287{
288 GList *iter = val->children;
289 while ( iter )
290 {
291 vik_layer_change_coord_mode ( VIK_LAYER(iter->data), mode );
292 iter = iter->next;
293 }
294}
295
296static void disconnect_layer_signal ( VikLayer *vl, VikAggregateLayer *val )
297{
298 g_assert(DISCONNECT_UPDATE_SIGNAL(vl,val)==1);
299}
300
301void vik_aggregate_layer_free ( VikAggregateLayer *val )
302{
303 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
304 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
305 g_list_free ( val->children );
306}
307
308static void delete_layer_iter ( VikLayer *vl )
309{
310 if ( vl->realized )
311 vik_treeview_item_delete ( vl->vt, &(vl->iter) );
312}
313
314void vik_aggregate_layer_clear ( VikAggregateLayer *val )
315{
316 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
317 g_list_foreach ( val->children, (GFunc)(delete_layer_iter), NULL );
318 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
319 g_list_free ( val->children );
320 val->children = NULL;
321}
322
323gboolean vik_aggregate_layer_delete ( VikAggregateLayer *val, GtkTreeIter *iter )
324{
325 VikLayer *l = VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, iter ) );
326 gboolean was_visible = l->visible;
327
328 vik_treeview_item_delete ( VIK_LAYER(val)->vt, iter );
329 val->children = g_list_remove ( val->children, l );
330 g_assert(DISCONNECT_UPDATE_SIGNAL(l,val)==1);
331 g_object_unref ( l );
332
333 return was_visible;
334}
335
336/* returns 0 == we're good, 1 == didn't find any layers, 2 == got rejected */
337guint vik_aggregate_layer_tool ( VikAggregateLayer *val, guint16 layer_type, VikToolInterfaceFunc tool_func, GdkEventButton *event, VikViewport *vvp )
338{
339 GList *iter = val->children;
340 gboolean found_rej = FALSE;
341 if (!iter)
342 return FALSE;
343 while (iter->next)
344 iter = iter->next;
345
346 while ( iter )
347 {
348 /* if this layer "accepts" the tool call */
349 if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == layer_type )
350 {
351 if ( tool_func ( VIK_LAYER(iter->data), event, vvp ) )
352 return 0;
353 else
354 found_rej = TRUE;
355 }
356
357 /* recursive -- try the same for the child aggregate layer. */
358 else if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == VIK_LAYER_AGGREGATE )
359 {
360 gint rv = vik_aggregate_layer_tool(VIK_AGGREGATE_LAYER(iter->data), layer_type, tool_func, event, vvp);
361 if ( rv == 0 )
362 return 0;
363 else if ( rv == 2 )
364 found_rej = TRUE;
365 }
366 iter = iter->prev;
367 }
368 return found_rej ? 2 : 1; /* no one wanted to accept the tool call in this layer */
369}
370
371VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer *val, gint type )
372{
373 VikLayer *rv;
374 GList *ls = val->children;
375 if (!ls)
376 return NULL;
377 while (ls->next)
378 ls = ls->next;
379
380 while ( ls )
381 {
382 if ( VIK_LAYER(ls->data)->visible && VIK_LAYER(ls->data)->type == type )
383 return VIK_LAYER(ls->data);
384 else if ( VIK_LAYER(ls->data)->visible && VIK_LAYER(ls->data)->type == VIK_LAYER_AGGREGATE )
385 {
386 rv = vik_aggregate_layer_get_top_visible_layer_of_type(VIK_AGGREGATE_LAYER(ls->data), type);
387 if ( rv )
388 return rv;
389 }
390 ls = ls->prev;
391 }
392 return NULL;
393}
394
395void vik_aggregate_layer_realize ( VikAggregateLayer *val, VikTreeview *vt, GtkTreeIter *layer_iter )
396{
397 GList *i = val->children;
398 GtkTreeIter iter;
399 while ( i )
400 {
401 vik_treeview_add_layer ( VIK_LAYER(val)->vt, layer_iter, &iter, VIK_LAYER(i->data)->name, val,
402 VIK_LAYER(i->data), VIK_LAYER(i->data)->type, VIK_LAYER(i->data)->type );
403 if ( ! VIK_LAYER(i->data)->visible )
404 vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE );
405 vik_layer_realize ( VIK_LAYER(i->data), VIK_LAYER(val)->vt, &iter );
406 i = i->next;
407 }
408}
409
410const GList *vik_aggregate_layer_get_children ( VikAggregateLayer *val )
411{
412 return val->children;
413}
414
415gboolean vik_aggregate_layer_is_empty ( VikAggregateLayer *val )
416{
417 if ( val->children )
418 return FALSE;
419 return TRUE;
420}
70a23263
AF
421
422static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path )
423{
424 VikTreeview *vt = VIK_LAYER(val_src)->vt;
425 VikLayer *vl = vik_treeview_item_get_pointer(vt, src_item_iter);
e673b75f 426 GtkTreeIter dest_iter;
c390aa71
AF
427 gchar *dp;
428 gboolean target_exists;
e673b75f 429
c390aa71
AF
430 dp = gtk_tree_path_to_string(dest_path);
431 target_exists = vik_treeview_get_iter_from_path_str(vt, &dest_iter, dp);
801ce684
EB
432
433 /* vik_aggregate_layer_delete unrefs, but we don't want that here.
434 * we're still using the layer. */
435 g_object_ref ( vl );
436 vik_aggregate_layer_delete(val_src, src_item_iter);
437
c390aa71 438 if (target_exists) {
e673b75f
AF
439 vik_aggregate_layer_insert_layer(val_dest, vl, &dest_iter);
440 } else {
441 vik_aggregate_layer_insert_layer(val_dest, vl, NULL); /* append */
442 }
c390aa71 443 g_free(dp);
70a23263
AF
444}
445