source: tags/arb-6.0/WINDOW/aw_select.hxx

Last change on this file was 12267, checked in by westram, 10 years ago
File size: 7.2 KB
Line 
1// ================================================================ //
2//                                                                  //
3//   File      : aw_select.hxx                                      //
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#ifndef AW_SELECT_HXX
13#define AW_SELECT_HXX
14
15#ifndef AW_WINDOW_HXX
16#include <aw_window.hxx>
17#endif
18#ifndef ARBDB_H
19#include <arbdb.h>
20#endif
21#ifndef AW_SCALAR_HXX
22#include "aw_scalar.hxx"
23#endif
24
25// cppcheck-suppress noConstructor
26class AW_selection_list_entry : virtual Noncopyable {
27    static const size_t MAX_DISPLAY_LENGTH = 8192;
28    // 8192 -> no wrap-around in motif (gtk may handle different value)
29    // 100000 -> works in motif (no crash, but ugly because line wraps around - overwriting itself; also happens in gtk)
30    // setting it to 750000 crashes with "X Error BadLength" in motif (when a string with that length is displayed)
31
32    char *displayed;
33
34    static char *copy_string_for_display(const char *str);
35
36public:
37    // @@@ make members private
38    AW_scalar  value;
39    bool is_selected;                                // internal use only
40    AW_selection_list_entry *next;
41
42    template<typename T>
43    AW_selection_list_entry(const char *display, T val)
44        : displayed(copy_string_for_display(display)),
45          value(val),
46          is_selected(false),
47          next(NULL)
48    {}
49    ~AW_selection_list_entry() { free(displayed); }
50
51    template<typename T>
52    void set_value(T val) { value = AW_scalar(val); }
53
54    const char *get_displayed() const { return displayed; } // may differ from string passed to set_displayed() if the string was longer than MAX_DISPLAY_LENGTH
55    void set_displayed(const char *displayed_) { freeset(displayed, copy_string_for_display(displayed_)); }
56};
57
58typedef int (*sellist_cmp_fun)(const char *disp1, const char *disp2);
59
60class AW_selection_list {
61    AW_selection_list_entry *get_entry_at(int index) const;
62
63    char             *variable_name;
64    AW_VARIABLE_TYPE  variable_type;
65
66public:
67    AW_selection_list(const char *variable_namei, int variable_typei, Widget select_list_widgeti);
68
69    Widget select_list_widget;
70
71    AW_selection_list_entry *list_table;
72    AW_selection_list_entry *last_of_list_table;
73    AW_selection_list_entry *default_select;
74    AW_selection_list      *next;
75
76    // ******************** real public ***************
77
78    const char *get_awar_name() const { return variable_name; }
79#if defined(ASSERTION_USED)
80    GB_TYPES get_awar_type() const { return GB_TYPES(variable_type); }
81#endif
82   
83    void selectAll();
84    void deselectAll();
85
86    size_t size();
87
88    void insert(const char *displayed, const char *value);
89    void insert_default(const char *displayed, const char *value);
90    void insert(const char *displayed, int32_t value);
91    void insert_default(const char *displayed, int32_t value);
92    void insert(const char *displayed, GBDATA *pointer);
93    void insert_default(const char *displayed, GBDATA *pointer);
94
95    void init_from_array(const CharPtrArray& entries, const char *defaultEntry);
96   
97    void update();
98    void refresh();
99
100    void sort(bool backward, bool case_sensitive); // uses displayed value!
101    void sortCustom(sellist_cmp_fun cmp);          // uses displayed value!
102
103    // ---------------------------------------------------
104    // the following functions work for string awars only:
105
106    const char *get_awar_value() const;
107    void set_awar_value(const char *new_value);
108
109    const char *get_default_value() const;
110    const char *get_default_display() const;
111
112    void select_default();
113
114    const char *get_selected_value() const; // may differ from get_awar_value() if default is selected (returns value passed to insert_default)
115
116    int get_index_of(const char *searched_value);
117    int get_index_of_displayed(const char *displayed);
118    int get_index_of_selected();
119
120    const char *get_value_at(int index);
121
122    void select_element_at(int wanted_index);
123    void move_selection(int offset);
124
125    bool default_is_selected() const;
126
127    void delete_element_at(int index);
128    void delete_value(const char *value);
129    void delete_default();
130    void clear();
131
132    void move_content_to(AW_selection_list *target_list);
133
134    void to_array(StrArray& array, bool values);
135    GB_HASH *to_hash(bool case_sens);
136    char *get_content_as_string(long number_of_lines); // displayed content (e.g. for printing)
137
138    // save/load:
139    void set_file_suffix(const char *suffix);
140    GB_ERROR load(const char *filename);
141    GB_ERROR save(const char *filename, long number_of_lines);
142};
143
144class AW_selection_list_iterator {
145    AW_selection_list_entry *entry;
146public:
147    AW_selection_list_iterator(AW_selection_list *sellist)
148        : entry(sellist->list_table)
149    {}
150    AW_selection_list_iterator(AW_selection_list *sellist, int index)
151        : entry(sellist->list_table)
152    {
153        aw_assert(index>=0);
154        forward(index);
155    }
156
157    operator bool() const { return entry; }
158
159    const char *get_displayed() { return entry ? entry->get_displayed() : NULL; }
160    const char *get_value() { return entry ? entry->value.get_string() : NULL; }
161
162    void set_displayed(const char *disp) { aw_assert(entry); entry->set_displayed(disp); }
163    void set_value(const char *val) { aw_assert(entry); entry->set_value(val); }
164
165    void forward(size_t offset) {
166        while (offset--) ++(*this);
167    }
168    AW_selection_list_iterator& operator++() {
169        if (entry) entry = entry->next;
170        return *this;
171    }
172};
173
174class AW_selection : virtual Noncopyable {
175    //! a AW_selection_list which knows how to fill itself
176    // (clients can't modify)
177
178    AW_selection_list *sellist;
179
180    virtual void fill() = 0;
181
182protected: 
183    void insert(const char *displayed, const char *value) { sellist->insert(displayed, value); }
184    void insert_default(const char *displayed, const char *value) { sellist->insert_default(displayed, value); }
185
186    void insert(const char *displayed, int32_t value) { sellist->insert(displayed, value); }
187    void insert_default(const char *displayed, int32_t value) { sellist->insert_default(displayed, value); }
188
189public:
190    AW_selection(AW_selection_list *sellist_) : sellist(sellist_) {}
191    virtual ~AW_selection() {}
192
193    void refresh();
194    AW_selection_list *get_sellist() { return sellist; }
195   
196    void get_values(StrArray& intoArray) { get_sellist()->to_array(intoArray, true); }
197    void get_displayed(StrArray& intoArray) { get_sellist()->to_array(intoArray, false); }
198};
199
200
201class AW_DB_selection : public AW_selection { // derived from a Noncopyable
202    GBDATA *gbd;                                    // root container of data displayed in selection list
203public:
204    AW_DB_selection(AW_selection_list *sellist_, GBDATA *gbd_);
205    virtual ~AW_DB_selection();
206   
207    GBDATA *get_gbd() { return gbd; }
208    GBDATA *get_gb_main() { return GB_get_root(gbd); }
209};
210
211
212#else
213#error aw_select.hxx included twice
214#endif // AW_SELECT_HXX
Note: See TracBrowser for help on using the repository browser.