[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / flex-container / flex-container-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_FLEX_CONTAINER_H
2 #define DALI_TOOLKIT_INTERNAL_FLEX_CONTAINER_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/weak-handle.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/controls/control-impl.h>
26 #include <dali-toolkit/public-api/controls/flex-container/flex-container.h>
27 #include <dali-toolkit/third-party/yoga/Yoga.h>
28
29 namespace Dali
30 {
31 namespace Toolkit
32 {
33 namespace Internal
34 {
35 /**
36  * FlexContainer is a custom control for laying out actors in a Flexbox layout
37  * @see Dali::Toolkit:FlexContainer for more details
38  */
39 class FlexContainer : public Control
40 {
41 public:
42   /**
43    * The structure to store the style properties and layout information of flex item
44    */
45   struct FlexItemNode
46   {
47     WeakHandle<Dali::Actor> actor; ///< Actor handle of the flex item
48     YGNodeRef               node;  ///< The style properties and layout information
49   };
50
51   typedef std::vector<FlexItemNode> FlexItemNodeContainer;
52
53 public:
54   /**
55    * Construct a new FlexContainer.
56    */
57   FlexContainer();
58
59   /**
60    * Create a new FlexContainer.
61    * @return A smart-pointer to the newly allocated FlexContainer.
62    */
63   static Toolkit::FlexContainer New();
64
65   /**
66    * @brief Set the primary direction in which content is ordered.
67    * @param[in] contentDirection The direction of the content.
68    */
69   void SetContentDirection(Toolkit::FlexContainer::ContentDirection contentDirection);
70
71   /**
72    * @brief Get the direction of the content.
73    * @return The direction of the content.
74    */
75   Toolkit::FlexContainer::ContentDirection GetContentDirection();
76
77   /**
78    * @brief Set the direction flex items are laid out.
79    * @param[in] flexDirection The direction flex items are laid out.
80    */
81   void SetFlexDirection(Toolkit::FlexContainer::FlexDirection flexDirection);
82
83   /**
84    * @brief Get the direction flex items are laid out.
85    * @return The direction flex items are laid out.
86    */
87   Toolkit::FlexContainer::FlexDirection GetFlexDirection();
88
89   /**
90    * @brief Set whether the flex items should wrap or not, if there
91    * is no enough room for them on one flex line.
92    * @param[in] flexWrap The wrap type.
93    */
94   void SetFlexWrap(Toolkit::FlexContainer::WrapType flexWrap);
95
96   /**
97    * @brief Get whether the flex items should wrap or not, if there
98    * is no enough room for them on one flex line.
99    * @return The wrap type.
100    */
101   Toolkit::FlexContainer::WrapType GetFlexWrap();
102
103   /**
104    * @brief Set the horizontal alignment of the flex items when the items
105    * do not use all available space on the main-axis.
106    * @param[in] justifyContent The horizontal alignment of flex items.
107    */
108   void SetJustifyContent(Toolkit::FlexContainer::Justification justifyContent);
109
110   /**
111    * @brief Get the horizontal alignment of the flex items when the items
112    * do not use all available space on the main-axis.
113    * @return The horizontal alignment of flex items.
114    */
115   Toolkit::FlexContainer::Justification GetJustifyContent();
116
117   /**
118    * @brief Set the vertical alignment of the flex items when the items
119    * do not use all available space on the cross-axis.
120    * @param[in] alignItems The vertical alignment of flex items.
121    */
122   void SetAlignItems(Toolkit::FlexContainer::Alignment alignItems);
123
124   /**
125    * @brief Get the vertical alignment of the flex items when the items
126    * do not use all available space on the cross-axis.
127    * @return The vertical alignment of flex items.
128    */
129   Toolkit::FlexContainer::Alignment GetAlignItems();
130
131   /**
132    * @brief Set the vertical alignment of the flex lines when the lines
133    * do not use all available space on the cross-axis.
134    * @param[in] alignItems The vertical alignment of flex lines.
135    */
136   void SetAlignContent(Toolkit::FlexContainer::Alignment alignContent);
137
138   /**
139    * @brief Get the vertical alignment of the flex lines when the lines
140    * do not use all available space on the cross-axis.
141    * @return The vertical alignment of flex lines.
142    */
143   Toolkit::FlexContainer::Alignment GetAlignContent();
144
145   // Properties
146
147   /**
148    * Called when a property of an object of this type is set.
149    * @param[in] object The object whose property is set.
150    * @param[in] index The property index.
151    * @param[in] value The new property value.
152    */
153   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
154
155   /**
156    * Called to retrieve a property of an object of this type.
157    * @param[in] object The object whose property is to be retrieved.
158    * @param[in] index The property index.
159    * @return The current value of the property.
160    */
161   static Property::Value GetProperty(BaseObject* object, Property::Index index);
162
163 private: // From Control
164   /**
165    * @copydoc Control::OnInitialize()
166    */
167   void OnInitialize() override;
168
169   /**
170    * @copydoc Control::OnChildAdd(Actor& child)
171    */
172   void OnChildAdd(Actor& child) override;
173
174   /**
175    * @copydoc Control::OnChildRemove(Actor& child)
176    */
177   void OnChildRemove(Actor& child) override;
178
179   /**
180    * @copydoc Control::OnRelayout
181    */
182   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
183
184   /**
185    * @copydoc Control::RelayoutDependentOnChildren()
186    */
187   bool RelayoutDependentOnChildren(Dimension::Type dimension = Dimension::ALL_DIMENSIONS) override;
188
189   /**
190    * @copydoc Control::GetNextKeyboardFocusableActor
191    */
192   Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled) override;
193
194   /**
195    * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
196    */
197   void OnSizeSet(const Vector3& size) override;
198
199   /**
200   * @copydoc OnLayoutDirectionChanged( Dali::Actor actor, Dali::LayoutDirection::Type type )
201   * @param[in] actor The actor whose layoutDirection is changed.
202   * @param[in] type  The layoutDirection.
203   */
204   void OnLayoutDirectionChanged(Dali::Actor actor, Dali::LayoutDirection::Type type);
205
206 private: // Implementation
207   /**
208    * Calculate the layout properties of all the children
209    */
210   void ComputeLayout();
211
212   /**
213    * Calculate the layout of the children and relayout them with their new size and position
214    */
215   void RelayoutChildren();
216
217   /**
218    * A reference counted object may only be deleted by calling Unreference()
219    */
220   virtual ~FlexContainer();
221
222 private:
223   // Undefined copy constructor and assignment operators
224   FlexContainer(const FlexContainer&);
225   FlexContainer& operator=(const FlexContainer& rhs);
226
227 private:                                // Data
228   FlexItemNode          mRootNode;      ///< Style properties and layout information of flex container
229   FlexItemNodeContainer mChildrenNodes; ///< Style properties and layout information of flex items in the container
230
231   Toolkit::FlexContainer::ContentDirection mContentDirection; ///< The content direction of the container
232   Toolkit::FlexContainer::FlexDirection    mFlexDirection;    ///< The flex direction of the container
233   Toolkit::FlexContainer::WrapType         mFlexWrap;         ///< The wrap type of the container
234   Toolkit::FlexContainer::Justification    mJustifyContent;   ///< The alignment of flex items in the container on the main-axis
235   Toolkit::FlexContainer::Alignment        mAlignItems;       ///< The alignment of flex items in the container on the cross-axis
236   Toolkit::FlexContainer::Alignment        mAlignContent;     ///< The alignment of flex lines in the container on the cross-axis
237 };
238
239 } // namespace Internal
240
241 // Helpers for public-api forwarding methods
242
243 inline Toolkit::Internal::FlexContainer& GetImpl(Toolkit::FlexContainer& flexContainer)
244 {
245   DALI_ASSERT_ALWAYS(flexContainer);
246
247   Dali::RefObject& handle = flexContainer.GetImplementation();
248
249   return static_cast<Toolkit::Internal::FlexContainer&>(handle);
250 }
251
252 inline const Toolkit::Internal::FlexContainer& GetImpl(const Toolkit::FlexContainer& flexContainer)
253 {
254   DALI_ASSERT_ALWAYS(flexContainer);
255
256   const Dali::RefObject& handle = flexContainer.GetImplementation();
257
258   return static_cast<const Toolkit::Internal::FlexContainer&>(handle);
259 }
260
261 } // namespace Toolkit
262
263 } // namespace Dali
264
265 #endif // DALI_TOOLKIT_INTERNAL_FLEX_CONTAINER_H