]> git.street.me.uk Git - andy/viking.git/blame - src/vikaggregatelayer.c
Added ruler drawing to the ruler tool
[andy/viking.git] / src / vikaggregatelayer.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22#include "viking.h"
23#include "vikaggregatelayer_pixmap.h"
24
25#include <string.h>
26
27#define DISCONNECT_UPDATE_SIGNAL(vl, val) g_signal_handlers_disconnect_matched(vl, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, val)
28
29static VikAggregateLayer *aggregate_layer_copy ( VikAggregateLayer *val, gpointer vp );
30static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode );
70a23263 31static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path );
50a14534
EB
32
33VikLayerInterface vik_aggregate_layer_interface = {
34 "Aggregate",
35 &aggregatelayer_pixbuf,
36
37 NULL,
38 0,
39
40 NULL,
41 0,
42 NULL,
43 0,
44
45 (VikLayerFuncCreate) vik_aggregate_layer_create,
46 (VikLayerFuncRealize) vik_aggregate_layer_realize,
47 (VikLayerFuncPostRead) NULL,
48 (VikLayerFuncFree) vik_aggregate_layer_free,
49
50 (VikLayerFuncProperties) NULL,
51 (VikLayerFuncDraw) vik_aggregate_layer_draw,
52 (VikLayerFuncChangeCoordMode) aggregate_layer_change_coord_mode,
53
54 (VikLayerFuncAddMenuItems) NULL,
55 (VikLayerFuncSublayerAddMenuItems) NULL,
56
57 (VikLayerFuncSublayerRenameRequest) NULL,
58 (VikLayerFuncSublayerToggleVisible) NULL,
59
60 (VikLayerFuncCopy) aggregate_layer_copy,
61
62 (VikLayerFuncSetParam) NULL,
63 (VikLayerFuncGetParam) NULL,
64
65 (VikLayerFuncReadFileData) NULL,
66 (VikLayerFuncWriteFileData) NULL,
67
68 (VikLayerFuncCopyItem) NULL,
69 (VikLayerFuncPasteItem) NULL,
70 (VikLayerFuncFreeCopiedItem) NULL,
70a23263 71 (VikLayerFuncDragDropRequest) aggregate_layer_drag_drop_request,
50a14534
EB
72};
73
74struct _VikAggregateLayer {
75 VikLayer vl;
76 GList *children;
77};
78
79GType vik_aggregate_layer_get_type ()
80{
81 static GType val_type = 0;
82
83 if (!val_type)
84 {
85 static const GTypeInfo val_info =
86 {
87 sizeof (VikAggregateLayerClass),
88 NULL, /* base_init */
89 NULL, /* base_finalize */
90 NULL, /* class init */
91 NULL, /* class_finalize */
92 NULL, /* class_data */
93 sizeof (VikAggregateLayer),
94 0,
95 NULL /* instance init */
96 };
97 val_type = g_type_register_static ( VIK_LAYER_TYPE, "VikAggregateLayer", &val_info, 0 );
98 }
99
100 return val_type;
101}
102
103VikAggregateLayer *vik_aggregate_layer_create (VikViewport *vp)
104{
105 VikAggregateLayer *rv = vik_aggregate_layer_new ();
106 vik_layer_rename ( VIK_LAYER(rv), vik_aggregate_layer_interface.name );
107 return rv;
108}
109
110static VikAggregateLayer *aggregate_layer_copy ( VikAggregateLayer *val, gpointer vp )
111{
112 VikAggregateLayer *rv = vik_aggregate_layer_new ();
113 VikLayer *child_layer;
114 GList *child = val->children;
115 while ( child )
116 {
117 child_layer = vik_layer_copy ( VIK_LAYER(child->data), vp );
118 if ( child_layer )
119 rv->children = g_list_append ( rv->children, child_layer );
120 g_signal_connect_swapped ( G_OBJECT(child_layer), "update", G_CALLBACK(vik_layer_emit_update), rv );
121 child = child->next;
122 }
123 return rv;
124}
125
126VikAggregateLayer *vik_aggregate_layer_new ()
127{
128 VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( g_object_new ( VIK_AGGREGATE_LAYER_TYPE, NULL ) );
129 vik_layer_init ( VIK_LAYER(val), VIK_LAYER_AGGREGATE );
130 val->children = NULL;
131 return val;
132}
133
134void vik_aggregate_layer_insert_layer ( VikAggregateLayer *val, VikLayer *l, GtkTreeIter *replace_iter )
135{
50a14534 136 GtkTreeIter iter;
e673b75f 137
50a14534
EB
138 if ( VIK_LAYER(val)->realized )
139 {
140 vik_treeview_insert_layer ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), &iter, l->name, val, l, l->type, l->type, replace_iter );
141 if ( ! l->visible )
142 vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE );
143 vik_layer_realize ( l, VIK_LAYER(val)->vt, &iter );
144
145 if ( val->children == NULL )
146 vik_treeview_expand ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter) );
147 }
50a14534 148
e673b75f
AF
149 if (replace_iter) {
150 GList *theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, replace_iter ) );
151 val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone)+1 );
152 } else {
153 val->children = g_list_append ( val->children, l );
154 }
50a14534
EB
155 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update), val );
156}
157
158void vik_aggregate_layer_add_layer ( VikAggregateLayer *val, VikLayer *l )
159{
160 GtkTreeIter iter;
161
162 if ( VIK_LAYER(val)->realized )
163 {
164 vik_treeview_add_layer ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), &iter, l->name, val, l, l->type, l->type);
165 if ( ! l->visible )
166 vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE );
167 vik_layer_realize ( l, VIK_LAYER(val)->vt, &iter );
168
169 if ( val->children == NULL )
170 vik_treeview_expand ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter) );
171 }
172
173 val->children = g_list_append ( val->children, l );
174 g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update), val );
175}
176
177void vik_aggregate_layer_move_layer ( VikAggregateLayer *val, GtkTreeIter *child_iter, gboolean up )
178{
179 GList *theone, *first, *second;
180 vik_treeview_move_item ( VIK_LAYER(val)->vt, child_iter, up );
181
182 theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, child_iter ) );
183
184 g_assert ( theone != NULL );
185
186 /* the old switcheroo */
187 if ( up && theone->next )
188 {
189 first = theone;
190 second = theone->next;
191 }
192 else if ( !up && theone->prev )
193 {
194 first = theone->prev;
195 second = theone;
196 }
197 else
198 return;
199
200 first->next = second->next;
201 second->prev = first->prev;
202 first->prev = second;
203 second->next = first;
204
205 /* second is now first */
206
207 if ( second->prev )
208 second->prev->next = second;
209 if ( first->next )
210 first->next->prev = first;
211
212 if ( second->prev == NULL )
213 val->children = second;
214}
215
216void vik_aggregate_layer_draw ( VikAggregateLayer *val, gpointer data )
217{
218 g_list_foreach ( val->children, (GFunc)(vik_layer_draw), data );
219}
220
221static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode )
222{
223 GList *iter = val->children;
224 while ( iter )
225 {
226 vik_layer_change_coord_mode ( VIK_LAYER(iter->data), mode );
227 iter = iter->next;
228 }
229}
230
231static void disconnect_layer_signal ( VikLayer *vl, VikAggregateLayer *val )
232{
233 g_assert(DISCONNECT_UPDATE_SIGNAL(vl,val)==1);
234}
235
236void vik_aggregate_layer_free ( VikAggregateLayer *val )
237{
238 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
239 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
240 g_list_free ( val->children );
241}
242
243static void delete_layer_iter ( VikLayer *vl )
244{
245 if ( vl->realized )
246 vik_treeview_item_delete ( vl->vt, &(vl->iter) );
247}
248
249void vik_aggregate_layer_clear ( VikAggregateLayer *val )
250{
251 g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val );
252 g_list_foreach ( val->children, (GFunc)(delete_layer_iter), NULL );
253 g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL );
254 g_list_free ( val->children );
255 val->children = NULL;
256}
257
258gboolean vik_aggregate_layer_delete ( VikAggregateLayer *val, GtkTreeIter *iter )
259{
260 VikLayer *l = VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, iter ) );
261 gboolean was_visible = l->visible;
262
263 vik_treeview_item_delete ( VIK_LAYER(val)->vt, iter );
264 val->children = g_list_remove ( val->children, l );
265 g_assert(DISCONNECT_UPDATE_SIGNAL(l,val)==1);
266 g_object_unref ( l );
267
268 return was_visible;
269}
270
271/* returns 0 == we're good, 1 == didn't find any layers, 2 == got rejected */
272guint vik_aggregate_layer_tool ( VikAggregateLayer *val, guint16 layer_type, VikToolInterfaceFunc tool_func, GdkEventButton *event, VikViewport *vvp )
273{
274 GList *iter = val->children;
275 gboolean found_rej = FALSE;
276 if (!iter)
277 return FALSE;
278 while (iter->next)
279 iter = iter->next;
280
281 while ( iter )
282 {
283 /* if this layer "accepts" the tool call */
284 if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == layer_type )
285 {
286 if ( tool_func ( VIK_LAYER(iter->data), event, vvp ) )
287 return 0;
288 else
289 found_rej = TRUE;
290 }
291
292 /* recursive -- try the same for the child aggregate layer. */
293 else if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == VIK_LAYER_AGGREGATE )
294 {
295 gint rv = vik_aggregate_layer_tool(VIK_AGGREGATE_LAYER(iter->data), layer_type, tool_func, event, vvp);
296 if ( rv == 0 )
297 return 0;
298 else if ( rv == 2 )
299 found_rej = TRUE;
300 }
301 iter = iter->prev;
302 }
303 return found_rej ? 2 : 1; /* no one wanted to accept the tool call in this layer */
304}
305
306VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer *val, gint type )
307{
308 VikLayer *rv;
309 GList *ls = val->children;
310 if (!ls)
311 return NULL;
312 while (ls->next)
313 ls = ls->next;
314
315 while ( ls )
316 {
317 if ( VIK_LAYER(ls->data)->visible && VIK_LAYER(ls->data)->type == type )
318 return VIK_LAYER(ls->data);
319 else if ( VIK_LAYER(ls->data)->visible && VIK_LAYER(ls->data)->type == VIK_LAYER_AGGREGATE )
320 {
321 rv = vik_aggregate_layer_get_top_visible_layer_of_type(VIK_AGGREGATE_LAYER(ls->data), type);
322 if ( rv )
323 return rv;
324 }
325 ls = ls->prev;
326 }
327 return NULL;
328}
329
330void vik_aggregate_layer_realize ( VikAggregateLayer *val, VikTreeview *vt, GtkTreeIter *layer_iter )
331{
332 GList *i = val->children;
333 GtkTreeIter iter;
334 while ( i )
335 {
336 vik_treeview_add_layer ( VIK_LAYER(val)->vt, layer_iter, &iter, VIK_LAYER(i->data)->name, val,
337 VIK_LAYER(i->data), VIK_LAYER(i->data)->type, VIK_LAYER(i->data)->type );
338 if ( ! VIK_LAYER(i->data)->visible )
339 vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE );
340 vik_layer_realize ( VIK_LAYER(i->data), VIK_LAYER(val)->vt, &iter );
341 i = i->next;
342 }
343}
344
345const GList *vik_aggregate_layer_get_children ( VikAggregateLayer *val )
346{
347 return val->children;
348}
349
350gboolean vik_aggregate_layer_is_empty ( VikAggregateLayer *val )
351{
352 if ( val->children )
353 return FALSE;
354 return TRUE;
355}
70a23263 356
e673b75f
AF
357static void aggregate_layer_remove ( VikAggregateLayer *val, GtkTreeIter *iter )
358{
359 VikLayer *l = VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, iter ) );
360 vik_treeview_item_delete ( VIK_LAYER(val)->vt, iter );
361 val->children = g_list_remove ( val->children, l );
362}
363
70a23263
AF
364static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path )
365{
366 VikTreeview *vt = VIK_LAYER(val_src)->vt;
367 VikLayer *vl = vik_treeview_item_get_pointer(vt, src_item_iter);
e673b75f
AF
368 gchar *ps;
369 GtkTreeIter dest_iter;
370
371 ps = gtk_tree_path_to_string(dest_path);
372 if (vik_treeview_get_iter_from_path_str(vt, &dest_iter, ps)) {
373 vik_aggregate_layer_insert_layer(val_dest, vl, &dest_iter);
374 } else {
375 vik_aggregate_layer_insert_layer(val_dest, vl, NULL); /* append */
376 }
377 aggregate_layer_remove(val_src, src_item_iter);
378 g_free(ps);
70a23263
AF
379}
380