1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : NT_edconf.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include "NT_cb.h" |
---|
12 | #include "NT_local.h" |
---|
13 | |
---|
14 | #include <awt_sel_boxes.hxx> |
---|
15 | #include <aw_awars.hxx> |
---|
16 | #include <aw_root.hxx> |
---|
17 | #include <aw_msg.hxx> |
---|
18 | #include <ad_config.h> |
---|
19 | #include <arbdbt.h> |
---|
20 | #include <arb_strbuf.h> |
---|
21 | #include <arb_global_defs.h> |
---|
22 | |
---|
23 | static void init_config_awars(AW_root *root) { |
---|
24 | root->awar_string(AWAR_CONFIGURATION, "default_configuration", GLOBAL.gb_main); |
---|
25 | } |
---|
26 | |
---|
27 | // ----------------------------- |
---|
28 | // class Store_species |
---|
29 | |
---|
30 | class Store_species : virtual Noncopyable { |
---|
31 | // stores an amount of species: |
---|
32 | GBT_TREE *node; |
---|
33 | Store_species *next; |
---|
34 | public: |
---|
35 | Store_species(GBT_TREE *aNode) { |
---|
36 | node = aNode; |
---|
37 | next = 0; |
---|
38 | } |
---|
39 | ~Store_species(); |
---|
40 | |
---|
41 | Store_species* add(Store_species *list) { |
---|
42 | nt_assert(next==0); |
---|
43 | next = list; |
---|
44 | return this; |
---|
45 | } |
---|
46 | |
---|
47 | Store_species* remove() { |
---|
48 | Store_species *follower = next; |
---|
49 | next = 0; |
---|
50 | return follower; |
---|
51 | } |
---|
52 | |
---|
53 | GBT_TREE *getNode() const { return node; } |
---|
54 | |
---|
55 | void call(void (*aPizza)(GBT_TREE*)) const; |
---|
56 | }; |
---|
57 | |
---|
58 | Store_species::~Store_species() { |
---|
59 | delete next; |
---|
60 | } |
---|
61 | |
---|
62 | void Store_species::call(void (*aPizza)(GBT_TREE*)) const { |
---|
63 | aPizza(node); |
---|
64 | if (next) next->call(aPizza); |
---|
65 | } |
---|
66 | |
---|
67 | static void unmark_species(GBT_TREE *node) { |
---|
68 | nt_assert(node); |
---|
69 | nt_assert(node->gb_node); |
---|
70 | nt_assert(GB_read_flag(node->gb_node)!=0); |
---|
71 | GB_write_flag(node->gb_node, 0); |
---|
72 | } |
---|
73 | |
---|
74 | static void mark_species(GBT_TREE *node, Store_species **extra_marked_species) { |
---|
75 | nt_assert(node); |
---|
76 | nt_assert(node->gb_node); |
---|
77 | nt_assert(GB_read_flag(node->gb_node)==0); |
---|
78 | GB_write_flag(node->gb_node, 1); |
---|
79 | |
---|
80 | *extra_marked_species = (new Store_species(node))->add(*extra_marked_species); |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | static GBT_TREE *rightmost_leaf(GBT_TREE *node) { |
---|
86 | nt_assert(node); |
---|
87 | while (!node->is_leaf) { |
---|
88 | node = node->rightson; |
---|
89 | nt_assert(node); |
---|
90 | } |
---|
91 | return node; |
---|
92 | } |
---|
93 | |
---|
94 | static GBT_TREE *left_neighbour_leaf(GBT_TREE *node) { |
---|
95 | if (node) { |
---|
96 | GBT_TREE *father = node->father; |
---|
97 | while (father) { |
---|
98 | if (father->rightson==node) { |
---|
99 | node = rightmost_leaf(father->leftson); |
---|
100 | nt_assert(node->is_leaf); |
---|
101 | if (!node->gb_node) { // Zombie |
---|
102 | node = left_neighbour_leaf(node); |
---|
103 | } |
---|
104 | return node; |
---|
105 | } |
---|
106 | node = father; |
---|
107 | father = node->father; |
---|
108 | } |
---|
109 | } |
---|
110 | return 0; |
---|
111 | } |
---|
112 | |
---|
113 | static int nt_build_conf_string_rek(GB_HASH *used, GBT_TREE *tree, GBS_strstruct *memfile, |
---|
114 | Store_species **extra_marked_species, int use_species_aside, |
---|
115 | int *auto_mark, int marked_at_left, int *marked_at_right) |
---|
116 | { |
---|
117 | /*! Builds a configuration string from a tree. |
---|
118 | * |
---|
119 | * @param used all species inserted by this function are stored here |
---|
120 | * @param tree used for group information |
---|
121 | * @param memfile generated configuration string is stored here |
---|
122 | * @param extra_marked_species all extra marked species are inserted here |
---|
123 | * @param use_species_aside number of species to mark left and right of marked species |
---|
124 | * @param auto_mark number species to extra-mark (if not already marked) |
---|
125 | * @param marked_at_left number of species which were marked (looking to left) |
---|
126 | * @param marked_at_right number of species which are marked (when returning from recursion) |
---|
127 | * |
---|
128 | * @return the number of marked species |
---|
129 | * |
---|
130 | * -------------------------------------------------- |
---|
131 | * Format of configuration string : [Part]+ \0 |
---|
132 | * |
---|
133 | * Part : '\A' ( Group | Species | Sai ) |
---|
134 | * |
---|
135 | * Group : ( OpenedGroup | ClosedGroup ) |
---|
136 | * OpenedGroup : 'G' GroupDef |
---|
137 | * ClosedGroup : 'F' GroupDef |
---|
138 | * GroupDef : 'groupname' [PART]* EndGroup |
---|
139 | * EndGroup : '\AE' |
---|
140 | * |
---|
141 | * SPECIES : 'L' 'speciesname' |
---|
142 | * SAI : 'S' 'sainame' |
---|
143 | * |
---|
144 | * \0 : ASCII 0 (eos) |
---|
145 | * \A : ASCII 1 |
---|
146 | */ |
---|
147 | |
---|
148 | if (!tree) return 0; |
---|
149 | if (tree->is_leaf) { |
---|
150 | if (!tree->gb_node) { |
---|
151 | *marked_at_right = marked_at_left; |
---|
152 | return 0; // Zombie |
---|
153 | } |
---|
154 | |
---|
155 | if (!GB_read_flag(tree->gb_node)) { // unmarked species |
---|
156 | if (*auto_mark) { |
---|
157 | (*auto_mark)--; |
---|
158 | mark_species(tree, extra_marked_species); |
---|
159 | } |
---|
160 | else { |
---|
161 | *marked_at_right = 0; |
---|
162 | return 0; |
---|
163 | } |
---|
164 | } |
---|
165 | else { // marked species |
---|
166 | if (marked_at_left<use_species_aside) { |
---|
167 | // on the left side there are not as many marked species as needed! |
---|
168 | |
---|
169 | nt_assert(marked_at_left>=0); |
---|
170 | |
---|
171 | GBT_TREE *leaf_at_left = tree; |
---|
172 | int step_over = marked_at_left+1; // step over myself |
---|
173 | int then_mark = use_species_aside-marked_at_left; |
---|
174 | |
---|
175 | while (step_over--) { // step over self and over any adjacent, marked species |
---|
176 | leaf_at_left = left_neighbour_leaf(leaf_at_left); |
---|
177 | } |
---|
178 | |
---|
179 | Store_species *marked_back = 0; |
---|
180 | while (leaf_at_left && then_mark--) { // then additionally mark some species |
---|
181 | if (GB_read_flag(leaf_at_left->gb_node) == 0) { // if they are not marked yet |
---|
182 | mark_species(leaf_at_left, extra_marked_species); |
---|
183 | marked_back = (new Store_species(leaf_at_left))->add(marked_back); |
---|
184 | } |
---|
185 | leaf_at_left = left_neighbour_leaf(leaf_at_left); |
---|
186 | } |
---|
187 | |
---|
188 | while (marked_back) { |
---|
189 | GBS_chrcat(memfile, 1); // Separated by 1 |
---|
190 | GBS_strcat(memfile, "L"); |
---|
191 | GBS_strcat(memfile, marked_back->getNode()->name); |
---|
192 | GBS_write_hash(used, marked_back->getNode()->name, 1); // Mark species |
---|
193 | |
---|
194 | Store_species *rest = marked_back->remove(); |
---|
195 | delete marked_back; |
---|
196 | marked_back = rest; |
---|
197 | } |
---|
198 | |
---|
199 | marked_at_left = use_species_aside; |
---|
200 | } |
---|
201 | // now use_species_aside species to left are marked! |
---|
202 | *auto_mark = use_species_aside; |
---|
203 | } |
---|
204 | |
---|
205 | GBS_chrcat(memfile, 1); // Separated by 1 |
---|
206 | GBS_strcat(memfile, "L"); |
---|
207 | GBS_strcat(memfile, tree->name); |
---|
208 | GBS_write_hash(used, tree->name, 1); // Mark species |
---|
209 | |
---|
210 | *marked_at_right = marked_at_left+1; |
---|
211 | return 1; |
---|
212 | } |
---|
213 | |
---|
214 | long oldpos = GBS_memoffset(memfile); |
---|
215 | if (tree->gb_node && tree->name) { // but we are a group |
---|
216 | GBDATA *gb_grouped = GB_entry(tree->gb_node, "grouped"); |
---|
217 | GBS_chrcat(memfile, 1); // Separated by 1 |
---|
218 | if (gb_grouped && GB_read_byte(gb_grouped)) { |
---|
219 | GBS_strcat(memfile, "F"); |
---|
220 | } |
---|
221 | else { |
---|
222 | GBS_strcat(memfile, "G"); |
---|
223 | } |
---|
224 | |
---|
225 | GBS_strcat(memfile, tree->name); |
---|
226 | } |
---|
227 | |
---|
228 | int right_of_leftson; |
---|
229 | long nspecies = nt_build_conf_string_rek(used, tree->leftson, memfile, extra_marked_species, use_species_aside, auto_mark, marked_at_left, &right_of_leftson); |
---|
230 | nspecies += nt_build_conf_string_rek(used, tree->rightson, memfile, extra_marked_species, use_species_aside, auto_mark, right_of_leftson, marked_at_right); |
---|
231 | |
---|
232 | if (tree->gb_node && tree->name) { // but we are a group |
---|
233 | GBS_chrcat(memfile, 1); // Separated by 1 |
---|
234 | GBS_chrcat(memfile, 'E'); // Group end indicated by 'E' |
---|
235 | } |
---|
236 | |
---|
237 | if (!nspecies) { |
---|
238 | long newpos = GBS_memoffset(memfile); |
---|
239 | GBS_str_cut_tail(memfile, newpos-oldpos); // delete group info |
---|
240 | } |
---|
241 | return nspecies; |
---|
242 | } |
---|
243 | |
---|
244 | struct SAI_string_builder { |
---|
245 | GBS_strstruct *sai_middle; |
---|
246 | const char *last_group_name; |
---|
247 | }; |
---|
248 | |
---|
249 | static long nt_build_sai_string_by_hash(const char *key, long val, void *cd_sai_builder) { |
---|
250 | SAI_string_builder *sai_builder = (SAI_string_builder*)cd_sai_builder; |
---|
251 | |
---|
252 | const char *sep = strchr(key, 1); |
---|
253 | if (!sep) return val; // what's wrong |
---|
254 | |
---|
255 | GBS_strstruct *sai_middle = sai_builder->sai_middle; |
---|
256 | const char *last_group_name = sai_builder->last_group_name; |
---|
257 | |
---|
258 | if (!last_group_name || strncmp(key, last_group_name, sep-key)) { // new group |
---|
259 | if (last_group_name) { |
---|
260 | GBS_chrcat(sai_middle, 1); // Separated by 1 |
---|
261 | GBS_chrcat(sai_middle, 'E'); // End of old group |
---|
262 | } |
---|
263 | GBS_chrcat(sai_middle, 1); // Separated by 1 |
---|
264 | GBS_strcat(sai_middle, "FSAI:"); |
---|
265 | GBS_strncat(sai_middle, key, sep-key); |
---|
266 | sai_builder->last_group_name = key; |
---|
267 | } |
---|
268 | GBS_chrcat(sai_middle, 1); // Separated by 1 |
---|
269 | GBS_strcat(sai_middle, "S"); |
---|
270 | GBS_strcat(sai_middle, sep+1); |
---|
271 | return val; |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | static void nt_build_sai_string(GBS_strstruct *topfile, GBS_strstruct *middlefile) { |
---|
276 | //! collect all Sais, place some SAI in top area, rest in middle |
---|
277 | |
---|
278 | GBDATA *gb_sai_data = GBT_get_SAI_data(GLOBAL.gb_main); |
---|
279 | if (gb_sai_data) { |
---|
280 | GB_HASH *hash = GBS_create_hash(GB_number_of_subentries(gb_sai_data), GB_IGNORE_CASE); |
---|
281 | |
---|
282 | for (GBDATA *gb_sai = GBT_first_SAI_rel_SAI_data(gb_sai_data); gb_sai; gb_sai = GBT_next_SAI(gb_sai)) { |
---|
283 | GBDATA *gb_name = GB_search(gb_sai, "name", GB_FIND); |
---|
284 | if (gb_name) { |
---|
285 | char *name = GB_read_string(gb_name); |
---|
286 | |
---|
287 | if (strcmp(name, "HELIX") == 0 || strcmp(name, "HELIX_NR") == 0 || strcmp(name, "ECOLI") == 0) { |
---|
288 | GBS_chrcat(topfile, 1); // Separated by 1 |
---|
289 | GBS_strcat(topfile, "S"); |
---|
290 | GBS_strcat(topfile, name); |
---|
291 | } |
---|
292 | else { |
---|
293 | GBDATA *gb_gn = GB_search(gb_sai, "sai_group", GB_FIND); |
---|
294 | char *gn; |
---|
295 | |
---|
296 | if (gb_gn) gn = GB_read_string(gb_gn); |
---|
297 | else gn = strdup("SAI's"); |
---|
298 | |
---|
299 | char *cn = new char[strlen(gn) + strlen(name) + 2]; |
---|
300 | sprintf(cn, "%s%c%s", gn, 1, name); |
---|
301 | GBS_write_hash(hash, cn, 1); |
---|
302 | delete [] cn; |
---|
303 | free(gn); |
---|
304 | } |
---|
305 | free(name); |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | // open surrounding SAI-group: |
---|
310 | GBS_chrcat(middlefile, 1); |
---|
311 | GBS_strcat(middlefile, "GSAI-Maingroup"); |
---|
312 | |
---|
313 | SAI_string_builder sai_builder = { middlefile, 0 }; |
---|
314 | GBS_hash_do_sorted_loop(hash, nt_build_sai_string_by_hash, GBS_HCF_sortedByKey, &sai_builder); |
---|
315 | if (sai_builder.last_group_name) { |
---|
316 | GBS_chrcat(middlefile, 1); // Separated by 1 |
---|
317 | GBS_chrcat(middlefile, 'E'); // End of old group |
---|
318 | } |
---|
319 | |
---|
320 | // close surrounding SAI-group: |
---|
321 | GBS_chrcat(middlefile, 1); |
---|
322 | GBS_chrcat(middlefile, 'E'); |
---|
323 | |
---|
324 | GBS_free_hash(hash); |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | static void nt_build_conf_marked(GB_HASH *used, GBS_strstruct *file) { |
---|
329 | GBS_chrcat(file, 1); // Separated by 1 |
---|
330 | GBS_strcat(file, "FMore Sequences"); |
---|
331 | GBDATA *gb_species; |
---|
332 | for (gb_species = GBT_first_marked_species(GLOBAL.gb_main); |
---|
333 | gb_species; |
---|
334 | gb_species = GBT_next_marked_species(gb_species)) { |
---|
335 | char *name = GBT_read_string(gb_species, "name"); |
---|
336 | if (GBS_read_hash(used, name)) { |
---|
337 | free(name); |
---|
338 | continue; |
---|
339 | } |
---|
340 | GBS_chrcat(file, 1); |
---|
341 | GBS_strcat(file, "L"); |
---|
342 | GBS_strcat(file, name); |
---|
343 | free(name); |
---|
344 | } |
---|
345 | |
---|
346 | GBS_chrcat(file, 1); // Separated by 1 |
---|
347 | GBS_chrcat(file, 'E'); // Group end indicated by 'E' |
---|
348 | } |
---|
349 | |
---|
350 | enum extractType { |
---|
351 | CONF_EXTRACT, |
---|
352 | CONF_MARK, |
---|
353 | CONF_UNMARK, |
---|
354 | CONF_INVERT, |
---|
355 | CONF_COMBINE // logical AND |
---|
356 | }; |
---|
357 | |
---|
358 | static void nt_extract_configuration(AW_window *aww, extractType ext_type) { |
---|
359 | GB_transaction ta(GLOBAL.gb_main); |
---|
360 | AW_root *aw_root = aww->get_root(); |
---|
361 | char *cn = aw_root->awar(AWAR_CONFIGURATION)->read_string(); |
---|
362 | |
---|
363 | if (strcmp(cn, NO_CONFIG_SELECTED) == 0) { |
---|
364 | aw_message("Please select a configuration"); |
---|
365 | } |
---|
366 | else { |
---|
367 | GBDATA *gb_configuration = GBT_find_configuration(GLOBAL.gb_main, cn); |
---|
368 | if (!gb_configuration) { |
---|
369 | aw_message(GBS_global_string("Configuration '%s' not found in the database", cn)); |
---|
370 | } |
---|
371 | else { |
---|
372 | GBDATA *gb_middle_area = GB_search(gb_configuration, "middle_area", GB_STRING); |
---|
373 | char *md = 0; |
---|
374 | size_t unknown_species = 0; |
---|
375 | bool refresh = false; |
---|
376 | |
---|
377 | if (gb_middle_area) { |
---|
378 | GB_HASH *was_marked = NULL; // only used for CONF_COMBINE |
---|
379 | |
---|
380 | switch (ext_type) { |
---|
381 | case CONF_EXTRACT: |
---|
382 | GBT_mark_all(GLOBAL.gb_main, 0); // unmark all for extract |
---|
383 | refresh = true; |
---|
384 | break; |
---|
385 | case CONF_COMBINE: { |
---|
386 | // store all marked species in hash and unmark them |
---|
387 | was_marked = GBS_create_hash(GBT_get_species_count(GLOBAL.gb_main), GB_IGNORE_CASE); |
---|
388 | for (GBDATA *gbd = GBT_first_marked_species(GLOBAL.gb_main); gbd; gbd = GBT_next_marked_species(gbd)) { |
---|
389 | int marked = GB_read_flag(gbd); |
---|
390 | if (marked) { |
---|
391 | GBS_write_hash(was_marked, GBT_read_name(gbd), 1); |
---|
392 | GB_write_flag(gbd, 0); |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | refresh = refresh || GBS_hash_count_elems(was_marked); |
---|
397 | break; |
---|
398 | } |
---|
399 | default: |
---|
400 | break; |
---|
401 | } |
---|
402 | |
---|
403 | md = GB_read_string(gb_middle_area); |
---|
404 | if (md) { |
---|
405 | char *p; |
---|
406 | char tokens[2]; |
---|
407 | tokens[0] = 1; |
---|
408 | tokens[1] = 0; |
---|
409 | for (p = strtok(md, tokens); p; p = strtok(NULL, tokens)) { |
---|
410 | if (p[0] == 'L') { |
---|
411 | const char *species_name = p+1; |
---|
412 | GBDATA *gb_species = GBT_find_species(GLOBAL.gb_main, species_name); |
---|
413 | |
---|
414 | if (gb_species) { |
---|
415 | int oldmark = GB_read_flag(gb_species); |
---|
416 | int newmark = oldmark; |
---|
417 | switch (ext_type) { |
---|
418 | case CONF_EXTRACT: |
---|
419 | case CONF_MARK: newmark = 1; break; |
---|
420 | case CONF_UNMARK: newmark = 0; break; |
---|
421 | case CONF_INVERT: newmark = !oldmark; break; |
---|
422 | case CONF_COMBINE: { |
---|
423 | nt_assert(!oldmark); // should have been unmarked above |
---|
424 | newmark = GBS_read_hash(was_marked, species_name); // mark if was_marked |
---|
425 | break; |
---|
426 | } |
---|
427 | default: nt_assert(0); break; |
---|
428 | } |
---|
429 | if (newmark != oldmark) { |
---|
430 | GB_write_flag(gb_species, newmark); |
---|
431 | refresh = true; |
---|
432 | } |
---|
433 | } |
---|
434 | else { |
---|
435 | unknown_species++; |
---|
436 | } |
---|
437 | } |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | if (was_marked) GBS_free_hash(was_marked); |
---|
442 | } |
---|
443 | |
---|
444 | if (unknown_species>0) { |
---|
445 | aw_message(GBS_global_string("configuration '%s' contains %zu unknown species", cn, unknown_species)); |
---|
446 | } |
---|
447 | |
---|
448 | if (refresh) aw_root->awar(AWAR_TREE_REFRESH)->touch(); |
---|
449 | |
---|
450 | free(md); |
---|
451 | } |
---|
452 | } |
---|
453 | free(cn); |
---|
454 | } |
---|
455 | |
---|
456 | static void nt_delete_configuration(AW_window *aww) { |
---|
457 | GB_transaction transaction_var(GLOBAL.gb_main); |
---|
458 | char *cn = aww->get_root()->awar(AWAR_CONFIGURATION)->read_string(); |
---|
459 | GBDATA *gb_configuration = GBT_find_configuration(GLOBAL.gb_main, cn); |
---|
460 | if (gb_configuration) { |
---|
461 | GB_ERROR error = GB_delete(gb_configuration); |
---|
462 | if (error) aw_message(error); |
---|
463 | } |
---|
464 | free(cn); |
---|
465 | } |
---|
466 | |
---|
467 | |
---|
468 | static GB_ERROR nt_create_configuration(AW_window *, GBT_TREE *tree, const char *conf_name, int use_species_aside) { |
---|
469 | char *to_free = NULL; |
---|
470 | GB_ERROR error = NULL; |
---|
471 | |
---|
472 | if (!conf_name) { |
---|
473 | char *existing_configs = awt_create_string_on_configurations(GLOBAL.gb_main); |
---|
474 | conf_name = to_free = aw_string_selection2awar("CREATE CONFIGURATION", "Enter name of configuration:", AWAR_CONFIGURATION, existing_configs, NULL); |
---|
475 | free(existing_configs); |
---|
476 | } |
---|
477 | |
---|
478 | if (!conf_name || !conf_name[0]) error = "no config name given"; |
---|
479 | else { |
---|
480 | if (use_species_aside==-1) { |
---|
481 | static int last_used_species_aside = 3; |
---|
482 | { |
---|
483 | const char *val = GBS_global_string("%i", last_used_species_aside); |
---|
484 | char *use_species = aw_input("How many extra species to view aside marked:", val); |
---|
485 | if (use_species) use_species_aside = atoi(use_species); |
---|
486 | free(use_species); |
---|
487 | } |
---|
488 | |
---|
489 | if (use_species_aside<1) error = "illegal number of 'species aside'"; |
---|
490 | else last_used_species_aside = use_species_aside; // remember for next time |
---|
491 | } |
---|
492 | |
---|
493 | if (!error) { |
---|
494 | GB_transaction ta(GLOBAL.gb_main); // open close transaction |
---|
495 | GB_HASH *used = GBS_create_hash(GBT_get_species_count(GLOBAL.gb_main), GB_MIND_CASE); |
---|
496 | GBS_strstruct *topfile = GBS_stropen(1000); |
---|
497 | GBS_strstruct *topmid = GBS_stropen(10000); |
---|
498 | { |
---|
499 | GBS_strstruct *middlefile = GBS_stropen(10000); |
---|
500 | nt_build_sai_string(topfile, topmid); |
---|
501 | |
---|
502 | if (use_species_aside) { |
---|
503 | Store_species *extra_marked_species = 0; |
---|
504 | int auto_mark = 0; |
---|
505 | int marked_at_right; |
---|
506 | |
---|
507 | nt_build_conf_string_rek(used, tree, middlefile, &extra_marked_species, use_species_aside, &auto_mark, use_species_aside, &marked_at_right); |
---|
508 | if (extra_marked_species) { |
---|
509 | extra_marked_species->call(unmark_species); |
---|
510 | delete extra_marked_species; |
---|
511 | } |
---|
512 | } |
---|
513 | else { |
---|
514 | int dummy_1=0, dummy_2; |
---|
515 | nt_build_conf_string_rek(used, tree, middlefile, 0, 0, &dummy_1, 0, &dummy_2); |
---|
516 | } |
---|
517 | nt_build_conf_marked(used, topmid); |
---|
518 | char *mid = GBS_strclose(middlefile); |
---|
519 | GBS_strcat(topmid, mid); |
---|
520 | free(mid); |
---|
521 | } |
---|
522 | |
---|
523 | { |
---|
524 | char *middle = GBS_strclose(topmid); |
---|
525 | char *top = GBS_strclose(topfile); |
---|
526 | GBDATA *gb_config = GBT_create_configuration(GLOBAL.gb_main, conf_name); |
---|
527 | |
---|
528 | if (!gb_config) error = GB_await_error(); |
---|
529 | else { |
---|
530 | error = GBT_write_string(gb_config, "top_area", top); |
---|
531 | if (!error) error = GBT_write_string(gb_config, "middle_area", middle); |
---|
532 | } |
---|
533 | |
---|
534 | free(middle); |
---|
535 | free(top); |
---|
536 | } |
---|
537 | GBS_free_hash(used); |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | free(to_free); |
---|
542 | return error; |
---|
543 | } |
---|
544 | |
---|
545 | static void nt_store_configuration(AW_window*, AWT_canvas *ntw) { |
---|
546 | GB_ERROR err = nt_create_configuration(0, nt_get_tree_root_of_canvas(ntw), 0, 0); |
---|
547 | aw_message_if(err); |
---|
548 | } |
---|
549 | |
---|
550 | static void nt_rename_configuration(AW_window *aww) { |
---|
551 | AW_awar *awar_curr_cfg = aww->get_root()->awar(AWAR_CONFIGURATION); |
---|
552 | char *old_name = awar_curr_cfg->read_string(); |
---|
553 | GB_ERROR err = 0; |
---|
554 | |
---|
555 | GB_transaction ta(GLOBAL.gb_main); |
---|
556 | |
---|
557 | char *new_name = aw_input("Rename selection", "Enter the new name of the selection", old_name); |
---|
558 | if (new_name) { |
---|
559 | GBDATA *gb_existing_cfg = GBT_find_configuration(GLOBAL.gb_main, new_name); |
---|
560 | if (gb_existing_cfg) err = GBS_global_string("There is already a selection named '%s'", new_name); |
---|
561 | else { |
---|
562 | GBDATA *gb_old_cfg = GBT_find_configuration(GLOBAL.gb_main, old_name); |
---|
563 | if (gb_old_cfg) { |
---|
564 | GBDATA *gb_name = GB_entry(gb_old_cfg, "name"); |
---|
565 | if (gb_name) { |
---|
566 | err = GB_write_string(gb_name, new_name); |
---|
567 | if (!err) awar_curr_cfg->write_string(new_name); |
---|
568 | } |
---|
569 | else err = "Selection has no name"; |
---|
570 | } |
---|
571 | else err = "Can't find that selection"; |
---|
572 | } |
---|
573 | free(new_name); |
---|
574 | } |
---|
575 | |
---|
576 | if (err) aw_message(err); |
---|
577 | free(old_name); |
---|
578 | } |
---|
579 | |
---|
580 | static AW_window *create_configuration_admin_window(AW_root *root, AWT_canvas *ntw) { |
---|
581 | static AW_window_simple *existing_aws[MAX_NT_WINDOWS] = { MAX_NT_WINDOWS_NULLINIT }; |
---|
582 | |
---|
583 | int ntw_id = NT_get_canvas_id(ntw); |
---|
584 | if (!existing_aws[ntw_id]) { |
---|
585 | init_config_awars(root); |
---|
586 | |
---|
587 | AW_window_simple *aws = new AW_window_simple; |
---|
588 | aws->init(root, "SPECIES_SELECTIONS", "Species Selections"); |
---|
589 | aws->load_xfig("nt_selection.fig"); |
---|
590 | |
---|
591 | aws->at("close"); |
---|
592 | aws->callback((AW_CB0)AW_POPDOWN); |
---|
593 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
594 | |
---|
595 | aws->at("help"); |
---|
596 | aws->callback(makeHelpCallback("configuration.hlp")); |
---|
597 | aws->create_button("HELP", "HELP", "H"); |
---|
598 | |
---|
599 | aws->at("list"); |
---|
600 | awt_create_selection_list_on_configurations(GLOBAL.gb_main, aws, AWAR_CONFIGURATION, false); |
---|
601 | |
---|
602 | aws->at("store"); |
---|
603 | aws->callback(makeWindowCallback(nt_store_configuration, ntw)); |
---|
604 | aws->create_button(GBS_global_string("STORE_%i", ntw_id), "STORE", "S"); |
---|
605 | |
---|
606 | aws->at("extract"); |
---|
607 | aws->callback(makeWindowCallback(nt_extract_configuration, CONF_EXTRACT)); |
---|
608 | aws->create_button("EXTRACT", "EXTRACT", "E"); |
---|
609 | |
---|
610 | aws->at("mark"); |
---|
611 | aws->callback(makeWindowCallback(nt_extract_configuration, CONF_MARK)); |
---|
612 | aws->create_button("MARK", "MARK", "M"); |
---|
613 | |
---|
614 | aws->at("unmark"); |
---|
615 | aws->callback(makeWindowCallback(nt_extract_configuration, CONF_UNMARK)); |
---|
616 | aws->create_button("UNMARK", "UNMARK", "U"); |
---|
617 | |
---|
618 | aws->at("invert"); |
---|
619 | aws->callback(makeWindowCallback(nt_extract_configuration, CONF_INVERT)); |
---|
620 | aws->create_button("INVERT", "INVERT", "I"); |
---|
621 | |
---|
622 | aws->at("combine"); |
---|
623 | aws->callback(makeWindowCallback(nt_extract_configuration, CONF_COMBINE)); |
---|
624 | aws->create_button("COMBINE", "COMBINE", "C"); |
---|
625 | |
---|
626 | aws->at("delete"); |
---|
627 | aws->callback(nt_delete_configuration); |
---|
628 | aws->create_button("DELETE", "DELETE", "D"); |
---|
629 | |
---|
630 | aws->at("rename"); |
---|
631 | aws->callback(nt_rename_configuration); |
---|
632 | aws->create_button("RENAME", "RENAME", "R"); |
---|
633 | |
---|
634 | existing_aws[ntw_id] = aws; |
---|
635 | } |
---|
636 | return existing_aws[ntw_id]; |
---|
637 | } |
---|
638 | |
---|
639 | void NT_popup_configuration_admin(AW_window *aw_main, AW_CL cl_ntw, AW_CL) { |
---|
640 | AW_window *aww = create_configuration_admin_window(aw_main->get_root(), (AWT_canvas*)cl_ntw); |
---|
641 | aww->activate(); |
---|
642 | } |
---|
643 | |
---|
644 | // ----------------------------------------- |
---|
645 | // various ways to start the editor |
---|
646 | |
---|
647 | #define CONFNAME "default_configuration" |
---|
648 | |
---|
649 | static void nt_start_editor_on_configuration(AW_window *aww) { |
---|
650 | aww->hide(); |
---|
651 | |
---|
652 | const char *cn = aww->get_root()->awar(AWAR_CONFIGURATION)->read_char_pntr(); |
---|
653 | const char *com = GBS_global_string("arb_edit4 -c '%s' &", cn); |
---|
654 | |
---|
655 | aw_message_if(GBK_system(com)); |
---|
656 | } |
---|
657 | |
---|
658 | AW_window *NT_create_startEditorOnOldConfiguration_window(AW_root *awr) { |
---|
659 | static AW_window_simple *aws = 0; |
---|
660 | if (!aws) { |
---|
661 | init_config_awars(awr); |
---|
662 | |
---|
663 | aws = new AW_window_simple; |
---|
664 | aws->init(awr, "SELECT_CONFIGURATION", "SELECT A CONFIGURATION"); |
---|
665 | aws->at(10, 10); |
---|
666 | aws->auto_space(0, 0); |
---|
667 | awt_create_selection_list_on_configurations(GLOBAL.gb_main, aws, AWAR_CONFIGURATION, false); |
---|
668 | aws->at_newline(); |
---|
669 | |
---|
670 | aws->callback((AW_CB0)nt_start_editor_on_configuration); |
---|
671 | aws->create_button("START", "START"); |
---|
672 | |
---|
673 | aws->callback(AW_POPDOWN); |
---|
674 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
675 | |
---|
676 | aws->window_fit(); |
---|
677 | } |
---|
678 | return aws; |
---|
679 | } |
---|
680 | |
---|
681 | void NT_start_editor_on_tree(AW_window *, AW_CL cl_use_species_aside, AW_CL cl_ntw) { |
---|
682 | GB_ERROR error = nt_create_configuration(0, nt_get_tree_root_of_canvas((AWT_canvas*)cl_ntw), CONFNAME, (int)cl_use_species_aside); |
---|
683 | if (!error) error = GBK_system("arb_edit4 -c " CONFNAME " &"); |
---|
684 | aw_message_if(error); |
---|
685 | } |
---|
686 | |
---|
687 | |
---|