root/trunk/AWT/AWT_sel_boxes.cxx

Revision 8730, 32.4 KB (checked in by westram, 13 days ago)
  • more renames
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// ================================================================ //
2//                                                                  //
3//   File      : AWT_sel_boxes.cxx                                  //
4//   Purpose   :                                                    //
5//                                                                  //
6//   Institute of Microbiology (Technical University Munich)        //
7//   http://www.arb-home.de/                                        //
8//                                                                  //
9// ================================================================ //
10
11#include "awt.hxx"
12#include "awt_sel_boxes.hxx"
13
14#include <item_sel_list.h>
15
16#include <aw_awars.hxx>
17#include <aw_file.hxx>
18#include <aw_msg.hxx>
19#include <aw_root.hxx>
20#include <aw_edit.hxx>
21
22#include <ad_config.h>
23
24#include <arbdbt.h>
25#include <arb_strbuf.h>
26#include <arb_strarray.h>
27#include <arb_file.h>
28#include <arb_global_defs.h>
29
30#include <list>
31#include "awt_modules.hxx"
32
33using namespace std;
34
35
36
37// --------------------------------------
38//      selection boxes on alignments
39
40class AWT_alignment_selection : public AW_DB_selection { // derived from a Noncopyable
41    char *ali_type_match;                           // filter for wanted alignments (GBS_string_eval command)
42public:
43    AWT_alignment_selection(AW_selection_list *sellist_, GBDATA *gb_presets, const char *ali_type_match_)
44        : AW_DB_selection(sellist_, gb_presets)
45        , ali_type_match(nulldup(ali_type_match_))
46    {}
47   
48    void fill() {
49        GBDATA         *gb_presets = get_gbd();
50        GB_transaction  ta(gb_presets);
51
52        for (GBDATA *gb_alignment = GB_entry(gb_presets, "alignment");
53             gb_alignment;
54             gb_alignment = GB_nextEntry(gb_alignment))
55        {
56            char *alignment_type = GBT_read_string(gb_alignment, "alignment_type");
57            char *alignment_name = GBT_read_string(gb_alignment, "alignment_name");
58            char *str            = GBS_string_eval(alignment_type, ali_type_match, 0);
59
60            if (!*str) insert(alignment_name, alignment_name);
61            free(str);
62            free(alignment_type);
63            free(alignment_name);
64        }
65        insert_default(DISPLAY_NONE, NO_ALI_SELECTED);
66    }
67
68    void reconfigure(const char *new_ali_type_match) {
69        freedup(ali_type_match, new_ali_type_match);
70        refresh();
71    }
72};
73
74AW_DB_selection *awt_create_selection_list_on_alignments(GBDATA *gb_main, AW_window *aws, const char *varname, const char *ali_type_match) {
75    // Create selection lists on alignments
76    //
77    // if 'ali_type_match' is set, then only insert alignments,
78    // where 'ali_type_match' GBS_string_eval's the alignment type
79
80    GBDATA *gb_presets;
81    {
82        GB_transaction ta(gb_main);
83        gb_presets = GBT_get_presets(gb_main);
84    }
85    AW_selection_list       *sellist = aws->create_selection_list(varname, 0, "", 20, 3);
86    AWT_alignment_selection *alisel  = new AWT_alignment_selection(sellist, gb_presets, ali_type_match);
87    alisel->refresh(); // belongs to window now
88    return alisel;
89}
90
91void awt_reconfigure_selection_list_on_alignments(AW_DB_selection *dbsel, const char *ali_type_match) {
92    AWT_alignment_selection *alisel = dynamic_cast<AWT_alignment_selection*>(dbsel);
93    alisel->reconfigure(ali_type_match);
94}
95
96// ---------------------------------
97//      selection boxes on trees
98
99struct AWT_tree_selection: public AW_DB_selection {
100    AWT_tree_selection(AW_selection_list *sellist_, GBDATA *gb_tree_data)
101        : AW_DB_selection(sellist_, gb_tree_data)
102    {}
103
104    void fill() {
105        GBDATA         *gb_main = get_gb_main();
106        GB_transaction  ta(gb_main);
107
108        ConstStrArray tree_names;
109        GBT_get_tree_names(tree_names, gb_main, true);
110
111        if (!tree_names.empty()) {
112            int maxTreeNameLen = 0;
113            for (int i = 0; tree_names[i]; ++i) {
114                const char *tree = tree_names[i];
115                int         len  = strlen(tree);
116                if (len>maxTreeNameLen) maxTreeNameLen = len;
117            }
118            for (int i = 0; tree_names[i]; ++i) {
119                const char *tree = tree_names[i];
120                const char *info = GBT_tree_info_string(gb_main, tree, maxTreeNameLen);
121                if (info) {
122                    insert(info, tree);
123                }
124                else {
125                    aw_message(GB_await_error());
126                    insert(tree, tree);
127                }
128            }
129        }
130        insert_default(DISPLAY_NONE, NO_TREE_SELECTED); 
131    }
132};
133
134AW_DB_selection *awt_create_selection_list_on_trees(GBDATA *gb_main, AW_window *aws, const char *varname) {
135    GBDATA *gb_tree_data;
136    {
137        GB_transaction ta(gb_main);
138        gb_tree_data = GBT_get_tree_data(gb_main);
139    }
140    AW_selection_list  *sellist = aws->create_selection_list(varname, 0, "", 40, 4);
141    AWT_tree_selection *treesel = new AWT_tree_selection(sellist, gb_tree_data); // owned by nobody
142    treesel->refresh();
143    return treesel;
144}
145
146
147// --------------------------------------
148//      selection boxes on pt-servers
149
150#define PT_SERVERNAME_LENGTH        23              // that's for buttons
151#define PT_SERVERNAME_SELLIST_WIDTH 30              // this for lists
152#define PT_SERVER_TRACKLOG_TIMER    10000           // every 10 seconds
153
154class AWT_ptserver_selection : public AW_selection {
155    typedef list<AWT_ptserver_selection*> PTserverSelections;
156   
157    static PTserverSelections ptserver_selections;
158public:
159    AWT_ptserver_selection(AW_selection_list *sellist_);
160
161    void fill();
162
163    static void refresh_all();
164};
165
166AWT_ptserver_selection::PTserverSelections AWT_ptserver_selection::ptserver_selections;
167
168void AWT_ptserver_selection::fill() {
169    const char * const *pt_servers = GBS_get_arb_tcp_entries("ARB_PT_SERVER*");
170
171    int count = 0;
172    while (pt_servers[count]) count++;
173
174    for (int i=0; i<count; i++) {
175        char *choice = GBS_ptserver_id_to_choice(i, 1);
176        if (!choice) {
177            aw_message(GB_await_error());
178            break;
179        }
180        insert(choice, (long)i);
181        free(choice);
182    }
183
184    insert_default("-undefined-", (long)-1);
185}
186
187void AWT_ptserver_selection::refresh_all() {
188    PTserverSelections::iterator end = ptserver_selections.end();
189    for (PTserverSelections::iterator pts_sel = ptserver_selections.begin(); pts_sel != end; ++pts_sel) {
190        (*pts_sel)->refresh();
191    }
192}
193static void awt_refresh_all_pt_server_selection_lists() {
194    AWT_ptserver_selection::refresh_all();
195}
196static void track_log_cb(AW_root *awr) {
197    static long  last_ptserverlog_mod = 0;
198    const char  *ptserverlog          = GBS_ptserver_logname();
199    long         ptserverlog_mod      = GB_time_of_file(ptserverlog);
200
201    if (ptserverlog_mod != last_ptserverlog_mod) {
202#if defined(DEBUG)
203        fprintf(stderr, "%s modified!\n", ptserverlog);
204#endif // DEBUG
205        AWT_ptserver_selection::refresh_all();
206        last_ptserverlog_mod = ptserverlog_mod;
207    }
208
209    awr->add_timed_callback(PT_SERVER_TRACKLOG_TIMER, track_log_cb);
210}
211
212AWT_ptserver_selection::AWT_ptserver_selection(AW_selection_list *sellist_)
213    : AW_selection(sellist_)
214{
215    if (ptserver_selections.empty()) {
216        // first pt server selection list -> install log tracker
217        AW_root::SINGLETON->add_timed_callback(PT_SERVER_TRACKLOG_TIMER, track_log_cb);
218    }
219    ptserver_selections.push_back(this);
220}
221
222
223static void arb_tcp_dat_changed_cb(const char * /* path */, bool fileChanged, bool /* editorTerminated */) {
224    if (fileChanged) {
225        awt_refresh_all_pt_server_selection_lists();
226    }
227}
228
229void awt_edit_arbtcpdat_cb(AW_window *aww, AW_CL cl_gb_main) { 
230    GBDATA *gb_main  = (GBDATA*)cl_gb_main;
231    char   *filename = GB_arbtcpdat_path();
232
233    AW_edit(filename, arb_tcp_dat_changed_cb, aww, gb_main);
234    free(filename);
235}
236
237static char *readable_pt_servername(int index, int maxlength) {
238    char *fullname = GBS_ptserver_id_to_choice(index, 0);
239    if (!fullname) {
240#ifdef DEBUG
241        printf("awar given to awt_create_selection_list_on_pt_servers() does not contain a valid index\n");
242#endif
243        return strdup("-undefined-");
244    }
245
246    int len = strlen(fullname);
247    if (len <= maxlength) {
248        return fullname;
249    }
250
251    int remove  = len-maxlength;
252    fullname[0] = '.';
253    fullname[1] = '.';
254    strcpy(fullname+2, fullname+2+remove);
255
256    return fullname;
257}
258
259static void update_ptserver_button(AW_root *aw_root, AW_CL cl_varname) {
260    const char *varname              = (const char *)cl_varname;
261    char       *awar_buttontext_name = GBS_global_string_copy("/tmp/%s_BUTTON", varname);
262    char       *readable_name        = readable_pt_servername(aw_root->awar(varname)->read_int(), PT_SERVERNAME_LENGTH);
263
264    aw_root->awar(awar_buttontext_name)->write_string(readable_name);
265
266    free(readable_name);
267    free(awar_buttontext_name);
268}
269
270static AW_window *awt_popup_selection_list_on_pt_servers(AW_root *aw_root, const char *varname) {
271    AW_window_simple *aw_popup = new AW_window_simple;
272
273    aw_popup->init(aw_root, "SELECT_PT_SERVER", "Select a PT-Server");
274    aw_popup->auto_space(10, 10);
275
276    aw_popup->at_newline();
277    aw_popup->callback((AW_CB0)AW_POPDOWN);
278    AW_selection_list *sellist = aw_popup->create_selection_list(varname, 0, "", PT_SERVERNAME_SELLIST_WIDTH, 20);
279
280    aw_popup->at_newline();
281    aw_popup->callback((AW_CB0)AW_POPDOWN);
282    aw_popup->create_button("CLOSE", "CLOSE", "C");
283
284    aw_popup->window_fit();
285    aw_popup->recalc_pos_atShow(AW_REPOS_TO_MOUSE);
286
287    (new AWT_ptserver_selection(sellist))->refresh();
288
289    return aw_popup;
290}
291
292void awt_create_selection_list_on_pt_servers(AW_window *aws, const char *varname, bool popup) {
293    if (popup) {
294        AW_root *aw_root              = aws->get_root();
295        char    *awar_buttontext_name = GBS_global_string_copy("/tmp/%s_BUTTON", varname);
296        int      ptserver_index       = aw_root->awar(varname)->read_int();
297
298        if (ptserver_index<0) { // fix invalid pt_server indices
299            ptserver_index = 0;
300            aw_root->awar(varname)->write_int(ptserver_index);
301        }
302
303        char  *readable_name = readable_pt_servername(ptserver_index, PT_SERVERNAME_LENGTH);
304        AW_CL  cl_varname    = (AW_CL)strdup(varname); // make copy of awar_name for callbacks
305
306        aw_root->awar_string(awar_buttontext_name, readable_name, AW_ROOT_DEFAULT);
307        aw_root->awar(varname)->add_callback(update_ptserver_button, cl_varname);
308
309        int old_button_length = aws->get_button_length();
310
311        aws->button_length(PT_SERVERNAME_LENGTH+1);
312        aws->callback(AW_POPUP, (AW_CL)awt_popup_selection_list_on_pt_servers, cl_varname);
313        aws->create_button("CURR_PT_SERVER", awar_buttontext_name);
314
315        aws->button_length(old_button_length);
316
317        free(readable_name);
318        free(awar_buttontext_name);
319    }
320    else {
321        (new AWT_ptserver_selection(aws->create_selection_list(varname)))->refresh();
322    }
323}
324
325// ----------------------------------
326//      selection boxes on tables
327
328
329#if defined(WARN_TODO)
330#warning derive awt_sel_list_for_tables from AW_DB_selection
331#endif
332
333struct awt_sel_list_for_tables {
334    AW_window         *aws;
335    GBDATA            *gb_main;
336    AW_selection_list *id;
337    const char        *table_name;
338};
339
340static void awt_create_selection_list_on_tables_cb(GBDATA *, struct awt_sel_list_for_tables *cbs) {
341    cbs->id->clear();
342    GBDATA *gb_table;
343    for (gb_table = GBT_first_table(cbs->gb_main);
344         gb_table;
345         gb_table = GBT_next_table(gb_table)) {
346
347        GBDATA *gb_name = GB_entry(gb_table, "name");
348        GBDATA *gb_description = GB_search(gb_table, "description", GB_STRING);
349        if (!gb_name) continue;
350        char *table_name = GB_read_string(gb_name);
351        char *description = GB_read_string(gb_description);
352        const char *info_text = GBS_global_string("%s: %s", table_name, description);
353        cbs->id->insert(info_text, table_name);
354        free(table_name);
355        free(description);
356    }
357    cbs->id->insert_default("", "");
358    cbs->id->update();
359}
360
361void awt_create_selection_list_on_tables(GBDATA *gb_main, AW_window *aws, const char *varname)
362{
363    AW_selection_list *id;
364    GBDATA  *gb_table_data;
365    struct awt_sel_list_for_tables *cbs;
366    GB_push_transaction(gb_main);
367
368    id = aws->create_selection_list(varname, 0, "", 40, 8);
369    cbs = new awt_sel_list_for_tables;
370    cbs->aws = aws;
371    cbs->gb_main = gb_main;
372    cbs->id = id;
373
374    awt_create_selection_list_on_tables_cb(0, cbs);
375
376    gb_table_data = GB_search(gb_main, "table_data", GB_CREATE_CONTAINER);
377    GB_add_callback(gb_table_data, GB_CB_CHANGED, (GB_CB)awt_create_selection_list_on_tables_cb, (int *)cbs);
378
379    GB_pop_transaction(gb_main);
380}
381
382static void awt_create_selection_list_on_table_fields_cb(GBDATA *, struct awt_sel_list_for_tables *cbs) {
383    cbs->id->clear();
384    GBDATA  *gb_table = GBT_open_table(cbs->gb_main, cbs->table_name, true); // read only
385    GBDATA *gb_table_field;
386    for (gb_table_field = GBT_first_table_field(gb_table);
387         gb_table_field;
388         gb_table_field = GBT_next_table_field(gb_table_field))
389    {
390        GBDATA *gb_name = GB_entry(gb_table_field, "name");
391        GBDATA *gb_description = GB_search(gb_table_field, "description", GB_STRING);
392        if (!gb_name) continue;
393        char *table_name = GB_read_string(gb_name);
394        char *description = GB_read_string(gb_description);
395        const char *info_text = GBS_global_string("%s: %s", table_name, description);
396        cbs->id->insert(info_text, table_name);
397        free(table_name);
398        free(description);
399    }
400    cbs->id->insert_default("", "");
401    cbs->id->update();
402}
403
404void awt_create_selection_list_on_table_fields(GBDATA *gb_main, AW_window *aws, const char *table_name, const char *varname) {
405    // if tablename == 0 -> take fields from species table
406
407    AW_selection_list              *id;
408    struct awt_sel_list_for_tables *cbs;
409    GB_push_transaction(gb_main);
410
411    id = aws->create_selection_list(varname, 0, "", 40, 8);
412    cbs = new awt_sel_list_for_tables;
413    cbs->aws = aws;
414    cbs->gb_main = gb_main;
415    cbs->id = id;
416    cbs->table_name = strdup(table_name);
417
418    awt_create_selection_list_on_table_fields_cb(0, cbs);
419
420    GBDATA  *gb_table = GBT_open_table(gb_main, table_name, true); // read only
421    if (gb_table) {
422        GB_add_callback(gb_table, GB_CB_CHANGED, (GB_CB)awt_create_selection_list_on_table_fields_cb, (int *)cbs);
423    }
424    GB_pop_transaction(gb_main);
425}
426
427// -------------------------------------------------
428//      selection boxes on editor configurations
429
430class AWT_configuration_selection : public AW_DB_selection {
431public:
432    AWT_configuration_selection(AW_selection_list *sellist_, GBDATA *gb_configuration_data)
433        : AW_DB_selection(sellist_, gb_configuration_data)
434    {}
435
436    void fill() {
437        ConstStrArray config;
438        GBT_get_configuration_names(config, get_gb_main());
439
440        if (!config.empty()) {
441            for (int c = 0; config[c]; c++) insert(config[c], config[c]);
442        }
443        insert_default(DISPLAY_NONE, "????");
444    }
445};
446
447void awt_create_selection_list_on_configurations(GBDATA *gb_main, AW_window *aws, const char *varname) {
448    GBDATA *gb_configuration_data;
449    {
450        GB_transaction ta(gb_main);
451        gb_configuration_data = GB_search(gb_main, CONFIG_DATA_PATH, GB_CREATE_CONTAINER);
452    }
453    AW_selection_list *sellist = aws->create_selection_list(varname, 0, "", 40, 15);
454    (new AWT_configuration_selection(sellist, gb_configuration_data))->refresh();
455}
456
457char *awt_create_string_on_configurations(GBDATA *gb_main) {
458    // returns semicolon-separated string containing configuration names
459    // (or NULL if no configs exist)
460
461    GB_push_transaction(gb_main);
462
463    ConstStrArray config;
464    GBT_get_configuration_names(config, gb_main);
465
466    char *result = 0;
467
468    if (!config.empty()) {
469        GBS_strstruct *out = GBS_stropen(1000);
470        for (int c = 0; config[c]; c++) {
471            if (c>0) GBS_chrcat(out, ';');
472            GBS_strcat(out, config[c]);
473        }
474        result = GBS_strclose(out);
475    }
476
477    GB_pop_transaction(gb_main);
478    return result;
479}
480
481// ----------------------
482//      SAI selection
483
484
485class AWT_sai_selection : public AW_DB_selection { // derived from a Noncopyable
486    awt_sai_sellist_filter filter_poc;
487    AW_CL                  filter_cd;
488
489public:
490
491    AWT_sai_selection(AW_selection_list *sellist_, GBDATA *gb_sai_data, awt_sai_sellist_filter filter_poc_, AW_CL filter_cd_)
492        : AW_DB_selection(sellist_, gb_sai_data),
493          filter_poc(filter_poc_),
494          filter_cd(filter_cd_)
495    {}
496
497    void fill();
498};
499
500void AWT_sai_selection::fill() {
501    AW_selection_list *sel = get_sellist();
502    sel->clear();
503
504    GBDATA         *gb_main = get_gb_main();
505    GB_transaction  ta(gb_main);
506
507    for (GBDATA *gb_extended = GBT_first_SAI(gb_main);
508         gb_extended;
509         gb_extended = GBT_next_SAI(gb_extended))
510    {
511        if (filter_poc) {
512            char *res = filter_poc(gb_extended, filter_cd);
513            if (res) {
514                sel->insert(res, GBT_read_name(gb_extended));
515                free(res);
516            }
517        }
518        else {
519            const char *name     = GBT_read_name(gb_extended);
520            GBDATA     *gb_group = GB_entry(gb_extended, "sai_group");
521
522            if (gb_group) {
523                const char *group          = GB_read_char_pntr(gb_group);
524                char       *group_and_name = GBS_global_string_copy("[%s] %s", group, name);
525
526                sel->insert(group_and_name, name);
527                free(group_and_name);
528            }
529            else {
530                sel->insert(name, name);
531            }
532        }
533    }
534    sel->sort(false, false);
535
536    sel->insert_default(DISPLAY_NONE, "none");
537    sel->update();
538}
539
540void awt_selection_list_on_sai_update_cb(GBDATA *, AWT_sai_selection *saisel) {
541    /* update the selection box defined by awt_create_selection_list_on_sai
542     *
543     * useful only when filterproc is defined
544     * (changes to SAIs will automatically callback this function)
545     */
546
547    saisel->refresh();
548}
549
550AWT_sai_selection *SAI_selection_list_spec::create_list(AW_window *aws) const {
551    GB_transaction ta(gb_main);
552
553    AW_selection_list *sellist     = aws->create_selection_list(awar_name, 0, "", 40, 4);
554    GBDATA            *gb_sai_data = GBT_get_SAI_data(gb_main);
555    AWT_sai_selection *saisel      = new AWT_sai_selection(sellist, gb_sai_data, filter_poc, filter_cd);
556
557    awt_selection_list_on_sai_update_cb(0, saisel);
558    GB_add_callback(gb_sai_data, GB_CB_CHANGED, (GB_CB)awt_selection_list_on_sai_update_cb, (int *)saisel);
559
560    return saisel;
561}
562
563void awt_popup_filtered_sai_selection_list(AW_root *aw_root, AW_CL cl_sellist_spec) {
564    const SAI_selection_list_spec *spec      = (const SAI_selection_list_spec*)cl_sellist_spec;
565    const char                    *awar_name = spec->get_awar_name();
566   
567    static GB_HASH *SAI_window_hash       = 0;
568    if (!SAI_window_hash) SAI_window_hash = GBS_create_hash(10, GB_MIND_CASE);
569
570    AW_window_simple *aws = reinterpret_cast<AW_window_simple *>(GBS_read_hash(SAI_window_hash, awar_name));
571
572    if (!aws) {
573        aws = new AW_window_simple;
574        aws->init(aw_root, "SELECT_SAI", "SELECT SAI");
575        aws->load_xfig("select_simple.fig");
576
577        aws->at("selection");
578        aws->callback((AW_CB0)AW_POPDOWN);
579        spec->create_list(aws);
580
581        aws->at("button");
582        aws->callback(AW_POPDOWN);
583        aws->create_button("CLOSE", "CLOSE", "C");
584
585        aws->window_fit();
586
587        GBS_write_hash(SAI_window_hash, awar_name, reinterpret_cast<long>(aws));
588    }
589
590    aws->activate();
591}
592void awt_popup_filtered_sai_selection_list(AW_window *aww, AW_CL cl_sellist_spec) {
593    awt_popup_filtered_sai_selection_list(aww->get_root(), cl_sellist_spec);
594}
595
596void awt_popup_sai_selection_list(AW_root *aw_root, AW_CL cl_awar_name, AW_CL cl_gb_main) {
597    const char *awar_name = reinterpret_cast<const char *>(cl_awar_name);
598    GBDATA *gb_main = reinterpret_cast<GBDATA *>(cl_gb_main);
599
600    SAI_selection_list_spec spec(awar_name, gb_main);
601    awt_popup_filtered_sai_selection_list(aw_root, AW_CL(&spec));
602}
603
604void awt_popup_sai_selection_list(AW_window *aww, AW_CL cl_awar_name, AW_CL cl_gb_main) {
605    awt_popup_sai_selection_list(aww->get_root(), cl_awar_name, cl_gb_main);
606}
607
608AWT_sai_selection *awt_create_selection_list_on_sai(GBDATA *gb_main, AW_window *aws, const char *varname, awt_sai_sellist_filter filter_poc, AW_CL filter_cd) {
609    /* Selection list for SAIs
610     *
611     * if filter_proc is set then show only those items on which
612     * filter_proc returns a string (string must be a heap copy)
613     */
614    SAI_selection_list_spec spec(varname, gb_main);
615    spec.define_filter(filter_poc, filter_cd);
616    return spec.create_list(aws);
617}
618
619void awt_create_SAI_selection_button(GBDATA *gb_main, AW_window *aws, const char *varname, awt_sai_sellist_filter filter_poc, AW_CL filter_cd) {
620    SAI_selection_list_spec *spec = new SAI_selection_list_spec(varname, gb_main);
621    spec->define_filter(filter_poc, filter_cd);
622    aws->callback(awt_popup_filtered_sai_selection_list, AW_CL(spec));
623    aws->create_button("SELECT_SAI", varname);
624}
625
626// ******************** selection boxes on saving selection lists ********************
627
628static void create_save_box_for_selection_lists_save(AW_window *aws, AW_CL selidcd, AW_CL basenamecd)
629{
630    AW_selection_list *selid       = (AW_selection_list *)selidcd;
631    char              *awar_prefix = (char *)basenamecd;
632
633    char    *bline_anz = GBS_global_string_copy("%s/line_anz", awar_prefix);
634    AW_root *aw_root   = aws->get_root();
635    long     lineanz   = aw_root->awar(bline_anz)->read_int();
636    char    *filename  = AW_get_selected_fullname(aw_root, awar_prefix);
637
638    GB_ERROR error = selid->save(filename, lineanz);
639
640    if (!error) AW_refresh_fileselection(aw_root, awar_prefix);
641    aws->hide_or_notify(error);
642    free(filename);
643    free(bline_anz);
644}
645
646AW_window *create_save_box_for_selection_lists(AW_root *aw_root, AW_CL selid)
647{
648    AW_selection_list *selection_list = (AW_selection_list*)selid;
649
650    char *var_id         = GBS_string_2_key(selection_list->variable_name);
651    char *awar_base_name = GBS_global_string_copy("tmp/save_box_sel_%s", var_id); // don't free (passed to callback)
652    char *awar_line_anz  = GBS_global_string_copy("%s/line_anz", awar_base_name);
653    {
654        AW_create_fileselection_awars(aw_root, awar_base_name, ".", GBS_global_string("noname.list"), "list");
655        aw_root->awar_int(awar_line_anz, 0, AW_ROOT_DEFAULT);
656    }
657
658    AW_window_simple *aws       = new AW_window_simple;
659    char             *window_id = GBS_global_string_copy("SAVE_SELECTION_BOX_%s", var_id);
660
661    aws->init(aw_root, window_id, "SAVE BOX");
662    aws->load_xfig("sl_s_box.fig");
663
664    aws->at("close"); aws->callback((AW_CB0)AW_POPDOWN);
665    aws->create_button("CLOSE", "CLOSE", "C");
666
667    aws->at("save");
668    aws->highlight();
669    aws->callback(create_save_box_for_selection_lists_save, selid, (AW_CL)awar_base_name); // loose ownership of awar_base_name!
670    aws->create_button("SAVE", "SAVE", "S");
671
672    aws->at("nlines");
673    aws->create_option_menu(awar_line_anz, 0, "");
674    aws->insert_default_option("all", "a", 0);
675    aws->insert_option("50",    "a", 50);
676    aws->insert_option("100",   "a", 100);
677    aws->insert_option("500",   "a", 500);
678    aws->insert_option("1000",  "a", 1000);
679    aws->insert_option("5000",  "a", 5000);
680    aws->insert_option("10000", "a", 10000);
681    aws->update_option_menu();
682
683    AW_create_fileselection(aws, awar_base_name);
684
685    free(window_id);
686    free(awar_line_anz);
687    free(var_id);
688
689    aws->recalc_pos_atShow(AW_REPOS_TO_MOUSE);
690
691    return aws;
692}
693
694static void AWT_load_list(AW_window *aww, AW_CL sel_id, AW_CL ibase_name)
695{
696    AW_selection_list * selid       = (AW_selection_list *)sel_id;
697    char *basename = (char *)ibase_name;
698
699    AW_root     *aw_root    = aww->get_root();
700    GB_ERROR    error;
701
702    char *filename = AW_get_selected_fullname(aw_root, basename);
703    error          = selid->load(filename);
704
705    if (error) aw_message(error);
706
707    AW_POPDOWN(aww);
708
709    delete filename;
710}
711
712AW_window *create_load_box_for_selection_lists(AW_root *aw_root, AW_CL selid)
713{
714    char base_name[100];
715    sprintf(base_name, "tmp/load_box_sel_%li", (long)selid);
716
717    AW_create_fileselection_awars(aw_root, base_name, ".", "list", "");
718
719    AW_window_simple *aws = new AW_window_simple;
720    aws->init(aw_root, "LOAD_SELECTION_BOX", "Load box");
721    aws->load_xfig("sl_l_box.fig");
722
723    aws->at("close");
724    aws->callback((AW_CB0)AW_POPDOWN);
725    aws->create_button("CLOSE", "CLOSE", "C");
726
727    aws->at("load");
728    aws->highlight();
729    aws->callback(AWT_load_list, selid, (AW_CL)strdup(base_name));
730    aws->create_button("LOAD", "LOAD", "L");
731
732    AW_create_fileselection(aws, base_name);
733
734    aws->recalc_pos_atShow(AW_REPOS_TO_MOUSE);
735
736    return aws;
737}
738
739
740void create_print_box_for_selection_lists(AW_window *aw_window, AW_CL selid) {
741    AW_root           *aw_root = aw_window->get_root();
742    AW_selection_list *sellist = (AW_selection_list *)selid;
743    char              *data    = sellist->get_content_as_string(-1);
744    AWT_create_ascii_print_window(aw_root, data, "no title");
745    delete data;
746}
747
748
749
750AW_window *awt_create_load_box(AW_root *aw_root, const char *load_what, const char *file_extension, char **set_file_name_awar,
751                               void (*callback)(AW_window*),
752                               AW_window* (*create_popup)(AW_root *, AW_default))
753{
754    /* general purpose file selection box
755     *
756     * You can either provide a normal 'callback' or a 'create_popup'-callback
757     * (the not-used callback has to be NULL)
758     */
759
760
761    char *base_name = GBS_global_string_copy("tmp/load_box_%s", load_what);
762
763    AW_create_fileselection_awars(aw_root, base_name, ".", file_extension, "");
764
765    if (set_file_name_awar) {
766        *set_file_name_awar = GBS_global_string_copy("%s/file_name", base_name);
767    }
768
769    AW_window_simple *aws = new AW_window_simple;
770    {
771        char title[100];
772        sprintf(title, "Load %s", load_what);
773        aws->init(aw_root, title, title);
774        aws->load_xfig("load_box.fig");
775    }
776
777    aws->at("close");
778    aws->callback((AW_CB0)AW_POPDOWN);
779    aws->create_button("CLOSE", "CLOSE", "C");
780
781    aws->at("help");
782    aws->callback(AW_POPUP_HELP, (AW_CL)"");
783    aws->create_button("HELP", "HELP");
784
785    aws->at("go");
786    aws->highlight();
787
788    if (callback) {
789        awt_assert(!create_popup);
790        aws->callback((AW_CB0)callback);
791    }
792    else {
793        awt_assert(create_popup);
794        aws->callback((AW_CB1)AW_POPUP, (AW_CL)create_popup);
795    }
796
797    aws->create_button("LOAD", "LOAD", "L");
798
799    AW_create_fileselection(aws, base_name);
800    free(base_name);
801    aws->recalc_pos_atShow(AW_REPOS_TO_MOUSE);
802
803    return aws;
804}
805
806// --------------------------------------------------------------------------------
807
808#define SUBSET_NOELEM_DISPLAY "<none>"
809
810class AW_subset_selection : public AW_selection {
811    AW_selection_list& parent_sellist;
812
813    static void finish_fill_box(AW_selection_list *parent_sellist, AW_selection_list *sub_sellist) {
814        sub_sellist->insert_default(parent_sellist->get_default_display(), parent_sellist->get_default_value());
815        sub_sellist->update();
816    }
817
818    static AW_selection_list *create_box(AW_window *aww, AW_selection_list& parent_sellist) {
819        const char *parent_awar_name = parent_sellist.variable_name;
820        awt_assert(parent_awar_name[0] != '/');
821        awt_assert(parent_sellist.variable_type == AW_STRING); // only impl for strings
822
823        AW_root *aw_root   = aww->get_root();
824        char    *awar_name = GBS_global_string_copy("tmp/subsel/%s", parent_awar_name);
825
826        aw_root->awar_string(awar_name);
827
828        AW_selection_list *sub_sellist = aww->create_selection_list(awar_name);
829        finish_fill_box(&parent_sellist, sub_sellist);
830
831        free(awar_name);
832
833        return sub_sellist;
834    }
835
836public:
837    AW_subset_selection(AW_window *aww, AW_selection_list& parent_sellist_)
838        : AW_selection(create_box(aww, parent_sellist_)),
839          parent_sellist(parent_sellist_)
840    {}
841
842    AW_selection_list *get_parent_sellist() const { return &parent_sellist; }
843
844    const char *default_select_value() const { return parent_sellist.get_default_value(); }
845    const char *default_select_display() const { return parent_sellist.get_default_display(); }
846
847    void get_subset(StrArray& subset) {
848        get_sellist()->to_array(subset, true);
849    }
850
851    void fill() { awt_assert(0); } // unused
852
853    void collect_subset_cb(awt_collect_mode what) {
854        AW_selection_list *subset_list = get_sellist();
855        AW_selection_list *whole_list  = get_parent_sellist();
856
857        switch(what) {
858            case ACM_FILL:
859                for (AW_selection_list_iterator listEntry(whole_list); listEntry; ++listEntry) {
860                    if (subset_list->get_index_of(listEntry.get_value()) == -1) { // only add not already existing elements
861                        subset_list->insert(listEntry.get_displayed(), listEntry.get_value());
862                    }
863                }
864                finish_fill_box(whole_list, subset_list);
865                break;
866
867            case ACM_ADD: {
868                if (!whole_list->default_is_selected()) {
869                    const char *selected   = whole_list->get_awar_value();
870                    int         src_index = whole_list->get_index_of(selected);
871                   
872                    if (subset_list->get_index_of(selected) == -1) { // not yet in subset_list
873                        AW_selection_list_iterator entry(whole_list, src_index);
874                        subset_list->insert(entry.get_displayed(), entry.get_value());
875                        subset_list->update();
876                    }
877
878                    subset_list->set_awar_value(selected); // position right side to newly added or already existing alignment
879                    whole_list->select_element_at(src_index+1);    // go down 1 position on left side
880                }
881
882                break;
883            }
884            case ACM_REMOVE: {
885                if (!subset_list->default_is_selected()) {
886                    char *selected     = strdup(subset_list->get_awar_value());
887                    int   old_position = subset_list->get_index_of(selected);
888
889                    subset_list->delete_element_at(old_position);
890                    finish_fill_box(whole_list, subset_list);
891
892                    subset_list->select_element_at(old_position);
893                    whole_list->set_awar_value(selected); // set left selection to deleted alignment
894                    free(selected);
895                }
896                break;
897            }
898            case ACM_EMPTY:
899                subset_list->clear();
900                finish_fill_box(whole_list, subset_list);
901                break;
902        }
903    }
904    void reorder_subset_cb(awt_reorder_mode dest) {
905        AW_selection_list *subset_list = get_sellist();
906
907        if (!subset_list->default_is_selected()) {
908            const char *selected = subset_list->get_awar_value();
909
910            StrArray listContent;
911            subset_list->to_array(listContent, true);
912
913            int old_pos = GBT_names_index_of(listContent, selected);
914            if (old_pos >= 0) {
915                int new_pos;
916                switch (dest) {
917                    case ARM_TOP:    new_pos= 0;         break;
918                    case ARM_UP:     new_pos= old_pos-1; break;
919                    case ARM_DOWN:   new_pos= old_pos+1; break;
920                    case ARM_BOTTOM: new_pos= -1;        break;
921                }
922                if (old_pos != new_pos) {
923                    GBT_names_move(listContent, old_pos, new_pos);
924                    subset_list->init_from_array(listContent, subset_list->get_default_value());
925                }
926            }
927        }
928    }
929};
930
931static void collect_subset_cb(AW_window *, awt_collect_mode what, AW_CL cl_subsel) { ((AW_subset_selection*)cl_subsel)->collect_subset_cb(what); }
932static void reorder_subset_cb(AW_window *, awt_reorder_mode dest, AW_CL cl_subsel) { ((AW_subset_selection*)cl_subsel)->reorder_subset_cb(dest); }
933
934AW_selection *awt_create_subset_selection_list(AW_window *aww, AW_selection_list *parent_selection, const char *at_box, const char *at_add, const char *at_sort) {
935    awt_assert(parent_selection);
936
937    aww->at(at_box);
938    int x_list = aww->get_at_xposition();
939
940    AW_subset_selection *subsel = new AW_subset_selection(aww, *parent_selection);
941
942    aww->button_length(0);
943
944    aww->at(at_add);
945    int x_buttons = aww->get_at_xposition();
946
947    bool move_rightwards = x_list>x_buttons;
948    awt_create_collect_buttons(aww, move_rightwards, collect_subset_cb, (AW_CL)subsel);
949
950    aww->at(at_sort);
951    awt_create_order_buttons(aww, reorder_subset_cb, (AW_CL)subsel);
952
953    return subsel;
954}
955
956
Note: See TracBrowser for help on using the browser.