neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
i_layout_item.hpp
Go to the documentation of this file.
1// i_layout_item.hpp
2/*
3 neogfx C++ App/Game Engine
4 Copyright (c) 2018, 2020 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>
28
29namespace neogfx
30{
31 class i_layout;
32 class i_spacer;
33 class i_widget;
34 class i_layout_item_cache;
35
36 struct not_a_layout : std::logic_error { not_a_layout() : std::logic_error("neogfx::not_a_layout") {} };
37 struct not_a_widget : std::logic_error { not_a_widget() : std::logic_error("neogfx::not_a_widget") {} };
38 struct not_a_spacer : std::logic_error { not_a_spacer() : std::logic_error("neogfx::not_a_spacer") {} };
39 struct no_parent_layout : std::logic_error { no_parent_layout() : std::logic_error("neogfx::no_parent_layout") {} };
40 struct no_parent_layout_item : std::logic_error { no_parent_layout_item() : std::logic_error("neogfx::no_parent_layout_item") {} };
41 struct no_parent_widget : std::logic_error { no_parent_widget() : std::logic_error("neogfx::no_parent_widget") {} };
42 struct no_layout_manager : std::logic_error { no_layout_manager() : std::logic_error("neogfx::no_layout_manager") {} };
43 struct layout_item_not_found : std::logic_error { layout_item_not_found() : std::logic_error{ "neogfx::layout_item_not_found" } {} };
44 struct ancestor_layout_type_not_found : std::logic_error { ancestor_layout_type_not_found() : std::logic_error{ "neogfx::ancestor_layout_type_not_found" } {} };
45 struct cannot_fix_weightings : std::logic_error { cannot_fix_weightings() : std::logic_error{ "neogfx::cannot_fix_weightings" } {} };
46
48 {
50 Layout,
51 Spacer,
52 Widget
53 };
54
55 class i_layout_item : public i_reference_counted, public i_property_owner, public i_geometry, public i_anchorable
56 {
57 friend class layout_item_cache;
58 public:
60 public:
61 virtual ~i_layout_item() = default;
62 public:
63 virtual const i_string& id() const = 0;
64 virtual void set_id(const i_string& aId) = 0;
65 public:
66 virtual bool is_cache() const = 0;
67 public:
68 virtual bool is_layout() const = 0;
69 virtual const i_layout& as_layout() const = 0;
70 virtual i_layout& as_layout() = 0;
71 virtual bool is_spacer() const = 0;
72 virtual const i_spacer& as_spacer() const = 0;
73 virtual i_spacer& as_spacer() = 0;
74 virtual bool is_widget() const = 0;
75 virtual const i_widget& as_widget() const = 0;
76 virtual i_widget& as_widget() = 0;
77 public:
78 virtual bool has_parent_layout_item() const = 0;
79 virtual const i_layout_item& parent_layout_item() const = 0;
81 public:
82 virtual bool has_parent_layout() const = 0;
83 virtual const i_layout& parent_layout() const = 0;
84 virtual i_layout& parent_layout() = 0;
85 virtual void set_parent_layout(i_layout* aParentLayout) = 0;
86 virtual bool has_parent_widget() const = 0;
87 virtual const i_widget& parent_widget() const = 0;
88 virtual i_widget& parent_widget() = 0;
89 virtual void set_parent_widget(i_widget* aParentWidget) = 0;
90 virtual bool has_layout_manager() const = 0;
91 virtual const i_widget& layout_manager() const = 0;
92 virtual i_widget& layout_manager() = 0;
93 public:
94 virtual void update_layout(bool aDeferLayout = true, bool aAncestors = false) = 0;
95 virtual void layout_as(const point& aPosition, const size& aSize) = 0;
96 public:
98 virtual void fix_weightings(bool aRecalculate = true) = 0;
99 public:
100 virtual void layout_item_enabled(i_layout_item& aItem) = 0;
101 virtual void layout_item_disabled(i_layout_item& aItem) = 0;
102 public:
103 virtual bool visible() const = 0;
104 public:
105 template <typename LayoutType>
107 {
108 return find_ancestor_layout<LayoutType>() != nullptr;
109 }
110 template <typename LayoutType>
112 {
113 if (has_parent_layout())
114 {
115 auto existing = dynamic_cast<LayoutType*>(&parent_layout());
116 if (existing != nullptr)
117 return existing;
118 return parent_layout().template find_ancestor_layout<LayoutType>();
119 }
120 return nullptr;
121 }
122 template <typename LayoutType>
123 LayoutType& ancestor_layout()
124 {
125 auto existing = find_ancestor_layout<LayoutType>();
126 if (existing != nullptr)
127 return *existing;
129 }
130 bool same_parent_widget_as(i_layout_item const& aOther) const
131 {
132 return has_parent_widget() && aOther.has_parent_widget() && &parent_widget() == &aOther.parent_widget();
133 }
134 public:
135 };
136
137 template <typename LayoutItemType, layout_item_category ParentItemCategory = layout_item_category::Unspecified>
138 class size_policy_of_parent : public LayoutItemType
139 {
140 private:
141 typedef LayoutItemType base_type;
142 public:
143 using base_type::base_type;
144 public:
145 neogfx::size_policy size_policy() const override
146 {
147 if (base_type::has_size_policy())
148 {
149 return base_type::size_policy();
150 }
151 else if (base_type::has_parent_layout_item())
152 {
153 if constexpr (ParentItemCategory == layout_item_category::Layout)
154 {
155 if (base_type::has_parent_layout())
156 return base_type::parent_layout().size_policy();
157 return base_type::size_policy();
158 }
159 else if constexpr (ParentItemCategory == layout_item_category::Widget)
160 {
161 if (base_type::has_parent_widget())
162 return base_type::parent_widget().size_policy();
163 return base_type::size_policy();
164 }
165 else
166 return base_type::parent_layout_item().size_policy();
167 }
168 return base_type::size_policy();
169 }
170 };
171
173 {
174 public:
175 virtual std::uint32_t id() const = 0;
176 virtual void increment_id() = 0;
177 virtual bool& in_progress() = 0;
178 virtual bool& querying_ideal_size() = 0;
179 public:
180 static uuid const& iid() { static uuid const sIid{ 0xd7e05b0f, 0xc4eb, 0x440a, 0x844e, { 0x35, 0x18, 0xc0, 0x48, 0xee, 0x53 } }; return sIid; }
181 };
182
183 inline uint32_t global_layout_id()
184 {
185 return service<i_item_layout>().id();
186 }
187
189 {
190 return service<i_item_layout>().querying_ideal_size();
191 }
192}
virtual std::uint32_t id() const =0
virtual bool & in_progress()=0
virtual bool & querying_ideal_size()=0
virtual void increment_id()=0
static uuid const & iid()
virtual const i_widget & layout_manager() const =0
virtual i_widget & layout_manager()=0
virtual i_layout & as_layout()=0
i_layout_item abstract_type
virtual void layout_as(const point &aPosition, const size &aSize)=0
virtual const i_widget & as_widget() const =0
virtual const i_layout & parent_layout() const =0
virtual bool is_layout() const =0
virtual const i_layout_item & parent_layout_item() const =0
virtual void set_id(const i_string &aId)=0
bool has_ancestor_layout() const
virtual void set_parent_widget(i_widget *aParentWidget)=0
virtual void layout_item_enabled(i_layout_item &aItem)=0
virtual bool has_parent_layout() const =0
virtual i_spacer & as_spacer()=0
virtual bool has_parent_widget() const =0
virtual i_layout & parent_layout()=0
virtual i_layout_item & parent_layout_item()=0
LayoutType & ancestor_layout()
virtual i_widget & parent_widget()=0
virtual const i_spacer & as_spacer() const =0
virtual bool has_parent_layout_item() const =0
virtual bool visible() const =0
LayoutType * find_ancestor_layout()
virtual bool has_layout_manager() const =0
virtual void set_parent_layout(i_layout *aParentLayout)=0
virtual void fix_weightings(bool aRecalculate=true)=0
virtual const i_widget & parent_widget() const =0
virtual i_widget & as_widget()=0
bool same_parent_widget_as(i_layout_item const &aOther) const
virtual void invalidate_combined_transformation()=0
virtual void update_layout(bool aDeferLayout=true, bool aAncestors=false)=0
virtual bool is_widget() const =0
virtual const i_string & id() const =0
virtual bool is_cache() const =0
virtual void layout_item_disabled(i_layout_item &aItem)=0
virtual bool is_spacer() const =0
virtual ~i_layout_item()=default
virtual const i_layout & as_layout() const =0
neogfx::size_policy size_policy() const override
uint32_t global_layout_id()
bool querying_ideal_size()