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 static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *len );
33 static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
34 static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode );
35 static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path );
37 static void aggregate_layer_add_menu_items ( VikAggregateLayer *val, GtkMenu *menu, gpointer vlp );
39 VikLayerInterface vik_aggregate_layer_interface = {
43 &vikaggregatelayer_pixbuf,
55 (VikLayerFuncCreate) vik_aggregate_layer_create,
56 (VikLayerFuncRealize) vik_aggregate_layer_realize,
57 (VikLayerFuncPostRead) NULL,
58 (VikLayerFuncFree) vik_aggregate_layer_free,
60 (VikLayerFuncProperties) NULL,
61 (VikLayerFuncDraw) vik_aggregate_layer_draw,
62 (VikLayerFuncChangeCoordMode) aggregate_layer_change_coord_mode,
64 (VikLayerFuncSetMenuItemsSelection) NULL,
65 (VikLayerFuncGetMenuItemsSelection) NULL,
67 (VikLayerFuncAddMenuItems) aggregate_layer_add_menu_items,
68 (VikLayerFuncSublayerAddMenuItems) NULL,
70 (VikLayerFuncSublayerRenameRequest) NULL,
71 (VikLayerFuncSublayerToggleVisible) NULL,
72 (VikLayerFuncSublayerTooltip) NULL,
73 (VikLayerFuncLayerTooltip) NULL,
74 (VikLayerFuncLayerSelected) NULL,
76 (VikLayerFuncMarshall) aggregate_layer_marshall,
77 (VikLayerFuncUnmarshall) aggregate_layer_unmarshall,
79 (VikLayerFuncSetParam) NULL,
80 (VikLayerFuncGetParam) NULL,
81 (VikLayerFuncChangeParam) NULL,
83 (VikLayerFuncReadFileData) NULL,
84 (VikLayerFuncWriteFileData) NULL,
86 (VikLayerFuncDeleteItem) NULL,
87 (VikLayerFuncCutItem) NULL,
88 (VikLayerFuncCopyItem) NULL,
89 (VikLayerFuncPasteItem) NULL,
90 (VikLayerFuncFreeCopiedItem) NULL,
91 (VikLayerFuncDragDropRequest) aggregate_layer_drag_drop_request,
93 (VikLayerFuncSelectClick) NULL,
94 (VikLayerFuncSelectMove) NULL,
95 (VikLayerFuncSelectRelease) NULL,
96 (VikLayerFuncSelectedViewportMenu) NULL,
99 struct _VikAggregateLayer {
103 GtkWidget *tracks_analysis_dialog;
106 GType vik_aggregate_layer_get_type ()
108 static GType val_type = 0;
112 static const GTypeInfo val_info =
114 sizeof (VikAggregateLayerClass),
115 NULL, /* base_init */
116 NULL, /* base_finalize */
117 NULL, /* class init */
118 NULL, /* class_finalize */
119 NULL, /* class_data */
120 sizeof (VikAggregateLayer),
122 NULL /* instance init */
124 val_type = g_type_register_static ( VIK_LAYER_TYPE, "VikAggregateLayer", &val_info, 0 );
130 VikAggregateLayer *vik_aggregate_layer_create (VikViewport *vp)
132 VikAggregateLayer *rv = vik_aggregate_layer_new ();
133 vik_layer_rename ( VIK_LAYER(rv), vik_aggregate_layer_interface.name );
137 static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *datalen )
139 GList *child = val->children;
140 VikLayer *child_layer;
143 GByteArray* b = g_byte_array_new ();
146 #define alm_append(obj, sz) \
148 g_byte_array_append ( b, (guint8 *)&len, sizeof(len) ); \
149 g_byte_array_append ( b, (guint8 *)(obj), len );
151 vik_layer_marshall_params(VIK_LAYER(val), &ld, &ll);
156 child_layer = VIK_LAYER(child->data);
157 vik_layer_marshall ( child_layer, &ld, &ll );
166 g_byte_array_free(b, FALSE);
170 static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
172 #define alm_size (*(gint *)data)
174 len -= sizeof(gint) + alm_size; \
175 data += sizeof(gint) + alm_size;
177 VikAggregateLayer *rv = vik_aggregate_layer_new();
178 VikLayer *child_layer;
180 vik_layer_unmarshall_params ( VIK_LAYER(rv), data+sizeof(gint), alm_size, vvp );
184 child_layer = vik_layer_unmarshall ( data + sizeof(gint), alm_size, vvp );
186 rv->children = g_list_append ( rv->children, child_layer );
187 g_signal_connect_swapped ( G_OBJECT(child_layer), "update", G_CALLBACK(vik_layer_emit_update_secondary), rv );
191 // g_print("aggregate_layer_unmarshall ended with len=%d\n", len);
197 VikAggregateLayer *vik_aggregate_layer_new ()
199 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( g_object_new ( VIK_AGGREGATE_LAYER_TYPE, NULL ) );
200 vik_layer_set_type ( VIK_LAYER(val), VIK_LAYER_AGGREGATE );
201 val->children = NULL;
205 void vik_aggregate_layer_insert_layer ( VikAggregateLayer *val, VikLayer *l, GtkTreeIter *replace_iter )
208 VikLayer *vl = VIK_LAYER(val);
210 // By default layers are inserted above the selected layer
211 gboolean put_above = TRUE;
213 // These types are 'base' types in that you what other information on top
214 if ( l->type == VIK_LAYER_MAPS || l->type == VIK_LAYER_DEM || l->type == VIK_LAYER_GEOREF )
219 vik_treeview_insert_layer ( vl->vt, &(vl->iter), &iter, l->name, val, put_above, l, l->type, l->type, replace_iter );
221 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
222 vik_layer_realize ( l, vl->vt, &iter );
224 if ( val->children == NULL )
225 vik_treeview_expand ( vl->vt, &(vl->iter) );
229 GList *theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, replace_iter ) );
231 val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone)+1 );
233 // Thus insert 'here' (so don't add 1)
234 val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone) );
236 // Effectively insert at 'end' of the list to match how displayed in the treeview
237 // - but since it is drawn from 'bottom first' it is actually the first in the child list
238 // This ordering is especially important if it is a map or similar type,
239 // which needs be drawn first for the layering draw method to work properly.
240 // ATM this only happens when a layer is drag/dropped to the end of an aggregate layer
241 val->children = g_list_prepend ( val->children, l );
243 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
247 * vik_aggregate_layer_add_layer:
248 * @allow_reordering: should be set for GUI interactions,
249 * whereas loading from a file needs strict ordering and so should be FALSE
251 void vik_aggregate_layer_add_layer ( VikAggregateLayer *val, VikLayer *l, gboolean allow_reordering )
254 VikLayer *vl = VIK_LAYER(val);
256 // By default layers go to the top
257 gboolean put_above = TRUE;
259 if ( allow_reordering ) {
260 // These types are 'base' types in that you what other information on top
261 if ( l->type == VIK_LAYER_MAPS || l->type == VIK_LAYER_DEM || l->type == VIK_LAYER_GEOREF )
267 vik_treeview_add_layer ( vl->vt, &(vl->iter), &iter, l->name, val, put_above, l, l->type, l->type);
269 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
270 vik_layer_realize ( l, vl->vt, &iter );
272 if ( val->children == NULL )
273 vik_treeview_expand ( vl->vt, &(vl->iter) );
277 val->children = g_list_append ( val->children, l );
279 val->children = g_list_prepend ( val->children, l );
281 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
284 void vik_aggregate_layer_move_layer ( VikAggregateLayer *val, GtkTreeIter *child_iter, gboolean up )
286 GList *theone, *first, *second;
287 VikLayer *vl = VIK_LAYER(val);
288 vik_treeview_move_item ( vl->vt, child_iter, up );
290 theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, child_iter ) );
292 g_assert ( theone != NULL );
294 /* the old switcheroo */
295 if ( up && theone->next )
298 second = theone->next;
300 else if ( !up && theone->prev )
302 first = theone->prev;
308 first->next = second->next;
309 second->prev = first->prev;
310 first->prev = second;
311 second->next = first;
313 /* second is now first */
316 second->prev->next = second;
318 first->next->prev = first;
320 if ( second->prev == NULL )
321 val->children = second;
324 /* Draw the aggregate layer. If vik viewport is in half_drawn mode, this means we are only
325 * to draw the layers above and including the trigger layer.
326 * To do this we don't draw any layers if in half drawn mode, unless we find the
327 * trigger layer, in which case we pull up the saved pixmap, turn off half drawn mode and
328 * start drawing layers.
329 * Also, if we were never in half drawn mode, we save a snapshot
330 * of the pixmap before drawing the trigger layer so we can use it again
333 void vik_aggregate_layer_draw ( VikAggregateLayer *val, VikViewport *vp )
335 GList *iter = val->children;
337 VikLayer *trigger = VIK_LAYER(vik_viewport_get_trigger( vp ));
339 vl = VIK_LAYER(iter->data);
340 if ( vl == trigger ) {
341 if ( vik_viewport_get_half_drawn ( vp ) ) {
342 vik_viewport_set_half_drawn ( vp, FALSE );
343 vik_viewport_snapshot_load( vp );
345 vik_viewport_snapshot_save( vp );
348 if ( vl->type == VIK_LAYER_AGGREGATE || vl->type == VIK_LAYER_GPS || ! vik_viewport_get_half_drawn( vp ) )
349 vik_layer_draw ( vl, vp );
354 static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode )
356 GList *iter = val->children;
359 vik_layer_change_coord_mode ( VIK_LAYER(iter->data), mode );
364 // A slightly better way of defining the menu callback information
365 // This should be easier to extend/rework compared to previously
372 typedef gpointer menu_array_values[MA_LAST];
374 static void aggregate_layer_child_visible_toggle ( menu_array_values values )
376 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
377 VikLayersPanel *vlp = VIK_LAYERS_PANEL ( values[MA_VLP] );
380 // Loop around all (child) layers applying visibility setting
381 // This does not descend the tree if there are aggregates within aggregrate - just the first level of layers held
382 GList *iter = val->children;
384 vl = VIK_LAYER ( iter->data );
385 vl->visible = !vl->visible;
386 // Also set checkbox on/off
387 vik_treeview_item_toggle_visible ( vik_layers_panel_get_treeview ( vlp ), &(vl->iter) );
390 // Redraw as view may have changed
391 vik_layer_emit_update ( VIK_LAYER ( val ) );
394 static void aggregate_layer_child_visible ( menu_array_values values, gboolean on_off)
396 // Convert data back to correct types
397 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
398 VikLayersPanel *vlp = VIK_LAYERS_PANEL ( values[MA_VLP] );
401 // Loop around all (child) layers applying visibility setting
402 // This does not descend the tree if there are aggregates within aggregrate - just the first level of layers held
403 GList *iter = val->children;
405 vl = VIK_LAYER ( iter->data );
406 vl->visible = on_off;
407 // Also set checkbox on_off
408 vik_treeview_item_set_visible ( vik_layers_panel_get_treeview ( vlp ), &(vl->iter), on_off );
411 // Redraw as view may have changed
412 vik_layer_emit_update ( VIK_LAYER ( val ) );
415 static void aggregate_layer_child_visible_on ( menu_array_values values )
417 aggregate_layer_child_visible ( values, TRUE );
420 static void aggregate_layer_child_visible_off ( menu_array_values values )
422 aggregate_layer_child_visible ( values, FALSE );
426 * If order is true sort ascending, otherwise a descending sort
428 static gint sort_layer_compare ( gconstpointer a, gconstpointer b, gpointer order )
430 VikLayer *sa = (VikLayer *)a;
431 VikLayer *sb = (VikLayer *)b;
433 // Default ascending order
434 gint answer = g_strcmp0 ( sa->name, sb->name );
436 if ( GPOINTER_TO_INT(order) ) {
437 // Invert sort order for ascending order
444 static void aggregate_layer_sort_a2z ( menu_array_values values )
446 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
447 vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_ALPHABETICAL_ASCENDING );
448 val->children = g_list_sort_with_data ( val->children, sort_layer_compare, GINT_TO_POINTER(TRUE) );
451 static void aggregate_layer_sort_z2a ( menu_array_values values )
453 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
454 vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_ALPHABETICAL_DESCENDING );
455 val->children = g_list_sort_with_data ( val->children, sort_layer_compare, GINT_TO_POINTER(FALSE) );
459 * aggregate_layer_waypoint_create_list:
460 * @vl: The layer that should create the waypoint and layers list
461 * @user_data: Not used in this function
463 * Returns: A list of #vik_trw_waypoint_list_t
465 static GList* aggregate_layer_waypoint_create_list ( VikLayer *vl, gpointer user_data )
467 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
469 // Get all TRW layers
470 GList *layers = NULL;
471 layers = vik_aggregate_layer_get_all_layers_of_type ( val, layers, VIK_LAYER_TRW, TRUE );
473 // For each TRW layers keep adding the waypoints to build a list of all of them
474 GList *waypoints_and_layers = NULL;
475 layers = g_list_first ( layers );
477 GList *waypoints = NULL;
478 waypoints = g_list_concat ( waypoints, g_hash_table_get_values ( vik_trw_layer_get_waypoints( VIK_TRW_LAYER(layers->data) ) ) );
480 waypoints_and_layers = g_list_concat ( waypoints_and_layers, vik_trw_layer_build_waypoint_list_t ( VIK_TRW_LAYER(layers->data), waypoints ) );
482 layers = g_list_next ( layers );
484 g_list_free ( layers );
486 return waypoints_and_layers;
489 static void aggregate_layer_waypoint_list_dialog ( menu_array_values values )
491 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
492 gchar *title = g_strdup_printf ( _("%s: Waypoint List"), VIK_LAYER(val)->name );
493 vik_trw_layer_waypoint_list_show_dialog ( title, VIK_LAYER(val), NULL, aggregate_layer_waypoint_create_list, TRUE );
498 * Search all TrackWaypoint layers in this aggregate layer for an item on the user specified date
500 static void aggregate_layer_search_date ( menu_array_values values )
502 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
504 gchar *date_str = a_dialog_get_date ( VIK_GTK_WINDOW_FROM_LAYER(val), _("Search by Date") );
509 VikViewport *vvp = vik_window_viewport ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(val)) );
512 gl = vik_aggregate_layer_get_all_layers_of_type ( val, gl, VIK_LAYER_TRW, TRUE );
513 gboolean found = FALSE;
514 // Search tracks first
515 while ( gl && !found ) {
516 // Make it auto select the item if found
517 found = vik_trw_layer_find_date ( VIK_TRW_LAYER(gl->data), date_str, &position, vvp, TRUE, TRUE );
518 gl = g_list_next ( gl );
522 // Reset and try on Waypoints
524 gl = vik_aggregate_layer_get_all_layers_of_type ( val, gl, VIK_LAYER_TRW, TRUE );
525 while ( gl && !found ) {
526 // Make it auto select the item if found
527 found = vik_trw_layer_find_date ( VIK_TRW_LAYER(gl->data), date_str, &position, vvp, FALSE, TRUE );
528 gl = g_list_next ( gl );
534 a_dialog_info_msg ( VIK_GTK_WINDOW_FROM_LAYER(val), _("No items found with the requested date.") );
539 * aggregate_layer_track_create_list:
540 * @vl: The layer that should create the track and layers list
541 * @user_data: Not used in this function
543 * Returns: A list of #vik_trw_track_list_t
545 static GList* aggregate_layer_track_create_list ( VikLayer *vl, gpointer user_data )
547 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
549 // Get all TRW layers
550 GList *layers = NULL;
551 layers = vik_aggregate_layer_get_all_layers_of_type ( val, layers, VIK_LAYER_TRW, TRUE );
553 // For each TRW layers keep adding the tracks and routes to build a list of all of them
554 GList *tracks_and_layers = NULL;
555 layers = g_list_first ( layers );
557 GList *tracks = NULL;
558 tracks = g_list_concat ( tracks, g_hash_table_get_values ( vik_trw_layer_get_tracks( VIK_TRW_LAYER(layers->data) ) ) );
559 tracks = g_list_concat ( tracks, g_hash_table_get_values ( vik_trw_layer_get_routes( VIK_TRW_LAYER(layers->data) ) ) );
561 tracks_and_layers = g_list_concat ( tracks_and_layers, vik_trw_layer_build_track_list_t ( VIK_TRW_LAYER(layers->data), tracks ) );
563 layers = g_list_next ( layers );
565 g_list_free ( layers );
567 return tracks_and_layers;
570 static void aggregate_layer_track_list_dialog ( menu_array_values values )
572 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
573 gchar *title = g_strdup_printf ( _("%s: Track and Route List"), VIK_LAYER(val)->name );
574 vik_trw_layer_track_list_show_dialog ( title, VIK_LAYER(val), NULL, aggregate_layer_track_create_list, TRUE );
579 * aggregate_layer_analyse_close:
581 * Stuff to do on dialog closure
583 static void aggregate_layer_analyse_close ( GtkWidget *dialog, gint resp, VikLayer* vl )
585 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
586 gtk_widget_destroy ( dialog );
587 val->tracks_analysis_dialog = NULL;
590 static void aggregate_layer_analyse ( menu_array_values values )
592 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
594 // There can only be one!
595 if ( val->tracks_analysis_dialog )
598 val->tracks_analysis_dialog = vik_trw_layer_analyse_this ( VIK_GTK_WINDOW_FROM_LAYER(VIK_LAYER(val)),
599 VIK_LAYER(val)->name,
602 aggregate_layer_track_create_list,
603 aggregate_layer_analyse_close );
606 static void aggregate_layer_add_menu_items ( VikAggregateLayer *val, GtkMenu *menu, gpointer vlp )
608 // Data to pass on in menu functions
609 static menu_array_values values;
610 values[MA_VAL] = val;
611 values[MA_VLP] = vlp;
613 GtkWidget *item = gtk_menu_item_new();
614 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
615 gtk_widget_show ( item );
617 GtkWidget *vis_submenu = gtk_menu_new ();
618 item = gtk_menu_item_new_with_mnemonic ( _("_Visibility") );
619 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
620 gtk_widget_show ( item );
621 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), vis_submenu );
623 item = gtk_image_menu_item_new_with_mnemonic ( _("_Show All") );
624 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_APPLY, GTK_ICON_SIZE_MENU) );
625 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_on), values );
626 gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
627 gtk_widget_show ( item );
629 item = gtk_image_menu_item_new_with_mnemonic ( _("_Hide All") );
630 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU) );
631 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_off), values );
632 gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
633 gtk_widget_show ( item );
635 item = gtk_image_menu_item_new_with_mnemonic ( _("_Toggle") );
636 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_MENU) );
637 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_toggle), values );
638 gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
639 gtk_widget_show ( item );
641 GtkWidget *submenu_sort = gtk_menu_new ();
642 item = gtk_image_menu_item_new_with_mnemonic ( _("_Sort") );
643 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_MENU) );
644 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
645 gtk_widget_show ( item );
646 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu_sort );
648 item = gtk_image_menu_item_new_with_mnemonic ( _("Name _Ascending") );
649 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_ASCENDING, GTK_ICON_SIZE_MENU) );
650 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_a2z), values );
651 gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item );
652 gtk_widget_show ( item );
654 item = gtk_image_menu_item_new_with_mnemonic ( _("Name _Descending") );
655 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_DESCENDING, GTK_ICON_SIZE_MENU) );
656 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_z2a), values );
657 gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item );
658 gtk_widget_show ( item );
660 item = gtk_menu_item_new_with_mnemonic ( _("_Statistics") );
661 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_analyse), values );
662 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
663 gtk_widget_show ( item );
665 item = gtk_image_menu_item_new_with_mnemonic ( _("Track _List...") );
666 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_INDEX, GTK_ICON_SIZE_MENU) );
667 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_track_list_dialog), values );
668 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
669 gtk_widget_show ( item );
671 item = gtk_image_menu_item_new_with_mnemonic ( _("_Waypoint List...") );
672 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_INDEX, GTK_ICON_SIZE_MENU) );
673 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_waypoint_list_dialog), values );
674 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
675 gtk_widget_show ( item );
677 item = gtk_image_menu_item_new_with_mnemonic ( _("Search _by Date...") );
678 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU) );
679 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_search_date), values );
680 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
681 gtk_widget_set_tooltip_text (item, _("Find the first item with a specified date"));
682 gtk_widget_show ( item );
685 static void disconnect_layer_signal ( VikLayer *vl, VikAggregateLayer *val )
687 guint number_handlers = g_signal_handlers_disconnect_matched(vl, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, val);
688 if ( number_handlers != 1 )
689 g_critical ("%s: Unexpected number of disconnect handlers: %d", __FUNCTION__, number_handlers);
692 void vik_aggregate_layer_free ( VikAggregateLayer *val )
694 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
695 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
696 g_list_free ( val->children );
697 if ( val->tracks_analysis_dialog != NULL )
698 gtk_widget_destroy ( val->tracks_analysis_dialog );
701 static void delete_layer_iter ( VikLayer *vl )
704 vik_treeview_item_delete ( vl->vt, &(vl->iter) );
707 void vik_aggregate_layer_clear ( VikAggregateLayer *val )
709 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
710 g_list_foreach ( val->children, (GFunc)(delete_layer_iter), NULL );
711 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
712 g_list_free ( val->children );
713 val->children = NULL;
716 gboolean vik_aggregate_layer_delete ( VikAggregateLayer *val, GtkTreeIter *iter )
718 VikLayer *l = VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, iter ) );
719 gboolean was_visible = l->visible;
721 vik_treeview_item_delete ( VIK_LAYER(val)->vt, iter );
722 val->children = g_list_remove ( val->children, l );
723 disconnect_layer_signal ( l, val );
724 g_object_unref ( l );
730 /* returns 0 == we're good, 1 == didn't find any layers, 2 == got rejected */
731 guint vik_aggregate_layer_tool ( VikAggregateLayer *val, VikLayerTypeEnum layer_type, VikToolInterfaceFunc tool_func, GdkEventButton *event, VikViewport *vvp )
733 GList *iter = val->children;
734 gboolean found_rej = FALSE;
742 /* if this layer "accepts" the tool call */
743 if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == layer_type )
745 if ( tool_func ( VIK_LAYER(iter->data), event, vvp ) )
751 /* recursive -- try the same for the child aggregate layer. */
752 else if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == VIK_LAYER_AGGREGATE )
754 gint rv = vik_aggregate_layer_tool(VIK_AGGREGATE_LAYER(iter->data), layer_type, tool_func, event, vvp);
762 return found_rej ? 2 : 1; /* no one wanted to accept the tool call in this layer */
766 VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer *val, VikLayerTypeEnum type )
769 GList *ls = val->children;
777 VikLayer *vl = VIK_LAYER(ls->data);
778 if ( vl->visible && vl->type == type )
780 else if ( vl->visible && vl->type == VIK_LAYER_AGGREGATE )
782 rv = vik_aggregate_layer_get_top_visible_layer_of_type(VIK_AGGREGATE_LAYER(vl), type);
791 GList *vik_aggregate_layer_get_all_layers_of_type(VikAggregateLayer *val, GList *layers, VikLayerTypeEnum type, gboolean include_invisible)
794 GList *children = val->children;
799 // Where appropriate *don't* include non-visible layers
801 vl = VIK_LAYER(children->data);
802 if (vl->type == VIK_LAYER_AGGREGATE ) {
803 // Don't even consider invisible aggregrates, unless told to
804 if (vl->visible || include_invisible)
805 l = vik_aggregate_layer_get_all_layers_of_type(VIK_AGGREGATE_LAYER(children->data), l, type, include_invisible);
807 else if (vl->type == type) {
808 if (vl->visible || include_invisible)
809 l = g_list_prepend(l, children->data); /* now in top down order */
811 else if (type == VIK_LAYER_TRW) {
812 /* GPS layers contain TRW layers. cf with usage in file.c */
813 if (VIK_LAYER(children->data)->type == VIK_LAYER_GPS) {
814 if (VIK_LAYER(children->data)->visible || include_invisible) {
815 if (!vik_gps_layer_is_empty(VIK_GPS_LAYER(children->data))) {
817 can not use g_list_concat due to wrong copy method - crashes if used a couple times !!
818 l = g_list_concat (l, vik_gps_layer_get_children (VIK_GPS_LAYER(children->data)));
820 /* create own copy method instead :( */
821 GList *gps_trw_layers = (GList *)vik_gps_layer_get_children (VIK_GPS_LAYER(children->data));
822 int n_layers = g_list_length (gps_trw_layers);
824 for ( layer = 0; layer < n_layers; layer++) {
825 l = g_list_prepend(l, gps_trw_layers->data);
826 gps_trw_layers = gps_trw_layers->next;
828 g_list_free(gps_trw_layers);
833 children = children->next;
838 void vik_aggregate_layer_realize ( VikAggregateLayer *val, VikTreeview *vt, GtkTreeIter *layer_iter )
840 GList *i = val->children;
842 VikLayer *vl = VIK_LAYER(val);
846 vli = VIK_LAYER(i->data);
847 vik_treeview_add_layer ( vl->vt, layer_iter, &iter, vli->name, val, TRUE,
848 vli, vli->type, vli->type );
849 if ( ! vli->visible )
850 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
851 vik_layer_realize ( vli, vl->vt, &iter );
856 const GList *vik_aggregate_layer_get_children ( VikAggregateLayer *val )
858 return val->children;
861 gboolean vik_aggregate_layer_is_empty ( VikAggregateLayer *val )
868 static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path )
870 VikTreeview *vt = VIK_LAYER(val_src)->vt;
871 VikLayer *vl = vik_treeview_item_get_pointer(vt, src_item_iter);
872 GtkTreeIter dest_iter;
874 gboolean target_exists;
876 dp = gtk_tree_path_to_string(dest_path);
877 target_exists = vik_treeview_get_iter_from_path_str(vt, &dest_iter, dp);
879 /* vik_aggregate_layer_delete unrefs, but we don't want that here.
880 * we're still using the layer. */
882 vik_aggregate_layer_delete(val_src, src_item_iter);
885 vik_aggregate_layer_insert_layer(val_dest, vl, &dest_iter);
887 vik_aggregate_layer_insert_layer(val_dest, vl, NULL); /* append */