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/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-child-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>
41 using LayoutItemPtr = IntrusivePtr<LayoutItem>;
45 * Base class for layouts.
47 class DALI_TOOLKIT_API LayoutItem : public BaseObject,
58 * A reference counted object may only be deleted by calling Unreference()
60 virtual ~LayoutItem();
67 * @param[in] owner The owner (container view / child view / visual ) of this layout
68 * @return a new LayoutItem object
70 static LayoutItemPtr New( Handle& owner );
73 * @brief Remove the default copy constructor
75 LayoutItem(const LayoutItem& copy)=delete;
78 * @brief Remove the default assignment operator
80 LayoutItem& operator=(const LayoutItem& rhs)=delete;
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
87 void Initialize( Handle& owner, const std::string& containerType );
90 * @brief Get a handle to the owner of this layout
92 * @return a handle to the owner of this layout
94 Handle GetOwner() const;
97 * @brief Unparent this layout from it's parent, remove it from it's owner
98 * and remove any layout children in derived types.
103 * @brief Set whether this layout should be animated or not
105 * @param[in] animateLayout True if the layout should be animated when applied
107 void SetAnimateLayout( bool animateLayout );
110 * @brief Get whether this layout should be animated or not
112 * @return True if the layout should be animated when applied
114 bool IsLayoutAnimated() const;
117 * @brief This is called to find out how big a layout should be.
119 * The parent supplies constraint information in the width and height parameters.
121 * The actual measurement work of a layout is performed in
122 * {@link #OnMeasure(MeasureSpec, MeasureSpec)}, called by this method. Therefore, only
123 * {@link #OnMeasure(MeasureSpec, MeasureSpec)} can and must be overridden by subclasses.
125 * @param[in] widthMeasureSpec Horizontal space requirements as imposed by the parent
126 * @param[in] heightMeasureSpec Vertical space requirements as imposed by the parent
128 * @see #OnMeasure(MeasureSpec, MeasureSpec)
130 void Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec );
133 * @brief Assign a size and position to a layout and all of its descendants.
135 * This is the second phase of the layout mechanism. (The first is measuring). In this phase, each parent
136 * calls layout on all of its children to position them. This is typically done using the child
137 * measurements that were stored in the measure pass.
139 * Derived classes with children should override OnLayout. In that method, they should call Layout on each
142 * @param[in] left position, relative to parent
143 * @param[in] top position, relative to parent
144 * @param[in] right position, relative to parent
145 * @param[in] bottom position, relative to parent
147 void Layout( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
150 * @brief Utility to return a default size.
152 * Uses the supplied size if the MeasureSpec imposed no constraints. Will get larger if allowed by the
155 * @param[in] size Default size for this layout
156 * @param[in] measureSpec Constraints imposed by the parent
157 * @return The size this layout should be.
159 static LayoutLength GetDefaultSize( LayoutLength size, MeasureSpec measureSpec );
162 * @copydoc LayoutChild::SetParent
164 void SetParent( LayoutParent* parent ) override;
167 * @copydoc LayoutChild::GetParent
169 LayoutParent* GetParent() override;
172 * @brief Request that this layout is re-laid out.
174 * This will make this layout and all it's parent layouts dirty.
176 void RequestLayout();
179 * @brief Predicate to determine if this layout has been requested to re-layout
181 * @return True if a layout request has occured on this layout
183 bool IsLayoutRequested() const;
186 * @brief Set layout requested flag (mark the layout dirty).
188 void SetLayoutRequested();
191 * @brief Get the measured width (without any measurement flags).
193 * This method should be used only during measurement and layout calculations.
195 * Use {@link Dali::Actor::GetTargetSize()} to see how wide a control is after layout
197 LayoutLength GetMeasuredWidth() const;
200 * @brief Get the measured height (without any measurement flags).
202 * This method should be used only during measurement and layout calculations.
204 * Use {@link Dali::Actor::GetTargetSize()} to see how high a control is after layout
206 LayoutLength GetMeasuredHeight() const;
209 * @brief Get the measured width and state.
211 * This method should be used only during measurement and layout calculations.
213 * Use {@link Dali::Actor::GetTargetSize()} to see how wide a view is after layout
215 MeasuredSize GetMeasuredWidthAndState() const;
218 * @brief Get the measured height and state.
220 * This method should be used only during measurement and layout calculations.
222 * Use {@link Dali::Actor::GetTargetSize()} to see how high a view is after layout
224 MeasuredSize GetMeasuredHeightAndState() const;
227 * @brief Returns the suggested minimum width that the layout should use.
229 * This returns the maximum of the layout's minimum width and the background's minimum width
231 * When being used in {@link #OnMeasure()}, the caller should still
232 * ensure the returned width is within the requirements of the parent.
234 * @return The suggested minimum width of the layout.
236 LayoutLength GetSuggestedMinimumWidth() const;
239 * @brief Returns the suggested minimum height that the layout should use.
241 * This returns the maximum of the layout's minimum height and the background's minimum height
243 * When being used in {@link #OnMeasure()}, the caller should still
244 * ensure the returned height is within the requirements of the parent.
246 * @return The suggested minimum height of the layout.
248 LayoutLength GetSuggestedMinimumHeight() const;
251 * @brief Sets the minimum width of the layout.
253 * It is not guaranteed the layout will be able to achieve this minimum width (for example, if its parent
254 * layout constrains it with less available width).
256 * @param[in] minWidth The minimum width the layout will try to be, in pixels
258 * @see #GetMinimumWidth()
260 void SetMinimumWidth( LayoutLength minWidth );
263 * @brief Sets the minimum height of the layout.
265 * It is not guaranteed the layout will be able to achieve this minimum height (for example, if its parent
266 * layout constrains it with less available height).
268 * @param[in] minHeight The minimum height the layout will try to be, in pixels
270 * @see #GetMinimumHeight()
272 void SetMinimumHeight( LayoutLength minHeight );
275 * @brief Returns the minimum width of the layout.
277 * @return the minimum width the layout will try to be, in pixels
279 * @see #SetMinimumWidth(LayoutLength)
281 LayoutLength GetMinimumWidth() const;
284 * @brief Returns the minimum height of the layout.
286 * @return the minimum height the layout will try to be, in pixels
288 * @see #SetMinimumHeight(LayoutLength)
290 LayoutLength GetMinimumHeight() const;
293 * Get the padding information.
294 * @return The padding information
296 Extents GetPadding() const;
299 * Get the margin information.
300 * @return The margin information
302 Extents GetMargin() const;
306 * @brief Allow directly deriving classes to remove layout children when unparented
308 virtual void OnUnparent(){};
311 * @brief Ensure direct derived types register their child properties with the owner
313 * @param[in] containerType The type name of the owner container
315 virtual void OnRegisterChildProperties( const std::string& containerType );
318 * @brief Measure the layout and its content to determine the measured width and the
321 * This method is invoked by {@link #Measure(MeasureSpec, MeasureSpec)} and
322 * should be overridden by subclasses to provide accurate and efficient
323 * measurement of their contents.
325 * <strong>CONTRACT:</strong> When overriding this method, you
326 * <em>must</em> call {@link #SetMeasuredDimensions(MeasuredSize,MeasuredSize)} to store the
327 * measured width and height of this layout. Failure to do so will trigger an
328 * <code>IllegalStateException</code>, thrown by
329 * {@link #Measure(MeasureSpec,MeasureSpec)}. Calling the superclass'
330 * {@link #OnMeasure(MeasureSpec,MeasureSpec)} is a valid use.
332 * The base class implementation of measure defaults to the background size,
333 * unless a larger size is allowed by the MeasureSpec. Subclasses should
334 * override {@link #OnMeasure(MeasureSpec,MeasureSpec)} to provide better measurements of
337 * If this method is overridden, it is the subclass's responsibility to make
338 * sure the measured height and width are at least the layout's minimum height
339 * and width ({@link #GetSuggestedMinimumHeight()} and
340 * {@link #GetSuggestedMinimumWidth()}).
342 * @param[in] widthMeasureSpec horizontal space requirements as imposed by the parent.
343 * The requirements are encoded with
344 * {@link MeasureSpec}.
345 * @param[in] heightMeasureSpec vertical space requirements as imposed by the parent.
346 * The requirements are encoded with
347 * {@link MeasureSpec}.
349 * @see #GetMeasuredWidth()
350 * @see #GetMeasuredHeight()
351 * @see #GetSuggestedMinimumHeight()
352 * @see #GetSuggestedMinimumWidth()
353 * @see MeasureSpec#GetMode(int)
354 * @see MeasureSpec#GetSize(int)
356 virtual void OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec );
359 * @brief Called from Layout() when this layout should assign a size and position to each of its children.
361 * Derived classes with children should override this method and call Layout() on each of their children.
363 * @param[in] changed This is a new size or position for this layout
364 * @param[in] left Left position, relative to parent
365 * @param[in] top Top position, relative to parent
366 * @param[in] right Right position, relative to parent
367 * @param[in] bottom Bottom position, relative to parent
369 virtual void OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
373 * @brief This method must be called by {@link #OnMeasure(MeasureSpec,MeasureSpec)} to store the
374 * measured width and measured height.
376 * Failing to do so will trigger an exception at measurement time.
378 * @param[in] measuredWidth The measured width of this layout. This may have a state of
379 * {@link MeasuredSize::MEASURED_SIZE_TOO_SMALL}
381 * @param[in] measuredHeight The measured height of this layout. This may have a state of
382 * {@link MeasuredSize::MEASURED_SIZE_TOO_SMALL}
384 void SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight );
387 * @brief Utility to reconcile a desired size and state, with constraints imposed by a MeasureSpec.
389 * @param[in] size How big the layout wants to be.
390 * @param[in] measureSpec Constraints imposed by the parent.
391 * @param[in] childMeasuredState Size information bit mask for the layout's children.
393 * @return A measured size, which may indicate that it is too small.
395 static MeasuredSize ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec,
396 MeasuredSize::State childMeasuredState );
399 * @brief Sets the frame (the size and position) of the layout onto it's owner
401 * @todo Consider instead, collating properties into LayoutCollector in order to set/animate them all
403 * @param[in] left The horizontal position of the left edge of this frame within the parent layout
404 * @param[in] top The vertical position of the top edge of this frame within the parent layout
405 * @param[in] right The horizontal position of the right edge of this frame within the parent layout
406 * @param[in] bottom The vertical position of the bottom edge of this frame within the parent layout
408 bool SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
411 * Virtual method to inform derived classes when the layout size changed
412 * @param[in] newSize The new size of the layout
413 * @param[in] oldSize The old size of the layout
415 virtual void OnSizeChanged( LayoutSize newSize, LayoutSize oldSize );
419 * @brief Initialization method for LayoutGroup to override
421 virtual void OnInitialize();
424 * @brief Called when a layer animation state is changed.
426 virtual void OnAnimationStateChanged( bool animateLayout ) {}
430 * @brief Called to change the size of the layout.
432 * @param[in] newSize The new size of the layout
433 * @param[in] oldSize The old size of the layout
435 void SizeChange( LayoutSize newSize, LayoutSize oldSize );
438 * @brief Triggered when a layout animation finished.
440 * @param[in] animation A handle to the layout animation
442 void OnLayoutAnimationFinished( Animation& animation );
445 * @brief Register child properties of layout with owner type.
447 * The Actor hierarchy uses these registered properties in the type
448 * system to ensure child custom properties are properly initialized.
450 * @param[in] containerType The type of the containing view (owner)
452 void RegisterChildProperties( const std::string& containerType );
455 class Impl; // Class declaration is public so we can add devel API's in the future
459 std::unique_ptr<Impl> mImpl; ///< Implementation class holds all the data
460 SlotDelegate<LayoutItem> mSlotDelegate;
463 } //namespace Internal
465 inline Internal::LayoutItem& GetImplementation( Dali::Toolkit::LayoutItem& handle )
467 DALI_ASSERT_ALWAYS( handle && "LayoutItem handle is empty" );
468 BaseObject& object = handle.GetBaseObject();
469 return static_cast< Internal::LayoutItem& >( object );
472 inline const Internal::LayoutItem& GetImplementation( const Dali::Toolkit::LayoutItem& handle )
474 DALI_ASSERT_ALWAYS( handle && "LayoutItem handle is empty" );
475 const BaseObject& object = handle.GetBaseObject();
476 return static_cast< const Internal::LayoutItem& >( object );
479 } //namespace Toolkit
482 #endif // DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_ITEM_H