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 
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 
96 
98  bool hidden() const;
99 
101  void set_hidden(bool value = true);
102 
105 
107  bool needs_layout() const;
108 
111 
113  const ViewGeometry &geometry() const;
114 
119 
124 
127 
130 
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;
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 
181 
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 
211 
213  void set_cursor(const CursorDescription &cursor);
215 
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 
266 
269 
272 
274  Pointf to_root_pos(const Pointf &pos);
275 
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 }
void prev_focus()
Give focus to the previous view in the keyboard tab index order.
Pointf from_root_pos(const Pointf &pos)
Map from root content to local content coordinates.
Signal< void(FocusChangeEvent &)> & sig_focus_lost(bool use_capture=false)
View lost focus event.
Signal< void(KeyEvent &)> & sig_key_press(bool use_capture=false)
Key pressed event.
Signal< void(PointerEvent &)> & sig_pointer_press(bool use_capture=false)
Pointer button pressed event.
const Mat4f & view_transform() const
Current view transform.
Signal< void(FocusChangeEvent &)> & sig_focus_gained(bool use_capture=false)
View gained focus event.
const std::vector< std::shared_ptr< View > > & subviews() const
List of all immediate child views.
Pointer (mouse/tablet) event.
Definition: pointer_event.h:68
void remove_focus()
Remove focus from this view.
std::shared_ptr< View > find_view_at(const Pointf &pos) const
Find descendant view at the specified content relative position.
virtual float get_preferred_width(Canvas &canvas)
Calculates the preferred width of this view.
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.
void set_needs_render()
Signals this view needs to be rendered again.
Signal< void(PointerEvent &)> & sig_pointer_enter(bool use_capture=false)
Pointer entering view geometry event.
void remove_from_super()
Remove view from parent.
bool local_root() const
Indicates if the view acts as a local root for layout and rendering.
void set_focus_policy(FocusPolicy policy)
Set if this view automatically can gain focus.
friend class ViewImpl
Definition: view.h:299
Window resize event.
Definition: resize_event.h:37
Window was activated or deactivated event.
Definition: activation_change_event.h:44
bool is_static_position_and_visible() const
Test if view should participate in static layout calculations (layout_subviews)
Signal< void(KeyEvent &)> & sig_key_release(bool use_capture=false)
Key released event.
Pointf to_root_pos(const Pointf &pos)
Map from local content to root content coordinates.
Signal< void(PointerEvent &)> & sig_pointer_double_click(bool use_capture=false)
Pointer button double clicked event.
void set_geometry(const ViewGeometry &geometry)
View focus changed event.
Definition: focus_change_event.h:44
Signal< void(ResizeEvent &)> & sig_resize(bool use_capture=false)
Window resize event.
FocusPolicy
Automatic focus policy.
Definition: focus_policy.h:35
2D (x,y) point structure - Float
Definition: point.h:73
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...
virtual float get_preferred_height(Canvas &canvas, float width)
Calculates the preferred height of this view.
Signal< void(ActivationChangeEvent &)> & sig_deactivated(bool use_capture=false)
Window deactivated event.
Definition: signal.h:105
bool state(const std::string &name) const
Test if a style state is currently set.
void update_cursor(DisplayWindow &window)
Update window cursor to the cursor used by this view.
View * focus_view() const
The view receiving keyboard events or nullptr if no view has the focus.
void clear_exception_encountered()
void next_focus()
Give focus to the next view in the keyboard tab index order.
2D Graphics Canvas
Definition: canvas.h:73
const StyleCascade & style_cascade() const
Style cascade currently active for this view.
Signal< void(PointerEvent &)> & sig_pointer_proximity_change(bool use_capture=false)
Pointer proximity change event.
void set_cursor(const CursorDescription &cursor)
Set the cursor icon used when cursor is above this view.
Base class for views participating as a root view in the user interface.
Definition: root_view.h:38
virtual float get_first_baseline_offset(Canvas &canvas, float width)
Calculates the offset to the first baseline.
static void dispatch_event(View *target, EventUI *e, bool no_propagation=false)
Dispatch event to signals listening for events.
bool content_clipped() const
Content clipping flag.
void set_cursor(StandardCursor type)
static float linear(float t)
void set_content_clipped(bool clipped)
Specifies if content should be clipped during rendering.
unsigned int tab_index() const
Tab index for keyboard focus changes.
bool needs_layout() const
Test if view geometry needs to be recalculated.
Canvas get_canvas() const
bool hidden() const
Test if view is set to hidden.
View for an area of the user interface.
Definition: view.h:62
void set_view_transform(const Mat4f &transform)
Specifies the view transform to be applied before its contents and children are rendered.
Base class for events being dispatched through the view hiarchy.
Definition: UI/Events/event.h:48
FocusPolicy focus_policy() const
Focus policy active for this view.
Pointf to_screen_pos(const Pointf &pos)
Map from local content to screen coordinates.
Pointf from_screen_pos(const Pointf &pos)
Map from screen to local content coordinates.
virtual void render_content(Canvas &canvas)
Renders the content of a view.
Definition: view.h:284
void set_focus()
Set this view as the focused view.
bool has_focus() const
Test if this view is receiving keyboard input.
Definition: view.h:198
4D matrix
Definition: mat2.h:51
virtual void subview_removed(const std::shared_ptr< View > &view)
Child view was removed from this view.
Definition: view.h:290
This class contains everything to construct a cursor - its data, default settings etc.
Definition: cursor_description.h:71
virtual void subview_added(const std::shared_ptr< View > &view)
Child view was added to this view.
Definition: view.h:287
View * root_view()
Definition: clanapp.h:36
void set_needs_layout()
Forces recalculation of view geometry before next rendering.
Signal< void(PointerEvent &)> & sig_pointer_release(bool use_capture=false)
Pointer button released event.
Signal< void(CloseEvent &)> & sig_close(bool use_capture=false)
Window close button clicked event.
void set_tab_index(unsigned int index)
Sets the tab index used for keyboard focus changes.
Signal< void(PointerEvent &)> & sig_pointer_move(bool use_capture=false)
Pointer moved above view event.
Window close button was clicked event.
Definition: close_event.h:37
void stop_animations()
Stop all activate animation functions.
const View * root_view() const
Root view in view hierachy.
Definition: view_geometry.h:39
virtual void layout_subviews(Canvas &canvas)
Sets the view geometry for all subviews of this view.
Definition: signal.h:141
void set_hidden(bool value=true)
Hides a view from layout and rendering.
StandardCursor
Standard Cursor.
Definition: display_window.h:71
SlotContainer slots
Slot container helping with automatic disconnection of connected slots when the view is destroyed.
Definition: view.h:83
Signal< void(PointerEvent &)> & sig_pointer_leave(bool use_capture=false)
Pointer leaving view geometry event.
View * owner_view() const
View hierachy owner or nullptr if there is no owner.
void add_subview(const std::shared_ptr< View > &view)
Add a child view.
Top-level window class.
Definition: display_window.h:102
void set_inherit_cursor()
Specify that the cursor icon is inherited from the super view.
const ViewGeometry & geometry() const
Actual view position and size after layout.
Style value resolver.
Definition: style_cascade.h:66
Keyboard key event.
Definition: key_event.h:48
Signal< void(ActivationChangeEvent &)> & sig_activated(bool use_capture=false)
Window activated event.
const std::shared_ptr< Style > & style(const std::string &state=std::string()) const
Style properties for the specified state.
View * superview() const
Parent view node or nullptr if the view is the current root node.
void set_state(const std::string &name, bool value)
Set or clear style state.
bool render_exception_encountered() const
Test if this view generated an exception during rendering.
virtual float get_last_baseline_offset(Canvas &canvas, float width)
Calculates the offset to the last baseline.