| 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 | char *displayed; |
|---|
| 28 | AW_scalar value; |
|---|
| 29 | |
|---|
| 30 | static char *copy_string_for_display(const char *str); |
|---|
| 31 | |
|---|
| 32 | public: |
|---|
| 33 | // @@@ make members private |
|---|
| 34 | AW_selection_list_entry *next; |
|---|
| 35 | |
|---|
| 36 | static const size_t MAX_DISPLAY_LENGTH = 8192; // 8192 -> no wrap-around happens in motif |
|---|
| 37 | // 100000 -> works in motif (no crash, but ugly because line wraps around - overwriting itself) |
|---|
| 38 | // setting it to 750000 crashes with "X Error BadLength" in motif (when a string with that length is displayed) |
|---|
| 39 | |
|---|
| 40 | template<typename T> |
|---|
| 41 | AW_selection_list_entry(const char *display, T val) |
|---|
| 42 | : displayed(copy_string_for_display(display)), |
|---|
| 43 | value(val), |
|---|
| 44 | next(NULp) |
|---|
| 45 | {} |
|---|
| 46 | ~AW_selection_list_entry() { free(displayed); } |
|---|
| 47 | |
|---|
| 48 | const AW_scalar& get_value() const { return value; } |
|---|
| 49 | void set_value(const AW_scalar& val) { value = val; } |
|---|
| 50 | |
|---|
| 51 | const char *get_displayed() const { return displayed; } // may differ from string passed to set_displayed() if the string was longer than MAX_DISPLAY_LENGTH |
|---|
| 52 | void set_displayed(const char *displayed_) { freeset(displayed, copy_string_for_display(displayed_)); } |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | typedef int (*sellist_cmp_fun)(const char *disp1, const char *disp2); |
|---|
| 56 | typedef void (*sellist_update_cb)(AW_selection_list *, AW_CL); |
|---|
| 57 | |
|---|
| 58 | class AW_selection_list : virtual Noncopyable { |
|---|
| 59 | AW_selection_list_entry *get_entry_at(int index) const; |
|---|
| 60 | |
|---|
| 61 | char *variable_name; |
|---|
| 62 | AW_VARIABLE_TYPE variable_type; |
|---|
| 63 | |
|---|
| 64 | sellist_update_cb update_cb; |
|---|
| 65 | AW_CL cl_update; |
|---|
| 66 | |
|---|
| 67 | void append_entry(AW_selection_list_entry *new_entry); |
|---|
| 68 | |
|---|
| 69 | public: |
|---|
| 70 | AW_selection_list(const char *variable_name_, int variable_type_, Widget select_list_widget_); |
|---|
| 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 | // type-independent: |
|---|
| 90 | void insert(const char *displayed, const AW_scalar& value); |
|---|
| 91 | void insert_default(const char *displayed, const AW_scalar& value); |
|---|
| 92 | |
|---|
| 93 | template <class T> void insert(const char *displayed, T value) { insert(displayed, AW_scalar(value)); } |
|---|
| 94 | template <class T> void insert_default(const char *displayed, T value) { insert_default(displayed, AW_scalar(value)); } |
|---|
| 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 set_update_callback(sellist_update_cb ucb, AW_CL cl_user); |
|---|
| 102 | |
|---|
| 103 | void sort(bool backward, bool case_sensitive); // uses displayed value! |
|---|
| 104 | void sortCustom(sellist_cmp_fun cmp); // uses displayed value! |
|---|
| 105 | |
|---|
| 106 | AW_scalar get_awar_value() const; |
|---|
| 107 | void set_awar_value(const AW_scalar& new_value); |
|---|
| 108 | |
|---|
| 109 | const AW_scalar *get_default_value() const; |
|---|
| 110 | const char *get_default_display() const; |
|---|
| 111 | |
|---|
| 112 | void select_default(); |
|---|
| 113 | |
|---|
| 114 | const AW_scalar *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 AW_scalar& searched_value); |
|---|
| 117 | int get_index_of_selected(); |
|---|
| 118 | |
|---|
| 119 | const AW_scalar *get_value_at(int index); |
|---|
| 120 | |
|---|
| 121 | void select_element_at(int wanted_index); |
|---|
| 122 | void move_selection(int offset); |
|---|
| 123 | |
|---|
| 124 | bool default_is_selected() const; |
|---|
| 125 | |
|---|
| 126 | void delete_element_at(int index); |
|---|
| 127 | void delete_value(const AW_scalar& value); |
|---|
| 128 | void delete_default(); |
|---|
| 129 | void clear(); |
|---|
| 130 | |
|---|
| 131 | void move_content_to(AW_selection_list *target_list); |
|---|
| 132 | |
|---|
| 133 | void to_array(StrArray& array, bool values); // only works with string-type selection-lists! |
|---|
| 134 | GB_HASH *to_hash(bool case_sens); // only works with string-type selection-lists! |
|---|
| 135 | |
|---|
| 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() : NULp; } |
|---|
| 160 | const AW_scalar *get_value() const { return entry ? &entry->get_value() : NULp; } |
|---|
| 161 | |
|---|
| 162 | void set_displayed(const char *disp) { aw_assert(entry); entry->set_displayed(disp); } |
|---|
| 163 | void set_value(const AW_scalar& 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_plain(const char *displayed_value) { insert(displayed_value, displayed_value); } |
|---|
| 187 | |
|---|
| 188 | void insert(const char *displayed, int32_t value) { sellist->insert(displayed, value); } |
|---|
| 189 | void insert_default(const char *displayed, int32_t value) { sellist->insert_default(displayed, value); } |
|---|
| 190 | |
|---|
| 191 | public: |
|---|
| 192 | AW_selection(AW_selection_list *sellist_) : sellist(sellist_) {} |
|---|
| 193 | virtual ~AW_selection() {} |
|---|
| 194 | |
|---|
| 195 | void refresh(); |
|---|
| 196 | AW_selection_list *get_sellist() { return sellist; } |
|---|
| 197 | |
|---|
| 198 | void get_values(StrArray& intoArray) { get_sellist()->to_array(intoArray, true); } |
|---|
| 199 | void get_displayed(StrArray& intoArray) { get_sellist()->to_array(intoArray, false); } |
|---|
| 200 | }; |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | class AW_DB_selection : public AW_selection { // derived from a Noncopyable |
|---|
| 204 | GBDATA *gbd; // root container of data displayed in selection list |
|---|
| 205 | public: |
|---|
| 206 | AW_DB_selection(AW_selection_list *sellist_, GBDATA *gbd_); |
|---|
| 207 | ~AW_DB_selection() OVERRIDE; |
|---|
| 208 | |
|---|
| 209 | GBDATA *get_gbd() { return gbd; } |
|---|
| 210 | GBDATA *get_gb_main(); |
|---|
| 211 | }; |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | #else |
|---|
| 215 | #error aw_select.hxx included twice |
|---|
| 216 | #endif // AW_SELECT_HXX |
|---|