+// A slightly better way of defining the menu callback information
+// This should be easier to extend/rework compared to previously
+typedef enum {
+ MA_VAL = 0,
+ MA_VLP,
+ MA_LAST
+} menu_array_index;
+
+typedef gpointer menu_array_values[MA_LAST];
+
+static void aggregate_layer_child_visible_toggle ( menu_array_values values )
+{
+ VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
+ VikLayersPanel *vlp = VIK_LAYERS_PANEL ( values[MA_VLP] );
+ VikLayer *vl;
+
+ // Loop around all (child) layers applying visibility setting
+ // This does not descend the tree if there are aggregates within aggregrate - just the first level of layers held
+ GList *iter = val->children;
+ while ( iter ) {
+ vl = VIK_LAYER ( iter->data );
+ vl->visible = !vl->visible;
+ // Also set checkbox on/off
+ vik_treeview_item_toggle_visible ( vik_layers_panel_get_treeview ( vlp ), &(vl->iter) );
+ iter = iter->next;
+ }
+ // Redraw as view may have changed
+ vik_layer_emit_update ( VIK_LAYER ( val ) );
+}
+
+static void aggregate_layer_child_visible ( menu_array_values values, gboolean on_off)
+{
+ // Convert data back to correct types
+ VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
+ VikLayersPanel *vlp = VIK_LAYERS_PANEL ( values[MA_VLP] );
+ VikLayer *vl;
+
+ // Loop around all (child) layers applying visibility setting
+ // This does not descend the tree if there are aggregates within aggregrate - just the first level of layers held
+ GList *iter = val->children;
+ while ( iter ) {
+ vl = VIK_LAYER ( iter->data );
+ vl->visible = on_off;
+ // Also set checkbox on_off
+ vik_treeview_item_set_visible ( vik_layers_panel_get_treeview ( vlp ), &(vl->iter), on_off );
+ iter = iter->next;
+ }
+ // Redraw as view may have changed
+ vik_layer_emit_update ( VIK_LAYER ( val ) );
+}
+
+static void aggregate_layer_child_visible_on ( menu_array_values values )
+{
+ aggregate_layer_child_visible ( values, TRUE );
+}
+
+static void aggregate_layer_child_visible_off ( menu_array_values values )
+{
+ aggregate_layer_child_visible ( values, FALSE );
+}
+
+/**
+ * If order is true sort ascending, otherwise a descending sort
+ */
+static gint sort_layer_compare ( gconstpointer a, gconstpointer b, gpointer order )
+{
+ VikLayer *sa = (VikLayer *)a;
+ VikLayer *sb = (VikLayer *)b;
+
+ // Default ascending order
+ gint answer = g_strcmp0 ( sa->name, sb->name );
+
+ if ( GPOINTER_TO_INT(order) ) {
+ // Invert sort order for ascending order
+ answer = -answer;
+ }
+
+ return answer;
+}
+
+static void aggregate_layer_sort_a2z ( menu_array_values values )
+{
+ VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
+ vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_ALPHABETICAL_ASCENDING );
+ val->children = g_list_sort_with_data ( val->children, sort_layer_compare, GINT_TO_POINTER(TRUE) );
+}
+
+static void aggregate_layer_sort_z2a ( menu_array_values values )
+{
+ VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
+ vik_treeview_sort_children ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), VL_SO_ALPHABETICAL_DESCENDING );
+ val->children = g_list_sort_with_data ( val->children, sort_layer_compare, GINT_TO_POINTER(FALSE) );
+}
+
+/**
+ * aggregate_layer_waypoint_create_list:
+ * @vl: The layer that should create the waypoint and layers list
+ * @user_data: Not used in this function
+ *
+ * Returns: A list of #vik_trw_waypoint_list_t
+ */
+static GList* aggregate_layer_waypoint_create_list ( VikLayer *vl, gpointer user_data )
+{
+ VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
+
+ // Get all TRW layers
+ GList *layers = NULL;
+ layers = vik_aggregate_layer_get_all_layers_of_type ( val, layers, VIK_LAYER_TRW, TRUE );
+
+ // For each TRW layers keep adding the waypoints to build a list of all of them
+ GList *waypoints_and_layers = NULL;
+ layers = g_list_first ( layers );
+ while ( layers ) {
+ GList *waypoints = NULL;
+ waypoints = g_list_concat ( waypoints, g_hash_table_get_values ( vik_trw_layer_get_waypoints( VIK_TRW_LAYER(layers->data) ) ) );
+
+ waypoints_and_layers = g_list_concat ( waypoints_and_layers, vik_trw_layer_build_waypoint_list_t ( VIK_TRW_LAYER(layers->data), waypoints ) );
+
+ layers = g_list_next ( layers );
+ }
+ g_list_free ( layers );
+
+ return waypoints_and_layers;
+}
+
+static void aggregate_layer_waypoint_list_dialog ( menu_array_values values )
+{
+ VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
+ gchar *title = g_strdup_printf ( _("%s: Waypoint List"), VIK_LAYER(val)->name );
+ vik_trw_layer_waypoint_list_show_dialog ( title, VIK_LAYER(val), NULL, aggregate_layer_waypoint_create_list, TRUE );
+ g_free ( title );
+}
+
+/**
+ * Search all TrackWaypoint layers in this aggregate layer for an item on the user specified date
+ */
+static void aggregate_layer_search_date ( menu_array_values values )
+{
+ VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
+ VikCoord position;
+ gchar *date_str = a_dialog_get_date ( VIK_GTK_WINDOW_FROM_LAYER(val), _("Search by Date") );
+
+ if ( !date_str )
+ return;
+
+ VikViewport *vvp = vik_window_viewport ( VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(val)) );
+
+ GList *gl = NULL;
+ gl = vik_aggregate_layer_get_all_layers_of_type ( val, gl, VIK_LAYER_TRW, TRUE );
+ gboolean found = FALSE;
+ // Search tracks first
+ while ( gl && !found ) {
+ // Make it auto select the item if found
+ found = vik_trw_layer_find_date ( VIK_TRW_LAYER(gl->data), date_str, &position, vvp, TRUE, TRUE );
+ gl = g_list_next ( gl );
+ }
+ g_list_free ( gl );
+ if ( !found ) {
+ // Reset and try on Waypoints
+ gl = NULL;
+ gl = vik_aggregate_layer_get_all_layers_of_type ( val, gl, VIK_LAYER_TRW, TRUE );
+ while ( gl && !found ) {
+ // Make it auto select the item if found
+ found = vik_trw_layer_find_date ( VIK_TRW_LAYER(gl->data), date_str, &position, vvp, FALSE, TRUE );
+ gl = g_list_next ( gl );
+ }
+ g_list_free ( gl );
+ }
+
+ if ( !found )
+ a_dialog_info_msg ( VIK_GTK_WINDOW_FROM_LAYER(val), _("No items found with the requested date.") );
+ g_free ( date_str );
+}
+
+/**
+ * aggregate_layer_track_create_list:
+ * @vl: The layer that should create the track and layers list
+ * @user_data: Not used in this function
+ *
+ * Returns: A list of #vik_trw_track_list_t
+ */
+static GList* aggregate_layer_track_create_list ( VikLayer *vl, gpointer user_data )
+{
+ VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
+
+ // Get all TRW layers
+ GList *layers = NULL;
+ layers = vik_aggregate_layer_get_all_layers_of_type ( val, layers, VIK_LAYER_TRW, TRUE );
+
+ // For each TRW layers keep adding the tracks and routes to build a list of all of them
+ GList *tracks_and_layers = NULL;
+ layers = g_list_first ( layers );
+ while ( layers ) {
+ GList *tracks = NULL;
+ tracks = g_list_concat ( tracks, g_hash_table_get_values ( vik_trw_layer_get_tracks( VIK_TRW_LAYER(layers->data) ) ) );
+ tracks = g_list_concat ( tracks, g_hash_table_get_values ( vik_trw_layer_get_routes( VIK_TRW_LAYER(layers->data) ) ) );
+
+ tracks_and_layers = g_list_concat ( tracks_and_layers, vik_trw_layer_build_track_list_t ( VIK_TRW_LAYER(layers->data), tracks ) );
+
+ layers = g_list_next ( layers );
+ }
+ g_list_free ( layers );
+
+ return tracks_and_layers;
+}
+
+static void aggregate_layer_track_list_dialog ( menu_array_values values )
+{
+ VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
+ gchar *title = g_strdup_printf ( _("%s: Track and Route List"), VIK_LAYER(val)->name );
+ vik_trw_layer_track_list_show_dialog ( title, VIK_LAYER(val), NULL, aggregate_layer_track_create_list, TRUE );
+ g_free ( title );
+}
+
+/**
+ * aggregate_layer_analyse_close:
+ *
+ * Stuff to do on dialog closure
+ */
+static void aggregate_layer_analyse_close ( GtkWidget *dialog, gint resp, VikLayer* vl )
+{
+ VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
+ gtk_widget_destroy ( dialog );
+ val->tracks_analysis_dialog = NULL;
+}
+
+static void aggregate_layer_analyse ( menu_array_values values )
+{
+ VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( values[MA_VAL] );
+
+ // There can only be one!
+ if ( val->tracks_analysis_dialog )
+ return;
+
+ val->tracks_analysis_dialog = vik_trw_layer_analyse_this ( VIK_GTK_WINDOW_FROM_LAYER(VIK_LAYER(val)),
+ VIK_LAYER(val)->name,
+ VIK_LAYER(val),
+ NULL,
+ aggregate_layer_track_create_list,
+ aggregate_layer_analyse_close );
+}
+
+static void aggregate_layer_add_menu_items ( VikAggregateLayer *val, GtkMenu *menu, gpointer vlp )
+{
+ // Data to pass on in menu functions
+ static menu_array_values values;
+ values[MA_VAL] = val;
+ values[MA_VLP] = vlp;
+
+ GtkWidget *item = gtk_menu_item_new();
+ gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
+ gtk_widget_show ( item );
+
+ GtkWidget *vis_submenu = gtk_menu_new ();
+ item = gtk_menu_item_new_with_mnemonic ( _("_Visibility") );
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+ gtk_widget_show ( item );
+ gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), vis_submenu );
+
+ item = gtk_image_menu_item_new_with_mnemonic ( _("_Show All") );
+ gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_APPLY, GTK_ICON_SIZE_MENU) );
+ g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_on), values );
+ gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
+ gtk_widget_show ( item );
+
+ item = gtk_image_menu_item_new_with_mnemonic ( _("_Hide All") );
+ gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU) );
+ g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_off), values );
+ gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
+ gtk_widget_show ( item );
+
+ item = gtk_image_menu_item_new_with_mnemonic ( _("_Toggle") );
+ gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_MENU) );
+ g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_child_visible_toggle), values );
+ gtk_menu_shell_append (GTK_MENU_SHELL (vis_submenu), item);
+ gtk_widget_show ( item );
+
+ GtkWidget *submenu_sort = gtk_menu_new ();
+ item = gtk_image_menu_item_new_with_mnemonic ( _("_Sort") );
+ gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_MENU) );
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+ gtk_widget_show ( item );
+ gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu_sort );
+
+ item = gtk_image_menu_item_new_with_mnemonic ( _("Name _Ascending") );
+ gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_ASCENDING, GTK_ICON_SIZE_MENU) );
+ g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_a2z), values );
+ gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item );
+ gtk_widget_show ( item );
+
+ item = gtk_image_menu_item_new_with_mnemonic ( _("Name _Descending") );
+ gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_SORT_DESCENDING, GTK_ICON_SIZE_MENU) );
+ g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_sort_z2a), values );
+ gtk_menu_shell_append ( GTK_MENU_SHELL(submenu_sort), item );
+ gtk_widget_show ( item );
+
+ item = gtk_menu_item_new_with_mnemonic ( _("_Statistics") );
+ g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_analyse), values );
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+ gtk_widget_show ( item );
+
+ item = gtk_image_menu_item_new_with_mnemonic ( _("Track _List...") );
+ gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_INDEX, GTK_ICON_SIZE_MENU) );
+ g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_track_list_dialog), values );
+ gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
+ gtk_widget_show ( item );
+
+ item = gtk_image_menu_item_new_with_mnemonic ( _("_Waypoint List...") );
+ gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_INDEX, GTK_ICON_SIZE_MENU) );
+ g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_waypoint_list_dialog), values );
+ gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
+ gtk_widget_show ( item );
+
+ GtkWidget *search_submenu = gtk_menu_new ();
+ item = gtk_image_menu_item_new_with_mnemonic ( _("Searc_h") );
+ gtk_image_menu_item_set_image ( (GtkImageMenuItem*)item, gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU) );
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+ gtk_widget_show ( item );
+ gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), search_submenu );
+
+ item = gtk_menu_item_new_with_mnemonic ( _("By _Date...") );
+ g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(aggregate_layer_search_date), values );
+ gtk_menu_shell_append ( GTK_MENU_SHELL(search_submenu), item );
+ gtk_widget_set_tooltip_text (item, _("Find the first item with a specified date"));
+ gtk_widget_show ( item );
+}
+