]>
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 ); | |
0a6cab71 AF |
30 | static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *len ); |
31 | static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp ); | |
50a14534 | 32 | static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode ); |
70a23263 | 33 | static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path ); |
50a14534 EB |
34 | |
35 | VikLayerInterface vik_aggregate_layer_interface = { | |
36 | "Aggregate", | |
37 | &aggregatelayer_pixbuf, | |
38 | ||
39 | NULL, | |
40 | 0, | |
41 | ||
42 | NULL, | |
43 | 0, | |
44 | NULL, | |
45 | 0, | |
46 | ||
47 | (VikLayerFuncCreate) vik_aggregate_layer_create, | |
48 | (VikLayerFuncRealize) vik_aggregate_layer_realize, | |
49 | (VikLayerFuncPostRead) NULL, | |
50 | (VikLayerFuncFree) vik_aggregate_layer_free, | |
51 | ||
52 | (VikLayerFuncProperties) NULL, | |
53 | (VikLayerFuncDraw) vik_aggregate_layer_draw, | |
54 | (VikLayerFuncChangeCoordMode) aggregate_layer_change_coord_mode, | |
55 | ||
56 | (VikLayerFuncAddMenuItems) NULL, | |
57 | (VikLayerFuncSublayerAddMenuItems) NULL, | |
58 | ||
59 | (VikLayerFuncSublayerRenameRequest) NULL, | |
60 | (VikLayerFuncSublayerToggleVisible) NULL, | |
61 | ||
62 | (VikLayerFuncCopy) aggregate_layer_copy, | |
0a6cab71 AF |
63 | (VikLayerFuncMarshall) aggregate_layer_marshall, |
64 | (VikLayerFuncUnmarshall) aggregate_layer_unmarshall, | |
50a14534 EB |
65 | |
66 | (VikLayerFuncSetParam) NULL, | |
67 | (VikLayerFuncGetParam) NULL, | |
68 | ||
69 | (VikLayerFuncReadFileData) NULL, | |
70 | (VikLayerFuncWriteFileData) NULL, | |
71 | ||
72 | (VikLayerFuncCopyItem) NULL, | |
73 | (VikLayerFuncPasteItem) NULL, | |
74 | (VikLayerFuncFreeCopiedItem) NULL, | |
70a23263 | 75 | (VikLayerFuncDragDropRequest) aggregate_layer_drag_drop_request, |
50a14534 EB |
76 | }; |
77 | ||
78 | struct _VikAggregateLayer { | |
79 | VikLayer vl; | |
80 | GList *children; | |
81 | }; | |
82 | ||
83 | GType vik_aggregate_layer_get_type () | |
84 | { | |
85 | static GType val_type = 0; | |
86 | ||
87 | if (!val_type) | |
88 | { | |
89 | static const GTypeInfo val_info = | |
90 | { | |
91 | sizeof (VikAggregateLayerClass), | |
92 | NULL, /* base_init */ | |
93 | NULL, /* base_finalize */ | |
94 | NULL, /* class init */ | |
95 | NULL, /* class_finalize */ | |
96 | NULL, /* class_data */ | |
97 | sizeof (VikAggregateLayer), | |
98 | 0, | |
99 | NULL /* instance init */ | |
100 | }; | |
101 | val_type = g_type_register_static ( VIK_LAYER_TYPE, "VikAggregateLayer", &val_info, 0 ); | |
102 | } | |
103 | ||
104 | return val_type; | |
105 | } | |
106 | ||
107 | VikAggregateLayer *vik_aggregate_layer_create (VikViewport *vp) | |
108 | { | |
109 | VikAggregateLayer *rv = vik_aggregate_layer_new (); | |
110 | vik_layer_rename ( VIK_LAYER(rv), vik_aggregate_layer_interface.name ); | |
111 | return rv; | |
112 | } | |
113 | ||
114 | static VikAggregateLayer *aggregate_layer_copy ( VikAggregateLayer *val, gpointer vp ) | |
115 | { | |
116 | VikAggregateLayer *rv = vik_aggregate_layer_new (); | |
117 | VikLayer *child_layer; | |
118 | GList *child = val->children; | |
119 | while ( child ) | |
120 | { | |
121 | child_layer = vik_layer_copy ( VIK_LAYER(child->data), vp ); | |
0a6cab71 | 122 | if ( child_layer ) { |
50a14534 EB |
123 | rv->children = g_list_append ( rv->children, child_layer ); |
124 | g_signal_connect_swapped ( G_OBJECT(child_layer), "update", G_CALLBACK(vik_layer_emit_update), rv ); | |
0a6cab71 | 125 | } |
50a14534 EB |
126 | child = child->next; |
127 | } | |
128 | return rv; | |
129 | } | |
130 | ||
0a6cab71 AF |
131 | static void aggregate_layer_marshall( VikAggregateLayer *val, guint8 **data, gint *datalen ) |
132 | { | |
133 | GList *child = val->children; | |
134 | VikLayer *child_layer; | |
135 | guint8 *ld; | |
136 | gint ll; | |
137 | GByteArray* b = g_byte_array_new (); | |
138 | gint len; | |
139 | ||
140 | #define alm_append(obj, sz) \ | |
141 | len = (sz); \ | |
142 | g_byte_array_append ( b, (guint8 *)&len, sizeof(len) ); \ | |
143 | g_byte_array_append ( b, (guint8 *)(obj), len ); | |
144 | ||
145 | vik_layer_marshall_params(VIK_LAYER(val), &ld, &ll); | |
146 | alm_append(ld, ll); | |
147 | g_free(ld); | |
148 | ||
149 | while (child) { | |
150 | child_layer = VIK_LAYER(child->data); | |
151 | vik_layer_marshall ( child_layer, &ld, &ll ); | |
152 | if (ld) { | |
153 | alm_append(ld, ll); | |
154 | g_free(ld); | |
155 | } | |
156 | child = child->next; | |
157 | } | |
158 | *data = b->data; | |
159 | *datalen = b->len; | |
160 | g_byte_array_free(b, FALSE); | |
161 | #undef alm_append | |
162 | } | |
163 | ||
164 | static VikAggregateLayer *aggregate_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp ) | |
165 | { | |
166 | #define alm_size (*(gint *)data) | |
167 | #define alm_next \ | |
168 | len -= sizeof(gint) + alm_size; \ | |
169 | data += sizeof(gint) + alm_size; | |
170 | ||
171 | VikAggregateLayer *rv = vik_aggregate_layer_new(); | |
172 | VikLayer *child_layer; | |
173 | ||
174 | vik_layer_unmarshall_params ( VIK_LAYER(rv), data+sizeof(gint), alm_size, vvp ); | |
175 | alm_next; | |
176 | ||
177 | while (len>0) { | |
178 | child_layer = vik_layer_unmarshall ( data + sizeof(gint), alm_size, vvp ); | |
179 | if (child_layer) { | |
180 | rv->children = g_list_append ( rv->children, child_layer ); | |
181 | g_signal_connect_swapped ( G_OBJECT(child_layer), "update", G_CALLBACK(vik_layer_emit_update), rv ); | |
182 | } | |
183 | alm_next; | |
184 | } | |
185 | // g_print("aggregate_layer_unmarshall ended with len=%d\n", len); | |
186 | return rv; | |
187 | #undef alm_size | |
188 | #undef alm_next | |
189 | } | |
190 | ||
50a14534 EB |
191 | VikAggregateLayer *vik_aggregate_layer_new () |
192 | { | |
193 | VikAggregateLayer *val = VIK_AGGREGATE_LAYER ( g_object_new ( VIK_AGGREGATE_LAYER_TYPE, NULL ) ); | |
194 | vik_layer_init ( VIK_LAYER(val), VIK_LAYER_AGGREGATE ); | |
195 | val->children = NULL; | |
196 | return val; | |
197 | } | |
198 | ||
199 | void vik_aggregate_layer_insert_layer ( VikAggregateLayer *val, VikLayer *l, GtkTreeIter *replace_iter ) | |
200 | { | |
50a14534 | 201 | GtkTreeIter iter; |
e673b75f | 202 | |
50a14534 EB |
203 | if ( VIK_LAYER(val)->realized ) |
204 | { | |
205 | vik_treeview_insert_layer ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), &iter, l->name, val, l, l->type, l->type, replace_iter ); | |
206 | if ( ! l->visible ) | |
207 | vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE ); | |
208 | vik_layer_realize ( l, VIK_LAYER(val)->vt, &iter ); | |
209 | ||
210 | if ( val->children == NULL ) | |
211 | vik_treeview_expand ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter) ); | |
212 | } | |
50a14534 | 213 | |
e673b75f AF |
214 | if (replace_iter) { |
215 | GList *theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, replace_iter ) ); | |
216 | val->children = g_list_insert ( val->children, l, g_list_position(val->children,theone)+1 ); | |
217 | } else { | |
218 | val->children = g_list_append ( val->children, l ); | |
219 | } | |
50a14534 EB |
220 | g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update), val ); |
221 | } | |
222 | ||
223 | void vik_aggregate_layer_add_layer ( VikAggregateLayer *val, VikLayer *l ) | |
224 | { | |
225 | GtkTreeIter iter; | |
226 | ||
227 | if ( VIK_LAYER(val)->realized ) | |
228 | { | |
229 | vik_treeview_add_layer ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter), &iter, l->name, val, l, l->type, l->type); | |
230 | if ( ! l->visible ) | |
231 | vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE ); | |
232 | vik_layer_realize ( l, VIK_LAYER(val)->vt, &iter ); | |
233 | ||
234 | if ( val->children == NULL ) | |
235 | vik_treeview_expand ( VIK_LAYER(val)->vt, &(VIK_LAYER(val)->iter) ); | |
236 | } | |
237 | ||
238 | val->children = g_list_append ( val->children, l ); | |
239 | g_signal_connect_swapped ( G_OBJECT(l), "update", G_CALLBACK(vik_layer_emit_update), val ); | |
240 | } | |
241 | ||
242 | void vik_aggregate_layer_move_layer ( VikAggregateLayer *val, GtkTreeIter *child_iter, gboolean up ) | |
243 | { | |
244 | GList *theone, *first, *second; | |
245 | vik_treeview_move_item ( VIK_LAYER(val)->vt, child_iter, up ); | |
246 | ||
247 | theone = g_list_find ( val->children, vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, child_iter ) ); | |
248 | ||
249 | g_assert ( theone != NULL ); | |
250 | ||
251 | /* the old switcheroo */ | |
252 | if ( up && theone->next ) | |
253 | { | |
254 | first = theone; | |
255 | second = theone->next; | |
256 | } | |
257 | else if ( !up && theone->prev ) | |
258 | { | |
259 | first = theone->prev; | |
260 | second = theone; | |
261 | } | |
262 | else | |
263 | return; | |
264 | ||
265 | first->next = second->next; | |
266 | second->prev = first->prev; | |
267 | first->prev = second; | |
268 | second->next = first; | |
269 | ||
270 | /* second is now first */ | |
271 | ||
272 | if ( second->prev ) | |
273 | second->prev->next = second; | |
274 | if ( first->next ) | |
275 | first->next->prev = first; | |
276 | ||
277 | if ( second->prev == NULL ) | |
278 | val->children = second; | |
279 | } | |
280 | ||
281 | void vik_aggregate_layer_draw ( VikAggregateLayer *val, gpointer data ) | |
282 | { | |
283 | g_list_foreach ( val->children, (GFunc)(vik_layer_draw), data ); | |
284 | } | |
285 | ||
286 | static void aggregate_layer_change_coord_mode ( VikAggregateLayer *val, VikCoordMode mode ) | |
287 | { | |
288 | GList *iter = val->children; | |
289 | while ( iter ) | |
290 | { | |
291 | vik_layer_change_coord_mode ( VIK_LAYER(iter->data), mode ); | |
292 | iter = iter->next; | |
293 | } | |
294 | } | |
295 | ||
296 | static void disconnect_layer_signal ( VikLayer *vl, VikAggregateLayer *val ) | |
297 | { | |
298 | g_assert(DISCONNECT_UPDATE_SIGNAL(vl,val)==1); | |
299 | } | |
300 | ||
301 | void vik_aggregate_layer_free ( VikAggregateLayer *val ) | |
302 | { | |
303 | g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val ); | |
304 | g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL ); | |
305 | g_list_free ( val->children ); | |
306 | } | |
307 | ||
308 | static void delete_layer_iter ( VikLayer *vl ) | |
309 | { | |
310 | if ( vl->realized ) | |
311 | vik_treeview_item_delete ( vl->vt, &(vl->iter) ); | |
312 | } | |
313 | ||
314 | void vik_aggregate_layer_clear ( VikAggregateLayer *val ) | |
315 | { | |
316 | g_list_foreach ( val->children, (GFunc)(disconnect_layer_signal), val ); | |
317 | g_list_foreach ( val->children, (GFunc)(delete_layer_iter), NULL ); | |
318 | g_list_foreach ( val->children, (GFunc)(g_object_unref), NULL ); | |
319 | g_list_free ( val->children ); | |
320 | val->children = NULL; | |
321 | } | |
322 | ||
323 | gboolean vik_aggregate_layer_delete ( VikAggregateLayer *val, GtkTreeIter *iter ) | |
324 | { | |
325 | VikLayer *l = VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(val)->vt, iter ) ); | |
326 | gboolean was_visible = l->visible; | |
327 | ||
328 | vik_treeview_item_delete ( VIK_LAYER(val)->vt, iter ); | |
329 | val->children = g_list_remove ( val->children, l ); | |
330 | g_assert(DISCONNECT_UPDATE_SIGNAL(l,val)==1); | |
331 | g_object_unref ( l ); | |
332 | ||
333 | return was_visible; | |
334 | } | |
335 | ||
941aa6e9 | 336 | #if 0 |
50a14534 EB |
337 | /* returns 0 == we're good, 1 == didn't find any layers, 2 == got rejected */ |
338 | guint vik_aggregate_layer_tool ( VikAggregateLayer *val, guint16 layer_type, VikToolInterfaceFunc tool_func, GdkEventButton *event, VikViewport *vvp ) | |
339 | { | |
340 | GList *iter = val->children; | |
341 | gboolean found_rej = FALSE; | |
342 | if (!iter) | |
343 | return FALSE; | |
344 | while (iter->next) | |
345 | iter = iter->next; | |
346 | ||
347 | while ( iter ) | |
348 | { | |
349 | /* if this layer "accepts" the tool call */ | |
350 | if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == layer_type ) | |
351 | { | |
352 | if ( tool_func ( VIK_LAYER(iter->data), event, vvp ) ) | |
353 | return 0; | |
354 | else | |
355 | found_rej = TRUE; | |
356 | } | |
357 | ||
358 | /* recursive -- try the same for the child aggregate layer. */ | |
359 | else if ( VIK_LAYER(iter->data)->visible && VIK_LAYER(iter->data)->type == VIK_LAYER_AGGREGATE ) | |
360 | { | |
361 | gint rv = vik_aggregate_layer_tool(VIK_AGGREGATE_LAYER(iter->data), layer_type, tool_func, event, vvp); | |
362 | if ( rv == 0 ) | |
363 | return 0; | |
364 | else if ( rv == 2 ) | |
365 | found_rej = TRUE; | |
366 | } | |
367 | iter = iter->prev; | |
368 | } | |
369 | return found_rej ? 2 : 1; /* no one wanted to accept the tool call in this layer */ | |
370 | } | |
941aa6e9 | 371 | #endif |
50a14534 EB |
372 | |
373 | VikLayer *vik_aggregate_layer_get_top_visible_layer_of_type ( VikAggregateLayer *val, gint type ) | |
374 | { | |
375 | VikLayer *rv; | |
376 | GList *ls = val->children; | |
377 | if (!ls) | |
378 | return NULL; | |
379 | while (ls->next) | |
380 | ls = ls->next; | |
381 | ||
382 | while ( ls ) | |
383 | { | |
384 | if ( VIK_LAYER(ls->data)->visible && VIK_LAYER(ls->data)->type == type ) | |
385 | return VIK_LAYER(ls->data); | |
386 | else if ( VIK_LAYER(ls->data)->visible && VIK_LAYER(ls->data)->type == VIK_LAYER_AGGREGATE ) | |
387 | { | |
388 | rv = vik_aggregate_layer_get_top_visible_layer_of_type(VIK_AGGREGATE_LAYER(ls->data), type); | |
389 | if ( rv ) | |
390 | return rv; | |
391 | } | |
392 | ls = ls->prev; | |
393 | } | |
394 | return NULL; | |
395 | } | |
396 | ||
397 | void vik_aggregate_layer_realize ( VikAggregateLayer *val, VikTreeview *vt, GtkTreeIter *layer_iter ) | |
398 | { | |
399 | GList *i = val->children; | |
400 | GtkTreeIter iter; | |
401 | while ( i ) | |
402 | { | |
403 | vik_treeview_add_layer ( VIK_LAYER(val)->vt, layer_iter, &iter, VIK_LAYER(i->data)->name, val, | |
404 | VIK_LAYER(i->data), VIK_LAYER(i->data)->type, VIK_LAYER(i->data)->type ); | |
405 | if ( ! VIK_LAYER(i->data)->visible ) | |
406 | vik_treeview_item_set_visible ( VIK_LAYER(val)->vt, &iter, FALSE ); | |
407 | vik_layer_realize ( VIK_LAYER(i->data), VIK_LAYER(val)->vt, &iter ); | |
408 | i = i->next; | |
409 | } | |
410 | } | |
411 | ||
412 | const GList *vik_aggregate_layer_get_children ( VikAggregateLayer *val ) | |
413 | { | |
414 | return val->children; | |
415 | } | |
416 | ||
417 | gboolean vik_aggregate_layer_is_empty ( VikAggregateLayer *val ) | |
418 | { | |
419 | if ( val->children ) | |
420 | return FALSE; | |
421 | return TRUE; | |
422 | } | |
70a23263 AF |
423 | |
424 | static void aggregate_layer_drag_drop_request ( VikAggregateLayer *val_src, VikAggregateLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path ) | |
425 | { | |
426 | VikTreeview *vt = VIK_LAYER(val_src)->vt; | |
427 | VikLayer *vl = vik_treeview_item_get_pointer(vt, src_item_iter); | |
e673b75f | 428 | GtkTreeIter dest_iter; |
c390aa71 AF |
429 | gchar *dp; |
430 | gboolean target_exists; | |
e673b75f | 431 | |
c390aa71 AF |
432 | dp = gtk_tree_path_to_string(dest_path); |
433 | target_exists = vik_treeview_get_iter_from_path_str(vt, &dest_iter, dp); | |
801ce684 EB |
434 | |
435 | /* vik_aggregate_layer_delete unrefs, but we don't want that here. | |
436 | * we're still using the layer. */ | |
437 | g_object_ref ( vl ); | |
438 | vik_aggregate_layer_delete(val_src, src_item_iter); | |
439 | ||
c390aa71 | 440 | if (target_exists) { |
e673b75f AF |
441 | vik_aggregate_layer_insert_layer(val_dest, vl, &dest_iter); |
442 | } else { | |
443 | vik_aggregate_layer_insert_layer(val_dest, vl, NULL); /* append */ | |
444 | } | |
c390aa71 | 445 | g_free(dp); |
70a23263 AF |
446 | } |
447 |