neoGFX
Cross-platform C++ app/game engine
widget.hpp
Go to the documentation of this file.
1 // widget.hpp
2 /*
3  neogfx C++ GUI Library
4  Copyright (c) 2015 Leigh Johnston. All Rights Reserved.
5 
6  This program is free software: you can redistribute it and / or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include <neogfx/neogfx.hpp>
23 #include <neolib/timer.hpp>
24 #include <neogfx/core/object.hpp>
25 #include <neogfx/core/property.hpp>
28 
29 namespace neogfx
30 {
31  class widget : public layout_item<object<i_widget>>
32  {
33  public:
34  define_declared_event(VisibilityChanged, visibility_changed)
35  define_declared_event(PositionChanged, position_changed)
36  define_declared_event(SizeChanged, size_changed)
37  define_declared_event(LayoutCompleted, layout_completed)
38  define_declared_event(Painting, painting, i_graphics_context&)
40  define_declared_event(ChildrenPainted, children_painted, i_graphics_context&)
42  define_declared_event(NonClientMouse, non_client_mouse_event, const neogfx::non_client_mouse_event&)
44  define_declared_event(Focus, focus_event, neogfx::focus_event)
45  public:
46  typedef i_widget abstract_type;
47  private:
49  public:
50  widget();
51  widget(const widget&) = delete;
52  widget(i_widget& aParent);
53  widget(i_layout& aLayout);
54  ~widget();
55  // i_object
56  public:
57  void property_changed(i_property& aProperty) override;
58  // i_widget
59  public:
60  bool is_singular() const override;
61  void set_singular(bool aSingular) override;
62  bool is_root() const override;
63  bool has_root() const override;
64  const i_window& root() const override;
65  i_window& root() override;
66  bool has_parent() const override;
67  const i_widget& parent() const override;
68  i_widget& parent() override;
69  void set_parent(i_widget& aParent) override;
70  void parent_changed() override;
71  bool adding_child() const override;
72  i_widget& add(i_widget& aChild) override;
73  i_widget& add(std::shared_ptr<i_widget> aChild) override;
74  std::shared_ptr<i_widget> remove(i_widget& aChild, bool aSingular = false) override;
75  void remove_all() override;
76  bool has_children() const override;
77  const widget_list& children() const override;
78  widget_list::const_iterator last() const override;
79  widget_list::iterator last() override;
80  widget_list::const_iterator find(const i_widget& aChild, bool aThrowIfNotFound = true) const override;
81  widget_list::iterator find(const i_widget& aChild, bool aThrowIfNotFound = true) override;
82  const i_widget& before() const override;
83  i_widget& before() override;
84  const i_widget& after() const override;
85  i_widget& after() override;
86  void link_before(i_widget* aPreviousWidget) override;
87  void link_after(i_widget* aNextWidget) override;
88  void unlink() override;
89  bool has_layout() const override;
90  void set_layout(i_layout& aLayout, bool aMoveExistingItems = true) override;
91  void set_layout(std::shared_ptr<i_layout> aLayout, bool aMoveExistingItems = true) override;
92  const i_layout& layout() const override;
93  i_layout& layout() override;
94  bool can_defer_layout() const override;
95  bool has_layout_manager() const override;
96  const i_widget& layout_manager() const override;
97  i_widget& layout_manager() override;
98  bool is_managing_layout() const override;
99  void layout_items(bool aDefer = false) override;
100  void layout_items_started() override;
101  bool layout_items_in_progress() const override;
102  void layout_items_completed() override;
103  // i_units_context
104  public:
105  bool high_dpi() const override;
106  dimension dpi_scale_factor() const override;
107  public:
108  bool device_metrics_available() const override;
109  const i_device_metrics& device_metrics() const override;
110  // i_geometry
111  public:
112  point position() const override;
113  void set_position(const point& aPosition) override;
114  size extents() const override;
115  void set_extents(const size& aSize) override;
116  bool has_size_policy() const override;
117  neogfx::size_policy size_policy() const override;
118  void set_size_policy(const optional_size_policy& aSizePolicy, bool aUpdateLayout = true) override;
119  bool has_weight() const override;
120  size weight() const override;
121  void set_weight(const optional_size& aWeight, bool aUpdateLayout = true) override;
122  bool has_minimum_size() const override;
123  size minimum_size(const optional_size& aAvailableSpace = {}) const override;
124  void set_minimum_size(const optional_size& aMinimumSize, bool aUpdateLayout = true) override;
125  bool has_maximum_size() const override;
126  size maximum_size(const optional_size& aAvailableSpace = {}) const override;
127  void set_maximum_size(const optional_size& aMaximumSize, bool aUpdateLayout = true) override;
128  bool has_fixed_size() const override;
129  size fixed_size() const override;
130  void set_fixed_size(const optional_size& aFixedSize, bool aUpdateLayout = true) override;
131  bool has_margins() const override;
132  neogfx::margins margins() const override;
133  void set_margins(const optional_margins& aMargins, bool aUpdateLayout = true) override;
134  // i_skinnable_item
135  public:
136  bool is_widget() const override;
137  const i_widget& as_widget() const override;
138  public:
139  rect element_rect(skin_element aElement) const override;
140  // i_widget
141  public:
142  bool has_logical_coordinate_system() const override;
144  void set_logical_coordinate_system(const optional_logical_coordinate_system& aLogicalCoordinateSystem) override;
145  point origin() const override;
146  rect non_client_rect() const override;
147  rect client_rect(bool aIncludeMargins = true) const override;
148  void move(const point& aPosition) override;
149  void moved() override;
150  void parent_moved() override;
151  void resize(const size& aSize) override;
152  void resized() override;
153  const i_widget& get_widget_at(const point& aPosition) const override;
154  i_widget& get_widget_at(const point& aPosition) override;
155  bool part_active(widget_part aPart) const override;
156  widget_part part(const point& aPosition) const override;
157  widget_part hit_test(const point& aPosition) const override;
158  // i_layout_item
159  public:
160  bool is_layout() const override;
161  const i_layout& as_layout() const override;
162  i_layout& as_layout() override;
163  i_widget& as_widget() override;
164  public:
165  bool has_parent_layout() const override;
166  const i_layout& parent_layout() const override;
167  i_layout& parent_layout() override;
168  void set_parent_layout(i_layout* aParentLayout) override;
169  bool has_layout_owner() const override;
170  const i_widget& layout_owner() const override;
171  i_widget& layout_owner() override;
172  void set_layout_owner(i_widget* aOwner) override;
173  bool is_proxy() const override;
174  const i_layout_item_proxy& proxy_for_layout() const override;
176  public:
177  void layout_as(const point& aPosition, const size& aSize) override;
178  // i_widget
179  public:
180  using i_widget::update;
181  bool update(const rect& aUpdateRect) override;
182  bool requires_update() const override;
183  rect update_rect() const override;
184  rect default_clip_rect(bool aIncludeNonClient = false) const override;
185  bool ready_to_render() const override;
186  void render(i_graphics_context& aGc) const override;
187  bool transparent_background() const override;
188  void paint_non_client(i_graphics_context& aGc) const override;
189  void paint(i_graphics_context& aGc) const override;
190  void paint_non_client_after(i_graphics_context& aGc) const override;
191  public:
192  double opacity() const override;
193  void set_opacity(double aOpacity) override;
194  double transparency() const override;
195  void set_transparency(double aTransparency) override;
196  bool has_foreground_color() const override;
197  color foreground_color() const override;
198  void set_foreground_color(const optional_color& aForegroundColor = optional_color{}) override;
199  bool has_background_color() const override;
200  color background_color() const override;
201  void set_background_color(const optional_color& aBackgroundColor = optional_color{}) override;
202  color container_background_color() const override;
203  bool has_font() const override;
204  const neogfx::font& font() const override;
205  void set_font(const optional_font& aFont) override;
206  public:
207  bool visible() const override;
208  bool effectively_visible() const override;
209  bool hidden() const override;
210  bool effectively_hidden() const override;
211  bool show(bool aVisible) override;
212  bool enabled() const override;
213  bool effectively_enabled() const override;
214  bool disabled() const override;
215  bool effectively_disabled() const override;
216  bool enable(bool aEnable) override;
217  bool entered() const override;
218  bool can_capture() const override;
219  bool capturing() const override;
220  const optional_point& capture_position() const override;
221  void set_capture(capture_reason aReason = capture_reason::Other, const optional_point& aPosition = {}) override;
222  void release_capture(capture_reason aReason = capture_reason::Other) override;
223  void non_client_set_capture() override;
224  void non_client_release_capture() override;
225  void captured() override;
226  void capture_released() override;
227  neogfx::focus_policy focus_policy() const override;
228  void set_focus_policy(neogfx::focus_policy aFocusPolicy) override;
229  bool can_set_focus(focus_reason aFocusReason) const override;
230  bool has_focus() const override;
231  bool child_has_focus() const override;
232  void set_focus(focus_reason aFocusReason = focus_reason::Other) override;
233  void release_focus() override;
234  void focus_gained(focus_reason aFocusReason) override;
235  void focus_lost(focus_reason aFocusReason) override;
236  public:
237  bool ignore_mouse_events() const override;
238  void set_ignore_mouse_events(bool aIgnoreMouseEvents) override;
239  bool ignore_non_client_mouse_events() const override;
240  void set_ignore_non_client_mouse_events(bool aIgnoreNonClientMouseEvents) override;
241  bool mouse_event_is_non_client() const override;
242  void mouse_wheel_scrolled(mouse_wheel aWheel, const point& aPosition, delta aDelta, key_modifiers_e aKeyModifiers) override;
243  void mouse_button_pressed(mouse_button aButton, const point& aPosition, key_modifiers_e aKeyModifiers) override;
244  void mouse_button_double_clicked(mouse_button aButton, const point& aPosition, key_modifiers_e aKeyModifiers) override;
245  void mouse_button_released(mouse_button aButton, const point& aPosition) override;
246  void mouse_moved(const point& aPosition, key_modifiers_e aKeyModifiers) override;
247  void mouse_entered(const point& aPosition) override;
248  void mouse_left() override;
249  neogfx::mouse_cursor mouse_cursor() const override;
250  public:
251  bool key_pressed(scan_code_e aScanCode, key_code_e aKeyCode, key_modifiers_e aKeyModifiers) override;
252  bool key_released(scan_code_e aScanCode, key_code_e aKeyCode, key_modifiers_e aKeyModifiers) override;
253  bool text_input(const std::string& aText) override;
254  bool sys_text_input(const std::string& aText) override;
255  public:
256  const i_widget& widget_for_mouse_event(const point& aPosition, bool aForHitTest = false) const override;
257  i_widget& widget_for_mouse_event(const point& aPosition, bool aForHitTest = false) override;
258  // helpers
259  public:
261  using i_widget::show;
262  using i_widget::hide;
263  using i_widget::enable;
264  using i_widget::disable;
265  // state
266  private:
267  bool iSingular;
268  i_widget* iParent;
269  mutable std::optional<const i_window*> iRoot;
270  mutable std::optional<bool> iDeviceMetricsAvailable;
271  widget_list iChildren;
272  bool iAddingChild;
273  i_widget* iLinkBefore;
274  i_widget* iLinkAfter;
275  i_layout* iParentLayout;
276  uint32_t iLayoutInProgress;
277  std::shared_ptr<i_layout> iLayout;
278  class layout_timer;
279  std::unique_ptr<layout_timer> iLayoutTimer;
280  mutable std::pair<optional_rect, optional_rect> iDefaultClipRect;
281  mutable optional_point iOrigin;
282  optional_point iCapturePosition;
283  // properties / anchors
284  public:
285  define_property(property_category::hard_geometry, optional_logical_coordinate_system, LogicalCoordinateSystem, logical_coordinate_system)
286  define_property(property_category::hard_geometry, bool, Visible, visible, true)
289  define_property(property_category::other_appearance, double, Opacity, opacity, 1.0)
290  define_property(property_category::color, optional_color, ForegroundColor, foreground_color)
291  define_property(property_category::color, optional_color, BackgroundColor, background_color)
293  define_property(property_category::other, bool, IgnoreMouseEvents, ignore_mouse_events, false)
294  define_property(property_category::other, bool, IgnoreNonClientMouseEvents, ignore_non_client_mouse_events, true)
295  };
296 }
size extents() const override
const i_window & root() const override
void layout_items(bool aDefer=false) override
bool has_parent() const override
point position() const override
void set_layout(i_layout &aLayout, bool aMoveExistingItems=true) override
rect element_rect(skin_element aElement) const override
void unlink() override
bool sys_text_input(const std::string &aText) override
bool has_foreground_color() const override
double transparency() const override
void focus_lost(focus_reason aFocusReason) override
void set_parent_layout(i_layout *aParentLayout) override
void captured() override
void set_ignore_mouse_events(bool aIgnoreMouseEvents) override
bool is_singular() const override
void set_logical_coordinate_system(const optional_logical_coordinate_system &aLogicalCoordinateSystem) override
neogfx::mouse_cursor mouse_cursor() const override
void release_focus() override
void mouse_button_double_clicked(mouse_button aButton, const point &aPosition, key_modifiers_e aKeyModifiers) override
void set_transparency(double aTransparency) override
void layout_items_started() override
std::optional< color > optional_color
Definition: color.hpp:1021
bool part_active(widget_part aPart) const override
bool is_layout() const override
widget_part hit_test(const point &aPosition) const override
bool is_widget() const override
void set_opacity(double aOpacity) override
void paint_non_client(i_graphics_context &aGc) const override
bool has_layout_owner() const override
const i_widget & get_widget_at(const point &aPosition) const override
std::optional< margins > optional_margins
position_changed layout_completed painted
Definition: widget.hpp:39
bool entered() const override
rect non_client_rect() const override
bool can_capture() const override
void non_client_release_capture() override
const i_device_metrics & device_metrics() const override
void set_position(const point &aPosition) override
bool has_maximum_size() const override
void link_after(i_widget *aNextWidget) override
bool has_size_policy() const override
void mouse_wheel_scrolled(mouse_wheel aWheel, const point &aPosition, delta aDelta, key_modifiers_e aKeyModifiers) override
void set_layout_owner(i_widget *aOwner) override
std::optional< font > optional_font
Definition: font.hpp:203
void set_font(const optional_font &aFont) override
widget_part part(const point &aPosition) const override
widget_list::const_iterator find(const i_widget &aChild, bool aThrowIfNotFound=true) const override
logical_coordinate_system
Definition: geometrical.hpp:48
void set_focus_policy(neogfx::focus_policy aFocusPolicy) override
void set_background_color(const optional_color &aBackgroundColor=optional_color{}) override
void parent_changed() override
bool visible() const override
neogfx::margins margins() const override
bool transparent_background() const override
const i_widget & parent() const override
std::optional< size_policy > optional_size_policy
Definition: i_geometry.hpp:194
color container_background_color() const override
void set_parent(i_widget &aParent) override
void set_singular(bool aSingular) override
bool show(bool aVisible) override
rect default_clip_rect(bool aIncludeNonClient=false) const override
bool update(const rect &aUpdateRect) override
point origin() const override
const widget_list & children() const override
const i_widget & layout_manager() const override
void mouse_entered(const point &aPosition) override
bool text_input(const std::string &aText) override
void set_focus(focus_reason aFocusReason=focus_reason::Other) override
bool effectively_disabled() const override
std::optional< size > optional_size
void set_capture(capture_reason aReason=capture_reason::Other, const optional_point &aPosition={}) override
bool has_background_color() const override
void set_minimum_size(const optional_size &aMinimumSize, bool aUpdateLayout=true) override
void set_margins(const optional_margins &aMargins, bool aUpdateLayout=true) override
void parent_moved() override
virtual void set_size_policy(const optional_size_policy &aSizePolicy, bool aUpdateLayout=true)=0
void release_capture(capture_reason aReason=capture_reason::Other) override
size minimum_size(const optional_size &aAvailableSpace={}) const override
void link_before(i_widget *aPreviousWidget) override
bool effectively_visible() const override
bool is_proxy() const override
void paint_non_client_after(i_graphics_context &aGc) const override
bool has_margins() const override
const i_widget & as_widget() const override
define_declared_event(VisibilityChanged, visibility_changed) define_declared_event(PositionChanged
bool ready_to_render() const override
neogfx::focus_policy focus_policy() const override
std::optional< logical_coordinate_system > optional_logical_coordinate_system
Definition: geometrical.hpp:76
void moved() override
void resized() override
virtual bool update(const rect &aUpdateRect)=0
void property_changed(i_property &aProperty) override
size maximum_size(const optional_size &aAvailableSpace={}) const override
void layout_items_completed() override
void set_maximum_size(const optional_size &aMaximumSize, bool aUpdateLayout=true) override
bool has_focus() const override
bool key_released(scan_code_e aScanCode, key_code_e aKeyCode, key_modifiers_e aKeyModifiers) override
const i_layout & as_layout() const override
bool adding_child() const override
bool child_has_focus() const override
size fixed_size() const override
bool has_children() const override
bool is_root() const override
color background_color() const override
mouse_wheel
Definition: i_mouse.hpp:41
bool effectively_enabled() const override
neogfx::logical_coordinate_system logical_coordinate_system() const override
bool layout_items_in_progress() const override
bool has_logical_coordinate_system() const override
const i_widget & after() const override
dimension dpi_scale_factor() const override
bool ignore_mouse_events() const override
bool can_set_focus(focus_reason aFocusReason) const override
size weight() const override
bool has_root() const override
void set_size_policy(const optional_size_policy &aSizePolicy, bool aUpdateLayout=true) override
const i_widget & before() const override
bool hidden() const override
void resize(const size &aSize) override
bool ignore_non_client_mouse_events() const override
void mouse_button_released(mouse_button aButton, const point &aPosition) override
void set_ignore_non_client_mouse_events(bool aIgnoreNonClientMouseEvents) override
bool enable(bool aEnable) override
void remove_all() override
const i_widget & layout_owner() const override
bool has_font() const override
void set_foreground_color(const optional_color &aForegroundColor=optional_color{}) override
bool has_weight() const override
color foreground_color() const override
std::shared_ptr< i_widget > remove(i_widget &aChild, bool aSingular=false) override
mouse_button
Definition: i_mouse.hpp:30
widget_list::const_iterator last() const override
void layout_as(const point &aPosition, const size &aSize) override
bool effectively_hidden() const override
bool has_layout_manager() const override
bool has_minimum_size() const override
const i_layout_item_proxy & proxy_for_layout() const override
const i_widget & widget_for_mouse_event(const point &aPosition, bool aForHitTest=false) const override
bool capturing() const override
default_geometry_value_type dimension
Definition: geometrical.hpp:34
bool key_pressed(scan_code_e aScanCode, key_code_e aKeyCode, key_modifiers_e aKeyModifiers) override
void non_client_set_capture() override
void mouse_button_pressed(mouse_button aButton, const point &aPosition, key_modifiers_e aKeyModifiers) override
void set_fixed_size(const optional_size &aFixedSize, bool aUpdateLayout=true) override
void set_weight(const optional_size &aWeight, bool aUpdateLayout=true) override
bool is_managing_layout() const override
i_widget & add(i_widget &aChild) override
rect update_rect() const override
const optional_point & capture_position() const override
bool high_dpi() const override
void capture_released() override
bool device_metrics_available() const override
double opacity() const override
void mouse_moved(const point &aPosition, key_modifiers_e aKeyModifiers) override
bool can_defer_layout() const override
void move(const point &aPosition) override
bool has_fixed_size() const override
#define define_property(category, type, name, calculator,...)
Definition: property.hpp:316
void focus_gained(focus_reason aFocusReason) override
void paint(i_graphics_context &aGc) const override
bool enabled() const override
bool mouse_event_is_non_client() const override
void render(i_graphics_context &aGc) const override
bool has_parent_layout() const override
bool disabled() const override
std::optional< point > optional_point
bool has_layout() const override
void mouse_left() override
rect client_rect(bool aIncludeMargins=true) const override
const i_layout & parent_layout() const override
void set_extents(const size &aSize) override
bool requires_update() const override
const neogfx::font & font() const override