libwtk-sdl2  0.0.1
widget.hpp
1 #ifndef LIBWTK_SDL2_WIDGET_HPP
2 #define LIBWTK_SDL2_WIDGET_HPP
3 
4 #include <memory>
5 #include <optional>
6 #include <vector>
7 
8 #include "context_info.hpp"
9 #include "draw_context.hpp"
10 #include "geometry.hpp"
11 #include "key_event.hpp"
12 #include "mouse_event.hpp"
13 #include "navigation_type.hpp"
14 #include "selection_context.hpp"
15 #include "swipe.hpp"
16 
17 struct widget;
18 
22 typedef std::shared_ptr<widget> widget_ptr;
23 
24 enum class dirty_type
25 {
26  CLEAN,
27  CHILD_DIRTY,
28  DIRTY
29 };
30 
31 // This will choose the maximum dirty_type that is necessary such that a widget
32 // tree will be properly drawn.
33 dirty_type combine(dirty_type a, dirty_type b);
34 
35 struct size_hint
36 {
37  size_hint(vec min, vec nat);
38  size_hint(vec min);
39 
40  vec minimal;
41  vec natural;
42 };
43 
52 struct widget
53 {
54 
55  widget();
56  widget(widget const &) = delete;
57  widget & operator=(widget const &) = delete;
58  virtual ~widget();
59 
73  virtual void on_child_dirty(widget * child);
74 
82  virtual std::vector<widget *> get_visible_children();
83  virtual std::vector<widget const *> get_visible_children() const;
84 
91  void clear_dirty();
92 
93  void mark_dirty();
94  void mark_child_dirty(widget * child);
95 
100  void draw(draw_context & dc, selection_context const & sc) const;
101 
105  void draw_dirty(draw_context & dc, selection_context const & sc) const;
106 
110  virtual void on_draw(draw_context & dc, selection_context const & sc) const = 0;
111 
119  virtual void on_mouse_up_event(mouse_up_event const & e);
120  virtual void on_mouse_down_event(mouse_down_event const & e);
121  virtual void on_mouse_move_event(mouse_move_event const & e);
122  virtual void on_key_event(key_event const & e);
123 
128  virtual void on_activate();
129 
135  SDL_Rect const & get_box() const;
136 
142  context_info const & get_context_info() const;
143 
147  void set_context_info(context_info const & ci);
148 
153  void set_parent(widget * parent);
154 
158  virtual std::vector<widget *> get_children();
159  virtual std::vector<widget const *> get_children() const;
160 
170  void apply_layout(SDL_Rect box);
171 
176  virtual void on_box_allocated();
177 
178  // TODO change encoding: none(), height(int), width(int)
184  virtual size_hint get_size_hint(int width = -1, int height = -1) const = 0;
185 
196  virtual bool can_use_intermediate_size() const;
197 
212  virtual widget * find_selectable(navigation_type nt, point center);
213 
223  virtual widget * navigate_selectable(navigation_type nt, point center);
224 
234  virtual widget * navigate_selectable_from_children(navigation_type nt, widget * w, point center);
235 
236  virtual void on_select();
237  virtual void on_unselect();
238 
241  private:
242 
243  dirty_type _dirty;
244 
245  protected:
246 
256  std::optional<swipe_info> get_swipe_info_with_context_info(mouse_up_event const & e);
257 
262  widget * navigate_selectable_parent(navigation_type nt, point center);
263 
266  widget * _parent;
267 
268  private:
269 
270  void notify_parent_child_dirty();
271 
272  // cached for efficiency, may be replaced by just width and height
273  SDL_Rect _box;
274 
275  context_info const * _context_info;
276 };
277 
278 int opt_min(int opt_length, int length);
279 int opt_max(int opt_length, int length);
280 int opt_or_value(int opt_length, int length);
281 int opt_change(int opt_length, int length);
282 
283 #endif
284 
Definition: mouse_event.hpp:19
virtual size_hint get_size_hint(int width=-1, int height=-1) const =0
virtual void on_box_allocated()
std::optional< swipe_info > get_swipe_info_with_context_info(mouse_up_event const &e)
SDL_Rect const & get_box() const
Definition: mouse_event.hpp:8
virtual bool can_use_intermediate_size() const
virtual void on_child_dirty(widget *child)
void set_parent(widget *parent)
Definition: context_info.hpp:7
virtual widget * navigate_selectable(navigation_type nt, point center)
virtual widget * navigate_selectable_from_children(navigation_type nt, widget *w, point center)
void draw(draw_context &dc, selection_context const &sc) const
Definition: draw_context.hpp:126
Definition: geometry.hpp:12
virtual void on_draw(draw_context &dc, selection_context const &sc) const =0
Definition: key_event.hpp:4
void draw_dirty(draw_context &dc, selection_context const &sc) const
virtual void on_activate()
virtual std::vector< widget * > get_visible_children()
void apply_layout(SDL_Rect box)
Definition: geometry.hpp:6
context_info const & get_context_info() const
widget * navigate_selectable_parent(navigation_type nt, point center)
Definition: selection_context.hpp:18
void clear_dirty()
Definition: mouse_event.hpp:26
Definition: widget.hpp:52
virtual std::vector< widget * > get_children()
virtual widget * find_selectable(navigation_type nt, point center)
void set_context_info(context_info const &ci)
Definition: widget.hpp:35
Definition: box.hpp:11