source: tags/arb-6.0-rc1/EDIT4/ED4_root.cxx

Last change on this file was 11645, checked in by westram, 11 years ago
  • fix missing/duplicated/invalid mnemonics in rest of arb (completes [11643])
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 78.5 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : ED4_root.cxx                                      //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "ed4_class.hxx"
12#include "ed4_awars.hxx"
13#include "ed4_tools.hxx"
14#include "ed4_block.hxx"
15#include "ed4_dots.hxx"
16#include "ed4_nds.hxx"
17#include "ed4_list.hxx"
18#include "ed4_plugins.hxx"
19#include "ed4_visualizeSAI.hxx"
20#include "ed4_naligner.hxx"
21#include "ed4_ProteinViewer.hxx"
22#include "ed4_protein_2nd_structure.hxx"
23#include "graph_aligner_gui.hxx"
24#include "ed4_colStat.hxx"
25
26#include <ed4_extern.hxx>
27#include <fast_aligner.hxx>
28#include <AW_helix.hxx>
29#include <gde.hxx>
30#include <awt.hxx>
31#include <awt_seq_colors.hxx>
32#include <awt_map_key.hxx>
33#include <aw_awars.hxx>
34#include <aw_preset.hxx>
35#include <aw_msg.hxx>
36#include <arb_progress.h>
37#include <aw_root.hxx>
38#include <aw_question.hxx>
39#include <aw_advice.hxx>
40#include "../WINDOW/aw_status.hxx" // @@@ hack - obsolete when EDIT4 status works like elsewhere
41#include <arb_version.h>
42#include <arb_file.h>
43#include <arbdbt.h>
44#include <ad_cb.h>
45#include <macros.hxx>
46
47#include <cctype>
48
49AW_window *AWTC_create_island_hopping_window(AW_root *root, AW_CL);
50
51ED4_WinContext ED4_WinContext::current_context;
52
53void ED4_folding_line::warn_illegal_dimension() {
54#if defined(DEBUG)
55    if (dimension<0.0) {
56        // e4_assert(0); // crashes gdb when called from scrollbar callback - so instead only dump backtrace
57        const char *msg = GBS_global_string("illegal dimension %f\n", dimension);
58        GBK_dump_backtrace(stderr, msg);
59    }
60#endif
61}
62
63void ED4_WinContext::warn_missing_context() const {
64    e4_assert(0);
65    GBK_dump_backtrace(stderr, "Missing context");
66    aw_message("Missing context - please send information from console to devel@arb-home.de");
67}
68
69static ARB_ERROR request_terminal_refresh(ED4_base *base, AW_CL cl_level) {
70    ED4_level lev = ED4_level(cl_level);
71    if (lev == ED4_L_NO_LEVEL || (base->spec.level&lev) != 0) {
72        if (base->is_terminal()) base->request_refresh();
73    }
74    return NULL;
75}
76
77void ED4_root::request_refresh_for_all_terminals() {
78    main_manager->route_down_hierarchy(request_terminal_refresh, ED4_L_NO_LEVEL).expect_no_error();
79}
80
81void ED4_root::request_refresh_for_specific_terminals(ED4_level lev) {
82    main_manager->route_down_hierarchy(request_terminal_refresh, lev).expect_no_error();
83}
84
85
86static ARB_ERROR request_sequence_refresh(ED4_base *base, AW_CL cl_consensi) {
87    ARB_ERROR error;
88    if (base->spec.level & ED4_L_SPECIES) {
89        bool consensi = bool(cl_consensi);
90        if (base->is_consensus_manager() == consensi) {
91            error = base->to_manager()->route_down_hierarchy(request_terminal_refresh, ED4_L_SEQUENCE_STRING);
92        }
93    }
94    return error;
95}
96
97// if you want to refresh consensi AND sequences you may use request_refresh_for_specific_terminals(ED4_L_SEQUENCE_STRING)
98void ED4_root::request_refresh_for_consensus_terminals() {
99    main_manager->route_down_hierarchy(request_sequence_refresh, true).expect_no_error();
100}
101void ED4_root::request_refresh_for_sequence_terminals() {
102    if (main_manager) {
103        main_manager->route_down_hierarchy(request_sequence_refresh, false).expect_no_error();
104    }
105}
106
107void ED4_root::refresh_window_simple(bool redraw) {
108    // if 'redraw' -> update everything (ignoring refresh flag)
109    int refresh_all = 0;
110    if (redraw) {
111#if defined(TRACE_REFRESH)
112        fprintf(stderr, "- clear display (refresh_window_simple(redraw=true) called)\n"); fflush(stderr);
113#endif
114        main_manager->update_info.set_clear_at_refresh(1);
115        refresh_all = 1;
116    }
117
118    main_manager->Show(refresh_all, 0);
119    if (redraw) main_manager->update_info.set_clear_at_refresh(0);
120}
121
122void ED4_root::handle_update_requests(bool& redraw) {
123    if (main_manager->update_info.delete_requested) {
124#if defined(TRACE_REFRESH)
125        fprintf(stderr, "- handling requested deletes\n"); fflush(stderr);
126#endif
127        main_manager->delete_requested_children();
128        redraw = true;
129    }
130
131    if (main_manager->update_info.update_requested) {
132#if defined(TRACE_REFRESH)
133        fprintf(stderr, "- handling requested updates\n"); fflush(stderr);
134#endif
135        main_manager->update_requested_children();
136        redraw = true; // @@@ needed ?
137    }
138
139    while (main_manager->update_info.resize) {
140#if defined(TRACE_REFRESH)
141        fprintf(stderr, "- handling requested resizes\n"); fflush(stderr);
142#endif
143        main_manager->resize_requested_children();
144        redraw = true;
145    }
146
147    // make sure all update request have been handled:
148    e4_assert(!main_manager->update_info.delete_requested);
149    e4_assert(!main_manager->update_info.update_requested);
150    e4_assert(!main_manager->update_info.resize);
151}
152
153void ED4_root::special_window_refresh(bool handle_updates) {
154    // this function should only be used for window specific updates (e.g. cursor placement/deletion)
155    e4_assert(ED4_WinContext::have_context());
156
157    bool redraw = true;
158    if (handle_updates) handle_update_requests(redraw);
159    refresh_window_simple(redraw);
160    // do NOT clear_refresh_requests here!! this is no full refresh!
161}
162
163ED4_returncode ED4_root::refresh_all_windows(bool redraw) {
164    // if 'redraw' -> update everything (ignoring refresh flag)
165    GB_transaction ta(GLOBAL_gb_main);
166
167    handle_update_requests(redraw);
168   
169    ED4_window *window = first_window;
170    while (window) {
171        ED4_LocalWinContext uses(window);
172        refresh_window_simple(redraw);
173        window = window->next;
174    }
175
176    if (main_manager->update_info.refresh) main_manager->clear_refresh();
177
178    return (ED4_R_OK);
179}
180
181// --------------------------------------------------------------------------------
182
183#ifdef UNIT_TESTS
184#ifndef TEST_UNIT_H
185#include <test_unit.h>
186#endif
187
188static arb_test::match_expectation correct_win2world_calculation(ED4_foldable& foldable, int xwin_org, int ywin_org, int xwrld_expd, int ywrld_expd) {
189    using namespace arb_test;
190    match_expectation precondition(all().of(that(xwrld_expd).is_more_or_equal(xwin_org),
191                                            that(ywrld_expd).is_more_or_equal(ywin_org)));
192
193    AW_pos xwrld_calc = xwin_org;
194    AW_pos ywrld_calc = ywin_org;
195    foldable.win_to_world_coords(&xwrld_calc, &ywrld_calc);
196
197    match_expectation win_2_world_conversion(all().of(that(xwrld_calc).is_equal_to(xwrld_expd),
198                                                      that(ywrld_calc).is_equal_to(ywrld_expd)));
199
200    AW_pos xwin_back = xwrld_calc;
201    AW_pos ywin_back = ywrld_calc;
202    foldable.world_to_win_coords(&xwin_back, &ywin_back);
203
204    match_expectation world_back2_win_conversion(all().of(that(xwin_back).is_equal_to(xwin_org),
205                                                          that(ywin_back).is_equal_to(ywin_org)));
206
207    return all().of(precondition, win_2_world_conversion, world_back2_win_conversion);
208}
209
210static arb_test::match_expectation correct_world2win_calculation(ED4_foldable& foldable, int xwrld_org, int ywrld_org, int xwin_expd, int ywin_expd) {
211    using namespace arb_test;
212    match_expectation precondition(all().of(that(xwrld_org).is_more_or_equal(xwin_expd),
213                                            that(ywrld_org).is_more_or_equal(ywin_expd)));
214
215
216    AW_pos xwin_calc = xwrld_org;
217    AW_pos ywin_calc = ywrld_org;
218    foldable.world_to_win_coords(&xwin_calc, &ywin_calc);
219
220    match_expectation world_2_win_conversion(all().of(that(xwin_calc).is_equal_to(xwin_expd),
221                                                      that(ywin_calc).is_equal_to(ywin_expd)));
222
223    return all().of(precondition, world_2_win_conversion);
224}
225
226#define TEST_EXPECT_WIN_UNFOLDED(xwi,ywi)              TEST_EXPECTATION(correct_win2world_calculation(foldable, xwi, ywi, xwi, ywi))
227#define TEST_EXPECT_WIN_WORLD_FOLDING(xwi,ywi,fx,fy)   TEST_EXPECTATION(correct_win2world_calculation(foldable, xwi, ywi, (xwi)+(fx), (ywi)+(fy)))
228#define TEST_EXPECT_WORLD_WIN_FOLDING(xwo,ywo,xwi,ywi) TEST_EXPECTATION(correct_world2win_calculation(foldable, xwo, ywo, xwi, ywi))
229
230#define TEST_EXPECT_WIN_WORLD_FOLDING__BROKEN(xwi,ywi,fx,fy)        TEST_EXPECTATION__BROKEN(correct_win2world_calculation(foldable, xwi, ywi, (xwi)+(fx), (ywi)+(fy)))
231#define TEST_EXPECT_WIN_WORLD_FOLDING__BROKENIF(when,xwi,ywi,fx,fy) TEST_EXPECTATION__BROKENIF(when, correct_win2world_calculation(foldable, xwi, ywi, (xwi)+(fx), (ywi)+(fy)))
232
233void TEST_win_2_world() {
234    ED4_foldable foldable;
235
236    const int X1 = 200;
237    const int X2 = 300;
238    const int Y1 = 100;
239    const int Y2 = 200;
240
241    ED4_folding_line *hor100 = foldable.insert_folding_line(Y1, 0, ED4_P_HORIZONTAL);
242    ED4_folding_line *ver200 = foldable.insert_folding_line(X1, 0, ED4_P_VERTICAL);
243    ED4_folding_line *hor200 = foldable.insert_folding_line(Y2, 0, ED4_P_HORIZONTAL);
244    ED4_folding_line *ver300 = foldable.insert_folding_line(X2, 0, ED4_P_VERTICAL);
245
246    // nothing folded yet
247
248    const int x01 = X1/2;
249    const int x12 = (X1+X2)/2;
250    const int x23 = X2+x01;
251
252    const int y01 = Y1/2;
253    const int y12 = (Y1+Y2)/2;
254    const int y23 = Y2+y01;
255
256    TEST_EXPECT_WIN_UNFOLDED(x01, y01);
257    TEST_EXPECT_WIN_UNFOLDED(x12, y01);
258    TEST_EXPECT_WIN_UNFOLDED(x23, y01);
259
260    TEST_EXPECT_WIN_UNFOLDED(x01, y12);
261    TEST_EXPECT_WIN_UNFOLDED(x12, y12);
262    TEST_EXPECT_WIN_UNFOLDED(x23, y12);
263
264    TEST_EXPECT_WIN_UNFOLDED(x01, y23);
265    TEST_EXPECT_WIN_UNFOLDED(x12, y23);
266    TEST_EXPECT_WIN_UNFOLDED(x23, y23);
267
268    for (int FACTOR = 1; FACTOR <= 100; FACTOR += 7) {
269        TEST_ANNOTATE(GBS_global_string("FACTOR=%i", FACTOR));
270        int H1 = FACTOR* 10;
271        int H2 = FACTOR* 40;
272        int V1 = FACTOR* 20;
273        int V2 = FACTOR* 80;
274
275        hor100->set_dimension(H1);
276        hor200->set_dimension(H2);
277        ver200->set_dimension(V1);
278        ver300->set_dimension(V2);
279
280        TEST_EXPECT_WIN_UNFOLDED     (x01, y01); // always in unfolded range
281        TEST_EXPECT_WIN_WORLD_FOLDING(x12, y01, V1,    0);
282        TEST_EXPECT_WIN_WORLD_FOLDING(x23, y01, V1+V2, 0);
283
284        TEST_EXPECT_WIN_WORLD_FOLDING(x01, y12, 0,     H1);
285        TEST_EXPECT_WIN_WORLD_FOLDING(x12, y12, V1,    H1);
286        TEST_EXPECT_WIN_WORLD_FOLDING(x23, y12, V1+V2, H1);
287
288        TEST_EXPECT_WIN_WORLD_FOLDING(x01, y23, 0,     H1+H2);
289        TEST_EXPECT_WIN_WORLD_FOLDING(x12, y23, V1,    H1+H2);
290        TEST_EXPECT_WIN_WORLD_FOLDING(x23, y23, V1+V2, H1+H2);
291
292        // test "folded" world positions.
293        // they result in win positions lower than folding line!
294        TEST_EXPECT_WORLD_WIN_FOLDING(X1-1,    Y1-1,    X1-1,    Y1-1);    // left of/above folded range
295        TEST_EXPECT_WORLD_WIN_FOLDING(X1,      Y1,      X1-V1,   Y1-H1);   // left/upper end of folded range
296        TEST_EXPECT_WORLD_WIN_FOLDING(X1+5,    Y1+5,    X1+5-V1, Y1+5-H1); // in folded range
297        TEST_EXPECT_WORLD_WIN_FOLDING(X1+V1,   Y1+H1,   X1,      Y1);      // right/lower end of folded range
298        TEST_EXPECT_WORLD_WIN_FOLDING(X1+V1+1, Y1+H1+1, X1+1,    Y1+1);    // right of/below folded range
299    }
300}
301
302#endif // UNIT_TESTS
303
304// --------------------------------------------------------------------------------
305
306ED4_returncode ED4_root::deselect_all()
307{
308    ED4_multi_species_manager *main_multi_man = middle_area_man->get_multi_species_manager();
309    main_multi_man->deselect_all_species_and_SAI();
310
311    main_multi_man = top_area_man->get_multi_species_manager();
312    main_multi_man->deselect_all_species_and_SAI();
313
314    return ED4_R_OK;
315}
316
317void ED4_root::remove_from_selected(ED4_species_name_terminal *name_term) { // @@@ change param to ED4_species_manager ?
318    if (name_term) {
319        if ((selected_objects->has_elem(name_term->selection_info))) {
320            selected_objects->remove_elem(name_term->selection_info);
321
322            delete name_term->selection_info;
323            name_term->selection_info    = NULL;
324            name_term->containing_species_manager()->set_selected(false);
325            name_term->dragged = false; // @@@ caller shall do this
326
327#ifdef DEBUG
328            GBDATA *gbd = name_term->get_species_pointer();
329
330            if (gbd) {
331                printf("removed term '%s'\n", GB_read_char_pntr(gbd));
332            }
333            else {
334                ED4_species_manager *spec_man = name_term->get_parent(ED4_L_SPECIES)->to_species_manager();
335
336                if (spec_man->is_consensus_manager()) {
337                    printf("removed consensus '%s'\n", name_term->id);
338                }
339                else {
340                    printf("removed unknown term '%s'\n", name_term->id ? name_term->id : "NULL");
341                }
342            }
343#endif
344
345            name_term->request_refresh();
346            ED4_sequence_terminal *seq_term = name_term->corresponding_sequence_terminal();
347            if (seq_term) seq_term->request_refresh();
348
349            // ProtView: Refresh corresponding orf terminals
350            if (alignment_type == GB_AT_DNA) {
351                PV_CallBackFunction(this->aw_root);
352            }
353
354            ED4_multi_species_manager *multi_man = name_term->get_parent(ED4_L_MULTI_SPECIES)->to_multi_species_manager();
355            multi_man->invalidate_species_counters();
356        }
357    }
358}
359
360void ED4_root::announce_useraction_in(AW_window *aww) {
361    for (ED4_window *win = first_window; win; win = win->next) {
362        if (win->aww == aww) {
363            most_recently_used_window = win;
364        }
365    }
366}
367
368ED4_returncode ED4_root::add_to_selected(ED4_species_name_terminal *name_term) { // @@@ change param to ED4_species_manager ?
369    if (!name_term || !(name_term->dynamic_prop & ED4_P_SELECTABLE)) {   // check if object exists and may be selected
370        return (ED4_R_IMPOSSIBLE);
371    }
372
373    if (!(selected_objects->has_elem(name_term->selection_info))) {     // object is really new to our list => calculate current extension and append it
374        ED4_selection_entry *sel_info = new ED4_selection_entry;
375        name_term->selection_info     = sel_info;
376
377        if (name_term->dynamic_prop & ED4_P_IS_HANDLE) { // object is a handle for an object up in the hierarchy => search it
378            ED4_level  mlevel     = name_term->spec.handled_level;
379            ED4_base  *tmp_object = name_term;
380
381            while ((tmp_object != NULL) && !(tmp_object->spec.level & mlevel)) {
382                tmp_object = tmp_object->parent;
383            }
384
385            if (!tmp_object) return (ED4_R_WARNING); // no target level found
386
387            sel_info->actual_width  = tmp_object->extension.size[WIDTH];
388            sel_info->actual_height = tmp_object->extension.size[HEIGHT];
389        }
390        else // selected object is no handle => take it directly
391        {
392            sel_info->actual_width  = name_term->extension.size[WIDTH];
393            sel_info->actual_height = name_term->extension.size[HEIGHT];
394        }
395
396        sel_info->drag_old_x  = 0;
397        sel_info->drag_old_y  = 0;
398        sel_info->drag_off_x  = 0;
399        sel_info->drag_off_y  = 0;
400        sel_info->old_event_y = 0;
401
402        sel_info->object = name_term;
403
404        selected_objects->prepend_elem(sel_info);
405
406        name_term->containing_species_manager()->set_selected(true);
407
408#ifdef DEBUG
409        GBDATA *gbd = name_term->get_species_pointer();
410
411        if (gbd) {
412            printf("added term '%s'\n", GB_read_char_pntr(gbd));
413        }
414        else {
415            ED4_species_manager *spec_man = name_term->get_parent(ED4_L_SPECIES)->to_species_manager();
416            if (spec_man->is_consensus_manager()) {
417                printf("added consensus '%s'\n", name_term->id);
418            }
419            else {
420                printf("added unknown term '%s'\n", name_term->id ? name_term->id : "NULL");
421            }
422        }
423#endif
424
425        name_term->request_refresh();
426        ED4_sequence_terminal *seq_term = name_term->corresponding_sequence_terminal();
427        if (seq_term) seq_term->request_refresh();
428
429        // ProtView: Refresh corresponding orf terminals
430        if (alignment_type == GB_AT_DNA) {
431            PV_CallBackFunction(this->aw_root);
432        }
433
434        ED4_multi_species_manager *multi_man = name_term->get_parent(ED4_L_MULTI_SPECIES)->to_multi_species_manager();
435        multi_man->invalidate_species_counters();
436
437        return (ED4_R_OK);
438    }
439
440    return (ED4_R_IMPOSSIBLE);
441}
442
443void ED4_root::resize_all() {
444    while (main_manager->update_info.resize) {
445        main_manager->resize_requested_children();
446    }
447}
448
449static ARB_ERROR change_char_table_length(ED4_base *base, AW_CL new_length) {
450    if (base->is_abstract_group_manager()) {
451        ED4_abstract_group_manager *group_man = base->to_abstract_group_manager();
452        group_man->table().change_table_length(new_length);
453    }
454    return NULL;
455}
456
457void ED4_alignment_length_changed(GBDATA *gb_alignment_len, GB_CB_TYPE IF_ASSERTION_USED(gbtype)) // callback from database
458{
459    e4_assert(gbtype==GB_CB_CHANGED);
460    int new_length = GB_read_int(gb_alignment_len);
461
462#if defined(DEBUG) && 1
463    printf("alignment_length_changed from %i to %i\n", MAXSEQUENCECHARACTERLENGTH, new_length);
464#endif
465
466    if (MAXSEQUENCECHARACTERLENGTH!=new_length) { // otherwise we already did this (i.e. we were called by changed_by_database)
467        bool was_increased = new_length>MAXSEQUENCECHARACTERLENGTH;
468
469        MAXSEQUENCECHARACTERLENGTH = new_length;
470
471        {
472            const char *err = ED4_ROOT->helix->init(GLOBAL_gb_main); // reload helix
473            aw_message_if(err);
474        }
475        {
476            const char *err = ED4_ROOT->ecoli_ref->init(GLOBAL_gb_main); // reload ecoli
477            aw_message_if(err);
478        }
479
480        if (ED4_ROOT->alignment_type == GB_AT_AA) {
481            // TODO: is this needed here?
482            const char *err = ED4_pfold_set_SAI(&ED4_ROOT->protstruct, GLOBAL_gb_main, ED4_ROOT->alignment_name, &ED4_ROOT->protstruct_len); // reload protstruct
483            aw_message_if(err);
484        }
485
486        if (was_increased) {
487            ED4_ROOT->main_manager->route_down_hierarchy(change_char_table_length, new_length).expect_no_error();
488            ED4_ROOT->root_group_man->remap()->mark_compile_needed_force();
489        }
490    }
491}
492
493ED4_returncode ED4_root::init_alignment() {
494    GB_transaction ta(GLOBAL_gb_main);
495
496    alignment_name = GBT_get_default_alignment(GLOBAL_gb_main);
497    alignment_type = GBT_get_alignment_type(GLOBAL_gb_main, alignment_name);
498    if (alignment_type==GB_AT_UNKNOWN) {
499        aw_popup_exit("You have to select a valid alignment before you can start ARB_EDIT4");
500    }
501
502    GBDATA *gb_alignment = GBT_get_alignment(GLOBAL_gb_main, alignment_name);
503    if (!gb_alignment) aw_popup_exit("You can't edit without an existing alignment");
504
505    GBDATA *gb_alignment_len = GB_search(gb_alignment, "alignment_len", GB_FIND);
506    int alignment_length = GB_read_int(gb_alignment_len);
507    MAXSEQUENCECHARACTERLENGTH = alignment_length;
508
509    GB_add_callback(gb_alignment_len, GB_CB_CHANGED, makeDatabaseCallback(ED4_alignment_length_changed));
510
511    aw_root->awar_string(AWAR_EDITOR_ALIGNMENT, alignment_name);
512
513    return ED4_R_OK;
514}
515
516void ED4_root::recalc_font_group() {
517    font_group.unregisterAll();
518    for (int f=ED4_G_FIRST_FONT; f<=ED4_G_LAST_FONT; f++) {
519        ED4_MostRecentWinContext context;
520        font_group.registerFont(current_device(), f);
521    }
522}
523
524static ARB_ERROR force_group_update(ED4_base *base) {
525    if (base->is_multi_species_manager()) {
526        base->to_multi_species_manager()->update_requested_by_child();
527    }
528    return NULL;
529}
530
531ED4_returncode ED4_root::create_hierarchy(char *area_string_middle, char *area_string_top) {
532    // creates internal hierarchy of editor
533
534    long total_no_of_groups  = 0;
535    long total_no_of_species = 0;
536    {
537        // count species and related info (e.g. helix) displayed in the top region (a == 0)
538        // count no of species and sais including groups in middle region (a == 1)
539
540        const char *area_string[2] = { area_string_top, area_string_middle };
541        for (int a = 0; a<2; ++a) {
542            long group_count;
543            long species_count;
544            database->calc_no_of_all(area_string[a], &group_count, &species_count);
545
546            total_no_of_groups  += group_count;
547            total_no_of_species += species_count;
548        }
549    }
550
551    SmartPtr<arb_progress> startup_progress = new arb_progress("EDIT4 startup");
552
553    GB_push_transaction(GLOBAL_gb_main);
554
555    ecoli_ref = new BI_ecoli_ref;
556    ecoli_ref->init(GLOBAL_gb_main);
557
558    // [former position of ali-init-code]
559
560    main_manager   = new ED4_main_manager("Main_Manager", 0, 0, 0, 0, NULL); // build a test hierarchy
561    root_group_man = new ED4_root_group_manager("Root_Group_Manager", 0, 0, 0, 0, main_manager);
562   
563    main_manager->children->append_member(root_group_man);
564
565    ED4_device_manager *device_manager = new ED4_device_manager("Device_Manager", 0, 0, 0, 0, root_group_man);
566    root_group_man->children->append_member(device_manager);
567
568    ED4_calc_terminal_extentions();
569
570    {
571        int col_stat_term_height = 50; // @@@ Hoehe des ColumnStatistics Terminals ausrechnen
572
573        ref_terminals.init(new ED4_sequence_info_terminal("Reference_Sequence_Info_Terminal", /* NULL, */ 250, 0, MAXINFOWIDTH, TERMINALHEIGHT, NULL),
574                           new ED4_sequence_terminal("Reference_Sequence_Terminal", 300, 0, 300, TERMINALHEIGHT, NULL, false),
575                           new ED4_sequence_info_terminal("Reference_ColumnStatistics_Info_Terminal", /* NULL, */ 250, 0, MAXINFOWIDTH, col_stat_term_height, NULL),
576                           new ED4_columnStat_terminal("Reference_ColumnStatistics_Terminal", 300, 0, 300, col_stat_term_height, NULL));
577    }
578
579
580    recalc_font_group();
581
582    ED4_index y = 0;
583
584    {
585        arb_progress species_progress("Loading species", total_no_of_species);
586       
587        const int XPOS_MULTIMAN = 100;
588
589        // ********** Top Area beginning **********
590
591        ED4_multi_species_manager *top_multi_species_manager;
592        ED4_spacer_terminal       *top_spacer_terminal;
593        ED4_spacer_terminal       *top_mid_spacer_terminal;
594        ED4_spacer_terminal       *top_multi_spacer_terminal_beg;
595        ED4_line_terminal         *top_mid_line_terminal;
596        {
597            ED4_area_manager *top_area_manager = new ED4_area_manager("Top_Area_Manager", 0, y, 0, 0, device_manager);
598            device_manager->children->append_member(top_area_manager);
599            top_area_man = top_area_manager;
600
601            top_spacer_terminal = new ED4_spacer_terminal("Top_Spacer", true, 0, 0, 100, 10, top_area_manager);
602            top_area_manager->children->append_member(top_spacer_terminal);
603
604            top_multi_species_manager = new ED4_multi_species_manager("Top_MultiSpecies_Manager", XPOS_MULTIMAN, 0, 0, 0, top_area_manager);
605            top_area_manager->children->append_member(top_multi_species_manager);
606
607            top_multi_spacer_terminal_beg = new ED4_spacer_terminal("Top_Multi_Spacer_Terminal_Beg", true, 0, 0, 0, 3, top_multi_species_manager);
608            top_multi_species_manager->children->append_member(top_multi_spacer_terminal_beg);
609
610            y += 3;
611
612            reference = new AWT_reference(GLOBAL_gb_main);
613
614            int index = 0;
615            database->scan_string(top_multi_species_manager, ref_terminals.get_ref_sequence_info(), ref_terminals.get_ref_sequence(),
616                                  area_string_top, &index, &y, species_progress);
617            GB_pop_transaction(GLOBAL_gb_main);
618
619            const int TOP_MID_LINE_HEIGHT   = 3;
620            int       TOP_MID_SPACER_HEIGHT = font_group.get_max_height()-TOP_MID_LINE_HEIGHT;
621
622            top_mid_line_terminal = new ED4_line_terminal("Top_Mid_Line_Terminal", 0, y, 0, TOP_MID_LINE_HEIGHT, device_manager);    // width will be set below
623            device_manager->children->append_member(top_mid_line_terminal);
624
625            y += TOP_MID_LINE_HEIGHT;
626
627
628            top_mid_spacer_terminal = new ED4_spacer_terminal("Top_Middle_Spacer", true, 0, y, 880, TOP_MID_SPACER_HEIGHT,   device_manager);
629            device_manager->children->append_member(top_mid_spacer_terminal);
630
631            // needed to avoid text-clipping problems:
632            main_manager->set_top_middle_spacer_terminal(top_mid_spacer_terminal);
633            main_manager->set_top_middle_line_terminal(top_mid_line_terminal);
634
635            y += TOP_MID_SPACER_HEIGHT; // add top-mid_spacer_terminal height
636        }
637
638        // ********** Middle Area beginning **********
639
640        ED4_area_manager          *middle_area_manager;
641        ED4_tree_terminal         *tree_terminal;
642        ED4_multi_species_manager *mid_multi_species_manager;
643        ED4_spacer_terminal       *mid_multi_spacer_terminal_beg;
644        ED4_line_terminal         *mid_bot_line_terminal;
645        ED4_spacer_terminal       *total_bottom_spacer;
646        {
647            middle_area_manager = new ED4_area_manager("Middle_Area_Manager", 0, y, 0, 0, device_manager);
648            device_manager->children->append_member(middle_area_manager);
649            middle_area_man = middle_area_manager;
650
651            tree_terminal = new ED4_tree_terminal("Tree", 0, 0, 2, 0, middle_area_manager);
652            middle_area_manager->children->append_member(tree_terminal);
653
654            mid_multi_species_manager = new ED4_multi_species_manager("Middle_MultiSpecies_Manager", XPOS_MULTIMAN, 0, 0, 0, middle_area_manager);
655            middle_area_manager->children->append_member(mid_multi_species_manager);
656
657            mid_multi_spacer_terminal_beg = new ED4_spacer_terminal("Mid_Multi_Spacer_Terminal_Beg", true, 0, 0, 0, 3, mid_multi_species_manager);
658            mid_multi_species_manager->children->append_member(mid_multi_spacer_terminal_beg);
659
660            y+=3;               // dummy height, to create a dummy layout ( to preserve order of objects )
661
662            scroll_links.link_for_ver_slider = middle_area_manager;
663
664            ED4_index help = y;
665            {
666                GB_transaction ta(GLOBAL_gb_main);
667                int index  = 0;
668                database->scan_string(mid_multi_species_manager, ref_terminals.get_ref_sequence_info(), ref_terminals.get_ref_sequence(),
669                                      area_string_middle, &index, &y, species_progress);
670            }
671
672            {
673                ED4_spacer_terminal *mid_bot_spacer_terminal = new ED4_spacer_terminal("Middle_Bot_Spacer_Terminal", true, 0, y, 880, 10, device_manager);
674                device_manager->children->append_member(mid_bot_spacer_terminal);
675            }
676
677            tree_terminal->extension.size[HEIGHT] = y - help;
678
679            y += 10; // add top-mid_spacer_terminal height
680
681            mid_bot_line_terminal = new ED4_line_terminal("Mid_Bot_Line_Terminal", 0, y, 0, 3, device_manager);    // width will be set below
682            device_manager->children->append_member(mid_bot_line_terminal);
683            y += 3;
684
685            total_bottom_spacer = new ED4_spacer_terminal("Total_Bottom_Spacer_terminal", true, 0, y, 0, 10000, device_manager);
686            device_manager->children->append_member(total_bottom_spacer);
687            y += 10000;
688        }
689
690        if (scroll_links.link_for_hor_slider) {
691            long ext_width = long(scroll_links.link_for_hor_slider->extension.size[WIDTH]);
692
693            top_multi_spacer_terminal_beg->extension.size[WIDTH] = ext_width + MAXSPECIESWIDTH + SEQUENCEINFOSIZE;
694            mid_multi_spacer_terminal_beg->extension.size[WIDTH] = ext_width + MAXSPECIESWIDTH + SEQUENCEINFOSIZE;
695            total_bottom_spacer->extension.size[WIDTH] = ext_width + MAXSPECIESWIDTH + SEQUENCEINFOSIZE;
696
697            top_mid_line_terminal->extension.size[WIDTH] = ext_width + TREETERMINALSIZE + MAXSPECIESWIDTH + SEQUENCEINFOSIZE;
698            mid_bot_line_terminal->extension.size[WIDTH] = ext_width + TREETERMINALSIZE + MAXSPECIESWIDTH + SEQUENCEINFOSIZE;
699
700        }
701
702        tree_terminal->set_links(NULL, mid_multi_species_manager);                          // set links
703        top_spacer_terminal->set_links(tree_terminal, top_multi_species_manager);
704        top_mid_spacer_terminal->set_links(middle_area_manager, NULL);
705        total_bottom_spacer->set_links(mid_bot_line_terminal, 0);
706
707        species_progress.done();
708    }
709
710    first_window->update_window_coords();
711    resize_all();
712
713    main_manager->route_down_hierarchy(force_group_update).expect_no_error();
714
715    if (!scroll_links.link_for_hor_slider) { // happens when no species AND no SAI has data
716        startup_progress->done();
717        startup_progress.SetNull(); // make progress disappear (otherwise prompter below is often behind progress window)
718        GB_pop_transaction(GLOBAL_gb_main);
719       
720        aw_popup_ok(GBS_global_string("No species/SAI contains data in '%s'\nARB_EDIT4 will terminate", alignment_name));
721        ED4_exit();
722    }
723
724    // build consensi
725    {
726        arb_progress consensi_progress("Initializing consensi", total_no_of_species+total_no_of_groups+1); // 1 is root_group_man
727
728        root_group_man->create_consensus(root_group_man, &consensi_progress);
729        e4_assert(root_group_man->table().ok());
730        consensi_progress.done(); // if there is a "top"-group, progress increment is one to low
731    }
732
733    root_group_man->remap()->mark_compile_needed_force();
734    root_group_man->update_remap();
735
736    // calc size and display:
737
738    resize_all();
739
740    e4_assert(main_manager);
741
742    {
743        ED4_base *x_link      = scroll_links.link_for_hor_slider;
744        ED4_base *y_link      = scroll_links.link_for_ver_slider;
745        ED4_base *width_link  = x_link;
746        ED4_base *height_link = y_link;
747
748        ED4_window *win = first_window;
749        while (win) {
750            win->set_scrolled_rectangle(x_link, y_link, width_link, height_link);
751            win->aww->show();
752            win->update_scrolled_rectangle();
753            win = win->next;
754        }
755    }
756
757    ED4_trigger_instant_refresh();
758    ED4_finish_and_show_notFoundMessage();
759
760    return ED4_R_OK;
761}
762
763ED4_returncode ED4_root::get_area_rectangle(AW_screen_area *rect, AW_pos x, AW_pos y) {
764    // returns win-coordinates of area (defined by folding lines) which contains position x/y
765    int                    x1, x2, y1, y2;
766    const AW_screen_area&  area_rect = current_device()->get_area_size();
767
768    x1 = area_rect.l;
769    for (const ED4_folding_line *flv=current_ed4w()->get_vertical_folding(); ; flv = flv->get_next()) {
770        if (flv) {
771            x2 = int(flv->get_pos()); // @@@ use AW_INT ?
772        }
773        else {
774            x2 = area_rect.r;
775            if (x1==x2) {
776                break;
777            }
778        }
779
780        y1 = area_rect.t;
781        for (const ED4_folding_line *flh=current_ed4w()->get_horizontal_folding(); ; flh = flh->get_next()) {
782            if (flh) {
783                y2 = int(flh->get_pos()); // @@@ use AW_INT ?
784            }
785            else {
786                y2 = area_rect.b;
787                if (y1==y2) {
788                    break;
789                }
790            }
791
792            if (x1<=x && x<=x2 && y1<=y && y<=y2) {
793                rect->t = y1;
794                rect->b = y2;
795                rect->l = x1;
796                rect->r = x2;
797                return ED4_R_OK;
798            }
799            y1 = y2;
800            if (!flh) break;
801        }
802        x1 = x2;
803        if (!flv) break;
804    }
805    return ED4_R_IMPOSSIBLE; // no area contains x/y :-(
806}
807
808void ED4_root::copy_window_struct(ED4_window *source,   ED4_window *destination)
809{
810    destination->slider_pos_horizontal  = source->slider_pos_horizontal;
811    destination->slider_pos_vertical    = source->slider_pos_vertical;
812    destination->coords         = source->coords;
813}
814
815
816static void reload_helix_cb() { 
817    const char *err = ED4_ROOT->helix->init(GLOBAL_gb_main);
818    if (err) aw_message(err);
819    ED4_request_full_refresh();
820}
821
822
823static void reload_ecoli_cb() {
824    const char *err = ED4_ROOT->ecoli_ref->init(GLOBAL_gb_main);
825    if (err) aw_message(err);
826    ED4_request_full_refresh();
827}
828
829// ---------------------------------------
830//      recursion through all species
831
832typedef ARB_ERROR (*ED4_Species_Callback)(GBDATA*, AW_CL);
833
834static ARB_ERROR do_sth_with_species(ED4_base *base, AW_CL cl_spec_cb, AW_CL cd) {
835    ARB_ERROR error = NULL;
836
837    if (base->is_species_manager()) {
838        ED4_Species_Callback       cb                    = (ED4_Species_Callback)cl_spec_cb;
839        ED4_species_manager       *species_manager       = base->to_species_manager();
840        ED4_species_name_terminal *species_name_terminal = species_manager->search_spec_child_rek(ED4_L_SPECIES_NAME)->to_species_name_terminal();
841
842        if (species_name_terminal->get_species_pointer()) {
843            char *species_name = GB_read_as_string(species_name_terminal->get_species_pointer());
844
845            e4_assert(species_name);
846            GBDATA *species = GBT_find_species(GLOBAL_gb_main, species_name);
847
848            error = species
849                ? cb(species, cd)
850                : GB_append_exportedError(GBS_global_string("can't find species '%s'", species_name));
851
852            free(species_name);
853        }
854    }
855
856    return error;
857}
858
859
860static ARB_ERROR ED4_with_all_loaded_species(ED4_Species_Callback cb, AW_CL cd) {
861    return ED4_ROOT->root_group_man->route_down_hierarchy(do_sth_with_species, (AW_CL)cb, cd);
862}
863
864static bool has_species_name(ED4_base *base, AW_CL cl_species_name) {
865    ED4_species_name_terminal *name_term = base->to_species_name_terminal();
866    GBDATA *gbd = name_term->get_species_pointer();
867
868    if (gbd) {
869        const char *name = GB_read_char_pntr(gbd);
870        e4_assert(name);
871        return strcmp(name, (const char*)cl_species_name)==0;
872    }
873
874    return false;
875}
876
877ED4_species_name_terminal *ED4_find_species_name_terminal(const char *species_name)
878{
879    ED4_base *base = ED4_ROOT->root_group_man->find_first_that(ED4_L_SPECIES_NAME, has_species_name, (AW_CL)species_name);
880
881    return base ? base->to_species_name_terminal() : 0;
882}
883
884static char *get_group_consensus(const char *species_name, PosRange range) {
885    ED4_species_name_terminal *name_term = ED4_find_species_name_terminal(species_name);
886    char *consensus = 0;
887
888    if (name_term) {
889        ED4_abstract_group_manager *group_man = name_term->get_parent(ED4_level(ED4_L_GROUP|ED4_L_ROOTGROUP))->to_abstract_group_manager();
890        if (group_man) {
891            consensus = group_man->table().build_consensus_string(range);
892        }
893    }
894
895    return consensus;
896}
897
898static bool get_selected_range(PosRange& range) {
899    ED4_selected_elem *listElem = ED4_ROOT->selected_objects->head();
900    if (listElem) {
901        ED4_sequence_terminal *seqTerm = listElem->elem()->object->corresponding_sequence_terminal();
902        return ED4_get_selected_range(seqTerm, range);
903    }
904    return false;
905}
906
907static ED4_selected_elem *curr_aligner_elem = 0;
908
909static GBDATA *get_next_selected_species() {
910    if (!curr_aligner_elem) return 0;
911
912    ED4_species_manager *specMan = curr_aligner_elem->elem()->object->containing_species_manager();
913    curr_aligner_elem = curr_aligner_elem->next();
914    return specMan->get_species_pointer();
915}
916
917static GBDATA *get_first_selected_species(int *total_no_of_selected_species)
918{
919    int selected = ED4_ROOT->selected_objects->size();
920
921    if (total_no_of_selected_species) {
922        *total_no_of_selected_species = selected;
923    }
924
925    if (selected) {
926        curr_aligner_elem = ED4_ROOT->selected_objects->head();
927    }
928    else {
929        curr_aligner_elem = 0;
930    }
931
932    return get_next_selected_species();
933}
934
935struct AlignDataAccess dataAccess_4_aligner = {
936    1,                                              // default is to do a refresh
937    ED4_trigger_instant_refresh,                    // with this function
938    get_group_consensus,                            // aligner fetches consensus of group of species via this function
939    get_selected_range,                             // aligner fetches column range of selection via this function
940    get_first_selected_species,                     // aligner fetches first and..
941    get_next_selected_species,                      // .. following selected species via this functions
942    0,                                              // AW_helix (needed for island_hopping)
943    NULL,                                           // gb_main
944};
945static ARB_ERROR ED4_delete_temp_entries(GBDATA *species, AW_CL cl_alignment_name) {
946    return FastAligner_delete_temp_entries(species, (GB_CSTR)cl_alignment_name);
947}
948
949static void ED4_remove_faligner_entries(AW_window *, AW_CL, AW_CL) {
950    ARB_ERROR error = GB_begin_transaction(GLOBAL_gb_main);
951    if (!error) error = ED4_with_all_loaded_species(ED4_delete_temp_entries, (AW_CL)ED4_ROOT->alignment_name);
952    GB_end_transaction_show_error(GLOBAL_gb_main, error, aw_message);
953}
954
955#if defined(DEBUG) && 0
956
957void ED4_testSplitNMerge(AW_window *aw, AW_CL, AW_CL)
958{
959    AW_root *root = aw->get_root();
960    char *name;
961    GBDATA *species;
962    GBDATA *gbd;
963    char *data;
964    long length;
965
966    GB_ERROR error = GB_begin_transaction(gb_main);
967    char *ali = ED4_ROOT->alignment_name;
968
969    if (!error)
970    {
971        name = root->awar(AWAR_SPECIES_NAME)->read_string();
972        species = GBT_find_species(gb_main, name);
973        gbd = species ? GBT_read_sequence(species, ali) : NULL;
974        data = gbd ? GB_read_string(gbd) : NULL;
975        length = gbd ? GB_read_string_count(gbd) : NULL;
976
977        if (data)
978        {
979            char *newData = AWTC_testConstructSequence(data);
980
981            if (newData)
982            {
983                error = GB_write_string(gbd, newData);
984                delete [] newData;
985            }
986        }
987        else
988        {
989            error = GB_get_error();
990            if (!error) error = GB_export_error("Can't read data of '%s'", name);
991        }
992
993        delete name;
994        delete data;
995    }
996
997    GB_end_transaction_show_error(gb_main, error, aw_message);
998}
999
1000#endif
1001
1002static void toggle_helix_for_SAI(AW_window *aww, AW_CL, AW_CL) {
1003    ED4_LocalWinContext  uses(aww);
1004    ED4_cursor          *cursor = &current_cursor();
1005
1006    if (cursor->in_SAI_terminal()) {
1007        ED4_sequence_terminal      *sai_term      = cursor->owner_of_cursor->to_sequence_terminal();
1008        ED4_sequence_info_terminal *sai_info_term = sai_term->parent->search_spec_child_rek(ED4_L_SEQUENCE_INFO)->to_sequence_info_terminal();
1009
1010        GBDATA         *gb_sai_data = sai_info_term->data();
1011        GB_transaction  ta(gb_sai_data);
1012
1013        GBDATA *gb_sai      = GB_get_grandfather(gb_sai_data);
1014        GBDATA *gb_disp_sec = GB_searchOrCreate_int(gb_sai, "showsec", 0);
1015
1016        bool show_sec = 1-bool(GB_read_int(gb_disp_sec));
1017        GB_ERROR error = GB_write_int(gb_disp_sec, show_sec);
1018        if (!error) {
1019            sai_term->set_secstruct_display(show_sec);
1020            sai_term->request_refresh();
1021        }
1022        if (error) aw_message(error);
1023    }
1024    else {
1025        aw_message("Please select an SAI");
1026    }
1027}
1028
1029static void title_mode_changed(AW_root *aw_root, AW_window *aww)
1030{
1031    int title_mode = aw_root->awar(AWAR_EDIT_TITLE_MODE)->read_int();
1032
1033    if (title_mode==0) {
1034        aww->set_info_area_height(57);
1035    }
1036    else {
1037        aww->set_info_area_height(170);
1038    }
1039}
1040
1041static void ED4_undo_redo(AW_window *aww, AW_CL undo_type) {
1042    ED4_LocalWinContext uses(aww);
1043    GB_ERROR error = GB_undo(GLOBAL_gb_main, (GB_UNDO_TYPE)undo_type);
1044
1045    if (error) {
1046        aw_message(error);
1047    }
1048    else {
1049        GB_begin_transaction(GLOBAL_gb_main);
1050        GB_commit_transaction(GLOBAL_gb_main);
1051        ED4_cursor *cursor = &current_cursor();
1052        if (cursor->owner_of_cursor) cursor->owner_of_cursor->request_refresh();
1053    }
1054}
1055
1056static void ED4_clear_errors(AW_window *aww, AW_CL) {
1057    aw_clear_message_cb(aww);
1058}
1059
1060static AW_window *ED4_zoom_message_window(AW_root *root, AW_CL)
1061{
1062    AW_window_simple *aws = new AW_window_simple;
1063
1064    aws->init(root, "ZOOM_ERR_MSG", "Errors and warnings");
1065    aws->load_xfig("edit4/message.fig");
1066
1067    aws->callback((AW_CB0)AW_POPDOWN);
1068    aws->at("hide");
1069    aws->create_button("HIDE", "HIDE");
1070
1071    aws->callback(ED4_clear_errors, (AW_CL)0);
1072    aws->at("clear");
1073    aws->create_button("CLEAR", "CLEAR");
1074
1075    aws->at("errortext");
1076    aws->create_text_field(AWAR_ERROR_MESSAGES);
1077
1078    return (AW_window *)aws;
1079}
1080
1081
1082static char *cat(char *toBuf, const char *s1, const char *s2)
1083{
1084    char *buf = toBuf;
1085
1086    while ((*buf++=*s1++)!=0) ;
1087    buf--;
1088    while ((*buf++=*s2++)!=0) ;
1089
1090    return toBuf;
1091}
1092
1093static void insert_search_fields(AW_window_menu_modes *awmm,
1094                                 const char *label_prefix, const char *macro_prefix,
1095                                 const char *pattern_awar_name, const char *show_awar_name,
1096                                 int short_form, ED4_SearchPositionType type, ED4_window *ed4w)
1097{
1098    char buf[200];
1099
1100    if (!short_form) {
1101        awmm->at(cat(buf, label_prefix, "search"));
1102        awmm->create_input_field(pattern_awar_name, 30);
1103    }
1104
1105    awmm->at(cat(buf, label_prefix, "n"));
1106    awmm->callback(ED4_search_cb, ED4_encodeSearchDescriptor(+1, type), (AW_CL)ed4w);
1107    awmm->create_button(cat(buf, macro_prefix, "_SEARCH_NEXT"), "#edit/next.xpm");
1108
1109    awmm->at(cat(buf, label_prefix, "l"));
1110    awmm->callback(ED4_search_cb, ED4_encodeSearchDescriptor(-1, type), (AW_CL)ed4w);
1111    awmm->create_button(cat(buf, macro_prefix, "_SEARCH_LAST"), "#edit/last.xpm");
1112
1113    awmm->at(cat(buf, label_prefix, "d"));
1114    awmm->callback(ED4_popup_search_window, (AW_CL)type);
1115    awmm->create_button(cat(buf, macro_prefix, "_SEARCH_DETAIL"), "#edit/detail.xpm");
1116
1117    awmm->at(cat(buf, label_prefix, "s"));
1118    awmm->create_toggle(show_awar_name);
1119}
1120
1121static void ED4_set_protection(AW_window *aww, AW_CL cd1, AW_CL /* cd2 */) {
1122    ED4_LocalWinContext uses(aww);
1123    ED4_cursor *cursor = &current_cursor();
1124    GB_ERROR    error  = 0;
1125
1126    if (cursor->owner_of_cursor) {
1127        int                         wanted_protection = int(cd1);
1128        ED4_sequence_terminal      *seq_term          = cursor->owner_of_cursor->to_sequence_terminal();
1129        ED4_sequence_info_terminal *seq_info_term     = seq_term->parent->search_spec_child_rek(ED4_L_SEQUENCE_INFO)->to_sequence_info_terminal();
1130        GBDATA                     *gbd               = seq_info_term->data();
1131
1132        GB_push_transaction(gbd);
1133
1134        GB_push_my_security(gbd);
1135        error = GB_write_security_write(gbd, wanted_protection);
1136        GB_pop_my_security(gbd);
1137
1138        error = GB_end_transaction(gbd, error);
1139    }
1140    else {
1141        error = "No species selected";
1142    }
1143
1144    if (error) aw_message(error);
1145}
1146
1147enum MenuSelectType {
1148    ED4_MS_NONE,
1149    ED4_MS_ALL,
1150    ED4_MS_INVERT,
1151    ED4_MS_INVERT_GROUP,
1152    ED4_MS_UNMARK_ALL,
1153    ED4_MS_MARK_SELECTED,
1154    ED4_MS_UNMARK_SELECTED,
1155    ED4_MS_SELECT_MARKED,
1156    ED4_MS_DESELECT_MARKED,
1157    ED4_MS_TOGGLE_BLOCKTYPE
1158};
1159
1160static void ED4_menu_select(AW_window *aww, AW_CL type, AW_CL) {
1161    GB_transaction ta(GLOBAL_gb_main);
1162    MenuSelectType select = MenuSelectType(type);
1163    ED4_multi_species_manager *middle_multi_man = ED4_ROOT->middle_area_man->get_multi_species_manager();
1164
1165    e4_assert(middle_multi_man);
1166
1167    switch (select) {
1168        case ED4_MS_NONE: {
1169            if (ED4_getBlocktype()!=ED4_BT_NOBLOCK) {
1170                ED4_ROOT->deselect_all();
1171                ED4_setBlocktype(ED4_BT_NOBLOCK);
1172            }
1173            break;
1174        }
1175        case ED4_MS_ALL: {
1176            middle_multi_man->select_all(true); // only species
1177            ED4_correctBlocktypeAfterSelection();
1178            break;
1179        }
1180        case ED4_MS_INVERT: {
1181            middle_multi_man->invert_selection_of_all_species();
1182            ED4_correctBlocktypeAfterSelection();
1183            break;
1184        }
1185        case ED4_MS_SELECT_MARKED: {
1186            middle_multi_man->marked_species_select(true);
1187            ED4_correctBlocktypeAfterSelection();
1188            break;
1189        }
1190        case ED4_MS_DESELECT_MARKED: {
1191            middle_multi_man->marked_species_select(false);
1192            ED4_correctBlocktypeAfterSelection();
1193            break;
1194        }
1195        case ED4_MS_UNMARK_ALL: {
1196            GBT_mark_all(GLOBAL_gb_main, 0);
1197            break;
1198        }
1199        case ED4_MS_MARK_SELECTED: {
1200            middle_multi_man->selected_species_mark(true);
1201            ED4_correctBlocktypeAfterSelection();
1202            break;
1203        }
1204        case ED4_MS_UNMARK_SELECTED: {
1205            middle_multi_man->selected_species_mark(false);
1206            ED4_correctBlocktypeAfterSelection();
1207            break;
1208        }
1209        case ED4_MS_INVERT_GROUP: {
1210            ED4_cursor *cursor = &ED4_ROOT->first_window->get_matching_ed4w(aww)->cursor;
1211            int done = 0;
1212
1213            if (cursor->owner_of_cursor) {
1214                ED4_multi_species_manager *multi_man = cursor->owner_of_cursor->get_parent(ED4_L_MULTI_SPECIES)->to_multi_species_manager();
1215
1216                multi_man->invert_selection_of_all_species();
1217                ED4_correctBlocktypeAfterSelection();
1218                done = 1;
1219            }
1220
1221            if (!done) {
1222                aw_message("Place cursor into group");
1223            }
1224            break;
1225        }
1226        case ED4_MS_TOGGLE_BLOCKTYPE: {
1227            ED4_toggle_block_type();
1228            break;
1229        }
1230        default: {
1231            e4_assert(0);
1232            break;
1233        }
1234    }
1235
1236    ED4_request_full_refresh();
1237}
1238
1239static void ED4_menu_perform_block_operation(AW_window */*aww*/, AW_CL type, AW_CL) {
1240    ED4_perform_block_operation(ED4_blockoperation_type(type));
1241}
1242
1243static void modes_cb(AW_window*, ED4_species_mode smode) {
1244    ED4_ROOT->species_mode = smode;
1245    for (ED4_window *win = ED4_ROOT->first_window; win; win = win->next) {
1246        win->aww->select_mode(smode);
1247    }
1248}
1249
1250void ED4_no_dangerous_modes() {
1251    if (ED4_ROOT->species_mode == ED4_SM_KILL) {
1252        modes_cb(NULL, ED4_SM_MOVE);
1253    }
1254}
1255
1256void ED4_init_aligner_data_access(AlignDataAccess *data_access) {
1257    GB_ERROR  error          = GB_push_transaction(GLOBAL_gb_main);
1258    char     *alignment_name = GBT_get_default_alignment(GLOBAL_gb_main);
1259    long      alilen         = GBT_get_alignment_len(GLOBAL_gb_main, alignment_name);
1260
1261    data_access->gb_main = GLOBAL_gb_main;
1262
1263    if (alilen<=0) error = GB_await_error();
1264    else {
1265        char   *helix_string = 0;
1266        char   *helix_name   = GBT_get_default_helix(GLOBAL_gb_main);
1267        GBDATA *gb_helix_con = GBT_find_SAI(GLOBAL_gb_main, helix_name);
1268        if (gb_helix_con) {
1269            GBDATA *gb_helix = GBT_read_sequence(gb_helix_con, alignment_name);
1270            if (gb_helix) helix_string = GB_read_string(gb_helix);
1271        }
1272        free(helix_name);
1273        freeset(data_access->helix_string, helix_string);
1274    }
1275
1276    free(alignment_name);
1277    error = GB_end_transaction(GLOBAL_gb_main, error);
1278    if (error) aw_message(error);
1279}
1280
1281static AW_window *ED4_create_faligner_window(AW_root *awr, AW_CL cl_AlignDataAccess) {
1282    AlignDataAccess *data_access = (AlignDataAccess*)cl_AlignDataAccess;
1283
1284#if defined(DEBUG)
1285    static AlignDataAccess *last_data_access = NULL;
1286
1287    e4_assert(!last_data_access || (last_data_access == data_access)); // there shall be only one AlignDataAccess
1288    last_data_access = data_access;
1289#endif
1290
1291    static AW_window *aws = NULL;
1292    if (!aws) {
1293        ED4_init_aligner_data_access(data_access);
1294        aws = FastAligner_create_window(awr, data_access);
1295    }
1296    return aws;
1297}
1298
1299static void ED4_save_properties(AW_window *aw, AW_CL cl_mode, AW_CL) {
1300    int mode = (int)cl_mode;
1301
1302    AW_save_specific_properties(aw, ED4_propertyName(mode));
1303}
1304
1305static AW_window *ED4_create_gc_window(AW_root *aw_root, AW_gc_manager id) {
1306    static AW_window *gc_win = 0;
1307    if (!gc_win) {
1308        gc_win = AW_create_gc_window(aw_root, id);
1309    }
1310    return gc_win;
1311}
1312
1313static void refresh_on_gc_change_cb() {
1314    ED4_expose_recalculations();
1315    ED4_request_full_instant_refresh();
1316}
1317
1318ED4_returncode ED4_root::generate_window(AW_device **device, ED4_window **new_window) {
1319    {
1320        ED4_window *ed4w = first_window;
1321
1322        while (ed4w) { // before creating a window look for a hidden window
1323            if (ed4w->is_hidden) {
1324                ed4w->aww->show();
1325                ed4w->is_hidden = false;
1326                return ED4_R_BREAK;
1327            }
1328            ed4w = ed4w->next;
1329        }
1330    }
1331
1332    if (ED4_window::no_of_windows == MAXWINDOWS)                            // no more than 5 windows allowed
1333    {
1334        aw_message(GBS_global_string("Restricted to %i windows", MAXWINDOWS));
1335        return ED4_R_BREAK;
1336    }
1337
1338    AW_window_menu_modes *awmm = new AW_window_menu_modes;
1339    {
1340        int   winNum   = ED4_window::no_of_windows+1;
1341        char *winName  = winNum>1 ? GBS_global_string_copy("ARB_EDIT4_%i", winNum) : strdup("ARB_EDIT4");
1342        char *winTitle = GBS_global_string_copy("ARB_EDIT4 *%d* [%s]", winNum, alignment_name);
1343
1344        awmm->init(aw_root, winName, winTitle, 800, 450);
1345
1346        free(winTitle);
1347        free(winName);
1348    }
1349
1350    *device     = awmm->get_device(AW_MIDDLE_AREA); // points to Middle Area device
1351    *new_window = ED4_window::insert_window(awmm);   // append to window list
1352
1353    e4_assert(ED4_window::no_of_windows >= 1);
1354    bool clone = ED4_window::no_of_windows>1;
1355    if (!clone) {                                   // this is the first window
1356        AW_init_color_group_defaults("arb_edit4");
1357    }
1358    else { // additional edit windows
1359        copy_window_struct(first_window, *new_window);
1360    }
1361
1362    ED4_LocalWinContext uses(*new_window);
1363
1364    // each window has its own gc-manager
1365    gc_manager = AW_manage_GC(awmm,
1366                              "ARB_EDIT4",                   // but all gc-managers use the same colors
1367                              *device,
1368                              ED4_G_STANDARD,                // GC_Standard configuration
1369                              ED4_G_DRAG,
1370                              AW_GCM_DATA_AREA,
1371                              makeWindowCallback(refresh_on_gc_change_cb), // callback triggering refresh on gc-change
1372                              true,                          // use color groups
1373
1374                              "#f8f8f8",
1375                              "STANDARD$black",              // Standard Color showing sequences
1376                              "#SEQUENCES (0)$#505050",      // default color for sequences (color 0)
1377                              "+-HELIX (1)$#8E0000",  "+-COLOR 2$#0000dd",    "-COLOR 3$#00AA55",
1378                              "+-COLOR 4$#80f",       "+-COLOR 5$#c0a020",    "-COLOR 6$grey",
1379                              "+-COLOR 7$#ff0000",    "+-COLOR 8$#44aaff",    "-COLOR 9$#ffaa00",
1380
1381                              "+-RANGE 0$#FFFFFF",    "+-RANGE 1$#F0F0F0",    "-RANGE 2$#E0E0E0",
1382                              "+-RANGE 3$#D8D8D8",    "+-RANGE 4$#D0D0D0",    "-RANGE 5$#C8C8C8",
1383                              "+-RANGE 6$#C0C0C0",    "+-RANGE 7$#B8B8B8",    "-RANGE 8$#B0B0B0",
1384                              "-RANGE 9$#A0A0A0",
1385
1386                              // colors used to Paint search patterns
1387                              // (do not change the names of these gcs)
1388                              "+-User1$#B8E2F8",      "+-User2$#B8E2F8",      "-Probe$#B8E2F8", // see also SEC_graphic::init_devices
1389                              "+-Primer(l)$#A9FE54",  "+-Primer(r)$#A9FE54",  "-Primer(g)$#A9FE54",
1390                              "+-Sig(l)$#DBB0FF",     "+-Sig(r)$#DBB0FF",     "-Sig(g)$#DBB0FF",
1391
1392                              "+-MISMATCHES$#FF9AFF", "-CURSOR$#FF0080",
1393                              "+-MARKED$#f4f8e0",     "-SELECTED$#FFFF80",
1394
1395                              NULL);
1396
1397    // since the gc-managers of all EDIT4-windows point to the same window properties,
1398    // changing fonts and colors is always done on first gc-manager
1399    static AW_gc_manager first_gc_manager = 0;
1400    if (!first_gc_manager) first_gc_manager = gc_manager;
1401
1402    // --------------
1403    //      File
1404
1405#if defined(DEBUG)
1406    AWT_create_debug_menu(awmm);
1407#endif // DEBUG
1408
1409    awmm->create_menu("File", "F", AWM_ALL);
1410
1411    awmm->insert_menu_topic("new_win",        "New Editor Window",     "W", 0, AWM_ALL, ED4_new_editor_window);
1412    awmm->insert_menu_topic("save_config",    "Save Configuration",    "S", 0, AWM_ALL, (AW_CB)ED4_save_configuration, (AW_CL) 0,                                     (int)0);
1413    awmm->insert_menu_topic("save_config_as", "Save Configuration As", "A", 0, AWM_ALL, AW_POPUP,                      (AW_CL) ED4_save_configuration_as_open_window, (int)0);
1414    awmm->sep______________();
1415    awmm->insert_menu_topic("load_config",   "Load Configuration",   "L", 0, AWM_ALL, AW_POPUP,           (AW_CL)ED4_start_editor_on_old_configuration, 0);
1416    awmm->insert_menu_topic("reload_config", "Reload Configuration", "R", 0, AWM_ALL, ED4_restart_editor, 0,                                            0);
1417    insert_macro_menu_entry(awmm, true);
1418    awmm->sep______________();
1419    GDE_load_menu(awmm, AWM_ALL, "Print");
1420    awmm->sep______________();
1421
1422    if (clone) awmm->insert_menu_topic("close", "CLOSE", "C", 0, AWM_ALL, ED4_quit_editor);
1423    else       awmm->insert_menu_topic("quit",   "QUIT",  "Q", 0, AWM_ALL, ED4_quit_editor);
1424
1425    // ----------------
1426    //      Create
1427
1428    awmm->create_menu("Create", "C", AWM_ALL);
1429    awmm->insert_menu_topic("create_species",                "Create new species",                "n", 0, AWM_ALL, AW_POPUP, (AW_CL)ED4_create_new_seq_window, (int)0);
1430    awmm->insert_menu_topic("create_species_from_consensus", "Create new species from consensus", "u", 0, AWM_ALL, AW_POPUP, (AW_CL)ED4_create_new_seq_window, (int)1);
1431    awmm->insert_menu_topic("copy_species",                  "Copy current species",              "C", 0, AWM_ALL, AW_POPUP, (AW_CL)ED4_create_new_seq_window, (int)2);
1432    awmm->sep______________();
1433    awmm->insert_menu_topic("create_group",           "Create new Group",              "G", 0, AWM_ALL, group_species_cb, 0, 0);
1434    awmm->insert_menu_topic("create_groups_by_field", "Create new groups using Field", "F", 0, AWM_ALL, group_species_cb, 1, 0);
1435
1436    // --------------
1437    //      Edit
1438
1439    awmm->create_menu("Edit", "E", AWM_ALL);
1440    awmm->insert_menu_topic("refresh",      "Refresh [Ctrl-L]",           "f", 0, AWM_ALL, (AW_CB)ED4_request_full_refresh,       0, 0);
1441    awmm->insert_menu_topic("load_current", "Load current species [GET]", "G", 0, AWM_ALL, ED4_get_and_jump_to_current_from_menu, 0, 0);
1442    awmm->insert_menu_topic("load_marked",  "Load marked species",        "m", 0, AWM_ALL, ED4_get_marked_from_menu,              0, 0);
1443    awmm->sep______________();
1444    awmm->insert_menu_topic("refresh_ecoli",       "Reload Ecoli sequence",        "E", "ecoliref.hlp", AWM_ALL, (AW_CB)reload_ecoli_cb, 0, 0);
1445    awmm->insert_menu_topic("refresh_helix",       "Reload Helix",                 "H", "helix.hlp",    AWM_ALL, (AW_CB)reload_helix_cb, 0, 0);
1446    awmm->insert_menu_topic("helix_jump_opposite", "Jump helix opposite [Ctrl-J]", "J", 0,              AWM_ALL, ED4_helix_jump_opposite);
1447    awmm->sep______________();
1448
1449    awmm->insert_sub_menu("Set protection of current ", "p");
1450    {
1451        char macro[] = "to_0",
1452            topic[] = ".. to 0",
1453            hotkey[] = "0";
1454
1455        for (char i='0'; i<='6'; i++) {
1456            macro[3] = topic[6] = hotkey[0] = i;
1457            awmm->insert_menu_topic(macro, topic, hotkey, "security.hlp", AWM_ALL, ED4_set_protection, AW_CL(i-'0'), 0);
1458        }
1459    }
1460    awmm->close_sub_menu();
1461
1462#if !defined(NDEBUG) && 0
1463    awmm->insert_menu_topic(0,               "Test (test split & merge)", "T", 0, AWM_ALL, ED4_testSplitNMerge, 1, 0);
1464#endif
1465   
1466    awmm->sep______________();
1467    awmm->insert_menu_topic("fast_aligner",       INTEGRATED_ALIGNERS_TITLE,             "I", "faligner.hlp",     AWM_ALL,            AW_POPUP,                               (AW_CL)ED4_create_faligner_window, (AW_CL)&dataAccess_4_aligner);
1468    awmm->insert_menu_topic("fast_align_set_ref", "Set aligner reference [Ctrl-R]",      "R", "faligner.hlp",     AWM_ALL,            (AW_CB)FastAligner_set_reference_species, (AW_CL)aw_root,                    0);
1469    awmm->insert_menu_topic("align_sequence",     "Old aligner from ARB_EDIT [broken]",  "O", "ne_align_seq.hlp", AWM_DISABLED,       AW_POPUP,                               (AW_CL)create_naligner_window,     0);
1470    awmm->insert_menu_topic("sina",               "SINA (SILVA Incremental Aligner)",    "S", "sina_main.hlp",    sina_mask(aw_root), show_sina_window,                       (AW_CL)&dataAccess_4_aligner,      0);
1471    awmm->insert_menu_topic("del_ali_tmp",        "Remove all aligner Entries",          "v", 0,                  AWM_ALL,            ED4_remove_faligner_entries,            1,                                 0);
1472    awmm->sep______________();
1473    awmm->insert_menu_topic("missing_bases", "Dot potentially missing bases", "D", "missbase.hlp", AWM_EXP, ED4_popup_dot_missing_bases_window, 0, 0);
1474
1475    if (alignment_type == GB_AT_RNA) { // if the database contains valid alignment of rRNA sequences
1476        awmm->sep______________();
1477        awmm->insert_menu_topic("sec_edit", "Edit secondary structure", "c", "arb_secedit.hlp", AWM_ALL, ED4_start_plugin, (AW_CL)GLOBAL_gb_main, (AW_CL)"SECEDIT");
1478#if defined(ARB_OPENGL)
1479        awmm->insert_menu_topic("rna3d", "View 3D molecule", "3", "rna3d_general.hlp", AWM_ALL, ED4_start_plugin, (AW_CL)GLOBAL_gb_main, (AW_CL)"RNA3D");
1480#endif // ARB_OPENGL
1481#if defined(DEBUG)
1482        awmm->insert_menu_topic("noplugin", "DEBUG: call unknown plugin", "u", 0, AWM_ALL, ED4_start_plugin, (AW_CL)GLOBAL_gb_main, (AW_CL)"testplugin");
1483#endif // DEBUG
1484    }
1485
1486    // --------------
1487    //      View
1488
1489    awmm->create_menu("View", "V", AWM_ALL);
1490    awmm->insert_sub_menu("Search", "S");
1491    {
1492        int         s;
1493        const char *hotkeys  = "12Poiclrg";
1494        char        hotkey[] = "_";
1495
1496        e4_assert(strlen(hotkeys) == SEARCH_PATTERNS);
1497
1498        for (s=0; s<SEARCH_PATTERNS; s++) {
1499            ED4_SearchPositionType type = ED4_SearchPositionType(s);
1500            const char *id = ED4_SearchPositionTypeId[type];
1501            int len = strlen(id);
1502            char *macro_name = GB_give_buffer(2*(len+8));
1503            char *menu_entry_name = macro_name+(len+8);
1504
1505#ifndef NDEBUG
1506            memset(macro_name, 0, 2*(len+8)); // to avoid memchk-warning
1507#endif
1508            sprintf(macro_name, "%s_SEARCH", id);
1509            char *p = macro_name;
1510            while (1) {
1511                char c = *p++;
1512                if (!c) break;
1513                p[-1] = toupper(c);
1514            }
1515            sprintf(menu_entry_name, "%s Search", id);
1516
1517            hotkey[0] = hotkeys[s];
1518            awmm->insert_menu_topic(awmm->local_id(macro_name), menu_entry_name, hotkey, "e4_search.hlp", AWM_ALL, ED4_popup_search_window, AW_CL(type));
1519        }
1520    }
1521    awmm->close_sub_menu();
1522    awmm->sep______________();
1523    awmm->insert_sub_menu("Cursor position ", "p");
1524    awmm->insert_menu_topic("store_curpos",   "Store cursor position",    "S", 0, AWM_ALL, ED4_store_curpos);
1525    awmm->insert_menu_topic("restore_curpos", "Restore cursor position ", "R", 0, AWM_ALL, ED4_restore_curpos);
1526    awmm->insert_menu_topic("clear_curpos",   "Clear stored positions",   "C", 0, AWM_ALL, makeWindowCallback(ED4_clear_stored_curpos));
1527    awmm->close_sub_menu();
1528
1529    awmm->sep______________();
1530    awmm->insert_menu_topic("change_cursor", "Change cursor type",                "t", 0,                   AWM_ALL, ED4_change_cursor);
1531    awmm->insert_menu_topic("show_all",      "Show all bases ",                   "a", "set_reference.hlp", AWM_ALL, ED4_set_reference_species, 1, 0);
1532    awmm->insert_menu_topic("show_diff",     "Show only differences to selected", "d", "set_reference.hlp", AWM_ALL, ED4_set_reference_species, 0, 0);
1533    awmm->sep______________();
1534    awmm->insert_menu_topic("enable_col_stat",  "Activate column statistics", "v", "st_ml.hlp", AWM_EXP, ED4_activate_col_stat,            0, 0);
1535    awmm->insert_menu_topic("disable_col_stat", "Disable column statistics",  "i", "st_ml.hlp", AWM_EXP, ED4_disable_col_stat,             0, 0);
1536    awmm->insert_menu_topic("detail_col_stat",  "Toggle detailed Col.-Stat.", "C", "st_ml.hlp", AWM_EXP, ED4_toggle_detailed_column_stats, 0, 0);
1537    awmm->insert_menu_topic("dcs_threshold",    "Set threshold for D.c.s.",   "f", "st_ml.hlp", AWM_EXP, ED4_set_col_stat_threshold,       0, 0);
1538    awmm->sep______________();
1539    awmm->insert_menu_topic("visualize_SAI", "Visualize SAIs",                "z", "visualizeSAI.hlp",   AWM_ALL, AW_POPUP,             (AW_CL)ED4_createVisualizeSAI_window, 0);
1540    awmm->insert_menu_topic("toggle_saisec", "Toggle secondary info for SAI", "o", "toggle_secinfo.hlp", AWM_ALL, toggle_helix_for_SAI, 0,                                    0);
1541
1542    // Enable ProteinViewer only for DNA sequence type
1543    if (alignment_type == GB_AT_DNA) {
1544        awmm->insert_menu_topic("Protein_Viewer", "Protein Viewer", "w", "proteinViewer.hlp", AWM_ALL, AW_POPUP, (AW_CL)ED4_CreateProteinViewer_window, 0);
1545    }
1546
1547    // ---------------
1548    //      Block
1549
1550    awmm->create_menu("Block", "B", AWM_ALL);
1551
1552    awmm->insert_menu_topic("select_marked",   "Select marked species",   "e", "e4_block.hlp", AWM_ALL, ED4_menu_select, AW_CL(ED4_MS_SELECT_MARKED),   0);
1553    awmm->insert_menu_topic("deselect_marked", "Deselect marked species", "k", "e4_block.hlp", AWM_ALL, ED4_menu_select, AW_CL(ED4_MS_DESELECT_MARKED), 0);
1554    awmm->insert_menu_topic("select_all",      "Select all species",      "S", "e4_block.hlp", AWM_ALL, ED4_menu_select, AW_CL(ED4_MS_ALL),             0);
1555    awmm->insert_menu_topic("deselect_all",    "Deselect all",            "D", "e4_block.hlp", AWM_ALL, ED4_menu_select, AW_CL(ED4_MS_NONE),            0);
1556    awmm->sep______________();
1557    awmm->insert_menu_topic("mark_selected",   "Mark selected species",   "M", "e4_block.hlp", AWM_ALL, ED4_menu_select, AW_CL(ED4_MS_MARK_SELECTED),   0);
1558    awmm->insert_menu_topic("unmark_selected", "Unmark selected species", "n", "e4_block.hlp", AWM_ALL, ED4_menu_select, AW_CL(ED4_MS_UNMARK_SELECTED), 0);
1559    awmm->insert_menu_topic("unmark_all",      "Unmark all species",      "U", "e4_block.hlp", AWM_ALL, ED4_menu_select, AW_CL(ED4_MS_UNMARK_ALL),      0);
1560    awmm->sep______________();
1561    awmm->insert_menu_topic("invert_all",   "Invert selected species", "I", "e4_block.hlp", AWM_ALL, ED4_menu_select, AW_CL(ED4_MS_INVERT),       0);
1562    awmm->insert_menu_topic("invert_group", "Invert group",            "g", "e4_block.hlp", AWM_ALL, ED4_menu_select, AW_CL(ED4_MS_INVERT_GROUP), 0);
1563    awmm->sep______________();
1564    awmm->insert_menu_topic("lowcase", "Change to lower case ", "w", "e4_block.hlp", AWM_ALL, ED4_menu_perform_block_operation, AW_CL(ED4_BO_LOWER_CASE), 0);
1565    awmm->insert_menu_topic("upcase",  "Change to upper case",  "p", "e4_block.hlp", AWM_ALL, ED4_menu_perform_block_operation, AW_CL(ED4_BO_UPPER_CASE), 0);
1566    awmm->sep______________();
1567    awmm->insert_menu_topic("reverse",            "Reverse selection ",    "v", "e4_block.hlp", AWM_ALL, ED4_menu_perform_block_operation, AW_CL(ED4_BO_REVERSE),            0);
1568    awmm->insert_menu_topic("complement",         "Complement selection ", "o", "e4_block.hlp", AWM_ALL, ED4_menu_perform_block_operation, AW_CL(ED4_BO_COMPLEMENT),         0);
1569    awmm->insert_menu_topic("reverse_complement", "Reverse complement",    "t", "e4_block.hlp", AWM_ALL, ED4_menu_perform_block_operation, AW_CL(ED4_BO_REVERSE_COMPLEMENT), 0);
1570    awmm->sep______________();
1571    awmm->insert_menu_topic("unalignBlockLeft",   "Unalign block left",   "a", "e4_block.hlp", AWM_ALL, ED4_menu_perform_block_operation, AW_CL(ED4_BO_UNALIGN_LEFT),   0);
1572    awmm->insert_menu_topic("unalignBlockCenter", "Unalign block center", "c", "e4_block.hlp", AWM_ALL, ED4_menu_perform_block_operation, AW_CL(ED4_BO_UNALIGN_CENTER), 0);
1573    awmm->insert_menu_topic("unalignBlockRight",  "Unalign block right",  "b", "e4_block.hlp", AWM_ALL, ED4_menu_perform_block_operation, AW_CL(ED4_BO_UNALIGN_RIGHT),  0);
1574    awmm->sep______________();
1575    awmm->insert_menu_topic("replace", "Search & Replace ", "h", "e4_replace.hlp", AWM_ALL, AW_POPUP, AW_CL(ED4_create_replace_window), 0);
1576    awmm->insert_menu_topic("setsai",  "Modify SAI ",       "y", "e4_modsai.hlp",  AWM_ALL, AW_POPUP, AW_CL(ED4_create_modsai_window),  0);
1577    awmm->sep______________();
1578    awmm->insert_menu_topic("toggle_block_type", "Exchange: Line<->Column", "x", "e4_block.hlp", AWM_ALL, ED4_menu_select,                  AW_CL(ED4_MS_TOGGLE_BLOCKTYPE), 0);
1579    awmm->insert_menu_topic("shift_left",        "Shift block left ",       "l", "e4_block.hlp", AWM_ALL, ED4_menu_perform_block_operation, AW_CL(ED4_BO_SHIFT_LEFT),       0);
1580    awmm->insert_menu_topic("shift_right",       "Shift block right",       "r", "e4_block.hlp", AWM_ALL, ED4_menu_perform_block_operation, AW_CL(ED4_BO_SHIFT_RIGHT),      0);
1581
1582    // --------------------
1583    //      Properties
1584
1585    awmm->create_menu("Properties", "P", AWM_ALL);
1586
1587    awmm->insert_menu_topic("props_frame",     "Frame Settings ",       "F", 0,                  AWM_ALL, AW_preset_window);
1588    awmm->insert_menu_topic("props_options",   "Editor Options ",       "O", "e4_options.hlp",   AWM_ALL, ED4_create_level_1_options_window);
1589    awmm->insert_menu_topic("props_consensus", "Consensus Definition ", "u", "e4_consensus.hlp", AWM_ALL, ED4_create_consensus_definition_window);
1590    awmm->sep______________();
1591
1592    awmm->insert_menu_topic("props_data",       "Change Colors & Fonts ", "C", 0,                     AWM_ALL, AW_POPUP, (AW_CL)ED4_create_gc_window,     (AW_CL)first_gc_manager);
1593    awmm->insert_menu_topic("props_seq_colors", "Sequence color mapping", "S", "sequence_colors.hlp", AWM_ALL, AW_POPUP, (AW_CL)create_seq_colors_window, (AW_CL)sequence_colors);
1594
1595    awmm->sep______________();
1596
1597    if (alignment_type == GB_AT_AA) awmm->insert_menu_topic("props_pfold",     "Protein Match Settings ", "P", "pfold_props.hlp", AWM_ALL, AW_POPUP, (AW_CL)ED4_pfold_create_props_window, (AW_CL)ED4_request_relayout);
1598    else                            awmm->insert_menu_topic("props_helix_sym", "Helix Settings ",         "H", "helixsym.hlp",    AWM_ALL, AW_POPUP, (AW_CL)create_helix_props_window,     (AW_CL)ED4_request_relayout);
1599
1600    awmm->insert_menu_topic("props_key_map", "Key Mappings ",              "K", "nekey_map.hlp", AWM_ALL, AW_POPUP, (AW_CL)create_key_map_window, 0);
1601    awmm->insert_menu_topic("props_nds",     "Select visible info (NDS) ", "D", "ed4_nds.hlp",   AWM_ALL, AW_POPUP, (AW_CL)ED4_create_nds_window, 0);
1602    awmm->sep______________();
1603    AW_insert_common_property_menu_entries(awmm);
1604    awmm->sep______________();
1605    awmm->insert_sub_menu("Save properties ...", "a");
1606    {
1607        static const char * const tag[] = { "save_alispecific_props", "save_alitype_props", "save_props" };
1608        static const char * const entry_type[] = { "alignment specific ", "ali-type specific ", "" };
1609
1610        // check what is the default mode
1611        int default_mode = -1;
1612        for (int mode = 0; mode <= 2; ++mode) {
1613            if (strcmp(GB_path_in_arbprop(ED4_propertyName(mode)), db_name) == 0) {
1614                default_mode = mode;
1615                break;
1616            }
1617        }
1618        if (default_mode == -1) default_mode = 2; // no properties yet -> use 'edit4.arb'
1619
1620        const char *entry = GBS_global_string("Save loaded Properties (%s)", ED4_propertyName(default_mode));
1621        awmm->insert_menu_topic("save_loaded_props", entry, "l", "e4_defaults.hlp", AWM_ALL, ED4_save_properties, (AW_CL)default_mode, 0);
1622        awmm->sep______________();
1623
1624        for (int mode = 2; mode >= 0; --mode) {
1625            char hotkey[] = "x";
1626            hotkey[0]     = "Pta"[mode];
1627            entry         = GBS_global_string("Save %sProperties (%s)", entry_type[mode], ED4_propertyName(mode));
1628            awmm->insert_menu_topic(tag[mode], entry, hotkey, "e4_defaults.hlp", AWM_ALL, ED4_save_properties, (AW_CL)mode, 0);
1629        }
1630    }
1631    awmm->close_sub_menu();
1632
1633    awmm->insert_help_topic("ARB_EDIT4 help",     "E", "e4.hlp", AWM_ALL, makeHelpCallback("e4.hlp"));
1634
1635    // ----------------------------------------------------------------------------------------------------
1636
1637    aw_root->awar_int(AWAR_EDIT_TITLE_MODE)->add_callback((AW_RCB1)title_mode_changed, (AW_CL)awmm);
1638    awmm->set_bottom_area_height(0);   // No bottom area
1639
1640    awmm->auto_space(5, -2);
1641    awmm->shadow_width(3);
1642
1643    int db_pathx, db_pathy;
1644    awmm->get_at_position(&db_pathx, &db_pathy);
1645
1646    awmm->shadow_width(1);
1647    awmm->load_xfig("edit4/editmenu.fig", false);
1648
1649    // --------------------------
1650    //      help /quit /fold
1651
1652    awmm->button_length(0);
1653
1654    awmm->at("quit");
1655    awmm->callback(ED4_quit_editor);
1656    awmm->help_text("quit.hlp");
1657
1658    if (clone) awmm->create_button("CLOSE", "#close.xpm");
1659    else       awmm->create_button("QUIT", "#quit.xpm");
1660
1661    awmm->at("help");
1662    awmm->callback(AW_help_entry_pressed);
1663    awmm->help_text("e4.hlp");
1664    awmm->create_button("HELP", "#help.xpm");
1665
1666    awmm->at("fold");
1667    awmm->help_text("e4.hlp");
1668    awmm->create_toggle(AWAR_EDIT_TITLE_MODE, "#more.xpm", "#less.xpm");
1669
1670    // -------------------
1671    //      positions
1672
1673    awmm->button_length(0);
1674
1675    awmm->at("posTxt");     awmm->create_button(0, "Position");
1676
1677    awmm->button_length(6+1);
1678
1679    awmm->at("ecoliTxt");   awmm->create_button(0, ED4_AWAR_NDS_ECOLI_NAME, 0, "+");
1680
1681    awmm->button_length(0);
1682
1683    awmm->at("baseTxt");    awmm->create_button(0, "Base");
1684    awmm->at("iupacTxt");   awmm->create_button(0, "IUPAC");
1685    awmm->at("helixnrTxt"); awmm->create_button(0, "Helix#");
1686
1687    awmm->at("pos");
1688    awmm->callback((AW_CB)ED4_jump_to_cursor_position, (AW_CL) current_ed4w()->awar_path_for_cursor, AW_CL(ED4_POS_SEQUENCE));
1689    awmm->create_input_field(current_ed4w()->awar_path_for_cursor, 7);
1690
1691    awmm->at("ecoli");
1692    awmm->callback((AW_CB)ED4_jump_to_cursor_position, (AW_CL) current_ed4w()->awar_path_for_Ecoli, AW_CL(ED4_POS_ECOLI));
1693    awmm->create_input_field(current_ed4w()->awar_path_for_Ecoli, 6);
1694
1695    awmm->at("base");
1696    awmm->callback((AW_CB)ED4_jump_to_cursor_position, (AW_CL) current_ed4w()->awar_path_for_basePos, AW_CL(ED4_POS_BASE));
1697    awmm->create_input_field(current_ed4w()->awar_path_for_basePos, 6);
1698
1699    awmm->at("iupac");
1700    awmm->callback((AW_CB)ED4_set_iupac, (AW_CL) current_ed4w()->awar_path_for_IUPAC, AW_CL(0));
1701    awmm->create_input_field(current_ed4w()->awar_path_for_IUPAC, 4);
1702
1703    awmm->at("helixnr");
1704    awmm->callback((AW_CB)ED4_set_helixnr, (AW_CL) current_ed4w()->awar_path_for_helixNr, AW_CL(0));
1705    awmm->create_input_field(current_ed4w()->awar_path_for_helixNr, 5);
1706
1707    // ----------------------------
1708    //      jump/get/undo/redo
1709
1710    awmm->button_length(4);
1711
1712    awmm->at("jump");
1713    awmm->callback(ED4_jump_to_current_species, 0);
1714    awmm->help_text("e4.hlp");
1715    awmm->create_button("JUMP", "Jump");
1716
1717    awmm->at("get");
1718    awmm->callback(ED4_get_and_jump_to_current, 0);
1719    awmm->help_text("e4.hlp");
1720    awmm->create_button("GET", "Get");
1721
1722    awmm->button_length(0);
1723
1724    awmm->at("undo");
1725    awmm->callback(ED4_undo_redo, GB_UNDO_UNDO);
1726    awmm->help_text("undo.hlp");
1727    awmm->create_button("UNDO", "#undo.xpm");
1728
1729    awmm->at("redo");
1730    awmm->callback(ED4_undo_redo, GB_UNDO_REDO);
1731    awmm->help_text("undo.hlp");
1732    awmm->create_button("REDO", "#redo.xpm");
1733
1734    // --------------------------
1735    //      aligner / SAIviz
1736
1737    awmm->button_length(7);
1738
1739    awmm->at("aligner");
1740    awmm->callback(AW_POPUP, (AW_CL)ED4_create_faligner_window, (AW_CL)&dataAccess_4_aligner);
1741    awmm->help_text("faligner.hlp");
1742    awmm->create_button("ALIGNER", "Aligner");
1743
1744    awmm->at("saiviz");
1745    awmm->callback(AW_POPUP, (AW_CL)ED4_createVisualizeSAI_window, 0);
1746    awmm->help_text("visualizeSAI.hlp");
1747    awmm->create_button("SAIVIZ", "SAIviz");
1748
1749    // -------------------------------------------
1750    //      align/insert/protection/direction
1751
1752    awmm->button_length(0);
1753
1754    awmm->at("protect");
1755    awmm->create_option_menu(AWAR_EDIT_SECURITY_LEVEL);
1756    awmm->insert_option("0", 0, 0);
1757    awmm->insert_option("1", 0, 1);
1758    awmm->insert_option("2", 0, 2);
1759    awmm->insert_option("3", 0, 3);
1760    awmm->insert_option("4", 0, 4);
1761    awmm->insert_option("5", 0, 5);
1762    awmm->insert_default_option("6", 0, 6);
1763    awmm->update_option_menu();
1764
1765    // draw protection icon AFTER protection!!
1766    awmm->at("pico");
1767    awmm->create_button(NULL, "#protect.xpm");
1768
1769    // draw align/edit-button AFTER protection!!
1770    awmm->at("edit");
1771    awmm->create_toggle(AWAR_EDIT_MODE, "#edit/align.xpm", "#edit/editseq.xpm", 7);
1772
1773    awmm->at("insert");
1774    awmm->create_text_toggle(AWAR_INSERT_MODE, "Replace", "Insert", 7);
1775
1776    awmm->at("direct");
1777    awmm->create_toggle(AWAR_EDIT_RIGHTWARD, "#edit/3to5.xpm", "#edit/5to3.xpm", 7);
1778
1779    // -------------------------
1780    //      secedit / rna3d
1781
1782    int xoffset = 0;
1783
1784    if (alignment_type == GB_AT_RNA) { // if the database contains valid alignment of rRNA sequences
1785        // add buttons for RNA3D and SECEDIT plugins
1786
1787        awmm->button_length(0);
1788
1789        awmm->at("secedit");
1790        awmm->callback(ED4_start_plugin, (AW_CL)GLOBAL_gb_main, (AW_CL)"SECEDIT");
1791        awmm->help_text("arb_secedit.hlp");
1792        awmm->create_button("SECEDIT", "#edit/secedit.xpm");
1793
1794#if defined(ARB_OPENGL)
1795        awmm->at("rna3d");
1796        awmm->callback(ED4_start_plugin, (AW_CL)GLOBAL_gb_main, (AW_CL)"RNA3D");
1797        awmm->help_text("rna3d_general.hlp");
1798        awmm->create_button("RNA3D", "#edit/rna3d.xpm");
1799#endif // ARB_OPENGL
1800    }
1801    else {
1802        awmm->at("secedit");
1803        int xsecedit = awmm->get_at_xposition();
1804        awmm->at("zoom");
1805        int xzoom    = awmm->get_at_xposition();
1806        xoffset      = xsecedit-xzoom; // shift message stuff to the left by xoffset
1807    }
1808
1809    {
1810        int x, y;
1811
1812        awmm->at("zoom");
1813        if (xoffset) { awmm->get_at_position(&x, &y); awmm->at(x+xoffset, y); }
1814        awmm->callback(AW_POPUP, (AW_CL)ED4_zoom_message_window, (AW_CL)0);
1815        awmm->create_button("ZOOM", "#edit/zoom.xpm");
1816
1817        awmm->at("clear");
1818        if (xoffset) { awmm->get_at_position(&x, &y); awmm->at(x+xoffset, y); }
1819        awmm->callback(ED4_clear_errors, (AW_CL)0);
1820        awmm->create_button("CLEAR", "#edit/clear.xpm");
1821
1822        awmm->at("errortext");
1823        if (xoffset) { awmm->get_at_position(&x, &y); awmm->at(x+xoffset, y); }
1824        aw_root->awar_string(AWAR_ERROR_MESSAGES, "This is ARB Edit4 [Build " ARB_VERSION "]");
1825        awmm->create_text_field(AWAR_ERROR_MESSAGES);
1826        aw_set_local_message();
1827    }
1828
1829    // ---------------------
1830    //      'more' area
1831
1832    awmm->at("cons");
1833    awmm->create_toggle(ED4_AWAR_CONSENSUS_SHOW, "#edit/nocons.xpm", "#edit/cons.xpm");
1834
1835    awmm->at("num");
1836    awmm->create_toggle(ED4_AWAR_DIGITS_AS_REPEAT, "#edit/norepeat.xpm", "#edit/repeat.xpm");
1837
1838    awmm->at("key");
1839    awmm->create_toggle("key_mapping/enable", "#edit/nokeymap.xpm", "#edit/keymap.xpm");
1840
1841    // search
1842
1843    awmm->button_length(0);
1844#define INSERT_SEARCH_FIELDS(Short, label_prefix, prefix)       \
1845    insert_search_fields(awmm,                                  \
1846                         #label_prefix,                         \
1847                         #prefix,                               \
1848                         ED4_AWAR_##prefix##_SEARCH_PATTERN,    \
1849                         ED4_AWAR_##prefix##_SEARCH_SHOW,       \
1850                         Short,                                 \
1851                         ED4_##prefix##_PATTERN,                \
1852                         current_ed4w()                         \
1853        )
1854
1855    INSERT_SEARCH_FIELDS(0, u1, USER1);
1856    INSERT_SEARCH_FIELDS(0, u2, USER2);
1857    INSERT_SEARCH_FIELDS(0, pro, PROBE);
1858    INSERT_SEARCH_FIELDS(1, pri1, PRIMER1);
1859    INSERT_SEARCH_FIELDS(1, pri2, PRIMER2);
1860    INSERT_SEARCH_FIELDS(1, pri3, PRIMER3);
1861    INSERT_SEARCH_FIELDS(1, sig1, SIG1);
1862    INSERT_SEARCH_FIELDS(1, sig2, SIG2);
1863    INSERT_SEARCH_FIELDS(1, sig3, SIG3);
1864
1865#undef INSERT_SEARCH_FIELDS
1866
1867    awmm->at("alast");
1868    awmm->callback(ED4_search_cb, ED4_encodeSearchDescriptor(-1, ED4_ANY_PATTERN), (AW_CL)current_ed4w());
1869    awmm->create_button("ALL_SEARCH_LAST", "#edit/last.xpm");
1870
1871    awmm->at("anext");
1872    awmm->callback(ED4_search_cb, ED4_encodeSearchDescriptor(+1, ED4_ANY_PATTERN), (AW_CL)current_ed4w());
1873    awmm->create_button("ALL_SEARCH_NEXT", "#edit/next.xpm");
1874
1875    title_mode_changed(aw_root, awmm);
1876
1877    // Buttons at left window border
1878
1879    awmm->create_mode("edit/arrow.xpm", "normal.hlp", AWM_ALL, makeWindowCallback(modes_cb, ED4_SM_MOVE));
1880    awmm->create_mode("edit/kill.xpm",  "kill.hlp",   AWM_ALL, makeWindowCallback(modes_cb, ED4_SM_KILL));
1881    awmm->create_mode("edit/mark.xpm",  "mark.hlp",   AWM_ALL, makeWindowCallback(modes_cb, ED4_SM_MARK));
1882
1883    FastAligner_create_variables(awmm->get_root(), props_db);
1884
1885#if defined(DEBUG)
1886    AWT_check_action_ids(awmm->get_root(), "");
1887#endif
1888
1889    return (ED4_R_OK);
1890}
1891
1892AW_window *ED4_root::create_new_window() {
1893    // only the first window, other windows are generated by generate_window
1894    AW_device  *device     = NULL;
1895    ED4_window *new_window = NULL;
1896
1897    generate_window(&device, &new_window);
1898
1899    ED4_LocalWinContext uses(new_window);
1900   
1901    ED4_calc_terminal_extentions();
1902
1903    DRAW                    = 1;
1904    move_cursor             = 0;
1905    max_seq_terminal_length = 0;
1906
1907    ED4_init_notFoundMessage();
1908
1909    return new_window->aww;
1910}
1911
1912ED4_index ED4_root::pixel2pos(AW_pos click_x) {
1913    // 'click_x' is the x-offset into the terminal in pixels
1914    // returns the x-offset into the terminal in base positions (clipped to max. allowed position)
1915
1916    int       length_of_char = font_group.get_width(ED4_G_SEQUENCES);
1917    ED4_index scr_pos        = int((click_x-CHARACTEROFFSET) / length_of_char);
1918    int       max_scrpos     = root_group_man->remap()->get_max_screen_pos();
1919
1920    if (scr_pos>max_scrpos) scr_pos = max_scrpos;
1921
1922    return scr_pos;
1923}
1924
1925static char *detectProperties() {
1926    char *propname = NULL;
1927
1928    // check if edit4_?na.arb / edit4_ali_???.arb exist in $ARB_PROP
1929    for (int mode = 0; !propname && mode <= 1; ++mode) { 
1930        const char *fullprop = GB_path_in_arbprop(ED4_propertyName(mode));
1931        if (GB_is_regularfile(fullprop)) {
1932            freedup(propname, fullprop);
1933        }
1934    }
1935
1936    // if not, use 'mode 2', i.e. "edit4.arb"
1937    // (no full path, we want to load default from arb_defaults)
1938    if (!propname) propname = strdup(ED4_propertyName(2));
1939
1940    return propname;
1941}
1942
1943ED4_root::ED4_root(int* argc, char*** argv)
1944    : most_recently_used_window(0),
1945      db_name(detectProperties()),
1946      aw_root(AWT_create_root(db_name, "ARB_EDIT4", need_macro_ability(), argc, argv)),
1947      props_db(AW_ROOT_DEFAULT),
1948      first_window(0),
1949      main_manager(0),
1950      middle_area_man(0),
1951      top_area_man(0),
1952      root_group_man(0),
1953      database(0),
1954      selected_objects(new ED4_selected_list), 
1955      folding_action(0),
1956      species_mode(ED4_SM_MOVE),
1957      ecoli_ref(0),
1958      alignment_name(0),
1959      alignment_type(GB_AT_UNKNOWN),
1960      reference(0),
1961      sequence_colors(0),
1962      gc_manager(0),
1963      st_ml(0),
1964      helix(0),
1965      helix_spacing(0),
1966      helix_add_spacing(0),
1967      terminal_add_spacing(0),
1968      protstruct(0),
1969      protstruct_len(0),
1970      edk(0),
1971      edit_string(0),
1972      column_stat_activated(false),
1973      column_stat_initialized(false),
1974      visualizeSAI(false),
1975      visualizeSAI_allSpecies(false),
1976      temp_gc(0)
1977{}
1978
1979
1980ED4_root::~ED4_root() {
1981    delete aw_root;
1982    delete first_window;
1983    delete main_manager;
1984    delete middle_area_man;
1985    delete top_area_man;
1986    delete database;
1987    delete ecoli_ref;
1988    delete selected_objects;
1989
1990    free(protstruct);
1991    free(db_name);
1992}
Note: See TracBrowser for help on using the repository browser.