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

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