[dali_2.3.19] 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) 2019 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
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 /**
39  * FlexContainer is a custom control for laying out actors in a Flexbox layout
40  * @see Dali::Toolkit:FlexContainer for more details
41  */
42 class FlexContainer : public Control
43 {
44 public:
45
46   /**
47    * The structure to store the style properties and layout information of flex item
48    */
49   struct FlexItemNode
50   {
51     WeakHandle< Dali::Actor > actor;      ///< Actor handle of the flex item
52     YGNodeRef node;                     ///< The style properties and layout information
53   };
54
55   typedef std::vector< FlexItemNode > FlexItemNodeContainer;
56
57 public:
58
59   /**
60    * Construct a new FlexContainer.
61    */
62   FlexContainer();
63
64   /**
65    * Create a new FlexContainer.
66    * @return A smart-pointer to the newly allocated FlexContainer.
67    */
68   static Toolkit::FlexContainer New();
69
70   /**
71    * @brief Set the primary direction in which content is ordered.
72    * @param[in] contentDirection The direction of the content.
73    */
74   void SetContentDirection(Toolkit::FlexContainer::ContentDirection contentDirection);
75
76   /**
77    * @brief Get the direction of the content.
78    * @return The direction of the content.
79    */
80   Toolkit::FlexContainer::ContentDirection GetContentDirection();
81
82   /**
83    * @brief Set the direction flex items are laid out.
84    * @param[in] flexDirection The direction flex items are laid out.
85    */
86   void SetFlexDirection(Toolkit::FlexContainer::FlexDirection flexDirection);
87
88   /**
89    * @brief Get the direction flex items are laid out.
90    * @return The direction flex items are laid out.
91    */
92   Toolkit::FlexContainer::FlexDirection GetFlexDirection();
93
94   /**
95    * @brief Set whether the flex items should wrap or not, if there
96    * is no enough room for them on one flex line.
97    * @param[in] flexWrap The wrap type.
98    */
99   void SetFlexWrap(Toolkit::FlexContainer::WrapType flexWrap);
100
101   /**
102    * @brief Get whether the flex items should wrap or not, if there
103    * is no enough room for them on one flex line.
104    * @return The wrap type.
105    */
106   Toolkit::FlexContainer::WrapType GetFlexWrap();
107
108   /**
109    * @brief Set the horizontal alignment of the flex items when the items
110    * do not use all available space on the main-axis.
111    * @param[in] justifyContent The horizontal alignment of flex items.
112    */
113   void SetJustifyContent(Toolkit::FlexContainer::Justification justifyContent);
114
115   /**
116    * @brief Get the horizontal alignment of the flex items when the items
117    * do not use all available space on the main-axis.
118    * @return The horizontal alignment of flex items.
119    */
120   Toolkit::FlexContainer::Justification GetJustifyContent();
121
122   /**
123    * @brief Set the vertical alignment of the flex items when the items
124    * do not use all available space on the cross-axis.
125    * @param[in] alignItems The vertical alignment of flex items.
126    */
127   void SetAlignItems(Toolkit::FlexContainer::Alignment alignItems);
128
129   /**
130    * @brief Get the vertical alignment of the flex items when the items
131    * do not use all available space on the cross-axis.
132    * @return The vertical alignment of flex items.
133    */
134   Toolkit::FlexContainer::Alignment GetAlignItems();
135
136   /**
137    * @brief Set the vertical alignment of the flex lines when the lines
138    * do not use all available space on the cross-axis.
139    * @param[in] alignItems The vertical alignment of flex lines.
140    */
141   void SetAlignContent(Toolkit::FlexContainer::Alignment alignContent);
142
143   /**
144    * @brief Get the vertical alignment of the flex lines when the lines
145    * do not use all available space on the cross-axis.
146    * @return The vertical alignment of flex lines.
147    */
148   Toolkit::FlexContainer::Alignment GetAlignContent();
149
150   // Properties
151
152   /**
153    * Called when a property of an object of this type is set.
154    * @param[in] object The object whose property is set.
155    * @param[in] index The property index.
156    * @param[in] value The new property value.
157    */
158   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
159
160   /**
161    * Called to retrieve a property of an object of this type.
162    * @param[in] object The object whose property is to be retrieved.
163    * @param[in] index The property index.
164    * @return The current value of the property.
165    */
166   static Property::Value GetProperty( BaseObject* object, Property::Index index );
167
168 private: // From Control
169
170   /**
171    * @copydoc Control::OnInitialize()
172    */
173   virtual void OnInitialize();
174
175   /**
176    * @copydoc Control::OnChildAdd(Actor& child)
177    */
178   virtual void OnChildAdd( Actor& child );
179
180   /**
181    * @copydoc Control::OnChildRemove(Actor& child)
182    */
183   virtual void OnChildRemove( Actor& child );
184
185   /**
186    * @copydoc Control::OnRelayout
187    */
188   virtual void OnRelayout( const Vector2& size, RelayoutContainer& container );
189
190   /**
191    * @copydoc Control::RelayoutDependentOnChildren()
192    */
193   virtual bool RelayoutDependentOnChildren( Dimension::Type dimension = Dimension::ALL_DIMENSIONS );
194
195   /**
196    * @copydoc Control::GetNextKeyboardFocusableActor
197    */
198   virtual Actor GetNextKeyboardFocusableActor( Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled );
199
200   /**
201    * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
202    */
203   virtual void OnSizeSet( const Vector3& size );
204
205   /**
206   * @copydoc OnLayoutDirectionChanged( Dali::Actor actor, Dali::LayoutDirection::Type type )
207   * @param[in] actor The actor whose layoutDirection is changed.
208   * @param[in] type  The layoutDirection.
209   */
210   void OnLayoutDirectionChanged( Dali::Actor actor, Dali::LayoutDirection::Type type );
211
212 private: // Implementation
213
214   /**
215    * Calculate the layout properties of all the children
216    */
217   void ComputeLayout();
218
219   /**
220    * Calculate the layout of the children and relayout them with their new size and position
221    */
222   void RelayoutChildren();
223
224   /**
225    * A reference counted object may only be deleted by calling Unreference()
226    */
227   virtual ~FlexContainer();
228
229 private:
230
231   // Undefined copy constructor and assignment operators
232   FlexContainer(const FlexContainer&);
233   FlexContainer& operator=(const FlexContainer& rhs);
234
235 private: // Data
236
237   FlexItemNode mRootNode;                    ///< Style properties and layout information of flex container
238   FlexItemNodeContainer mChildrenNodes;      ///< Style properties and layout information of flex items in the container
239
240   Toolkit::FlexContainer::ContentDirection mContentDirection;        ///< The content direction of the container
241   Toolkit::FlexContainer::FlexDirection mFlexDirection;              ///< The flex direction of the container
242   Toolkit::FlexContainer::WrapType mFlexWrap;                        ///< The wrap type of the container
243   Toolkit::FlexContainer::Justification mJustifyContent;             ///< The alignment of flex items in the container on the main-axis
244   Toolkit::FlexContainer::Alignment mAlignItems;                     ///< The alignment of flex items in the container on the cross-axis
245   Toolkit::FlexContainer::Alignment mAlignContent;                   ///< The alignment of flex lines in the container on the cross-axis
246 };
247
248 } // namespace Internal
249
250 // Helpers for public-api forwarding methods
251
252 inline Toolkit::Internal::FlexContainer& GetImpl( Toolkit::FlexContainer& flexContainer )
253 {
254   DALI_ASSERT_ALWAYS(flexContainer);
255
256   Dali::RefObject& handle = flexContainer.GetImplementation();
257
258   return static_cast<Toolkit::Internal::FlexContainer&>(handle);
259 }
260
261 inline const Toolkit::Internal::FlexContainer& GetImpl( const Toolkit::FlexContainer& flexContainer )
262 {
263   DALI_ASSERT_ALWAYS(flexContainer);
264
265   const Dali::RefObject& handle = flexContainer.GetImplementation();
266
267   return static_cast<const Toolkit::Internal::FlexContainer&>(handle);
268 }
269
270 } // namespace Toolkit
271
272 } // namespace Dali
273
274 #endif // DALI_TOOLKIT_INTERNAL_FLEX_CONTAINER_H