2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "viktrwlayer_analysis.h"
25 #include "viktrwlayer_tracklist.h"
26 #include "viktrwlayer_waypointlist.h"
27 #include "icons/icons.h"
30 #include <glib/gi18n.h>
32 #define DISCONNECT_UPDATE_SIGNAL(vl, val) g_signal_handlers_disconnect_matched(vl, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, val)
34 static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *len );
35 static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
36 static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode );
37 static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path );
39 static void aggregate_layer_add_menu_items ( VikAggregateLayer *val, GtkMenu *menu, gpointer vlp );
41 VikLayerInterface vik_aggregate_layer_interface = {
45 &vikaggregatelayer_pixbuf,
57 (VikLayerFuncCreate) vik_aggregate_layer_create,
58 (VikLayerFuncRealize) vik_aggregate_layer_realize,
59 (VikLayerFuncPostRead) NULL,
60 (VikLayerFuncFree) vik_aggregate_layer_free,
62 (VikLayerFuncProperties) NULL,
63 (VikLayerFuncDraw) vik_aggregate_layer_draw,
64 (VikLayerFuncChangeCoordMode) aggregate_layer_change_coord_mode,
66 (VikLayerFuncSetMenuItemsSelection) NULL,
67 (VikLayerFuncGetMenuItemsSelection) NULL,
69 (VikLayerFuncAddMenuItems) aggregate_layer_add_menu_items,
70 (VikLayerFuncSublayerAddMenuItems) NULL,
72 (VikLayerFuncSublayerRenameRequest) NULL,
73 (VikLayerFuncSublayerToggleVisible) NULL,
74 (VikLayerFuncSublayerTooltip) NULL,
75 (VikLayerFuncLayerTooltip) NULL,
76 (VikLayerFuncLayerSelected) NULL,
78 (VikLayerFuncMarshall) aggregate_layer_marshall,
79 (VikLayerFuncUnmarshall) aggregate_layer_unmarshall,
81 (VikLayerFuncSetParam) NULL,
82 (VikLayerFuncGetParam) NULL,
84 (VikLayerFuncReadFileData) NULL,
85 (VikLayerFuncWriteFileData) NULL,
87 (VikLayerFuncDeleteItem) NULL,
88 (VikLayerFuncCutItem) NULL,
89 (VikLayerFuncCopyItem) NULL,
90 (VikLayerFuncPasteItem) NULL,
91 (VikLayerFuncFreeCopiedItem) NULL,
92 (VikLayerFuncDragDropRequest) aggregate_layer_drag_drop_request,
94 (VikLayerFuncSelectClick) NULL,
95 (VikLayerFuncSelectMove) NULL,
96 (VikLayerFuncSelectRelease) NULL,
97 (VikLayerFuncSelectedViewportMenu) NULL,
100 struct _VikAggregateLayer {
104 GtkWidget *tracks_analysis_dialog;
107 GType vik_aggregate_layer_get_type ()
109 static GType val_type = 0;
113 static const GTypeInfo val_info =
115 sizeof (VikAggregateLayerClass),
116 NULL, /* base_init */
117 NULL, /* base_finalize */
118 NULL, /* class init */
119 NULL, /* class_finalize */
120 NULL, /* class_data */
121 sizeof (VikAggregateLayer),
123 NULL /* instance init */
125 val_type = g_type_register_static ( VIK_LAYER_TYPE, "VikAggregateLayer", &val_info, 0 );
131 VikAggregateLayer *vik_aggregate_layer_create (VikViewport *vp)
133 VikAggregateLayer *rv = vik_aggregate_layer_new ();
134 vik_layer_rename ( VIK_LAYER(rv), vik_aggregate_layer_interface.name );
138 static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *datalen )
140 GList *child = val->children;
141 VikLayer *child_layer;
144 GByteArray* b = g_byte_array_new ();
147 #define alm_append(obj, sz) \
149 g_byte_array_append ( b, (guint8 *)&len, sizeof(len) ); \
150 g_byte_array_append ( b, (guint8 *)(obj), len );
152 vik_layer_marshall_params(VIK_LAYER(val), &ld, &ll);
157 child_layer = VIK_LAYER(child->data);
158 vik_layer_marshall ( child_layer, &ld, &ll );
167 g_byte_array_free(b, FALSE);
171 static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
173 #define alm_size (*(gint *)data)
175 len -= sizeof(gint) + alm_size; \
176 data += sizeof(gint) + alm_size;
178 VikAggregateLayer *rv = vik_aggregate_layer_new();
179 VikLayer *child_layer;
181 vik_layer_unmarshall_params ( VIK_LAYER(rv), data+sizeof(gint), alm_size, vvp );
185 child_layer = vik_layer_unmarshall ( data + sizeof(gint), alm_size, vvp );
187 rv->children = g_list_append ( rv->children, child_layer );
188 g_signal_connect_swapped ( G_OBJECT(child_layer), "update", G_CALLBACK(vik_layer_emit_update_secondary), rv );
192 // g_print("aggregate_layer_unmarshall ended with len=%d\n", len);
198 VikAggregateLayer *vik_aggregate_layer_new ()
200 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( g_object_new ( VIK_AGGREGATE_LAYER_TYPE, NULL ) );
201 vik_layer_set_type ( VIK_LAYER(val), VIK_LAYER_AGGREGATE );
202 val->children = NULL;
206 void vik_aggregate_layer_insert_layer ( VikAggregateLayer *val, VikLayer *l, GtkTreeIter *replace_iter )
209 VikLayer *vl = VIK_LAYER(val);
211 // By default layers are inserted above the selected layer
212 gboolean put_above = TRUE;
214 // These types are 'base' types in that you what other information on top
215 if ( l->type == VIK_LAYER_MAPS || l->type == VIK_LAYER_DEM || l->type == VIK_LAYER_GEOREF )
220 vik_treeview_insert_layer ( vl->vt, &(vl->iter), &iter, l->name, val, put_above, l, l->type, l->type, replace_iter );
222 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
223 vik_layer_realize ( l, vl->vt, &iter );
225 if ( val->children == NULL )
226 vik_treeview_expand ( vl->vt, &(vl->iter) );
230 GList *theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, replace_iter ) );
232 val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone)+1 );
234 // Thus insert 'here' (so don't add 1)
235 val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone) );
237 // Effectively insert at 'end' of the list to match how displayed in the treeview
238 // - but since it is drawn from 'bottom first' it is actually the first in the child list
239 // This ordering is especially important if it is a map or similar type,
240 // which needs be drawn first for the layering draw method to work properly.
241 // ATM this only happens when a layer is drag/dropped to the end of an aggregate layer
242 val->children = g_list_prepend ( val->children, l );
244 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
248 * vik_aggregate_layer_add_layer:
249 * @allow_reordering: should be set for GUI interactions,
250 * whereas loading from a file needs strict ordering and so should be FALSE
252 void vik_aggregate_layer_add_layer ( VikAggregateLayer *val, VikLayer *l, gboolean allow_reordering )
255 VikLayer *vl = VIK_LAYER(val);
257 // By default layers go to the top
258 gboolean put_above = TRUE;
260 if ( allow_reordering ) {
261 // These types are 'base' types in that you what other information on top
262 if ( l->type == VIK_LAYER_MAPS || l->type == VIK_LAYER_DEM || l->type == VIK_LAYER_GEOREF )
268 vik_treeview_add_layer ( vl->vt, &(vl->iter), &iter, l->name, val, put_above, l, l->type, l->type);
270 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
271 vik_layer_realize ( l, vl->vt, &iter );
273 if ( val->children == NULL )
274 vik_treeview_expand ( vl->vt, &(vl->iter) );
278 val->children = g_list_append ( val->children, l );
280 val->children = g_list_prepend ( val->children, l );
282 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
285 void vik_aggregate_layer_move_layer ( VikAggregateLayer *val, GtkTreeIter *child_iter, gboolean up )
287 GList *theone, *first, *second;
288 VikLayer *vl = VIK_LAYER(val);
289 vik_treeview_move_item ( vl->vt, child_iter, up );
291 theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, child_iter ) );
293 g_assert ( theone != NULL );
295 /* the old switcheroo */
296 if ( up && theone->next )
299 second = theone->next;
301 else if ( !up && theone->prev )
303 first = theone->prev;
309 first->next = second->next;
310 second->prev = first->prev;
311 first->prev = second;
312 second->next = first;
314 /* second is now first */
317 second->prev->next = second;
319 first->next->prev = first;
321 if ( second->prev == NULL )
322 val->children = second;
325 /* Draw the aggregate layer. If vik viewport is in half_drawn mode, this means we are only
326 * to draw the layers above and including the trigger layer.
327 * To do this we don't draw any layers if in half drawn mode, unless we find the
328 * trigger layer, in which case we pull up the saved pixmap, turn off half drawn mode and
329 * start drawing layers.
330 * Also, if we were never in half drawn mode, we save a snapshot
331 * of the pixmap before drawing the trigger layer so we can use it again
334 void vik_aggregate_layer_draw ( VikAggregateLayer *val, VikViewport *vp )
336 GList *iter = val->children;
338 VikLayer *trigger = VIK_LAYER(vik_viewport_get_trigger( vp ));
340 vl = VIK_LAYER(iter->data);
341 if ( vl == trigger ) {
342 if ( vik_viewport_get_half_drawn ( vp ) ) {
343 vik_viewport_set_half_drawn ( vp, FALSE );
344 vik_viewport_snapshot_load( vp );
346 vik_viewport_snapshot_save( vp );
349 if ( vl->type == VIK_LAYER_AGGREGATE || vl->type == VIK_LAYER_GPS || ! vik_viewport_get_half_drawn( vp ) )
350 vik_layer_draw ( vl, vp );
355 static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode )
357 GList *iter = val->children;
360 vik_layer_change_coord_mode ( VIK_LAYER(iter->data), mode );
365 static void aggregate_layer_child_visible_toggle ( gpointer data[2] )
367 // Convert data back to correct types
368 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( data[0] );
369 VikLayersPanel *vlp = VIK_LAYERS_PANEL ( data[1] );
372 // Loop around all (child) layers applying visibility setting
373 // This does not descend the tree if there are aggregates within aggregrate - just the first level of layers held
374 GList *iter = val->children;
376 vl = VIK_LAYER ( iter->data );
377 vl->visible = !vl->visible;
378 // Also set checkbox on/off
379 vik_treeview_item_toggle_visible ( vik_layers_panel_get_treeview ( vlp ), &(vl->iter) );
382 // Redraw as view may have changed
383 vik_layer_emit_update ( VIK_LAYER ( val ) );
386 static void aggregate_layer_child_visible ( gpointer data[2], gboolean on_off)
388 // Convert data back to correct types
389 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( data[0] );
390 VikLayersPanel *vlp = VIK_LAYERS_PANEL ( data[1] );
393 // Loop around all (child) layers applying visibility setting
394 // This does not descend the tree if there are aggregates within aggregrate - just the first level of layers held
395 GList *iter = val->children;
397 vl = VIK_LAYER ( iter->data );
398 vl->visible = on_off;
399 // Also set checkbox on_off
400 vik_treeview_item_set_visible ( vik_layers_panel_get_treeview ( vlp ), &(vl->iter), on_off );
403 // Redraw as view may have changed
404 vik_layer_emit_update ( VIK_LAYER ( val ) );
407 static void aggregate_layer_child_visible_on ( gpointer data[2] )
409 aggregate_layer_child_visible ( data, TRUE );
412 static void aggregate_layer_child_visible_off ( gpointer data[2] )
414 aggregate_layer_child_visible ( data, FALSE );
418 * If order is true sort ascending, otherwise a descending sort
420 static gint sort_layer_compare ( gconstpointer a, gconstpointer b, gpointer order )
422 VikLayer *sa = (VikLayer *)a;
423 VikLayer *sb = (VikLayer *)b;
425 // Default ascending order
426 gint answer = g_strcmp0 ( sa->name, sb->name );
428 if ( GPOINTER_TO_INT(order) ) {
429 // Invert sort order for ascending order
436 static void aggregate_layer_sort_a2z ( gpointer lav[2] )
438 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(lav[0]);
439 vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_ALPHABETICAL_ASCENDING );
440 val->children = g_list_sort_with_data ( val->children, sort_layer_compare, GINT_TO_POINTER(TRUE) );
443 static void aggregate_layer_sort_z2a ( gpointer lav[2] )
445 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(lav[0]);
446 vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_ALPHABETICAL_DESCENDING );
447 val->children = g_list_sort_with_data ( val->children, sort_layer_compare, GINT_TO_POINTER(FALSE) );
451 * aggregate_layer_waypoint_create_list:
452 * @vl: The layer that should create the waypoint and layers list
453 * @user_data: Not used in this function
455 * Returns: A list of #vik_trw_waypoint_list_t
457 static GList* aggregate_layer_waypoint_create_list ( VikLayer *vl, gpointer user_data )
459 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
461 // Get all TRW layers
462 GList *layers = NULL;
463 layers = vik_aggregate_layer_get_all_layers_of_type ( val, layers, VIK_LAYER_TRW, TRUE );
465 // For each TRW layers keep adding the waypoints to build a list of all of them
466 GList *waypoints_and_layers = NULL;
467 layers = g_list_first ( layers );
469 GList *waypoints = NULL;
470 waypoints = g_list_concat ( waypoints, g_hash_table_get_values ( vik_trw_layer_get_waypoints( VIK_TRW_LAYER(layers->data) ) ) );
472 waypoints_and_layers = g_list_concat ( waypoints_and_layers, vik_trw_layer_build_waypoint_list_t ( VIK_TRW_LAYER(layers->data), waypoints ) );
474 layers = g_list_next ( layers );
476 g_list_free ( layers );
478 return waypoints_and_layers;
481 static void aggregate_layer_waypoint_list_dialog ( gpointer data[2] )
483 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(data[0]);
484 gchar *title = g_strdup_printf ( _("%s: Waypoint List"), VIK_LAYER(val)->name );
485 vik_trw_layer_waypoint_list_show_dialog ( title, VIK_LAYER(val), NULL, aggregate_layer_waypoint_create_list, TRUE );
490 * aggregate_layer_track_create_list:
491 * @vl: The layer that should create the track and layers list
492 * @user_data: Not used in this function
494 * Returns: A list of #vik_trw_track_list_t
496 static GList* aggregate_layer_track_create_list ( VikLayer *vl, gpointer user_data )
498 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
500 // Get all TRW layers
501 GList *layers = NULL;
502 layers = vik_aggregate_layer_get_all_layers_of_type ( val, layers, VIK_LAYER_TRW, TRUE );
504 // For each TRW layers keep adding the tracks and routes to build a list of all of them
505 GList *tracks_and_layers = NULL;
506 layers = g_list_first ( layers );
508 GList *tracks = NULL;
509 tracks = g_list_concat ( tracks, g_hash_table_get_values ( vik_trw_layer_get_tracks( VIK_TRW_LAYER(layers->data) ) ) );
510 tracks = g_list_concat ( tracks, g_hash_table_get_values ( vik_trw_layer_get_routes( VIK_TRW_LAYER(layers->data) ) ) );
512 tracks_and_layers = g_list_concat ( tracks_and_layers, vik_trw_layer_build_track_list_t ( VIK_TRW_LAYER(layers->data), tracks ) );
514 layers = g_list_next ( layers );
516 g_list_free ( layers );
518 return tracks_and_layers;
521 static void aggregate_layer_track_list_dialog ( gpointer data[2] )
523 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(data[0]);
524 gchar *title = g_strdup_printf ( _("%s: Track and Route List"), VIK_LAYER(val)->name );
525 vik_trw_layer_track_list_show_dialog ( title, VIK_LAYER(val), NULL, aggregate_layer_track_create_list, TRUE );
530 * aggregate_layer_analyse_close:
532 * Stuff to do on dialog closure
534 static void aggregate_layer_analyse_close ( GtkWidget *dialog, gint resp, VikLayer* vl )
536 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
537 gtk_widget_destroy ( dialog );
538 val->tracks_analysis_dialog = NULL;
541 static void aggregate_layer_analyse ( gpointer data[2] )
543 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(data[0]);
545 // There can only be one!
546 if ( val->tracks_analysis_dialog )
549 val->tracks_analysis_dialog = vik_trw_layer_analyse_this ( VIK_GTK_WINDOW_FROM_LAYER(VIK_LAYER(val)),
550 VIK_LAYER(val)->name,
553 aggregate_layer_track_create_list,
554 aggregate_layer_analyse_close );
557 static void aggregate_layer_add_menu_items ( VikAggregateLayer *val, GtkMenu *menu, gpointer vlp )
559 // Data to pass on in menu functions
560 static gpointer data[2];
564 GtkWidget *item = gtk_menu_item_new();
565 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
566 gtk_widget_show ( item );
568 GtkWidget *vis_submenu = gtk_menu_new ();
569 item = gtk_menu_item_new_with_mnemonic ( _("_Visibility") );
570 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
571 gtk_widget_show ( item );
572 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), vis_submenu );
574 item = gtk_image_menu_item_new_with_mnemonic ( _("_Show All") );
575 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_APPLY, GTK_ICON_SIZE_MENU) );
576 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_on), data );
577 gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
578 gtk_widget_show ( item );
580 item = gtk_image_menu_item_new_with_mnemonic ( _("_Hide All") );
581 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU) );
582 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_off), data );
583 gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
584 gtk_widget_show ( item );
586 item = gtk_image_menu_item_new_with_mnemonic ( _("_Toggle") );
587 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_MENU) );
588 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_toggle), data );
589 gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
590 gtk_widget_show ( item );
592 GtkWidget *submenu_sort = gtk_menu_new ();
593 item = gtk_image_menu_item_new_with_mnemonic ( _("_Sort") );
594 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_MENU) );
595 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
596 gtk_widget_show ( item );
597 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu_sort );
599 item = gtk_image_menu_item_new_with_mnemonic ( _("Name _Ascending") );
600 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_ASCENDING, GTK_ICON_SIZE_MENU) );
601 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_a2z), data );
602 gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item );
603 gtk_widget_show ( item );
605 item = gtk_image_menu_item_new_with_mnemonic ( _("Name _Descending") );
606 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_DESCENDING, GTK_ICON_SIZE_MENU) );
607 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_z2a), data );
608 gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item );
609 gtk_widget_show ( item );
611 item = gtk_menu_item_new_with_mnemonic ( _("_Statistics") );
612 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_analyse), data );
613 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
614 gtk_widget_show ( item );
616 item = gtk_image_menu_item_new_with_mnemonic ( _("Track _List...") );
617 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_INDEX, GTK_ICON_SIZE_MENU) );
618 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_track_list_dialog), data );
619 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
620 gtk_widget_show ( item );
622 item = gtk_image_menu_item_new_with_mnemonic ( _("_Waypoint List...") );
623 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_INDEX, GTK_ICON_SIZE_MENU) );
624 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_waypoint_list_dialog), data );
625 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
626 gtk_widget_show ( item );
629 static void disconnect_layer_signal ( VikLayer *vl, VikAggregateLayer *val )
631 g_assert(DISCONNECT_UPDATE_SIGNAL(vl,val)==1);
634 void vik_aggregate_layer_free ( VikAggregateLayer *val )
636 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
637 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
638 g_list_free ( val->children );
639 if ( val->tracks_analysis_dialog != NULL )
640 gtk_widget_destroy ( val->tracks_analysis_dialog );
643 static void delete_layer_iter ( VikLayer *vl )
646 vik_treeview_item_delete ( vl->vt, &(vl->iter) );
649 void vik_aggregate_layer_clear ( VikAggregateLayer *val )
651 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
652 g_list_foreach ( val->children, (GFunc)(delete_layer_iter), NULL );
653 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
654 g_list_free ( val->children );
655 val->children = NULL;
658 gboolean vik_aggregate_layer_delete ( VikAggregateLayer *val, GtkTreeIter *iter )
660 VikLayer *l = VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, iter ) );
661 gboolean was_visible = l->visible;
663 vik_treeview_item_delete ( VIK_LAYER(val)->vt, iter );
664 val->children = g_list_remove ( val->children, l );
665 g_assert(DISCONNECT_UPDATE_SIGNAL(l,val)==1);
666 g_object_unref ( l );
672 /* returns 0 == we're good, 1 == didn't find any layers, 2 == got rejected */
673 guint vik_aggregate_layer_tool ( VikAggregateLayer *val, VikLayerTypeEnum layer_type, VikToolInterfaceFunc tool_func, GdkEventButton *event, VikViewport *vvp )
675 GList *iter = val->children;
676 gboolean found_rej = FALSE;
684 /* if this layer "accepts" the tool call */
685 if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == layer_type )
687 if ( tool_func ( VIK_LAYER(iter->data), event, vvp ) )
693 /* recursive -- try the same for the child aggregate layer. */
694 else if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == VIK_LAYER_AGGREGATE )
696 gint rv = vik_aggregate_layer_tool(VIK_AGGREGATE_LAYER(iter->data), layer_type, tool_func, event, vvp);
704 return found_rej ? 2 : 1; /* no one wanted to accept the tool call in this layer */
708 VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer *val, VikLayerTypeEnum type )
711 GList *ls = val->children;
719 VikLayer *vl = VIK_LAYER(ls->data);
720 if ( vl->visible && vl->type == type )
722 else if ( vl->visible && vl->type == VIK_LAYER_AGGREGATE )
724 rv = vik_aggregate_layer_get_top_visible_layer_of_type(VIK_AGGREGATE_LAYER(vl), type);
733 GList *vik_aggregate_layer_get_all_layers_of_type(VikAggregateLayer *val, GList *layers, VikLayerTypeEnum type, gboolean include_invisible)
736 GList *children = val->children;
741 // Where appropriate *don't* include non-visible layers
743 vl = VIK_LAYER(children->data);
744 if (vl->type == VIK_LAYER_AGGREGATE ) {
745 // Don't even consider invisible aggregrates, unless told to
746 if (vl->visible || include_invisible)
747 l = vik_aggregate_layer_get_all_layers_of_type(VIK_AGGREGATE_LAYER(children->data), l, type, include_invisible);
749 else if (vl->type == type) {
750 if (vl->visible || include_invisible)
751 l = g_list_prepend(l, children->data); /* now in top down order */
753 else if (type == VIK_LAYER_TRW) {
754 /* GPS layers contain TRW layers. cf with usage in file.c */
755 if (VIK_LAYER(children->data)->type == VIK_LAYER_GPS) {
756 if (VIK_LAYER(children->data)->visible || include_invisible) {
757 if (!vik_gps_layer_is_empty(VIK_GPS_LAYER(children->data))) {
759 can not use g_list_concat due to wrong copy method - crashes if used a couple times !!
760 l = g_list_concat (l, vik_gps_layer_get_children (VIK_GPS_LAYER(children->data)));
762 /* create own copy method instead :( */
763 GList *gps_trw_layers = (GList *)vik_gps_layer_get_children (VIK_GPS_LAYER(children->data));
764 int n_layers = g_list_length (gps_trw_layers);
766 for ( layer = 0; layer < n_layers; layer++) {
767 l = g_list_prepend(l, gps_trw_layers->data);
768 gps_trw_layers = gps_trw_layers->next;
770 g_list_free(gps_trw_layers);
775 children = children->next;
780 void vik_aggregate_layer_realize ( VikAggregateLayer *val, VikTreeview *vt, GtkTreeIter *layer_iter )
782 GList *i = val->children;
784 VikLayer *vl = VIK_LAYER(val);
788 vli = VIK_LAYER(i->data);
789 vik_treeview_add_layer ( vl->vt, layer_iter, &iter, vli->name, val, TRUE,
790 vli, vli->type, vli->type );
791 if ( ! vli->visible )
792 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
793 vik_layer_realize ( vli, vl->vt, &iter );
798 const GList *vik_aggregate_layer_get_children ( VikAggregateLayer *val )
800 return val->children;
803 gboolean vik_aggregate_layer_is_empty ( VikAggregateLayer *val )
810 static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path )
812 VikTreeview *vt = VIK_LAYER(val_src)->vt;
813 VikLayer *vl = vik_treeview_item_get_pointer(vt, src_item_iter);
814 GtkTreeIter dest_iter;
816 gboolean target_exists;
818 dp = gtk_tree_path_to_string(dest_path);
819 target_exists = vik_treeview_get_iter_from_path_str(vt, &dest_iter, dp);
821 /* vik_aggregate_layer_delete unrefs, but we don't want that here.
822 * we're still using the layer. */
824 vik_aggregate_layer_delete(val_src, src_item_iter);
827 vik_aggregate_layer_insert_layer(val_dest, vl, &dest_iter);
829 vik_aggregate_layer_insert_layer(val_dest, vl, NULL); /* append */