Exporting one constructor used by csharpbinder and making an API private.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / layout-item-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_ITEM_H
2 #define DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_ITEM_H
3 /*
4  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <memory>
20
21 #include <dali/public-api/common/intrusive-ptr.h>
22 #include <dali/public-api/object/base-object.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/actors/actor-enumerations.h>
25 #include <dali-toolkit/devel-api/layouting/child-layout-data.h>
26 #include <dali-toolkit/devel-api/layouting/layout-item.h>
27 #include <dali-toolkit/devel-api/layouting/layout-parent-impl.h>
28 #include <dali-toolkit/devel-api/layouting/layout-controller.h>
29 #include <dali-toolkit/devel-api/layouting/layout-size.h>
30 #include <dali-toolkit/devel-api/layouting/measure-spec.h>
31 #include <dali-toolkit/devel-api/layouting/measured-size.h>
32
33 namespace Dali
34 {
35 namespace Toolkit
36 {
37 namespace Internal
38 {
39
40 class LayoutItem;
41 using LayoutItemPtr = IntrusivePtr<LayoutItem>;
42
43
44 /**
45  * Base class for layouts.
46  */
47 class DALI_IMPORT_API LayoutItem : public BaseObject,
48                                    public LayoutParent
49 {
50 public:
51   /**
52    * Constructor.
53    */
54   LayoutItem();
55
56 protected:
57   /**
58    * A reference counted object may only be deleted by calling Unreference()
59    */
60   virtual ~LayoutItem();
61
62 public:
63
64 /**
65    * @brief Construct
66    *
67    * @param[in] owner The owner (container view / child view / visual ) of this layout
68    * @return a new LayoutItem object
69    */
70   static LayoutItemPtr New( Handle& owner );
71
72   /**
73    * @brief Remove the default copy constructor
74    */
75   LayoutItem(const LayoutItem& copy)=delete;
76
77   /**
78    * @brief Remove the default assignment operator
79    */
80   LayoutItem& operator=(const LayoutItem& rhs)=delete;
81
82   /**
83    * @brief Initialize the layout with it's owner and owner's type name
84    * @param[in] owner a handle to the owner container
85    * @param[in] containerType the type name of the owner container
86    */
87   void Initialize( Handle& owner, const std::string& containerType );
88
89   /**
90    * @brief Get a handle to the owner of this layout
91    *
92    * @return a handle to the owner of this layout
93    */
94   Handle GetOwner() const;
95
96   /**
97    * @brief Unparent this layout from it's owner, and remove any layout children in derived types
98    */
99   void Unparent();
100
101   /**
102    * @brief Set whether this layout should be animated or not
103    *
104    * @param[in] animateLayout True if the layout should be animated when applied
105    */
106   void SetAnimateLayout( bool animateLayout );
107
108   /**
109    * @brief Get whether this layout should be animated or not
110    *
111    * @return True if the layout should be animated when applied
112    */
113   bool IsLayoutAnimated() const;
114
115   /**
116    * @brief  This is called to find out how big a layout should be.
117    *
118    * The parent supplies constraint information in the width and height parameters.
119    *
120    * The actual measurement work of a layout is performed in
121    * {@link #OnMeasure(MeasureSpec, MeasureSpec)}, called by this method. Therefore, only
122    * {@link #OnMeasure(MeasureSpec, MeasureSpec)} can and must be overridden by subclasses.
123    *
124    * @param[in] widthMeasureSpec Horizontal space requirements as imposed by the parent
125    * @param[in] heightMeasureSpec Vertical space requirements as imposed by the parent
126    *
127    * @see #OnMeasure(MeasureSpec, MeasureSpec)
128    */
129   void Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec );
130
131   /**
132    * @brief Assign a size and position to a layout and all of its descendants.
133    *
134    * This is the second phase of the layout mechanism.  (The first is measuring). In this phase, each parent
135    * calls layout on all of its children to position them.  This is typically done using the child
136    * measurements that were stored in the measure pass.
137    *
138    * Derived classes with children should override OnLayout. In that method, they should call Layout on each
139    * of their children.
140    *
141    * @param[in] left position, relative to parent
142    * @param[in] top position, relative to parent
143    * @param[in] right position, relative to parent
144    * @param[in] bottom position, relative to parent
145    */
146   void Layout( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
147
148   /**
149    * @brief Utility to return a default size.
150    *
151    * Uses the supplied size if the MeasureSpec imposed no constraints. Will get larger if allowed by the
152    * MeasureSpec.
153    *
154    * @param[in] size Default size for this layout
155    * @param[in] measureSpec Constraints imposed by the parent
156    * @return The size this layout should be.
157    */
158   static LayoutLength GetDefaultSize( LayoutLength size, MeasureSpec measureSpec );
159
160   /**
161    * @copydoc LayoutParent::GetParent
162    */
163   virtual LayoutParent* GetParent() override;
164
165   /**
166    * @brief Request that this layout is re-laid out.
167    *
168    * This will make this layout and all it's parent layouts dirty.
169    */
170   void RequestLayout();
171
172   /**
173    * @brief Predicate to determine if this layout has been requested to re-layout
174    *
175    * @return True if a layout request has occured on this layout
176    */
177   bool IsLayoutRequested() const;
178
179   /**
180    * @brief Set layout requested flag (mark the layout dirty).
181    */
182   void SetLayoutRequested();
183
184   /**
185    * @brief Get the measured width (without any measurement flags).
186    *
187    * This method should be used only during measurement and layout calculations.
188    *
189    * Use {@link Dali::Actor::GetTargetSize()} to see how wide a control is after layout
190    */
191   LayoutLength GetMeasuredWidth() const;
192
193   /**
194    * @brief Get the measured height (without any measurement flags).
195    *
196    * This method should be used only during measurement and layout calculations.
197    *
198    * Use {@link Dali::Actor::GetTargetSize()} to see how high a control is after layout
199    */
200   LayoutLength GetMeasuredHeight() const;
201
202   /**
203    * @brief Get the measured width and state.
204    *
205    * This method should be used only during measurement and layout calculations.
206    *
207    * Use {@link Dali::Actor::GetTargetSize()} to see how wide a view is after layout
208    */
209   MeasuredSize GetMeasuredWidthAndState() const;
210
211   /**
212    * @brief Get the measured height and state.
213    *
214    * This method should be used only during measurement and layout calculations.
215    *
216    * Use {@link Dali::Actor::GetTargetSize()} to see how high a view is after layout
217    */
218   MeasuredSize GetMeasuredHeightAndState() const;
219
220   /**
221    * @brief Returns the suggested minimum width that the layout should use.
222    *
223    * This returns the maximum of the layout's minimum width and the background's minimum width
224    *
225    * When being used in {@link #OnMeasure()}, the caller should still
226    * ensure the returned width is within the requirements of the parent.
227    *
228    * @return The suggested minimum width of the layout.
229    */
230   LayoutLength GetSuggestedMinimumWidth() const;
231
232   /**
233    * @brief Returns the suggested minimum height that the layout should use.
234    *
235    * This returns the maximum of the layout's minimum height and the background's minimum height
236    *
237    * When being used in {@link #OnMeasure()}, the caller should still
238    * ensure the returned height is within the requirements of the parent.
239    *
240    * @return The suggested minimum height of the layout.
241    */
242   LayoutLength GetSuggestedMinimumHeight() const;
243
244   /**
245    * @brief Sets the minimum width of the layout.
246    *
247    * It is not guaranteed the layout will be able to achieve this minimum width (for example, if its parent
248    * layout constrains it with less available width).
249    *
250    * @param[in] minWidth The minimum width the layout will try to be, in pixels
251    *
252    * @see #GetMinimumWidth()
253    */
254   void SetMinimumWidth( LayoutLength minWidth );
255
256   /**
257    * @brief Sets the minimum height of the layout.
258    *
259    * It is not guaranteed the layout will be able to achieve this minimum height (for example, if its parent
260    * layout constrains it with less available height).
261    *
262    * @param[in] minHeight The minimum height the layout will try to be, in pixels
263    *
264    * @see #GetMinimumHeight()
265    */
266   void SetMinimumHeight( LayoutLength minHeight );
267
268   /**
269    * @brief Returns the minimum width of the layout.
270    *
271    * @return the minimum width the layout will try to be, in pixels
272    *
273    * @see #SetMinimumWidth(LayoutLength)
274    */
275   LayoutLength GetMinimumWidth() const;
276
277   /**
278    * @brief Returns the minimum height of the layout.
279    *
280    * @return the minimum height the layout will try to be, in pixels
281    *
282    * @see #SetMinimumHeight(LayoutLength)
283    */
284   LayoutLength GetMinimumHeight() const;
285
286   /**
287    * Get the padding information.
288    * @return The padding information
289    */
290   Extents GetPadding() const;
291
292   /**
293    * Get the margin information.
294    * @return The margin information
295    */
296   Extents GetMargin() const;
297
298 protected:
299   /**
300    * @brief Allow directly deriving classes to remove layout children when unparented
301    */
302   virtual void OnUnparent(){};
303
304   /**
305    * @brief Ensure direct derived types register their child properties with the owner
306    *
307    * @param[in] containerType The type name of the owner container
308    */
309   virtual void OnRegisterChildProperties( const std::string& containerType );
310
311   /**
312    * @brief Measure the layout and its content to determine the measured width and the
313    * measured height.
314    *
315    * This method is invoked by {@link #Measure(MeasureSpec, MeasureSpec)} and
316    * should be overridden by subclasses to provide accurate and efficient
317    * measurement of their contents.
318    *
319    * <strong>CONTRACT:</strong> When overriding this method, you
320    * <em>must</em> call {@link #SetMeasuredDimensions(MeasuredSize,MeasuredSize)} to store the
321    * measured width and height of this layout. Failure to do so will trigger an
322    * <code>IllegalStateException</code>, thrown by
323    * {@link #Measure(MeasureSpec,MeasureSpec)}. Calling the superclass'
324    * {@link #OnMeasure(MeasureSpec,MeasureSpec)} is a valid use.
325    *
326    * The base class implementation of measure defaults to the background size,
327    * unless a larger size is allowed by the MeasureSpec. Subclasses should
328    * override {@link #OnMeasure(MeasureSpec,MeasureSpec)} to provide better measurements of
329    * their content.
330    *
331    * If this method is overridden, it is the subclass's responsibility to make
332    * sure the measured height and width are at least the layout's minimum height
333    * and width ({@link #GetSuggestedMinimumHeight()} and
334    * {@link #GetSuggestedMinimumWidth()}).
335    *
336    * @param[in] widthMeasureSpec horizontal space requirements as imposed by the parent.
337    *                             The requirements are encoded with
338    *                             {@link MeasureSpec}.
339    * @param[in] heightMeasureSpec vertical space requirements as imposed by the parent.
340    *                              The requirements are encoded with
341    *                              {@link MeasureSpec}.
342    *
343    * @see #GetMeasuredWidth()
344    * @see #GetMeasuredHeight()
345    * @see #GetSuggestedMinimumHeight()
346    * @see #GetSuggestedMinimumWidth()
347    * @see MeasureSpec#GetMode(int)
348    * @see MeasureSpec#GetSize(int)
349    */
350   virtual void OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec );
351
352   /**
353    * @brief Called from Layout() when this layout should assign a size and position to each of its children.
354    *
355    * Derived classes with children should override this method and call Layout() on each of their children.
356    *
357    * @param[in] changed This is a new size or position for this layout
358    * @param[in] left Left position, relative to parent
359    * @param[in] top Top position, relative to parent
360    * @param[in] right Right position, relative to parent
361    * @param[in] bottom Bottom position, relative to parent
362    */
363   virtual void OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
364
365
366   /**
367    * @brief This method must be called by {@link #OnMeasure(MeasureSpec,MeasureSpec)} to store the
368    * measured width and measured height.
369    *
370    * Failing to do so will trigger an exception at measurement time.
371    *
372    * @param[in] measuredWidth The measured width of this layout. This may have a state of
373    * {@link MeasuredSize::MEASURED_SIZE_TOO_SMALL}
374    *
375    * @param[in] measuredHeight The measured height of this layout. This may have a state of
376    * {@link MeasuredSize::MEASURED_SIZE_TOO_SMALL}
377    */
378   void SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight );
379
380   /**
381    * @brief Utility to reconcile a desired size and state, with constraints imposed by a MeasureSpec.
382    *
383    * @param[in] size How big the layout wants to be.
384    * @param[in] measureSpec Constraints imposed by the parent.
385    * @param[in] childMeasuredState Size information bit mask for the layout's children.
386    *
387    * @return A measured size, which may indicate that it is too small.
388    */
389   static MeasuredSize ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec,
390                                            MeasuredSize::State childMeasuredState );
391
392   /**
393    * @brief Sets the frame (the size and position) of the layout onto it's owner
394    *
395    * @todo Consider instead, collating properties into LayoutCollector in order to set/animate them all
396    * in one block.
397    * @param[in] left The horizontal position of the left edge of this frame within the parent layout
398    * @param[in] top The vertical position of the top edge of this frame within the parent layout
399    * @param[in] right The horizontal position of the right edge of this frame within the parent layout
400    * @param[in] bottom The vertical position of the bottom edge of this frame within the parent layout
401    */
402   bool SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
403
404   /**
405    * Virtual method to inform derived classes when the layout size changed
406    * @param[in] newSize The new size of the layout
407    * @param[in] oldSize The old size of the layout
408    */
409   virtual void OnSizeChanged( LayoutSize newSize, LayoutSize oldSize );
410
411
412   /**
413    * @brief Initialization method for LayoutGroup to override
414    */
415   virtual void OnInitialize();
416
417 private:
418   /**
419    * @brief Called to change the size of the layout.
420    *
421    * @param[in] newSize The new size of the layout
422    * @param[in] oldSize The old size of the layout
423    */
424   void SizeChange( LayoutSize newSize, LayoutSize oldSize );
425
426   /**
427    * @brief Triggered when a layout animation finished.
428    *
429    * @param[in] animation  A handle to the layout animation
430    */
431   void OnLayoutAnimationFinished( Animation& animation );
432
433   /**
434    * @brief Register child properties of layout with owner type.
435    *
436    * The Actor hierarchy uses these registered properties in the type
437    * system to ensure child custom properties are properly initialized.
438    *
439    * @param[in] containerType The type of the containing view (owner)
440    */
441   void RegisterChildProperties( const std::string& containerType );
442
443 public:
444   class Impl; // Class declaration is public so we can add devel API's in the future
445
446
447 private:
448   std::unique_ptr<Impl> mImpl; ///< Implementation class holds all the data
449   SlotDelegate<LayoutItem> mSlotDelegate;
450 };
451
452 } //namespace Internal
453
454 inline Internal::LayoutItem& GetImplementation( Dali::Toolkit::LayoutItem& handle )
455 {
456   DALI_ASSERT_ALWAYS( handle && "LayoutItem handle is empty" );
457   BaseObject& object = handle.GetBaseObject();
458   return static_cast< Internal::LayoutItem& >( object );
459 }
460
461 inline const Internal::LayoutItem& GetImplementation( const Dali::Toolkit::LayoutItem& handle )
462 {
463   DALI_ASSERT_ALWAYS( handle && "LayoutItem handle is empty" );
464   const BaseObject& object = handle.GetBaseObject();
465   return static_cast< const Internal::LayoutItem& >( object );
466 }
467
468 } //namespace Toolkit
469 } //namespace Dali
470
471 #endif // DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_ITEM_H