view.h Source File - ClanLib SDK
Description: view.h Source File

view.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 The ClanLib Team
4**
5** This software is provided 'as-is', without any express or implied
6** warranty. In no event will the authors be held liable for any damages
7** arising from the use of this software.
8**
9** Permission is granted to anyone to use this software for any purpose,
10** including commercial applications, and to alter it and redistribute it
11** freely, subject to the following restrictions:
12**
13** 1. The origin of this software must not be misrepresented; you must not
14** claim that you wrote the original software. If you use this software
15** in a product, an acknowledgment in the product documentation would be
16** appreciated but is not required.
17** 2. Altered source versions must be plainly marked as such, and must not be
18** misrepresented as being the original software.
19** 3. This notice may not be removed or altered from any source distribution.
20**
21** Note: Some of the libraries ClanLib may link to may have additional
22** requirements or restrictions.
23**
24** File Author(s):
25**
26** Magnus Norddahl
27** Artem Khomenko (Direct redraw changed state of View, without redraw the entire window).
28*/
29
30#pragma once
31
32#include "../../Core/Math/mat4.h"
33#include "../../Core/Math/rect.h"
34#include "../../Core/Math/easing.h"
35#include "../../Core/Signals/signal.h"
36#include "../../UI/Events/event.h"
37#include "../View/view_geometry.h"
38#include "../Style/style.h"
39#include "../Style/style_cascade.h"
40#include "../Style/style_get_value.h"
41#include "focus_policy.h"
42#include <vector>
43#include <memory>
44#include <functional>
45
46namespace clan
47{
48 class Style;
49 class StyleCascade;
50 class Canvas;
51 class ActivationChangeEvent;
52 class CloseEvent;
53 class FocusChangeEvent;
54 class PointerEvent;
55 class ResizeEvent;
56 class KeyEvent;
57 class ViewImpl;
58 class CursorDescription;
59 enum class StandardCursor;
60 class DisplayWindow;
61 class ViewTree;
62 class ViewAction;
63
65 class View : public std::enable_shared_from_this<View>
66 {
67 public:
68 View();
69 virtual ~View();
70
72 const StyleCascade &style_cascade() const;
73
75 const std::shared_ptr<Style> &style(const std::string &state = std::string()) const;
76
78 bool state(const std::string &name) const;
79
81 void set_state(const std::string &name, bool value);
82
84 void set_state_cascade(const std::string &name, bool value);
85
87 SlotContainer slots;
88
90 View *parent() const;
91
93 const std::vector<std::shared_ptr<View>> &children() const;
94
96 void add_child(const std::shared_ptr<View> &view);
97
98 template<typename T, typename... Types>
99 std::shared_ptr<T> add_child(Types &&... args)
100 {
101 auto child = std::make_shared<T>(std::forward<Types>(args)...);
102 add_child(child);
103 return child;
104 }
105
106 std::shared_ptr<View> add_child()
107 {
108 return add_child<View>();
109 }
110
112 void remove_from_parent();
113
115 void add_action(const std::shared_ptr<ViewAction> &action);
116
117 template<typename T, typename... Types>
118 std::shared_ptr<T> add_action(Types &&... args)
119 {
120 auto action = std::make_shared<T>(std::forward<Types>(args)...);
121 add_action(action);
122 return action;
123 }
124
126 const std::vector<std::shared_ptr<ViewAction>> &actions() const;
127
129 bool hidden() const;
130
132 void set_hidden(bool value = true);
133
135 bool is_static_position_and_visible() const;
136
138 bool needs_layout() const;
139
141 void set_needs_layout();
142
144 const ViewGeometry &geometry() const;
145
149 void set_geometry(const ViewGeometry &geometry);
150
154 Canvas canvas() const;
155
157 void set_needs_render();
158
160 bool render_exception_encountered() const;
161
165 void clear_exception_encountered();
166
168 const Mat4f &view_transform() const;
169
171 void set_view_transform(const Mat4f &transform);
172
174 bool content_clipped() const;
175
177 void set_content_clipped(bool clipped);
178
180 float preferred_width(Canvas &canvas);
181
183 float preferred_height(Canvas &canvas, float width);
184
186 float definite_width();
187
189 float definite_height();
190
192 bool is_width_definite();
193
195 bool is_height_definite();
196
198 float first_baseline_offset(Canvas &canvas, float width);
199
201 float last_baseline_offset(Canvas &canvas, float width);
202
204 virtual void layout_children(Canvas &canvas);
205
207 const ViewTree *view_tree() const;
208 ViewTree *view_tree();
209
211 View *focus_view() const;
212
214 std::shared_ptr<View> find_view_at(const Pointf &pos) const;
215
217 FocusPolicy focus_policy() const;
218
220 void set_focus_policy(FocusPolicy policy);
221
223 unsigned int tab_index() const;
224
226 void set_tab_index(unsigned int index);
227
229 void set_focus();
230
232 void remove_focus();
233
235 bool has_focus() const { return focus_view() == this; }
236
238 void prev_focus();
239
241 void next_focus();
242
244 void animate(float from, float to, const std::function<void(float)> &setter, int duration_ms = 400, const std::function<float(float)> &easing = Easing::linear, std::function<void()> animation_end = std::function<void()>());
245
247 void stop_animations();
248
250 void set_cursor(const CursorDescription &cursor);
251 void set_cursor(StandardCursor type);
252
254 void set_inherit_cursor();
255
257 Signal<void(ActivationChangeEvent &)> &sig_activated(bool use_capture = false);
258
260 Signal<void(ActivationChangeEvent &)> &sig_deactivated(bool use_capture = false);
261
263 Signal<void(CloseEvent &)> &sig_close(bool use_capture = false);
264
266 Signal<void(ResizeEvent &)> &sig_resize(bool use_capture = false);
267
269 Signal<void(FocusChangeEvent &)> &sig_focus_gained(bool use_capture = false);
270
272 Signal<void(FocusChangeEvent &)> &sig_focus_lost(bool use_capture = false);
273
275 Signal<void(PointerEvent &)> &sig_pointer_enter(bool use_capture = false);
276
278 Signal<void(PointerEvent &)> &sig_pointer_leave(bool use_capture = false);
279
281 Signal<void(PointerEvent &)> &sig_pointer_move(bool use_capture = false);
282
284 Signal<void(PointerEvent &)> &sig_pointer_press(bool use_capture = false);
285
287 Signal<void(PointerEvent &)> &sig_pointer_release(bool use_capture = false);
288
290 Signal<void(PointerEvent &)> &sig_pointer_double_click(bool use_capture = false);
291
293 Signal<void(PointerEvent &)> &sig_pointer_proximity_change(bool use_capture = false);
294
296 Signal<void(KeyEvent &)> &sig_key_press(bool use_capture = false);
297
299 Signal<void(KeyEvent &)> &sig_key_release(bool use_capture = false);
300
302 void update_cursor(DisplayWindow &window);
303
305 Pointf to_screen_pos(const Pointf &pos);
306
308 Pointf from_screen_pos(const Pointf &pos);
309
311 Pointf to_root_pos(const Pointf &pos, bool relative_to_margin = false);
312
314 Pointf from_root_pos(const Pointf &pos);
315
317 static void dispatch_event(View *target, EventUI *e, bool no_propagation = false);
318
320 void draw_without_layout();
321
322 protected:
324 virtual void render_content(Canvas &canvas) { }
325
327 virtual void render_border(Canvas &canvas);
328
330 virtual void render_background(Canvas &canvas);
331
333 virtual void child_added(const std::shared_ptr<View> &view) { }
334
336 virtual void child_removed(const std::shared_ptr<View> &view) { }
337
339 virtual float calculate_preferred_width(Canvas &canvas);
340
342 virtual float calculate_preferred_height(Canvas &canvas, float width);
343
345 virtual float calculate_first_baseline_offset(Canvas &canvas, float width);
346
348 virtual float calculate_last_baseline_offset(Canvas &canvas, float width);
349
351 virtual float calculate_definite_width(bool &out_is_definite);
352
354 virtual float calculate_definite_height(bool &out_is_definite);
355
356 virtual void updated_view_tree() {}
357
358 private:
359 View(const View &) = delete;
360 View &operator=(const View &) = delete;
361
362 std::unique_ptr<ViewImpl> impl;
363
364 friend class ViewTree;
365 friend class ViewImpl;
366 friend class ViewAction;
367 };
368}
clan::ActivationChangeEvent
Window was activated or deactivated event.
Definition activation_change_event.h:44
clan::Canvas
2D Graphics Canvas
Definition canvas.h:72
clan::CloseEvent
Window close button was clicked event.
Definition close_event.h:37
clan::CursorDescription
This class contains everything to construct a cursor - its data, default settings etc.
Definition cursor_description.h:70
clan::DisplayWindow
Top-level window class.
Definition display_window.h:101
clan::Easing::linear
static float linear(float t)
clan::EventUI
Base class for events being dispatched through the view hiarchy.
Definition UI/Events/event.h:48
clan::FocusChangeEvent
View focus changed event.
Definition focus_change_event.h:44
clan::KeyEvent
Keyboard key event.
Definition key_event.h:48
clan::PointerEvent
Pointer event.
Definition pointer_event.h:68
clan::Pointf
2D (x,y) point structure - Float
Definition point.h:72
clan::ResizeEvent
Window resize event.
Definition resize_event.h:37
clan::Signal
Definition signal.h:105
clan::SlotContainer
Definition signal.h:141
clan::StyleCascade
Style value resolver.
Definition style_cascade.h:68
clan::Style
Style property set.
Definition style.h:46
clan::ViewAction
Recognizes actions in a view and captures input for the duration of the action.
Definition view_action.h:45
clan::ViewGeometry
Definition view_geometry.h:39
clan::ViewTree
Base class for managing a tree of views.
Definition view_tree.h:42
clan::View
View for an area of the user interface.
Definition view.h:66
clan::View::needs_layout
bool needs_layout() const
Test if view geometry needs to be recalculated.
clan::View::sig_key_release
Signal< void(KeyEvent &)> & sig_key_release(bool use_capture=false)
Key released event.
clan::View::calculate_preferred_height
virtual float calculate_preferred_height(Canvas &canvas, float width)
Calculates the preferred height of this view.
clan::View::calculate_first_baseline_offset
virtual float calculate_first_baseline_offset(Canvas &canvas, float width)
Calculates the offset to the first baseline.
clan::View::set_state_cascade
void set_state_cascade(const std::string &name, bool value)
Sets the state for this view and all children recursively, until a manually set state of the same nam...
clan::View::calculate_preferred_width
virtual float calculate_preferred_width(Canvas &canvas)
Calculates the preferred width of this view.
clan::View::render_exception_encountered
bool render_exception_encountered() const
Test if this view generated an exception during rendering.
clan::View::draw_without_layout
void draw_without_layout()
Render view and its children directly, without re-layout.
clan::View::parent
View * parent() const
Parent view node or nullptr if the view is the current root node.
clan::View::View
View()
clan::View::has_focus
bool has_focus() const
Test if this view is receiving keyboard input.
Definition view.h:235
clan::View::add_action
void add_action(const std::shared_ptr< ViewAction > &action)
Add an action recognizer.
clan::View::set_focus
void set_focus()
Set this view as the focused view.
clan::View::hidden
bool hidden() const
Test if view is set to hidden.
clan::View::sig_pointer_enter
Signal< void(PointerEvent &)> & sig_pointer_enter(bool use_capture=false)
Pointer entering view geometry event.
clan::View::render_border
virtual void render_border(Canvas &canvas)
Renders the border of a view.
clan::View::set_cursor
void set_cursor(const CursorDescription &cursor)
Set the cursor icon used when cursor is above this view.
clan::View::last_baseline_offset
float last_baseline_offset(Canvas &canvas, float width)
Calculates the offset to the last baseline.
clan::View::children
const std::vector< std::shared_ptr< View > > & children() const
List of all immediate child views.
clan::View::geometry
const ViewGeometry & geometry() const
Actual view position and size after layout.
clan::View::sig_key_press
Signal< void(KeyEvent &)> & sig_key_press(bool use_capture=false)
Key pressed event.
clan::View::style_cascade
const StyleCascade & style_cascade() const
Style cascade currently active for this view.
clan::View::actions
const std::vector< std::shared_ptr< ViewAction > > & actions() const
List of all action recognizers.
clan::View::set_inherit_cursor
void set_inherit_cursor()
Specify that the cursor icon is inherited from the parent view.
clan::View::sig_pointer_proximity_change
Signal< void(PointerEvent &)> & sig_pointer_proximity_change(bool use_capture=false)
Pointer proximity change event.
clan::View::calculate_definite_width
virtual float calculate_definite_width(bool &out_is_definite)
The content width used for percentages or other definite calculations.
clan::View::add_child
std::shared_ptr< T > add_child(Types &&... args)
Definition view.h:99
clan::View::from_screen_pos
Pointf from_screen_pos(const Pointf &pos)
Map from screen to local content coordinates.
clan::View::sig_deactivated
Signal< void(ActivationChangeEvent &)> & sig_deactivated(bool use_capture=false)
Window deactivated event.
clan::View::dispatch_event
static void dispatch_event(View *target, EventUI *e, bool no_propagation=false)
Dispatch event to signals listening for events.
clan::View::view_tree
ViewTree * view_tree()
clan::View::set_content_clipped
void set_content_clipped(bool clipped)
Specifies if content should be clipped during rendering.
clan::View::sig_pointer_press
Signal< void(PointerEvent &)> & sig_pointer_press(bool use_capture=false)
Pointer button pressed event.
clan::View::stop_animations
void stop_animations()
Stop all activate animation functions.
clan::View::child_added
virtual void child_added(const std::shared_ptr< View > &view)
Child view was added to this view.
Definition view.h:333
clan::View::sig_activated
Signal< void(ActivationChangeEvent &)> & sig_activated(bool use_capture=false)
Window activated event.
clan::View::set_tab_index
void set_tab_index(unsigned int index)
Sets the tab index used for keyboard focus changes.
clan::View::state
bool state(const std::string &name) const
Test if a style state is currently set.
clan::View::to_root_pos
Pointf to_root_pos(const Pointf &pos, bool relative_to_margin=false)
Map from local content to root content or margin (plus content, padding, border and margin) coordinat...
clan::View::ViewImpl
friend class ViewImpl
Definition view.h:365
clan::View::set_focus_policy
void set_focus_policy(FocusPolicy policy)
Set if this view automatically can gain focus.
clan::View::to_screen_pos
Pointf to_screen_pos(const Pointf &pos)
Map from local content to screen coordinates.
clan::View::definite_height
float definite_height()
The content height used for percentages or other definite calculations.
clan::View::preferred_height
float preferred_height(Canvas &canvas, float width)
Calculates the preferred height of this view.
clan::View::sig_pointer_release
Signal< void(PointerEvent &)> & sig_pointer_release(bool use_capture=false)
Pointer button released event.
clan::View::is_width_definite
bool is_width_definite()
Test if the view has a definite width.
clan::View::sig_resize
Signal< void(ResizeEvent &)> & sig_resize(bool use_capture=false)
Window resize event.
clan::View::~View
virtual ~View()
clan::View::updated_view_tree
virtual void updated_view_tree()
Definition view.h:356
clan::View::first_baseline_offset
float first_baseline_offset(Canvas &canvas, float width)
Calculates the offset to the first baseline.
clan::View::sig_pointer_leave
Signal< void(PointerEvent &)> & sig_pointer_leave(bool use_capture=false)
Pointer leaving view geometry event.
clan::View::next_focus
void next_focus()
Give focus to the next view in the keyboard tab index order.
clan::View::set_state
void set_state(const std::string &name, bool value)
Set or clear style state.
clan::View::preferred_width
float preferred_width(Canvas &canvas)
Calculates the preferred width of this view.
clan::View::calculate_last_baseline_offset
virtual float calculate_last_baseline_offset(Canvas &canvas, float width)
Calculates the offset to the last baseline.
clan::View::set_hidden
void set_hidden(bool value=true)
Hides a view from layout and rendering.
clan::View::add_child
void add_child(const std::shared_ptr< View > &view)
Add a child view.
clan::View::add_child
std::shared_ptr< View > add_child()
Definition view.h:106
clan::View::sig_pointer_move
Signal< void(PointerEvent &)> & sig_pointer_move(bool use_capture=false)
Pointer moved above view event.
clan::View::slots
SlotContainer slots
Slot container helping with automatic disconnection of connected slots when the view is destroyed.
Definition view.h:87
clan::View::focus_view
View * focus_view() const
The view receiving keyboard events or nullptr if no view has the focus.
clan::View::render_content
virtual void render_content(Canvas &canvas)
Renders the content of a view.
Definition view.h:324
clan::View::animate
void animate(float from, float to, const std::function< void(float)> &setter, int duration_ms=400, const std::function< float(float)> &easing=Easing::linear, std::function< void()> animation_end=std::function< void()>())
Continously call an animation function for the specified duration.
clan::View::render_background
virtual void render_background(Canvas &canvas)
Renders the background of a view.
clan::View::focus_policy
FocusPolicy focus_policy() const
Focus policy active for this view.
clan::View::calculate_definite_height
virtual float calculate_definite_height(bool &out_is_definite)
The content height used for percentages or other definite calculations.
clan::View::set_needs_render
void set_needs_render()
Signals this view needs to be rendered again.
clan::View::set_cursor
void set_cursor(StandardCursor type)
clan::View::tab_index
unsigned int tab_index() const
Tab index for keyboard focus changes.
clan::View::set_geometry
void set_geometry(const ViewGeometry &geometry)
clan::View::is_static_position_and_visible
bool is_static_position_and_visible() const
Test if view should participate in static layout calculations (layout_children)
clan::View::set_needs_layout
void set_needs_layout()
Forces recalculation of view geometry before next rendering.
clan::View::add_action
std::shared_ptr< T > add_action(Types &&... args)
Definition view.h:118
clan::View::sig_focus_lost
Signal< void(FocusChangeEvent &)> & sig_focus_lost(bool use_capture=false)
View lost focus event.
clan::View::definite_width
float definite_width()
The content width used for percentages or other definite calculations.
clan::View::prev_focus
void prev_focus()
Give focus to the previous view in the keyboard tab index order.
clan::View::remove_focus
void remove_focus()
Remove focus from this view.
clan::View::sig_close
Signal< void(CloseEvent &)> & sig_close(bool use_capture=false)
Window close button clicked event.
clan::View::child_removed
virtual void child_removed(const std::shared_ptr< View > &view)
Child view was removed from this view.
Definition view.h:336
clan::View::is_height_definite
bool is_height_definite()
Test if the view has a definite height.
clan::View::ViewTree
friend class ViewTree
Definition view.h:364
clan::View::sig_focus_gained
Signal< void(FocusChangeEvent &)> & sig_focus_gained(bool use_capture=false)
View gained focus event.
clan::View::layout_children
virtual void layout_children(Canvas &canvas)
Sets the view geometry for all children of this view.
clan::View::update_cursor
void update_cursor(DisplayWindow &window)
Update window cursor to the cursor used by this view.
clan::View::view_tree
const ViewTree * view_tree() const
Tree in view hierachy.
clan::View::style
const std::shared_ptr< Style > & style(const std::string &state=std::string()) const
Style properties for the specified state.
clan::View::find_view_at
std::shared_ptr< View > find_view_at(const Pointf &pos) const
Find descendant view at the specified content relative position.
clan::View::canvas
Canvas canvas() const
clan::View::content_clipped
bool content_clipped() const
Content clipping flag.
clan::View::from_root_pos
Pointf from_root_pos(const Pointf &pos)
Map from root content to local content coordinates.
clan::View::remove_from_parent
void remove_from_parent()
Remove view from parent.
clan::View::view_transform
const Mat4f & view_transform() const
Current view transform.
clan::View::sig_pointer_double_click
Signal< void(PointerEvent &)> & sig_pointer_double_click(bool use_capture=false)
Pointer button double clicked event.
clan::View::ViewAction
friend class ViewAction
Definition view.h:366
clan::View::clear_exception_encountered
void clear_exception_encountered()
clan::View::set_view_transform
void set_view_transform(const Mat4f &transform)
Specifies the view transform to be applied before its contents and children are rendered.
clan::Mat4f
Mat4< float > Mat4f
Definition mat4.h:521
clan::StandardCursor
StandardCursor
Standard Cursor.
Definition display_window.h:69
clan
Definition clanapp.h:36
clan::FocusPolicy
FocusPolicy
Automatic focus policy.
Definition focus_policy.h:35
clan::Key::e
@ e
Definition keys.h:85