ARB
aw_select.hxx
Go to the documentation of this file.
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
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
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);
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_);
72 
74 
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 
146 public:
148  : entry(sellist->list_table)
149  {}
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  }
169  if (entry) entry = entry->next;
170  return *this;
171  }
172 };
173 
174 class AW_selection : virtual Noncopyable {
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_);
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
int(* sellist_cmp_fun)(const char *disp1, const char *disp2)
Definition: aw_select.hxx:55
const char * GB_ERROR
Definition: arb_core.h:25
void insert_default(const char *displayed, const char *value)
Definition: aw_select.hxx:184
void sort(bool backward, bool case_sensitive)
Definition: AW_select.cxx:502
GBDATA * get_gbd()
Definition: aw_select.hxx:209
void forward(size_t offset)
Definition: aw_select.hxx:165
void select_element_at(int wanted_index)
Definition: AW_select.cxx:433
void set_file_suffix(const char *suffix)
Definition: AW_select.cxx:450
void set_displayed(const char *disp)
Definition: aw_select.hxx:162
void insert(const char *displayed, T value)
Definition: aw_select.hxx:93
void insert_default(const char *displayed, const AW_scalar &value)
Definition: AW_select.cxx:385
void insert_default(const char *displayed, T value)
Definition: aw_select.hxx:94
AW_selection_list_entry(const char *display, T val)
Definition: aw_select.hxx:41
virtual ~AW_selection()
Definition: aw_select.hxx:193
AW_selection_list(const char *variable_name_, int variable_type_, Widget select_list_widget_)
Definition: AW_select.cxx:32
void set_awar_value(const AW_scalar &new_value)
Definition: AW_select.cxx:444
void delete_element_at(int index)
Definition: AW_select.cxx:248
void set_value(const AW_scalar &val)
Definition: aw_select.hxx:49
bool default_is_selected() const
Definition: AW_select.cxx:203
int get_index_of_selected()
Definition: AW_select.cxx:316
void get_values(StrArray &intoArray)
Definition: aw_select.hxx:198
static const size_t MAX_DISPLAY_LENGTH
Definition: aw_select.hxx:36
void insert(const char *displayed, const char *value)
Definition: aw_select.hxx:183
~AW_selection_list_entry()
Definition: aw_select.hxx:46
void insert(const char *displayed, const AW_scalar &value)
Definition: AW_select.cxx:380
AW_selection_list_entry * last_of_list_table
Definition: aw_select.hxx:76
void to_array(StrArray &array, bool values)
Definition: AW_select.cxx:516
Definition: aw_select.hxx:26
void get_displayed(StrArray &intoArray)
Definition: aw_select.hxx:199
AW_VARIABLE_TYPE
Definition: aw_base.hxx:53
const AW_scalar * get_value_at(int index)
Definition: AW_select.cxx:425
GB_HASH * to_hash(bool case_sens)
Definition: AW_select.cxx:532
GB_TYPES get_awar_type() const
Definition: aw_select.hxx:84
GBDATA * get_gb_main()
Definition: AW_select.cxx:592
void set_update_callback(sellist_update_cb ucb, AW_CL cl_user)
Definition: AW_select.cxx:92
#define aw_assert(bed)
Definition: aw_position.hxx:29
AW_DB_selection(AW_selection_list *sellist_, GBDATA *gbd_)
Definition: AW_select.cxx:579
const char * get_displayed() const
Definition: aw_select.hxx:51
AW_selection_list_iterator & operator++()
Definition: aw_select.hxx:168
int get_index_of(const AW_scalar &searched_value)
Definition: AW_select.cxx:303
AW_selection(AW_selection_list *sellist_)
Definition: aw_select.hxx:192
const char * get_displayed()
Definition: aw_select.hxx:159
const AW_scalar & get_value() const
Definition: aw_select.hxx:48
AW_selection_list_entry * default_select
Definition: aw_select.hxx:77
AW_selection_list_entry * next
Definition: aw_select.hxx:34
AW_selection_list_iterator(AW_selection_list *sellist, int index)
Definition: aw_select.hxx:150
#define cmp(h1, h2)
Definition: admap.cxx:50
void set_displayed(const char *displayed_)
Definition: aw_select.hxx:52
void set_value(const AW_scalar &val)
Definition: aw_select.hxx:163
void(* sellist_update_cb)(AW_selection_list *, AW_CL)
Definition: aw_select.hxx:56
long AW_CL
Definition: cb.h:21
const AW_scalar * get_value() const
Definition: aw_select.hxx:160
#define OVERRIDE
Definition: cxxforward.h:112
char * get_content_as_string(long number_of_lines)
Definition: AW_select.cxx:280
void move_selection(int offset)
Definition: AW_select.cxx:415
const char * get_awar_name() const
Definition: aw_select.hxx:82
AW_selection_list_iterator(AW_selection_list *sellist)
Definition: aw_select.hxx:147
Widget select_list_widget
Definition: aw_select.hxx:73
~AW_DB_selection() OVERRIDE
Definition: AW_select.cxx:587
const char * get_default_display() const
Definition: AW_select.cxx:295
GB_ERROR load(const char *filename)
void insert(const char *displayed, int32_t value)
Definition: aw_select.hxx:188
#define NULp
Definition: cxxforward.h:116
#define offset(field)
Definition: GLwDrawA.c:73
void insert_plain(const char *displayed_value)
Definition: aw_select.hxx:186
AW_selection_list_entry * list_table
Definition: aw_select.hxx:75
GB_TYPES
Definition: arbdb.h:62
Definition: trnsprob.h:20
const AW_scalar * get_default_value() const
Definition: AW_select.cxx:298
void move_content_to(AW_selection_list *target_list)
Definition: AW_select.cxx:391
void init_from_array(const CharPtrArray &entries, const char *default_displayed, const char *default_value)
Definition: AW_select.cxx:322
const AW_scalar * get_selected_value() const
Definition: AW_select.cxx:210
struct _WidgetRec * Widget
Definition: aw_base.hxx:48
AW_scalar get_awar_value() const
Definition: AW_select.cxx:274
AW_selection_list * get_sellist()
Definition: aw_select.hxx:196
void delete_value(const AW_scalar &value)
Definition: AW_select.cxx:269
GB_ERROR save(const char *filename, long number_of_lines)
AW_selection_list * next
Definition: aw_select.hxx:78
void insert_default(const char *displayed, int32_t value)
Definition: aw_select.hxx:189
void sortCustom(sellist_cmp_fun cmp)
Definition: AW_select.cxx:478
void refresh()
Definition: AW_select.cxx:184