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 |
---|
26 | class AW_selection_list_entry : virtual Noncopyable { |
---|
27 | #if defined(ARB_GTK) |
---|
28 | static const size_t MAX_DISPLAY_LENGTH = 599000; // 599000 -> no wrap-around happens in gtk |
---|
29 | #else // ARB_MOTIF |
---|
30 | static const size_t MAX_DISPLAY_LENGTH = 8192; // 8192 -> no wrap-around happens in motif |
---|
31 | #endif |
---|
32 | // 100000 -> works in motif (no crash, but ugly because line wraps around - overwriting itself; also happens in gtk, e.g. for len=600000) |
---|
33 | // setting it to 750000 crashes with "X Error BadLength" in motif (when a string with that length is displayed) |
---|
34 | |
---|
35 | char *displayed; |
---|
36 | |
---|
37 | static char *copy_string_for_display(const char *str); |
---|
38 | |
---|
39 | public: |
---|
40 | // @@@ make members private |
---|
41 | AW_scalar value; |
---|
42 | bool is_selected; // internal use only |
---|
43 | AW_selection_list_entry *next; |
---|
44 | |
---|
45 | template<typename T> |
---|
46 | AW_selection_list_entry(const char *display, T val) |
---|
47 | : displayed(copy_string_for_display(display)), |
---|
48 | value(val), |
---|
49 | is_selected(false), |
---|
50 | next(NULL) |
---|
51 | {} |
---|
52 | ~AW_selection_list_entry() { free(displayed); } |
---|
53 | |
---|
54 | template<typename T> |
---|
55 | void set_value(T val) { value = AW_scalar(val); } |
---|
56 | |
---|
57 | const char *get_displayed() const { return displayed; } // may differ from string passed to set_displayed() if the string was longer than MAX_DISPLAY_LENGTH |
---|
58 | void set_displayed(const char *displayed_) { freeset(displayed, copy_string_for_display(displayed_)); } |
---|
59 | }; |
---|
60 | |
---|
61 | typedef int (*sellist_cmp_fun)(const char *disp1, const char *disp2); |
---|
62 | |
---|
63 | class AW_selection_list : virtual Noncopyable { |
---|
64 | AW_selection_list_entry *get_entry_at(int index) const; |
---|
65 | |
---|
66 | char *variable_name; |
---|
67 | AW_VARIABLE_TYPE variable_type; |
---|
68 | |
---|
69 | public: |
---|
70 | AW_selection_list(const char *variable_namei, int variable_typei, Widget select_list_widgeti); |
---|
71 | ~AW_selection_list(); |
---|
72 | |
---|
73 | Widget select_list_widget; |
---|
74 | |
---|
75 | AW_selection_list_entry *list_table; |
---|
76 | AW_selection_list_entry *last_of_list_table; |
---|
77 | AW_selection_list_entry *default_select; |
---|
78 | AW_selection_list *next; |
---|
79 | |
---|
80 | // ******************** real public *************** |
---|
81 | |
---|
82 | const char *get_awar_name() const { return variable_name; } |
---|
83 | #if defined(ASSERTION_USED) |
---|
84 | GB_TYPES get_awar_type() const { return GB_TYPES(variable_type); } |
---|
85 | #endif |
---|
86 | |
---|
87 | size_t size(); |
---|
88 | |
---|
89 | void insert(const char *displayed, const char *value); |
---|
90 | void insert_default(const char *displayed, const char *value); |
---|
91 | void insert(const char *displayed, int32_t value); |
---|
92 | void insert_default(const char *displayed, int32_t value); |
---|
93 | void insert(const char *displayed, GBDATA *pointer); |
---|
94 | void insert_default(const char *displayed, GBDATA *pointer); |
---|
95 | |
---|
96 | void init_from_array(const CharPtrArray& entries, const char *default_displayed, const char *default_value); |
---|
97 | |
---|
98 | void update(); |
---|
99 | void refresh(); |
---|
100 | |
---|
101 | void sort(bool backward, bool case_sensitive); // uses displayed value! |
---|
102 | void sortCustom(sellist_cmp_fun cmp); // uses displayed value! |
---|
103 | |
---|
104 | // --------------------------------------------------- |
---|
105 | // the following functions work for string awars only: |
---|
106 | |
---|
107 | const char *get_awar_value() const; |
---|
108 | void set_awar_value(const char *new_value); |
---|
109 | |
---|
110 | const char *get_default_value() const; |
---|
111 | const char *get_default_display() const; |
---|
112 | |
---|
113 | void select_default(); |
---|
114 | |
---|
115 | const char *get_selected_value() const; // may differ from get_awar_value() if default is selected (returns value passed to insert_default) |
---|
116 | |
---|
117 | int get_index_of(const char *searched_value); |
---|
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 | |
---|
144 | class AW_selection_list_iterator { |
---|
145 | AW_selection_list_entry *entry; |
---|
146 | public: |
---|
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 | |
---|
174 | class 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 | |
---|
182 | protected: |
---|
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 | |
---|
189 | public: |
---|
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 | |
---|
201 | class AW_DB_selection : public AW_selection { // derived from a Noncopyable |
---|
202 | GBDATA *gbd; // root container of data displayed in selection list |
---|
203 | public: |
---|
204 | AW_DB_selection(AW_selection_list *sellist_, GBDATA *gbd_); |
---|
205 | virtual ~AW_DB_selection() OVERRIDE; |
---|
206 | |
---|
207 | GBDATA *get_gbd() { return gbd; } |
---|
208 | GBDATA *get_gb_main(); |
---|
209 | }; |
---|
210 | |
---|
211 | |
---|
212 | #else |
---|
213 | #error aw_select.hxx included twice |
---|
214 | #endif // AW_SELECT_HXX |
---|