Parent Directory
|
Revision Log
Bug fixes incl. a memory leak
1 | harbaum | 1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. | ||
3 | * | ||
4 | * This file is part of OSM2Go. | ||
5 | * | ||
6 | * OSM2Go 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 3 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * OSM2Go 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 OSM2Go. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | |||
20 | #include "appdata.h" | ||
21 | |||
22 | achadwick | 99 | /* UI sizes */ |
23 | harbaum | 152 | /* TH: All this dialog size stuff should imho go into one central place */ |
24 | achadwick | 99 | |
25 | #ifdef USE_HILDON | ||
26 | // Making the dialog a little wider makes it less "crowded" | ||
27 | static const guint LIST_OF_RELATIONS_DIALOG_WIDTH = 500; | ||
28 | static const guint LIST_OF_RELATIONS_DIALOG_HEIGHT = 300; | ||
29 | static const guint LIST_OF_MEMBERS_DIALOG_WIDTH = 500; | ||
30 | static const guint LIST_OF_MEMBERS_DIALOG_HEIGHT = 300; | ||
31 | #else | ||
32 | // Desktop mode dialogs should be narrower and taller | ||
33 | static const guint LIST_OF_RELATIONS_DIALOG_WIDTH = 475; | ||
34 | static const guint LIST_OF_RELATIONS_DIALOG_HEIGHT = 350; | ||
35 | static const guint LIST_OF_MEMBERS_DIALOG_WIDTH = 450; | ||
36 | static const guint LIST_OF_MEMBERS_DIALOG_HEIGHT = 350; | ||
37 | #endif | ||
38 | |||
39 | |||
40 | harbaum | 1 | /* --------------- relation dialog for an item (node or way) ----------- */ |
41 | |||
42 | typedef struct { | ||
43 | relation_item_t *item; | ||
44 | appdata_t *appdata; | ||
45 | harbaum | 148 | GtkWidget *dialog, *list; |
46 | harbaum | 1 | GtkListStore *store; |
47 | } relitem_context_t; | ||
48 | |||
49 | enum { | ||
50 | RELITEM_COL_SELECTED = 0, | ||
51 | RELITEM_COL_TYPE, | ||
52 | RELITEM_COL_ROLE, | ||
53 | RELITEM_COL_NAME, | ||
54 | RELITEM_COL_DATA, | ||
55 | RELITEM_NUM_COLS | ||
56 | }; | ||
57 | |||
58 | typedef struct role_chain_s { | ||
59 | char *role; | ||
60 | struct role_chain_s *next; | ||
61 | } role_chain_t; | ||
62 | |||
63 | harbaum | 73 | static gboolean relation_add_item(GtkWidget *parent, |
64 | harbaum | 1 | relation_t *relation, relation_item_t *item) { |
65 | role_chain_t *chain = NULL, **chainP = &chain; | ||
66 | |||
67 | printf("add item of type %d to relation #%ld\n", | ||
68 | item->type, relation->id); | ||
69 | |||
70 | /* ask the user for the role of the new item in this relation */ | ||
71 | |||
72 | /* collect roles first */ | ||
73 | member_t *member = relation->member; | ||
74 | while(member) { | ||
75 | if(member->role) { | ||
76 | /* check if this role has already been saved */ | ||
77 | gboolean already_stored = FALSE; | ||
78 | role_chain_t *crole = chain; | ||
79 | while(crole) { | ||
80 | if(strcasecmp(crole->role, member->role) == 0) already_stored = TRUE; | ||
81 | crole = crole->next; | ||
82 | } | ||
83 | |||
84 | /* not stored yet: attach it */ | ||
85 | if(!already_stored) { | ||
86 | *chainP = g_new0(role_chain_t, 1); | ||
87 | (*chainP)->role = g_strdup(member->role); | ||
88 | chainP = &(*chainP)->next; | ||
89 | } | ||
90 | } | ||
91 | member = member->next; | ||
92 | } | ||
93 | |||
94 | /* ------------------ role dialog ---------------- */ | ||
95 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Select role"), | ||
96 | GTK_WINDOW(parent), GTK_DIALOG_MODAL, | ||
97 | GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, | ||
98 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, | ||
99 | NULL); | ||
100 | |||
101 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); | ||
102 | |||
103 | char *type = osm_tag_get_by_key(relation->tag, "type"); | ||
104 | |||
105 | char *info_str = NULL; | ||
106 | if(type) info_str = g_strdup_printf(_("In relation of type: %s"), type); | ||
107 | else info_str = g_strdup_printf(_("In relation #%ld"), relation->id); | ||
108 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), | ||
109 | gtk_label_new(info_str)); | ||
110 | g_free(info_str); | ||
111 | |||
112 | char *name = osm_tag_get_by_key(relation->tag, "name"); | ||
113 | if(name) | ||
114 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), | ||
115 | gtk_label_new(name)); | ||
116 | |||
117 | GtkWidget *hbox = gtk_hbox_new(FALSE, 8); | ||
118 | gtk_box_pack_start_defaults(GTK_BOX(hbox), gtk_label_new(_("Role:"))); | ||
119 | |||
120 | GtkWidget *entry = NULL; | ||
121 | if(chain) { | ||
122 | entry = gtk_combo_box_entry_new_text(); | ||
123 | |||
124 | /* fill combo box with presets */ | ||
125 | while(chain) { | ||
126 | role_chain_t *next = chain->next; | ||
127 | gtk_combo_box_append_text(GTK_COMBO_BOX(entry), chain->role); | ||
128 | g_free(chain); | ||
129 | chain = next; | ||
130 | } | ||
131 | } else | ||
132 | entry = gtk_entry_new(); | ||
133 | |||
134 | gtk_box_pack_start_defaults(GTK_BOX(hbox), entry); | ||
135 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); | ||
136 | |||
137 | gtk_widget_show_all(dialog); | ||
138 | if(GTK_RESPONSE_ACCEPT != gtk_dialog_run(GTK_DIALOG(dialog))) { | ||
139 | printf("user clicked cancel\n"); | ||
140 | gtk_widget_destroy(dialog); | ||
141 | harbaum | 73 | return FALSE; |
142 | harbaum | 1 | } |
143 | |||
144 | printf("user clicked ok\n"); | ||
145 | |||
146 | /* get role from dialog */ | ||
147 | char *ptr = NULL; | ||
148 | |||
149 | if(GTK_IS_COMBO_BOX(entry)) | ||
150 | ptr = gtk_combo_box_get_active_text(GTK_COMBO_BOX(entry)); | ||
151 | else | ||
152 | ptr = (char*)gtk_entry_get_text(GTK_ENTRY(entry)); | ||
153 | |||
154 | char *role = NULL; | ||
155 | if(ptr && strlen(ptr)) role = g_strdup(ptr); | ||
156 | |||
157 | gtk_widget_destroy(dialog); | ||
158 | |||
159 | /* search end of member chain */ | ||
160 | member_t **memberP = &relation->member; | ||
161 | while(*memberP) memberP = &(*memberP)->next; | ||
162 | |||
163 | /* create new member */ | ||
164 | *memberP = g_new0(member_t, 1); | ||
165 | (*memberP)->type = item->type; | ||
166 | (*memberP)->role = role; | ||
167 | switch(item->type) { | ||
168 | case NODE: | ||
169 | (*memberP)->node = item->node; | ||
170 | break; | ||
171 | case WAY: | ||
172 | (*memberP)->way = item->way; | ||
173 | break; | ||
174 | case RELATION: | ||
175 | (*memberP)->relation = item->relation; | ||
176 | break; | ||
177 | default: | ||
178 | g_assert((item->type == NODE)||(item->type == WAY)|| | ||
179 | (item->type == RELATION)); | ||
180 | break; | ||
181 | } | ||
182 | |||
183 | relation->flags |= OSM_FLAG_DIRTY; | ||
184 | harbaum | 73 | return TRUE; |
185 | harbaum | 1 | } |
186 | |||
187 | harbaum | 73 | static void relation_remove_item(relation_t *relation, relation_item_t *item) { |
188 | |||
189 | printf("remove item of type %d from relation #%ld\n", | ||
190 | item->type, relation->id); | ||
191 | |||
192 | member_t **member = &relation->member; | ||
193 | while(*member) { | ||
194 | if(((*member)->type == item->type) && | ||
195 | (((item->type == NODE) && (item->node == (*member)->node)) || | ||
196 | ((item->type == WAY) && (item->way == (*member)->way)) || | ||
197 | ((item->type == RELATION) && (item->relation == (*member)->relation)))) { | ||
198 | |||
199 | member_t *next = (*member)->next; | ||
200 | osm_member_free(*member); | ||
201 | *member = next; | ||
202 | |||
203 | relation->flags |= OSM_FLAG_DIRTY; | ||
204 | |||
205 | return; | ||
206 | } else | ||
207 | member = &(*member)->next; | ||
208 | } | ||
209 | g_assert(0); | ||
210 | } | ||
211 | |||
212 | harbaum | 76 | static void relation_item_list_selected(relitem_context_t *context, |
213 | harbaum | 73 | gboolean selected) { |
214 | |||
215 | harbaum | 148 | list_button_enable(context->list, LIST_BUTTON_REMOVE, selected); |
216 | list_button_enable(context->list, LIST_BUTTON_EDIT, selected); | ||
217 | harbaum | 73 | } |
218 | |||
219 | achadwick | 99 | /* try to find something descriptive */ |
220 | static char *relation_get_descriptive_name(relation_t *relation) { | ||
221 | char *name = osm_tag_get_by_key(relation->tag, "ref"); | ||
222 | if (!name) | ||
223 | name = osm_tag_get_by_key(relation->tag, "name"); | ||
224 | if (!name) | ||
225 | name = osm_tag_get_by_key(relation->tag, "note"); | ||
226 | if (!name) | ||
227 | name = osm_tag_get_by_key(relation->tag, "fix" "me"); | ||
228 | return name; | ||
229 | } | ||
230 | |||
231 | harbaum | 76 | static void on_relation_item_add(GtkWidget *but, relitem_context_t *context) { |
232 | harbaum | 73 | /* create a new relation */ |
233 | |||
234 | relation_t *relation = osm_relation_new(); | ||
235 | if(!info_dialog(context->dialog, context->appdata, relation)) { | ||
236 | printf("tag edit cancelled\n"); | ||
237 | osm_relation_free(relation); | ||
238 | } else { | ||
239 | osm_relation_attach(context->appdata->osm, relation); | ||
240 | |||
241 | /* add to list */ | ||
242 | |||
243 | /* append a row for the new data */ | ||
244 | achadwick | 99 | char *name = relation_get_descriptive_name(relation); |
245 | harbaum | 73 | |
246 | GtkTreeIter iter; | ||
247 | gtk_list_store_append(context->store, &iter); | ||
248 | gtk_list_store_set(context->store, &iter, | ||
249 | RELITEM_COL_SELECTED, FALSE, | ||
250 | RELITEM_COL_TYPE, | ||
251 | osm_tag_get_by_key(relation->tag, "type"), | ||
252 | RELITEM_COL_NAME, name, | ||
253 | RELITEM_COL_DATA, relation, | ||
254 | -1); | ||
255 | |||
256 | harbaum | 148 | gtk_tree_selection_select_iter(list_get_selection(context->list), &iter); |
257 | harbaum | 73 | } |
258 | harbaum | 1 | } |
259 | |||
260 | static relation_t *get_selection(relitem_context_t *context) { | ||
261 | GtkTreeSelection *selection; | ||
262 | GtkTreeModel *model; | ||
263 | GtkTreeIter iter; | ||
264 | |||
265 | harbaum | 148 | selection = list_get_selection(context->list); |
266 | harbaum | 1 | if(gtk_tree_selection_get_selected(selection, &model, &iter)) { |
267 | relation_t *relation; | ||
268 | gtk_tree_model_get(model, &iter, RELITEM_COL_DATA, &relation, -1); | ||
269 | return(relation); | ||
270 | } | ||
271 | return NULL; | ||
272 | } | ||
273 | |||
274 | harbaum | 76 | static void on_relation_item_edit(GtkWidget *but, relitem_context_t *context) { |
275 | harbaum | 1 | relation_t *sel = get_selection(context); |
276 | if(!sel) return; | ||
277 | |||
278 | achadwick | 99 | printf("edit relation item #%ld\n", sel->id); |
279 | harbaum | 1 | |
280 | achadwick | 99 | if (!info_dialog(context->dialog, context->appdata, sel)) |
281 | return; | ||
282 | |||
283 | // Locate the changed item | ||
284 | GtkTreeIter iter; | ||
285 | gboolean valid = gtk_tree_model_get_iter_first( | ||
286 | GTK_TREE_MODEL(context->store), &iter); | ||
287 | while (valid) { | ||
288 | relation_t *row_rel; | ||
289 | gtk_tree_model_get(GTK_TREE_MODEL(context->store), &iter, | ||
290 | RELITEM_COL_DATA, &row_rel, | ||
291 | -1); | ||
292 | if (row_rel == sel) | ||
293 | break; | ||
294 | valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(context->store), &iter); | ||
295 | } | ||
296 | if (!valid) | ||
297 | return; | ||
298 | |||
299 | // Found it. Update all visible fields that belong to the relation iself. | ||
300 | gtk_list_store_set(context->store, &iter, | ||
301 | RELITEM_COL_TYPE, osm_tag_get_by_key(sel->tag, "type"), | ||
302 | RELITEM_COL_NAME, relation_get_descriptive_name(sel), | ||
303 | -1); | ||
304 | |||
305 | // Order will probably have changed, so refocus | ||
306 | harbaum | 148 | list_focus_on(context->list, &iter, TRUE); |
307 | harbaum | 1 | } |
308 | |||
309 | harbaum | 75 | /* remove the selected relation */ |
310 | harbaum | 76 | static void on_relation_item_remove(GtkWidget *but, relitem_context_t *context) { |
311 | harbaum | 1 | relation_t *sel = get_selection(context); |
312 | if(!sel) return; | ||
313 | |||
314 | printf("remove relation #%ld\n", sel->id); | ||
315 | harbaum | 75 | |
316 | harbaum | 77 | gint members = osm_relation_members_num(sel); |
317 | harbaum | 75 | |
318 | if(members) | ||
319 | if(!yes_no_f(context->dialog, NULL, 0, 0, | ||
320 | _("Delete non-empty relation?"), | ||
321 | _("This relation still has %d members. " | ||
322 | "Delete it anyway?"), members)) | ||
323 | return; | ||
324 | |||
325 | /* first remove selected row from list */ | ||
326 | GtkTreeIter iter; | ||
327 | harbaum | 148 | GtkTreeSelection *selection = list_get_selection(context->list); |
328 | harbaum | 75 | if(gtk_tree_selection_get_selected(selection, NULL, &iter)) |
329 | gtk_list_store_remove(context->store, &iter); | ||
330 | |||
331 | /* then really delete it */ | ||
332 | osm_relation_delete(context->appdata->osm, sel, FALSE); | ||
333 | |||
334 | harbaum | 76 | relation_item_list_selected(context, FALSE); |
335 | harbaum | 1 | } |
336 | |||
337 | harbaum | 73 | static char *relitem_get_role_in_relation(relation_item_t *item, relation_t *relation) { |
338 | member_t *member = relation->member; | ||
339 | while(member) { | ||
340 | switch(member->type) { | ||
341 | |||
342 | case NODE: | ||
343 | if((item->type == NODE) && (item->node == member->node)) | ||
344 | return member->role; | ||
345 | break; | ||
346 | |||
347 | case WAY: | ||
348 | if((item->type == WAY) && (item->way == member->way)) | ||
349 | return member->role; | ||
350 | break; | ||
351 | |||
352 | default: | ||
353 | break; | ||
354 | } | ||
355 | member = member->next; | ||
356 | } | ||
357 | return NULL; | ||
358 | } | ||
359 | |||
360 | harbaum | 1 | static void |
361 | relitem_toggled(GtkCellRendererToggle *cell, const gchar *path_str, | ||
362 | harbaum | 75 | relitem_context_t *context) { |
363 | harbaum | 1 | GtkTreePath *path; |
364 | GtkTreeIter iter; | ||
365 | |||
366 | path = gtk_tree_path_new_from_string(path_str); | ||
367 | gtk_tree_model_get_iter(GTK_TREE_MODEL(context->store), &iter, path); | ||
368 | harbaum | 73 | gtk_tree_path_free(path); |
369 | harbaum | 1 | |
370 | /* get current enabled flag */ | ||
371 | gboolean enabled; | ||
372 | harbaum | 73 | relation_t *relation = NULL; |
373 | harbaum | 1 | gtk_tree_model_get(GTK_TREE_MODEL(context->store), &iter, |
374 | harbaum | 73 | RELITEM_COL_SELECTED, &enabled, |
375 | RELITEM_COL_DATA, &relation, | ||
376 | -1); | ||
377 | harbaum | 1 | |
378 | achadwick | 99 | list_pre_inplace_edit_tweak(GTK_TREE_MODEL(context->store)); |
379 | |||
380 | harbaum | 73 | if(!enabled) { |
381 | printf("will now become be part of this relation\n"); | ||
382 | if(relation_add_item(context->dialog, relation, context->item)) | ||
383 | gtk_list_store_set(context->store, &iter, | ||
384 | RELITEM_COL_SELECTED, TRUE, | ||
385 | RELITEM_COL_ROLE, | ||
386 | relitem_get_role_in_relation(context->item, relation), | ||
387 | -1); | ||
388 | } else { | ||
389 | printf("item will not be part of this relation anymore\n"); | ||
390 | relation_remove_item(relation, context->item); | ||
391 | gtk_list_store_set(context->store, &iter, | ||
392 | RELITEM_COL_SELECTED, FALSE, | ||
393 | RELITEM_COL_ROLE, NULL, | ||
394 | -1); | ||
395 | } | ||
396 | achadwick | 99 | |
397 | harbaum | 1 | } |
398 | |||
399 | static gboolean relitem_is_in_relation(relation_item_t *item, relation_t *relation) { | ||
400 | member_t *member = relation->member; | ||
401 | while(member) { | ||
402 | switch(member->type) { | ||
403 | |||
404 | case NODE: | ||
405 | if((item->type == NODE) && (item->node == member->node)) | ||
406 | return TRUE; | ||
407 | break; | ||
408 | |||
409 | case WAY: | ||
410 | if((item->type == WAY) && (item->way == member->way)) | ||
411 | return TRUE; | ||
412 | break; | ||
413 | |||
414 | default: | ||
415 | break; | ||
416 | } | ||
417 | member = member->next; | ||
418 | } | ||
419 | return FALSE; | ||
420 | } | ||
421 | |||
422 | harbaum | 76 | static GtkWidget *relation_item_list_widget(relitem_context_t *context) { |
423 | harbaum | 148 | context->list = list_new(LIST_HILDON_WITH_HEADERS); |
424 | harbaum | 1 | |
425 | harbaum | 148 | list_set_columns(context->list, |
426 | _(""), RELITEM_COL_SELECTED, LIST_FLAG_TOGGLE, | ||
427 | G_CALLBACK(relitem_toggled), context, | ||
428 | _("Type"), RELITEM_COL_TYPE, 0, | ||
429 | _("Role"), RELITEM_COL_ROLE, 0, | ||
430 | _("Name"), RELITEM_COL_NAME, LIST_FLAG_ELLIPSIZE, | ||
431 | NULL); | ||
432 | harbaum | 1 | |
433 | /* build and fill the store */ | ||
434 | context->store = gtk_list_store_new(RELITEM_NUM_COLS, | ||
435 | G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, | ||
436 | G_TYPE_STRING, G_TYPE_POINTER); | ||
437 | |||
438 | harbaum | 148 | list_set_store(context->list, context->store); |
439 | harbaum | 1 | |
440 | achadwick | 99 | // Debatable whether to sort by the "selected" or the "Name" column by |
441 | // default. Both are be useful, in different ways. | ||
442 | gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(context->store), | ||
443 | RELITEM_COL_NAME, GTK_SORT_ASCENDING); | ||
444 | |||
445 | harbaum | 1 | GtkTreeIter iter; |
446 | relation_t *relation = context->appdata->osm->relation; | ||
447 | while(relation) { | ||
448 | /* try to find something descriptive */ | ||
449 | achadwick | 99 | char *name = relation_get_descriptive_name(relation); |
450 | harbaum | 1 | |
451 | /* Append a row and fill in some data */ | ||
452 | gtk_list_store_append(context->store, &iter); | ||
453 | gtk_list_store_set(context->store, &iter, | ||
454 | RELITEM_COL_SELECTED, relitem_is_in_relation(context->item, relation), | ||
455 | RELITEM_COL_TYPE, osm_tag_get_by_key(relation->tag, "type"), | ||
456 | RELITEM_COL_ROLE, relitem_get_role_in_relation(context->item, relation), | ||
457 | RELITEM_COL_NAME, name, | ||
458 | RELITEM_COL_DATA, relation, | ||
459 | -1); | ||
460 | |||
461 | relation = relation->next; | ||
462 | } | ||
463 | |||
464 | g_object_unref(context->store); | ||
465 | |||
466 | harbaum | 148 | list_set_static_buttons(context->list, G_CALLBACK(on_relation_item_add), |
467 | G_CALLBACK(on_relation_item_edit),G_CALLBACK(on_relation_item_remove), context); | ||
468 | harbaum | 1 | |
469 | harbaum | 76 | relation_item_list_selected(context, FALSE); |
470 | harbaum | 1 | |
471 | harbaum | 148 | return context->list; |
472 | harbaum | 1 | } |
473 | |||
474 | void relation_add_dialog(appdata_t *appdata, relation_item_t *relitem) { | ||
475 | relitem_context_t *context = g_new0(relitem_context_t, 1); | ||
476 | map_t *map = appdata->map; | ||
477 | g_assert(map); | ||
478 | |||
479 | context->appdata = appdata; | ||
480 | context->item = relitem; | ||
481 | |||
482 | char *str = NULL; | ||
483 | switch(relitem->type) { | ||
484 | case NODE: | ||
485 | str = g_strdup_printf(_("Relations for node #%ld"), relitem->node->id); | ||
486 | break; | ||
487 | case WAY: | ||
488 | str = g_strdup_printf(_("Relations for way #%ld"), relitem->way->id); | ||
489 | break; | ||
490 | default: | ||
491 | g_assert((relitem->type == NODE) || (relitem->type == WAY)); | ||
492 | break; | ||
493 | } | ||
494 | |||
495 | context->dialog = gtk_dialog_new_with_buttons(str, | ||
496 | GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL, | ||
497 | harbaum | 73 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
498 | harbaum | 1 | NULL); |
499 | g_free(str); | ||
500 | |||
501 | gtk_dialog_set_default_response(GTK_DIALOG(context->dialog), | ||
502 | harbaum | 76 | GTK_RESPONSE_CLOSE); |
503 | harbaum | 1 | |
504 | achadwick | 99 | gtk_window_set_default_size(GTK_WINDOW(context->dialog), |
505 | LIST_OF_RELATIONS_DIALOG_WIDTH, | ||
506 | LIST_OF_RELATIONS_DIALOG_HEIGHT); | ||
507 | harbaum | 1 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), |
508 | harbaum | 76 | relation_item_list_widget(context), TRUE, TRUE, 0); |
509 | harbaum | 1 | |
510 | /* ----------------------------------- */ | ||
511 | |||
512 | gtk_widget_show_all(context->dialog); | ||
513 | harbaum | 73 | gtk_dialog_run(GTK_DIALOG(context->dialog)); |
514 | harbaum | 1 | gtk_widget_destroy(context->dialog); |
515 | |||
516 | g_free(context); | ||
517 | } | ||
518 | harbaum | 76 | |
519 | /* -------------------- global relation list ----------------- */ | ||
520 | |||
521 | typedef struct { | ||
522 | appdata_t *appdata; | ||
523 | harbaum | 148 | GtkWidget *dialog, *list; |
524 | harbaum | 76 | GtkListStore *store; |
525 | } relation_context_t; | ||
526 | |||
527 | enum { | ||
528 | RELATION_COL_ID = 0, | ||
529 | RELATION_COL_TYPE, | ||
530 | RELATION_COL_NAME, | ||
531 | RELATION_COL_MEMBERS, | ||
532 | RELATION_COL_DATA, | ||
533 | RELATION_NUM_COLS | ||
534 | }; | ||
535 | |||
536 | harbaum | 77 | static relation_t *get_selected_relation(relation_context_t *context) { |
537 | GtkTreeSelection *selection; | ||
538 | GtkTreeModel *model; | ||
539 | GtkTreeIter iter; | ||
540 | |||
541 | harbaum | 148 | selection = list_get_selection(context->list); |
542 | harbaum | 77 | if(gtk_tree_selection_get_selected(selection, &model, &iter)) { |
543 | relation_t *relation; | ||
544 | gtk_tree_model_get(model, &iter, RELATION_COL_DATA, &relation, -1); | ||
545 | return(relation); | ||
546 | } | ||
547 | return NULL; | ||
548 | } | ||
549 | |||
550 | harbaum | 76 | static void relation_list_selected(relation_context_t *context, |
551 | harbaum | 77 | relation_t *selected) { |
552 | harbaum | 76 | |
553 | harbaum | 148 | list_button_enable(context->list, LIST_BUTTON_USER0, |
554 | (selected != NULL) && (selected->member != NULL)); | ||
555 | harbaum | 77 | |
556 | harbaum | 148 | list_button_enable(context->list, LIST_BUTTON_REMOVE, selected != NULL); |
557 | list_button_enable(context->list, LIST_BUTTON_EDIT, selected != NULL); | ||
558 | harbaum | 76 | } |
559 | |||
560 | static gboolean | ||
561 | relation_list_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, | ||
562 | GtkTreePath *path, gboolean path_currently_selected, | ||
563 | gpointer userdata) { | ||
564 | relation_context_t *context = (relation_context_t*)userdata; | ||
565 | GtkTreeIter iter; | ||
566 | |||
567 | if(gtk_tree_model_get_iter(model, &iter, path)) { | ||
568 | g_assert(gtk_tree_path_get_depth(path) == 1); | ||
569 | harbaum | 77 | |
570 | relation_t *relation = NULL; | ||
571 | gtk_tree_model_get(model, &iter, RELATION_COL_DATA, &relation, -1); | ||
572 | relation_list_selected(context, relation); | ||
573 | harbaum | 76 | } |
574 | |||
575 | return TRUE; /* allow selection state to change */ | ||
576 | } | ||
577 | |||
578 | typedef struct { | ||
579 | relation_t *relation; | ||
580 | GtkWidget *dialog, *view; | ||
581 | GtkListStore *store; | ||
582 | } member_context_t; | ||
583 | |||
584 | enum { | ||
585 | MEMBER_COL_TYPE = 0, | ||
586 | MEMBER_COL_ID, | ||
587 | MEMBER_COL_NAME, | ||
588 | MEMBER_COL_ROLE, | ||
589 | MEMBER_COL_REF_ONLY, | ||
590 | MEMBER_COL_DATA, | ||
591 | MEMBER_NUM_COLS | ||
592 | }; | ||
593 | |||
594 | static gboolean | ||
595 | member_list_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, | ||
596 | GtkTreePath *path, gboolean path_currently_selected, | ||
597 | gpointer userdata) { | ||
598 | GtkTreeIter iter; | ||
599 | |||
600 | if(gtk_tree_model_get_iter(model, &iter, path)) { | ||
601 | g_assert(gtk_tree_path_get_depth(path) == 1); | ||
602 | |||
603 | member_t *member = NULL; | ||
604 | gtk_tree_model_get(model, &iter, MEMBER_COL_DATA, &member, -1); | ||
605 | if(member && member->type < NODE_ID) | ||
606 | return TRUE; | ||
607 | } | ||
608 | |||
609 | return FALSE; | ||
610 | } | ||
611 | |||
612 | |||
613 | static GtkWidget *member_list_widget(member_context_t *context) { | ||
614 | GtkWidget *vbox = gtk_vbox_new(FALSE,3); | ||
615 | context->view = gtk_tree_view_new(); | ||
616 | |||
617 | gtk_tree_selection_set_select_function( | ||
618 | gtk_tree_view_get_selection(GTK_TREE_VIEW(context->view)), | ||
619 | member_list_selection_func, | ||
620 | context, NULL); | ||
621 | |||
622 | /* --- "type" column --- */ | ||
623 | GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); | ||
624 | g_object_set(renderer, "foreground", "grey", NULL); | ||
625 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(context->view), | ||
626 | -1, _("Type"), renderer, "text", MEMBER_COL_TYPE, | ||
627 | "foreground-set", MEMBER_COL_REF_ONLY, NULL); | ||
628 | |||
629 | |||
630 | /* --- "id" column --- */ | ||
631 | renderer = gtk_cell_renderer_text_new(); | ||
632 | g_object_set(renderer, "foreground", "grey", NULL); | ||
633 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(context->view), | ||
634 | -1, _("Id"), renderer, "text", MEMBER_COL_ID, | ||
635 | "foreground-set", MEMBER_COL_REF_ONLY, NULL); | ||
636 | |||
637 | /* --- "Name" column --- */ | ||
638 | renderer = gtk_cell_renderer_text_new(); | ||
639 | g_object_set(renderer, "foreground", "grey", NULL); | ||
640 | g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL); | ||
641 | GtkTreeViewColumn *column = | ||
642 | gtk_tree_view_column_new_with_attributes(_("Name"), renderer, | ||
643 | "text", MEMBER_COL_NAME, | ||
644 | "foreground-set", MEMBER_COL_REF_ONLY, NULL); | ||
645 | gtk_tree_view_column_set_expand(column, TRUE); | ||
646 | gtk_tree_view_insert_column(GTK_TREE_VIEW(context->view), column, -1); | ||
647 | |||
648 | /* --- "role" column --- */ | ||
649 | renderer = gtk_cell_renderer_text_new(); | ||
650 | g_object_set(renderer, "foreground", "grey", NULL); | ||
651 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(context->view), | ||
652 | -1, _("Role"), renderer, "text", MEMBER_COL_ROLE, | ||
653 | "foreground-set", MEMBER_COL_REF_ONLY, NULL); | ||
654 | |||
655 | /* build and fill the store */ | ||
656 | context->store = gtk_list_store_new(MEMBER_NUM_COLS, | ||
657 | G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, | ||
658 | G_TYPE_BOOLEAN, G_TYPE_POINTER); | ||
659 | |||
660 | gtk_tree_view_set_model(GTK_TREE_VIEW(context->view), | ||
661 | GTK_TREE_MODEL(context->store)); | ||
662 | |||
663 | GtkTreeIter iter; | ||
664 | member_t *member = context->relation->member; | ||
665 | while(member) { | ||
666 | tag_t *tags = osm_object_get_tags(member->type, member->ptr); | ||
667 | char *id = osm_id_string(member->type, member->ptr); | ||
668 | |||
669 | /* try to find something descriptive */ | ||
670 | char *name = NULL; | ||
671 | if(tags) | ||
672 | name = osm_tag_get_by_key(tags, "name"); | ||
673 | |||
674 | /* Append a row and fill in some data */ | ||
675 | gtk_list_store_append(context->store, &iter); | ||
676 | gtk_list_store_set(context->store, &iter, | ||
677 | MEMBER_COL_TYPE, osm_type_string(member->type), | ||
678 | MEMBER_COL_ID, id, | ||
679 | MEMBER_COL_NAME, name, | ||
680 | MEMBER_COL_ROLE, member->role, | ||
681 | MEMBER_COL_REF_ONLY, member->type >= NODE_ID, | ||
682 | MEMBER_COL_DATA, member, | ||
683 | -1); | ||
684 | |||
685 | g_free(id); | ||
686 | member = member->next; | ||
687 | } | ||
688 | |||
689 | g_object_unref(context->store); | ||
690 | |||
691 | /* put it into a scrolled window */ | ||
692 | GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); | ||
693 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), | ||
694 | GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); | ||
695 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), | ||
696 | GTK_SHADOW_ETCHED_IN); | ||
697 | gtk_container_add(GTK_CONTAINER(scrolled_window), context->view); | ||
698 | |||
699 | gtk_box_pack_start_defaults(GTK_BOX(vbox), scrolled_window); | ||
700 | |||
701 | return vbox; | ||
702 | } | ||
703 | |||
704 | /* user clicked "members..." button in relation list */ | ||
705 | static void on_relation_members(GtkWidget *but, relation_context_t *context) { | ||
706 | member_context_t *mcontext = g_new0(member_context_t, 1); | ||
707 | |||
708 | /* display members list */ | ||
709 | mcontext->relation = get_selected_relation(context); | ||
710 | if(!mcontext->relation) return; | ||
711 | |||
712 | char *str = osm_tag_get_by_key(mcontext->relation->tag, "name"); | ||
713 | if(!str) str = osm_tag_get_by_key(mcontext->relation->tag, "ref"); | ||
714 | if(!str) | ||
715 | str = g_strdup_printf(_("Members of relation #%ld"), | ||
716 | mcontext->relation->id); | ||
717 | else | ||
718 | str = g_strdup_printf(_("Members of relation \"%s\""), str); | ||
719 | |||
720 | mcontext->dialog = | ||
721 | gtk_dialog_new_with_buttons(str, | ||
722 | GTK_WINDOW(context->dialog), GTK_DIALOG_MODAL, | ||
723 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, | ||
724 | NULL); | ||
725 | g_free(str); | ||
726 | |||
727 | gtk_dialog_set_default_response(GTK_DIALOG(mcontext->dialog), | ||
728 | GTK_RESPONSE_CLOSE); | ||
729 | |||
730 | achadwick | 99 | gtk_window_set_default_size(GTK_WINDOW(mcontext->dialog), |
731 | LIST_OF_MEMBERS_DIALOG_WIDTH, | ||
732 | LIST_OF_MEMBERS_DIALOG_HEIGHT); | ||
733 | harbaum | 76 | |
734 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(mcontext->dialog)->vbox), | ||
735 | member_list_widget(mcontext), TRUE, TRUE, 0); | ||
736 | |||
737 | /* ----------------------------------- */ | ||
738 | |||
739 | gtk_widget_show_all(mcontext->dialog); | ||
740 | gtk_dialog_run(GTK_DIALOG(mcontext->dialog)); | ||
741 | gtk_widget_destroy(mcontext->dialog); | ||
742 | |||
743 | g_free(mcontext); | ||
744 | } | ||
745 | |||
746 | harbaum | 77 | static void on_relation_add(GtkWidget *but, relation_context_t *context) { |
747 | /* create a new relation */ | ||
748 | |||
749 | relation_t *relation = osm_relation_new(); | ||
750 | if(!info_dialog(context->dialog, context->appdata, relation)) { | ||
751 | printf("tag edit cancelled\n"); | ||
752 | osm_relation_free(relation); | ||
753 | } else { | ||
754 | osm_relation_attach(context->appdata->osm, relation); | ||
755 | |||
756 | /* append a row for the new data */ | ||
757 | |||
758 | achadwick | 99 | char *name = relation_get_descriptive_name(relation); |
759 | harbaum | 77 | |
760 | achadwick | 99 | guint num = osm_relation_members_num(relation); |
761 | harbaum | 77 | |
762 | /* Append a row and fill in some data */ | ||
763 | GtkTreeIter iter; | ||
764 | gtk_list_store_append(context->store, &iter); | ||
765 | gtk_list_store_set(context->store, &iter, | ||
766 | achadwick | 99 | RELATION_COL_ID, relation->id, |
767 | harbaum | 77 | RELATION_COL_TYPE, |
768 | osm_tag_get_by_key(relation->tag, "type"), | ||
769 | RELATION_COL_NAME, name, | ||
770 | RELATION_COL_MEMBERS, num, | ||
771 | RELATION_COL_DATA, relation, | ||
772 | -1); | ||
773 | |||
774 | harbaum | 148 | gtk_tree_selection_select_iter(list_get_selection(context->list), &iter); |
775 | harbaum | 77 | |
776 | /* scroll to end */ | ||
777 | // GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(); | ||
778 | /* xyz */ | ||
779 | } | ||
780 | } | ||
781 | |||
782 | /* user clicked "edit..." button in relation list */ | ||
783 | static void on_relation_edit(GtkWidget *but, relation_context_t *context) { | ||
784 | relation_t *sel = get_selected_relation(context); | ||
785 | if(!sel) return; | ||
786 | |||
787 | printf("edit relation #%ld\n", sel->id); | ||
788 | |||
789 | achadwick | 99 | if (!info_dialog(context->dialog, context->appdata, sel)) |
790 | return; | ||
791 | |||
792 | // Locate the changed item | ||
793 | GtkTreeIter iter; | ||
794 | gboolean valid = gtk_tree_model_get_iter_first( | ||
795 | GTK_TREE_MODEL(context->store), &iter); | ||
796 | while (valid) { | ||
797 | relation_t *row_rel; | ||
798 | gtk_tree_model_get(GTK_TREE_MODEL(context->store), &iter, | ||
799 | RELATION_COL_DATA, &row_rel, | ||
800 | -1); | ||
801 | if (row_rel == sel) | ||
802 | break; | ||
803 | valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(context->store), &iter); | ||
804 | } | ||
805 | if (!valid) | ||
806 | return; | ||
807 | |||
808 | // Found it. Update all visible fields. | ||
809 | gtk_list_store_set(context->store, &iter, | ||
810 | RELATION_COL_ID, sel->id, | ||
811 | RELATION_COL_TYPE, osm_tag_get_by_key(sel->tag, "type"), | ||
812 | RELATION_COL_NAME, relation_get_descriptive_name(sel), | ||
813 | RELATION_COL_MEMBERS, osm_relation_members_num(sel), | ||
814 | -1); | ||
815 | |||
816 | // Order will probably have changed, so refocus | ||
817 | harbaum | 148 | list_focus_on(context->list, &iter, TRUE); |
818 | harbaum | 77 | } |
819 | |||
820 | achadwick | 99 | |
821 | harbaum | 77 | /* remove the selected relation */ |
822 | static void on_relation_remove(GtkWidget *but, relation_context_t *context) { | ||
823 | relation_t *sel = get_selected_relation(context); | ||
824 | if(!sel) return; | ||
825 | |||
826 | printf("remove relation #%ld\n", sel->id); | ||
827 | |||
828 | gint members = osm_relation_members_num(sel); | ||
829 | |||
830 | if(members) | ||
831 | if(!yes_no_f(context->dialog, NULL, 0, 0, | ||
832 | _("Delete non-empty relation?"), | ||
833 | _("This relation still has %d members. " | ||
834 | "Delete it anyway?"), members)) | ||
835 | return; | ||
836 | |||
837 | /* first remove selected row from list */ | ||
838 | GtkTreeIter iter; | ||
839 | harbaum | 148 | GtkTreeSelection *selection = list_get_selection(context->list); |
840 | harbaum | 77 | if(gtk_tree_selection_get_selected(selection, NULL, &iter)) |
841 | gtk_list_store_remove(context->store, &iter); | ||
842 | |||
843 | /* then really delete it */ | ||
844 | osm_relation_delete(context->appdata->osm, sel, FALSE); | ||
845 | |||
846 | relation_list_selected(context, NULL); | ||
847 | } | ||
848 | |||
849 | harbaum | 76 | static GtkWidget *relation_list_widget(relation_context_t *context) { |
850 | harbaum | 148 | context->list = list_new(LIST_HILDON_WITH_HEADERS); |
851 | harbaum | 76 | |
852 | harbaum | 148 | list_set_selection_function(context->list, relation_list_selection_func, context); |
853 | harbaum | 76 | |
854 | harbaum | 148 | list_set_columns(context->list, |
855 | _("Id"), RELATION_COL_ID, 0, | ||
856 | _("Type"), RELATION_COL_TYPE, 0, | ||
857 | _("Name"), RELATION_COL_NAME, LIST_FLAG_ELLIPSIZE, | ||
858 | _("Members"), RELATION_COL_MEMBERS, 0, | ||
859 | NULL); | ||
860 | harbaum | 76 | |
861 | /* build and fill the store */ | ||
862 | context->store = gtk_list_store_new(RELATION_NUM_COLS, | ||
863 | achadwick | 99 | G_TYPE_ITEM_ID_T, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, |
864 | harbaum | 76 | G_TYPE_POINTER); |
865 | |||
866 | harbaum | 148 | list_set_store(context->list, context->store); |
867 | harbaum | 76 | |
868 | achadwick | 99 | // Sorting by ref/name by default is useful for places with lots of numbered |
869 | // bus routes. Especially for small screens. | ||
870 | gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(context->store), | ||
871 | RELATION_COL_NAME, GTK_SORT_ASCENDING); | ||
872 | |||
873 | harbaum | 76 | GtkTreeIter iter; |
874 | relation_t *relation = context->appdata->osm->relation; | ||
875 | while(relation) { | ||
876 | achadwick | 99 | char *name = relation_get_descriptive_name(relation); |
877 | harbaum | 76 | |
878 | achadwick | 99 | guint num = osm_relation_members_num(relation); |
879 | harbaum | 76 | |
880 | /* Append a row and fill in some data */ | ||
881 | gtk_list_store_append(context->store, &iter); | ||
882 | gtk_list_store_set(context->store, &iter, | ||
883 | achadwick | 99 | RELATION_COL_ID, relation->id, |
884 | harbaum | 76 | RELATION_COL_TYPE, |
885 | osm_tag_get_by_key(relation->tag, "type"), | ||
886 | RELATION_COL_NAME, name, | ||
887 | RELATION_COL_MEMBERS, num, | ||
888 | RELATION_COL_DATA, relation, | ||
889 | -1); | ||
890 | achadwick | 99 | |
891 | harbaum | 76 | relation = relation->next; |
892 | } | ||
893 | |||
894 | g_object_unref(context->store); | ||
895 | |||
896 | harbaum | 148 | list_set_static_buttons(context->list, G_CALLBACK(on_relation_add), |
897 | G_CALLBACK(on_relation_edit), G_CALLBACK(on_relation_remove), context); | ||
898 | harbaum | 76 | |
899 | harbaum | 148 | list_set_user_buttons(context->list, |
900 | LIST_BUTTON_USER0, _("Members..."), G_CALLBACK(on_relation_members), | ||
901 | 0); | ||
902 | harbaum | 76 | |
903 | harbaum | 77 | relation_list_selected(context, NULL); |
904 | harbaum | 76 | |
905 | harbaum | 148 | return context->list; |
906 | harbaum | 76 | } |
907 | |||
908 | /* a global view on all relations */ | ||
909 | void relation_list(appdata_t *appdata) { | ||
910 | relation_context_t *context = g_new0(relation_context_t, 1); | ||
911 | context->appdata = appdata; | ||
912 | |||
913 | printf("relation list\n"); | ||
914 | |||
915 | context->dialog = | ||
916 | gtk_dialog_new_with_buttons(_("All relations"), | ||
917 | GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL, | ||
918 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, | ||
919 | NULL); | ||
920 | |||
921 | gtk_dialog_set_default_response(GTK_DIALOG(context->dialog), | ||
922 | GTK_RESPONSE_CLOSE); | ||
923 | |||
924 | achadwick | 99 | gtk_window_set_default_size(GTK_WINDOW(context->dialog), |
925 | LIST_OF_RELATIONS_DIALOG_WIDTH, | ||
926 | LIST_OF_RELATIONS_DIALOG_HEIGHT); | ||
927 | |||
928 | harbaum | 76 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(context->dialog)->vbox), |
929 | relation_list_widget(context), TRUE, TRUE, 0); | ||
930 | |||
931 | /* ----------------------------------- */ | ||
932 | |||
933 | gtk_widget_show_all(context->dialog); | ||
934 | gtk_dialog_run(GTK_DIALOG(context->dialog)); | ||
935 | gtk_widget_destroy(context->dialog); | ||
936 | |||
937 | g_free(context); | ||
938 | } | ||
939 | achadwick | 99 | |
940 | |||
941 | // vim:et:ts=8:sw=2:sts=2:ai |