FDOSTUI
FreeDOS Text User Interface
widget.hpp
Go to the documentation of this file.
1 /*
2  WIDGET.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 __widget_hpp__
11 
12 #include "box.h"
13 #include "event.h"
14 #include "cursor.h"
15 #include <stddef.h>
16 
17 class group;
18 
19 class widget
20 {
21 
22 public:
23 
24  enum damage
25  {
27  DAMAGE_ALL = (1<<0),
28  DAMAGE_LABEL = (1<<1),
29  DAMAGE_VALUE = (1<<2),
30  DAMAGE_SCROLL = (1<<3),
31  DAMAGE_MOVED = (1<<4),
32  DAMAGE_RESERVED1 = (1<<5),
33  DAMAGE_RESERVED2 = (1<<6),
35  };
36 
37  widget(
38  int const i_pos_x,
39  int const i_pos_y,
40  unsigned int const i_len_x,
41  unsigned int const i_len_y);
42 
43  virtual
44  ~widget();
45 
46  virtual void
47  set_damage(
48  unsigned int const i_damage=DAMAGE_NONE);
49 
50  virtual bool
51  close(
52  bool const i_shutting_down);
53 
54  bool
55  contains(
56  int const i_pos_x,
57  int const i_pos_y) const;
58 
59  bool
60  contains(
61  struct box const& i_box) const;
62 
63  virtual void
64  draw() const = 0;
65 
66  virtual enum event_response
68  enum scancodes const i_scan);
69 
70  virtual enum event_response
71  event_key(
72  struct event_key const& i_event);
73 
74  virtual enum event_response
76  struct event_mouse const& i_event);
77 
78  virtual void
79  focus_enter();
80 
81  virtual void
82  focus_leave();
83 
84  void
85  get_box(
86  struct box& o_box) const;
87 
88  bool
89  get_can_focus() const;
90 
91  bool
92  get_enabled() const;
93 
94  bool
95  get_has_focus() const;
96 
97  group*const
98  get_parent() const;
99 
100  void*
101  get_user_data() const;
102 
103  bool
104  get_visible() const;
105 
106  int
107  get_pos_x() const;
108 
109  int
110  get_pos_y() const;
111 
112  unsigned int
113  get_len_x() const;
114 
115  unsigned int
116  get_len_y() const;
117 
118  virtual void
119  move(
120  int const i_pos_x,
121  int const i_pos_y);
122 
123  virtual void
124  resize(
125  unsigned int const i_len_x,
126  unsigned int const i_len_y);
127 
128  void
130  bool const i_can_close);
131 
132  void
133  set_disabled();
134 
135  void
136  set_enabled();
137 
138  void
139  set_hidden();
140 
141  virtual void
142  set_parent(
143  group*const i_parent);
144 
145  void
147  void* io_user_data);
148 
149  void
150  set_visible();
151 
152  void
154  int*const o_pos_x,
155  int*const o_pos_y) const;
156 
157 protected:
158 
159  unsigned int m_damage;
160 
161  enum flags
162  {
163  VISIBLE= (1 << 0),
164  ENABLED= (1 << 1),
165  CAN_FOCUS= (1 << 2),
166  HAS_FOCUS= (1 << 3),
167  CAN_CLOSE= (1 << 4)
168  };
169 
170  unsigned int m_flags;
171  struct box m_box;
173  void* m_user_data;
174 
175 private:
176 
177  widget();
178 
179  widget(
180  const widget&);
181 
182  widget&
183  operator=(widget const&);
184 
185 };
186 
187 inline bool
189  int const i_pos_x,
190  int const i_pos_y) const
191 {
192  bool l_hit;
193 
194  l_hit= static_cast<bool>(box_contains(i_pos_x, i_pos_y, &m_box));
195 
196  return l_hit;
197 }
198 
199 inline bool
201  struct box const& i_box) const
202 {
203  bool l_hit;
204 
205  l_hit= static_cast<bool>(box_contains_box(&m_box, &i_box));
206 
207  return l_hit;
208 }
209 
210 inline void
212  int const i_pos_x,
213  int const i_pos_y)
214 {
215  m_box.m_pos_x= i_pos_x;
216  m_box.m_pos_y= i_pos_y;
217  return;
218 }
219 
220 inline void
222  unsigned int const i_len_x,
223  unsigned int const i_len_y)
224 {
225  m_box.m_len_x= i_len_x;
226  m_box.m_len_y= i_len_y;
227  return;
228 }
229 
230 inline void
232 {
234  return;
235 }
236 
237 inline void
239 {
241 #if defined(__CURSES__)
242  if (0 == (CURSOR_CAN_HIDE & _cursor_features))
243  {
244  int l_cur_x;
245  int l_cur_y;
246  translate_screen(&l_cur_x, &l_cur_y);
247  ::move(l_cur_y, l_cur_x);
248  }
249 #endif
250  return;
251 }
252 
253 inline void
255 {
257  return;
258 }
259 
260 inline void
262  struct box& o_box) const
263 {
264  o_box= m_box;
265  return;
266 }
267 
268 inline bool
270 {
271  return ((m_flags & widget::CAN_FOCUS) && (m_flags & widget::ENABLED));
272 }
273 
274 inline bool
276 {
277  return (m_flags & widget::ENABLED);
278 }
279 
280 inline bool
282 {
283  return (m_flags & widget::HAS_FOCUS);
284 }
285 
286 inline group*const
288 {
289  return m_parent;
290 }
291 
292 inline void*
294 {
295  return m_user_data;
296 }
297 
298 inline bool
300 {
301  return (m_flags & widget::VISIBLE);
302 }
303 
304 inline int
306 {
307  return m_box.m_pos_x;
308 }
309 
310 inline int
312 {
313  return m_box.m_pos_y;
314 }
315 
316 inline unsigned int
318 {
319  return m_box.m_len_x;
320 }
321 
322 inline unsigned int
324 {
325  return m_box.m_len_y;
326 }
327 
328 inline void
330  bool const i_can_close)
331 {
332  if (i_can_close)
333  {
335  }
336  else
337  {
339  }
340  return;
341 }
342 
343 inline void
345 {
347  return;
348 }
349 
350 inline void
352 {
354  return;
355 }
356 
357 inline void
359  group*const i_parent)
360 {
361  m_parent= i_parent;
362 }
363 
364 inline void
366  void* io_user_data)
367 {
368  m_user_data= io_user_data;
369  return;
370 }
371 
372 inline void
374 {
376  return;
377 }
378 
379 inline enum event_response
381  enum scancodes const i_scan)
382 {
383  return RESPONSE_NONE;
384 }
385 
386 inline enum event_response
388  struct event_key const& i_event)
389 {
390  return RESPONSE_NONE;
391 }
392 
393 inline enum event_response
395  struct event_mouse const& i_event)
396 {
397  return RESPONSE_NONE;
398 }
399 
400 inline void
402  unsigned int const i_damage)
403 {
404  m_damage= i_damage;
405 }
406 
407 #define __widget_hpp__
408 #endif
Definition: event.h:20
Definition: widget.hpp:26
scancodes
ASCII Scan Codes.
Definition: keyboard.h:26
virtual void resize(unsigned int const i_len_x, unsigned int const i_len_y)
resize the widget
Definition: widget.hpp:221
int m_pos_y
Definition: box.h:15
Definition: widget.hpp:33
flags
Definition: widget.hpp:161
void set_can_close(bool const i_can_close)
set internal indicator if widget can be closed
Definition: widget.hpp:329
manipulation of the screens cursor
virtual void focus_enter()
called when the widget receives focus
Definition: widget.hpp:238
bool get_has_focus() const
gets whether or not the widget has focus
Definition: widget.hpp:281
virtual bool close(bool const i_shutting_down)
called when widget is about to be destroyed
Definition: widget.cpp:42
struct box m_box
Definition: widget.hpp:171
unsigned int m_len_y
Definition: box.h:17
bool get_enabled() const
gets whether or not the widget is enabled
Definition: widget.hpp:275
void set_user_data(void *io_user_data)
sets the user defined data pointer
Definition: widget.hpp:365
void set_enabled()
enable the widget
Definition: widget.hpp:344
base class for all derived widgets
Definition: widget.hpp:19
bool get_visible() const
gets whether or not the widget is visible
Definition: widget.hpp:299
void set_disabled()
disable the widget
Definition: widget.hpp:231
Definition: widget.hpp:165
group * m_parent
Definition: widget.hpp:172
virtual enum event_response event_key_default(enum scancodes const i_scan)
default key event handler
Definition: widget.hpp:380
int m_pos_x
Definition: box.h:14
Definition: widget.hpp:163
Definition: widget.hpp:29
Definition: widget.hpp:166
unsigned int get_len_x() const
gets the horizontal length of the widget
Definition: widget.hpp:317
virtual enum event_response event_mouse(struct event_mouse const &i_event)
handle mouse event
Definition: widget.hpp:394
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 get_box(struct box &o_box) const
gets the rectangular region this widget occupies
Definition: widget.hpp:261
void set_hidden()
hide the widget
Definition: widget.hpp:351
Definition: widget.hpp:30
a rectanglular region
Definition: widget.hpp:34
void set_visible()
sets the widget as visible
Definition: widget.hpp:373
unsigned int get_len_y() const
gets the vertical length of the widget
Definition: widget.hpp:323
int box_contains(int const i_pos_x, int const i_pos_y, struct box const *i_box)
test if point is within box
Definition: box.c:13
Definition: widget.hpp:28
virtual void move(int const i_pos_x, int const i_pos_y)
move the widget to a new position
Definition: widget.hpp:211
Definition: widget.hpp:167
void * m_user_data
Definition: widget.hpp:173
unsigned int m_flags
Definition: widget.hpp:170
Definition: widget.hpp:32
unsigned int m_damage
Definition: widget.hpp:159
virtual ~widget()
destructor
Definition: widget.cpp:36
damage
bit mask indicating what needs to be redrawn
Definition: widget.hpp:24
int box_contains_box(struct box const *i_box1, struct box const *i_box2)
test if a box is contained in another
Definition: box.c:69
event_response
response
Definition: event.h:14
Definition: widget.hpp:31
void * get_user_data() const
gets user defined data pointer
Definition: widget.hpp:293
void translate_screen(int *const o_pos_x, int *const o_pos_y) const
translate a widgets relative coordinates to screen coordinates
Definition: widget.cpp:61
unsigned int m_len_x
Definition: box.h:16
Data structures for event handling.
unsigned int _cursor_features
global to hold the cursor features
Definition: ncurses/cursor.c:17
Definition: cursor.h:18
virtual void set_damage(unsigned int const i_damage=DAMAGE_NONE)
set the damage bits
Definition: widget.hpp:401
Definition: widget.hpp:164
Definition: widget.hpp:27
group *const get_parent() const
gets the parent widget
Definition: widget.hpp:287
virtual void draw() const =0
draws the widget
bool contains(int const i_pos_x, int const i_pos_y) const
test if point is contained within the widget
Definition: widget.hpp:188
bool get_can_focus() const
gets whether or not the widget can receive focus
Definition: widget.hpp:269
virtual enum event_response event_key(struct event_key const &i_event)
handle key event
Definition: widget.hpp:387
int get_pos_y() const
gets the relative y position of the widget
Definition: widget.hpp:311
container for a mouse event
Definition: event.h:37
int get_pos_x() const
gets the relative x position of the widget
Definition: widget.hpp:305
defines a rectangular region
Definition: box.h:12
virtual void focus_leave()
called when the widget looses focus
Definition: widget.hpp:254
container for a keyboard event
Definition: event.h:23