2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
23 #include "icons/icons.h"
27 #define DISCONNECT_UPDATE_SIGNAL(vl, val) g_signal_handlers_disconnect_matched(vl, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, val)
29 static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *len );
30 static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
31 static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode );
32 static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path );
34 VikLayerInterface vik_aggregate_layer_interface = {
36 &vikaggregatelayer_pixbuf,
48 (VikLayerFuncCreate) vik_aggregate_layer_create,
49 (VikLayerFuncRealize) vik_aggregate_layer_realize,
50 (VikLayerFuncPostRead) NULL,
51 (VikLayerFuncFree) vik_aggregate_layer_free,
53 (VikLayerFuncProperties) NULL,
54 (VikLayerFuncDraw) vik_aggregate_layer_draw,
55 (VikLayerFuncChangeCoordMode) aggregate_layer_change_coord_mode,
57 (VikLayerFuncSetMenuItemsSelection) NULL,
58 (VikLayerFuncGetMenuItemsSelection) NULL,
60 (VikLayerFuncAddMenuItems) NULL,
61 (VikLayerFuncSublayerAddMenuItems) NULL,
63 (VikLayerFuncSublayerRenameRequest) NULL,
64 (VikLayerFuncSublayerToggleVisible) NULL,
65 (VikLayerFuncSublayerTooltip) NULL,
66 (VikLayerFuncLayerTooltip) NULL,
67 (VikLayerFuncLayerSelected) NULL,
69 (VikLayerFuncMarshall) aggregate_layer_marshall,
70 (VikLayerFuncUnmarshall) aggregate_layer_unmarshall,
72 (VikLayerFuncSetParam) NULL,
73 (VikLayerFuncGetParam) NULL,
75 (VikLayerFuncReadFileData) NULL,
76 (VikLayerFuncWriteFileData) NULL,
78 (VikLayerFuncDeleteItem) NULL,
79 (VikLayerFuncCutItem) NULL,
80 (VikLayerFuncCopyItem) NULL,
81 (VikLayerFuncPasteItem) NULL,
82 (VikLayerFuncFreeCopiedItem) NULL,
83 (VikLayerFuncDragDropRequest) aggregate_layer_drag_drop_request,
85 (VikLayerFuncSelectClick) NULL,
86 (VikLayerFuncSelectMove) NULL,
87 (VikLayerFuncSelectRelease) NULL,
88 (VikLayerFuncSelectedViewportMenu) NULL,
91 struct _VikAggregateLayer {
96 GType vik_aggregate_layer_get_type ()
98 static GType val_type = 0;
102 static const GTypeInfo val_info =
104 sizeof (VikAggregateLayerClass),
105 NULL, /* base_init */
106 NULL, /* base_finalize */
107 NULL, /* class init */
108 NULL, /* class_finalize */
109 NULL, /* class_data */
110 sizeof (VikAggregateLayer),
112 NULL /* instance init */
114 val_type = g_type_register_static ( VIK_LAYER_TYPE, "VikAggregateLayer", &val_info, 0 );
120 VikAggregateLayer *vik_aggregate_layer_create (VikViewport *vp)
122 VikAggregateLayer *rv = vik_aggregate_layer_new ();
123 vik_layer_rename ( VIK_LAYER(rv), vik_aggregate_layer_interface.name );
127 static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *datalen )
129 GList *child = val->children;
130 VikLayer *child_layer;
133 GByteArray* b = g_byte_array_new ();
136 #define alm_append(obj, sz) \
138 g_byte_array_append ( b, (guint8 *)&len, sizeof(len) ); \
139 g_byte_array_append ( b, (guint8 *)(obj), len );
141 vik_layer_marshall_params(VIK_LAYER(val), &ld, &ll);
146 child_layer = VIK_LAYER(child->data);
147 vik_layer_marshall ( child_layer, &ld, &ll );
156 g_byte_array_free(b, FALSE);
160 static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
162 #define alm_size (*(gint *)data)
164 len -= sizeof(gint) + alm_size; \
165 data += sizeof(gint) + alm_size;
167 VikAggregateLayer *rv = vik_aggregate_layer_new();
168 VikLayer *child_layer;
170 vik_layer_unmarshall_params ( VIK_LAYER(rv), data+sizeof(gint), alm_size, vvp );
174 child_layer = vik_layer_unmarshall ( data + sizeof(gint), alm_size, vvp );
176 rv->children = g_list_append ( rv->children, child_layer );
177 g_signal_connect_swapped ( G_OBJECT(child_layer), "update", G_CALLBACK(vik_layer_emit_update_secondary), rv );
181 // g_print("aggregate_layer_unmarshall ended with len=%d\n", len);
187 VikAggregateLayer *vik_aggregate_layer_new ()
189 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( g_object_new ( VIK_AGGREGATE_LAYER_TYPE, NULL ) );
190 vik_layer_init ( VIK_LAYER(val), VIK_LAYER_AGGREGATE );
191 val->children = NULL;
195 void vik_aggregate_layer_insert_layer ( VikAggregateLayer *val, VikLayer *l, GtkTreeIter *replace_iter )
198 VikLayer *vl = VIK_LAYER(val);
202 vik_treeview_insert_layer ( vl->vt, &(vl->iter), &iter, l->name, val, l, l->type, l->type, replace_iter );
204 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
205 vik_layer_realize ( l, vl->vt, &iter );
207 if ( val->children == NULL )
208 vik_treeview_expand ( vl->vt, &(vl->iter) );
212 GList *theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, replace_iter ) );
213 val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone)+1 );
215 // Effectively insert at 'end' of the list to match how displayed in the treeview
216 // - but since it is drawn from 'bottom first' it is actually the first in the child list
217 // This ordering is especially important if it is a map or similar type,
218 // which needs be drawn first for the layering draw method to work properly.
219 // ATM this only happens when a layer is drag/dropped to the end of an aggregate layer
220 val->children = g_list_prepend ( val->children, l );
222 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
225 void vik_aggregate_layer_add_layer ( VikAggregateLayer *val, VikLayer *l )
228 VikLayer *vl = VIK_LAYER(val);
232 vik_treeview_add_layer ( vl->vt, &(vl->iter), &iter, l->name, val, l, l->type, l->type);
234 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
235 vik_layer_realize ( l, vl->vt, &iter );
237 if ( val->children == NULL )
238 vik_treeview_expand ( vl->vt, &(vl->iter) );
241 val->children = g_list_append ( val->children, l );
242 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
245 void vik_aggregate_layer_move_layer ( VikAggregateLayer *val, GtkTreeIter *child_iter, gboolean up )
247 GList *theone, *first, *second;
248 VikLayer *vl = VIK_LAYER(val);
249 vik_treeview_move_item ( vl->vt, child_iter, up );
251 theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, child_iter ) );
253 g_assert ( theone != NULL );
255 /* the old switcheroo */
256 if ( up && theone->next )
259 second = theone->next;
261 else if ( !up && theone->prev )
263 first = theone->prev;
269 first->next = second->next;
270 second->prev = first->prev;
271 first->prev = second;
272 second->next = first;
274 /* second is now first */
277 second->prev->next = second;
279 first->next->prev = first;
281 if ( second->prev == NULL )
282 val->children = second;
285 /* Draw the aggregate layer. If vik viewport is in half_drawn mode, this means we are only
286 * to draw the layers above and including the trigger layer.
287 * To do this we don't draw any layers if in half drawn mode, unless we find the
288 * trigger layer, in which case we pull up the saved pixmap, turn off half drawn mode and
289 * start drawing layers.
290 * Also, if we were never in half drawn mode, we save a snapshot
291 * of the pixmap before drawing the trigger layer so we can use it again
294 void vik_aggregate_layer_draw ( VikAggregateLayer *val, gpointer data )
296 GList *iter = val->children;
298 VikViewport *viewport = VIK_VIEWPORT(data);
299 VikLayer *trigger = VIK_LAYER(vik_viewport_get_trigger( viewport ));
301 vl = VIK_LAYER(iter->data);
302 if ( vl == trigger ) {
303 if ( vik_viewport_get_half_drawn ( viewport ) ) {
304 vik_viewport_set_half_drawn ( viewport, FALSE );
305 vik_viewport_snapshot_load( viewport );
307 vik_viewport_snapshot_save( viewport );
310 if ( vl->type == VIK_LAYER_AGGREGATE || vl->type == VIK_LAYER_GPS || ! vik_viewport_get_half_drawn( viewport ) )
311 vik_layer_draw ( vl, data );
316 static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode )
318 GList *iter = val->children;
321 vik_layer_change_coord_mode ( VIK_LAYER(iter->data), mode );
326 static void disconnect_layer_signal ( VikLayer *vl, VikAggregateLayer *val )
328 g_assert(DISCONNECT_UPDATE_SIGNAL(vl,val)==1);
331 void vik_aggregate_layer_free ( VikAggregateLayer *val )
333 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
334 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
335 g_list_free ( val->children );
338 static void delete_layer_iter ( VikLayer *vl )
341 vik_treeview_item_delete ( vl->vt, &(vl->iter) );
344 void vik_aggregate_layer_clear ( VikAggregateLayer *val )
346 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
347 g_list_foreach ( val->children, (GFunc)(delete_layer_iter), NULL );
348 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
349 g_list_free ( val->children );
350 val->children = NULL;
353 gboolean vik_aggregate_layer_delete ( VikAggregateLayer *val, GtkTreeIter *iter )
355 VikLayer *l = VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, iter ) );
356 gboolean was_visible = l->visible;
358 vik_treeview_item_delete ( VIK_LAYER(val)->vt, iter );
359 val->children = g_list_remove ( val->children, l );
360 g_assert(DISCONNECT_UPDATE_SIGNAL(l,val)==1);
361 g_object_unref ( l );
367 /* returns 0 == we're good, 1 == didn't find any layers, 2 == got rejected */
368 guint vik_aggregate_layer_tool ( VikAggregateLayer *val, guint16 layer_type, VikToolInterfaceFunc tool_func, GdkEventButton *event, VikViewport *vvp )
370 GList *iter = val->children;
371 gboolean found_rej = FALSE;
379 /* if this layer "accepts" the tool call */
380 if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == layer_type )
382 if ( tool_func ( VIK_LAYER(iter->data), event, vvp ) )
388 /* recursive -- try the same for the child aggregate layer. */
389 else if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == VIK_LAYER_AGGREGATE )
391 gint rv = vik_aggregate_layer_tool(VIK_AGGREGATE_LAYER(iter->data), layer_type, tool_func, event, vvp);
399 return found_rej ? 2 : 1; /* no one wanted to accept the tool call in this layer */
403 VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer *val, gint type )
406 GList *ls = val->children;
414 VikLayer *vl = VIK_LAYER(ls->data);
415 if ( vl->visible && vl->type == type )
417 else if ( vl->visible && vl->type == VIK_LAYER_AGGREGATE )
419 rv = vik_aggregate_layer_get_top_visible_layer_of_type(VIK_AGGREGATE_LAYER(vl), type);
428 GList *vik_aggregate_layer_get_all_layers_of_type(VikAggregateLayer *val, GList *layers, gint type, gboolean include_invisible)
431 GList *children = val->children;
436 // Where appropriate *don't* include non-visible layers
438 vl = VIK_LAYER(children->data);
439 if (vl->type == VIK_LAYER_AGGREGATE ) {
440 // Don't even consider invisible aggregrates, unless told to
441 if (vl->visible || include_invisible)
442 l = vik_aggregate_layer_get_all_layers_of_type(VIK_AGGREGATE_LAYER(children->data), l, type, include_invisible);
444 else if (vl->type == type) {
445 if (vl->visible || include_invisible)
446 l = g_list_prepend(l, children->data); /* now in top down order */
448 else if (type == VIK_LAYER_TRW) {
449 /* GPS layers contain TRW layers. cf with usage in file.c */
450 if (VIK_LAYER(children->data)->type == VIK_LAYER_GPS) {
451 if (VIK_LAYER(children->data)->visible || include_invisible) {
452 if (!vik_gps_layer_is_empty(VIK_GPS_LAYER(children->data))) {
454 can not use g_list_concat due to wrong copy method - crashes if used a couple times !!
455 l = g_list_concat (l, vik_gps_layer_get_children (VIK_GPS_LAYER(children->data)));
457 /* create own copy method instead :( */
458 GList *gps_trw_layers = (GList *)vik_gps_layer_get_children (VIK_GPS_LAYER(children->data));
459 int n_layers = g_list_length (gps_trw_layers);
461 for ( layer = 0; layer < n_layers; layer++) {
462 l = g_list_prepend(l, gps_trw_layers->data);
463 gps_trw_layers = gps_trw_layers->next;
465 g_list_free(gps_trw_layers);
470 children = children->next;
475 void vik_aggregate_layer_realize ( VikAggregateLayer *val, VikTreeview *vt, GtkTreeIter *layer_iter )
477 GList *i = val->children;
479 VikLayer *vl = VIK_LAYER(val);
483 vli = VIK_LAYER(i->data);
484 vik_treeview_add_layer ( vl->vt, layer_iter, &iter, vli->name, val,
485 vli, vli->type, vli->type );
486 if ( ! vli->visible )
487 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
488 vik_layer_realize ( vli, vl->vt, &iter );
493 const GList *vik_aggregate_layer_get_children ( VikAggregateLayer *val )
495 return val->children;
498 gboolean vik_aggregate_layer_is_empty ( VikAggregateLayer *val )
505 static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path )
507 VikTreeview *vt = VIK_LAYER(val_src)->vt;
508 VikLayer *vl = vik_treeview_item_get_pointer(vt, src_item_iter);
509 GtkTreeIter dest_iter;
511 gboolean target_exists;
513 dp = gtk_tree_path_to_string(dest_path);
514 target_exists = vik_treeview_get_iter_from_path_str(vt, &dest_iter, dp);
516 /* vik_aggregate_layer_delete unrefs, but we don't want that here.
517 * we're still using the layer. */
519 vik_aggregate_layer_delete(val_src, src_item_iter);
522 vik_aggregate_layer_insert_layer(val_dest, vl, &dest_iter);
524 vik_aggregate_layer_insert_layer(val_dest, vl, NULL); /* append */