// =============================================================== //
//                                                                 //
//   File      : NT_extern.cxx                                     //
//   Purpose   :                                                   //
//                                                                 //
//   Institute of Microbiology (Technical University Munich)       //
//   http://www.arb-home.de/                                       //
//                                                                 //
// =============================================================== //

#include "map_viewer.h"
#include "NT_local.h"
#include "ad_trees.h"
#include "NT_group_search.h"

#include <seq_quality.h>
#include <multi_probe.hxx>
#include <st_window.hxx>
#include <GEN.hxx>
#include <EXP.hxx>

#include <TreeCallbacks.hxx>
#include <AW_rename.hxx>
#include <probe_gui.hxx>
#include <primer_design.hxx>
#include <gde.hxx>
#include <awtc_submission.hxx>

#include <macros.hxx>
#include <MacroExitor.hxx>
#include <xcmd.hxx>
#include <nds.h>
#include <db_query.h>
#include <dbui.h>
#include <refentries.h>
#include <insdel.h>

#include <awti_export.hxx>
#include <awti_import.hxx>

#include <app.hxx>
#include <input_mask.hxx>
#include <sel_boxes.hxx>
#include <selection_admin.h>
#include <www.hxx>
#include <TreeAwars.hxx>
#include <arb_spec.hxx>

#include <aw_color_groups.hxx>
#include <aw_advice.hxx>
#include <aw_preset.hxx>
#include <aw_awars.hxx>
#include <aw_global_awars.hxx>
#include <aw_file.hxx>
#include <arb_progress.h>
#include <aw_select.hxx>
#include <aw_system.hxx>
#include <rootAsWin.h>

#include <ad_cb.h>

#include <arb_strbuf.h>
#include <arb_file.h>
#include <arb_version.h>

#include <saicalc.h>

#include <string>

#if defined(DEVEL_RALF)
# define TRACE_GROUP_SELECTION
#endif

#define AWAR_EXPORT_NDS              "tmp/export_nds"
#define AWAR_EXPORT_NDS_SEPARATOR    AWAR_EXPORT_NDS "/delimiter"
#define AWAR_MARKED_SPECIES_COUNTER  "tmp/disp_marked_species"
#define AWAR_NTREE_TITLE_MODE        "tmp/title_mode"

void create_probe_design_variables(AW_root *aw_root, AW_default def, AW_default global);

void       create_insertDeleteColumn_variables(AW_root *root, AW_default db1);
AW_window *create_insertDeleteColumn_window(AW_root *root);
AW_window *create_insertDeleteBySAI_window(AW_root *root, GBDATA *gb_main);

AW_window *create_tree_window(AW_root *aw_root, AWT_graphic *awd);

enum Export_Type {
    EXPORT_SPACE_PADDED_RIGHT_ALIGNED = 1, // format info (using spaces). slow - uses one extra pass for column width detection.
    EXPORT_SPACE_PADDED_LEFT_ALIGNED  = 4, // dito, but left aligned
    EXPORT_TAB_SEPARATED              = 2, // format info (using 1 tab per column - for easy import into star-calc, excel, etc. )
    EXPORT_COMMA_SEPARATED            = 3, // like EXPORT_TAB_SEPARATED, but using commas
};

static void nt_changesecurity(AW_root *aw_root) {
    int level = aw_root->awar(AWAR_SECURITY_LEVEL)->read_int();
    GB_push_transaction(GLOBAL.gb_main);
    GB_change_my_security(GLOBAL.gb_main, level); // changes user security
    GB_pop_transaction(GLOBAL.gb_main);
}

static void export_nds_cb(AW_window *aww, bool do_print) {
    AW_root *aw_root = aww->get_root();
    char    *name    = aw_root->awar(AWAR_EXPORT_NDS"/file_name")->read_string();
    FILE    *out     = fopen(name, "w");

    if (!out) {
        aw_message("Error: Cannot open and write to file");
    }
    else {
        GB_transaction ta(GLOBAL.gb_main);

        char              *tree_name    = aw_root->awar(AWAR_TREE)->read_string();
        const Export_Type  export_type  = (Export_Type)aw_root->awar(AWAR_EXPORT_NDS_SEPARATOR)->read_int();
        const NDS_Type     nds_type     = export_type == EXPORT_COMMA_SEPARATED ? NDS_OUTPUT_COMMA_SEPARATED : NDS_OUTPUT_TAB_SEPARATED;
        const bool         spacePadded  = export_type == EXPORT_SPACE_PADDED_LEFT_ALIGNED || export_type == EXPORT_SPACE_PADDED_RIGHT_ALIGNED;
        const int          passesNeeded = spacePadded ? 2 : 1;
        NDS_Labeler        labeler(nds_type);

        arb_progress progress("Exporting data using NDS", passesNeeded*GBT_count_marked_species(GLOBAL.gb_main));

        const size_t max_col = std::max(labeler.max_columns(GLOBAL.gb_main), 1L);
        size_t       col_max_len[max_col];

        const char *col_format = NULp;

        if (spacePadded) {
            for (size_t i = 0; i<max_col; ++i) {
                col_max_len[i] = 0;
            }
            if (export_type == EXPORT_SPACE_PADDED_RIGHT_ALIGNED) {
                col_format = "%*s";
            }
            else {
                col_format = "%-*s";
            }
        }

        for (int pass = 1; pass<=passesNeeded; ++pass) {
            for (GBDATA *gb_species = GBT_first_marked_species(GLOBAL.gb_main);
                 gb_species;
                 gb_species = GBT_next_marked_species(gb_species))
            {
                const char *label = labeler.speciesLabel(GLOBAL.gb_main, gb_species, NULp, tree_name);

                if (spacePadded) {
                    ConstStrArray parts;
                    GBT_split_string(parts, label, "\t", SPLIT_KEEPEMPTY);

                    size_t col_count = parts.size();
                    nt_assert(col_count<=max_col);

                    if (pass == 1) { // track maximum used width for all columns
                        for (size_t i = 0; i<col_count; ++i) {
                            col_max_len[i] = std::max(col_max_len[i], strlen(parts[i]));
                        }
                    }
                    else { // format according to tracked widths
                        for (size_t i = 0; i<col_count; ++i) {
                            if (i) fputc(' ', out);

                            arb_assert(col_max_len[i]>0);        // otherwise empty, unused column (might be ok, but might need different handling)
                            arb_assert(col_max_len[i]<=INT_MAX); // (protect cast below)

                            if (export_type == EXPORT_SPACE_PADDED_LEFT_ALIGNED && i == (col_count-1)) { // left aligned and last column
                                fputs(parts[i], out);
                            }
                            else {
                                fprintf(out, col_format, int(col_max_len[i]), parts[i]);
                            }
                        }
                        fputc('\n', out);
                    }
                }
                else {
                    fprintf(out, "%s\n", label);
                }
                ++progress;
            }
        }

        AW_refresh_fileselection(aw_root, AWAR_EXPORT_NDS);
        fclose(out);
        if (do_print) {
            GB_ERROR error = GB_textprint(name);
            if (error) aw_message(error);
        }
        free(tree_name);
    }
    free(name);
}

static AW_window *create_nds_export_window(AW_root *root) {
    AW_window_simple *aws = new AW_window_simple;
    aws->init(root, "EXPORT_NDS_OF_MARKED", "EXPORT NDS OF MARKED SPECIES");
    aws->load_xfig("sel_box_nds.fig");

    aws->callback(AW_POPDOWN);
    aws->at("close");
    aws->create_button("CLOSE", "CLOSE", "C");

    aws->callback(makeHelpCallback("arb_export_nds.hlp"));
    aws->at("help");
    aws->create_button("HELP", "HELP", "H");

    aws->at("save");
    aws->callback(makeWindowCallback(export_nds_cb, false));
    aws->create_button("SAVE", "SAVE", "S");

    aws->at("print");
    aws->callback(makeWindowCallback(export_nds_cb, true));
    aws->create_button("PRINT", "PRINT", "P");

    aws->at("toggle1");
    aws->label("Column output");
    aws->create_option_menu(AWAR_EXPORT_NDS_SEPARATOR);
    aws->insert_option        ("Space padded (left aligned)",  "S", EXPORT_SPACE_PADDED_LEFT_ALIGNED);
    aws->insert_option        ("Space padded (right aligned)", "r", EXPORT_SPACE_PADDED_RIGHT_ALIGNED);
    aws->insert_default_option("TAB separated",                "T", EXPORT_TAB_SEPARATED);
    aws->insert_option        ("Comma separated",              "C", EXPORT_COMMA_SEPARATED);
    aws->update_option_menu();

    AW_create_standard_fileselection(aws, AWAR_EXPORT_NDS);

    return aws;
}

static void create_export_nds_awars(AW_root *awr, AW_default def) {
    AW_create_fileselection_awars(awr, AWAR_EXPORT_NDS, "", ".nds", "export.nds");
    awr->awar_int(AWAR_EXPORT_NDS_SEPARATOR, EXPORT_TAB_SEPARATED, def);
}

static void selectedSpeciesChanged_cb(AW_root *awr) {
    const char *value = awr->awar(AWAR_SPECIES_NAME)->read_char_pntr();
    awr->awar(AWAR_INFO_BUTTON_TEXT)->write_string(value[0] ? value : "Species Info");
    if (value[0]) {
        awr->awar(AWAR_GROUP)->write_pointer(NULp); // deselect group when species gets selected
#if defined(TRACE_GROUP_SELECTION)
        fputs("clear AWAR_GROUP (because species selected)\n", stderr);
#endif
    }
}

static void expert_mode_changed_cb(AW_root *aw_root) {
    aw_root->awar(AWAR_AWM_MASK)->write_int(aw_root->awar(AWAR_EXPERT)->read_int() ? AWM_ALL : AWM_BASIC); // publish expert-mode globally
}

static void NT_toggle_expert_mode(AW_window *aww) { aww->get_root()->awar(AWAR_EXPERT)->toggle_toggle(); }
static void NT_toggle_focus_policy(AW_window *aww) { aww->get_root()->awar(AWAR_AW_FOCUS_FOLLOWS_MOUSE)->toggle_toggle(); }

static void nt_create_all_awars(AW_root *awr) {
    // creates awars for all modules reachable from ARB_NT main window

    awr->awar_string(AWAR_FOOTER, "", AW_ROOT_DEFAULT);
    if (GB_read_clients(GLOBAL.gb_main)>=0) {
        awr->awar_string(AWAR_TREE, "", GLOBAL.gb_main);
    }
    else {
        awr->awar_string(AWAR_TREE, "", AW_ROOT_DEFAULT);
    }

    awr->awar_string(AWAR_TOPAREA_SAIS, "ECOLI;HELIX;HELIX_NR", AW_ROOT_DEFAULT);

    awr->awar_string(AWAR_SPECIES_NAME, "",     GLOBAL.gb_main);
    awr->awar_string(AWAR_SAI_NAME, "",     GLOBAL.gb_main);
    awr->awar_string(AWAR_SAI_GLOBAL, "",     GLOBAL.gb_main);
    awr->awar_string(AWAR_INFO_BUTTON_TEXT, "Species Info",    GLOBAL.gb_main);
    awr->awar(AWAR_SPECIES_NAME)->add_callback(selectedSpeciesChanged_cb);
    awr->awar_int(AWAR_NTREE_TITLE_MODE, 1);

    awr->awar_string(AWAR_SAI_COLOR_STR, "", GLOBAL.gb_main); // sai visualization in probe match

    GEN_create_awars(awr, AW_ROOT_DEFAULT, GLOBAL.gb_main);
    EXP_create_awars(awr, AW_ROOT_DEFAULT, GLOBAL.gb_main);
#if defined(DEBUG)
    AWT_create_db_browser_awars(awr, AW_ROOT_DEFAULT);
#endif // DEBUG

    AW_create_namesadmin_awars(awr, GLOBAL.gb_main);

    awr->awar_int(AWAR_SECURITY_LEVEL, 0, AW_ROOT_DEFAULT);
    awr->awar(AWAR_SECURITY_LEVEL)->add_callback(nt_changesecurity);
#if defined(DEBUG) && 0
    awr->awar(AWAR_SECURITY_LEVEL)->write_int(6); // no security for debugging..
#endif // DEBUG

    create_insertDeleteColumn_variables(awr, AW_ROOT_DEFAULT);
    create_probe_design_variables(awr, AW_ROOT_DEFAULT, GLOBAL.gb_main);
    create_primer_design_variables(awr, AW_ROOT_DEFAULT, GLOBAL.gb_main);
    create_trees_var(awr, AW_ROOT_DEFAULT);
    DBUI::create_dbui_awars(awr);
    AP_create_consensus_var(awr, AW_ROOT_DEFAULT);
    {
        GB_ERROR gde_err = GDE_init(awr, AW_ROOT_DEFAULT, GLOBAL.gb_main, NULp, ARB_format_alignment, GDE_WINDOWTYPE_DEFAULT);
        if (gde_err) GBK_terminatef("Fatal: %s", gde_err);
    }
    NT_create_transpro_variables(awr, AW_ROOT_DEFAULT);
    NT_create_resort_awars(awr, AW_ROOT_DEFAULT);
    NT_create_compare_taxonomy_awars(awr, AW_ROOT_DEFAULT);
    NT_create_trackAliChanges_Awars(awr, AW_ROOT_DEFAULT);

    NT_create_alignment_vars(awr, AW_ROOT_DEFAULT, GLOBAL.gb_main);
    NT_create_extendeds_vars(awr, AW_ROOT_DEFAULT, GLOBAL.gb_main);
    NDS_create_vars(awr, AW_ROOT_DEFAULT, GLOBAL.gb_main);
    create_export_nds_awars(awr, AW_ROOT_DEFAULT);
    create_group_search_awars(awr, AW_ROOT_DEFAULT);
    TREE_create_awars(awr, GLOBAL.gb_main);

    awr->awar_string(AWAR_ERROR_MESSAGES, "", GLOBAL.gb_main);
    awr->awar_string(AWAR_DB_COMMENT, "<no description>", GLOBAL.gb_main);

    AWTC_create_submission_variables(awr, GLOBAL.gb_main);
    NT_createConcatenationAwars(awr, AW_ROOT_DEFAULT, GLOBAL.gb_main);
    NT_createValidNamesAwars(awr, AW_ROOT_DEFAULT); // lothar
    SQ_create_awars(awr, AW_ROOT_DEFAULT);
    RefEntries::create_refentries_awars(awr, AW_ROOT_DEFAULT);

    NT_create_multifurcate_tree_awars(awr, AW_ROOT_DEFAULT);

    GB_ERROR error = ARB_bind_global_awars(GLOBAL.gb_main);
    if (!error) {
        AW_awar *awar_expert = awr->awar(AWAR_EXPERT);
        awar_expert->add_callback(expert_mode_changed_cb);
        awar_expert->touch();

        awt_create_aww_vars(awr, AW_ROOT_DEFAULT);
    }

    if (error) aw_message(error);
}

// --------------------------------------------------------------------------------

static void nt_run(const char *command) {
    GB_ERROR error = GBK_system(command);
    if (error) {
        fprintf(stderr, "nt_run: Failed to run '%s' (Reason: %s)\n", command, error);
#if defined(DEBUG)
        GBK_dump_backtrace(stderr, "nt_run-error");
#endif
    }
}

void NT_start_if(const char *arb_ntree_args) {
    if (arb_ntree_args && arb_ntree_args[0]) {
        char *command = GBS_global_string_copy("arb_launcher --async arb %s", arb_ntree_args);
        nt_run(command);
        free(command);
    }
}

void NT_exit_now_restart_if(AW_root *aw_root, int exitcode, const char *restart_ntree_args) { // goes to header: __ATTR__NORETURN
    // Exit arb_ntree + optionally restart arb.
    //
    // 'exitcode':           exit with this code.
    // 'restart_ntree_args': if null or empty  -> just exit;
    //                       otherwise         -> restarts arb_ntree (with new ARB_PID).

    AWTI_cleanup_importer(aw_root);

    bool is_server_and_has_clients = GLOBAL.gb_main && GB_read_clients(GLOBAL.gb_main)>0;
    ARB_disconnect_from_db(aw_root, GLOBAL.gb_main);
    NT_start_if(restart_ntree_args);
    if (is_server_and_has_clients) {
        nt_run("(arb_clean session; echo ARB done) &"); // kills all clients
    }
    exit(exitcode);
}

class NT_MacroExitor: public MacroExitor {
    int         exitcode;
    std::string restart_ntree_args; // [if non-empty] restart arb with these arguments

    __ATTR__NORETURN void perform_exit() OVERRIDE {
        NT_exit_now_restart_if(get_root(), exitcode, restart_ntree_args.c_str());
    }

    GBDATA* get_gbmain_checked_for_save() OVERRIDE {
        return GLOBAL.gb_main;
    }

public:
    NT_MacroExitor(AW_root *aw_root_, int exitcode_, const char *restart_ntree_args_)
        : MacroExitor(aw_root_, "ARB"),
          exitcode(exitcode_),
          restart_ntree_args(null2empty(restart_ntree_args_)) // restart arb if not empty/null
    {}
};

void NT_confirm_exit_restart_if(AW_root *aw_root, int exitcode, const char *restart_ntree_args) {
    // finally (may) exit ARB using NT_exit_now_restart_if(). see there for arguments.
    //
    // Beforehand this will happen:
    // - if no macro running -> asks user to confirm quit (and maybe aborts exit/restart).
    // - otherwise waits for macro to finish, then quits w/o question.

    static SmartPtr<NT_MacroExitor> exitor;
    if (exitor.isNull()) exitor = new NT_MacroExitor(aw_root, exitcode, restart_ntree_args);
    exitor->maybe_exit_delayed();
    if (!exitor->is_performing_delayed_exit()) exitor.setNull();
}

void NT_confirm_exit(AW_window *aws, int exitcode) {
    NT_confirm_exit_restart_if(aws->get_root(), exitcode, NULp);
}

static void nt_start_2nd_arb(AW_window *aww, bool quit) {
    // start 2nd arb with intro window
    AW_root *aw_root = aww->get_root();
    char    *dir4intro;
    GB_split_full_path(aw_root->awar(AWAR_SAVED_DB_PATH)->read_char_pntr(), &dir4intro, NULp, NULp, NULp);
    if (!dir4intro) {
        dir4intro = ARB_strdup(".");
    }

    if (quit) {
        NT_confirm_exit_restart_if(aww->get_root(), EXIT_SUCCESS, dir4intro);
    }
    else {
        NT_start_if(dir4intro);
    }
    free(dir4intro);
}

// --------------------------------------------------------------------------------

static void NT_save_quick_cb(AW_window *aww) {
    AW_root *awr      = aww->get_root();
    char    *filename = awr->awar(AWAR_DB_PATH)->read_string();

    awr->dont_save_awars_with_default_value(GLOBAL.gb_main);

    GB_ERROR error = GB_save_quick(GLOBAL.gb_main, filename);
    if (error) aw_message(error);
    else {
        awr->awar(AWAR_SAVED_DB_PATH)->write_string(filename);
        AW_refresh_fileselection(awr, AWAR_DBBASE);
    }

    free(filename);
}


static void NT_save_quick_as_cb(AW_window *aww) {
    AW_root *awr      = aww->get_root();
    char    *filename = awr->awar(AWAR_DB_PATH)->read_string();

    // @@@ also use dont_save_awars_with_default_value here?

    GB_ERROR error = GB_save_quick_as(GLOBAL.gb_main, filename);
    if (!error) {
        awr->awar(AWAR_SAVED_DB_PATH)->write_string(filename);
        AW_refresh_fileselection(awr, AWAR_DBBASE);
    }
    aww->hide_or_notify(error);

    free(filename);
}

#define AWAR_DB_TYPE          AWAR_DBBASE "/type"      // created by AWT_insert_DBsaveType_selector
#define AWAR_DB_COMPRESSION   AWAR_DBBASE "/compression" // created by AWT_insert_DBcompression_selector
#define AWAR_DB_OPTI_TREENAME AWAR_DBBASE "/optimize_tree_name"

static void NT_save_as_cb(AW_window *aww) {
    AW_root    *awr      = aww->get_root();
    char       *filename = awr->awar(AWAR_DB_PATH)->read_string();
    const char *atype    = awr->awar(AWAR_DB_TYPE)->read_char_pntr();
    const char *ctype    = awr->awar(AWAR_DB_COMPRESSION)->read_char_pntr();
    char       *savetype = GBS_global_string_copy("%s%s", atype, ctype);

    awr->dont_save_awars_with_default_value(GLOBAL.gb_main);

    GB_ERROR error = GB_save(GLOBAL.gb_main, filename, savetype);
    if (!error) {
        awr->awar(AWAR_SAVED_DB_PATH)->write_string(filename);
        AW_refresh_fileselection(awr, AWAR_DBBASE);
    }
    aww->hide_or_notify(error);

    free(savetype);
    free(filename);
}

static AW_window *NT_create_save_quick_as_window(AW_root *aw_root, const char *base_name) {
    static AW_window_simple *aws = NULp;
    if (!aws) {
        aws = new AW_window_simple;
        aws->init(aw_root, "SAVE_CHANGES_TO", "Quicksave changes as");
        aws->load_xfig("save_as.fig");

        aws->at("close");
        aws->callback(AW_POPDOWN);
        aws->create_button("CLOSE", "CLOSE", "C");

        aws->callback(makeHelpCallback("save.hlp"));
        aws->at("help");
        aws->create_button("HELP", "HELP", "H");

        AW_create_standard_fileselection(aws, base_name);

        aws->at("comment");
        aws->create_text_field(AWAR_DB_COMMENT);

        aws->at("save");
        aws->callback(NT_save_quick_as_cb);
        aws->create_button("SAVE", "SAVE", "S");
    }
    return aws;
}

static void NT_database_optimization(AW_window *aww) {
    arb_progress progress(WEIGHTED, "Optimizing database compression", 0.38);
    // 38% for seq-compression is just a rough guess made with silvaNR99138
    // (a real guess would have to depend on ali-count, ali-length + field-count)

    GB_topSecurityLevel unsecured(GLOBAL.gb_main);

    GB_ERROR error = GB_begin_transaction(GLOBAL.gb_main);
    if (!error) {
        ConstStrArray ali_names;
        GBT_get_alignment_names(ali_names, GLOBAL.gb_main);

        arb_progress ali_progress("Optimizing sequence data", ali_names.size());
        ali_progress.allow_title_reuse();

        error = GBT_check_data(GLOBAL.gb_main, NULp);
        error = GB_end_transaction(GLOBAL.gb_main, error);

        if (!error) {
            char *tree_name = aww->get_root()->awar(AWAR_DB_OPTI_TREENAME)->read_string();
            for (int i = 0; ali_names[i]; ++i) {
                error = GBT_compress_sequence_tree2(GLOBAL.gb_main, tree_name, ali_names[i]);
                ali_progress.inc_and_check_user_abort(error);
            }
            free(tree_name);
        }
    }
    progress.inc_and_check_user_abort(error);

    if (!error) {
        error = GB_optimize(GLOBAL.gb_main);
        progress.inc_and_check_user_abort(error);
    }
    aww->hide_or_notify(error);
}

static AW_window *NT_create_database_optimization_window(AW_root *aw_root) {
    static AW_window_simple *aws = NULp;
    if (aws) return aws;
    GB_transaction ta(GLOBAL.gb_main);

    const char *largest_tree = GBT_name_of_largest_tree(GLOBAL.gb_main);
    aw_root->awar_string(AWAR_DB_OPTI_TREENAME, largest_tree);

    aws = new AW_window_simple;
    aws->init(aw_root, "OPTIMIZE_DATABASE", "Optimize database compression");
    aws->load_xfig("optimize.fig");

    aws->at("trees");
    awt_create_TREE_selection_list(GLOBAL.gb_main, aws, AWAR_DB_OPTI_TREENAME);

    aws->at("close");
    aws->callback(AW_POPDOWN);
    aws->create_button("CLOSE", "CLOSE", "C");

    aws->callback(makeHelpCallback("optimize.hlp"));
    aws->at("help");
    aws->create_button("HELP", "HELP", "H");

    aws->at("go");
    aws->callback(NT_database_optimization);
    aws->create_button("GO", "GO");
    return aws;
}

static AW_window *NT_create_save_as(AW_root *aw_root, const char *base_name) {
    static AW_window_simple *aws = NULp;
    if (aws) return aws;

    aws = new AW_window_simple;
    aws->init(aw_root, "SAVE_DB", "SAVE ARB DB");
    aws->load_xfig("save_as.fig");

    aws->at("close");
    aws->callback(AW_POPDOWN);
    aws->create_button("CLOSE", "CLOSE", "C");

    aws->at("help");
    aws->callback(makeHelpCallback("save.hlp"));
    aws->create_button("HELP", "HELP", "H");

    AW_create_standard_fileselection(aws, base_name);

    aws->at("type");
    AWT_insert_DBsaveType_selector(aws, AWAR_DB_TYPE);

    aws->at("compression");
    AWT_insert_DBcompression_selector(aws, AWAR_DB_COMPRESSION);

    aws->at("optimize");
    aws->callback(NT_create_database_optimization_window);
    aws->help_text("optimize.hlp");
    aws->create_autosize_button("OPTIMIZE", "Optimize database compression");

    aws->at("save");
    aws->callback(NT_save_as_cb);
    aws->create_button("SAVE", "SAVE", "S");

    aws->at("comment");
    aws->create_text_field(AWAR_DB_COMMENT);

    return aws;
}

static void NT_undo_cb(AW_window*, GB_UNDO_TYPE undo_type, TREE_canvas *ntw) {
    GB_ERROR error = GB_undo(GLOBAL.gb_main, undo_type);
    if (error) aw_message(error);
    else {
        GB_transaction ta(GLOBAL.gb_main);
        AWT_auto_refresh allowed_on(ntw);
        ntw->request_refresh();
    }
}

static void NT_infomode_cb(UNFIXED, TREE_canvas *canvas, AWT_COMMAND_MODE mode) {
    DBUI::popup_species_info_window(canvas->awr, canvas->gb_main);
    nt_mode_event(NULp, canvas, mode);
}

static void NT_mark_duplicates(UNFIXED, TREE_canvas *ntw) {
    AP_tree *tree_root = AWT_TREE(ntw)->get_root_node();
    if (tree_root) {
        AWT_auto_refresh allowed_on(ntw);
        GB_transaction   ta(ntw->gb_main);

        NT_mark_all_cb(NULp, ntw, 0); // unmark all
        tree_root->mark_duplicates();
        ntw->request_structure_update();
    }
}

static void NT_justify_branch_lenghs(UNFIXED, TREE_canvas *ntw) {
    GB_transaction  ta(ntw->gb_main);
    AP_tree        *tree_root = AWT_TREE(ntw)->get_root_node();

    if (tree_root) {
        AWT_auto_refresh allowed_on(ntw);
        tree_root->justify_branch_lenghs(ntw->gb_main);
        ntw->request_save_and_zoom_reset();
    }
}

#if defined(DEBUG)
static void NT_fix_database(AW_window *) {
    GB_ERROR err = NULp;
    err = GB_fix_database(GLOBAL.gb_main);
    if (err) aw_message(err);
}
#endif

static void relink_pseudo_species_to_organisms(GBDATA *&ref_gb_node, char *&ref_name, GB_HASH *organism_hash) {
    if (ref_gb_node) {
        if (GEN_is_pseudo_gene_species(ref_gb_node)) {
            GBDATA *gb_organism = GEN_find_origin_organism(ref_gb_node, organism_hash);

            if (gb_organism) {
                char *name = GBT_read_string(gb_organism, "name");

                if (name) {
                    freeset(ref_name, name);
                    ref_gb_node = gb_organism;
                }
            }
        }
    }
}

static void NT_pseudo_species_to_organism(AW_window *, TREE_canvas *ntw) {
    GB_transaction  ta(ntw->gb_main);
    AP_tree        *tree_root = AWT_TREE(ntw)->get_root_node();

    if (tree_root) {
        AWT_auto_refresh allowed_on(ntw);

        GB_HASH *organism_hash = GBT_create_organism_hash(ntw->gb_main);
        tree_root->relink_tree(ntw->gb_main, relink_pseudo_species_to_organisms, organism_hash);
        GBS_free_hash(organism_hash);
        ntw->request_save();
    }
}


// #########################################
// #########################################
// ###                                   ###
// ##          user mask section          ##
// ###                                   ###
// #########################################
// #########################################

struct nt_item_type_species_selector FINAL_TYPE : public awt_item_type_selector {
    nt_item_type_species_selector() : awt_item_type_selector(AWT_IT_SPECIES) {}
    ~nt_item_type_species_selector() OVERRIDE {}

    const char *get_self_awar() const OVERRIDE {
        return AWAR_SPECIES_NAME;
    }
    size_t get_self_awar_content_length() const OVERRIDE {
        return 12; // should be enough for "nnaammee.999"
    }
    GBDATA *current(AW_root *root, GBDATA *gb_main) const OVERRIDE { // give the current item
        char           *species_name = root->awar(get_self_awar())->read_string();
        GBDATA         *gb_species   = NULp;

        if (species_name[0]) {
            GB_transaction ta(gb_main);
            gb_species = GBT_find_species(gb_main, species_name);
        }

        free(species_name);
        return gb_species;
    }
    const char *getKeyPath() const OVERRIDE { // give the keypath for items
        return CHANGE_KEY_PATH;
    }
};

static nt_item_type_species_selector item_type_species;

static void NT_open_mask_window(AW_window *aww, int id, GBDATA *gb_main) {
    const awt_input_mask_descriptor *descriptor = AWT_look_input_mask(id);
    nt_assert(descriptor);
    if (descriptor) AWT_initialize_input_mask(aww->get_root(), gb_main, &item_type_species, descriptor->get_internal_maskname(), descriptor->is_local_mask());
}

static void NT_create_mask_submenu(AW_window_menu_modes *awm) {
    AWT_create_mask_submenu(awm, AWT_IT_SPECIES, NT_open_mask_window, GLOBAL.gb_main);
}
static AW_window *create_colorize_species_window(AW_root *aw_root) {
    return QUERY::create_colorize_items_window(aw_root, GLOBAL.gb_main, SPECIES_get_selector());
}

// --------------------------------------------------------------------------------------------------

static void NT_alltree_remove_leafs(AW_window *, GBT_TreeRemoveType mode, GBDATA *gb_main) {
    GB_ERROR       error = NULp;
    GB_transaction ta(gb_main);

    ConstStrArray tree_names;
    GBT_get_tree_names(tree_names, gb_main, false);

    if (!tree_names.empty()) {
        long treeCount = tree_names.size();

        const char *whats_removed = NULp;
        switch (mode) {
            case GBT_REMOVE_ZOMBIES: whats_removed = "zombies"; break;
            case GBT_REMOVE_MARKED:  whats_removed = "marked";  break;
            default: nt_assert(0); break;
        }

        const char *task = GBS_global_string("Deleting %s from %li trees", whats_removed, treeCount);

        arb_progress  progress(task, treeCount);
        GB_HASH      *species_hash = GBT_create_species_hash(gb_main);

        int modified = 0;

        for (int t = 0; t<treeCount && !error; t++) {
            progress.subtitle(tree_names[t]);
            TreeNode *tree = GBT_read_tree(gb_main, tree_names[t], new SimpleRoot);
            if (!tree) {
                aw_message(GBS_global_string("Can't load tree '%s' - skipped", tree_names[t]));
            }
            else {
                int removed        = 0;
                int groups_removed = 0;

                tree = GBT_remove_leafs(tree, mode, species_hash, &removed, &groups_removed);

                nt_assert(removed >= groups_removed);

                if (!tree) {
                    aw_message(GBS_global_string("'%s' would disappear. Please delete tree manually.", tree_names[t]));
                }
                else {
                    if (removed>0) {
                        error = GBT_write_tree(gb_main, tree_names[t], tree);
                        if (error) {
                            aw_message(GBS_global_string("Failed to write '%s' (Reason: %s)", tree_names[t], error));
                        }
                        else {
                            if (groups_removed>0) {
                                aw_message(GBS_global_string("Removed %i species and %i groups from '%s'", removed, groups_removed, tree_names[t]));
                            }
                            else {
                                aw_message(GBS_global_string("Removed %i species from '%s'", removed, tree_names[t]));
                            }
                            modified++;
                        }
                    }
                    UNCOVERED();
                    destroy(tree);
                }
            }
            progress.inc_and_check_user_abort(error);
        }

        if (modified) {
            aw_message(GBS_global_string("Changed %i of %li trees.", modified, treeCount));
        }
        else {
            aw_message("No tree modified");
        }

        GBS_free_hash(species_hash);
    }

    aw_message_if(ta.close(error));
}

// --------------------------------------------------------------------------------------------------

static ARB_ERROR mark_referred_species(GBDATA *gb_main, const DBItemSet& referred) {
    GB_transaction ta(gb_main);
    GBT_mark_all(gb_main, 0);

    DBItemSetIter end = referred.end();
    for (DBItemSetIter s = referred.begin(); s != end; ++s) {
        GB_write_flag(*s, 1);
    }
    return ARB_ERROR();
}

static AW_window *create_mark_by_refentries_window(AW_root *awr, GBDATA *gbmain) {
    static AW_window *aws = NULp;
    if (!aws) {
        static RefEntries::ReferringEntriesHandler reh(gbmain, SPECIES_get_selector());
        aws = RefEntries::create_refentries_window(awr, &reh, "markbyref", "Mark by reference", "markbyref.hlp", NULp, "Mark referenced", mark_referred_species);
    }
    return aws;
}

// --------------------------------------------------------------------------------------------------

static void merge_from_to(AW_root *awr, const char *db_awar_name, bool merge_to) {
    const char *db_name  = awr->awar(db_awar_name)->read_char_pntr();
    char       *quotedDB = GBK_singlequote(db_name);

    AW_system(
        GBS_global_string(
            merge_to
            ? "arb_ntree : %s &"
            : "arb_ntree %s : &",
            quotedDB)
        );

    free(quotedDB);
}

static void merge_from_cb(AW_window *aww, AW_CL cl_awarNamePtr) { merge_from_to(aww->get_root(), *(const char**)cl_awarNamePtr, false); }
static void merge_into_cb(AW_window *aww, AW_CL cl_awarNamePtr) { merge_from_to(aww->get_root(), *(const char**)cl_awarNamePtr, true); }

static AW_window *NT_create_merge_from_window(AW_root *awr) {
    static char *awar_name = NULp; // do not free, bound to callback
    AW_window *aw_from     =
        awt_create_load_box(awr, "Merge data from", "other ARB database",
                            ".", ".arb", &awar_name,
                            makeWindowCallback(merge_from_cb, AW_CL(&awar_name)));
    return aw_from;
}
static AW_window *NT_create_merge_to_window(AW_root *awr) {
    static char *awar_name = NULp; // do not free, bound to callback
    AW_window *aw_to       =
        awt_create_load_box(awr, "Merge data to", "other or new ARB database",
                            ".", ".arb", &awar_name,
                            makeWindowCallback(merge_into_cb, AW_CL(&awar_name)),
                            makeWindowCallback(AW_POPDOWN), NULp);
    return aw_to;
}

// --------------------------------------------------------------------------------------------------

static void update_main_window_title(AW_root *awr, AW_window_menu_modes *aww, int clone) {
    const char *filename   = awr->awar(AWAR_DB_NAME)->read_char_pntr();
    const char *base_title = GBS_global_string("%s - ARB", filename);
    aww->set_window_title(TREE_canvas::suffixed_title_for_index(base_title, clone));
}

static void canvas_tree_awar_changed_cb(AW_awar *, bool, TREE_canvas *ntw) {
    NT_reload_tree_event(AW_root::SINGLETON, ntw, true);
    AW_root::SINGLETON->awar(AWAR_NTREE_MAIN_WINDOW_COUNT)->touch(); // refresh canvas selection lists
    TREE_auto_jump_cb(NULp, ntw, AP_JUMP_REASON_TREE);
}

class TREE_canvas_registry {
    int          count;
    TREE_canvas *canvas[MAX_NT_WINDOWS];

    static TREE_canvas_registry *SINGLETON;

    TREE_canvas_registry() : count(0) {
        AW_root::SINGLETON->awar_int(AWAR_NTREE_MAIN_WINDOW_COUNT, 0, AW_ROOT_DEFAULT);
    }

public:
    static TREE_canvas_registry& instance() {
        if (!SINGLETON) SINGLETON = new TREE_canvas_registry;
        return *SINGLETON;
    }

    void register_canvas(TREE_canvas *ntw) {
        nt_assert(count<MAX_NT_WINDOWS);
        nt_assert(count == ntw->get_index()); // you have to register TREE_canvas'es in order of creation!
        canvas[count++] = ntw;
        ntw->awr->awar(AWAR_NTREE_MAIN_WINDOW_COUNT)->write_int(count); // trigger callbacks
    }

    int get_count() const  { return count; }

    TREE_canvas *get_canvas(int idx) const {
        if (idx<0 || idx>=count) return NULp;
        return canvas[idx];
    }
};

TREE_canvas_registry *TREE_canvas_registry::SINGLETON = NULp;

TREE_canvas *NT_get_canvas_by_index(int idx) {
    /*! return canvas with index 'idx' [0...MAX_NT_WINDOWS-1] */
    nt_assert(idx>=0 && idx<MAX_NT_WINDOWS);
    return TREE_canvas_registry::instance().get_canvas(idx);
}

#if defined(TRACE_GROUP_SELECTION)
static TREE_canvas *find_canvas_showing(AWT_graphic_tree *agt) {
    // look for (registered) canvas showing given AWT_graphic_tree
    // returns NULp if none found
    const TREE_canvas_registry& reg = TREE_canvas_registry::instance();

    int maxIdx = reg.get_count();
    for (int32_t i = 0; i<maxIdx; ++i) {
        TREE_canvas *ntw = reg.get_canvas(i);
        if (ntw->gfx == agt) return ntw;
    }
    return NULp;
}
#endif

void NT_fill_canvas_selection_list(class AW_selection_list *sellst, TREE_canvas *to_skip) {
    /*! insert canvases into selection list (using canvas-indices as values)
     */
    const TREE_canvas_registry& reg = TREE_canvas_registry::instance();

    int maxIdx = reg.get_count();
    for (int32_t i = 0; i<maxIdx; ++i) {
        TREE_canvas *ntw = reg.get_canvas(i);
        if (ntw && ntw != to_skip) {
            nt_assert(ntw->get_awar_tree());
            const char *treename    = ntw->get_awar_tree()->read_char_pntr();
            const char *description = i
                ? GBS_global_string("%s: %s", TREE_canvas::suffixed_title_for_index("ARB", i), treename)
                : GBS_global_string("ARB main: %s", treename);
            sellst->insert(description, i);
        }
    }
}

// ----------------------------------
//      group-selection handlers

void NT_deselect_group(AW_root *awr) {
    awr->awar(AWAR_GROUP)->write_pointer(NULp);
}

static bool track_group_selection_by_canvas = true;

static void group_selected_by_canvas_cb(AWT_graphic_tree *gfx) {
    const Group& group = gfx->get_selected_group();

#if defined(TRACE_GROUP_SELECTION)
    fprintf(stderr, "[%s] ", track_group_selection_by_canvas ? "track" : "dont track");
    TREE_canvas *ntw = find_canvas_showing(gfx);
    if (group.is_valid()) {
        fprintf(stderr, "canvas %i selects group '%s'\n", ntw->get_index(), group.get_name());
    }
    else {
        fprintf(stderr, "canvas %i de-selects group\n", ntw->get_index());
    }
#endif

    if (track_group_selection_by_canvas) {
        // set group-awar:
        gfx->get_root()->awar(AWAR_GROUP)->write_pointer(group.get_group_data()); // writes NULp if !group.is_valid()
    }
}

static void group_awar_changed_cb(AW_root *awr) {
    AW_awar *awar_group      = awr->awar(AWAR_GROUP);
    GBDATA  *gb_wanted_group = awar_group->read_pointer();

    GBDATA *gb_wanted_tree = NULp;
    {
        if (gb_wanted_group) {
            GB_transaction ta(gb_wanted_group);
            gb_wanted_tree = GB_get_father(gb_wanted_group);
            awr->awar(AWAR_SPECIES_NAME)->write_string(""); // deselect species when group gets selected
#if defined(TRACE_GROUP_SELECTION)
            fputs("clear AWAR_SPECIES_NAME (because group selected)\n", stderr);
#endif
        }
#if defined(TRACE_GROUP_SELECTION)
        const char *group_name = NULp;
        if (gb_wanted_group) {
            GB_transaction  ta(gb_wanted_group);
            GBDATA         *gb_name = GB_entry(gb_wanted_group, "group_name");
            if (gb_name) group_name = GB_read_char_pntr(gb_name);
        }
        fprintf(stderr, "group awar changed to %p", gb_wanted_group);
        if (group_name) fprintf(stderr, " (='%s')", group_name);
        fputc('\n', stderr);
#endif
    }

    LocallyModify<bool> ignore_group_selection_by_canvas(track_group_selection_by_canvas, false);

    {
        const TREE_canvas_registry& reg = TREE_canvas_registry::instance();

        int maxIdx = reg.get_count();
        for (int32_t i = 0; i<maxIdx; ++i) {
            TREE_canvas      *ntw        = reg.get_canvas(i);
            AWT_graphic_tree *agt        = AWT_TREE(ntw);
            const Group&      curr_group = agt->get_selected_group();

            GBDATA *gb_curr_tree = agt->get_tree_root()->get_gb_tree();
            if (gb_curr_tree == gb_wanted_tree) {
                AWT_auto_refresh allowed_on(ntw);
                if (!curr_group.is_valid() ||                       // no group selected
                    curr_group.get_group_data() != gb_wanted_group) // wrong group selected
                {
                    agt->select_group(gb_wanted_group);
                    ntw->request_refresh();
                }
                // perform auto-jump (also if group was already selected, otherwise group-traversal keys will not follow the selected group)
                TREE_auto_jump_cb(NULp, ntw, AP_JUMP_REASON_GROUP);
            }
            else {
                if (curr_group.is_valid()) {
                    AWT_auto_refresh allowed_on(ntw);
                    agt->deselect_group();
                    ntw->request_refresh();
                }
            }
        }
    }
}

// ----------------------------
//      create main window

static AW_window *popup_new_main_window(AW_root *awr, int clone, TREE_canvas **result_ntw) {
    /*! create ARB_NTREE main window
     * @param awr application root
     * @param clone == 0 -> first window (full functionality); >0 -> additional tree views (restricted functionality)
     * @param result_ntw is set to the created TREE_canvas (passed pointer may be NULp if result is not needed)
     */
    GB_push_transaction(GLOBAL.gb_main);

    const char *awar_tree = NULp;
    AW_window_menu_modes *awm = new AW_window_menu_modes;

    {
        char window_id[256];
        if (clone) {
            awar_tree = ARB_keep_string(GBS_global_string_copy(AWAR_TREE "_%i", clone));
            sprintf(window_id, "ARB_NT_%i", clone);
        }
        else {
            awar_tree = AWAR_TREE;
            sprintf(window_id, "ARB_NT");
        }
        awm->init(awr, window_id, "[starting arb]", 0, 0); // effective title is generated by update_main_window_title
    }

    awr->awar(AWAR_DB_NAME)
       ->add_callback(makeRootCallback(update_main_window_title, awm, clone))
       ->update();

    awm->button_length(5);

    if (!clone) AW_init_color_group_defaults("arb_ntree");

    AWT_graphic_tree *tree = TREE_generate_display(awr, GLOBAL.gb_main, launch_MapViewer_cb);

    if (!clone) { // while creating initial window
        AWT_graphic_tree::install_group_changed_callback(makeGraphicTreeCallback(group_selected_by_canvas_cb));
        awr->awar(AWAR_GROUP)->add_callback(group_awar_changed_cb);
    }

    TREE_canvas *ntw;
    {
        AW_awar *tree_awar = awr->awar_string(awar_tree);
        {
            AP_tree_display_style prev_style = tree->get_tree_style();
            tree->set_tree_style(AP_LIST_SIMPLE, NULp); // avoid NDS warnings during startup

            ntw = new TREE_canvas(GLOBAL.gb_main, awm, "ARB_NT", tree, tree_awar);
            nt_assert(ntw->get_index() == clone); // required for proper title enumeration.

            tree->set_tree_style(prev_style, ntw);
            ntw->set_mode(AWT_MODE_SELECT);
        }

        if (result_ntw) *result_ntw = ntw;

        {
            const char *tree_name          = tree_awar->read_char_pntr();
            const char *existing_tree_name = GBT_existing_tree(GLOBAL.gb_main, tree_name);

            if (existing_tree_name) {
                tree_awar->write_string(existing_tree_name);
                NT_reload_tree_event(awr, ntw, true); // load first tree
            }
            else {
                AW_advice("Your database contains no tree.", AW_ADVICE_TOGGLE_AND_HELP, NULp, "no_tree.hlp");
                tree->set_tree_style(AP_LIST_NDS, ntw); // no tree -> show NDS list
            }

            TREE_canvas_registry::instance().register_canvas(ntw);
            AWT_registerTreeAwarCallback(tree_awar, makeTreeAwarCallback(canvas_tree_awar_changed_cb, ntw), false);
        }
    }

    TREE_install_update_callbacks(ntw);

    bool is_genome_db = GEN_is_genome_db(GLOBAL.gb_main, 0); //  is this a genome database ? (default = 0 = not a genom db)

    WindowCallback popupinfo_wcb = RootAsWindowCallback::simple(DBUI::popup_species_info_window, GLOBAL.gb_main);

    // --------------
    //      File

#if defined(DEBUG)
    AWT_create_debug_menu(awm);
#endif // DEBUG

    bool allow_new_window = (ntw->get_index()+1) < MAX_NT_WINDOWS;

    if (clone) {
        awm->create_menu("File", "F", AWM_ALL);
        if (allow_new_window) {
            awm->insert_menu_topic(awm->local_id("new_window"), "New window (same database)", "N", "newwindow.hlp", AWM_ALL, makeCreateWindowCallback(popup_new_main_window, clone+1, (TREE_canvas**)NULp));
        }
        awm->insert_menu_topic("close", "Close", "C", NULp, AWM_ALL, AW_POPDOWN);
    }
    else {
#if defined(DEBUG)
        // add more to debug-menu
        awm->sep______________();
        awm->insert_menu_topic("fix_db",      "Fix database",            "F", NULp, AWM_ALL, NT_fix_database);
        awm->insert_menu_topic("debug_arbdb", "Print debug information", "d", NULp, AWM_ALL, makeWindowCallback(GB_print_debug_information, GLOBAL.gb_main));
        awm->insert_menu_topic("test_compr",  "Test compression",        "T", NULp, AWM_ALL, makeWindowCallback(GBT_compression_test,       GLOBAL.gb_main));
#endif // DEBUG

        awm->create_menu("File", "F", AWM_ALL);
        {
            // keep the following entries in sync with ../EDIT4/ED4_root.cxx@common_save_menu_entries
            awm->insert_menu_topic("save_changes", "Quicksave changes",          "s", "save.hlp",      AWM_ALL, NT_save_quick_cb);
            awm->insert_menu_topic("save_all_as",  "Save whole database as ...", "w", "save.hlp",      AWM_ALL, makeCreateWindowCallback(NT_create_save_as, AWAR_DBBASE));
            if (allow_new_window) {
                awm->sep______________();
                awm->insert_menu_topic("new_window", "New window (same database)", "N", "newwindow.hlp", AWM_ALL, makeCreateWindowCallback(popup_new_main_window, clone+1, (TREE_canvas**)NULp));
            }
            awm->sep______________();
            awm->insert_menu_topic("optimize_db",  "Optimize database compression", "O", "optimize.hlp",  AWM_ALL, NT_create_database_optimization_window);
            awm->sep______________();

            awm->insert_sub_menu("Import",      "I");
            {
                awm->insert_menu_topic("merge_from", "Merge from other ARB database", "d", "arb_merge_into.hlp", AWM_ALL, NT_create_merge_from_window);
                awm->insert_menu_topic("import_seq", "Import from external format",   "I", "arb_import.hlp",     AWM_ALL, makeWindowCallback(NT_import_sequences, ntw));
                GDE_load_menu(awm, AWM_EXP, "Import");
            }
            awm->close_sub_menu();

            awm->insert_sub_menu("Export",      "E");
            {
                awm->insert_menu_topic("export_to_ARB", "Merge to other or new ARB database", "A", "arb_merge_outof.hlp", AWM_ALL, NT_create_merge_to_window);
                awm->insert_menu_topic("export_seqs",   "Export to external format",          "f", "arb_export.hlp",      AWM_ALL, makeCreateWindowCallback(create_AWTC_export_window, GLOBAL.gb_main));
                GDE_load_menu(awm, AWM_ALL, "Export");
                awm->insert_menu_topic("export_nds",    "Export fields (to calc-sheet using NDS)", "N", "arb_export_nds.hlp",  AWM_ALL, create_nds_export_window);
            }
            awm->close_sub_menu();
            awm->sep______________();

            insert_macro_menu_entry(awm, false);

            awm->insert_sub_menu("VersionInfo/Bugreport/MailingList", "V");
            {
                awm->insert_menu_topic("version_info", "Version info (" ARB_VERSION_DETAILED ")", "V", "version.hlp", AWM_ALL, makeHelpCallback  ("version.hlp"));
                awm->insert_menu_topic("bug_report",   "Report bug",                              "b", NULp,          AWM_ALL, makeWindowCallback(AWT_openURL, "http://bugs.arb-home.de/wiki/BugReport"));
                awm->insert_menu_topic("mailing_list", "Mailing list",                            "M", NULp,          AWM_ALL, makeWindowCallback(AWT_openURL, "http://bugs.arb-home.de/wiki/ArbMailingList"));
            }
            awm->close_sub_menu();
            awm->sep______________();

            awm->insert_menu_topic("new_arb",     "Start second database",      "d", "quit.hlp", AWM_ALL, makeWindowCallback(nt_start_2nd_arb, false));
            awm->insert_menu_topic("restart_arb", "Quit + load other database", "l", "quit.hlp", AWM_ALL, makeWindowCallback(nt_start_2nd_arb, true));
            awm->insert_menu_topic("quit",        "Quit",                       "Q", "quit.hlp", AWM_ALL, makeWindowCallback(NT_confirm_exit, EXIT_SUCCESS));
        }

        // -----------------
        //      Species

        awm->create_menu("Species", "c", AWM_ALL);
        {
            awm->insert_menu_topic("species_search", "Search and query",    "q", "sp_search.hlp", AWM_ALL, makeCreateWindowCallback(DBUI::create_species_query_window, GLOBAL.gb_main));
            awm->insert_menu_topic("species_info",   "Species information", "i", "sp_info.hlp",   AWM_ALL, popupinfo_wcb);

            awm->sep______________();

            NT_insert_mark_submenus(awm, ntw, 1);
            awm->insert_menu_topic("mark_by_ref",     "Mark by reference..", "r", "markbyref.hlp",       AWM_EXP, makeCreateWindowCallback(create_mark_by_refentries_window, GLOBAL.gb_main));
            awm->insert_menu_topic("species_colors",  "Colors ...",          "l", "colorize.hlp",        AWM_ALL, create_colorize_species_window);
            awm->insert_menu_topic("selection_admin", "Selections ...",      "o", "species_configs.hlp", AWM_ALL, makeWindowCallback(NT_popup_configuration_admin, ntw));

            awm->sep______________();

            awm->insert_sub_menu("Database fields admin", "f");
            DBUI::insert_field_admin_menuitems(awm, GLOBAL.gb_main);
            awm->close_sub_menu();
            NT_create_mask_submenu(awm);

            awm->sep______________();

            awm->insert_menu_topic("del_marked",    "Delete Marked Species",    "D", "sp_del_mrkd.hlp", AWM_ALL, makeWindowCallback(NT_delete_mark_all_cb, ntw));

            awm->insert_sub_menu("Sort Species",         "s");
            {
                awm->insert_menu_topic("$sort_by_field", "According to Database Entries", "D", "sp_sort_fld.hlp",  AWM_ALL, NT_create_resort_window);
                awm->insert_menu_topic("$sort_by_tree",  "According to Phylogeny",        "P", "sp_sort_phyl.hlp", AWM_ALL, makeWindowCallback(NT_resort_data_by_phylogeny, ntw));
            }
            awm->close_sub_menu();

            awm->insert_sub_menu("Merge Species", "g", AWM_EXP);
            {
                awm->insert_menu_topic("merge_species", "Create merged species from similar species", "m", "merge_species.hlp", AWM_EXP, NT_createMergeSimilarSpeciesWindow);
                awm->insert_menu_topic("join_marked",   "Join Marked Species",                        "J", "species_join.hlp",  AWM_EXP, NT_create_species_join_window);
            }
            awm->close_sub_menu();

            awm->sep______________();

            awm->insert_menu_topic("species_submission", "Submit Species", "b", "submission.hlp", AWM_EXP, makeCreateWindowCallback(AWTC_create_submission_window, GLOBAL.gb_main));

            awm->sep______________();

            awm->insert_menu_topic("new_names", "Synchronize IDs", "e", "rename.hlp", AWM_ALL, makeCreateWindowCallback(AWTC_create_rename_window, GLOBAL.gb_main));

            awm->insert_sub_menu("Valid Names ...", "V", AWM_EXP);
            {
                awm->insert_menu_topic("imp_names",    "Import names from file", "I", "vn_import.hlp",  AWM_EXP, NT_importValidNames);
                awm->insert_menu_topic("del_names",    "Delete names from DB",   "D", "vn_delete.hlp",  AWM_EXP, NT_deleteValidNames);
                awm->insert_menu_topic("sug_names",    "Suggest valid names",    "v", "vn_suggest.hlp", AWM_EXP, NT_suggestValidNames);
                awm->insert_menu_topic("search_names", "Search manually",        "m", "vn_search.hlp",  AWM_EXP, NT_create_searchManuallyNames_window);
            }
            awm->close_sub_menu();
        }

        // ----------------------------
        //      Genes + Experiment

        if (is_genome_db) GEN_create_genes_submenu(awm, GLOBAL.gb_main, true);

        // ------------------
        //      Sequence

        awm->create_menu("Sequence", "S", AWM_ALL);
        {
            awm->insert_menu_topic("seq_admin",   "Sequence/Alignment Admin", "A", "ad_align.hlp",   AWM_ALL, makeCreateWindowCallback(NT_create_alignment_admin_window, (AW_window*)NULp));
            awm->insert_sub_menu("Insert/delete", "I");
            {
                awm->insert_menu_topic("ins_del_col", ".. column",     "c", "insdel.hlp",     AWM_ALL, create_insertDeleteColumn_window);
                awm->insert_menu_topic("ins_del_sai", ".. using SAI",  "S", "insdel_sai.hlp", AWM_ALL, makeCreateWindowCallback(create_insertDeleteBySAI_window,  GLOBAL.gb_main));
            }
            awm->close_sub_menu();
            awm->sep______________();

            awm->insert_sub_menu("Edit Sequences", "E");
            {
                awm->insert_menu_topic("new_arb_edit4",  "Using marked species and tree", "m", "arb_edit4.hlp", AWM_ALL, makeWindowCallback(NT_start_editor_on_tree,  0, ntw));
                awm->insert_menu_topic("new2_arb_edit4", "... plus relatives",            "r", "arb_edit4.hlp", AWM_ALL, makeWindowCallback(NT_start_editor_on_tree, -1, ntw));
                awm->insert_menu_topic("old_arb_edit4",  "Using earlier configuration",   "c", "arb_edit4.hlp", AWM_ALL, NT_create_startEditorOnOldConfiguration_window);
            }
            awm->close_sub_menu();

            awm->sep______________();

            awm->insert_sub_menu("Align sequences",  "l");
            {
                awm->insert_menu_topic("realign_dna", "Realign DNA according to aligned protein", "R", "realign_dna.hlp", AWM_ALL, NT_create_realign_dna_window);
                GDE_load_menu(awm, AWM_ALL, "Align");
            }
            awm->close_sub_menu();
            awm->insert_menu_topic("seq_concat",    "Concatenate sequences",   "C", "concatenate.hlp",       AWM_ALL, NT_createConcatenationWindow);
            awm->insert_menu_topic("seq_split",     "Split sequences",         "S", "split.hlp",             AWM_ALL, NT_create_splitAlignment_window);
            awm->insert_menu_topic("track_changes", "Track alignment changes", "k", "track_ali_changes.hlp", AWM_EXP, NT_create_trackAliChanges_window);
            awm->sep______________();

            awm->insert_menu_topic("dna_2_pro", "Perform translation",      "t", "translate_dna_2_pro.hlp", AWM_ALL, NT_create_dna_2_pro_window);
            awm->insert_menu_topic("arb_dist",  "Distance Matrix + ARB NJ", "D", "dist.hlp",                AWM_ALL, makeWindowCallback(AW_system_async_cb, "arb_dist"));
            awm->sep______________();

            awm->insert_menu_topic("seq_quality",   "Check Sequence Quality", "Q", "seq_quality.hlp",   AWM_EXP, makeCreateWindowCallback(SQ_create_seq_quality_window, GLOBAL.gb_main));
            awm->insert_menu_topic("chimera_check", "Chimera Check",          "m", "chimera_check.hlp", AWM_EXP, makeCreateWindowCallback(STAT_create_chimera_check_window, GLOBAL.gb_main));

            awm->sep______________();

            GDE_load_menu(awm, AWM_ALL, "Print");
        }

        // -------------
        //      SAI

        awm->create_menu("SAI", "A", AWM_ALL);
        {
            awm->insert_menu_topic("sai_admin", "SAI Admin",    "S", "ad_extended.hlp", AWM_ALL, NT_create_extendeds_window);
            awm->insert_menu_topic("sai_calc",  "Combine SAIs", "b", "saicalc.hlp",     AWM_ALL, makeWindowCallback(SAI_popup_calculator_window, GLOBAL.gb_main));
            awm->insert_sub_menu("Create SAI using ...", "C");
            {
                awm->insert_menu_topic("sai_max_freq",  "Max. Frequency",                                               "M", "max_freq.hlp",     AWM_EXP, AP_create_max_freq_window);
                awm->insert_menu_topic("sai_consensus", "Consensus",                                                    "C", "consensus.hlp",    AWM_ALL, AP_create_con_expert_window);
                awm->insert_menu_topic("pos_var_pars",  "Positional variability + Column statistic (parsimony method)", "a", "pos_var_pars.hlp", AWM_ALL, AP_create_pos_var_pars_window);
                awm->insert_menu_topic("arb_phyl",      "Filter by base frequency",                                     "F", "phylo.hlp",        AWM_ALL, makeWindowCallback(AW_system_async_cb, "arb_phylo"));
                awm->insert_menu_topic("sai_pfold",     "Protein secondary structure (field \"sec_struct\")",           "P", "pfold.hlp",        AWM_EXP, NT_create_sai_from_pfold);

                GDE_load_menu(awm, AWM_EXP, "SAI");
            }
            awm->close_sub_menu();

            awm->insert_sub_menu("Other functions", "O");
            {
                awm->insert_menu_topic("count_different_chars", "Count different chars/column",             "C", "count_chars.hlp",    AWM_EXP, makeWindowCallback(NT_count_different_chars, GLOBAL.gb_main));
                awm->insert_menu_topic("corr_mutat_analysis",   "Compute clusters of correlated positions", "m", "",                   AWM_ALL, makeWindowCallback(ARB_system_in_console_cb, "arb_rnacma", new XCmdType(XCMD_SERVSYNC_WAIT_ON_ERROR, GLOBAL.gb_main)));
                awm->insert_menu_topic("export_pos_var",        "Export Column Statistic (GNUPLOT format)", "E", "csp_2_gnuplot.hlp",  AWM_ALL, NT_create_colstat_2_gnuplot_window);
            }
            awm->close_sub_menu();
        }

        // ----------------
        //      Probes

        awm->create_menu("Probes", "P", AWM_ALL);
        {
            awm->insert_menu_topic("probe_design", "Design Probes",          "D", "probedesign.hlp", AWM_ALL, makeCreateWindowCallback(create_probe_design_window, GLOBAL.gb_main));
            awm->insert_menu_topic("probe_multi",  "Calculate Multi-Probes", "u", "multiprobe.hlp",  AWM_ALL, makeCreateWindowCallback(create_multiprobe_window, ntw));
            awm->insert_menu_topic("probe_match",  "Match Probes",           "M", "probematch.hlp",  AWM_ALL, makeCreateWindowCallback(create_probe_match_window, GLOBAL.gb_main));
            awm->sep______________();
            awm->insert_menu_topic("probe_match_sp",   "Match Probes with Specificity", "f", "probespec.hlp", AWM_ALL, makeCreateWindowCallback(create_probe_match_with_specificity_window,    ntw));
            awm->sep______________();
            awm->insert_menu_topic("primer_design_new", "Design Primers",            "P", "primer_new.hlp", AWM_EXP, makeCreateWindowCallback(create_primer_design_window, GLOBAL.gb_main));
            awm->insert_menu_topic("primer_design",     "Design Sequencing Primers", "S", "primer.hlp",     AWM_EXP, makeWindowCallback(ARB_system_in_console_cb, "arb_primer", new XCmdType(XCMD_SERVSYNC_WAIT_ON_ERROR, GLOBAL.gb_main)));
            awm->sep______________();
            awm->insert_menu_topic("pt_server_admin",   "PT_SERVER Admin",           "A", "probeadmin.hlp",  AWM_ALL, makeCreateWindowCallback(create_probe_admin_window, GLOBAL.gb_main));
        }

    }

    // --------------
    //      Tree

    awm->create_menu("Tree", "T", AWM_ALL);
    {
        if (!clone) {
            awm->insert_menu_topic("tree_admin", "Tree admin",               "i", "treeadm.hlp",   AWM_ALL, popup_tree_admin_window);
            awm->insert_menu_topic("nds",        "NDS (Node display setup)", "N", "props_nds.hlp", AWM_ALL, makeCreateWindowCallback(NDS_create_window, GLOBAL.gb_main));
            awm->sep______________();
            awm->insert_sub_menu("Add Species to Existing Tree", "A");
            {
                awm->insert_menu_topic("arb_pars_quick", "ARB Parsimony (Quick add marked)", "Q", "pars.hlp", AWM_ALL, makeWindowCallback(AW_system_async_cb, "arb_pars -add_marked -quit"));
                awm->insert_menu_topic("arb_pars",       "ARB Parsimony interactive",        "i", "pars.hlp", AWM_ALL, makeWindowCallback(AW_system_async_cb, "arb_pars"));
                GDE_load_menu(awm, AWM_EXP, "Incremental phylogeny");
            }
            awm->close_sub_menu();
        }

        awm->insert_sub_menu("Remove Species from Tree",     "R");
        {
            awm->insert_menu_topic(awm->local_id("tree_remove_deleted"), "Remove zombies", "z", "trm_del.hlp",    AWM_ALL, makeWindowCallback(NT_remove_leafs, ntw, AWT_REMOVE_ZOMBIES));
            awm->insert_menu_topic(awm->local_id("tree_remove_marked"),  "Remove marked",  "m", "trm_mrkd.hlp",   AWM_ALL, makeWindowCallback(NT_remove_leafs, ntw, AWT_REMOVE_MARKED));
            awm->insert_menu_topic(awm->local_id("tree_keep_marked"),    "Keep marked",    "K", "tkeep_mrkd.hlp", AWM_ALL, makeWindowCallback(NT_remove_leafs, ntw, AWT_KEEP_MARKED));
            awm->sep______________();
            awm->insert_menu_topic("all_tree_remove_deleted", "Remove zombies from ALL trees", "A", "trm_del.hlp",  AWM_ALL, makeWindowCallback(NT_alltree_remove_leafs, GBT_REMOVE_ZOMBIES, GLOBAL.gb_main));
            awm->insert_menu_topic("all_tree_remove_marked",  "Remove marked from ALL trees",  "L", "trm_mrkd.hlp", AWM_ALL, makeWindowCallback(NT_alltree_remove_leafs, GBT_REMOVE_MARKED,  GLOBAL.gb_main));
        }
        awm->close_sub_menu();

        if (!clone) {
            awm->insert_sub_menu("Build tree from sequence data",    "B");
            {
                awm->insert_sub_menu("Distance matrix methods", "D");
                awm->insert_menu_topic("arb_dist", "Distance Matrix + ARB NJ", "J", "dist.hlp", AWM_ALL, makeWindowCallback(AW_system_async_cb, "arb_dist"));
                GDE_load_menu(awm, AWM_ALL, "Phylogeny Distance Matrix");
                awm->close_sub_menu();

                awm->insert_sub_menu("Maximum Parsimony methods", "P");
                GDE_load_menu(awm, AWM_ALL, "Phylogeny max. parsimony");
                awm->close_sub_menu();

                awm->insert_sub_menu("Maximum Likelihood methods", "L");
                GDE_load_menu(awm, AWM_EXP, "Phylogeny max. Likelihood EXP");
                GDE_load_menu(awm, AWM_ALL, "Phylogeny max. Likelihood");
                awm->close_sub_menu();

                awm->insert_sub_menu("Other methods", "O", AWM_EXP);
                GDE_load_menu(awm, AWM_EXP, "Phylogeny (Other)");
                awm->close_sub_menu();
            }
            awm->close_sub_menu();
        }

        awm->insert_menu_topic(awm->local_id("consense_tree"), "Build consensus tree", "c", "consense_tree.hlp", AWM_ALL, NT_create_consense_window);

        awm->sep______________();

        awm->insert_sub_menu("Reset zoom",         "z");
        {
            awm->insert_menu_topic(awm->local_id("reset_logical_zoom"),  "Logical zoom",  "L", "rst_log_zoom.hlp",  AWM_ALL, makeWindowCallback(NT_reset_lzoom_cb, ntw));
            awm->insert_menu_topic(awm->local_id("reset_physical_zoom"), "Physical zoom", "P", "rst_phys_zoom.hlp", AWM_ALL, makeWindowCallback(NT_reset_pzoom_cb, ntw));
        }
        awm->close_sub_menu();

        awm->sep______________();

        // taxonomic groups
        NT_insert_collapse_submenu(awm, ntw);
        if (!clone) {
            awm->insert_menu_topic("group_search",     "Search groups...",    "g", "group_search.hlp",     AWM_ALL, makeWindowCallback      (popup_group_search_window,         GLOBAL.gb_main));
            awm->insert_menu_topic("compare_taxonomy", "Compare taxonomy...", "x", "compare_taxonomy.hlp", AWM_ALL, makeCreateWindowCallback(NT_create_compare_taxonomy_window, ntw));
            awm->insert_menu_topic("move_groups",      "Move groups...",      "v", "move_groups.hlp",      AWM_ALL, makeCreateWindowCallback(NT_create_moveGroupInfo_window));
        }

        awm->sep______________();

        if (!clone) {
            awm->insert_menu_topic("compare_topology", "Compare topology...", "o", "compare_topo.hlp",     AWM_ALL, makeCreateWindowCallback(NT_create_compareTopologies_window));
        }

        awm->insert_sub_menu("Modify branches", "M");
        {
            awm->insert_menu_topic(awm->local_id("tree_remove_remark"), "Remove bootstraps", "b", "trm_boot.hlp", AWM_ALL, makeWindowCallback(NT_remove_bootstrap, ntw));
            awm->insert_menu_topic(awm->local_id("tree_edit_remark"),   "Edit bootstraps",   "E", "ted_boot.hlp", AWM_ALL, makeWindowCallback(NT_edit_bootstrap, ntw));
            awm->sep______________();
            awm->insert_menu_topic(awm->local_id("tree_reset_lengths"),     "Reset branchlengths",   "R", "tbl_reset.hlp",   AWM_ALL, makeWindowCallback(NT_reset_branchlengths,   ntw));
            awm->insert_menu_topic(awm->local_id("justify_branch_lengths"), "Justify branchlengths", "J", "tbl_justify.hlp", AWM_ALL, makeWindowCallback(NT_justify_branch_lenghs, ntw));
            awm->insert_menu_topic(awm->local_id("tree_scale_lengths"),     "Scale Branchlengths",   "S", "tbl_scale.hlp",   AWM_ALL, makeWindowCallback(NT_scale_tree,            ntw));
            awm->sep______________();
            awm->insert_menu_topic(awm->local_id("tree_multifurcate"), "Multifurcate...", "M", "multifurcate.hlp", AWM_ALL, makeCreateWindowCallback(NT_create_multifurcate_tree_window, ntw));
            awm->sep______________();
            awm->insert_menu_topic(awm->local_id("tree_boot2len"), "Bootstraps -> Branchlengths", "l", "tbl_boot2len.hlp", AWM_ALL, makeWindowCallback(NT_move_boot_branch, ntw, 0));
            awm->insert_menu_topic(awm->local_id("tree_len2boot"), "Bootstraps <- Branchlengths", "o", "tbl_boot2len.hlp", AWM_ALL, makeWindowCallback(NT_move_boot_branch, ntw, 1));

        }
        awm->close_sub_menu();
        awm->insert_sub_menu("Beautify tree", "u");
        {
            awm->insert_menu_topic(awm->local_id("beautifyt_tree"),  "#beautifyt.xpm",  "",  "resorttree.hlp", AWM_ALL, makeWindowCallback(NT_resort_tree_cb, ntw, BIG_BRANCHES_TO_TOP));
            awm->insert_menu_topic(awm->local_id("beautifyc_tree"),  "#beautifyc.xpm",  "",  "resorttree.hlp", AWM_ALL, makeWindowCallback(NT_resort_tree_cb, ntw, BIG_BRANCHES_TO_EDGE));
            awm->insert_menu_topic(awm->local_id("beautifyb_tree"),  "#beautifyb.xpm",  "",  "resorttree.hlp", AWM_ALL, makeWindowCallback(NT_resort_tree_cb, ntw, BIG_BRANCHES_TO_BOTTOM));
            awm->insert_menu_topic(awm->local_id("beautifyr_tree"),  "Radial tree (1)", "1", "resorttree.hlp", AWM_ALL, makeWindowCallback(NT_resort_tree_cb, ntw, BIG_BRANCHES_TO_CENTER));
            awm->insert_menu_topic(awm->local_id("beautifyr2_tree"), "Radial tree (2)", "2", "resorttree.hlp", AWM_ALL, makeWindowCallback(NT_resort_tree_cb, ntw, BIG_BRANCHES_ALTERNATING));
            awm->sep______________();
            awm->insert_menu_topic(awm->local_id("sort_by_other"),   "By other tree",   "o", "resortbyother.hlp", AWM_ALL, makeCreateWindowCallback(NT_create_sort_tree_by_other_tree_window, ntw));
        }
        awm->close_sub_menu();

        awm->insert_menu_topic(awm->local_id("branch_analysis"), "Branch analysis", "h", "branch_analysis.hlp", AWM_ALL, makeCreateWindowCallback(NT_create_branch_analysis_window, ntw));
        awm->insert_menu_topic(awm->local_id("syncroots"),       "Adjust roots...", "j", "syncroots.hlp",       AWM_ALL, makeCreateWindowCallback(NT_create_syncroot_window,        GLOBAL.gb_main));
        awm->insert_menu_topic(awm->local_id("mark_duplicates"), "Mark duplicates", "k", "mark_duplicates.hlp", AWM_ALL, makeWindowCallback      (NT_mark_duplicates,               ntw));

        awm->sep______________();
        awm->insert_menu_topic(awm->local_id("shade"),            "Tree shading...",       "s", "tree_shading.hlp",     AWM_ALL, makeWindowCallback      (NT_configure_treeShader));
        awm->insert_menu_topic(awm->local_id("syncscroll"),       "Sync tree scrolling..", "e", "syncscroll.hlp",       AWM_ALL, makeCreateWindowCallback(NT_create_syncScroll_window, ntw));

        awm->sep______________();
        awm->insert_menu_topic(awm->local_id("tree_select_latest"), "Select Last Tree", "L", NULp, AWM_ALL, makeWindowCallback(NT_select_bottom_tree, awar_tree));

        awm->sep______________();
        awm->insert_menu_topic("transversion",       "Transversion analysis",   "y", "trans_anal.hlp", AWM_EXP, makeHelpCallback("trans_anal.hlp"));

        if (is_genome_db) {
            awm->sep______________();
            awm->insert_menu_topic(awm->local_id("tree_pseudo_species_to_organism"), "Relink tree to organisms", "i", "tree_pseudo.hlp", AWM_EXP, makeWindowCallback(NT_pseudo_species_to_organism, ntw));
        }

        if (!clone) {
            awm->sep______________();
            awm->insert_menu_topic("print_tree",  "Print tree",          "P", "tree2prt.hlp",  AWM_ALL, makeWindowCallback(AWT_popup_print_window, static_cast<AWT_canvas*>(ntw)));
            awm->insert_menu_topic("tree_2_xfig", "Export tree to XFIG", "F", "tree2file.hlp", AWM_ALL, makeWindowCallback(AWT_popup_tree_export_window, static_cast<AWT_canvas*>(ntw)));
        }

    }

    if (!clone) {
        // ---------------
        //      Tools

        awm->create_menu("Tools", "o", AWM_ALL);
        {
            awm->insert_menu_topic("names_admin", "Nameserver admin (IDs)", "s", "namesadmin.hlp", AWM_ALL, makeWindowCallback(AW_popup_namesadmin_window, GLOBAL.gb_main));
            awm->insert_sub_menu("DB admin", "D", AWM_EXP);
            {
                awm->insert_menu_topic("db_admin", "Re-repair DB", "R", "rerepair.hlp", AWM_EXP, makeWindowCallback(NT_rerepair_DB, GLOBAL.gb_main));
            }
            awm->close_sub_menu();
            awm->sep______________();

            awm->insert_sub_menu("GDE specials", "G", AWM_EXP);
            {
                GDE_load_menu(awm, AWM_EXP, NULp);
            }
            awm->close_sub_menu();

            awm->sep______________();
            awm->insert_menu_topic("xterm",    "Start XTERM",     "X", "xterm.hlp",   AWM_ALL, AW_console_cb);
            XCmdType *showlog_cmdtype = new XCmdType(XCMD_ASYNC_WAIT_ON_ERROR, GLOBAL.gb_main); // do not free (used in callback)
            awm->insert_menu_topic("viewlog",  "View ARB log",    "l", "console.hlp", AWM_ALL, makeWindowCallback(ARB_system_in_console_cb, "arb_show_log.pl run",    showlog_cmdtype)); // ../SH/arb_show_log.pl
            awm->insert_menu_topic("viewserv", "View server log", "V", "console.hlp", AWM_ALL, makeWindowCallback(ARB_system_in_console_cb, "arb_show_log.pl server", showlog_cmdtype));
            awm->insert_menu_topic("viewsys",  "View sysinfo",    "i", "console.hlp", AWM_ALL, makeWindowCallback(ARB_system_in_console_cb, "arb_show_log.pl sysinf", showlog_cmdtype));
            awm->sep______________();
            GDE_load_menu(awm, AWM_EXP, "User");
        }
        // --------------------
        //      Properties

        awm->create_menu("Properties", "r", AWM_ALL);
        {
            awm->insert_menu_topic("props_menu",                 "Frame settings ...",          "F", "props_frame.hlp",      AWM_ALL, AW_preset_window);
            awm->insert_menu_topic(awm->local_id("props_tree2"), "Tree options",                "o", "nt_tree_settings.hlp", AWM_ALL, TREE_create_settings_window);
            awm->insert_menu_topic("props_tree",                 "Tree colors & fonts",         "c", "color_props.hlp",      AWM_ALL, makeCreateWindowCallback(AW_create_gc_window, ntw->gc_manager));
            awm->insert_menu_topic("props_www",                  "Search world wide web (WWW)", "b", "props_www.hlp",        AWM_ALL, makeCreateWindowCallback(AWT_create_www_window, GLOBAL.gb_main));
            awm->sep______________();
            awm->insert_menu_topic("!toggle_expert", "Toggle expert mode",         "x", NULp, AWM_ALL, NT_toggle_expert_mode);
            awm->insert_menu_topic("!toggle_focus",  "Toggle focus follows mouse", "m", NULp, AWM_ALL, NT_toggle_focus_policy);
            awm->sep______________();
            AW_insert_common_property_menu_entries(awm);
            awm->sep______________();
            awm->insert_menu_topic("save_props", "Save properties (ntree.arb)", "S", "savedef.hlp", AWM_ALL, AW_save_properties);
        }
    }

    awm->insert_help_topic("ARB NTREE help", "N", "arb_ntree.hlp", AWM_ALL, makeHelpCallback("arb_ntree.hlp"));

    // ----------------------
    //      mode buttons
    //
    // keep them synchronized as far as possible with those in ARB_PARSIMONY
    // see ../PARSIMONY/PARS_main.cxx@keepModesSynchronized

    awm->create_mode("mode_select.xpm", "mode_select.hlp", AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_SELECT));
    awm->create_mode("mode_mark.xpm",   "mode_mark.hlp",   AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_MARK));
    awm->create_mode("mode_group.xpm",  "mode_group.hlp",  AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_GROUP));
    awm->create_mode("mode_zoom.xpm",   "mode_pzoom.hlp",  AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_ZOOM));
    awm->create_mode("mode_lzoom.xpm",  "mode_lzoom.hlp",  AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_LZOOM));

    awm->create_mode("mode_info.xpm", "mode_info.hlp", AWM_ALL, makeWindowCallback(NT_infomode_cb, ntw, AWT_MODE_INFO));
    awm->create_mode("mode_www.xpm",  "mode_www.hlp",  AWM_ALL, makeWindowCallback(nt_mode_event,  ntw, AWT_MODE_WWW));

    // topology-modification-modes
    awm->create_mode("mode_setroot.xpm",   "mode_setroot.hlp", AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_SETROOT));
    awm->create_mode("mode_swap.xpm",      "mode_swap.hlp",    AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_SWAP));
    awm->create_mode("mode_move.xpm",      "mode_move.hlp",    AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_MOVE));
    awm->create_mode("mode_length.xpm",    "mode_length.hlp",  AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_LENGTH));
    awm->create_mode("mode_multifurc.xpm", "mode_length.hlp",  AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_MULTIFURC));

    // layout-modes
    awm->create_mode("mode_line.xpm",   "mode_width.hlp",  AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_LINE));
    awm->create_mode("mode_rotate.xpm", "mode_rotate.hlp", AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_ROTATE));
    awm->create_mode("mode_spread.xpm", "mode_spread.hlp", AWM_ALL, makeWindowCallback(nt_mode_event, ntw, AWT_MODE_SPREAD));

    awm->set_info_area_height(250);
    awm->at(5, 2);
    awm->auto_space(-2, -2);
    awm->shadow_width(1);

    // -------------------------
    //      create top area

    int first_liney;
    int leftx;
    awm->get_at_position(&leftx, &first_liney);

    // ---------------------
    //      quit + help

    awm->button_length(0);

    if (clone) {
        awm->callback(AW_POPDOWN);
        awm->create_button("CLOSE", "#close.xpm");
    }
    else {
        awm->callback(makeWindowCallback(NT_confirm_exit, EXIT_SUCCESS));
        awm->help_text("quit.hlp");
        awm->create_button("QUIT", "#quit.xpm");
    }

    int db_pathx = awm->get_at_xposition();

    // now fetch the yposition for the 2nd button line
    awm->at_newline();
    int second_liney = awm->get_at_yposition();

    awm->callback(AW_help_entry_pressed);
    awm->help_text("arb_ntree.hlp");
    awm->create_button("?", "#help.xpm");

    // --------------------------
    //      save + undo/redo

    awm->callback(NT_save_quick_cb);
    awm->help_text("save.hlp");
    awm->create_button("SAVE", "#save.xpm");

    awm->callback(makeCreateWindowCallback(NT_create_save_as, AWAR_DBBASE));
    awm->help_text("save.hlp");
    awm->create_button("SAVE_AS", "#saveAs.xpm");

    // undo/redo:
    awm->callback(makeWindowCallback(NT_undo_cb, GB_UNDO_UNDO, ntw));
    awm->help_text("undo.hlp");
    awm->create_button("UNDO", "#undo.xpm");

    awm->callback(makeWindowCallback(NT_undo_cb, GB_UNDO_REDO, ntw));
    awm->help_text("undo.hlp");
    awm->create_button("REDO", "#redo.xpm");

    int db_pathx2 = awm->get_at_xposition();

    // fetch position for mode help-line:
    awm->at_newline();
    int third_liney = awm->get_at_yposition();

    awm->at(db_pathx, first_liney);
    // size of DB-name button is determined by buttons below:
    awm->at_attach_to(false, false, db_pathx2-db_pathx-1, second_liney-first_liney+1);
    awm->callback(makeCreateWindowCallback(NT_create_save_quick_as_window, AWAR_DBBASE));
    awm->help_text("save.hlp");
    awm->create_button("QUICK_SAVE_AS", AWAR_DB_NAME);

    // -----------------------------
    //      tree + tree display

#define TREE_BUTTON_OVERSIZE 6

    int db_treex      = awm->get_at_xposition();
    int second_uppery = second_liney-TREE_BUTTON_OVERSIZE;

    awm->at(db_treex, second_uppery);
    awm->button_length(0);

    awm->callback(makeWindowCallback(NT_set_tree_style, ntw, AP_TREE_RADIAL));
    awm->help_text("tr_type_radial.hlp");
    awm->create_button("RADIAL_TREE_TYPE", "#radial.xpm");

    awm->callback(makeWindowCallback(NT_set_tree_style, ntw, AP_TREE_NORMAL));
    awm->help_text("tr_type_dendro.hlp");
    awm->create_button("LIST_TREE_TYPE", "#dendro.xpm");

    awm->callback(makeWindowCallback(NT_set_tree_style, ntw, AP_TREE_IRS));
    awm->help_text("tr_type_irs.hlp");
    awm->create_button("FOLDED_LIST_TREE_TYPE", "#dendroIrs.xpm");

    awm->callback(makeWindowCallback(NT_set_tree_style, ntw, AP_LIST_NDS));
    awm->help_text("tr_type_nds.hlp");
    awm->create_button("NO_TREE_TYPE", "#listdisp.xpm");

    int db_treex2 = awm->get_at_xposition();

    awm->at(db_treex, first_liney);
    // size of tree-name button is determined by buttons below:
    awm->at_attach_to(false, false, db_treex2-db_treex-1, second_uppery-first_liney+1);
    awm->callback(makeCreateWindowCallback(NT_create_select_tree_window, awar_tree, ntw));
    awm->help_text("nt_tree_select.hlp");
    awm->create_button("SELECT_A_TREE", awar_tree);

    // ---------------------------------------------------------------
    //      protection, alignment + editor (and maybe Genome Map)

    int db_alignx = awm->get_at_xposition();

    // editor and genemap buttons have to be 44x24 sized
#define EDIT_XSIZE 50
#define EDIT_YSIZE 30 // same as 'save as' buttons

    // first draw protection:
    int protectx = db_alignx + 2*EDIT_XSIZE;

    awm->at(protectx+2, first_liney+1);
    awm->button_length(0);
    awm->create_button(NULp, "#protect.xpm");

    awm->at(protectx, second_liney+2);
    awm->create_option_menu(AWAR_SECURITY_LEVEL);
    awm->insert_option        ("0", NULp, 0);
    awm->insert_option        ("1", NULp, 1);
    awm->insert_option        ("2", NULp, 2);
    awm->insert_option        ("3", NULp, 3);
    awm->insert_option        ("4", NULp, 4);
    awm->insert_option        ("5", NULp, 5);
    awm->insert_default_option("6", NULp, 6);
    awm->update_option_menu();

    int db_searchx = awm->get_at_xposition() - 7;

    // draw ali/editor buttons AFTER protect menu (to get rid of it's label)
    awm->at(db_alignx, second_liney);

    awm->at_attach_to(false, false, ((2-is_genome_db)*EDIT_XSIZE), EDIT_YSIZE);
    awm->callback(makeWindowCallback(NT_start_editor_on_tree, 0, ntw));
    awm->help_text("arb_edit4.hlp");
    if (is_genome_db) awm->create_button("EDIT_SEQUENCES", "#editor_small.xpm");
    else              awm->create_button("EDIT_SEQUENCES", "#editor.xpm");

    if (is_genome_db) {
        awm->at_attach_to(false, false, EDIT_XSIZE, EDIT_YSIZE);
        awm->callback(makeCreateWindowCallback(GEN_create_first_map, GLOBAL.gb_main)); // initial gene map
        awm->help_text("gene_map.hlp");
        awm->create_button("OPEN_GENE_MAP", "#gen_map.xpm");
    }

    int db_alignx2 = awm->get_at_xposition();

    awm->at(db_alignx, first_liney);
    awm->at_attach_to(false, false, db_alignx2-db_alignx-1, second_liney-first_liney+1);
    awm->callback(NT_create_select_alignment_window);
    awm->help_text("nt_align_select.hlp");
    awm->create_button("SELECT_AN_ALIGNMENT", AWAR_DEFAULT_ALIGNMENT);

    // -------------------------------
    //      create mode-help line

    awm->at(leftx, third_liney);
    awm->button_length(AWAR_FOOTER_MAX_LEN);
    awm->create_button(NULp, AWAR_FOOTER);

    awm->at_newline();
    int bottomy = awm->get_at_yposition();

    // ---------------------------------------
    //      Info / marked / Search / Jump

    awm->button_length(7);

    awm->at(db_searchx, first_liney);
    awm->callback(makeCreateWindowCallback(DBUI::create_species_query_window, GLOBAL.gb_main));
    awm->help_text("sp_search.hlp");
    awm->create_button("SEARCH",  "Search");

    awm->at(db_searchx, second_uppery);
    awm->callback(makeWindowCallback(NT_jump_cb, ntw, AP_JUMP_BY_BUTTON));
    awm->help_text("tr_jump.hlp");
    awm->create_button("JUMP", "Jump");

    int db_infox = awm->get_at_xposition();

    awm->at(db_infox, first_liney);
    awm->button_length(13);
    awm->callback(popupinfo_wcb);
    awm->help_text("sp_info.hlp");
    awm->create_button("INFO",  AWAR_INFO_BUTTON_TEXT);

    awm->at(db_infox, second_uppery);
    create_species_selection_button(awm, makeWindowCallback(NT_popup_configuration_admin, ntw), "selection_admin2", AWAR_MARKED_SPECIES_COUNTER, GLOBAL.gb_main);

    // set height of top area:
    awm->set_info_area_height(bottomy+2);
    awm->set_bottom_area_height(0);

    NT_activate_configMarkers_display(ntw);

    // ------------------------------------
    //      Autostarts for development

#if defined(DEVEL_RALF)
    // if (is_genome_db) GEN_map_first(awr)->show();
#endif // DEVEL_RALF

    GB_pop_transaction(GLOBAL.gb_main);

#if defined(DEBUG)
    AWT_check_action_ids(awr, is_genome_db ? "_genome" : "");
#endif

    return awm;
}

TREE_canvas *NT_create_main_window(AW_root *aw_root) {
    GB_ERROR error = GB_request_undo_type(GLOBAL.gb_main, GB_UNDO_NONE);
    if (error) aw_message(error);

    nt_create_all_awars(aw_root);

    TREE_canvas *ntw = NULp;
    AW_window   *aww = popup_new_main_window(aw_root, 0, &ntw);
    NT_install_treeShader(ntw, GLOBAL.gb_main);
    aww->show();

    error = GB_request_undo_type(GLOBAL.gb_main, GB_UNDO_UNDO);
    if (error) aw_message(error);

    return ntw;
}
