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