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"
26 #include <glib/gi18n.h>
28 #define DISCONNECT_UPDATE_SIGNAL(vl, val) g_signal_handlers_disconnect_matched(vl, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, val)
30 static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *len );
31 static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
32 static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode );
33 static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path );
35 VikLayerInterface vik_aggregate_layer_interface = {
39 &vikaggregatelayer_pixbuf,
51 (VikLayerFuncCreate) vik_aggregate_layer_create,
52 (VikLayerFuncRealize) vik_aggregate_layer_realize,
53 (VikLayerFuncPostRead) NULL,
54 (VikLayerFuncFree) vik_aggregate_layer_free,
56 (VikLayerFuncProperties) NULL,
57 (VikLayerFuncDraw) vik_aggregate_layer_draw,
58 (VikLayerFuncChangeCoordMode) aggregate_layer_change_coord_mode,
60 (VikLayerFuncSetMenuItemsSelection) NULL,
61 (VikLayerFuncGetMenuItemsSelection) NULL,
63 (VikLayerFuncAddMenuItems) NULL,
64 (VikLayerFuncSublayerAddMenuItems) NULL,
66 (VikLayerFuncSublayerRenameRequest) NULL,
67 (VikLayerFuncSublayerToggleVisible) NULL,
68 (VikLayerFuncSublayerTooltip) NULL,
69 (VikLayerFuncLayerTooltip) NULL,
70 (VikLayerFuncLayerSelected) NULL,
72 (VikLayerFuncMarshall) aggregate_layer_marshall,
73 (VikLayerFuncUnmarshall) aggregate_layer_unmarshall,
75 (VikLayerFuncSetParam) NULL,
76 (VikLayerFuncGetParam) NULL,
78 (VikLayerFuncReadFileData) NULL,
79 (VikLayerFuncWriteFileData) NULL,
81 (VikLayerFuncDeleteItem) NULL,
82 (VikLayerFuncCutItem) NULL,
83 (VikLayerFuncCopyItem) NULL,
84 (VikLayerFuncPasteItem) NULL,
85 (VikLayerFuncFreeCopiedItem) NULL,
86 (VikLayerFuncDragDropRequest) aggregate_layer_drag_drop_request,
88 (VikLayerFuncSelectClick) NULL,
89 (VikLayerFuncSelectMove) NULL,
90 (VikLayerFuncSelectRelease) NULL,
91 (VikLayerFuncSelectedViewportMenu) NULL,
94 struct _VikAggregateLayer {
99 GType vik_aggregate_layer_get_type ()
101 static GType val_type = 0;
105 static const GTypeInfo val_info =
107 sizeof (VikAggregateLayerClass),
108 NULL, /* base_init */
109 NULL, /* base_finalize */
110 NULL, /* class init */
111 NULL, /* class_finalize */
112 NULL, /* class_data */
113 sizeof (VikAggregateLayer),
115 NULL /* instance init */
117 val_type = g_type_register_static ( VIK_LAYER_TYPE, "VikAggregateLayer", &val_info, 0 );
123 VikAggregateLayer *vik_aggregate_layer_create (VikViewport *vp)
125 VikAggregateLayer *rv = vik_aggregate_layer_new ();
126 vik_layer_rename ( VIK_LAYER(rv), vik_aggregate_layer_interface.name );
130 static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *datalen )
132 GList *child = val->children;
133 VikLayer *child_layer;
136 GByteArray* b = g_byte_array_new ();
139 #define alm_append(obj, sz) \
141 g_byte_array_append ( b, (guint8 *)&len, sizeof(len) ); \
142 g_byte_array_append ( b, (guint8 *)(obj), len );
144 vik_layer_marshall_params(VIK_LAYER(val), &ld, &ll);
149 child_layer = VIK_LAYER(child->data);
150 vik_layer_marshall ( child_layer, &ld, &ll );
159 g_byte_array_free(b, FALSE);
163 static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
165 #define alm_size (*(gint *)data)
167 len -= sizeof(gint) + alm_size; \
168 data += sizeof(gint) + alm_size;
170 VikAggregateLayer *rv = vik_aggregate_layer_new();
171 VikLayer *child_layer;
173 vik_layer_unmarshall_params ( VIK_LAYER(rv), data+sizeof(gint), alm_size, vvp );
177 child_layer = vik_layer_unmarshall ( data + sizeof(gint), alm_size, vvp );
179 rv->children = g_list_append ( rv->children, child_layer );
180 g_signal_connect_swapped ( G_OBJECT(child_layer), "update", G_CALLBACK(vik_layer_emit_update_secondary), rv );
184 // g_print("aggregate_layer_unmarshall ended with len=%d\n", len);
190 VikAggregateLayer *vik_aggregate_layer_new ()
192 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( g_object_new ( VIK_AGGREGATE_LAYER_TYPE, NULL ) );
193 vik_layer_set_type ( VIK_LAYER(val), VIK_LAYER_AGGREGATE );
194 val->children = NULL;
198 void vik_aggregate_layer_insert_layer ( VikAggregateLayer *val, VikLayer *l, GtkTreeIter *replace_iter )
201 VikLayer *vl = VIK_LAYER(val);
205 vik_treeview_insert_layer ( vl->vt, &(vl->iter), &iter, l->name, val, l, l->type, l->type, replace_iter );
207 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
208 vik_layer_realize ( l, vl->vt, &iter );
210 if ( val->children == NULL )
211 vik_treeview_expand ( vl->vt, &(vl->iter) );
215 GList *theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, replace_iter ) );
216 val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone)+1 );
218 // Effectively insert at 'end' of the list to match how displayed in the treeview
219 // - but since it is drawn from 'bottom first' it is actually the first in the child list
220 // This ordering is especially important if it is a map or similar type,
221 // which needs be drawn first for the layering draw method to work properly.
222 // ATM this only happens when a layer is drag/dropped to the end of an aggregate layer
223 val->children = g_list_prepend ( val->children, l );
225 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
228 void vik_aggregate_layer_add_layer ( VikAggregateLayer *val, VikLayer *l )
231 VikLayer *vl = VIK_LAYER(val);
235 vik_treeview_add_layer ( vl->vt, &(vl->iter), &iter, l->name, val, l, l->type, l->type);
237 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
238 vik_layer_realize ( l, vl->vt, &iter );
240 if ( val->children == NULL )
241 vik_treeview_expand ( vl->vt, &(vl->iter) );
244 val->children = g_list_append ( val->children, l );
245 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
248 void vik_aggregate_layer_move_layer ( VikAggregateLayer *val, GtkTreeIter *child_iter, gboolean up )
250 GList *theone, *first, *second;
251 VikLayer *vl = VIK_LAYER(val);
252 vik_treeview_move_item ( vl->vt, child_iter, up );
254 theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, child_iter ) );
256 g_assert ( theone != NULL );
258 /* the old switcheroo */
259 if ( up && theone->next )
262 second = theone->next;
264 else if ( !up && theone->prev )
266 first = theone->prev;
272 first->next = second->next;
273 second->prev = first->prev;
274 first->prev = second;
275 second->next = first;
277 /* second is now first */
280 second->prev->next = second;
282 first->next->prev = first;
284 if ( second->prev == NULL )
285 val->children = second;
288 /* Draw the aggregate layer. If vik viewport is in half_drawn mode, this means we are only
289 * to draw the layers above and including the trigger layer.
290 * To do this we don't draw any layers if in half drawn mode, unless we find the
291 * trigger layer, in which case we pull up the saved pixmap, turn off half drawn mode and
292 * start drawing layers.
293 * Also, if we were never in half drawn mode, we save a snapshot
294 * of the pixmap before drawing the trigger layer so we can use it again
297 void vik_aggregate_layer_draw ( VikAggregateLayer *val, gpointer data )
299 GList *iter = val->children;
301 VikViewport *viewport = VIK_VIEWPORT(data);
302 VikLayer *trigger = VIK_LAYER(vik_viewport_get_trigger( viewport ));
304 vl = VIK_LAYER(iter->data);
305 if ( vl == trigger ) {
306 if ( vik_viewport_get_half_drawn ( viewport ) ) {
307 vik_viewport_set_half_drawn ( viewport, FALSE );
308 vik_viewport_snapshot_load( viewport );
310 vik_viewport_snapshot_save( viewport );
313 if ( vl->type == VIK_LAYER_AGGREGATE || vl->type == VIK_LAYER_GPS || ! vik_viewport_get_half_drawn( viewport ) )
314 vik_layer_draw ( vl, data );
319 static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode )
321 GList *iter = val->children;
324 vik_layer_change_coord_mode ( VIK_LAYER(iter->data), mode );
329 static void disconnect_layer_signal ( VikLayer *vl, VikAggregateLayer *val )
331 g_assert(DISCONNECT_UPDATE_SIGNAL(vl,val)==1);
334 void vik_aggregate_layer_free ( VikAggregateLayer *val )
336 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
337 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
338 g_list_free ( val->children );
341 static void delete_layer_iter ( VikLayer *vl )
344 vik_treeview_item_delete ( vl->vt, &(vl->iter) );
347 void vik_aggregate_layer_clear ( VikAggregateLayer *val )
349 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
350 g_list_foreach ( val->children, (GFunc)(delete_layer_iter), NULL );
351 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
352 g_list_free ( val->children );
353 val->children = NULL;
356 gboolean vik_aggregate_layer_delete ( VikAggregateLayer *val, GtkTreeIter *iter )
358 VikLayer *l = VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, iter ) );
359 gboolean was_visible = l->visible;
361 vik_treeview_item_delete ( VIK_LAYER(val)->vt, iter );
362 val->children = g_list_remove ( val->children, l );
363 g_assert(DISCONNECT_UPDATE_SIGNAL(l,val)==1);
364 g_object_unref ( l );
370 /* returns 0 == we're good, 1 == didn't find any layers, 2 == got rejected */
371 guint vik_aggregate_layer_tool ( VikAggregateLayer *val, guint16 layer_type, VikToolInterfaceFunc tool_func, GdkEventButton *event, VikViewport *vvp )
373 GList *iter = val->children;
374 gboolean found_rej = FALSE;
382 /* if this layer "accepts" the tool call */
383 if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == layer_type )
385 if ( tool_func ( VIK_LAYER(iter->data), event, vvp ) )
391 /* recursive -- try the same for the child aggregate layer. */
392 else if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == VIK_LAYER_AGGREGATE )
394 gint rv = vik_aggregate_layer_tool(VIK_AGGREGATE_LAYER(iter->data), layer_type, tool_func, event, vvp);
402 return found_rej ? 2 : 1; /* no one wanted to accept the tool call in this layer */
406 VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer *val, gint type )
409 GList *ls = val->children;
417 VikLayer *vl = VIK_LAYER(ls->data);
418 if ( vl->visible && vl->type == type )
420 else if ( vl->visible && vl->type == VIK_LAYER_AGGREGATE )
422 rv = vik_aggregate_layer_get_top_visible_layer_of_type(VIK_AGGREGATE_LAYER(vl), type);
431 GList *vik_aggregate_layer_get_all_layers_of_type(VikAggregateLayer *val, GList *layers, gint type, gboolean include_invisible)
434 GList *children = val->children;
439 // Where appropriate *don't* include non-visible layers
441 vl = VIK_LAYER(children->data);
442 if (vl->type == VIK_LAYER_AGGREGATE ) {
443 // Don't even consider invisible aggregrates, unless told to
444 if (vl->visible || include_invisible)
445 l = vik_aggregate_layer_get_all_layers_of_type(VIK_AGGREGATE_LAYER(children->data), l, type, include_invisible);
447 else if (vl->type == type) {
448 if (vl->visible || include_invisible)
449 l = g_list_prepend(l, children->data); /* now in top down order */
451 else if (type == VIK_LAYER_TRW) {
452 /* GPS layers contain TRW layers. cf with usage in file.c */
453 if (VIK_LAYER(children->data)->type == VIK_LAYER_GPS) {
454 if (VIK_LAYER(children->data)->visible || include_invisible) {
455 if (!vik_gps_layer_is_empty(VIK_GPS_LAYER(children->data))) {
457 can not use g_list_concat due to wrong copy method - crashes if used a couple times !!
458 l = g_list_concat (l, vik_gps_layer_get_children (VIK_GPS_LAYER(children->data)));
460 /* create own copy method instead :( */
461 GList *gps_trw_layers = (GList *)vik_gps_layer_get_children (VIK_GPS_LAYER(children->data));
462 int n_layers = g_list_length (gps_trw_layers);
464 for ( layer = 0; layer < n_layers; layer++) {
465 l = g_list_prepend(l, gps_trw_layers->data);
466 gps_trw_layers = gps_trw_layers->next;
468 g_list_free(gps_trw_layers);
473 children = children->next;
478 void vik_aggregate_layer_realize ( VikAggregateLayer *val, VikTreeview *vt, GtkTreeIter *layer_iter )
480 GList *i = val->children;
482 VikLayer *vl = VIK_LAYER(val);
486 vli = VIK_LAYER(i->data);
487 vik_treeview_add_layer ( vl->vt, layer_iter, &iter, vli->name, val,
488 vli, vli->type, vli->type );
489 if ( ! vli->visible )
490 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
491 vik_layer_realize ( vli, vl->vt, &iter );
496 const GList *vik_aggregate_layer_get_children ( VikAggregateLayer *val )
498 return val->children;
501 gboolean vik_aggregate_layer_is_empty ( VikAggregateLayer *val )
508 static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path )
510 VikTreeview *vt = VIK_LAYER(val_src)->vt;
511 VikLayer *vl = vik_treeview_item_get_pointer(vt, src_item_iter);
512 GtkTreeIter dest_iter;
514 gboolean target_exists;
516 dp = gtk_tree_path_to_string(dest_path);
517 target_exists = vik_treeview_get_iter_from_path_str(vt, &dest_iter, dp);
519 /* vik_aggregate_layer_delete unrefs, but we don't want that here.
520 * we're still using the layer. */
522 vik_aggregate_layer_delete(val_src, src_item_iter);
525 vik_aggregate_layer_insert_layer(val_dest, vl, &dest_iter);
527 vik_aggregate_layer_insert_layer(val_dest, vl, NULL); /* append */