]> git.street.me.uk Git - andy/viking.git/blob - src/vikaggregatelayer.c
SF Features#116: Add an Acquire From URL option.
[andy/viking.git] / src / vikaggregatelayer.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 #include "viking.h"
24 #include "viktrwlayer_analysis.h"
25 #include "viktrwlayer_tracklist.h"
26 #include "viktrwlayer_waypointlist.h"
27 #include "icons/icons.h"
28
29 #include <string.h>
30 #include <glib/gi18n.h>
31
32 #define DISCONNECT_UPDATE_SIGNAL(vl, val) g_signal_handlers_disconnect_matched(vl, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, val)
33
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 );
38
39 static void aggregate_layer_add_menu_items ( VikAggregateLayer *val, GtkMenu *menu, gpointer vlp );
40
41 VikLayerInterface vik_aggregate_layer_interface = {
42   "Aggregate",
43   N_("Aggregate"),
44   "<control><shift>A",
45   &vikaggregatelayer_pixbuf,
46
47   NULL,
48   0,
49
50   NULL,
51   0,
52   NULL,
53   0,
54
55   VIK_MENU_ITEM_ALL,
56
57   (VikLayerFuncCreate)                  vik_aggregate_layer_create,
58   (VikLayerFuncRealize)                 vik_aggregate_layer_realize,
59   (VikLayerFuncPostRead)                NULL,
60   (VikLayerFuncFree)                    vik_aggregate_layer_free,
61
62   (VikLayerFuncProperties)              NULL,
63   (VikLayerFuncDraw)                    vik_aggregate_layer_draw,
64   (VikLayerFuncChangeCoordMode)         aggregate_layer_change_coord_mode,
65   
66   (VikLayerFuncSetMenuItemsSelection)   NULL,
67   (VikLayerFuncGetMenuItemsSelection)   NULL,
68
69   (VikLayerFuncAddMenuItems)            aggregate_layer_add_menu_items,
70   (VikLayerFuncSublayerAddMenuItems)    NULL,
71
72   (VikLayerFuncSublayerRenameRequest)   NULL,
73   (VikLayerFuncSublayerToggleVisible)   NULL,
74   (VikLayerFuncSublayerTooltip)         NULL,
75   (VikLayerFuncLayerTooltip)            NULL,
76   (VikLayerFuncLayerSelected)           NULL,
77
78   (VikLayerFuncMarshall)                aggregate_layer_marshall,
79   (VikLayerFuncUnmarshall)              aggregate_layer_unmarshall,
80
81   (VikLayerFuncSetParam)                NULL,
82   (VikLayerFuncGetParam)                NULL,
83
84   (VikLayerFuncReadFileData)            NULL,
85   (VikLayerFuncWriteFileData)           NULL,
86
87   (VikLayerFuncDeleteItem)              NULL,
88   (VikLayerFuncCutItem)                 NULL,
89   (VikLayerFuncCopyItem)                NULL,
90   (VikLayerFuncPasteItem)               NULL,
91   (VikLayerFuncFreeCopiedItem)          NULL,
92   (VikLayerFuncDragDropRequest)         aggregate_layer_drag_drop_request,
93
94   (VikLayerFuncSelectClick)             NULL,
95   (VikLayerFuncSelectMove)              NULL,
96   (VikLayerFuncSelectRelease)           NULL,
97   (VikLayerFuncSelectedViewportMenu)    NULL,
98 };
99
100 struct _VikAggregateLayer {
101   VikLayer vl;
102   GList *children;
103   // One per layer
104   GtkWidget *tracks_analysis_dialog;
105 };
106
107 GType vik_aggregate_layer_get_type ()
108 {
109   static GType val_type = 0;
110
111   if (!val_type)
112   {
113     static const GTypeInfo val_info =
114     {
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),
122       0,
123       NULL /* instance init */
124     };
125     val_type = g_type_register_static ( VIK_LAYER_TYPE, "VikAggregateLayer", &val_info, 0 );
126   }
127
128   return val_type;
129 }
130
131 VikAggregateLayer *vik_aggregate_layer_create (VikViewport *vp)
132 {
133   VikAggregateLayer *rv = vik_aggregate_layer_new ();
134   vik_layer_rename ( VIK_LAYER(rv), vik_aggregate_layer_interface.name );
135   return rv;
136 }
137
138 static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *datalen )
139 {
140   GList *child = val->children;
141   VikLayer *child_layer;
142   guint8 *ld; 
143   gint ll;
144   GByteArray* b = g_byte_array_new ();
145   gint len;
146
147 #define alm_append(obj, sz)     \
148   len = (sz);                   \
149   g_byte_array_append ( b, (guint8 *)&len, sizeof(len) );       \
150   g_byte_array_append ( b, (guint8 *)(obj), len );
151
152   vik_layer_marshall_params(VIK_LAYER(val), &ld, &ll);
153   alm_append(ld, ll);
154   g_free(ld);
155
156   while (child) {
157     child_layer = VIK_LAYER(child->data);
158     vik_layer_marshall ( child_layer, &ld, &ll );
159     if (ld) {
160       alm_append(ld, ll);
161       g_free(ld);
162     }
163     child = child->next;
164   }
165   *data = b->data;
166   *datalen = b->len;
167   g_byte_array_free(b, FALSE);
168 #undef alm_append
169 }
170
171 static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
172 {
173 #define alm_size (*(gint *)data)
174 #define alm_next \
175   len -= sizeof(gint) + alm_size; \
176   data += sizeof(gint) + alm_size;
177   
178   VikAggregateLayer *rv = vik_aggregate_layer_new();
179   VikLayer *child_layer;
180
181   vik_layer_unmarshall_params ( VIK_LAYER(rv), data+sizeof(gint), alm_size, vvp );
182   alm_next;
183
184   while (len>0) {
185     child_layer = vik_layer_unmarshall ( data + sizeof(gint), alm_size, vvp );
186     if (child_layer) {
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 );
189     }
190     alm_next;
191   }
192   //  g_print("aggregate_layer_unmarshall ended with len=%d\n", len);
193   return rv;
194 #undef alm_size
195 #undef alm_next
196 }
197
198 VikAggregateLayer *vik_aggregate_layer_new ()
199 {
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;
203   return val;
204 }
205
206 void vik_aggregate_layer_insert_layer ( VikAggregateLayer *val, VikLayer *l, GtkTreeIter *replace_iter )
207 {
208   GtkTreeIter iter;
209   VikLayer *vl = VIK_LAYER(val);
210
211   // By default layers are inserted above the selected layer
212   gboolean put_above = TRUE;
213
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 )
216     put_above = FALSE;
217
218   if ( vl->realized )
219   {
220     vik_treeview_insert_layer ( vl->vt, &(vl->iter), &iter, l->name, val, put_above, l, l->type, l->type, replace_iter );
221     if ( ! l->visible )
222       vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
223     vik_layer_realize ( l, vl->vt, &iter );
224
225     if ( val->children == NULL )
226       vik_treeview_expand ( vl->vt, &(vl->iter) );
227   }
228
229   if (replace_iter) {
230     GList *theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, replace_iter ) );
231     if ( put_above )
232       val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone)+1 );
233     else
234       // Thus insert 'here' (so don't add 1)
235       val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone) );
236   } else {
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 );
243   }
244   g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
245 }
246
247 /**
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
251  */
252 void vik_aggregate_layer_add_layer ( VikAggregateLayer *val, VikLayer *l, gboolean allow_reordering )
253 {
254   GtkTreeIter iter;
255   VikLayer *vl = VIK_LAYER(val);
256
257   // By default layers go to the top
258   gboolean put_above = TRUE;
259
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 )
263       put_above = FALSE;
264   }
265
266   if ( vl->realized )
267   {
268     vik_treeview_add_layer ( vl->vt, &(vl->iter), &iter, l->name, val, put_above, l, l->type, l->type);
269     if ( ! l->visible )
270       vik_treeview_item_set_visible ( vl->vt, &iter, FALSE );
271     vik_layer_realize ( l, vl->vt, &iter );
272
273     if ( val->children == NULL )
274       vik_treeview_expand ( vl->vt, &(vl->iter) );
275   }
276
277   if ( put_above )
278     val->children = g_list_append ( val->children, l );
279   else
280     val->children = g_list_prepend ( val->children, l );
281
282   g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update_secondary), val );
283 }
284
285 void vik_aggregate_layer_move_layer ( VikAggregateLayer *val, GtkTreeIter *child_iter, gboolean up )
286 {
287   GList *theone, *first, *second;
288   VikLayer *vl = VIK_LAYER(val);
289   vik_treeview_move_item ( vl->vt, child_iter, up );
290
291   theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( vl->vt, child_iter ) );
292
293   g_assert ( theone != NULL );
294
295   /* the old switcheroo */
296   if ( up && theone->next )
297   {
298     first = theone;
299     second = theone->next;
300   }
301   else if ( !up && theone->prev )
302   {
303     first = theone->prev;
304     second = theone;
305   }
306   else
307     return;
308
309   first->next = second->next;
310   second->prev = first->prev;
311   first->prev = second;
312   second->next = first;
313
314   /* second is now first */
315
316   if ( second->prev )
317     second->prev->next = second;
318   if ( first->next )
319     first->next->prev = first;
320
321   if ( second->prev == NULL )
322     val->children = second;
323 }
324
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
332  * later.
333  */
334 void vik_aggregate_layer_draw ( VikAggregateLayer *val, VikViewport *vp )
335 {
336   GList *iter = val->children;
337   VikLayer *vl;
338   VikLayer *trigger = VIK_LAYER(vik_viewport_get_trigger( vp ));
339   while ( iter ) {
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 );
345       } else {
346         vik_viewport_snapshot_save( vp );
347       }
348     }
349     if ( vl->type == VIK_LAYER_AGGREGATE || vl->type == VIK_LAYER_GPS || ! vik_viewport_get_half_drawn( vp ) )
350       vik_layer_draw ( vl, vp );
351     iter = iter->next;
352   }
353 }
354
355 static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode )
356 {
357   GList *iter = val->children;
358   while ( iter )
359   {
360     vik_layer_change_coord_mode ( VIK_LAYER(iter->data), mode );
361     iter = iter->next;
362   }
363 }
364
365 static void aggregate_layer_child_visible_toggle ( gpointer data[2] )
366 {
367   // Convert data back to correct types
368   VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( data[0] );
369   VikLayersPanel *vlp = VIK_LAYERS_PANEL ( data[1] );
370   VikLayer *vl;
371
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;
375   while ( iter ) {
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) );
380     iter = iter->next;
381   }
382   // Redraw as view may have changed
383   vik_layer_emit_update ( VIK_LAYER ( val ) );
384 }
385
386 static void aggregate_layer_child_visible ( gpointer data[2], gboolean on_off)
387 {
388   // Convert data back to correct types
389   VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( data[0] );
390   VikLayersPanel *vlp = VIK_LAYERS_PANEL ( data[1] );
391   VikLayer *vl;
392
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;
396   while ( iter ) {
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 );
401     iter = iter->next;
402   }
403   // Redraw as view may have changed
404   vik_layer_emit_update ( VIK_LAYER ( val ) );
405 }
406
407 static void aggregate_layer_child_visible_on ( gpointer data[2] )
408 {
409   aggregate_layer_child_visible ( data, TRUE );
410 }
411
412 static void aggregate_layer_child_visible_off ( gpointer data[2] )
413 {
414   aggregate_layer_child_visible ( data, FALSE );
415 }
416
417 /**
418  * If order is true sort ascending, otherwise a descending sort
419  */
420 static gint sort_layer_compare ( gconstpointer a, gconstpointer b, gpointer order )
421 {
422   VikLayer *sa = (VikLayer *)a;
423   VikLayer *sb = (VikLayer *)b;
424
425   // Default ascending order
426   gint answer = g_strcmp0 ( sa->name, sb->name );
427
428   if ( GPOINTER_TO_INT(order) ) {
429     // Invert sort order for ascending order
430     answer = -answer;
431   }
432
433   return answer;
434 }
435
436 static void aggregate_layer_sort_a2z ( gpointer lav[2] )
437 {
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) );
441 }
442
443 static void aggregate_layer_sort_z2a ( gpointer lav[2] )
444 {
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) );
448 }
449
450 /**
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
454  *
455  * Returns: A list of #vik_trw_waypoint_list_t
456  */
457 static GList* aggregate_layer_waypoint_create_list ( VikLayer *vl, gpointer user_data )
458 {
459   VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
460
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 );
464
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 );
468   while ( 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) ) ) );
471
472     waypoints_and_layers = g_list_concat ( waypoints_and_layers, vik_trw_layer_build_waypoint_list_t ( VIK_TRW_LAYER(layers->data), waypoints ) );
473
474     layers = g_list_next ( layers );
475   }
476   g_list_free ( layers );
477
478   return waypoints_and_layers;
479 }
480
481 static void aggregate_layer_waypoint_list_dialog ( gpointer data[2] )
482 {
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 );
486   g_free ( title );
487 }
488
489 /**
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
493  *
494  * Returns: A list of #vik_trw_track_list_t
495  */
496 static GList* aggregate_layer_track_create_list ( VikLayer *vl, gpointer user_data )
497 {
498   VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
499
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 );
503
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 );
507   while ( 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) ) ) );
511
512     tracks_and_layers = g_list_concat ( tracks_and_layers, vik_trw_layer_build_track_list_t ( VIK_TRW_LAYER(layers->data), tracks ) );
513
514     layers = g_list_next ( layers );
515   }
516   g_list_free ( layers );
517
518   return tracks_and_layers;
519 }
520
521 static void aggregate_layer_track_list_dialog ( gpointer data[2] )
522 {
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 );
526   g_free ( title );
527 }
528
529 /**
530  * aggregate_layer_analyse_close:
531  *
532  * Stuff to do on dialog closure
533  */
534 static void aggregate_layer_analyse_close ( GtkWidget *dialog, gint resp, VikLayer* vl )
535 {
536   VikAggregateLayer *val = VIK_AGGREGATE_LAYER(vl);
537   gtk_widget_destroy ( dialog );
538   val->tracks_analysis_dialog = NULL;
539 }
540
541 static void aggregate_layer_analyse ( gpointer data[2] )
542 {
543   VikAggregateLayer *val = VIK_AGGREGATE_LAYER(data[0]);
544
545   // There can only be one!
546   if ( val->tracks_analysis_dialog )
547     return;
548
549   val->tracks_analysis_dialog = vik_trw_layer_analyse_this ( VIK_GTK_WINDOW_FROM_LAYER(VIK_LAYER(val)),
550                                                              VIK_LAYER(val)->name,
551                                                              VIK_LAYER(val),
552                                                              NULL,
553                                                              aggregate_layer_track_create_list,
554                                                              aggregate_layer_analyse_close );
555 }
556
557 static void aggregate_layer_add_menu_items ( VikAggregateLayer *val, GtkMenu *menu, gpointer vlp )
558 {
559   // Data to pass on in menu functions
560   static gpointer data[2];
561   data[0] = val;
562   data[1] = vlp;
563
564   GtkWidget *item = gtk_menu_item_new();
565   gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
566   gtk_widget_show ( item );
567
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 );
573
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 );
579
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 );
585
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 );
591
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 );
598
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 );
604
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 );
610
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 );
615
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 );
621
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 );
627 }
628
629 static void disconnect_layer_signal ( VikLayer *vl, VikAggregateLayer *val )
630 {
631   g_assert(DISCONNECT_UPDATE_SIGNAL(vl,val)==1);
632 }
633
634 void vik_aggregate_layer_free ( VikAggregateLayer *val )
635 {
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 );
641 }
642
643 static void delete_layer_iter ( VikLayer *vl )
644 {
645   if ( vl->realized )
646     vik_treeview_item_delete ( vl->vt, &(vl->iter) );
647 }
648
649 void vik_aggregate_layer_clear ( VikAggregateLayer *val )
650 {
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;
656 }
657
658 gboolean vik_aggregate_layer_delete ( VikAggregateLayer *val, GtkTreeIter *iter )
659 {
660   VikLayer *l = VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, iter ) );
661   gboolean was_visible = l->visible;
662
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 );
667
668   return was_visible;
669 }
670
671 #if 0
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 )
674 {
675   GList *iter = val->children;
676   gboolean found_rej = FALSE;
677   if (!iter)
678     return FALSE;
679   while (iter->next)
680     iter = iter->next;
681
682   while ( iter )
683   {
684     /* if this layer "accepts" the tool call */
685     if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == layer_type )
686     {
687       if ( tool_func ( VIK_LAYER(iter->data), event, vvp ) )
688         return 0;
689       else
690         found_rej = TRUE;
691     }
692
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 )
695     {
696       gint rv = vik_aggregate_layer_tool(VIK_AGGREGATE_LAYER(iter->data), layer_type, tool_func, event, vvp);
697       if ( rv == 0 )
698         return 0;
699       else if ( rv == 2 )
700         found_rej = TRUE;
701     }
702     iter = iter->prev;
703   }
704   return found_rej ? 2 : 1; /* no one wanted to accept the tool call in this layer */
705 }
706 #endif 
707
708 VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer *val, VikLayerTypeEnum type )
709 {
710   VikLayer *rv;
711   GList *ls = val->children;
712   if (!ls)
713     return NULL;
714   while (ls->next)
715     ls = ls->next;
716
717   while ( ls )
718   {
719     VikLayer *vl = VIK_LAYER(ls->data);
720     if ( vl->visible && vl->type == type )
721       return vl;
722     else if ( vl->visible && vl->type == VIK_LAYER_AGGREGATE )
723     {
724       rv = vik_aggregate_layer_get_top_visible_layer_of_type(VIK_AGGREGATE_LAYER(vl), type);
725       if ( rv )
726         return rv;
727     }
728     ls = ls->prev;
729   }
730   return NULL;
731 }
732
733 GList *vik_aggregate_layer_get_all_layers_of_type(VikAggregateLayer *val, GList *layers, VikLayerTypeEnum type, gboolean include_invisible)
734 {
735   GList *l = layers;
736   GList *children = val->children;
737   VikLayer *vl;
738   if (!children)
739     return layers;
740
741   // Where appropriate *don't* include non-visible layers
742   while (children) {
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);
748     }
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 */
752     }
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))) {
758             /*
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)));
761             */
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);
765             int layer = 0;
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;
769             }
770             g_list_free(gps_trw_layers);
771           }
772         }
773       }
774     }
775     children = children->next;
776   }
777   return l;
778 }
779
780 void vik_aggregate_layer_realize ( VikAggregateLayer *val, VikTreeview *vt, GtkTreeIter *layer_iter )
781 {
782   GList *i = val->children;
783   GtkTreeIter iter;
784   VikLayer *vl = VIK_LAYER(val);
785   VikLayer *vli;
786   while ( i )
787   {
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 );
794     i = i->next;
795   }
796 }
797
798 const GList *vik_aggregate_layer_get_children ( VikAggregateLayer *val )
799 {
800   return val->children;
801 }
802
803 gboolean vik_aggregate_layer_is_empty ( VikAggregateLayer *val )
804 {
805   if ( val->children )
806     return FALSE;
807   return TRUE;
808 }
809
810 static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path )
811 {
812   VikTreeview *vt = VIK_LAYER(val_src)->vt;
813   VikLayer *vl = vik_treeview_item_get_pointer(vt, src_item_iter);
814   GtkTreeIter dest_iter;
815   gchar *dp;
816   gboolean target_exists;
817
818   dp = gtk_tree_path_to_string(dest_path);
819   target_exists = vik_treeview_get_iter_from_path_str(vt, &dest_iter, dp);
820
821   /* vik_aggregate_layer_delete unrefs, but we don't want that here.
822    * we're still using the layer. */
823   g_object_ref ( vl );
824   vik_aggregate_layer_delete(val_src, src_item_iter);
825
826   if (target_exists) {
827     vik_aggregate_layer_insert_layer(val_dest, vl, &dest_iter);
828   } else {
829     vik_aggregate_layer_insert_layer(val_dest, vl, NULL); /* append */
830   }
831   g_free(dp);
832 }
833