FDOSTUI
FreeDOS Text User Interface
button.hpp
Go to the documentation of this file.
1 /*
2  BUTTON.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 __button_hpp__
11 
12 #include "draw.h"
13 #include "widget.hpp"
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #if defined(__WATCOMC__)
18 #include <malloc.h>
19 #endif
20 
21 class button : public widget
22 {
23 
24 public:
25 
26  typedef void (*signal_clicked_t)(
27  button const* i_button,
28  void* io_user_data);
29 
30  enum style
31  {
32  STYLE_FLAT = (1 << 0),
33  STYLE_SHADOW = (1 << 1),
34  STYLE_BOX = (1 << 2)
35  };
36 
37  button(
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  ~button();
45 
46  virtual void
47  draw() const;
48 
49  virtual enum event_response
50  event_key(
51  struct event_key const& i_event);
52 
53  virtual enum event_response
55  struct event_mouse const& i_event);
56 
57  virtual void
58  focus_enter();
59 
60  virtual void
61  focus_leave();
62 
63  void
65  int const i_response);
66 
67  void
68  set_style(
69  unsigned int const i_style);
70 
71  void
72  set_text(
73  unsigned char const* i_str);
74 
75  void
77  signal_clicked_t i_signal);
78 
79 protected:
80 
81  enum type
82  {
86  };
87 
88  bool m_state;
89  enum type m_type;
90  unsigned int m_style;
91  unsigned char* m_label;
94 
95  virtual void
96  toggled();
97 
98 private:
99 
100  button();
101 
102  button(
103  const button&);
104 
105  button&
106  operator=(button const&);
107 
108  void
109  emit_clicked();
110 
111 };
112 
113 inline void
115  unsigned int const i_style)
116 {
117  m_style= i_style;
118  return;
119 }
120 
121 inline void
123  unsigned char const* i_str)
124 {
125 
126  free(m_label);
127  m_label= 0;
128 
129  if (i_str && i_str[0])
130  {
131  size_t l_len;
132  l_len= 1+strlen(reinterpret_cast<char const*>(i_str));
133  m_label= reinterpret_cast<unsigned char*>(malloc(l_len));
134  if (m_label)
135  {
136  memcpy(m_label, i_str, l_len);
137  }
138  }
139 
140  return;
141 }
142 
143 inline void
145  int const i_response)
146 {
147  m_response= static_cast<enum event_response>(i_response);
148  return;
149 }
150 
151 inline void
153  signal_clicked_t i_signal)
154 {
155  m_signal_clicked= i_signal;
156 }
157 
158 inline void
159 button::emit_clicked()
160 {
161 
162  if (m_signal_clicked)
163  {
164  (*m_signal_clicked)(this, m_user_data);
165  }
166 
167  return;
168 }
169 
170 inline void
172 {
173  return;
174 }
175 
176 #define __button_hpp__
177 #endif
void set_text(unsigned char const *i_str)
Definition: button.hpp:122
Definition: button.hpp:83
drawing routines that applies clipping
virtual void toggled()
method called when button state changes
Definition: button.hpp:171
virtual enum event_response event_key(struct event_key const &i_event)
handle key event
Definition: button.cpp:241
contains widget class
Definition: button.hpp:33
virtual enum event_response event_mouse(struct event_mouse const &i_event)
handle mouse event
Definition: button.cpp:286
virtual void draw() const
draws the widget
Definition: button.cpp:37
base class for all derived widgets
Definition: widget.hpp:19
Definition: button.hpp:32
void set_response(int const i_response)
sets response generated when button is clicked
Definition: button.hpp:144
Definition: button.hpp:84
enum type m_type
button type
Definition: button.hpp:89
bool m_state
on/off state (for normal button ignored)
Definition: button.hpp:88
Definition: button.hpp:34
Definition: button.hpp:85
void set_signal_clicked(signal_clicked_t i_signal)
set callback
Definition: button.hpp:152
virtual ~button()
destructor
Definition: button.cpp:30
void * m_user_data
Definition: widget.hpp:173
type
Definition: button.hpp:81
unsigned char * m_label
dynamically allocated buffer to hold label
Definition: button.hpp:91
void set_style(unsigned int const i_style)
set button style (no box, flat or box or box and shadow
Definition: button.hpp:114
event_response
response
Definition: event.h:14
style
indicator of the type of box that should be drawn around the button
Definition: button.hpp:30
virtual void focus_leave()
handle leaving focus
Definition: button.cpp:233
signal_clicked_t m_signal_clicked
pointer to signal emit function
Definition: button.hpp:93
unsigned int m_style
button style (button::style bit flags)
Definition: button.hpp:90
void(* signal_clicked_t)(button const *i_button, void *io_user_data)
function prototype to receive signal
Definition: button.hpp:26
Allows a user to click on a button.
Definition: button.hpp:21
enum event_response m_response
response generated when user clicks on the button
Definition: button.hpp:92
container for a mouse event
Definition: event.h:37
virtual void focus_enter()
handle entering focus
Definition: button.cpp:225
container for a keyboard event
Definition: event.h:23