source: branches/profile/WINDOW/AW_select.cxx

Last change on this file was 12438, checked in by westram, 10 years ago
  • activate errors on missing test-source location
    • publish all missing tests
    • missing location apparently only occurs
      • in dynamic libraries and
      • for last test of a module Note: the reverse is not true!
File size: 24.3 KB
Line 
1// ================================================================ //
2//                                                                  //
3//   File      : AW_select.cxx                                      //
4//   Purpose   :                                                    //
5//                                                                  //
6//   Coded by Ralf Westram (coder@reallysoft.de) in February 2010   //
7//   Institute of Microbiology (Technical University Munich)        //
8//   http://www.arb-home.de/                                        //
9//                                                                  //
10// ================================================================ //
11
12#include "aw_select.hxx"
13#include "aw_window_Xm.hxx"
14#include "aw_root.hxx"
15#include "aw_awar.hxx"
16
17#include <arb_strarray.h>
18#include <arb_strbuf.h>
19#include <arb_str.h>
20#include <arb_sort.h>
21#include <ad_cb.h>
22
23#include <Xm/List.h>
24
25__ATTR__NORETURN inline void selection_type_mismatch(const char *triedType) { type_mismatch(triedType, "selection-list"); }
26
27// --------------------------
28//      AW_selection_list
29
30
31AW_selection_list::AW_selection_list(const char *variable_namei, int variable_typei, Widget select_list_widgeti) {
32    // @@@ fix initialization
33    memset((char *)this, 0, sizeof(AW_selection_list));
34    variable_name          = nulldup(variable_namei);
35    variable_type          = (AW_VARIABLE_TYPE)variable_typei;
36    select_list_widget     = select_list_widgeti;
37    list_table             = NULL;
38    last_of_list_table     = NULL;
39    default_select         = NULL;
40}
41
42AW_selection_list::~AW_selection_list() {
43    clear();
44    free(variable_name);
45}
46
47inline XmString XmStringCreateSimple_wrapper(const char *text) {
48    return XmStringCreateSimple((char*)text);
49}
50
51void AW_selection_list::update() {
52    // Warning:
53    // update() will not set the connected awar to the default value
54    // if it contains a value which is not associated with a list entry!
55
56    size_t count = size();
57    if (default_select) count++;
58
59    XmString *strtab = new XmString[count];
60
61    count = 0;
62    for (AW_selection_list_entry *lt = list_table; lt; lt = lt->next) {
63        const char *s2 = lt->get_displayed();
64        if (!s2[0]) s2 = "  ";
65        strtab[count]  = XmStringCreateSimple_wrapper(s2);
66        count++;
67    }
68
69    if (default_select) {
70        const char *s2 = default_select->get_displayed();
71        if (!strlen(s2)) s2 = "  ";
72        strtab[count] = XmStringCreateSimple_wrapper(s2);
73        count++;
74    }
75    if (!count) {
76        strtab[count] = XmStringCreateSimple_wrapper("   ");
77        count ++;
78    }
79
80    XtVaSetValues(select_list_widget, XmNitemCount, count, XmNitems, strtab, NULL);
81
82    refresh();
83
84    for (size_t i=0; i<count; i++) XmStringFree(strtab[i]);
85    delete [] strtab;
86}
87
88void AW_selection_list::refresh() {
89    if (!variable_name) return;     // not connected to awar
90
91    AW_root *root  = AW_root::SINGLETON;
92    bool     found = false;
93    int      pos   = 0;
94    AW_awar *awar  = root->awar(variable_name);
95
96    AW_selection_list_entry *lt;
97
98    switch (variable_type) {
99        case AW_STRING: {
100            char *var_value = awar->read_string();
101            for (lt = list_table; lt; lt = lt->next) {
102                if (strcmp(var_value, lt->value.get_string()) == 0) {
103                    found = true;
104                    break;
105                }
106                pos++;
107            }
108            free(var_value);
109            break;
110        }
111        case AW_INT: {
112            int var_value = awar->read_int();
113            for (lt = list_table; lt; lt = lt->next) {
114                if (var_value == lt->value.get_int()) {
115                    found = true;
116                    break;
117                }
118                pos++;
119            }
120            break;
121        }
122        case AW_FLOAT: {
123            float var_value = awar->read_float();
124            for (lt = list_table; lt; lt = lt->next) {
125                if (var_value == lt->value.get_float()) {
126                    found = true;
127                    break;
128                }
129                pos++;
130            }
131            break;
132        }
133        case AW_POINTER: {
134            GBDATA *var_value = awar->read_pointer();
135            for (lt = list_table; lt; lt = lt->next) {
136                if (var_value == lt->value.get_pointer()) {
137                    found = true;
138                    break;
139                }
140                pos++;
141            }
142            break;
143        }
144        default:
145            aw_assert(0);
146            GB_warning("Unknown AWAR type");
147            break;
148    }
149
150    if (found || default_select) {
151        pos++;
152        int top;
153        int vis;
154        XtVaGetValues(select_list_widget,
155                      XmNvisibleItemCount, &vis,
156                      XmNtopItemPosition, &top,
157                      NULL);
158        XmListSelectPos(select_list_widget, pos, False);
159
160        if (pos < top) {
161            if (pos > 1) pos --;
162            XmListSetPos(select_list_widget, pos);
163        }
164        if (pos >= top + vis) {
165            XmListSetBottomPos(select_list_widget, pos + 1);
166        }
167    }
168    else {
169        GBK_terminatef("Selection list '%s' has no default selection", variable_name);
170    }
171}
172
173void AW_selection::refresh() {
174    get_sellist()->clear();
175    fill();
176    get_sellist()->update();
177}
178
179void AW_selection_list::clear() {
180    /** Remove all items from the list. Default item is removed as well.*/
181    while (list_table) {
182        AW_selection_list_entry *nextEntry = list_table->next;
183        delete list_table;
184        list_table = nextEntry;
185    }
186    list_table         = NULL;
187    last_of_list_table = NULL;
188
189    delete_default();
190}
191
192bool AW_selection_list::default_is_selected() const {
193    const char *sel = get_selected_value();
194    if (!sel) {
195        // (case should be impossible in gtk port)
196        return true; // handle "nothing" like default
197    }
198
199    const char *def = get_default_value();
200    return def && (strcmp(sel, def) == 0);
201}
202
203const char *AW_selection_list::get_selected_value() const {
204    int                      i;
205    AW_selection_list_entry *lt;
206    AW_selection_list_entry *found = 0;
207
208    aw_assert(select_list_widget);
209   
210    for (i=1, lt = list_table; lt; i++, lt = lt->next) {
211        lt->is_selected = XmListPosSelected(select_list_widget, i);
212        if (lt->is_selected && !found) found = lt;
213    }
214
215    if (default_select) {
216        default_select->is_selected = XmListPosSelected(select_list_widget, i);
217        if (default_select->is_selected && !found) found = default_select;
218    }
219    return found ? found->value.get_string() : NULL;
220}
221
222AW_selection_list_entry *AW_selection_list::get_entry_at(int index) const {
223    AW_selection_list_entry *entry = list_table;
224    while (index && entry) {
225        entry = entry->next;
226        index--;
227    }
228    return entry;
229}
230
231
232void AW_selection_list::select_default() {
233    set_awar_value(get_default_value());
234}
235
236void AW_selection_list::delete_element_at(const int index) {
237    if (index<0) return;
238
239    AW_selection_list_entry *prev = NULL;
240    if (index>0) {
241        prev = get_entry_at(index-1);
242        if (!prev) return; // invalid index
243    }
244
245    int selected_index = get_index_of_selected();
246    if (index == selected_index) select_default();
247
248    AW_selection_list_entry *toDel = prev ? prev->next : list_table;
249    aw_assert(toDel != default_select);
250
251    (prev ? prev->next : list_table) = toDel->next;
252    delete toDel;
253
254    if (last_of_list_table == toDel) last_of_list_table = prev;
255}
256
257void AW_selection_list::delete_value(const char *value) {
258    int index = get_index_of(value);
259    delete_element_at(index);
260}
261
262const char *AW_selection_list::get_awar_value() const {
263    AW_awar *awar = AW_root::SINGLETON->awar(variable_name);
264    return awar->read_char_pntr();
265}
266
267char *AW_selection_list::get_content_as_string(long number_of_lines) {
268    // number_of_lines == 0 -> print all
269
270    AW_selection_list_entry *lt;
271    GBS_strstruct *fd = GBS_stropen(10000);
272
273    for (lt = list_table; lt; lt = lt->next) {
274        number_of_lines--;
275        GBS_strcat(fd, lt->get_displayed());
276        GBS_chrcat(fd, '\n');
277        if (!number_of_lines) break;
278    }
279    return GBS_strclose(fd);
280}
281
282const char *AW_selection_list::get_default_display() const {
283    return default_select ? default_select->get_displayed() : NULL;
284}
285
286const char *AW_selection_list::get_default_value() const {
287    return default_select ? default_select->value.get_string() : NULL;
288}
289
290int AW_selection_list::get_index_of(const char *searched_value) {
291    /*! get index of an entry in the selection list
292     * @return 0..n-1 index of matching element (or -1)
293     */
294    int element_index = 0;
295    for (AW_selection_list_iterator entry(this); entry; ++entry) {
296        if (strcmp(entry.get_value(), searched_value) == 0) return element_index;
297        ++element_index;
298    }
299    return -1;
300}
301
302int AW_selection_list::get_index_of_selected() {
303    // returns index of element (or index of default)
304    const char *awar_value = get_awar_value();
305    return get_index_of(awar_value);
306}
307
308void AW_selection_list::init_from_array(const CharPtrArray& entries, const char *default_displayed, const char *default_value) {
309    // update selection list with contents of NULL-terminated array 'entries'
310    //
311    // 'default_displayed' and 'default_value' are used as default selection.
312    // To position the default selection, add 'default_value' to 'entries' as well.
313    //
314    // awar value will be changed to 'defaultEntry' if it does not match any other entry
315    //
316    // Note: This works only with selection lists bound to AW_STRING awars.
317
318    aw_assert(default_displayed);
319    aw_assert(default_value);
320
321    // use copies (just in case default_* points to a value free'd by clear())
322    char *defaultDispCopy  = strdup(default_displayed);
323    char *defaultValueCopy = strdup(default_value);
324
325    bool defInserted = false;
326
327    clear();
328    for (int i = 0; entries[i]; ++i) {
329        if (!defInserted && strcmp(entries[i], defaultValueCopy) == 0) {
330            insert_default(defaultDispCopy, defaultValueCopy);
331            defInserted = true;
332        }
333        else {
334            insert(entries[i], entries[i]);
335        }
336    }
337    if (!defInserted) insert_default(defaultDispCopy, defaultValueCopy);
338    update();
339
340    const char *selected = get_selected_value();
341    if (selected) set_awar_value(selected);
342
343    free(defaultValueCopy);
344    free(defaultDispCopy);
345}
346
347void AW_selection_list::insert(const char *displayed, const char *value) {
348    if (variable_type != AW_STRING) {
349        selection_type_mismatch("string");
350        return;
351    }
352
353    if (list_table) {
354        last_of_list_table->next = new AW_selection_list_entry(displayed, value);
355        last_of_list_table = last_of_list_table->next;
356        last_of_list_table->next = NULL;
357    }
358    else {
359        last_of_list_table = list_table = new AW_selection_list_entry(displayed, value);
360    }
361}
362
363void AW_selection_list::delete_default() {
364    /** Removes the default entry from the list*/
365    if (default_select) {
366        delete default_select;
367        default_select = NULL;
368    }
369}
370
371void AW_selection_list::insert_default(const char *displayed, const char *value) {
372    if (variable_type != AW_STRING) {
373        selection_type_mismatch("string");
374        return;
375    }
376    if (default_select) delete_default();
377    default_select = new AW_selection_list_entry(displayed, value);
378}
379
380void AW_selection_list::insert(const char *displayed, int32_t value) {
381    if (variable_type != AW_INT) {
382        selection_type_mismatch("int");
383        return;
384    }
385    if (list_table) {
386        last_of_list_table->next = new AW_selection_list_entry(displayed, value);
387        last_of_list_table = last_of_list_table->next;
388        last_of_list_table->next = NULL;
389    }
390    else {
391        last_of_list_table = list_table = new AW_selection_list_entry(displayed, value);
392    }
393}
394
395void AW_selection_list::insert_default(const char *displayed, int32_t value) {
396    if (variable_type != AW_INT) {
397        selection_type_mismatch("int");
398        return;
399    }
400    if (default_select) delete_default();
401    default_select = new AW_selection_list_entry(displayed, value);
402}
403
404
405void AW_selection_list::insert(const char *displayed, GBDATA *pointer) {
406    if (variable_type != AW_POINTER) {
407        selection_type_mismatch("pointer");
408        return;
409    }
410    if (list_table) {
411        last_of_list_table->next = new AW_selection_list_entry(displayed, pointer);
412        last_of_list_table = last_of_list_table->next;
413        last_of_list_table->next = NULL;
414    }
415    else {
416        last_of_list_table = list_table = new AW_selection_list_entry(displayed, pointer);
417    }
418}
419
420void AW_selection_list::insert_default(const char *displayed, GBDATA *pointer) {
421    if (variable_type != AW_POINTER) {
422        selection_type_mismatch("pointer");
423        return;
424    }
425    if (default_select) delete_default();
426    default_select = new AW_selection_list_entry(displayed, pointer);
427}
428
429
430void AW_selection_list::move_content_to(AW_selection_list *target_list) {
431    //! move all entries (despite default entry) to another AW_selection_list
432
433    if (default_select) {
434        char *defDisp = strdup(default_select->get_displayed());
435        char *defVal  = strdup(default_select->value.get_string());
436
437        delete_default();
438        move_content_to(target_list);
439        insert_default(defDisp, defVal);
440
441        free(defVal);
442        free(defDisp);
443    }
444    else {
445        AW_selection_list_entry *entry = list_table;
446        while (entry) {
447            if (!target_list->list_table) {
448                target_list->last_of_list_table = target_list->list_table = new AW_selection_list_entry(entry->get_displayed(), entry->value.get_string());
449            }
450            else {
451                target_list->last_of_list_table->next = new AW_selection_list_entry(entry->get_displayed(), entry->value.get_string());
452                target_list->last_of_list_table = target_list->last_of_list_table->next;
453                target_list->last_of_list_table->next = NULL;
454            }
455            entry = entry->next;
456        }
457        clear();
458    }
459}
460
461void AW_selection_list::move_selection(int offset) {
462    /*! move selection 'offset' position
463     *  offset == 1  -> select next element
464     *  offset == -1 -> select previous element
465     */
466
467    int index = get_index_of_selected();
468    select_element_at(index+offset);
469}
470
471const char *AW_selection_list::get_value_at(int index) {
472    // get value of the entry at position 'index' [0..n-1] of the 'selection_list'
473    // returns NULL if index is out of bounds
474    AW_selection_list_entry *entry = get_entry_at(index);
475    return entry ? entry->value.get_string() : NULL;
476}
477
478void AW_selection_list::select_element_at(int wanted_index) {
479    const char *wanted_value = get_value_at(wanted_index);
480
481    if (!wanted_value) {
482        wanted_value = get_default_value();
483        if (!wanted_value) wanted_value = "";
484    }
485
486    set_awar_value(wanted_value);
487}
488
489void AW_selection_list::set_awar_value(const char *new_value) {
490    AW_awar *awar = AW_root::SINGLETON->awar(variable_name);
491    awar->write_string(new_value);
492}
493
494void AW_selection_list::set_file_suffix(const char *suffix) {
495    AW_root *aw_root = AW_root::SINGLETON;
496    char     filter[200];
497    sprintf(filter, "tmp/save_box_sel_%li/filter", (long)this);
498    aw_root->awar_string(filter, suffix);
499    sprintf(filter, "tmp/load_box_sel_%li/filter", (long)this);
500    aw_root->awar_string(filter, suffix);
501}
502
503size_t AW_selection_list::size() {
504    AW_selection_list_entry *lt    = list_table;
505    size_t                  count = 0;
506
507    while (lt) {
508        ++count;
509        lt = lt->next;
510    }
511    return count;
512}
513
514static int sel_sort_backward(const char *d1, const char *d2) { return strcmp(d2, d1); }
515static int sel_isort_backward(const char *d1, const char *d2) { return ARB_stricmp(d2, d1); }
516static int gb_compare_function__2__sellist_cmp_fun(const void *t1, const void *t2, void *v_selcmp) {
517    sellist_cmp_fun selcmp = (sellist_cmp_fun)v_selcmp;
518    return selcmp(static_cast<const AW_selection_list_entry*>(t1)->get_displayed(),
519                  static_cast<const AW_selection_list_entry*>(t2)->get_displayed());
520}
521
522void AW_selection_list::sortCustom(sellist_cmp_fun cmp) {
523    // WARNING: function behaves different in motif and gtk:
524    // gtk also sorts default-element, motif always places default-element @ bottom
525    size_t count = size();
526    if (count) {
527        AW_selection_list_entry **tables = new AW_selection_list_entry *[count];
528        count = 0;
529        for (AW_selection_list_entry *lt = list_table; lt; lt = lt->next) {
530            tables[count++] = lt;
531        }
532
533        GB_sort((void**)tables, 0, count, gb_compare_function__2__sellist_cmp_fun, (void*)cmp);
534
535        size_t i;
536        for (i=0; i<count-1; i++) {
537            tables[i]->next = tables[i+1];
538        }
539        tables[i]->next = 0;
540        list_table = tables[0];
541        last_of_list_table = tables[i];
542
543        delete [] tables;
544    }
545}
546
547void AW_selection_list::sort(bool backward, bool case_sensitive) {
548    // WARNING: function behaves different in motif and gtk:
549    // gtk also sorts default-element, motif always places default-element @ bottom
550    sellist_cmp_fun cmp;
551    if (backward) {
552        if (case_sensitive) cmp = sel_sort_backward;
553        else cmp                = sel_isort_backward;
554    }
555    else {
556        if (case_sensitive) cmp = strcmp;
557        else cmp                = ARB_stricmp;
558    }
559    sortCustom(cmp);
560}
561
562void AW_selection_list::to_array(StrArray& array, bool values) {
563    /*! read contents of selection list into an array.
564     * @param values true->read values, false->read displayed strings
565     * Use GBT_free_names() to free the result.
566     *
567     * Note: if 'values' is true, this function only works for string selection lists!
568     */
569
570    array.reserve(size());
571
572    for (AW_selection_list_entry *lt = list_table; lt; lt = lt->next) {
573        array.put(strdup(values ? lt->value.get_string() : lt->get_displayed()));
574    }
575    aw_assert(array.size() == size());
576}
577
578GB_HASH *AW_selection_list::to_hash(bool case_sens) {
579    // creates a hash (key = value of selection list, value = display string from selection list)
580    // (Warning: changing the selection list will render the hash invalid!)
581
582    GB_HASH *hash = GBS_create_hash(size(), case_sens ? GB_MIND_CASE : GB_IGNORE_CASE);
583
584    for (AW_selection_list_entry *lt = list_table; lt; lt = lt->next) {
585        GBS_write_hash(hash, lt->value.get_string(), (long)lt->get_displayed());
586    }
587
588    return hash;
589}
590
591char *AW_selection_list_entry::copy_string_for_display(const char *str) {
592    size_t  len     = strlen(str);
593    bool    tooLong = len>MAX_DISPLAY_LENGTH;
594    char   *out;
595    if (tooLong) {
596        out = GB_strndup(str, MAX_DISPLAY_LENGTH);
597        { // add message about truncation
598            char   *truncated = GBS_global_string_copy(" <truncated - original contains %zu byte>", len);
599            size_t  tlen      = strlen(truncated);
600            aw_assert(MAX_DISPLAY_LENGTH>tlen);
601            memcpy(out+MAX_DISPLAY_LENGTH-tlen, truncated, tlen);
602        }
603        len = MAX_DISPLAY_LENGTH;
604    }
605    else {
606        out = GB_strduplen(str, len);
607    }
608
609    for (size_t i = 0; i<len; ++i) {
610        switch (out[i]) {
611            case ',':   out[i] = ';'; break;
612            case '\n':  out[i] = '#'; break;
613        }
614    }
615    return out;
616}
617
618// -------------------------
619//      AW_DB_selection
620
621static void AW_DB_selection_refresh_cb(GBDATA *, AW_DB_selection *selection) {
622    selection->refresh();
623}
624
625AW_DB_selection::AW_DB_selection(AW_selection_list *sellist_, GBDATA *gbd_)
626    : AW_selection(sellist_)
627    , gbd(gbd_)
628{
629    GB_transaction ta(gbd);
630    GB_add_callback(gbd, GB_CB_CHANGED, makeDatabaseCallback(AW_DB_selection_refresh_cb, this));
631}
632
633AW_DB_selection::~AW_DB_selection() {
634    GB_transaction ta(gbd);
635    GB_remove_callback(gbd, GB_CB_CHANGED, makeDatabaseCallback(AW_DB_selection_refresh_cb, this));
636}
637
638GBDATA *AW_DB_selection::get_gb_main() {
639    return GB_get_root(gbd);
640}
641
642// --------------------------------------------------------------------------------
643
644#ifdef UNIT_TESTS
645#ifndef TEST_UNIT_H
646#include <test_unit.h>
647#endif
648
649#define TEST_LIST_CONTENT(list,values,expected) do {    \
650        StrArray a;                                     \
651        (list).to_array(a, values);                     \
652        char *str = GBT_join_names(a, ';');             \
653        TEST_EXPECT_EQUAL(str, expected);               \
654        free(str);                                      \
655    } while(0)
656
657#define TEST_GET_LIST_CONTENT(list,expected) do {       \
658        char *str = (list).get_content_as_string(10);   \
659        TEST_EXPECT_EQUAL(str, expected);               \
660        free(str);                                      \
661    } while(0)
662
663void TEST_selection_list_access() {
664#if defined(ARB_GTK)
665    AW_selection_list list0(NULL, false);
666    AW_selection_list list1(NULL, false);
667    AW_selection_list list2(NULL, false);
668#else // ARB_MOTIF
669    AW_selection_list list0("bla", GB_STRING, NULL);
670    AW_selection_list list1("bla", GB_STRING, NULL);
671    AW_selection_list list2("alb", GB_STRING, NULL);
672#endif
673
674    list0.insert_default("First", "1st");
675    list0.insert("Second", "2nd");
676    list0.insert("Third", "3rd");
677
678    list1.insert("First", "1st");
679    list1.insert_default("Second", "2nd");
680    list1.insert("Third", "3rd");
681
682    list2.insert("First", "1st");
683    list2.insert("Second", "2nd");
684    list2.insert("Third", "3rd");
685    list2.insert_default("Default", "");
686
687    TEST_EXPECT_EQUAL(list0.size(), 2);
688    TEST_EXPECT_EQUAL(list1.size(), 2);
689    TEST_EXPECT_EQUAL(list2.size(), 3);
690
691    TEST_EXPECT_EQUAL(list1.get_default_value(), "2nd");
692    TEST_EXPECT_EQUAL(list1.get_default_display(), "Second");
693
694    TEST_EXPECT_EQUAL(list0.get_index_of("1st"), -1); // default value is not indexed
695    TEST_EXPECT_EQUAL(list0.get_index_of("2nd"), 0);
696    TEST_EXPECT_EQUAL(list0.get_index_of("3rd"), 1);  // = second non-default entry
697
698    TEST_EXPECT_EQUAL(list1.get_index_of("1st"), 0);
699    TEST_EXPECT_EQUAL(list1.get_index_of("2nd"), -1); // default value is not indexed
700    TEST_EXPECT_EQUAL(list1.get_index_of("3rd"), 1);  // = second non-default entry
701
702    TEST_EXPECT_EQUAL(list2.get_index_of("1st"), 0);
703    TEST_EXPECT_EQUAL(list2.get_index_of("2nd"), 1);
704    TEST_EXPECT_EQUAL(list2.get_index_of("3rd"), 2);
705
706
707    TEST_EXPECT_EQUAL(list0.get_value_at(0), "2nd");
708    TEST_EXPECT_EQUAL(list0.get_value_at(1), "3rd");
709    TEST_EXPECT_NULL(list0.get_value_at(2));
710
711    TEST_EXPECT_EQUAL(list1.get_value_at(0), "1st");
712    TEST_EXPECT_EQUAL(list1.get_value_at(1), "3rd");
713    TEST_EXPECT_NULL(list1.get_value_at(2));
714
715    TEST_EXPECT_EQUAL(list2.get_value_at(0), "1st");
716    TEST_EXPECT_EQUAL(list2.get_value_at(1), "2nd");
717    TEST_EXPECT_EQUAL(list2.get_value_at(2), "3rd");
718    TEST_EXPECT_NULL(list2.get_value_at(3));
719
720    TEST_LIST_CONTENT(list1, true,  "1st;3rd");
721    TEST_LIST_CONTENT(list1, false, "First;Third");
722    TEST_GET_LIST_CONTENT(list1,    "First\nThird\n");
723
724    TEST_LIST_CONTENT(list2, true,  "1st;2nd;3rd");
725    TEST_LIST_CONTENT(list2, false, "First;Second;Third");
726    TEST_GET_LIST_CONTENT(list2,    "First\nSecond\nThird\n");
727
728    {
729        AW_selection_list_iterator iter1(&list1);
730        AW_selection_list_iterator iter2(&list2);
731
732        TEST_EXPECT(bool(iter1));
733        TEST_EXPECT(bool(iter2));
734
735        TEST_EXPECT_EQUAL(iter1.get_displayed(), "First");
736        TEST_EXPECT_EQUAL(iter2.get_displayed(), "First");
737
738        TEST_EXPECT_EQUAL(iter1.get_value(), "1st");
739        TEST_EXPECT_EQUAL(iter2.get_value(), "1st");
740
741        ++iter1;
742        ++iter2;
743
744        TEST_EXPECT(bool(iter1));
745        TEST_EXPECT(bool(iter2));
746
747        TEST_EXPECT_EQUAL(iter1.get_displayed(), "Third");
748        TEST_EXPECT_EQUAL(iter2.get_displayed(), "Second");
749
750        TEST_EXPECT_EQUAL(iter1.get_value(), "3rd");
751        TEST_EXPECT_EQUAL(iter2.get_value(), "2nd");
752
753        ++iter1;
754        ++iter2;
755
756        TEST_REJECT(bool(iter1));
757        TEST_EXPECT(bool(iter2));
758    }
759
760    {
761#if defined(ARB_GTK)
762        AW_selection_list copy1(NULL, false);
763        AW_selection_list copy2(NULL, false);
764#else // ARB_MOTIF
765        AW_selection_list copy1("c1", GB_STRING, NULL);
766        AW_selection_list copy2("c2", GB_STRING, NULL);
767#endif
768
769        list1.move_content_to(&copy1);
770        list2.move_content_to(&copy2);
771
772        TEST_EXPECT_EQUAL(list1.size(), 0);
773        TEST_EXPECT_EQUAL(list2.size(), 0);
774
775        TEST_LIST_CONTENT(copy1, true, "1st;3rd");
776        TEST_LIST_CONTENT(copy2, true, "1st;2nd;3rd");
777    }
778}
779TEST_PUBLISH(TEST_selection_list_access);
780
781#endif // UNIT_TESTS
782
783// --------------------------------------------------------------------------------
784
Note: See TracBrowser for help on using the repository browser.