2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali-toolkit/internal/layouting/size-negotiation-mapper.h>
21 #include <dali/integration-api/debug.h>
24 #include <dali-toolkit/devel-api/controls/control-devel.h>
25 #include <dali-toolkit/devel-api/layouting/child-layout-data.h>
26 #include <dali-toolkit/devel-api/layouting/layout-item-impl.h>
30 #if defined(DEBUG_ENABLED)
31 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
35 * Set the width specification with the given layout params
37 void SetWidthLayoutParams( Dali::Toolkit::Control control, int layoutParams )
39 DALI_LOG_INFO( gLogFilter, Debug::General, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy Setting WIDTH LayoutParam(%d) for %s\n",
41 control.GetName().c_str() );
42 control.SetProperty( Dali::Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, layoutParams );
46 * Set the height specification with the given layout params
48 void SetHeightLayoutParams( Dali::Toolkit::Control control, int layoutParams )
50 DALI_LOG_INFO( gLogFilter, Debug::General, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy Setting HEIGHT LayoutParam(%d) for %s\n",
52 control.GetName().c_str() );
53 control.SetProperty( Dali::Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, layoutParams );
57 * Call SetResizePolicyRequired true on the layout belonging to the provided control
59 void SetResizePolicyRequiredFlag( Dali::Toolkit::Internal::LayoutItemPtr layout )
61 layout->SetResizePolicyRequired( true );
73 void SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( Toolkit::Control control, Toolkit::Internal::LayoutItemPtr layout, Dimension::Type dimension )
75 // Get control's current width specification
76 int matchedLayoutParamWidth = control.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
77 int matchedLayoutParamHeight = control.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
79 // Get control's Resize Policy that should be mapped to a width specification
80 const ResizePolicy::Type resizePolicy = control.GetResizePolicy( dimension );
82 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy ResizePolicy for control:%s resizePolicy %d for dimension %d\n",
83 control?control.GetName().c_str():"Invalid",
87 bool matchFound( false );
89 // Use the control's ResizePolicy to determine the new specification
90 switch( resizePolicy )
92 case ResizePolicy::USE_ASSIGNED_SIZE :
94 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy USE_ASSIGNED_SIZE\n");
95 // Set by legacy controls on their children. Will not be exposed to a Layout.
99 case ResizePolicy::FIXED :
101 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy FIXED\n");
102 Vector3 controlSize = control.GetTargetSize();
103 matchedLayoutParamWidth = controlSize.width;
104 matchedLayoutParamHeight = controlSize.height;
108 case ResizePolicy::USE_NATURAL_SIZE :
110 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy USE_NATURAL_SIZE\n");
111 // Default ResizePolicy for controls.
112 // LayoutGroups are containers, containers will not have a natural size.
115 case ResizePolicy::FILL_TO_PARENT :
117 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy FILL_TO_PARENT\n");
118 matchedLayoutParamWidth = ChildLayoutData::MATCH_PARENT;
119 matchedLayoutParamHeight = ChildLayoutData::MATCH_PARENT;
123 case ResizePolicy::SIZE_RELATIVE_TO_PARENT :
125 SetResizePolicyRequiredFlag( layout ); // Defer setting the exact size until the parent size is known.
128 case ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT :
130 SetResizePolicyRequiredFlag( layout ); // Defer setting the exact size until the parent size is known.
133 case ResizePolicy::FIT_TO_CHILDREN :
135 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsUsingResizePolicy FIT_TO_CHILDREN\n");
136 matchedLayoutParamWidth = ChildLayoutData::WRAP_CONTENT;
137 matchedLayoutParamHeight = ChildLayoutData::WRAP_CONTENT;
141 case ResizePolicy::DIMENSION_DEPENDENCY :
148 // Use the resize policy for the required dimensions only.
149 // Possible that only one dimension was set hence either the matchedLayoutParamWidth or
150 // matchedLayoutParamHeight should not be used.
153 if( dimension & Dimension::WIDTH )
155 SetWidthLayoutParams( control, matchedLayoutParamWidth );
158 if( dimension & Dimension::HEIGHT )
160 SetHeightLayoutParams( control, matchedLayoutParamHeight );
165 void SizeNegotiationMapper::GetSizeofChildForParentDependentResizePolicy( Toolkit::Control control, const MeasureSpec parentWidthSpecification, const MeasureSpec parentHeightSpecification, LayoutLength& childWidth, LayoutLength& childHeight)
167 DALI_LOG_INFO( gLogFilter, Debug::General, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy ResizePolicy required for %s\n", control.GetName().c_str() );
168 ResizePolicy::Type widthResizePolicy = control.GetResizePolicy( Dimension::WIDTH );
169 ResizePolicy::Type heightResizePolicy = control.GetResizePolicy( Dimension::HEIGHT );
170 Vector3 sizeModeFactor = control.GetSizeModeFactor();
172 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::MeasureChild SetLayoutParamsForParentDependantResizePolicy for control:%s resizePolicy w:%d h:%d modeFactor(%f,%f,%f)\n",
173 control?control.GetName().c_str():"Invalid",
174 widthResizePolicy, heightResizePolicy,
179 if( widthResizePolicy == ResizePolicy::SIZE_RELATIVE_TO_PARENT )
181 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy width SIZE_RELATIVE_TO_PARENT\n");
182 childWidth = parentWidthSpecification.GetSize().AsDecimal() * sizeModeFactor.x;
184 else if( widthResizePolicy == ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT )
186 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy width SIZE_FIXED_OFFSET_FROM_PARENT\n");
187 childWidth = parentWidthSpecification.GetSize().AsDecimal() + sizeModeFactor.x;
190 if( heightResizePolicy == ResizePolicy::SIZE_RELATIVE_TO_PARENT )
192 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy height SIZE_RELATIVE_TO_PARENT\n");
193 childHeight = parentHeightSpecification.GetSize().AsDecimal() * sizeModeFactor.y;
195 else if( heightResizePolicy == ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT )
197 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy height SIZE_FIXED_OFFSET_FROM_PARENT\n");
198 childHeight = parentHeightSpecification.GetSize().AsDecimal() + sizeModeFactor.y;
201 // DIMENSION_DEPENDENCY not supported
203 DALI_LOG_INFO( gLogFilter, Debug::General, "SizeNegotiationMapper::SetLayoutParamsForParentDependantResizePolicy child size(%f,%f)\n", childWidth.AsInteger(), childHeight.AsInteger() );
206 } // namespace Internal
208 } // namespace Toolkit