QA: more generic way of computing size of an array
[viking.git] / src / viklayerspanel.c
blob1fa9e5533aa7a4d071416c26c6af9f56bf234118
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include "viking.h"
28 #include <string.h>
30 #include <glib/gi18n.h>
32 enum {
33 VLP_UPDATE_SIGNAL,
34 VLP_LAST_SIGNAL
37 static void layers_panel_class_init ( VikLayersPanelClass *klass );
38 static void layers_panel_init ( VikLayersPanel *vlp );
39 static void layers_item_edited (VikLayersPanel *vlp, GtkTreeIter *iter, const gchar *new_text);
40 static void layers_item_toggled (VikLayersPanel *vlp, GtkTreeIter *iter);
42 static guint layers_panel_signals[VLP_LAST_SIGNAL] = { 0 };
44 static GObjectClass *parent_class;
46 struct _VikLayersPanel {
47 GtkVBox vbox;
49 VikAggregateLayer *toplayer;
50 GtkTreeIter toplayer_iter;
52 VikTreeview *vt;
53 VikViewport *vvp; /* reference */
55 GtkItemFactory *popup_factory;
58 static GtkItemFactoryEntry base_entries[] = {
59 { N_("/C_ut"), NULL, (GtkItemFactoryCallback) vik_layers_panel_cut_selected, -1, "<StockItem>", GTK_STOCK_CUT },
60 { N_("/_Copy"), NULL, (GtkItemFactoryCallback) vik_layers_panel_copy_selected, -1, "<StockItem>", GTK_STOCK_COPY },
61 { N_("/_Paste"), NULL, (GtkItemFactoryCallback) vik_layers_panel_paste_selected, -1, "<StockItem>", GTK_STOCK_PASTE },
62 { N_("/_Delete"), NULL, (GtkItemFactoryCallback) vik_layers_panel_delete_selected, -1, "<StockItem>", GTK_STOCK_DELETE },
63 { N_("/New Layer"), NULL, NULL, -1, "<Branch>" },
66 #define NUM_BASE_ENTRIES (sizeof(base_entries)/sizeof(base_entries[0]))
68 static void layers_item_toggled (VikLayersPanel *vlp, GtkTreeIter *iter);
69 static void layers_item_edited (VikLayersPanel *vlp, GtkTreeIter *iter, const gchar *new_text);
70 static void menu_popup_cb (VikLayersPanel *vlp);
71 static void layers_popup_cb (VikLayersPanel *vlp);
72 static void layers_popup ( VikLayersPanel *vlp, GtkTreeIter *iter, gint mouse_button );
73 static gboolean layers_button_press_cb (VikLayersPanel *vlp, GdkEventButton *event);
74 static void layers_move_item ( VikLayersPanel *vlp, gboolean up );
75 static void layers_move_item_up ( VikLayersPanel *vlp );
76 static void layers_move_item_down ( VikLayersPanel *vlp );
77 static void layers_panel_finalize ( GObject *gob );
79 GType vik_layers_panel_get_type()
81 static GType vlp_type = 0;
83 if (!vlp_type)
85 static const GTypeInfo vlp_info =
87 sizeof (VikLayersPanelClass),
88 NULL, /* base_init */
89 NULL, /* base_finalize */
90 (GClassInitFunc) layers_panel_class_init,
91 NULL, /* class_finalize */
92 NULL, /* class_data */
93 sizeof (VikLayersPanel),
95 (GInstanceInitFunc) layers_panel_init,
97 vlp_type = g_type_register_static ( GTK_TYPE_VBOX, "VikLayersPanel", &vlp_info, 0 );
100 return vlp_type;
103 static void layers_panel_class_init ( VikLayersPanelClass *klass )
105 GObjectClass *object_class;
107 object_class = G_OBJECT_CLASS (klass);
109 object_class->finalize = layers_panel_finalize;
111 parent_class = g_type_class_peek_parent (klass);
113 layers_panel_signals[VLP_UPDATE_SIGNAL] = g_signal_new ( "update", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (VikLayersPanelClass, update), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
116 VikLayersPanel *vik_layers_panel_new ()
118 return VIK_LAYERS_PANEL ( g_object_new ( VIK_LAYERS_PANEL_TYPE, NULL ) );
121 void vik_layers_panel_set_viewport ( VikLayersPanel *vlp, VikViewport *vvp )
123 vlp->vvp = vvp;
124 /* TODO: also update GCs (?) */
127 VikViewport *vik_layers_panel_get_viewport ( VikLayersPanel *vlp )
129 return vlp->vvp;
132 static void layers_panel_init ( VikLayersPanel *vlp )
134 GtkWidget *hbox;
135 GtkWidget *addbutton, *addimage;
136 GtkWidget *removebutton, *removeimage;
137 GtkWidget *upbutton, *upimage;
138 GtkWidget *downbutton, *downimage;
139 GtkWidget *cutbutton, *cutimage;
140 GtkWidget *copybutton, *copyimage;
141 GtkWidget *pastebutton, *pasteimage;
142 GtkWidget *scrolledwindow;
143 GtkItemFactoryEntry entry;
144 guint i, tmp;
146 vlp->vvp = NULL;
148 hbox = gtk_hbox_new ( TRUE, 2 );
149 vlp->vt = vik_treeview_new ( );
151 vlp->toplayer = vik_aggregate_layer_new ();
152 vik_layer_rename ( VIK_LAYER(vlp->toplayer), _("Top Layer"));
153 g_signal_connect_swapped ( G_OBJECT(vlp->toplayer), "update", G_CALLBACK(vik_layers_panel_emit_update), vlp );
155 vik_treeview_add_layer ( vlp->vt, NULL, &(vlp->toplayer_iter), VIK_LAYER(vlp->toplayer)->name, NULL, vlp->toplayer, VIK_LAYER_AGGREGATE, VIK_LAYER_AGGREGATE );
156 vik_layer_realize ( VIK_LAYER(vlp->toplayer), vlp->vt, &(vlp->toplayer_iter) );
158 g_signal_connect_swapped ( vlp->vt, "popup_menu", G_CALLBACK(menu_popup_cb), vlp);
159 g_signal_connect_swapped ( vlp->vt, "button_press_event", G_CALLBACK(layers_button_press_cb), vlp);
160 g_signal_connect_swapped ( vlp->vt, "item_toggled", G_CALLBACK(layers_item_toggled), vlp);
161 g_signal_connect_swapped ( vlp->vt, "item_edited", G_CALLBACK(layers_item_edited), vlp);
163 /* Add button */
164 addimage = gtk_image_new_from_stock ( GTK_STOCK_ADD, GTK_ICON_SIZE_SMALL_TOOLBAR );
165 addbutton = gtk_button_new ( );
166 gtk_container_add ( GTK_CONTAINER(addbutton), addimage );
167 gtk_box_pack_start ( GTK_BOX(hbox), addbutton, TRUE, TRUE, 0 );
168 g_signal_connect_swapped ( G_OBJECT(addbutton), "clicked", G_CALLBACK(layers_popup_cb), vlp );
169 /* Remove button */
170 removeimage = gtk_image_new_from_stock ( GTK_STOCK_REMOVE, GTK_ICON_SIZE_SMALL_TOOLBAR );
171 removebutton = gtk_button_new ( );
172 gtk_container_add ( GTK_CONTAINER(removebutton), removeimage );
173 gtk_box_pack_start ( GTK_BOX(hbox), removebutton, TRUE, TRUE, 0 );
174 g_signal_connect_swapped ( G_OBJECT(removebutton), "clicked", G_CALLBACK(vik_layers_panel_delete_selected), vlp );
175 /* Up button */
176 upimage = gtk_image_new_from_stock ( GTK_STOCK_GO_UP, GTK_ICON_SIZE_SMALL_TOOLBAR );
177 upbutton = gtk_button_new ( );
178 gtk_container_add ( GTK_CONTAINER(upbutton), upimage );
179 gtk_box_pack_start ( GTK_BOX(hbox), upbutton, TRUE, TRUE, 0 );
180 g_signal_connect_swapped ( G_OBJECT(upbutton), "clicked", G_CALLBACK(layers_move_item_up), vlp );
181 /* Down button */
182 downimage = gtk_image_new_from_stock ( GTK_STOCK_GO_DOWN, GTK_ICON_SIZE_SMALL_TOOLBAR );
183 downbutton = gtk_button_new ( );
184 gtk_container_add ( GTK_CONTAINER(downbutton), downimage );
185 gtk_box_pack_start ( GTK_BOX(hbox), downbutton, TRUE, TRUE, 0 );
186 g_signal_connect_swapped ( G_OBJECT(downbutton), "clicked", G_CALLBACK(layers_move_item_down), vlp );
187 /* Cut button */
188 cutimage = gtk_image_new_from_stock ( GTK_STOCK_CUT, GTK_ICON_SIZE_SMALL_TOOLBAR );
189 cutbutton = gtk_button_new ( );
190 gtk_container_add ( GTK_CONTAINER(cutbutton), cutimage );
191 gtk_box_pack_start ( GTK_BOX(hbox), cutbutton, TRUE, TRUE, 0 );
192 g_signal_connect_swapped ( G_OBJECT(cutbutton), "clicked", G_CALLBACK(vik_layers_panel_cut_selected), vlp );
193 /* Copy button */
194 copyimage = gtk_image_new_from_stock ( GTK_STOCK_COPY, GTK_ICON_SIZE_SMALL_TOOLBAR );
195 copybutton = gtk_button_new ( );
196 gtk_container_add ( GTK_CONTAINER(copybutton), copyimage );
197 gtk_box_pack_start ( GTK_BOX(hbox), copybutton, TRUE, TRUE, 0 );
198 g_signal_connect_swapped ( G_OBJECT(copybutton), "clicked", G_CALLBACK(vik_layers_panel_copy_selected), vlp );
199 /* Paste button */
200 pasteimage = gtk_image_new_from_stock ( GTK_STOCK_PASTE, GTK_ICON_SIZE_SMALL_TOOLBAR );
201 pastebutton = gtk_button_new ( );
202 gtk_container_add ( GTK_CONTAINER(pastebutton),pasteimage );
203 gtk_box_pack_start ( GTK_BOX(hbox), pastebutton, TRUE, TRUE, 0 );
204 g_signal_connect_swapped ( G_OBJECT(pastebutton), "clicked", G_CALLBACK(vik_layers_panel_paste_selected), vlp );
206 scrolledwindow = gtk_scrolled_window_new ( NULL, NULL );
207 gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
208 gtk_container_add ( GTK_CONTAINER(scrolledwindow), GTK_WIDGET(vlp->vt) );
210 gtk_box_pack_start ( GTK_BOX(vlp), scrolledwindow, TRUE, TRUE, 0 );
211 gtk_box_pack_start ( GTK_BOX(vlp), hbox, FALSE, FALSE, 0 );
213 vlp->popup_factory = gtk_item_factory_new ( GTK_TYPE_MENU, "<main>", NULL );
214 gtk_item_factory_create_items ( vlp->popup_factory, NUM_BASE_ENTRIES, base_entries, vlp );
215 for ( i = 0; i < VIK_LAYER_NUM_TYPES; i++ )
217 /* TODO: FIXME: if name has a '/' in it it will get all messed up. why not have an itemfactory field with
218 name, icon, shortcut, etc.? */
219 entry.path = g_strdup_printf("%s/New %s Layer", base_entries[NUM_BASE_ENTRIES-1].path, vik_layer_get_interface(i)->name );
220 entry.accelerator = NULL;
221 entry.callback = (GtkItemFactoryCallback) vik_layers_panel_new_layer;
222 entry.callback_action = i;
223 if ( vik_layer_get_interface(i)->icon )
225 entry.item_type = "<ImageItem>";
226 entry.extra_data = gdk_pixdata_serialize ( vik_layer_get_interface(i)->icon, &tmp );
228 else
229 entry.item_type = "<Item>";
231 gtk_item_factory_create_item ( vlp->popup_factory, &entry, vlp, 1 );
232 g_free ( (gpointer) entry.extra_data );
233 g_free ( entry.path );
237 void vik_layers_panel_emit_update ( VikLayersPanel *vlp )
239 g_signal_emit ( G_OBJECT(vlp), layers_panel_signals[VLP_UPDATE_SIGNAL], 0 );
242 static void layers_item_toggled (VikLayersPanel *vlp, GtkTreeIter *iter)
244 gboolean visible;
245 gpointer p;
246 gint type;
248 /* get type and data */
249 type = vik_treeview_item_get_type ( vlp->vt, iter );
250 p = vik_treeview_item_get_pointer ( vlp->vt, iter );
252 switch ( type )
254 case VIK_TREEVIEW_TYPE_LAYER:
255 visible = (VIK_LAYER(p)->visible ^= 1);
256 vik_layer_emit_update_although_invisible ( VIK_LAYER(p) ); /* set trigger for half-drawn */
257 break;
258 case VIK_TREEVIEW_TYPE_SUBLAYER:
259 visible = vik_layer_sublayer_toggle_visible ( VIK_LAYER(vik_treeview_item_get_parent(vlp->vt, iter)),
260 vik_treeview_item_get_data(vlp->vt, iter), p);
261 vik_layer_emit_update_although_invisible ( VIK_LAYER(vik_treeview_item_get_parent(vlp->vt, iter)) );
262 break;
263 default: return;
266 vik_treeview_item_set_visible ( vlp->vt, iter, visible );
269 static void layers_item_edited (VikLayersPanel *vlp, GtkTreeIter *iter, const gchar *new_text)
271 if ( vik_treeview_item_get_type ( vlp->vt, iter ) == VIK_TREEVIEW_TYPE_LAYER )
273 VikLayer *l;
275 /* get iter and layer */
276 l = VIK_LAYER ( vik_treeview_item_get_pointer ( vlp->vt, iter ) );
278 if ( strcmp ( l->name, new_text ) != 0 )
280 vik_layer_rename ( l, new_text );
281 vik_treeview_item_set_name ( vlp->vt, iter, l->name );
284 else
286 const gchar *name = vik_layer_sublayer_rename_request ( vik_treeview_item_get_parent ( vlp->vt, iter ), new_text, vlp, vik_treeview_item_get_data ( vlp->vt, iter ), vik_treeview_item_get_pointer ( vlp->vt, iter ), iter );
287 if ( name )
288 vik_treeview_item_set_name ( vlp->vt, iter, name);
292 static gboolean layers_button_press_cb ( VikLayersPanel *vlp, GdkEventButton *event )
294 if (event->button == 3)
296 GtkTreeIter iter;
297 if ( vik_treeview_get_iter_at_pos ( vlp->vt, &iter, event->x, event->y ) )
299 layers_popup ( vlp, &iter, 3 );
300 vik_treeview_item_select ( vlp->vt, &iter );
302 else
303 layers_popup ( vlp, NULL, 3 );
304 return TRUE;
306 return FALSE;
309 static void layers_popup ( VikLayersPanel *vlp, GtkTreeIter *iter, gint mouse_button )
311 GtkMenu *menu = NULL;
314 if ( iter )
316 if ( vik_treeview_item_get_type ( vlp->vt, iter ) == VIK_TREEVIEW_TYPE_LAYER )
318 VikLayer *layer = VIK_LAYER(vik_treeview_item_get_pointer ( vlp->vt, iter ));
320 if ( layer->type == VIK_LAYER_AGGREGATE )
321 menu = GTK_MENU(gtk_item_factory_get_widget ( vlp->popup_factory, "<main>" ));
322 else
324 GtkWidget *del, *prop;
325 VikStdLayerMenuItem menu_selection = vik_layer_get_menu_items_selection(layer);
327 menu = GTK_MENU ( gtk_menu_new () );
329 if (menu_selection & VIK_MENU_ITEM_PROPERTY) {
330 prop = gtk_image_menu_item_new_from_stock ( GTK_STOCK_PROPERTIES, NULL );
331 g_signal_connect_swapped ( G_OBJECT(prop), "activate", G_CALLBACK(vik_layers_panel_properties), vlp );
332 gtk_menu_shell_append (GTK_MENU_SHELL (menu), prop);
333 gtk_widget_show ( prop );
336 if (menu_selection & VIK_MENU_ITEM_CUT) {
337 del = gtk_image_menu_item_new_from_stock ( GTK_STOCK_CUT, NULL );
338 g_signal_connect_swapped ( G_OBJECT(del), "activate", G_CALLBACK(vik_layers_panel_cut_selected), vlp );
339 gtk_menu_shell_append (GTK_MENU_SHELL (menu), del);
340 gtk_widget_show ( del );
343 if (menu_selection & VIK_MENU_ITEM_COPY) {
344 del = gtk_image_menu_item_new_from_stock ( GTK_STOCK_COPY, NULL );
345 g_signal_connect_swapped ( G_OBJECT(del), "activate", G_CALLBACK(vik_layers_panel_copy_selected), vlp );
346 gtk_menu_shell_append (GTK_MENU_SHELL (menu), del);
347 gtk_widget_show ( del );
350 if (menu_selection & VIK_MENU_ITEM_PASTE) {
351 del = gtk_image_menu_item_new_from_stock ( GTK_STOCK_PASTE, NULL );
352 g_signal_connect_swapped ( G_OBJECT(del), "activate", G_CALLBACK(vik_layers_panel_paste_selected), vlp );
353 gtk_menu_shell_append (GTK_MENU_SHELL (menu), del);
354 gtk_widget_show ( del );
357 if (menu_selection & VIK_MENU_ITEM_DELETE) {
358 del = gtk_image_menu_item_new_from_stock ( GTK_STOCK_DELETE, NULL );
359 g_signal_connect_swapped ( G_OBJECT(del), "activate", G_CALLBACK(vik_layers_panel_delete_selected), vlp );
360 gtk_menu_shell_append (GTK_MENU_SHELL (menu), del);
361 gtk_widget_show ( del );
364 vik_layer_add_menu_items ( layer, menu, vlp );
367 else
369 menu = GTK_MENU ( gtk_menu_new () );
370 if ( ! vik_layer_sublayer_add_menu_items ( vik_treeview_item_get_parent ( vlp->vt, iter ), menu, vlp, vik_treeview_item_get_data ( vlp->vt, iter ), vik_treeview_item_get_pointer ( vlp->vt, iter ), iter ) )
372 gtk_widget_destroy ( GTK_WIDGET(menu) );
373 return;
375 /* TODO: specific things for different types */
378 else
379 menu = GTK_MENU(gtk_item_factory_get_widget ( vlp->popup_factory, base_entries[NUM_BASE_ENTRIES-1].path ));
380 gtk_menu_popup ( menu, NULL, NULL, NULL, NULL, mouse_button, gtk_get_current_event_time() );
383 static void menu_popup_cb ( VikLayersPanel *vlp )
385 GtkTreeIter iter;
386 layers_popup ( vlp, vik_treeview_get_selected_iter ( vlp->vt, &iter ) ? &iter : NULL, 0 );
389 static void layers_popup_cb ( VikLayersPanel *vlp )
391 layers_popup ( vlp, NULL, 0 );
394 gboolean vik_layers_panel_new_layer ( VikLayersPanel *vlp, gint type )
396 VikLayer *l;
397 g_assert ( vlp->vvp );
398 l = vik_layer_create ( type, vlp->vvp, VIK_GTK_WINDOW_FROM_WIDGET(vlp), TRUE );
399 if ( l )
401 vik_layers_panel_add_layer ( vlp, l );
402 vik_layers_panel_emit_update ( vlp );
403 return TRUE;
405 return FALSE;
408 void vik_layers_panel_add_layer ( VikLayersPanel *vlp, VikLayer *l )
410 GtkTreeIter iter;
411 GtkTreeIter *replace_iter = NULL;
413 /* could be something different so we have to do this */
414 vik_layer_change_coord_mode ( l, vik_viewport_get_coord_mode(vlp->vvp) );
416 if ( ! vik_treeview_get_selected_iter ( vlp->vt, &iter ) )
417 vik_aggregate_layer_add_layer ( vlp->toplayer, l );
418 else
420 VikAggregateLayer *addtoagg;
421 if (vik_treeview_item_get_type ( vlp->vt, &iter ) == VIK_TREEVIEW_TYPE_LAYER )
423 if ( IS_VIK_AGGREGATE_LAYER(vik_treeview_item_get_pointer ( vlp->vt, &iter )) )
424 addtoagg = VIK_AGGREGATE_LAYER(vik_treeview_item_get_pointer ( vlp->vt, &iter ));
425 else {
426 VikLayer *vl = VIK_LAYER(vik_treeview_item_get_parent ( vlp->vt, &iter ));
427 while ( ! IS_VIK_AGGREGATE_LAYER(vl) ) {
428 iter = vl->iter;
429 vl = VIK_LAYER(vik_treeview_item_get_parent ( vlp->vt, &vl->iter ));
430 g_assert ( vl->realized );
432 addtoagg = VIK_AGGREGATE_LAYER(vl);
433 replace_iter = &iter;
436 else
438 /* a sublayer is selected, first get its parent (layer), then find the layer's parent (aggr. layer) */
439 VikLayer *vl = VIK_LAYER(vik_treeview_item_get_parent ( vlp->vt, &iter ));
440 replace_iter = &(vl->iter);
441 g_assert ( vl->realized );
442 VikLayer *grandpa = (vik_treeview_item_get_parent ( vlp->vt, &(vl->iter) ) );
443 if (IS_VIK_AGGREGATE_LAYER(grandpa))
444 addtoagg = VIK_AGGREGATE_LAYER(grandpa);
445 else {
446 addtoagg = vlp->toplayer;
447 replace_iter = &grandpa->iter;
450 if ( replace_iter )
451 vik_aggregate_layer_insert_layer ( addtoagg, l, replace_iter );
452 else
453 vik_aggregate_layer_add_layer ( addtoagg, l );
457 static void layers_move_item ( VikLayersPanel *vlp, gboolean up )
459 GtkTreeIter iter;
460 VikAggregateLayer *parent;
462 /* TODO: deactivate the buttons and stuff */
463 if ( ! vik_treeview_get_selected_iter ( vlp->vt, &iter ) )
464 return;
466 vik_treeview_select_iter ( vlp->vt, &iter ); /* cancel any layer-name editing going on... */
468 if ( vik_treeview_item_get_type ( vlp->vt, &iter ) == VIK_TREEVIEW_TYPE_LAYER )
470 parent = VIK_AGGREGATE_LAYER(vik_treeview_item_get_parent ( vlp->vt, &iter ));
471 if ( parent ) /* not toplevel */
473 vik_aggregate_layer_move_layer ( parent, &iter, up );
474 vik_layers_panel_emit_update ( vlp );
479 gboolean vik_layers_panel_properties ( VikLayersPanel *vlp )
481 GtkTreeIter iter;
482 g_assert ( vlp->vvp );
484 if ( vik_treeview_get_selected_iter ( vlp->vt, &iter ) && vik_treeview_item_get_type ( vlp->vt, &iter ) == VIK_TREEVIEW_TYPE_LAYER )
486 if ( vik_treeview_item_get_data ( vlp->vt, &iter ) == VIK_LAYER_AGGREGATE )
487 a_dialog_info_msg ( VIK_GTK_WINDOW_FROM_WIDGET(vlp), _("Aggregate Layers have no settable properties.") );
488 vik_layer_properties ( VIK_LAYER( vik_treeview_item_get_pointer ( vlp->vt, &iter ) ), vlp->vvp );
489 return TRUE;
491 else
492 return FALSE;
495 void vik_layers_panel_draw_all ( VikLayersPanel *vlp )
497 if ( vlp->vvp && VIK_LAYER(vlp->toplayer)->visible )
498 vik_aggregate_layer_draw ( vlp->toplayer, vlp->vvp );
501 void vik_layers_panel_draw_all_using_viewport ( VikLayersPanel *vlp, VikViewport *vvp )
503 if ( vlp->vvp && VIK_LAYER(vlp->toplayer)->visible )
504 vik_aggregate_layer_draw ( vlp->toplayer, vvp );
507 void vik_layers_panel_cut_selected ( VikLayersPanel *vlp )
509 gint type;
510 GtkTreeIter iter;
512 g_return_if_fail ( vik_treeview_get_selected_iter ( vlp->vt, &iter ) );
514 type = vik_treeview_item_get_type ( vlp->vt, &iter );
516 if ( type == VIK_TREEVIEW_TYPE_LAYER )
518 VikAggregateLayer *parent = vik_treeview_item_get_parent ( vlp->vt, &iter );
520 if ( parent )
523 /* reset trigger if trigger deleted */
524 if ( vik_layers_panel_get_selected ( vlp ) == vik_viewport_get_trigger ( vlp->vvp ) )
525 vik_viewport_set_trigger ( vlp->vvp, NULL );
527 a_clipboard_copy_selected ( vlp );
528 if ( vik_aggregate_layer_delete ( parent, &iter ) )
529 vik_layers_panel_emit_update ( vlp );
531 else
532 a_dialog_info_msg ( VIK_GTK_WINDOW_FROM_WIDGET(vlp), _("You cannot cut the Top Layer.") );
536 void vik_layers_panel_copy_selected ( VikLayersPanel *vlp )
538 gint type;
539 GtkTreeIter iter;
541 g_return_if_fail ( vik_treeview_get_selected_iter ( vlp->vt, &iter ) );
543 type = vik_treeview_item_get_type ( vlp->vt, &iter );
545 if ( type == VIK_TREEVIEW_TYPE_LAYER ) {
546 a_clipboard_copy_selected ( vlp );
550 void vik_layers_panel_paste_selected ( VikLayersPanel *vlp )
552 GtkTreeIter iter;
553 g_return_if_fail ( vik_treeview_get_selected_iter ( vlp->vt, &iter ) );
554 a_clipboard_paste ( vlp );
557 void vik_layers_panel_delete_selected ( VikLayersPanel *vlp )
559 gint type;
560 GtkTreeIter iter;
562 g_return_if_fail ( vik_treeview_get_selected_iter ( vlp->vt, &iter ) );
564 type = vik_treeview_item_get_type ( vlp->vt, &iter );
566 if ( type == VIK_TREEVIEW_TYPE_LAYER )
568 VikAggregateLayer *parent = vik_treeview_item_get_parent ( vlp->vt, &iter );
569 if ( parent )
571 /* reset trigger if trigger deleted */
572 if ( vik_layers_panel_get_selected ( vlp ) == vik_viewport_get_trigger ( vlp->vvp ) )
573 vik_viewport_set_trigger ( vlp->vvp, NULL );
575 if (IS_VIK_AGGREGATE_LAYER(parent)) {
576 if ( vik_aggregate_layer_delete ( parent, &iter ) )
577 vik_layers_panel_emit_update ( vlp );
580 else
581 a_dialog_info_msg ( VIK_GTK_WINDOW_FROM_WIDGET(vlp), _("You cannot delete the Top Layer.") );
583 else if (type == VIK_TREEVIEW_TYPE_SUBLAYER) {
584 VikLayer *sel = vik_layers_panel_get_selected ( vlp );
585 if ( vik_layer_get_interface(sel->type)->delete_item ) {
586 gint subtype = vik_treeview_item_get_data( vlp->vt, &iter);
587 vik_layer_get_interface(sel->type)->delete_item ( sel, subtype, vik_treeview_item_get_pointer(sel->vt, &iter) );
592 VikLayer *vik_layers_panel_get_selected ( VikLayersPanel *vlp )
594 GtkTreeIter iter, parent;
595 gint type;
597 if ( ! vik_treeview_get_selected_iter ( vlp->vt, &iter ) )
598 return NULL;
600 type = vik_treeview_item_get_type ( vlp->vt, &iter );
602 while ( type != VIK_TREEVIEW_TYPE_LAYER )
604 if ( ! vik_treeview_item_get_parent_iter ( vlp->vt, &iter, &parent ) )
605 return NULL;
606 iter = parent;
607 type = vik_treeview_item_get_type ( vlp->vt, &iter );
610 return VIK_LAYER( vik_treeview_item_get_pointer ( vlp->vt, &iter ) );
613 static void layers_move_item_up ( VikLayersPanel *vlp )
615 layers_move_item ( vlp, TRUE );
618 static void layers_move_item_down ( VikLayersPanel *vlp )
620 layers_move_item ( vlp, FALSE );
623 #if 0
624 gboolean vik_layers_panel_tool ( VikLayersPanel *vlp, guint16 layer_type, VikToolInterfaceFunc tool_func, GdkEventButton *event, VikViewport *vvp )
626 VikLayer *vl = vik_layers_panel_get_selected ( vlp );
627 if ( vl && vl->type == layer_type )
629 tool_func ( vl, event, vvp );
630 return TRUE;
632 else if ( VIK_LAYER(vlp->toplayer)->visible &&
633 vik_aggregate_layer_tool ( vlp->toplayer, layer_type, tool_func, event, vvp ) != 1 ) /* either accepted or rejected, but a layer was found */
634 return TRUE;
635 return FALSE;
637 #endif
639 VikLayer *vik_layers_panel_get_layer_of_type ( VikLayersPanel *vlp, gint type )
641 VikLayer *rv = vik_layers_panel_get_selected ( vlp );
642 if ( rv == NULL || rv->type != type )
643 if ( VIK_LAYER(vlp->toplayer)->visible )
644 return vik_aggregate_layer_get_top_visible_layer_of_type ( vlp->toplayer, type );
645 else
646 return NULL;
647 else
648 return rv;
651 GList *vik_layers_panel_get_all_layers_of_type(VikLayersPanel *vlp, gint type)
653 GList *layers = NULL;
655 return (vik_aggregate_layer_get_all_layers_of_type ( vlp->toplayer, layers, type ));
658 VikAggregateLayer *vik_layers_panel_get_top_layer ( VikLayersPanel *vlp )
660 return vlp->toplayer;
663 void vik_layers_panel_clear ( VikLayersPanel *vlp )
665 if ( (! vik_aggregate_layer_is_empty(vlp->toplayer)) && a_dialog_overwrite ( VIK_GTK_WINDOW_FROM_WIDGET(vlp), _("Are you sure you wish to delete all layers?"), NULL ) )
666 vik_aggregate_layer_clear ( vlp->toplayer ); /* simply deletes all layers */
669 void vik_layers_panel_change_coord_mode ( VikLayersPanel *vlp, VikCoordMode mode )
671 vik_layer_change_coord_mode ( VIK_LAYER(vlp->toplayer), mode );
674 static void layers_panel_finalize ( GObject *gob )
676 VikLayersPanel *vlp = VIK_LAYERS_PANEL ( gob );
677 g_object_unref ( VIK_LAYER(vlp->toplayer) );
678 g_object_unref ( G_OBJECT(vlp->popup_factory) );
679 G_OBJECT_CLASS(parent_class)->finalize(gob);