Btk
widget.hpp
1 #if !defined(_BTK_WIDGET_HPP_)
2 #define _BTK_WIDGET_HPP_
3 #include <list>
4 #include "signal/signal.hpp"
5 #include "signal/function.hpp"
6 #include "rect.hpp"
7 #include "defs.hpp"
8 namespace Btk{
9  class Renderer;
10  class Window;
11  //Event forward decl
12  class Event;
13  class Widget;
14  class Container;
15 
16  struct WindowImpl;
17 
18  struct KeyEvent;
19  struct MouseEvent;
20  struct WheelEvent;
21  struct MotionEvent;
22  struct TextInputEvent;
23 
24  enum class FocusPolicy{
25  None,
26  KeyBoard,
27  Click,
28  Wheel
29  };
30  //Attribute for Widget
31  struct WidgetAttr{
32  bool hide = false;//<Is hide
33  bool user_rect = false;//<Using user defined position
34  bool container = false;//<Is container
35  bool disable = false;//<The widget is disabled?
36  FocusPolicy focus = FocusPolicy::None;//<Default the widget couldnot get focus
37  };
38  //Alignment
39  enum class Align:unsigned int{
40  Center,
41  //Vertical Alignment
42  Top,
43  Buttom,
44  //Horizontal Alignment
45  Right,
46  Left
47  };
48  enum class Orientation:unsigned int{
49  Vertical = 0,
50  Horizontal = 1,
51  V = Vertical,
52  H = Horizontal
53  };
54  #if 0
55 
59  class BTKAPI EventDispatcher{
60  public:
61  using container_type = std::list<Widget*>;
62 
63  EventDispatcher(container_type &w):
64  widgets(w){};
65  EventDispatcher(const EventDispatcher &) = delete;
66  ~EventDispatcher() = default;
67  public:
68  //Process Event
69  bool handle_click(MouseEvent &);
70  bool handle_motion(MotionEvent &);
71  bool handle_keyboard(KeyEvent &);
72  bool handle_textinput(TextInputEvent &);
79  bool handle(Event &);
80  private:
81  //A helper for set the current focus widget
82  void set_focus_widget(Widget *);
83 
84 
85  container_type& widgets;
86  Widget *focus_widget = nullptr;//The widget which has focus
87  Widget *drag_widget = nullptr;//The Dragging event
88  Widget *cur_widget = nullptr;//Mouse point widget
94  bool mouse_pressed = false;
95  bool drag_rejected = false;
100  bool managed_window = false;
101  friend struct WindowImpl;
102  };
103  #endif
104 
108  class BTKAPI Container{
109  public:
110  Container();
111  Container(const Container &) = delete;
112  ~Container();
118  public:
127  template<class T,class ...Args>
128  T& add(Args &&...args){
129  T *ptr = new T(
130  *this,
131  std::forward<Args>(args)...
132  );
133  widgets_list.push_back(ptr);
134  return *ptr;
135  }
136  bool add(Widget *w){
137  if(w != nullptr){
138  widgets_list.push_back(w);
139  return true;
140  }
141  return false;
142  }
147  void clear();
155  bool remove(Widget *widget);
163  bool detach(Widget *widget);
164  public:
165  //Process Event
166  bool handle_click(MouseEvent &);
167  bool handle_whell(WheelEvent &);
168  bool handle_motion(MotionEvent &);
169  bool handle_keyboard(KeyEvent &);
170  bool handle_textinput(TextInputEvent &);
177  bool handle(Event &);
184  return ev_filter;
185  }
186  protected:
187  //A helper for set the current focus widget
188  void set_focus_widget(Widget *);
189 
190  Widget *focus_widget = nullptr;//The widget which has focus
191  Widget *drag_widget = nullptr;//The Dragging event
192  Widget *cur_widget = nullptr;//Mouse point widget
198  bool mouse_pressed = false;
199  bool drag_rejected = false;
204  bool managed_window = false;
210  protected:
211  //top window
212  WindowImpl *window;
213  std::list<Widget*> widgets_list;
214  friend class Window;
215  friend class Widget;
216  friend struct System;
217  friend struct WindowImpl;
218  };
219  class BTKAPI Widget:public HasSlots{
220  public:
225  Widget():attr(),rect(
226  0,0,0,0
227  ),parent(nullptr){};
228  Widget(Container *parent):Widget(){
229  this->parent = parent;
230  }
231  Widget(Container &parent):Widget(){
232  this->parent = &parent;
233  }
234 
235  Widget(const Widget &) = delete;
236  virtual ~Widget();
237  virtual void draw(Renderer &render) = 0;
244  virtual bool handle(Event &);
245  bool visible() const noexcept{
246  return not attr.hide;
247  };
248  Vec2 position() const noexcept{
249  return {
250  rect.x,
251  rect.y
252  };
253  };
259  Window &master() const;
260  //Set widget rect
261  void set_rect(const Rect &rect);
262  void set_rect(int x,int y,int w,int h){
263  set_rect({x,y,w,h});
264  };
265  void set_position(const Vec2 &vec2){
266  set_rect(vec2.x,vec2.y,rect.w,rect.h);
267  };
268  int x() const noexcept{
269  return rect.x;
270  };
271  int y() const noexcept{
272  return rect.y;
273  };
274  int w() const noexcept{
275  return rect.w;
276  };
277  int h() const noexcept{
278  return rect.h;
279  };
280  bool is_enable() const noexcept{
281  return not attr.disable;
282  };
283  protected:
288  void redraw();
294  WindowImpl *window() const noexcept{
295  return parent->window;
296  }
297  protected:
298  WidgetAttr attr;//Widget attributes
299  Rect rect;//Widget rect
300  Container *parent;//Parents
301  friend class Window;
302  friend class Layout;
303  friend struct WindowImpl;
304  friend class Container;
305  friend void PushEvent(Event *,Widget &);
306  };
307 
308  class BTKAPI Line:public Widget{
309  public:
310  Line(Container &,Orientation);
311  Line(Container &,int x,int y,int w,int h,Orientation);
312  ~Line();
313 
314  void draw(Renderer &);
315  private:
316  Orientation orientation;
317  };
318 };
319 
320 #endif // _BTK_WIDGET_HPP_
Definition: core.hpp:17
Definition: widget.hpp:31
void PushEvent(Event *event, Window &receiver)
Push event to queue.
Definition: event.cpp:63
a SDL_Rect with methods
Definition: rect.hpp:10
WindowImpl * window() const noexcept
Get current window.
Definition: widget.hpp:294
Definition: widget.hpp:219
This header include many useful containers.
Definition: async.hpp:7
A Basic Window.
Definition: window.hpp:20
A Event of text input.
Definition: event.hpp:237
A event about mouse click.
Definition: event.hpp:106
Function< bool(Event &)> ev_filter
Event filter.
Definition: widget.hpp:209
T & add(Args &&...args)
Add a widget to the Container.
Definition: widget.hpp:128
Widget()
Construct a new Widget object.
Definition: widget.hpp:225
A event about keyboard.
Definition: event.hpp:171
Definition: rect.hpp:81
Definition: layout.hpp:6
Function< bool(Event &)> Filter
The event filter(return false to drop the event)
Definition: widget.hpp:117
Definition: signal.hpp:119
Function< bool(Event &)> & filter() noexcept
Get the Container EventFilter.
Definition: widget.hpp:183
A event about mouse motion.
Definition: event.hpp:216
A base event of all events.
Definition: event.hpp:17
Definition: window.hpp:39
Definition: event.hpp:287
A Container of Widget.
Definition: widget.hpp:108
Definition: widget.hpp:308