source: tags/ms_r16q2/WINDOW/aw_select.hxx

Last change on this file was 13983, checked in by westram, 9 years ago
  • allow to install callback called on AW_selection_list::update()
File size: 7.5 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#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
39public:
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
61typedef int (*sellist_cmp_fun)(const char *disp1, const char *disp2);
62typedef void (*sellist_update_cb)(AW_selection_list *, AW_CL);
63
64class AW_selection_list : virtual Noncopyable {
65    AW_selection_list_entry *get_entry_at(int index) const;
66
67    char             *variable_name;
68    AW_VARIABLE_TYPE  variable_type;
69
70    sellist_update_cb update_cb;
71    AW_CL             cl_update;
72
73public:
74    AW_selection_list(const char *variable_name_, int variable_type_, Widget select_list_widget_);
75    ~AW_selection_list();
76
77    Widget select_list_widget;
78
79    AW_selection_list_entry *list_table;
80    AW_selection_list_entry *last_of_list_table;
81    AW_selection_list_entry *default_select;
82    AW_selection_list      *next;
83
84    // ******************** real public ***************
85
86    const char *get_awar_name() const { return variable_name; }
87#if defined(ASSERTION_USED)
88    GB_TYPES get_awar_type() const { return GB_TYPES(variable_type); }
89#endif
90
91    size_t size();
92
93    void insert(const char *displayed, const char *value);
94    void insert_default(const char *displayed, const char *value);
95    void insert(const char *displayed, int32_t value);
96    void insert_default(const char *displayed, int32_t value);
97    void insert(const char *displayed, GBDATA *pointer);
98    void insert_default(const char *displayed, GBDATA *pointer);
99
100    void init_from_array(const CharPtrArray& entries, const char *default_displayed, const char *default_value);
101
102    void update();
103    void refresh();
104
105    void set_update_callback(sellist_update_cb ucb, AW_CL cl_user);
106
107    void sort(bool backward, bool case_sensitive); // uses displayed value!
108    void sortCustom(sellist_cmp_fun cmp);          // uses displayed value!
109
110    // ---------------------------------------------------
111    // the following functions work for string awars only:
112
113    const char *get_awar_value() const;
114    void set_awar_value(const char *new_value);
115
116    const char *get_default_value() const;
117    const char *get_default_display() const;
118
119    void select_default();
120
121    const char *get_selected_value() const; // may differ from get_awar_value() if default is selected (returns value passed to insert_default)
122
123    int get_index_of(const char *searched_value);
124    int get_index_of_selected();
125
126    const char *get_value_at(int index);
127
128    void select_element_at(int wanted_index);
129    void move_selection(int offset);
130
131    bool default_is_selected() const;
132
133    void delete_element_at(int index);
134    void delete_value(const char *value);
135    void delete_default();
136    void clear();
137
138    void move_content_to(AW_selection_list *target_list);
139
140    void to_array(StrArray& array, bool values);
141    GB_HASH *to_hash(bool case_sens);
142    char *get_content_as_string(long number_of_lines); // displayed content (e.g. for printing)
143
144    // save/load:
145    void set_file_suffix(const char *suffix);
146    GB_ERROR load(const char *filename);
147    GB_ERROR save(const char *filename, long number_of_lines);
148};
149
150class AW_selection_list_iterator {
151    AW_selection_list_entry *entry;
152public:
153    AW_selection_list_iterator(AW_selection_list *sellist)
154        : entry(sellist->list_table)
155    {}
156    AW_selection_list_iterator(AW_selection_list *sellist, int index)
157        : entry(sellist->list_table)
158    {
159        aw_assert(index>=0);
160        forward(index);
161    }
162
163    operator bool() const { return entry; }
164
165    const char *get_displayed() { return entry ? entry->get_displayed() : NULL; }
166    const char *get_value() { return entry ? entry->value.get_string() : NULL; }
167
168    void set_displayed(const char *disp) { aw_assert(entry); entry->set_displayed(disp); }
169    void set_value(const char *val) { aw_assert(entry); entry->set_value(val); }
170
171    void forward(size_t offset) {
172        while (offset--) ++(*this);
173    }
174    AW_selection_list_iterator& operator++() {
175        if (entry) entry = entry->next;
176        return *this;
177    }
178};
179
180class AW_selection : virtual Noncopyable {
181    //! a AW_selection_list which knows how to fill itself
182    // (clients can't modify)
183
184    AW_selection_list *sellist;
185
186    virtual void fill() = 0;
187
188protected: 
189    void insert(const char *displayed, const char *value) { sellist->insert(displayed, value); }
190    void insert_default(const char *displayed, const char *value) { sellist->insert_default(displayed, value); }
191
192    void insert(const char *displayed, int32_t value) { sellist->insert(displayed, value); }
193    void insert_default(const char *displayed, int32_t value) { sellist->insert_default(displayed, value); }
194
195public:
196    AW_selection(AW_selection_list *sellist_) : sellist(sellist_) {}
197    virtual ~AW_selection() {}
198
199    void refresh();
200    AW_selection_list *get_sellist() { return sellist; }
201   
202    void get_values(StrArray& intoArray) { get_sellist()->to_array(intoArray, true); }
203    void get_displayed(StrArray& intoArray) { get_sellist()->to_array(intoArray, false); }
204};
205
206
207class AW_DB_selection : public AW_selection { // derived from a Noncopyable
208    GBDATA *gbd;                                    // root container of data displayed in selection list
209public:
210    AW_DB_selection(AW_selection_list *sellist_, GBDATA *gbd_);
211    virtual ~AW_DB_selection() OVERRIDE;
212   
213    GBDATA *get_gbd() { return gbd; }
214    GBDATA *get_gb_main();
215};
216
217
218#else
219#error aw_select.hxx included twice
220#endif // AW_SELECT_HXX
Note: See TracBrowser for help on using the repository browser.