1 #ifndef DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_ITEM_H
2 #define DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_ITEM_H
4 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
45 using LayoutItemPtr = IntrusivePtr<LayoutItem>;
47 class LayoutTransitionData;
48 using LayoutTransitionDataPtr = IntrusivePtr<LayoutTransitionData>;
51 * Base class for layouts.
53 class DALI_TOOLKIT_API LayoutItem : public BaseObject,
64 * A reference counted object may only be deleted by calling Unreference()
66 virtual ~LayoutItem();
73 * @param[in] owner The owner (container view / child view / visual ) of this layout
74 * @return a new LayoutItem object
76 static LayoutItemPtr New( Handle& owner );
79 * @brief Remove the default copy constructor
81 LayoutItem(const LayoutItem& copy)=delete;
84 * @brief Remove the default assignment operator
86 LayoutItem& operator=(const LayoutItem& rhs)=delete;
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
93 void Initialize( Handle& owner, const std::string& containerType );
96 * @brief Get a handle to the owner of this layout
98 * @return a handle to the owner of this layout
100 Handle GetOwner() const;
103 * @brief Unparent this layout from it's parent, remove it from it's owner
104 * and remove any layout children in derived types.
109 * @brief Set whether this layout should be animated or not
111 * @param[in] animateLayout True if the layout should be animated when applied
113 void SetAnimateLayout( bool animateLayout );
116 * @brief Get whether this layout should be animated or not
118 * @return True if the layout should be animated when applied
120 bool IsLayoutAnimated() const;
123 * @brief Get the default transition
125 * @return The default transition
127 LayoutTransitionDataPtr GetDefaultTransition();
130 * @brief Set the layout transition data
131 * @param[in] layoutTransitionType The type of the transition
132 * @param[in] layoutTransitionDataPtr The transition data pointer
134 void SetTransitionData( int layoutTransitionType, LayoutTransitionDataPtr layoutTransitionDataPtr );
137 * @brief Get the transition data
138 * @param[in] layoutTransitionType The type of the transition
140 * @return The transition
142 LayoutTransitionDataPtr GetTransitionData( int layoutTransitionType ) const;
145 * @brief This is called to find out how big a layout should be.
147 * The parent supplies constraint information in the width and height parameters.
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.
153 * @param[in] widthMeasureSpec Horizontal space requirements as imposed by the parent
154 * @param[in] heightMeasureSpec Vertical space requirements as imposed by the parent
156 * @see #OnMeasure(MeasureSpec, MeasureSpec)
158 void Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec );
161 * @brief Assign a size and position to a layout and all of its descendants.
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.
167 * Derived classes with children should override OnLayout. In that method, they should call Layout on each
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
175 void Layout( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
178 * @brief Utility to return a default size.
180 * Uses the supplied size if the MeasureSpec imposed no constraints. Will get larger if allowed by the
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.
187 static LayoutLength GetDefaultSize( LayoutLength size, MeasureSpec measureSpec );
190 * @copydoc LayoutChild::SetParent
192 void SetParent( LayoutParent* parent ) override;
195 * @copydoc LayoutChild::GetParent
197 LayoutParent* GetParent() override;
200 * @brief Request that this layout is re-laid out.
202 * This will make this layout and all it's parent layouts dirty.
204 void RequestLayout();
207 * @brief Request that this layout is re-laid out with particular transition.
208 * @param[in] layoutTranstionType The transition type
210 * This will make this layout and all it's parent layouts dirty and set the transition queued.
212 void RequestLayout( Dali::Toolkit::LayoutTransitionData::LayoutTransitionType layoutTranstionType );
215 * @brief Predicate to determine if this layout has been requested to re-layout
217 * @return True if a layout request has occured on this layout
219 bool IsLayoutRequested() const;
222 * @brief Set layout requested flag (mark the layout dirty).
224 void SetLayoutRequested();
227 * @brief Checks if the Resize policy is being used for this LayoutItem
228 * @return true is ResizePolicy is used
230 bool IsResizePolicyRequired() const;
233 * @brief Sets if the ResizePolicy is needed or not for this LayoutItem
234 * @param[in] resizeRequired true or false flag
236 void SetResizePolicyRequired( bool resizeRequired );
239 * @brief Get the measured width (without any measurement flags).
241 * This method should be used only during measurement and layout calculations.
243 * Use {@link Dali::Actor::GetTargetSize()} to see how wide a control is after layout
245 LayoutLength GetMeasuredWidth() const;
248 * @brief Get the measured height (without any measurement flags).
250 * This method should be used only during measurement and layout calculations.
252 * Use {@link Dali::Actor::GetTargetSize()} to see how high a control is after layout
254 LayoutLength GetMeasuredHeight() const;
257 * @brief Get the measured width and state.
259 * This method should be used only during measurement and layout calculations.
261 * Use {@link Dali::Actor::GetTargetSize()} to see how wide a view is after layout
263 MeasuredSize GetMeasuredWidthAndState() const;
266 * @brief Get the measured height and state.
268 * This method should be used only during measurement and layout calculations.
270 * Use {@link Dali::Actor::GetTargetSize()} to see how high a view is after layout
272 MeasuredSize GetMeasuredHeightAndState() const;
275 * @brief Returns the suggested minimum width that the layout should use.
277 * This returns the maximum of the layout's minimum width and the background's minimum width
279 * When being used in {@link #OnMeasure()}, the caller should still
280 * ensure the returned width is within the requirements of the parent.
282 * @return The suggested minimum width of the layout.
284 LayoutLength GetSuggestedMinimumWidth() const;
287 * @brief Returns the suggested minimum height that the layout should use.
289 * This returns the maximum of the layout's minimum height and the background's minimum height
291 * When being used in {@link #OnMeasure()}, the caller should still
292 * ensure the returned height is within the requirements of the parent.
294 * @return The suggested minimum height of the layout.
296 LayoutLength GetSuggestedMinimumHeight() const;
299 * @brief Sets the minimum width of the layout.
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).
304 * @param[in] minWidth The minimum width the layout will try to be, in pixels
306 * @see #GetMinimumWidth()
308 void SetMinimumWidth( LayoutLength minWidth );
311 * @brief Sets the minimum height of the layout.
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).
316 * @param[in] minHeight The minimum height the layout will try to be, in pixels
318 * @see #GetMinimumHeight()
320 void SetMinimumHeight( LayoutLength minHeight );
323 * @brief Returns the minimum width of the layout.
325 * @return the minimum width the layout will try to be, in pixels
327 * @see #SetMinimumWidth(LayoutLength)
329 LayoutLength GetMinimumWidth() const;
332 * @brief Returns the minimum height of the layout.
334 * @return the minimum height the layout will try to be, in pixels
336 * @see #SetMinimumHeight(LayoutLength)
338 LayoutLength GetMinimumHeight() const;
341 * Get the padding information.
342 * @return The padding information
344 Extents GetPadding() const;
347 * Get the margin information.
348 * @return The margin information
350 Extents GetMargin() const;
354 * @brief Allow directly deriving classes to remove layout children when unparented
356 virtual void OnUnparent(){};
359 * @brief Ensure direct derived types register their child properties with the owner
361 * @param[in] containerType The type name of the owner container
363 virtual void OnRegisterChildProperties( const std::string& containerType );
366 * @brief Measure the layout and its content to determine the measured width and the
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.
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.
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
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()}).
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}.
397 * @see #GetMeasuredWidth()
398 * @see #GetMeasuredHeight()
399 * @see #GetSuggestedMinimumHeight()
400 * @see #GetSuggestedMinimumWidth()
401 * @see MeasureSpec#GetMode(int)
402 * @see MeasureSpec#GetSize(int)
404 virtual void OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec );
407 * @brief Called from Layout() when this layout should assign a size and position to each of its children.
409 * Derived classes with children should override this method and call Layout() on each of their children.
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
417 virtual void OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
421 * @brief This method must be called by {@link #OnMeasure(MeasureSpec,MeasureSpec)} to store the
422 * measured width and measured height.
424 * Failing to do so will trigger an exception at measurement time.
426 * @param[in] measuredWidth The measured width of this layout. This may have a state of
427 * {@link MeasuredSize::MEASURED_SIZE_TOO_SMALL}
429 * @param[in] measuredHeight The measured height of this layout. This may have a state of
430 * {@link MeasuredSize::MEASURED_SIZE_TOO_SMALL}
432 void SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight );
435 * @brief Utility to reconcile a desired size and state, with constraints imposed by a MeasureSpec.
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.
441 * @return A measured size, which may indicate that it is too small.
443 static MeasuredSize ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec,
444 MeasuredSize::State childMeasuredState );
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.
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
455 bool SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
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
462 virtual void OnSizeChanged( LayoutSize newSize, LayoutSize oldSize );
466 * @brief Initialization method for LayoutGroup to override
468 virtual void OnInitialize();
471 * @brief Called when a layer animation state is changed.
473 virtual void OnAnimationStateChanged( bool animateLayout ) {}
477 * @brief Called to change the size of the layout.
479 * @param[in] newSize The new size of the layout
480 * @param[in] oldSize The old size of the layout
482 void SizeChange( LayoutSize newSize, LayoutSize oldSize );
485 * @brief Triggered when a layout animation finished.
487 * @param[in] animation A handle to the layout animation
489 void OnLayoutAnimationFinished( Animation& animation );
492 * @brief Register child properties of layout with owner type.
494 * The Actor hierarchy uses these registered properties in the type
495 * system to ensure child custom properties are properly initialized.
497 * @param[in] containerType The type of the containing view (owner)
499 void RegisterChildProperties( const std::string& containerType );
502 class Impl; // Class declaration is public so we can add devel API's in the future
505 std::unique_ptr<Impl> mImpl; ///< Implementation class holds all the data
506 SlotDelegate<LayoutItem> mSlotDelegate;
509 } //namespace Internal
511 inline Internal::LayoutItem& GetImplementation( Dali::Toolkit::LayoutItem& handle )
513 DALI_ASSERT_ALWAYS( handle && "LayoutItem handle is empty" );
514 BaseObject& object = handle.GetBaseObject();
515 return static_cast< Internal::LayoutItem& >( object );
518 inline const Internal::LayoutItem& GetImplementation( const Dali::Toolkit::LayoutItem& handle )
520 DALI_ASSERT_ALWAYS( handle && "LayoutItem handle is empty" );
521 const BaseObject& object = handle.GetBaseObject();
522 return static_cast< const Internal::LayoutItem& >( object );
525 } //namespace Toolkit
528 #endif // DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_ITEM_H