view.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 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 */
28
29 #pragma once
30
31 #include "../../Core/Math/rect.h"
32 #include "../../Core/Math/easing.h"
33 #include "../../Core/Signals/signal.h"
34 #include "../../UI/Events/event.h"
35 #include "../View/view_geometry.h"
36 #include "../Style/style.h"
37 #include "../Style/style_cascade.h"
38 #include "../Style/style_get_value.h"
39 #include "focus_policy.h"
40 #include <vector>
41 #include <memory>
42 #include <functional>
43
44 namespace clan
45 {
46 class Style;
47 class StyleCascade;
48 class Canvas;
49 class ActivationChangeEvent;
50 class CloseEvent;
51 class FocusChangeEvent;
52 class PointerEvent;
53 class ResizeEvent;
54 class KeyEvent;
55 class ViewImpl;
56 class CursorDescription;
57 enum class StandardCursor;
58 class DisplayWindow;
59
61 class View : public std::enable_shared_from_this<View>
62 {
63 public:
64 View();
65 ~View();
66
68 const StyleCascade &style_cascade() const;
69
71 const std::shared_ptr<Style> &style(const std::string &state = std::string()) const;
72
74 bool state(const std::string &name) const;
75
77 void set_state(const std::string &name, bool value);
78
80 void set_state_cascade(const std::string &name, bool value);
81
83 SlotContainer slots;
84
86 View *superview() const;
87
89 const std::vector<std::shared_ptr<View>> &subviews() const;
90
92 void add_subview(const std::shared_ptr<View> &view);
93
95 void remove_from_super();
96
98 bool hidden() const;
99
101 void set_hidden(bool value = true);
102
104 bool is_static_position_and_visible() const;
105
107 bool needs_layout() const;
108
110 void set_needs_layout();
111
113 const ViewGeometry &geometry() const;
114
118 void set_geometry(const ViewGeometry &geometry);
119
123 Canvas get_canvas() const;
124
126 void set_needs_render();
127
129 bool render_exception_encountered() const;
130
134 void clear_exception_encountered();
135
137 const Mat4f &view_transform() const;
138
140 void set_view_transform(const Mat4f &transform);
141
143 bool content_clipped() const;
144
146 void set_content_clipped(bool clipped);
147
149 virtual float get_preferred_width(Canvas &canvas);
150
152 virtual float get_preferred_height(Canvas &canvas, float width);
153
155 virtual float get_first_baseline_offset(Canvas &canvas, float width);
156
158 virtual float get_last_baseline_offset(Canvas &canvas, float width);
159
161 virtual void layout_subviews(Canvas &canvas);
162
164 const View *root_view() const;
165 View *root_view();
166
168 View *owner_view() const;
169
171 View *focus_view() const;
172
174 bool local_root() const;
175
177 std::shared_ptr<View> find_view_at(const Pointf &pos) const;
178
180 FocusPolicy focus_policy() const;
181
183 void set_focus_policy(FocusPolicy policy);
184
186 unsigned int tab_index() const;
187
189 void set_tab_index(unsigned int index);
190
192 void set_focus();
193
195 void remove_focus();
196
198 bool has_focus() const { return focus_view() == this; }
199
201 void prev_focus();
202
204 void next_focus();
205
207 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()>());
208
210 void stop_animations();
211
213 void set_cursor(const CursorDescription &cursor);
214 void set_cursor(StandardCursor type);
215
217 void set_inherit_cursor();
218
220 Signal<void(ActivationChangeEvent &)> &sig_activated(bool use_capture = false);
221
223 Signal<void(ActivationChangeEvent &)> &sig_deactivated(bool use_capture = false);
224
226 Signal<void(CloseEvent &)> &sig_close(bool use_capture = false);
227
229 Signal<void(ResizeEvent &)> &sig_resize(bool use_capture = false);
230
232 Signal<void(FocusChangeEvent &)> &sig_focus_gained(bool use_capture = false);
233
235 Signal<void(FocusChangeEvent &)> &sig_focus_lost(bool use_capture = false);
236
238 Signal<void(PointerEvent &)> &sig_pointer_enter(bool use_capture = false);
239
241 Signal<void(PointerEvent &)> &sig_pointer_leave(bool use_capture = false);
242
244 Signal<void(PointerEvent &)> &sig_pointer_move(bool use_capture = false);
245
247 Signal<void(PointerEvent &)> &sig_pointer_press(bool use_capture = false);
248
250 Signal<void(PointerEvent &)> &sig_pointer_release(bool use_capture = false);
251
253 Signal<void(PointerEvent &)> &sig_pointer_double_click(bool use_capture = false);
254
256 Signal<void(PointerEvent &)> &sig_pointer_proximity_change(bool use_capture = false);
257
259 Signal<void(KeyEvent &)> &sig_key_press(bool use_capture = false);
260
262 Signal<void(KeyEvent &)> &sig_key_release(bool use_capture = false);
263
265 void update_cursor(DisplayWindow &window);
266
268 Pointf to_screen_pos(const Pointf &pos);
269
271 Pointf from_screen_pos(const Pointf &pos);
272
274 Pointf to_root_pos(const Pointf &pos);
275
277 Pointf from_root_pos(const Pointf &pos);
278
280 static void dispatch_event(View *target, EventUI *e, bool no_propagation = false);
281
282 protected:
284 virtual void render_content(Canvas &canvas) { }
285
287 virtual void subview_added(const std::shared_ptr<View> &view) { }
288
290 virtual void subview_removed(const std::shared_ptr<View> &view) { }
291
292 private:
293 View(const View &) = delete;
294 View &operator=(const View &) = delete;
295
296 std::unique_ptr<ViewImpl> impl;
297
298 friend class RootView;
299 friend class ViewImpl;
300 };
301 }
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::root_view
const View * root_view() const
Root view 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
Definition: clanapp.h:35
clan::View
View for an area of the user interface.
Definition: view.h:61
clan::View::sig_pointer_proximity_change
Signal< void(PointerEvent &)> & sig_pointer_proximity_change(bool use_capture=false)
Pointer proximity change event.
clan::StandardCursor
StandardCursor
Standard Cursor.
Definition: display_window.h:70
clan::StyleTokenType::function
C style comment block.
clan::View::from_root_pos
Pointf from_root_pos(const Pointf &pos)
Map from root content to local content coordinates.
clan::View::sig_focus_lost
Signal< void(FocusChangeEvent &)> & sig_focus_lost(bool use_capture=false)
View lost focus event.
clan::View::to_screen_pos
Pointf to_screen_pos(const Pointf &pos)
Map from local content to screen coordinates.
clan::Canvas
2D Graphics Canvas
Definition: canvas.h:72
clan::View::set_focus_policy
void set_focus_policy(FocusPolicy policy)
Set if this view automatically can gain focus.
clan::ViewGeometry
Definition: view_geometry.h:38
clan::View::sig_activated
Signal< void(ActivationChangeEvent &)> & sig_activated(bool use_capture=false)
Window activated event.
clan::View::sig_key_press
Signal< void(KeyEvent &)> & sig_key_press(bool use_capture=false)
Key pressed event.
clan::View::remove_focus
void remove_focus()
Remove focus from this view.
clan::View::next_focus
void next_focus()
Give focus to the next view in the keyboard tab index order.
clan::View::get_preferred_width
virtual float get_preferred_width(Canvas &canvas)
Calculates the preferred width of this view.
clan::View::sig_pointer_double_click
Signal< void(PointerEvent &)> & sig_pointer_double_click(bool use_capture=false)
Pointer button double clicked event.
clan::View::superview
View * superview() const
Parent view node or nullptr if the view is the current root node.
clan::View::ViewImpl
friend class ViewImpl
Definition: view.h:299
clan::View::focus_view
View * focus_view() const
The view receiving keyboard events or nullptr if no view has the focus.
clan::View::tab_index
unsigned int tab_index() const
Tab index for keyboard focus changes.
clan::View::sig_close
Signal< void(CloseEvent &)> & sig_close(bool use_capture=false)
Window close button clicked event.
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::View::focus_policy
FocusPolicy focus_policy() const
Focus policy active for this view.
clan::View::sig_deactivated
Signal< void(ActivationChangeEvent &)> & sig_deactivated(bool use_capture=false)
Window deactivated event.
clan::View::has_focus
bool has_focus() const
Test if this view is receiving keyboard input.
Definition: view.h:198
clan::View::clear_exception_encountered
void clear_exception_encountered()
clan::View::render_exception_encountered
bool render_exception_encountered() const
Test if this view generated an exception during rendering.
clan::Easing::linear
static float linear(float t)
clan::View::sig_pointer_leave
Signal< void(PointerEvent &)> & sig_pointer_leave(bool use_capture=false)
Pointer leaving view geometry event.
clan::View::subviews
const std::vector< std::shared_ptr< View > > & subviews() const
List of all immediate child views.
clan::StyleTokenType::string
@ keyword
clan::View::sig_pointer_press
Signal< void(PointerEvent &)> & sig_pointer_press(bool use_capture=false)
Pointer button pressed event.
clan::View::remove_from_super
void remove_from_super()
Remove view from parent.
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::set_content_clipped
void set_content_clipped(bool clipped)
Specifies if content should be clipped during rendering.
clan::View::get_canvas
Canvas get_canvas() const
clan::EventUI
Base class for events being dispatched through the view hiarchy.
Definition: UI/Events/event.h:47
clan::View::~View
~View()
clan::View::local_root
bool local_root() const
Indicates if the view acts as a local root for layout and rendering.
clan::View::sig_key_release
Signal< void(KeyEvent &)> & sig_key_release(bool use_capture=false)
Key released event.
clan::View::View
View()
clan::View::render_content
virtual void render_content(Canvas &canvas)
Renders the content of a view.
Definition: view.h:284
clan::View::sig_focus_gained
Signal< void(FocusChangeEvent &)> & sig_focus_gained(bool use_capture=false)
View gained focus event.
clan::View::add_subview
void add_subview(const std::shared_ptr< View > &view)
Add a child view.
clan::CursorDescription
This class contains everything to construct a cursor - its data, default settings etc...
Definition: cursor_description.h:70
clan::View::content_clipped
bool content_clipped() const
Content clipping flag.
clan::Signal
Definition: signal.h:104
clan::View::layout_subviews
virtual void layout_subviews(Canvas &canvas)
Sets the view geometry for all subviews of this view.
clan::View::set_state_cascade
void set_state_cascade(const std::string &name, bool value)
Sets the state for this view and all siblings recursively, until a manually set state of the same nam...
clan::View::set_cursor
void set_cursor(const CursorDescription &cursor)
Set the cursor icon used when cursor is above this view.
clan::View::subview_removed
virtual void subview_removed(const std::shared_ptr< View > &view)
Child view was removed from this view.
Definition: view.h:290
clan::View::sig_pointer_move
Signal< void(PointerEvent &)> & sig_pointer_move(bool use_capture=false)
Pointer moved above view event.
clan::View::from_screen_pos
Pointf from_screen_pos(const Pointf &pos)
Map from screen to local content coordinates.
clan::View::get_preferred_height
virtual float get_preferred_height(Canvas &canvas, float width)
Calculates the preferred height of this view.
clan::Key::e
clan::View::owner_view
View * owner_view() const
View hierachy owner or nullptr if there is no owner.
clan::View::set_geometry
void set_geometry(const ViewGeometry &geometry)
clan::View::hidden
bool hidden() const
Test if view is set to hidden.
clan::SlotContainer
Definition: signal.h:140
clan::View::stop_animations
void stop_animations()
Stop all activate animation functions.
clan::View::get_last_baseline_offset
virtual float get_last_baseline_offset(Canvas &canvas, float width)
Calculates the offset to the last baseline.
clan::View::get_first_baseline_offset
virtual float get_first_baseline_offset(Canvas &canvas, float width)
Calculates the offset to the first baseline.
clan::View::set_focus
void set_focus()
Set this view as the focused view.
clan::View::update_cursor
void update_cursor(DisplayWindow &window)
Update window cursor to the cursor used by this view.
clan::FocusPolicy
FocusPolicy
Automatic focus policy.
Definition: focus_policy.h:34
clan::View::needs_layout
bool needs_layout() const
Test if view geometry needs to be recalculated.
clan::View::sig_resize
Signal< void(ResizeEvent &)> & sig_resize(bool use_capture=false)
Window resize event.
clan::View::slots
SlotContainer slots
Slot container helping with automatic disconnection of connected slots when the view is destroyed...
Definition: view.h:83
clan::RootView
Base class for views participating as a root view in the user interface.
Definition: root_view.h:37
clan::StyleCascade
Style value resolver.
Definition: style_cascade.h:65
clan::View::view_transform
const Mat4f & view_transform() const
Current view transform.
clan::Mat4
4D matrix
Definition: mat2.h:51
clan::View::style_cascade
const StyleCascade & style_cascade() const
Style cascade currently active for this view.
clan::View::sig_pointer_enter
Signal< void(PointerEvent &)> & sig_pointer_enter(bool use_capture=false)
Pointer entering view geometry event.
clan::View::sig_pointer_release
Signal< void(PointerEvent &)> & sig_pointer_release(bool use_capture=false)
Pointer button released event.
clan::View::is_static_position_and_visible
bool is_static_position_and_visible() const
Test if view should participate in static layout calculations (layout_subviews)
clan::Pointf
2D (x,y) point structure - Float
Definition: point.h:72
clan::View::set_tab_index
void set_tab_index(unsigned int index)
Sets the tab index used for keyboard focus changes.
clan::View::geometry
const ViewGeometry & geometry() const
Actual view position and size after layout.
clan::View::set_inherit_cursor
void set_inherit_cursor()
Specify that the cursor icon is inherited from the super view.
clan::View::to_root_pos
Pointf to_root_pos(const Pointf &pos)
Map from local content to root content coordinates.
clan::View::set_needs_layout
void set_needs_layout()
Forces recalculation of view geometry before next rendering.
clan::DisplayWindow
Top-level window class.
Definition: display_window.h:101
clan::View::state
bool state(const std::string &name) const
Test if a style state is currently set.
clan::View::subview_added
virtual void subview_added(const std::shared_ptr< View > &view)
Child view was added to this view.
Definition: view.h:287
clan::View::set_hidden
void set_hidden(bool value=true)
Hides a view from layout and rendering.
clan::View::prev_focus
void prev_focus()
Give focus to the previous view in the keyboard tab index order.
clan::View::set_needs_render
void set_needs_render()
Signals this view needs to be rendered again.
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::set_state
void set_state(const std::string &name, bool value)
Set or clear style state.