Add FlexLayout.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / flex-layout.h
1 #ifndef DALI_TOOLKIT_LAYOUTING_FLEX_LAYOUT_H
2 #define DALI_TOOLKIT_LAYOUTING_FLEX_LAYOUT_H
3
4 /*
5  * Copyright (c) 2018 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 #include <dali/public-api/common/dali-common.h>
21 #include <dali/public-api/object/base-handle.h>
22 #include <dali-toolkit/public-api/dali-toolkit-common.h>
23 #include <dali-toolkit/devel-api/layouting/layout-group.h>
24 #include <dali-toolkit/devel-api/layouting/layout-size.h>
25
26 namespace Dali
27 {
28 namespace Toolkit
29 {
30
31 namespace Internal DALI_INTERNAL
32 {
33 class FlexLayout;
34 }
35
36 /**
37  * This class implements a flex layout.
38  *
39  * The flex layout implementation is based on open source Facebook Yoga layout engine.
40  * For more information about the flex layout API and how to use it please refer to
41  * https://yogalayout.com/docs/
42  * We implement the subset of the API in the class below.
43  */
44 class DALI_TOOLKIT_API FlexLayout : public LayoutGroup
45 {
46 public:
47
48   enum PropertyRange
49   {
50     CHILD_PROPERTY_START_INDEX = FLEX_LAYOUT_CHILD_PROPERTY_START_INDEX,
51     CHILD_PROPERTY_END_INDEX   = FLEX_LAYOUT_CHILD_PROPERTY_END_INDEX
52   };
53
54   /**
55    * @brief Enumeration for the direction of the main axis in the flex container. This determines
56    * the direction that flex items are laid out in the flex container.
57    */
58   enum class FlexDirection
59   {
60     COLUMN,                  ///< The flexible items are displayed vertically as a column
61     COLUMN_REVERSE,          ///< The flexible items are displayed vertically as a column, but in reverse order
62     ROW,                     ///< The flexible items are displayed horizontally as a row
63     ROW_REVERSE              ///< The flexible items are displayed horizontally as a row, but in reverse order
64   };
65
66   /**
67    * @brief Enumeration for the alignment of the flex items when the items do not use all available
68    * space on the main-axis.
69    */
70   enum class Justification
71   {
72     FLEX_START,              ///< Items are positioned at the beginning of the container
73     CENTER,                  ///< Items are positioned at the center of the container
74     FLEX_END,                ///< Items are positioned at the end of the container
75     SPACE_BETWEEN,           ///< Items are positioned with equal space between the lines
76     SPACE_AROUND             ///< Items are positioned with equal space before, between, and after the lines
77   };
78
79   /**
80    * @brief Enumeration for the alignment of the flex items or lines when the items or lines do not
81    * use all the available space on the cross-axis.
82    */
83   struct Alignment
84   {
85     enum Type
86     {
87       AUTO,                  ///< Inherits the same alignment from the parent (only valid for "alignSelf" property)
88       FLEX_START,            ///< At the beginning of the container
89       CENTER,                ///< At the center of the container
90       FLEX_END,              ///< At the end of the container
91       STRETCH                ///< Stretch to fit the container
92     };
93   };
94
95   /**
96    * @brief Enumeration for the wrap type of the flex container when there is no enough room for
97    * all the items on one flex line.
98    */
99   enum class WrapType
100   {
101     NO_WRAP,                 ///< Flex items laid out in single line (shrunk to fit the flex container along the main axis)
102     WRAP                     ///< Flex items laid out in multiple lines if needed
103   };
104
105   struct ChildProperty
106   {
107     enum
108     {
109       FLEX = CHILD_PROPERTY_START_INDEX, ///< name "flex",      The proportion of the free space in the container the flex item will receive. If all items in the container set this property, their sizes will be proportional to the specified flex factor,  type FLOAT
110       ALIGN_SELF                         ///< name "alignSelf", The alignment of the flex item along the cross axis, which, if set, overrides the default alignment for all items in the container, type INTEGER
111     };
112   };
113
114   /**
115    * @brief Creates an uninitialized FlexLayout handle.
116    *
117    * Initialize it using FlexLayout::New().
118    * Calling member functions with an uninitialized handle is not allowed.
119    */
120   FlexLayout();
121
122   /**
123    * @brief Creates a FlexLayout object.
124    */
125   static FlexLayout New();
126
127   /**
128    * @brief Downcasts a handle to a FlexLayout handle.
129    *
130    * If handle points to a FlexLayout, the downcast produces a valid handle.
131    * If not, the returned handle is left uninitialized.
132
133    * @param[in] handle to an object
134    * @return Handle to a FlexLayout or an uninitialized handle
135    */
136   static FlexLayout DownCast( BaseHandle handle );
137
138   /**
139    * @brief Copy constructor
140    */
141   FlexLayout( const FlexLayout& other );
142
143   /**
144    * @brief Assigment operator
145    */
146   FlexLayout& operator=( const FlexLayout& other );
147
148   /**
149    * @brief Default destructor.
150    *
151    * This is non-virtual, since derived Handle types must not contain data or virtual methods
152    */
153   ~FlexLayout()=default;
154
155   /**
156     * @brief Set the flex direction in the layout.
157     * The direction of the main-axis which determines the direction that flex items are laid out.
158     *
159     * @param[in] flexDirection The flex direction.
160     */
161   void SetFlexDirection( FlexDirection flexDirection );
162
163   /**
164     * @brief Get the flex direction in the layout.
165     *
166     * @return The flex direction.
167     */
168   FlexDirection GetFlexDirection() const;
169
170   /**
171     * @brief Set the justification in the layout.
172     *
173     * @param[in] flexJustification The flex justification.
174     */
175   void SetFlexJustification( Justification flexJustification );
176
177   /**
178     * @brief Get the flex justification in the layout.
179     *
180     * @return The flex justification.
181     */
182   Justification GetFlexJustification() const;
183
184   /**
185     * @brief Set the wrap in the layout.
186     *
187     * @param[in] flexWrap The flex wrap.
188     */
189   void SetFlexWrap( WrapType flexWrap );
190
191   /**
192     * @brief Get the flex wrap in the layout.
193     *
194     * @return The flex wrap.
195     */
196   WrapType GetFlexWrap() const;
197
198   /**
199     * @brief Set the alignment of the layout content.
200     *
201     * @param[in] flexAlignment The alignment of the content.
202     */
203   void SetFlexAlignment( Alignment::Type flexAlignment );
204
205   /**
206     * @brief Get the alignment of the layout content.
207     *
208     * @return The flex content alignment.
209     */
210   Alignment::Type GetFlexAlignment() const;
211
212   /**
213     * @brief Set the alignment of the layout items.
214     *
215     * @param[in] flexAlignment The alignment of the items.
216     */
217   void SetFlexItemsAlignment( Alignment::Type flexAlignment );
218
219   /**
220     * @brief Get the alignment of the layout items.
221     *
222     * @return The flex items alignment.
223     */
224   Alignment::Type GetFlexItemsAlignment() const;
225
226 public: // Not intended for application developers
227
228   /// @cond internal
229   /**
230    * @brief This constructor is used by FlexLayout::New() methods.
231    *
232    * @param[in] actor A pointer to a newly allocated Dali resource
233    */
234   explicit DALI_INTERNAL FlexLayout( Internal::FlexLayout* body );
235   /// @endcond
236 };
237
238 } // namespace Toolkit
239 } // namespace Dali
240
241 #endif // DALI_TOOLKIT_LAYOUTING_FLEX_LAYOUT_H