FDOSTUI
FreeDOS Text User Interface
listbox.hpp
Go to the documentation of this file.
1 /*
2  LISTBOX.HPP
3 
4  License CC0 PUBLIC DOMAIN
5 
6  To the extent possible under law, Mark J. Olesen has waived all copyright
7  and related or neighboring rights to FDOSTUI Library. This work is published
8  from: United States.
9 */
10 #ifndef __listbox_hpp__
11 
12 #include "scrllbar.hpp"
13 #include "array.h"
14 #include <string.h>
15 #include <stdlib.h>
16 #if defined(__WATCOMC__)
17 #include <malloc.h>
18 #endif
19 
20 class listbox : public widget
21 {
22 
23 public:
24 
25  typedef void (*signal_selected_t)(
26  listbox const* i_listbox,
27  void* io_user_data);
28 
29  listbox(
30  int const i_pos_x,
31  int const i_pos_y,
32  unsigned int const i_len_x,
33  unsigned int const i_len_y);
34 
35  virtual
36  ~listbox();
37 
38  int
39  add(
40  unsigned char const* i_label);
41 
42  virtual void
43  draw() const;
44 
45  virtual enum event_response
46  event_key(
47  struct event_key const& i_event);
48 
49  virtual enum event_response
51  struct event_mouse const& i_event);
52 
53  unsigned char const*
54  get_item(
55  size_t const i_slot) const;
56 
57  size_t
58  get_count() const;
59 
60  int
61  get_selected_first() const;
62 
63  int
65  int const i_iter) const;
66 
67  virtual void
68  move(
69  int const i_pos_x,
70  int const i_pos_y);
71 
72  virtual void
73  resize(
74  unsigned int const i_len_x,
75  unsigned int const i_len_y);
76 
77  void
78  remove(
79  size_t const i_slot);
80 
81  void
82  remove_all();
83 
84  void
85  set_label(
86  unsigned char const* i_str);
87 
88  void
90  bool const i_multiline);
91 
92  virtual void
93  set_parent(
94  group*const i_parent);
95 
96  void
98  int const i_response);
99 
100  void
102  signal_selected_t i_signal);
103 
104  void
105  set_selected(
106  size_t const i_slot,
107  bool const i_selected=true);
108 
109  void
110  unselect_all();
111 
112 protected:
113 
115  size_t m_line;
116  struct array m_array;
118  unsigned char* m_label;
121 
122 private:
123 
124  listbox();
125 
126  listbox(
127  const listbox&);
128 
129  listbox&
130  operator=(listbox const&);
131 
132  void
133  select(
134  size_t const i_slot);
135 
136  static void
137  vertical_cb(
138  widget const* i_widget,
139  void* io_user_data);
140 
141  void
143 
144  void
145  emit_selected();
146 
147 public:
148 
149  void
150  scroll_internal();
151 
152 };
153 
154 inline size_t
156 {
157  return m_array.m_taken;
158 }
159 
160 inline void
162  bool const i_multiline)
163 {
164 
165  m_multiline= i_multiline;
166 
167  return;
168 }
169 
170 inline void
172  unsigned char const* i_str)
173 {
174 
175  free(m_label);
176  m_label= 0;
177 
178  if (i_str && i_str[0])
179  {
180  size_t l_len;
181  l_len= 1+strlen(reinterpret_cast<char const*>(i_str));
182  m_label= reinterpret_cast<unsigned char*>(malloc(l_len));
183  if (m_label)
184  {
185  memcpy(m_label, i_str, l_len);
186  }
187  }
188 
189  return;
190 }
191 
192 inline void
194  group*const i_parent)
195 {
196 
197  widget::set_parent(i_parent);
198  m_vertical.set_parent(i_parent);
199 
200  return;
201 }
202 
203 inline void
205  int const i_response)
206 {
207  m_response= static_cast<enum event_response>(i_response);
208  return;
209 }
210 
211 inline void
213  signal_selected_t i_signal)
214 {
215 
216  m_signal_selected= i_signal;
217 
218  return;
219 }
220 
221 inline void
223 {
224 
225  if (m_signal_selected)
226  {
227  (*m_signal_selected)(this, m_user_data);
228  }
229 
230  return;
231 }
232 
233 #define __listbox_hpp__
234 #endif
unsigned char const * get_item(size_t const i_slot) const
retrieve an item from the listbox
Definition: listbox.cpp:554
void vertical_configure()
configure vertical scrollbar for display
Definition: listbox.cpp:680
void set_signal_selected(signal_selected_t i_signal)
set callback
Definition: listbox.hpp:212
virtual enum event_response event_mouse(struct event_mouse const &i_event)
handle mouse event
Definition: listbox.cpp:404
unsigned char * m_label
Definition: listbox.hpp:118
virtual void move(int const i_pos_x, int const i_pos_y)
move the widget to a new position
Definition: listbox.cpp:75
A slider widget with navigational arrow buttons at each end.
Definition: scrllbar.hpp:14
int get_selected_next(int const i_iter) const
get the next selected item in the listbox
Definition: listbox.cpp:598
contains scrollbar class
virtual enum event_response event_key(struct event_key const &i_event)
handle key event
Definition: listbox.cpp:459
void set_response(int const i_response)
set the response the widget returns when an item is selected
Definition: listbox.hpp:204
struct array m_array
Definition: listbox.hpp:116
void(* signal_selected_t)(listbox const *i_listbox, void *io_user_data)
function prototype to recieve signal
Definition: listbox.hpp:25
base class for all derived widgets
Definition: widget.hpp:19
signal_selected_t m_signal_selected
Definition: listbox.hpp:120
void set_multiline(bool const i_multiline)
set whether or not the user can select more than one item
Definition: listbox.hpp:161
virtual void resize(unsigned int const i_len_x, unsigned int const i_len_y)
resize the widget
Definition: listbox.cpp:87
int get_selected_first() const
get the first selected item in the listbox
Definition: listbox.cpp:573
A dynamic array that holds pointers to objects.
static void vertical_cb(widget const *i_widget, void *io_user_data)
callback for vertical scrollbar
Definition: listbox.cpp:28
virtual void set_parent(group *const i_parent)
set the parent group of this widget
Definition: listbox.hpp:193
virtual ~listbox()
destructor
Definition: listbox.cpp:64
container to hold a collection of widgets
Definition: group.hpp:15
virtual void set_parent(group *const i_parent)
sets the parent group of the widget
Definition: widget.hpp:358
void emit_selected()
emits the selected signal
Definition: listbox.hpp:222
int add(unsigned char const *i_label)
adds a value to the end of a list
Definition: listbox.cpp:100
container to hold objects
Definition: array.h:14
void scroll_internal()
called when value in the vertical scrollbar changed
Definition: listbox.cpp:544
void * m_user_data
Definition: widget.hpp:173
enum event_response m_response
Definition: listbox.hpp:119
void set_label(unsigned char const *i_str)
set the text label of the listbox
Definition: listbox.hpp:171
event_response
response
Definition: event.h:14
void set_selected(size_t const i_slot, bool const i_selected=true)
set an item as selected or unselected
Definition: listbox.cpp:169
size_t m_line
Definition: listbox.hpp:115
void select(size_t const i_slot)
selects an item
Definition: listbox.cpp:190
size_t get_count() const
get number of items in listbox
Definition: listbox.hpp:155
scrollbar m_vertical
Definition: listbox.hpp:117
void unselect_all()
unselect all items in the listbox
Definition: listbox.cpp:152
bool m_multiline
Definition: listbox.hpp:114
size_t m_taken
Definition: array.h:17
void remove_all()
remove all items from the listbox
Definition: listbox.cpp:653
container for a mouse event
Definition: event.h:37
Allows the user to choose a value from a list.
Definition: listbox.hpp:20
virtual void draw() const
draws the widget
Definition: listbox.cpp:244
container for a keyboard event
Definition: event.h:23