source: tags/ms_r17q2/WINDOW/aw_select.hxx

Last change on this file was 15781, checked in by westram, 7 years ago
File size: 7.6 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    AW_scalar  value;
37
38    static char *copy_string_for_display(const char *str);
39
40public:
41    // @@@ make members private
42    AW_selection_list_entry *next;
43
44    template<typename T>
45    AW_selection_list_entry(const char *display, T val)
46        : displayed(copy_string_for_display(display)),
47          value(val),
48          next(NULL)
49    {}
50    ~AW_selection_list_entry() { free(displayed); }
51
52    const AW_scalar& get_value() const { return value; }
53    void set_value(const AW_scalar& val) { value = val; }
54
55    const char *get_displayed() const { return displayed; } // may differ from string passed to set_displayed() if the string was longer than MAX_DISPLAY_LENGTH
56    void set_displayed(const char *displayed_) { freeset(displayed, copy_string_for_display(displayed_)); }
57};
58
59typedef int (*sellist_cmp_fun)(const char *disp1, const char *disp2);
60typedef void (*sellist_update_cb)(AW_selection_list *, AW_CL);
61
62class AW_selection_list : virtual Noncopyable {
63    AW_selection_list_entry *get_entry_at(int index) const;
64
65    char             *variable_name;
66    AW_VARIABLE_TYPE  variable_type;
67
68    sellist_update_cb update_cb;
69    AW_CL             cl_update;
70
71    void append_entry(AW_selection_list_entry *new_entry);
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    // type-independent:
94    void insert(const char *displayed, const AW_scalar& value);
95    void insert_default(const char *displayed, const AW_scalar& value);
96
97    template <class T> void insert(const char *displayed, T value) { insert(displayed, AW_scalar(value)); }
98    template <class T> void insert_default(const char *displayed, T value) { insert_default(displayed, AW_scalar(value)); }
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    AW_scalar get_awar_value() const;
111    void set_awar_value(const AW_scalar& new_value);
112
113    const AW_scalar *get_default_value() const;
114    const char *get_default_display() const;
115
116    void select_default();
117
118    const AW_scalar *get_selected_value() const; // may differ from get_awar_value() if default is selected (returns value passed to insert_default)
119
120    int get_index_of(const AW_scalar& searched_value);
121    int get_index_of_selected();
122
123    const AW_scalar *get_value_at(int index);
124
125    void select_element_at(int wanted_index);
126    void move_selection(int offset);
127
128    bool default_is_selected() const;
129
130    void delete_element_at(int index);
131    void delete_value(const AW_scalar& value);
132    void delete_default();
133    void clear();
134
135    void move_content_to(AW_selection_list *target_list);
136
137    void     to_array(StrArray& array, bool values); // only works with string-type selection-lists!
138    GB_HASH *to_hash(bool case_sens);                // only works with string-type selection-lists!
139
140    char *get_content_as_string(long number_of_lines); // displayed content (e.g. for printing)
141
142    // save/load:
143    void set_file_suffix(const char *suffix);
144    GB_ERROR load(const char *filename);
145    GB_ERROR save(const char *filename, long number_of_lines);
146};
147
148class AW_selection_list_iterator {
149    AW_selection_list_entry *entry;
150public:
151    AW_selection_list_iterator(AW_selection_list *sellist)
152        : entry(sellist->list_table)
153    {}
154    AW_selection_list_iterator(AW_selection_list *sellist, int index)
155        : entry(sellist->list_table)
156    {
157        aw_assert(index>=0);
158        forward(index);
159    }
160
161    operator bool() const { return entry; }
162
163    const char *get_displayed() { return entry ? entry->get_displayed() : NULL; }
164    const AW_scalar *get_value() const { return entry ? &entry->get_value() : NULL; }
165
166    void set_displayed(const char *disp) { aw_assert(entry); entry->set_displayed(disp); }
167    void set_value(const AW_scalar& val) { aw_assert(entry); entry->set_value(val); }
168
169    void forward(size_t offset) {
170        while (offset--) ++(*this);
171    }
172    AW_selection_list_iterator& operator++() {
173        if (entry) entry = entry->next;
174        return *this;
175    }
176};
177
178class AW_selection : virtual Noncopyable {
179    //! a AW_selection_list which knows how to fill itself
180    // (clients can't modify)
181
182    AW_selection_list *sellist;
183
184    virtual void fill() = 0;
185
186protected: 
187    void insert(const char *displayed, const char *value) { sellist->insert(displayed, value); }
188    void insert_default(const char *displayed, const char *value) { sellist->insert_default(displayed, value); }
189
190    void insert(const char *displayed, int32_t value) { sellist->insert(displayed, value); }
191    void insert_default(const char *displayed, int32_t value) { sellist->insert_default(displayed, value); }
192
193public:
194    AW_selection(AW_selection_list *sellist_) : sellist(sellist_) {}
195    virtual ~AW_selection() {}
196
197    void refresh();
198    AW_selection_list *get_sellist() { return sellist; }
199   
200    void get_values(StrArray& intoArray) { get_sellist()->to_array(intoArray, true); }
201    void get_displayed(StrArray& intoArray) { get_sellist()->to_array(intoArray, false); }
202};
203
204
205class AW_DB_selection : public AW_selection { // derived from a Noncopyable
206    GBDATA *gbd;                                    // root container of data displayed in selection list
207public:
208    AW_DB_selection(AW_selection_list *sellist_, GBDATA *gbd_);
209    ~AW_DB_selection() OVERRIDE;
210   
211    GBDATA *get_gbd() { return gbd; }
212    GBDATA *get_gb_main();
213};
214
215
216#else
217#error aw_select.hxx included twice
218#endif // AW_SELECT_HXX
Note: See TracBrowser for help on using the repository browser.