2 * Copyright (c) 2017 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.
19 #include <dali-toolkit/internal/controls/flex-container/flex-container-impl.h>
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/object/type-registry-helper.h>
26 #include <dali/devel-api/actors/actor-devel.h>
27 #include <dali/devel-api/scripting/scripting.h>
28 #include <dali/public-api/size-negotiation/relayout-container.h>
29 #include <dali/integration-api/debug.h>
36 #if defined(DEBUG_ENABLED)
37 // debugging support, very useful when new features are added or bugs are hunted down
38 // currently not called from code so compiler will optimize these away, kept here for future debugging
40 #define FLEX_CONTAINER_TAG "DALI Toolkit::FlexContainer "
41 #define FC_LOG(fmt, args...) Debug::LogMessage(Debug::DebugInfo, FLEX_CONTAINER_TAG fmt, ## args)
42 //#define FLEX_CONTAINER_DEBUG 1
44 #if defined(FLEX_CONTAINER_DEBUG)
45 void PrintNode( Toolkit::Internal::FlexContainer::FlexItemNodeContainer itemNodes )
47 // Print the style property and layout of all the children
48 for( unsigned int i = 0; i < itemNodes.size(); ++i )
50 FC_LOG( "Item %d style: \n", i );
51 print_css_node( itemNodes[i].node, (css_print_options_t)( CSS_PRINT_STYLE | CSS_PRINT_CHILDREN ) );
52 FC_LOG( "Item %d layout: \n", i );
53 print_css_node( itemNodes[i].node, (css_print_options_t)( CSS_PRINT_LAYOUT | CSS_PRINT_CHILDREN ) );
58 #endif // defined(FLEX_CONTAINER_DEBUG)
59 #endif // defined(DEBUG_ENABLED)
79 return Toolkit::FlexContainer::New();
82 // Setup properties, signals and actions using the type-registry.
83 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::FlexContainer, Toolkit::Control, Create );
85 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "contentDirection", INTEGER, CONTENT_DIRECTION )
86 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "flexDirection", INTEGER, FLEX_DIRECTION )
87 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "flexWrap", INTEGER, FLEX_WRAP )
88 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "justifyContent", INTEGER, JUSTIFY_CONTENT )
89 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "alignItems", INTEGER, ALIGN_ITEMS )
90 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "alignContent", INTEGER, ALIGN_CONTENT )
91 DALI_CHILD_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "flex", FLOAT, FLEX )
92 DALI_CHILD_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "alignSelf", INTEGER, ALIGN_SELF )
93 DALI_CHILD_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "flexMargin", VECTOR4, FLEX_MARGIN )
95 DALI_TYPE_REGISTRATION_END()
97 const Scripting::StringEnum ALIGN_SELF_STRING_TABLE[] =
99 { "auto", Toolkit::FlexContainer::ALIGN_AUTO },
100 { "flexStart", Toolkit::FlexContainer::ALIGN_FLEX_START },
101 { "center", Toolkit::FlexContainer::ALIGN_CENTER },
102 { "flexEnd", Toolkit::FlexContainer::ALIGN_FLEX_END },
103 { "stretch", Toolkit::FlexContainer::ALIGN_STRETCH }
105 const unsigned int ALIGN_SELF_STRING_TABLE_COUNT = sizeof( ALIGN_SELF_STRING_TABLE ) / sizeof( ALIGN_SELF_STRING_TABLE[0] );
107 const Scripting::StringEnum CONTENT_DIRECTION_STRING_TABLE[] =
109 { "inherit", Toolkit::FlexContainer::INHERIT },
110 { "LTR", Toolkit::FlexContainer::LTR },
111 { "RTL", Toolkit::FlexContainer::RTL }
113 const unsigned int CONTENT_DIRECTION_STRING_TABLE_COUNT = sizeof( CONTENT_DIRECTION_STRING_TABLE ) / sizeof( CONTENT_DIRECTION_STRING_TABLE[0] );
115 const Scripting::StringEnum FLEX_DIRECTION_STRING_TABLE[] =
117 { "column", Toolkit::FlexContainer::COLUMN },
118 { "columnReverse", Toolkit::FlexContainer::COLUMN_REVERSE },
119 { "row", Toolkit::FlexContainer::ROW },
120 { "rowReverse", Toolkit::FlexContainer::ROW_REVERSE }
122 const unsigned int FLEX_DIRECTION_STRING_TABLE_COUNT = sizeof( FLEX_DIRECTION_STRING_TABLE ) / sizeof( FLEX_DIRECTION_STRING_TABLE[0] );
124 const Scripting::StringEnum FLEX_WRAP_STRING_TABLE[] =
126 { "noWrap", Toolkit::FlexContainer::NO_WRAP },
127 { "wrap", Toolkit::FlexContainer::WRAP }
129 const unsigned int FLEX_WRAP_STRING_TABLE_COUNT = sizeof( FLEX_WRAP_STRING_TABLE ) / sizeof( FLEX_WRAP_STRING_TABLE[0] );
131 const Scripting::StringEnum JUSTIFY_CONTENT_STRING_TABLE[] =
133 { "flexStart", Toolkit::FlexContainer::JUSTIFY_FLEX_START },
134 { "center", Toolkit::FlexContainer::JUSTIFY_CENTER },
135 { "flexEnd", Toolkit::FlexContainer::JUSTIFY_FLEX_END },
136 { "spaceBetween", Toolkit::FlexContainer::JUSTIFY_SPACE_BETWEEN },
137 { "spaceAround", Toolkit::FlexContainer::JUSTIFY_SPACE_AROUND }
139 const unsigned int JUSTIFY_CONTENT_STRING_TABLE_COUNT = sizeof( JUSTIFY_CONTENT_STRING_TABLE ) / sizeof( JUSTIFY_CONTENT_STRING_TABLE[0] );
141 const Scripting::StringEnum ALIGN_ITEMS_STRING_TABLE[] =
143 { "flexStart", Toolkit::FlexContainer::ALIGN_FLEX_START },
144 { "center", Toolkit::FlexContainer::ALIGN_CENTER },
145 { "flexEnd", Toolkit::FlexContainer::ALIGN_FLEX_END },
146 { "stretch", Toolkit::FlexContainer::ALIGN_STRETCH }
148 const unsigned int ALIGN_ITEMS_STRING_TABLE_COUNT = sizeof( ALIGN_ITEMS_STRING_TABLE ) / sizeof( ALIGN_ITEMS_STRING_TABLE[0] );
150 const Scripting::StringEnum ALIGN_CONTENT_STRING_TABLE[] =
152 { "flexStart", Toolkit::FlexContainer::ALIGN_FLEX_START },
153 { "center", Toolkit::FlexContainer::ALIGN_CENTER },
154 { "flexEnd", Toolkit::FlexContainer::ALIGN_FLEX_END },
155 { "stretch", Toolkit::FlexContainer::ALIGN_STRETCH }
157 const unsigned int ALIGN_CONTENT_STRING_TABLE_COUNT = sizeof( ALIGN_CONTENT_STRING_TABLE ) / sizeof( ALIGN_CONTENT_STRING_TABLE[0] );
160 * The function used by the layout algorithm to be get the style properties
161 * and layout information of the child at the given index.
163 css_node_t* GetChildNodeAtIndex( void *childrenNodes, int i )
165 FlexContainer::FlexItemNodeContainer childrenNodeContainer = *( static_cast<FlexContainer::FlexItemNodeContainer*>( childrenNodes ) );
166 return childrenNodeContainer[i].node;
170 * The function used by the layout algorithm to check whether the node is dirty
173 bool IsNodeDirty( void *itemNodes )
175 // We only calculate the layout when the child is added or removed, or when
176 // style properties are changed. So should always return true here.
180 } // Unnamed namespace
182 Toolkit::FlexContainer FlexContainer::New()
184 // Create the implementation, temporarily owned by this handle on stack
185 IntrusivePtr< FlexContainer > impl = new FlexContainer();
187 // Pass ownership to CustomActor handle
188 Toolkit::FlexContainer handle( *impl );
190 // Second-phase init of the implementation
191 // This can only be done after the CustomActor connection has been made...
197 FlexContainer::~FlexContainer()
199 free_css_node( mRootNode.node );
201 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
203 free_css_node( mChildrenNodes[i].node );
206 mChildrenNodes.clear();
209 void FlexContainer::SetContentDirection( Toolkit::FlexContainer::ContentDirection contentDirection )
211 if( mContentDirection != contentDirection )
213 mContentDirection = contentDirection;
214 mRootNode.node->style.direction = static_cast<css_direction_t>( mContentDirection );
220 Toolkit::FlexContainer::ContentDirection FlexContainer::GetContentDirection()
222 return mContentDirection;
225 void FlexContainer::SetFlexDirection( Toolkit::FlexContainer::FlexDirection flexDirection )
227 if( mFlexDirection != flexDirection )
229 mFlexDirection = flexDirection;
230 mRootNode.node->style.flex_direction = static_cast<css_flex_direction_t>( mFlexDirection );
236 Toolkit::FlexContainer::FlexDirection FlexContainer::GetFlexDirection()
238 return mFlexDirection;
241 void FlexContainer::SetFlexWrap( Toolkit::FlexContainer::WrapType flexWrap )
243 if( mFlexWrap != flexWrap )
245 mFlexWrap = flexWrap;
246 mRootNode.node->style.flex_wrap = static_cast<css_wrap_type_t>( mFlexWrap );
252 Toolkit::FlexContainer::WrapType FlexContainer::GetFlexWrap()
257 void FlexContainer::SetJustifyContent( Toolkit::FlexContainer::Justification justifyContent )
259 if( mJustifyContent != justifyContent )
261 mJustifyContent = justifyContent;
262 mRootNode.node->style.justify_content = static_cast<css_justify_t>( mJustifyContent );
268 Toolkit::FlexContainer::Justification FlexContainer::GetJustifyContent()
270 return mJustifyContent;
273 void FlexContainer::SetAlignItems( Toolkit::FlexContainer::Alignment alignItems )
275 if( mAlignItems != alignItems )
277 mAlignItems = alignItems;
278 mRootNode.node->style.align_items = static_cast<css_align_t>( mAlignItems );
284 Toolkit::FlexContainer::Alignment FlexContainer::GetAlignItems()
289 void FlexContainer::SetAlignContent( Toolkit::FlexContainer::Alignment alignContent )
291 if( mAlignContent != alignContent )
293 mAlignContent = alignContent;
294 mRootNode.node->style.align_content = static_cast<css_align_t>( mAlignContent );
300 Toolkit::FlexContainer::Alignment FlexContainer::GetAlignContent()
302 return mAlignContent;
305 void FlexContainer::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
307 Toolkit::FlexContainer flexContainer = Toolkit::FlexContainer::DownCast( Dali::BaseHandle( object ) );
311 FlexContainer& flexContainerImpl( GetImpl( flexContainer ) );
314 case Toolkit::FlexContainer::Property::CONTENT_DIRECTION:
316 Toolkit::FlexContainer::ContentDirection contentDirection( Toolkit::FlexContainer::INHERIT );
318 if( value.GetType() == Property::INTEGER )
320 flexContainerImpl.SetContentDirection( static_cast<Toolkit::FlexContainer::ContentDirection>( value.Get< int >() ) );
322 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::ContentDirection >( value.Get< std::string >().c_str(),
323 CONTENT_DIRECTION_STRING_TABLE,
324 CONTENT_DIRECTION_STRING_TABLE_COUNT,
327 flexContainerImpl.SetContentDirection(contentDirection);
331 case Toolkit::FlexContainer::Property::FLEX_DIRECTION:
333 Toolkit::FlexContainer::FlexDirection flexDirection( Toolkit::FlexContainer::COLUMN );
335 if( value.GetType() == Property::INTEGER )
337 flexContainerImpl.SetFlexDirection( static_cast<Toolkit::FlexContainer::FlexDirection>( value.Get< int >() ) );
339 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::FlexDirection >( value.Get< std::string >().c_str(),
340 FLEX_DIRECTION_STRING_TABLE,
341 FLEX_DIRECTION_STRING_TABLE_COUNT,
344 flexContainerImpl.SetFlexDirection(flexDirection);
348 case Toolkit::FlexContainer::Property::FLEX_WRAP:
350 Toolkit::FlexContainer::WrapType flexWrap( Toolkit::FlexContainer::NO_WRAP );
352 if( value.GetType() == Property::INTEGER )
354 flexContainerImpl.SetFlexWrap( static_cast<Toolkit::FlexContainer::WrapType>( value.Get< int >() ) );
356 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::WrapType >( value.Get< std::string >().c_str(),
357 FLEX_WRAP_STRING_TABLE,
358 FLEX_WRAP_STRING_TABLE_COUNT,
361 flexContainerImpl.SetFlexWrap(flexWrap);
365 case Toolkit::FlexContainer::Property::JUSTIFY_CONTENT:
367 Toolkit::FlexContainer::Justification justifyContent( Toolkit::FlexContainer::JUSTIFY_FLEX_START );
369 if( value.GetType() == Property::INTEGER )
371 flexContainerImpl.SetJustifyContent( static_cast<Toolkit::FlexContainer::Justification>( value.Get< int >() ) );
373 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::Justification >( value.Get< std::string >().c_str(),
374 JUSTIFY_CONTENT_STRING_TABLE,
375 JUSTIFY_CONTENT_STRING_TABLE_COUNT,
378 flexContainerImpl.SetJustifyContent(justifyContent);
382 case Toolkit::FlexContainer::Property::ALIGN_ITEMS:
384 Toolkit::FlexContainer::Alignment alignItems( Toolkit::FlexContainer::ALIGN_STRETCH );
386 if( value.GetType() == Property::INTEGER )
388 flexContainerImpl.SetAlignItems( static_cast<Toolkit::FlexContainer::Alignment>( value.Get< int >() ) );
390 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::Alignment >( value.Get< std::string >().c_str(),
391 ALIGN_ITEMS_STRING_TABLE,
392 ALIGN_ITEMS_STRING_TABLE_COUNT,
395 flexContainerImpl.SetAlignItems(alignItems);
399 case Toolkit::FlexContainer::Property::ALIGN_CONTENT:
401 Toolkit::FlexContainer::Alignment alignContent( Toolkit::FlexContainer::ALIGN_FLEX_START );
403 if( value.GetType() == Property::INTEGER )
405 flexContainerImpl.SetAlignContent( static_cast<Toolkit::FlexContainer::Alignment>( value.Get< int >() ) );
407 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::Alignment >( value.Get< std::string >().c_str(),
408 ALIGN_CONTENT_STRING_TABLE,
409 ALIGN_CONTENT_STRING_TABLE_COUNT,
412 flexContainerImpl.SetAlignContent(alignContent);
420 Property::Value FlexContainer::GetProperty( BaseObject* object, Property::Index index )
422 Property::Value value;
424 Toolkit::FlexContainer flexContainer = Toolkit::FlexContainer::DownCast( Dali::BaseHandle( object ) );
428 FlexContainer& flexContainerImpl( GetImpl( flexContainer ) );
431 case Toolkit::FlexContainer::Property::CONTENT_DIRECTION:
433 value = flexContainerImpl.GetContentDirection();
436 case Toolkit::FlexContainer::Property::FLEX_DIRECTION:
438 value = flexContainerImpl.GetFlexDirection();
441 case Toolkit::FlexContainer::Property::FLEX_WRAP:
443 value = flexContainerImpl.GetFlexWrap();
446 case Toolkit::FlexContainer::Property::JUSTIFY_CONTENT:
448 value = flexContainerImpl.GetJustifyContent();
451 case Toolkit::FlexContainer::Property::ALIGN_ITEMS:
453 value = flexContainerImpl.GetAlignItems();
456 case Toolkit::FlexContainer::Property::ALIGN_CONTENT:
458 value = flexContainerImpl.GetAlignContent();
467 void FlexContainer::OnChildAdd( Actor& child )
469 // Create a new node for the child.
470 FlexItemNode childNode;
471 childNode.actor = child;
472 childNode.node = new_css_node();
473 childNode.node->get_child = GetChildNodeAtIndex;
474 childNode.node->is_dirty = IsNodeDirty;
475 mChildrenNodes.push_back(childNode);
477 Control::OnChildAdd( child );
480 void FlexContainer::OnChildRemove( Actor& child )
482 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
484 if( mChildrenNodes[i].actor.GetHandle() == child )
486 free_css_node( mChildrenNodes[i].node );
487 mChildrenNodes.erase( mChildrenNodes.begin() + i );
489 // Relayout the container only if instances were found
495 Control::OnChildRemove( child );
498 void FlexContainer::OnRelayout( const Vector2& size, RelayoutContainer& container )
500 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
502 Actor child = mChildrenNodes[i].actor.GetHandle();
505 // Anchor actor to top left of the container
506 if( child.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >() )
508 child.SetAnchorPoint( AnchorPoint::TOP_LEFT );
510 child.SetParentOrigin( ParentOrigin::TOP_LEFT );
512 float negotiatedWidth = child.GetRelayoutSize(Dimension::WIDTH);
513 float negotiatedHeight = child.GetRelayoutSize(Dimension::HEIGHT);
515 if( negotiatedWidth > 0 )
517 mChildrenNodes[i].node->style.dimensions[CSS_WIDTH] = negotiatedWidth;
519 if( negotiatedHeight > 0 )
521 mChildrenNodes[i].node->style.dimensions[CSS_HEIGHT] = negotiatedHeight;
526 // Relayout the container
529 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
531 Actor child = mChildrenNodes[i].actor.GetHandle();
534 if( child.GetPropertyType( Toolkit::FlexContainer::ChildProperty::FLEX ) != Property::NONE )
536 // Only Set to USE_ASSIGNED_SIZE if the child actor is flexible.
538 if( child.GetResizePolicy( Dimension::WIDTH ) != ResizePolicy::USE_ASSIGNED_SIZE )
540 child.SetResizePolicy( ResizePolicy::USE_ASSIGNED_SIZE, Dimension::WIDTH );
542 if( child.GetResizePolicy( Dimension::HEIGHT ) != ResizePolicy::USE_ASSIGNED_SIZE )
544 child.SetResizePolicy( ResizePolicy::USE_ASSIGNED_SIZE, Dimension::HEIGHT );
548 container.Add( child, Vector2(mChildrenNodes[i].node->layout.dimensions[CSS_WIDTH], mChildrenNodes[i].node->layout.dimensions[CSS_HEIGHT] ) );
553 bool FlexContainer::RelayoutDependentOnChildren( Dimension::Type dimension )
558 void FlexContainer::OnSizeSet( const Vector3& size )
564 mRootNode.node->style.dimensions[CSS_WIDTH] = size.x;
565 mRootNode.node->style.dimensions[CSS_HEIGHT] = size.y;
570 Control::OnSizeSet( size );
573 void FlexContainer::ComputeLayout()
577 mRootNode.node->children_count = mChildrenNodes.size();
579 // Intialize the layout.
580 mRootNode.node->layout.position[CSS_LEFT] = 0;
581 mRootNode.node->layout.position[CSS_TOP] = 0;
582 mRootNode.node->layout.position[CSS_BOTTOM] = 0;
583 mRootNode.node->layout.position[CSS_RIGHT] = 0;
584 mRootNode.node->layout.dimensions[CSS_WIDTH] = CSS_UNDEFINED;
585 mRootNode.node->layout.dimensions[CSS_HEIGHT] = CSS_UNDEFINED;
587 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
589 css_node_t* childNode = mChildrenNodes[i].node;
590 Actor childActor = mChildrenNodes[i].actor.GetHandle();
592 childNode->layout.position[CSS_LEFT] = 0;
593 childNode->layout.position[CSS_TOP] = 0;
594 childNode->layout.position[CSS_BOTTOM] = 0;
595 childNode->layout.position[CSS_RIGHT] = 0;
596 childNode->layout.dimensions[CSS_WIDTH] = CSS_UNDEFINED;
597 childNode->layout.dimensions[CSS_HEIGHT] = CSS_UNDEFINED;
599 // Intialize the style of the child.
600 childNode->style.minDimensions[CSS_WIDTH] = childActor.GetMinimumSize().x;
601 childNode->style.minDimensions[CSS_HEIGHT] = childActor.GetMinimumSize().y;
602 childNode->style.maxDimensions[CSS_WIDTH] = childActor.GetMaximumSize().x;
603 childNode->style.maxDimensions[CSS_HEIGHT] = childActor.GetMaximumSize().y;
605 // Check child properties on the child for how to layout it.
606 // These properties should be dynamically registered to the child which
607 // would be added to FlexContainer.
609 if( childActor.GetPropertyType( Toolkit::FlexContainer::ChildProperty::FLEX ) != Property::NONE )
611 childNode->style.flex = childActor.GetProperty( Toolkit::FlexContainer::ChildProperty::FLEX ).Get<float>();
614 Toolkit::FlexContainer::Alignment alignSelf( Toolkit::FlexContainer::ALIGN_AUTO );
615 if( childActor.GetPropertyType( Toolkit::FlexContainer::FlexContainer::ChildProperty::ALIGN_SELF ) != Property::NONE )
617 Property::Value alignSelfPropertyValue = childActor.GetProperty( Toolkit::FlexContainer::ChildProperty::ALIGN_SELF );
618 if( alignSelfPropertyValue.GetType() == Property::INTEGER )
620 alignSelf = static_cast<Toolkit::FlexContainer::Alignment>( alignSelfPropertyValue.Get< int >() );
622 else if( alignSelfPropertyValue.GetType() == Property::STRING )
624 std::string value = alignSelfPropertyValue.Get<std::string>();
625 Scripting::GetEnumeration< Toolkit::FlexContainer::Alignment >( value.c_str(),
626 ALIGN_SELF_STRING_TABLE,
627 ALIGN_SELF_STRING_TABLE_COUNT,
631 childNode->style.align_self = static_cast<css_align_t>(alignSelf);
633 if( childActor.GetPropertyType( Toolkit::FlexContainer::ChildProperty::FLEX_MARGIN ) != Property::NONE )
635 Vector4 flexMargin = childActor.GetProperty( Toolkit::FlexContainer::ChildProperty::FLEX_MARGIN ).Get<Vector4>();
636 childNode->style.margin[CSS_LEFT] = flexMargin.x;
637 childNode->style.margin[CSS_TOP] = flexMargin.y;
638 childNode->style.margin[CSS_RIGHT] = flexMargin.z;
639 childNode->style.margin[CSS_BOTTOM] = flexMargin.w;
643 // Calculate the layout
644 layoutNode( mRootNode.node, Self().GetMaximumSize().x, Self().GetMaximumSize().y, mRootNode.node->style.direction );
648 void FlexContainer::RelayoutChildren()
652 // Set size and position of children according to the layout calculation
653 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
655 Dali::Actor child = mChildrenNodes[i].actor.GetHandle();
658 child.SetX( mChildrenNodes[i].node->layout.position[CSS_LEFT] );
659 child.SetY( mChildrenNodes[i].node->layout.position[CSS_TOP] );
664 Actor FlexContainer::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
666 Actor nextFocusableActor;
668 // First check whether there is any items in the container
669 if( mChildrenNodes.size() > 0 )
671 if ( !currentFocusedActor || currentFocusedActor == Self() )
673 // Nothing is currently focused, so the first child in the container should be focused.
674 nextFocusableActor = mChildrenNodes[0].actor.GetHandle();
678 // Check whether the current focused actor is within flex container
679 int currentFocusedActorIndex = -1;
680 for( unsigned int index = 0; index < mChildrenNodes.size(); index++ )
682 if( currentFocusedActor == mChildrenNodes[index].actor.GetHandle() )
684 currentFocusedActorIndex = index;
689 if( currentFocusedActorIndex > -1 )
691 int previousCheckedActorIndex = -1;
692 int nextFocusedActorIndex = currentFocusedActorIndex;
695 case Toolkit::Control::KeyboardFocus::LEFT:
696 case Toolkit::Control::KeyboardFocus::UP:
698 // Search the next focusable actor in the backward direction
701 nextFocusedActorIndex--;
702 if( nextFocusedActorIndex < 0 )
704 nextFocusedActorIndex = loopEnabled ? mChildrenNodes.size() - 1 : 0;
706 if( nextFocusedActorIndex != previousCheckedActorIndex && nextFocusedActorIndex != currentFocusedActorIndex )
708 previousCheckedActorIndex = nextFocusedActorIndex;
714 } while ( !mChildrenNodes[nextFocusedActorIndex].actor.GetHandle().IsKeyboardFocusable() );
717 case Toolkit::Control::KeyboardFocus::RIGHT:
718 case Toolkit::Control::KeyboardFocus::DOWN:
720 // Search the next focusable actor in the forward direction
723 nextFocusedActorIndex++;
724 if( nextFocusedActorIndex > static_cast<int>(mChildrenNodes.size() - 1) )
726 nextFocusedActorIndex = loopEnabled ? 0 : mChildrenNodes.size() - 1;
728 if( nextFocusedActorIndex != previousCheckedActorIndex && nextFocusedActorIndex != currentFocusedActorIndex )
730 previousCheckedActorIndex = nextFocusedActorIndex;
736 } while ( !mChildrenNodes[nextFocusedActorIndex].actor.GetHandle().IsKeyboardFocusable() );
745 if( nextFocusedActorIndex != currentFocusedActorIndex )
747 nextFocusableActor = mChildrenNodes[nextFocusedActorIndex].actor.GetHandle();
751 // No focusble child in the container
752 nextFocusableActor = Actor();
757 // The current focused actor is not within flex container, so the first child in the container should be focused.
758 nextFocusableActor = mChildrenNodes[0].actor.GetHandle();
763 return nextFocusableActor;
766 FlexContainer::FlexContainer()
767 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
768 mContentDirection( Toolkit::FlexContainer::INHERIT ),
769 mFlexDirection( Toolkit::FlexContainer::COLUMN ),
770 mFlexWrap( Toolkit::FlexContainer::NO_WRAP ),
771 mJustifyContent( Toolkit::FlexContainer::JUSTIFY_FLEX_START ),
772 mAlignItems( Toolkit::FlexContainer::ALIGN_STRETCH ),
773 mAlignContent( Toolkit::FlexContainer::ALIGN_FLEX_START )
775 SetKeyboardNavigationSupport( true );
778 void FlexContainer::OnInitialize()
780 // Initialize the node for the flex container itself
781 Dali::Actor self = Self();
782 mRootNode.actor = self;
783 mRootNode.node = new_css_node();
784 mRootNode.node->context = &mChildrenNodes;
787 mRootNode.node->style.direction = static_cast<css_direction_t>( mContentDirection );
788 mRootNode.node->style.flex_direction = static_cast<css_flex_direction_t>( mFlexDirection );
789 mRootNode.node->style.flex_wrap = static_cast<css_wrap_type_t>( mFlexWrap );
790 mRootNode.node->style.justify_content = static_cast<css_justify_t>( mJustifyContent );
791 mRootNode.node->style.align_items = static_cast<css_align_t>( mAlignItems );
792 mRootNode.node->style.align_content = static_cast<css_align_t>( mAlignContent );
795 mRootNode.node->get_child = GetChildNodeAtIndex;
796 mRootNode.node->is_dirty = IsNodeDirty;
798 // Make self as keyboard focusable and focus group
799 self.SetKeyboardFocusable( true );
800 SetAsKeyboardFocusGroup( true );
803 } // namespace Internal
805 } // namespace Toolkit