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