2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2013-2015, 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 );
36 static const gchar* aggregate_layer_tooltip ( VikAggregateLayer *val );
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 (VikLayerFuncGetTimestamp) NULL,
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) aggregate_layer_tooltip,
76 (VikLayerFuncLayerSelected) NULL,
78 (VikLayerFuncMarshall) aggregate_layer_marshall,
79 (VikLayerFuncUnmarshall) aggregate_layer_unmarshall,
81 (VikLayerFuncSetParam) NULL,
82 (VikLayerFuncGetParam) NULL,
83 (VikLayerFuncChangeParam) NULL,
85 (VikLayerFuncReadFileData) NULL,
86 (VikLayerFuncWriteFileData) NULL,
88 (VikLayerFuncDeleteItem) NULL,
89 (VikLayerFuncCutItem) NULL,
90 (VikLayerFuncCopyItem) NULL,
91 (VikLayerFuncPasteItem) NULL,
92 (VikLayerFuncFreeCopiedItem) NULL,
93 (VikLayerFuncDragDropRequest) aggregate_layer_drag_drop_request,
95 (VikLayerFuncSelectClick) NULL,
96 (VikLayerFuncSelectMove) NULL,
97 (VikLayerFuncSelectRelease) NULL,
98 (VikLayerFuncSelectedViewportMenu) NULL,
101 struct _VikAggregateLayer {
105 GtkWidget *tracks_analysis_dialog;
108 GType vik_aggregate_layer_get_type ()
110 static GType val_type = 0;
114 static const GTypeInfo val_info =
116 sizeof (VikAggregateLayerClass),
117 NULL, /* base_init */
118 NULL, /* base_finalize */
119 NULL, /* class init */
120 NULL, /* class_finalize */
121 NULL, /* class_data */
122 sizeof (VikAggregateLayer),
124 NULL /* instance init */
126 val_type = g_type_register_static ( VIK_LAYER_TYPE, "VikAggregateLayer", &val_info, 0 );
132 VikAggregateLayer *vik_aggregate_layer_create (VikViewport *vp)
134 VikAggregateLayer *rv = vik_aggregate_layer_new ();
135 vik_layer_rename ( VIK_LAYER(rv), vik_aggregate_layer_interface.name );
139 static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *datalen )
141 GList *child = val->children;
142 VikLayer *child_layer;
145 GByteArray* b = g_byte_array_new ();
148 #define alm_append(obj, sz) \
150 g_byte_array_append ( b, (guint8 *)&len, sizeof(len) ); \
151 g_byte_array_append ( b, (guint8 *)(obj), len );
153 vik_layer_marshall_params(VIK_LAYER(val), &ld, &ll);
158 child_layer = VIK_LAYER(child->data);
159 vik_layer_marshall ( child_layer, &ld, &ll );
168 g_byte_array_free(b, FALSE);
172 static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
174 #define alm_size (*(gint *)data)
176 len -= sizeof(gint) + alm_size; \
177 data += sizeof(gint) + alm_size;
179 VikAggregateLayer *rv = vik_aggregate_layer_new();
180 VikLayer *child_layer;
182 vik_layer_unmarshall_params ( VIK_LAYER(rv), data+sizeof(gint), alm_size, vvp );
186 child_layer = vik_layer_unmarshall ( data + sizeof(gint), alm_size, vvp );
188 rv->children = g_list_append ( rv->children, child_layer );
189 g_signal_connect_swapped ( G_OBJECT(child_layer), "update", G_CALLBACK(vik_layer_emit_update_secondary), rv );
193 // g_print("aggregate_layer_unmarshall ended with len=%d\n", len);
199 VikAggregateLayer *vik_aggregate_layer_new ()
201 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( g_object_new ( VIK_AGGREGATE_LAYER_TYPE, NULL ) );
202 vik_layer_set_type ( VIK_LAYER(val), VIK_LAYER_AGGREGATE );
203 val->children = NULL;
207 void vik_aggregate_layer_insert_layer ( VikAggregateLayer *val, VikLayer *l, GtkTreeIter *replace_iter )
210 VikLayer *vl = VIK_LAYER(val);
212 // By default layers are inserted above the selected layer
213 gboolean put_above = TRUE;
215 // These types are 'base' types in that you what other information on top
216 if ( l->type == VIK_LAYER_MAPS || l->type == VIK_LAYER_DEM || l->type == VIK_LAYER_GEOREF )
221 vik_treeview_insert_layer ( vl->vt, &(vl->iter), &iter, l->name, val, put_above, l, l->type, l->type, replace_iter, vik_layer_get_timestamp(l) );
223 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
224 vik_layer_realize ( l, vl->vt, &iter );
226 if ( val->children == NULL )
227 vik_treeview_expand ( vl->vt, &(vl->iter) );
231 GList *theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, replace_iter ) );
233 val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone)+1 );
235 // Thus insert 'here' (so don't add 1)
236 val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone) );
238 // Effectively insert at 'end' of the list to match how displayed in the treeview
239 // - but since it is drawn from 'bottom first' it is actually the first in the child list
240 // This ordering is especially important if it is a map or similar type,
241 // which needs be drawn first for the layering draw method to work properly.
242 // ATM this only happens when a layer is drag/dropped to the end of an aggregate layer
243 val->children = g_list_prepend ( val->children, l );
245 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
249 * vik_aggregate_layer_add_layer:
250 * @allow_reordering: should be set for GUI interactions,
251 * whereas loading from a file needs strict ordering and so should be FALSE
253 void vik_aggregate_layer_add_layer ( VikAggregateLayer *val, VikLayer *l, gboolean allow_reordering )
256 VikLayer *vl = VIK_LAYER(val);
258 // By default layers go to the top
259 gboolean put_above = TRUE;
261 if ( allow_reordering ) {
262 // These types are 'base' types in that you what other information on top
263 if ( l->type == VIK_LAYER_MAPS || l->type == VIK_LAYER_DEM || l->type == VIK_LAYER_GEOREF )
269 vik_treeview_add_layer ( vl->vt, &(vl->iter), &iter, l->name, val, put_above, l, l->type, l->type, vik_layer_get_timestamp(l) );
271 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
272 vik_layer_realize ( l, vl->vt, &iter );
274 if ( val->children == NULL )
275 vik_treeview_expand ( vl->vt, &(vl->iter) );
279 val->children = g_list_append ( val->children, l );
281 val->children = g_list_prepend ( val->children, l );
283 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
286 void vik_aggregate_layer_move_layer ( VikAggregateLayer *val, GtkTreeIter *child_iter, gboolean up )
288 GList *theone, *first, *second;
289 VikLayer *vl = VIK_LAYER(val);
290 vik_treeview_move_item ( vl->vt, child_iter, up );
292 theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, child_iter ) );
294 g_assert ( theone != NULL );
296 /* the old switcheroo */
297 if ( up && theone->next )
300 second = theone->next;
302 else if ( !up && theone->prev )
304 first = theone->prev;
310 first->next = second->next;
311 second->prev = first->prev;
312 first->prev = second;
313 second->next = first;
315 /* second is now first */
318 second->prev->next = second;
320 first->next->prev = first;
322 if ( second->prev == NULL )
323 val->children = second;
326 /* Draw the aggregate layer. If vik viewport is in half_drawn mode, this means we are only
327 * to draw the layers above and including the trigger layer.
328 * To do this we don't draw any layers if in half drawn mode, unless we find the
329 * trigger layer, in which case we pull up the saved pixmap, turn off half drawn mode and
330 * start drawing layers.
331 * Also, if we were never in half drawn mode, we save a snapshot
332 * of the pixmap before drawing the trigger layer so we can use it again
335 void vik_aggregate_layer_draw ( VikAggregateLayer *val, VikViewport *vp )
337 GList *iter = val->children;
339 VikLayer *trigger = VIK_LAYER(vik_viewport_get_trigger( vp ));
341 vl = VIK_LAYER(iter->data);
342 if ( vl == trigger ) {
343 if ( vik_viewport_get_half_drawn ( vp ) ) {
344 vik_viewport_set_half_drawn ( vp, FALSE );
345 vik_viewport_snapshot_load( vp );
347 vik_viewport_snapshot_save( vp );
350 if ( vl->type == VIK_LAYER_AGGREGATE || vl->type == VIK_LAYER_GPS || ! vik_viewport_get_half_drawn( vp ) )
351 vik_layer_draw ( vl, vp );
356 static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode )
358 GList *iter = val->children;
361 vik_layer_change_coord_mode ( VIK_LAYER(iter->data), mode );
366 // A slightly better way of defining the menu callback information
367 // This should be easier to extend/rework compared to previously
374 typedef gpointer menu_array_values[MA_LAST];
376 static void aggregate_layer_child_visible_toggle ( menu_array_values values )
378 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
379 VikLayersPanel *vlp = VIK_LAYERS_PANEL ( values[MA_VLP] );
382 // Loop around all (child) layers applying visibility setting
383 // This does not descend the tree if there are aggregates within aggregrate - just the first level of layers held
384 GList *iter = val->children;
386 vl = VIK_LAYER ( iter->data );
387 vl->visible = !vl->visible;
388 // Also set checkbox on/off
389 vik_treeview_item_toggle_visible ( vik_layers_panel_get_treeview ( vlp ), &(vl->iter) );
392 // Redraw as view may have changed
393 vik_layer_emit_update ( VIK_LAYER ( val ) );
396 static void aggregate_layer_child_visible ( menu_array_values values, gboolean on_off)
398 // Convert data back to correct types
399 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
400 VikLayersPanel *vlp = VIK_LAYERS_PANEL ( values[MA_VLP] );
403 // Loop around all (child) layers applying visibility setting
404 // This does not descend the tree if there are aggregates within aggregrate - just the first level of layers held
405 GList *iter = val->children;
407 vl = VIK_LAYER ( iter->data );
408 vl->visible = on_off;
409 // Also set checkbox on_off
410 vik_treeview_item_set_visible ( vik_layers_panel_get_treeview ( vlp ), &(vl->iter), on_off );
413 // Redraw as view may have changed
414 vik_layer_emit_update ( VIK_LAYER ( val ) );
417 static void aggregate_layer_child_visible_on ( menu_array_values values )
419 aggregate_layer_child_visible ( values, TRUE );
422 static void aggregate_layer_child_visible_off ( menu_array_values values )
424 aggregate_layer_child_visible ( values, FALSE );
428 * If order is true sort ascending, otherwise a descending sort
430 static gint sort_layer_compare ( gconstpointer a, gconstpointer b, gpointer order )
432 VikLayer *sa = (VikLayer *)a;
433 VikLayer *sb = (VikLayer *)b;
435 // Default ascending order
436 gint answer = g_strcmp0 ( sa->name, sb->name );
438 if ( GPOINTER_TO_INT(order) ) {
439 // Invert sort order for ascending order
446 static void aggregate_layer_sort_a2z ( menu_array_values values )
448 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
449 vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_ALPHABETICAL_ASCENDING );
450 val->children = g_list_sort_with_data ( val->children, sort_layer_compare, GINT_TO_POINTER(TRUE) );
453 static void aggregate_layer_sort_z2a ( menu_array_values values )
455 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
456 vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_ALPHABETICAL_DESCENDING );
457 val->children = g_list_sort_with_data ( val->children, sort_layer_compare, GINT_TO_POINTER(FALSE) );
461 * If order is true sort ascending, otherwise a descending sort
463 static gint sort_layer_compare_timestamp ( gconstpointer a, gconstpointer b, gpointer order )
465 VikLayer *sa = (VikLayer *)a;
466 VikLayer *sb = (VikLayer *)b;
468 // Default ascending order
469 // NB This might be relatively slow...
470 gint answer = ( vik_layer_get_timestamp(sa) > vik_layer_get_timestamp(sb) );
472 if ( GPOINTER_TO_INT(order) ) {
473 // Invert sort order for ascending order
480 static void aggregate_layer_sort_timestamp_ascend ( menu_array_values values )
482 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
483 vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_DATE_ASCENDING );
484 val->children = g_list_sort_with_data ( val->children, sort_layer_compare_timestamp, GINT_TO_POINTER(TRUE) );
487 static void aggregate_layer_sort_timestamp_descend ( menu_array_values values )
489 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
490 vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_DATE_DESCENDING );
491 val->children = g_list_sort_with_data ( val->children, sort_layer_compare_timestamp, GINT_TO_POINTER(FALSE) );
495 * aggregate_layer_waypoint_create_list:
496 * @vl: The layer that should create the waypoint and layers list
497 * @user_data: Not used in this function
499 * Returns: A list of #vik_trw_waypoint_list_t
501 static GList* aggregate_layer_waypoint_create_list ( VikLayer *vl, gpointer user_data )
503 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
505 // Get all TRW layers
506 GList *layers = NULL;
507 layers = vik_aggregate_layer_get_all_layers_of_type ( val, layers, VIK_LAYER_TRW, TRUE );
509 // For each TRW layers keep adding the waypoints to build a list of all of them
510 GList *waypoints_and_layers = NULL;
511 layers = g_list_first ( layers );
513 GList *waypoints = NULL;
514 waypoints = g_list_concat ( waypoints, g_hash_table_get_values ( vik_trw_layer_get_waypoints( VIK_TRW_LAYER(layers->data) ) ) );
516 waypoints_and_layers = g_list_concat ( waypoints_and_layers, vik_trw_layer_build_waypoint_list_t ( VIK_TRW_LAYER(layers->data), waypoints ) );
518 layers = g_list_next ( layers );
520 g_list_free ( layers );
522 return waypoints_and_layers;
525 static void aggregate_layer_waypoint_list_dialog ( menu_array_values values )
527 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
528 gchar *title = g_strdup_printf ( _("%s: Waypoint List"), VIK_LAYER(val)->name );
529 vik_trw_layer_waypoint_list_show_dialog ( title, VIK_LAYER(val), NULL, aggregate_layer_waypoint_create_list, TRUE );
534 * Search all TrackWaypoint layers in this aggregate layer for an item on the user specified date
536 static void aggregate_layer_search_date ( menu_array_values values )
538 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
540 gchar *date_str = a_dialog_get_date ( VIK_GTK_WINDOW_FROM_LAYER(val), _("Search by Date") );
545 VikViewport *vvp = vik_window_viewport ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(val)) );
548 gl = vik_aggregate_layer_get_all_layers_of_type ( val, gl, VIK_LAYER_TRW, TRUE );
549 gboolean found = FALSE;
550 // Search tracks first
551 while ( gl && !found ) {
552 // Make it auto select the item if found
553 found = vik_trw_layer_find_date ( VIK_TRW_LAYER(gl->data), date_str, &position, vvp, TRUE, TRUE );
554 gl = g_list_next ( gl );
558 // Reset and try on Waypoints
560 gl = vik_aggregate_layer_get_all_layers_of_type ( val, gl, VIK_LAYER_TRW, TRUE );
561 while ( gl && !found ) {
562 // Make it auto select the item if found
563 found = vik_trw_layer_find_date ( VIK_TRW_LAYER(gl->data), date_str, &position, vvp, FALSE, TRUE );
564 gl = g_list_next ( gl );
570 a_dialog_info_msg ( VIK_GTK_WINDOW_FROM_LAYER(val), _("No items found with the requested date.") );
575 * aggregate_layer_track_create_list:
576 * @vl: The layer that should create the track and layers list
577 * @user_data: Not used in this function
579 * Returns: A list of #vik_trw_track_list_t
581 static GList* aggregate_layer_track_create_list ( VikLayer *vl, gpointer user_data )
583 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
585 // Get all TRW layers
586 GList *layers = NULL;
587 layers = vik_aggregate_layer_get_all_layers_of_type ( val, layers, VIK_LAYER_TRW, TRUE );
589 // For each TRW layers keep adding the tracks and routes to build a list of all of them
590 GList *tracks_and_layers = NULL;
591 layers = g_list_first ( layers );
593 GList *tracks = NULL;
594 tracks = g_list_concat ( tracks, g_hash_table_get_values ( vik_trw_layer_get_tracks( VIK_TRW_LAYER(layers->data) ) ) );
595 tracks = g_list_concat ( tracks, g_hash_table_get_values ( vik_trw_layer_get_routes( VIK_TRW_LAYER(layers->data) ) ) );
597 tracks_and_layers = g_list_concat ( tracks_and_layers, vik_trw_layer_build_track_list_t ( VIK_TRW_LAYER(layers->data), tracks ) );
599 layers = g_list_next ( layers );
601 g_list_free ( layers );
603 return tracks_and_layers;
606 static void aggregate_layer_track_list_dialog ( menu_array_values values )
608 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
609 gchar *title = g_strdup_printf ( _("%s: Track and Route List"), VIK_LAYER(val)->name );
610 vik_trw_layer_track_list_show_dialog ( title, VIK_LAYER(val), NULL, aggregate_layer_track_create_list, TRUE );
615 * aggregate_layer_analyse_close:
617 * Stuff to do on dialog closure
619 static void aggregate_layer_analyse_close ( GtkWidget *dialog, gint resp, VikLayer* vl )
621 VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
622 gtk_widget_destroy ( dialog );
623 val->tracks_analysis_dialog = NULL;
626 static void aggregate_layer_analyse ( menu_array_values values )
628 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
630 // There can only be one!
631 if ( val->tracks_analysis_dialog )
634 val->tracks_analysis_dialog = vik_trw_layer_analyse_this ( VIK_GTK_WINDOW_FROM_LAYER(VIK_LAYER(val)),
635 VIK_LAYER(val)->name,
638 aggregate_layer_track_create_list,
639 aggregate_layer_analyse_close );
642 static void aggregate_layer_add_menu_items ( VikAggregateLayer *val, GtkMenu *menu, gpointer vlp )
644 // Data to pass on in menu functions
645 static menu_array_values values;
646 values[MA_VAL] = val;
647 values[MA_VLP] = vlp;
649 GtkWidget *item = gtk_menu_item_new();
650 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
651 gtk_widget_show ( item );
653 GtkWidget *vis_submenu = gtk_menu_new ();
654 item = gtk_menu_item_new_with_mnemonic ( _("_Visibility") );
655 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
656 gtk_widget_show ( item );
657 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), vis_submenu );
659 item = gtk_image_menu_item_new_with_mnemonic ( _("_Show All") );
660 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_APPLY, GTK_ICON_SIZE_MENU) );
661 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_on), values );
662 gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
663 gtk_widget_show ( item );
665 item = gtk_image_menu_item_new_with_mnemonic ( _("_Hide All") );
666 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU) );
667 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_off), values );
668 gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
669 gtk_widget_show ( item );
671 item = gtk_image_menu_item_new_with_mnemonic ( _("_Toggle") );
672 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_MENU) );
673 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_toggle), values );
674 gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
675 gtk_widget_show ( item );
677 GtkWidget *submenu_sort = gtk_menu_new ();
678 item = gtk_image_menu_item_new_with_mnemonic ( _("_Sort") );
679 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_MENU) );
680 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
681 gtk_widget_show ( item );
682 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu_sort );
684 item = gtk_image_menu_item_new_with_mnemonic ( _("Name _Ascending") );
685 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_ASCENDING, GTK_ICON_SIZE_MENU) );
686 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_a2z), values );
687 gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item );
688 gtk_widget_show ( item );
690 item = gtk_image_menu_item_new_with_mnemonic ( _("Name _Descending") );
691 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_DESCENDING, GTK_ICON_SIZE_MENU) );
692 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_z2a), values );
693 gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item );
694 gtk_widget_show ( item );
696 item = gtk_image_menu_item_new_with_mnemonic ( _("Date Ascending") );
697 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_ASCENDING, GTK_ICON_SIZE_MENU) );
698 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_timestamp_ascend), values );
699 gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item );
700 gtk_widget_show ( item );
702 item = gtk_image_menu_item_new_with_mnemonic ( _("Date Descending") );
703 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_DESCENDING, GTK_ICON_SIZE_MENU) );
704 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_timestamp_descend), values );
705 gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item );
706 gtk_widget_show ( item );
708 item = gtk_menu_item_new_with_mnemonic ( _("_Statistics") );
709 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_analyse), values );
710 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
711 gtk_widget_show ( item );
713 item = gtk_image_menu_item_new_with_mnemonic ( _("Track _List...") );
714 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_INDEX, GTK_ICON_SIZE_MENU) );
715 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_track_list_dialog), values );
716 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
717 gtk_widget_show ( item );
719 item = gtk_image_menu_item_new_with_mnemonic ( _("_Waypoint List...") );
720 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_INDEX, GTK_ICON_SIZE_MENU) );
721 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_waypoint_list_dialog), values );
722 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
723 gtk_widget_show ( item );
725 GtkWidget *search_submenu = gtk_menu_new ();
726 item = gtk_image_menu_item_new_with_mnemonic ( _("Searc_h") );
727 gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU) );
728 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
729 gtk_widget_show ( item );
730 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), search_submenu );
732 item = gtk_menu_item_new_with_mnemonic ( _("By _Date...") );
733 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_search_date), values );
734 gtk_menu_shell_append ( GTK_MENU_SHELL(search_submenu), item );
735 gtk_widget_set_tooltip_text (item, _("Find the first item with a specified date"));
736 gtk_widget_show ( item );
739 static void disconnect_layer_signal ( VikLayer *vl, VikAggregateLayer *val )
741 guint number_handlers = g_signal_handlers_disconnect_matched(vl, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, val);
742 if ( number_handlers != 1 )
743 g_critical ("%s: Unexpected number of disconnect handlers: %d", __FUNCTION__, number_handlers);
746 void vik_aggregate_layer_free ( VikAggregateLayer *val )
748 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
749 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
750 g_list_free ( val->children );
751 if ( val->tracks_analysis_dialog != NULL )
752 gtk_widget_destroy ( val->tracks_analysis_dialog );
755 static void delete_layer_iter ( VikLayer *vl )
758 vik_treeview_item_delete ( vl->vt, &(vl->iter) );
761 void vik_aggregate_layer_clear ( VikAggregateLayer *val )
763 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
764 g_list_foreach ( val->children, (GFunc)(delete_layer_iter), NULL );
765 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
766 g_list_free ( val->children );
767 val->children = NULL;
770 gboolean vik_aggregate_layer_delete ( VikAggregateLayer *val, GtkTreeIter *iter )
772 VikLayer *l = VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, iter ) );
773 gboolean was_visible = l->visible;
775 vik_treeview_item_delete ( VIK_LAYER(val)->vt, iter );
776 val->children = g_list_remove ( val->children, l );
777 disconnect_layer_signal ( l, val );
778 g_object_unref ( l );
784 /* returns 0 == we're good, 1 == didn't find any layers, 2 == got rejected */
785 guint vik_aggregate_layer_tool ( VikAggregateLayer *val, VikLayerTypeEnum layer_type, VikToolInterfaceFunc tool_func, GdkEventButton *event, VikViewport *vvp )
787 GList *iter = val->children;
788 gboolean found_rej = FALSE;
796 /* if this layer "accepts" the tool call */
797 if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == layer_type )
799 if ( tool_func ( VIK_LAYER(iter->data), event, vvp ) )
805 /* recursive -- try the same for the child aggregate layer. */
806 else if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == VIK_LAYER_AGGREGATE )
808 gint rv = vik_aggregate_layer_tool(VIK_AGGREGATE_LAYER(iter->data), layer_type, tool_func, event, vvp);
816 return found_rej ? 2 : 1; /* no one wanted to accept the tool call in this layer */
820 VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer *val, VikLayerTypeEnum type )
823 GList *ls = val->children;
831 VikLayer *vl = VIK_LAYER(ls->data);
832 if ( vl->visible && vl->type == type )
834 else if ( vl->visible && vl->type == VIK_LAYER_AGGREGATE )
836 rv = vik_aggregate_layer_get_top_visible_layer_of_type(VIK_AGGREGATE_LAYER(vl), type);
845 GList *vik_aggregate_layer_get_all_layers_of_type(VikAggregateLayer *val, GList *layers, VikLayerTypeEnum type, gboolean include_invisible)
848 GList *children = val->children;
853 // Where appropriate *don't* include non-visible layers
855 vl = VIK_LAYER(children->data);
856 if (vl->type == VIK_LAYER_AGGREGATE ) {
857 // Don't even consider invisible aggregrates, unless told to
858 if (vl->visible || include_invisible)
859 l = vik_aggregate_layer_get_all_layers_of_type(VIK_AGGREGATE_LAYER(children->data), l, type, include_invisible);
861 else if (vl->type == type) {
862 if (vl->visible || include_invisible)
863 l = g_list_prepend(l, children->data); /* now in top down order */
865 else if (type == VIK_LAYER_TRW) {
866 /* GPS layers contain TRW layers. cf with usage in file.c */
867 if (VIK_LAYER(children->data)->type == VIK_LAYER_GPS) {
868 if (VIK_LAYER(children->data)->visible || include_invisible) {
869 if (!vik_gps_layer_is_empty(VIK_GPS_LAYER(children->data))) {
871 can not use g_list_concat due to wrong copy method - crashes if used a couple times !!
872 l = g_list_concat (l, vik_gps_layer_get_children (VIK_GPS_LAYER(children->data)));
874 /* create own copy method instead :( */
875 GList *gps_trw_layers = (GList *)vik_gps_layer_get_children (VIK_GPS_LAYER(children->data));
876 int n_layers = g_list_length (gps_trw_layers);
878 for ( layer = 0; layer < n_layers; layer++) {
879 l = g_list_prepend(l, gps_trw_layers->data);
880 gps_trw_layers = gps_trw_layers->next;
882 g_list_free(gps_trw_layers);
887 children = children->next;
892 void vik_aggregate_layer_realize ( VikAggregateLayer *val, VikTreeview *vt, GtkTreeIter *layer_iter )
894 GList *i = val->children;
896 VikLayer *vl = VIK_LAYER(val);
900 vli = VIK_LAYER(i->data);
901 vik_treeview_add_layer ( vl->vt, layer_iter, &iter, vli->name, val, TRUE,
902 vli, vli->type, vli->type, vik_layer_get_timestamp(vli) );
903 if ( ! vli->visible )
904 vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
905 vik_layer_realize ( vli, vl->vt, &iter );
910 const GList *vik_aggregate_layer_get_children ( VikAggregateLayer *val )
912 return val->children;
915 gboolean vik_aggregate_layer_is_empty ( VikAggregateLayer *val )
922 static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path )
924 VikTreeview *vt = VIK_LAYER(val_src)->vt;
925 VikLayer *vl = vik_treeview_item_get_pointer(vt, src_item_iter);
926 GtkTreeIter dest_iter;
928 gboolean target_exists;
930 dp = gtk_tree_path_to_string(dest_path);
931 target_exists = vik_treeview_get_iter_from_path_str(vt, &dest_iter, dp);
933 /* vik_aggregate_layer_delete unrefs, but we don't want that here.
934 * we're still using the layer. */
936 vik_aggregate_layer_delete(val_src, src_item_iter);
939 vik_aggregate_layer_insert_layer(val_dest, vl, &dest_iter);
941 vik_aggregate_layer_insert_layer(val_dest, vl, NULL); /* append */
947 * Generate tooltip text for the layer.
949 static const gchar* aggregate_layer_tooltip ( VikAggregateLayer *val )
951 static gchar tmp_buf[128];
954 GList *children = val->children;
956 gint nn = g_list_length (children);
957 // Could have a more complicated tooltip that numbers each type of layers,
958 // but for now a simple overall count
959 g_snprintf (tmp_buf, sizeof(tmp_buf), ngettext("One layer", "%d layers", nn), nn );