2 * Copyright (c) 2016 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/devel-api/object/type-registry-helper.h>
26 #include <dali/devel-api/scripting/scripting.h>
27 #include <dali/public-api/size-negotiation/relayout-container.h>
28 #include <dali/integration-api/debug.h>
35 * Custom properties for how to lay out the actor.
37 * When an actor is add to the flex container, the following custom properties of the actor
38 * are checked to decide how to lay out the actor inside the flex container.
40 * These non-animatable properties should be registered to the child which would be added
41 * to the flex container, and once added their values can not be changed.
43 const char * const FLEX_PROPERTY_NAME("flex");
44 const char * const ALIGN_SELF_PROPERTY_NAME("alignSelf");
45 const char * const FLEX_MARGIN_PROPERTY_NAME("flexMargin");
47 #if defined(DEBUG_ENABLED)
48 // debugging support, very useful when new features are added or bugs are hunted down
49 // currently not called from code so compiler will optimize these away, kept here for future debugging
51 #define FLEX_CONTAINER_TAG "DALI Toolkit::FlexContainer "
52 #define FC_LOG(fmt, args...) Debug::LogMessage(Debug::DebugInfo, FLEX_CONTAINER_TAG fmt, ## args)
53 //#define FLEX_CONTAINER_DEBUG 1
55 #if defined(FLEX_CONTAINER_DEBUG)
56 void PrintNode( Toolkit::Internal::FlexContainer::FlexItemNodeContainer itemNodes )
58 // Print the style property and layout of all the children
59 for( unsigned int i = 0; i < itemNodes.size(); ++i )
61 FC_LOG( "Item %d style: \n", i );
62 print_css_node( itemNodes[i].node, (css_print_options_t)( CSS_PRINT_STYLE | CSS_PRINT_CHILDREN ) );
63 FC_LOG( "Item %d layout: \n", i );
64 print_css_node( itemNodes[i].node, (css_print_options_t)( CSS_PRINT_LAYOUT | CSS_PRINT_CHILDREN ) );
69 #endif // defined(FLEX_CONTAINER_DEBUG)
70 #endif // defined(DEBUG_ENABLED)
90 return Toolkit::FlexContainer::New();
93 // Setup properties, signals and actions using the type-registry.
94 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::FlexContainer, Toolkit::Control, Create );
96 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "contentDirection", INTEGER, CONTENT_DIRECTION )
97 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "flexDirection", INTEGER, FLEX_DIRECTION )
98 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "flexWrap", INTEGER, FLEX_WRAP )
99 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "justifyContent", INTEGER, JUSTIFY_CONTENT )
100 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "alignItems", INTEGER, ALIGN_ITEMS )
101 DALI_PROPERTY_REGISTRATION( Toolkit, FlexContainer, "alignContent", INTEGER, ALIGN_CONTENT )
103 DALI_TYPE_REGISTRATION_END()
105 const Scripting::StringEnum ALIGN_SELF_STRING_TABLE[] =
107 { "auto", Toolkit::FlexContainer::ALIGN_AUTO },
108 { "flexStart", Toolkit::FlexContainer::ALIGN_FLEX_START },
109 { "center", Toolkit::FlexContainer::ALIGN_CENTER },
110 { "flexEnd", Toolkit::FlexContainer::ALIGN_FLEX_END },
111 { "stretch", Toolkit::FlexContainer::ALIGN_STRETCH }
113 const unsigned int ALIGN_SELF_STRING_TABLE_COUNT = sizeof( ALIGN_SELF_STRING_TABLE ) / sizeof( ALIGN_SELF_STRING_TABLE[0] );
115 const Scripting::StringEnum CONTENT_DIRECTION_STRING_TABLE[] =
117 { "inherit", Toolkit::FlexContainer::INHERIT },
118 { "LTR", Toolkit::FlexContainer::LTR },
119 { "RTL", Toolkit::FlexContainer::RTL }
121 const unsigned int CONTENT_DIRECTION_STRING_TABLE_COUNT = sizeof( CONTENT_DIRECTION_STRING_TABLE ) / sizeof( CONTENT_DIRECTION_STRING_TABLE[0] );
123 const Scripting::StringEnum FLEX_DIRECTION_STRING_TABLE[] =
125 { "column", Toolkit::FlexContainer::COLUMN },
126 { "columnReverse", Toolkit::FlexContainer::COLUMN_REVERSE },
127 { "row", Toolkit::FlexContainer::ROW },
128 { "rowReverse", Toolkit::FlexContainer::ROW_REVERSE }
130 const unsigned int FLEX_DIRECTION_STRING_TABLE_COUNT = sizeof( FLEX_DIRECTION_STRING_TABLE ) / sizeof( FLEX_DIRECTION_STRING_TABLE[0] );
132 const Scripting::StringEnum FLEX_WRAP_STRING_TABLE[] =
134 { "noWrap", Toolkit::FlexContainer::NO_WRAP },
135 { "wrap", Toolkit::FlexContainer::WRAP }
137 const unsigned int FLEX_WRAP_STRING_TABLE_COUNT = sizeof( FLEX_WRAP_STRING_TABLE ) / sizeof( FLEX_WRAP_STRING_TABLE[0] );
139 const Scripting::StringEnum JUSTIFY_CONTENT_STRING_TABLE[] =
141 { "flexStart", Toolkit::FlexContainer::JUSTIFY_FLEX_START },
142 { "center", Toolkit::FlexContainer::JUSTIFY_CENTER },
143 { "flexEnd", Toolkit::FlexContainer::JUSTIFY_FLEX_END },
144 { "spaceBetween", Toolkit::FlexContainer::JUSTIFY_SPACE_BETWEEN },
145 { "spaceAround", Toolkit::FlexContainer::JUSTIFY_SPACE_AROUND }
147 const unsigned int JUSTIFY_CONTENT_STRING_TABLE_COUNT = sizeof( JUSTIFY_CONTENT_STRING_TABLE ) / sizeof( JUSTIFY_CONTENT_STRING_TABLE[0] );
149 const Scripting::StringEnum ALIGN_ITEMS_STRING_TABLE[] =
151 { "flexStart", Toolkit::FlexContainer::ALIGN_FLEX_START },
152 { "center", Toolkit::FlexContainer::ALIGN_CENTER },
153 { "flexEnd", Toolkit::FlexContainer::ALIGN_FLEX_END },
154 { "stretch", Toolkit::FlexContainer::ALIGN_STRETCH }
156 const unsigned int ALIGN_ITEMS_STRING_TABLE_COUNT = sizeof( ALIGN_ITEMS_STRING_TABLE ) / sizeof( ALIGN_ITEMS_STRING_TABLE[0] );
158 const Scripting::StringEnum ALIGN_CONTENT_STRING_TABLE[] =
160 { "flexStart", Toolkit::FlexContainer::ALIGN_FLEX_START },
161 { "center", Toolkit::FlexContainer::ALIGN_CENTER },
162 { "flexEnd", Toolkit::FlexContainer::ALIGN_FLEX_END },
163 { "stretch", Toolkit::FlexContainer::ALIGN_STRETCH }
165 const unsigned int ALIGN_CONTENT_STRING_TABLE_COUNT = sizeof( ALIGN_CONTENT_STRING_TABLE ) / sizeof( ALIGN_CONTENT_STRING_TABLE[0] );
168 * The function used by the layout algorithm to be get the style properties
169 * and layout information of the child at the given index.
171 css_node_t* GetChildNodeAtIndex( void *childrenNodes, int i )
173 FlexContainer::FlexItemNodeContainer childrenNodeContainer = *( static_cast<FlexContainer::FlexItemNodeContainer*>( childrenNodes ) );
174 return childrenNodeContainer[i].node;
178 * The function used by the layout algorithm to check whether the node is dirty
181 bool IsNodeDirty( void *itemNodes )
183 // We only calculate the layout when the child is added or removed, or when
184 // style properties are changed. So should always return true here.
188 } // Unnamed namespace
190 Toolkit::FlexContainer FlexContainer::New()
192 // Create the implementation, temporarily owned by this handle on stack
193 IntrusivePtr< FlexContainer > impl = new FlexContainer();
195 // Pass ownership to CustomActor handle
196 Toolkit::FlexContainer handle( *impl );
198 // Second-phase init of the implementation
199 // This can only be done after the CustomActor connection has been made...
205 FlexContainer::~FlexContainer()
207 free_css_node( mRootNode.node );
209 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
211 free_css_node( mChildrenNodes[i].node );
214 mChildrenNodes.clear();
217 void FlexContainer::SetContentDirection( Toolkit::FlexContainer::ContentDirection contentDirection )
219 if( mContentDirection != contentDirection )
221 mContentDirection = contentDirection;
222 mRootNode.node->style.direction = static_cast<css_direction_t>( mContentDirection );
228 Toolkit::FlexContainer::ContentDirection FlexContainer::GetContentDirection()
230 return mContentDirection;
233 void FlexContainer::SetFlexDirection( Toolkit::FlexContainer::FlexDirection flexDirection )
235 if( mFlexDirection != flexDirection )
237 mFlexDirection = flexDirection;
238 mRootNode.node->style.flex_direction = static_cast<css_flex_direction_t>( mFlexDirection );
244 Toolkit::FlexContainer::FlexDirection FlexContainer::GetFlexDirection()
246 return mFlexDirection;
249 void FlexContainer::SetFlexWrap( Toolkit::FlexContainer::WrapType flexWrap )
251 if( mFlexWrap != flexWrap )
253 mFlexWrap = flexWrap;
254 mRootNode.node->style.flex_wrap = static_cast<css_wrap_type_t>( mFlexWrap );
260 Toolkit::FlexContainer::WrapType FlexContainer::GetFlexWrap()
265 void FlexContainer::SetJustifyContent( Toolkit::FlexContainer::Justification justifyContent )
267 if( mJustifyContent != justifyContent )
269 mJustifyContent = justifyContent;
270 mRootNode.node->style.justify_content = static_cast<css_justify_t>( mJustifyContent );
276 Toolkit::FlexContainer::Justification FlexContainer::GetJustifyContent()
278 return mJustifyContent;
281 void FlexContainer::SetAlignItems( Toolkit::FlexContainer::Alignment alignItems )
283 if( mAlignItems != alignItems )
285 mAlignItems = alignItems;
286 mRootNode.node->style.align_items = static_cast<css_align_t>( mAlignItems );
292 Toolkit::FlexContainer::Alignment FlexContainer::GetAlignItems()
297 void FlexContainer::SetAlignContent( Toolkit::FlexContainer::Alignment alignContent )
299 if( mAlignContent != alignContent )
301 mAlignContent = alignContent;
302 mRootNode.node->style.align_content = static_cast<css_align_t>( mAlignContent );
308 Toolkit::FlexContainer::Alignment FlexContainer::GetAlignContent()
310 return mAlignContent;
313 void FlexContainer::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
315 Toolkit::FlexContainer flexContainer = Toolkit::FlexContainer::DownCast( Dali::BaseHandle( object ) );
319 FlexContainer& flexContainerImpl( GetImpl( flexContainer ) );
322 case Toolkit::FlexContainer::Property::CONTENT_DIRECTION:
324 Toolkit::FlexContainer::ContentDirection contentDirection( Toolkit::FlexContainer::INHERIT );
326 if( value.GetType() == Property::INTEGER )
328 flexContainerImpl.SetContentDirection( static_cast<Toolkit::FlexContainer::ContentDirection>( value.Get< int >() ) );
330 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::ContentDirection >( value.Get< std::string >().c_str(),
331 CONTENT_DIRECTION_STRING_TABLE,
332 CONTENT_DIRECTION_STRING_TABLE_COUNT,
335 flexContainerImpl.SetContentDirection(contentDirection);
339 case Toolkit::FlexContainer::Property::FLEX_DIRECTION:
341 Toolkit::FlexContainer::FlexDirection flexDirection( Toolkit::FlexContainer::COLUMN );
343 if( value.GetType() == Property::INTEGER )
345 flexContainerImpl.SetFlexDirection( static_cast<Toolkit::FlexContainer::FlexDirection>( value.Get< int >() ) );
347 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::FlexDirection >( value.Get< std::string >().c_str(),
348 FLEX_DIRECTION_STRING_TABLE,
349 FLEX_DIRECTION_STRING_TABLE_COUNT,
352 flexContainerImpl.SetFlexDirection(flexDirection);
356 case Toolkit::FlexContainer::Property::FLEX_WRAP:
358 Toolkit::FlexContainer::WrapType flexWrap( Toolkit::FlexContainer::NO_WRAP );
360 if( value.GetType() == Property::INTEGER )
362 flexContainerImpl.SetFlexWrap( static_cast<Toolkit::FlexContainer::WrapType>( value.Get< int >() ) );
364 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::WrapType >( value.Get< std::string >().c_str(),
365 FLEX_WRAP_STRING_TABLE,
366 FLEX_WRAP_STRING_TABLE_COUNT,
369 flexContainerImpl.SetFlexWrap(flexWrap);
373 case Toolkit::FlexContainer::Property::JUSTIFY_CONTENT:
375 Toolkit::FlexContainer::Justification justifyContent( Toolkit::FlexContainer::JUSTIFY_FLEX_START );
377 if( value.GetType() == Property::INTEGER )
379 flexContainerImpl.SetJustifyContent( static_cast<Toolkit::FlexContainer::Justification>( value.Get< int >() ) );
381 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::Justification >( value.Get< std::string >().c_str(),
382 JUSTIFY_CONTENT_STRING_TABLE,
383 JUSTIFY_CONTENT_STRING_TABLE_COUNT,
386 flexContainerImpl.SetJustifyContent(justifyContent);
390 case Toolkit::FlexContainer::Property::ALIGN_ITEMS:
392 Toolkit::FlexContainer::Alignment alignItems( Toolkit::FlexContainer::ALIGN_STRETCH );
394 if( value.GetType() == Property::INTEGER )
396 flexContainerImpl.SetAlignItems( static_cast<Toolkit::FlexContainer::Alignment>( value.Get< int >() ) );
398 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::Alignment >( value.Get< std::string >().c_str(),
399 ALIGN_ITEMS_STRING_TABLE,
400 ALIGN_ITEMS_STRING_TABLE_COUNT,
403 flexContainerImpl.SetAlignItems(alignItems);
407 case Toolkit::FlexContainer::Property::ALIGN_CONTENT:
409 Toolkit::FlexContainer::Alignment alignContent( Toolkit::FlexContainer::ALIGN_FLEX_START );
411 if( value.GetType() == Property::INTEGER )
413 flexContainerImpl.SetAlignContent( static_cast<Toolkit::FlexContainer::Alignment>( value.Get< int >() ) );
415 else if( Scripting::GetEnumeration< Toolkit::FlexContainer::Alignment >( value.Get< std::string >().c_str(),
416 ALIGN_CONTENT_STRING_TABLE,
417 ALIGN_CONTENT_STRING_TABLE_COUNT,
420 flexContainerImpl.SetAlignContent(alignContent);
428 Property::Value FlexContainer::GetProperty( BaseObject* object, Property::Index index )
430 Property::Value value;
432 Toolkit::FlexContainer flexContainer = Toolkit::FlexContainer::DownCast( Dali::BaseHandle( object ) );
436 FlexContainer& flexContainerImpl( GetImpl( flexContainer ) );
439 case Toolkit::FlexContainer::Property::CONTENT_DIRECTION:
441 value = flexContainerImpl.GetContentDirection();
444 case Toolkit::FlexContainer::Property::FLEX_DIRECTION:
446 value = flexContainerImpl.GetFlexDirection();
449 case Toolkit::FlexContainer::Property::FLEX_WRAP:
451 value = flexContainerImpl.GetFlexWrap();
454 case Toolkit::FlexContainer::Property::JUSTIFY_CONTENT:
456 value = flexContainerImpl.GetJustifyContent();
459 case Toolkit::FlexContainer::Property::ALIGN_ITEMS:
461 value = flexContainerImpl.GetAlignItems();
464 case Toolkit::FlexContainer::Property::ALIGN_CONTENT:
466 value = flexContainerImpl.GetAlignContent();
475 void FlexContainer::OnChildAdd( Actor& child )
477 Control::OnChildAdd( child );
479 // Anchor actor to top left of the container
480 child.SetAnchorPoint( AnchorPoint::TOP_LEFT );
481 child.SetParentOrigin( ParentOrigin::TOP_LEFT );
483 // Create a new node for the child.
484 FlexItemNode childNode;
485 childNode.actor = child;
486 childNode.node = new_css_node();
487 childNode.node->get_child = GetChildNodeAtIndex;
488 childNode.node->is_dirty = IsNodeDirty;
489 mChildrenNodes.push_back(childNode);
492 void FlexContainer::OnChildRemove( Actor& child )
494 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
496 if( mChildrenNodes[i].actor.GetHandle() == child )
498 free_css_node( mChildrenNodes[i].node );
499 mChildrenNodes.erase( mChildrenNodes.begin() + i );
501 // Relayout the container only if instances were found
507 Control::OnChildRemove( child );
510 void FlexContainer::OnRelayout( const Vector2& size, RelayoutContainer& container )
512 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
514 Actor child = mChildrenNodes[i].actor.GetHandle();
517 float negotiatedWidth = child.GetRelayoutSize(Dimension::WIDTH);
518 float negotiatedHeight = child.GetRelayoutSize(Dimension::HEIGHT);
520 if( negotiatedWidth > 0 )
522 mChildrenNodes[i].node->style.dimensions[CSS_WIDTH] = negotiatedWidth;
524 if( negotiatedHeight > 0 )
526 mChildrenNodes[i].node->style.dimensions[CSS_HEIGHT] = negotiatedHeight;
531 // Relayout the container
534 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
536 Actor child = mChildrenNodes[i].actor.GetHandle();
539 if( child.GetResizePolicy( Dimension::WIDTH ) != ResizePolicy::USE_ASSIGNED_SIZE )
541 child.SetResizePolicy( ResizePolicy::USE_ASSIGNED_SIZE, Dimension::WIDTH );
543 if( child.GetResizePolicy( Dimension::HEIGHT ) != ResizePolicy::USE_ASSIGNED_SIZE )
545 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;
571 void FlexContainer::OnSizeAnimation( Animation& animation, const Vector3& targetSize )
573 // @todo Animate the children to their target size and position
576 void FlexContainer::ComputeLayout()
580 mRootNode.node->children_count = mChildrenNodes.size();
582 // Intialize the layout.
583 mRootNode.node->layout.position[CSS_LEFT] = 0;
584 mRootNode.node->layout.position[CSS_TOP] = 0;
585 mRootNode.node->layout.position[CSS_BOTTOM] = 0;
586 mRootNode.node->layout.position[CSS_RIGHT] = 0;
587 mRootNode.node->layout.dimensions[CSS_WIDTH] = CSS_UNDEFINED;
588 mRootNode.node->layout.dimensions[CSS_HEIGHT] = CSS_UNDEFINED;
590 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
592 css_node_t* childNode = mChildrenNodes[i].node;
593 Actor childActor = mChildrenNodes[i].actor.GetHandle();
595 childNode->layout.position[CSS_LEFT] = 0;
596 childNode->layout.position[CSS_TOP] = 0;
597 childNode->layout.position[CSS_BOTTOM] = 0;
598 childNode->layout.position[CSS_RIGHT] = 0;
599 childNode->layout.dimensions[CSS_WIDTH] = CSS_UNDEFINED;
600 childNode->layout.dimensions[CSS_HEIGHT] = CSS_UNDEFINED;
602 // Intialize the style of the child.
603 childNode->style.minDimensions[CSS_WIDTH] = childActor.GetMinimumSize().x;
604 childNode->style.minDimensions[CSS_HEIGHT] = childActor.GetMinimumSize().y;
605 childNode->style.maxDimensions[CSS_WIDTH] = childActor.GetMaximumSize().x;
606 childNode->style.maxDimensions[CSS_HEIGHT] = childActor.GetMaximumSize().y;
608 // Test custom properties on the child
609 if( childActor.GetPropertyIndex( FLEX_PROPERTY_NAME ) != Property::INVALID_INDEX )
611 childNode->style.flex = childActor.GetProperty( childActor.GetPropertyIndex(FLEX_PROPERTY_NAME) ).Get<float>();
614 Property::Index alignSelfPropertyIndex = childActor.GetPropertyIndex( ALIGN_SELF_PROPERTY_NAME );
615 if( alignSelfPropertyIndex != Property::INVALID_INDEX )
617 Property::Value alignSelfPropertyValue = childActor.GetProperty( alignSelfPropertyIndex );
619 Toolkit::FlexContainer::Alignment alignSelf( Toolkit::FlexContainer::ALIGN_AUTO );
620 if( alignSelfPropertyValue.GetType() == Property::INTEGER )
622 alignSelf = static_cast<Toolkit::FlexContainer::Alignment>( alignSelfPropertyValue.Get< int >() );
624 else if( alignSelfPropertyValue.GetType() == Property::STRING )
626 std::string value = alignSelfPropertyValue.Get<std::string>();
627 Scripting::GetEnumeration< Toolkit::FlexContainer::Alignment >( value.c_str(),
628 ALIGN_SELF_STRING_TABLE,
629 ALIGN_SELF_STRING_TABLE_COUNT,
632 childNode->style.align_self = static_cast<css_align_t>(alignSelf);
635 if( childActor.GetPropertyIndex( FLEX_MARGIN_PROPERTY_NAME ) != Property::INVALID_INDEX )
637 Vector4 flexMargin = childActor.GetProperty( childActor.GetPropertyIndex(FLEX_MARGIN_PROPERTY_NAME) ).Get<Vector4>();
638 childNode->style.margin[CSS_LEFT] = flexMargin.x;
639 childNode->style.margin[CSS_TOP] = flexMargin.y;
640 childNode->style.margin[CSS_RIGHT] = flexMargin.z;
641 childNode->style.margin[CSS_BOTTOM] = flexMargin.w;
645 // Calculate the layout
646 layoutNode( mRootNode.node, Self().GetMaximumSize().x, Self().GetMaximumSize().y, mRootNode.node->style.direction );
650 void FlexContainer::RelayoutChildren()
654 // Set size and position of children according to the layout calculation
655 for( unsigned int i = 0; i < mChildrenNodes.size(); i++ )
657 Dali::Actor child = mChildrenNodes[i].actor.GetHandle();
660 child.SetX( mChildrenNodes[i].node->layout.position[CSS_LEFT] );
661 child.SetY( mChildrenNodes[i].node->layout.position[CSS_TOP] );
666 Actor FlexContainer::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
668 Actor nextFocusableActor;
670 // First check whether there is any items in the container
671 if( mChildrenNodes.size() > 0 )
673 if ( !currentFocusedActor || currentFocusedActor == Self() )
675 // Nothing is currently focused, so the first child in the container should be focused.
676 nextFocusableActor = mChildrenNodes[0].actor.GetHandle();
680 // Check whether the current focused actor is within flex container
681 int currentFocusedActorIndex = -1;
682 for( unsigned int index = 0; index < mChildrenNodes.size(); index++ )
684 if( currentFocusedActor == mChildrenNodes[index].actor.GetHandle() )
686 currentFocusedActorIndex = index;
691 if( currentFocusedActorIndex > -1 )
693 int previousCheckedActorIndex = -1;
694 int nextFocusedActorIndex = currentFocusedActorIndex;
697 case Toolkit::Control::KeyboardFocus::LEFT:
698 case Toolkit::Control::KeyboardFocus::UP:
700 // Search the next focusable actor in the backward direction
703 nextFocusedActorIndex--;
704 if( nextFocusedActorIndex < 0 )
706 nextFocusedActorIndex = loopEnabled ? mChildrenNodes.size() - 1 : 0;
708 if( nextFocusedActorIndex != previousCheckedActorIndex && nextFocusedActorIndex != currentFocusedActorIndex )
710 previousCheckedActorIndex = nextFocusedActorIndex;
716 } while ( !mChildrenNodes[nextFocusedActorIndex].actor.GetHandle().IsKeyboardFocusable() );
719 case Toolkit::Control::KeyboardFocus::RIGHT:
720 case Toolkit::Control::KeyboardFocus::DOWN:
722 // Search the next focusable actor in the forward direction
725 nextFocusedActorIndex++;
726 if( nextFocusedActorIndex > static_cast<int>(mChildrenNodes.size() - 1) )
728 nextFocusedActorIndex = loopEnabled ? 0 : mChildrenNodes.size() - 1;
730 if( nextFocusedActorIndex != previousCheckedActorIndex && nextFocusedActorIndex != currentFocusedActorIndex )
732 previousCheckedActorIndex = nextFocusedActorIndex;
738 } while ( !mChildrenNodes[nextFocusedActorIndex].actor.GetHandle().IsKeyboardFocusable() );
743 if( nextFocusedActorIndex != currentFocusedActorIndex )
745 nextFocusableActor = mChildrenNodes[nextFocusedActorIndex].actor.GetHandle();
749 // No focusble child in the container
750 nextFocusableActor = Actor();
755 // The current focused actor is not within flex container, so the first child in the container should be focused.
756 nextFocusableActor = mChildrenNodes[0].actor.GetHandle();
761 return nextFocusableActor;
764 FlexContainer::FlexContainer()
765 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
766 mContentDirection( Toolkit::FlexContainer::INHERIT ),
767 mFlexDirection( Toolkit::FlexContainer::COLUMN ),
768 mFlexWrap( Toolkit::FlexContainer::NO_WRAP ),
769 mJustifyContent( Toolkit::FlexContainer::JUSTIFY_FLEX_START ),
770 mAlignItems( Toolkit::FlexContainer::ALIGN_STRETCH ),
771 mAlignContent( Toolkit::FlexContainer::ALIGN_FLEX_START )
773 SetKeyboardNavigationSupport( true );
776 void FlexContainer::OnInitialize()
778 // Initialize the node for the flex container itself
779 Dali::Actor self = Self();
780 mRootNode.actor = self;
781 mRootNode.node = new_css_node();
782 mRootNode.node->context = &mChildrenNodes;
785 mRootNode.node->style.direction = static_cast<css_direction_t>( mContentDirection );
786 mRootNode.node->style.flex_direction = static_cast<css_flex_direction_t>( mFlexDirection );
787 mRootNode.node->style.flex_wrap = static_cast<css_wrap_type_t>( mFlexWrap );
788 mRootNode.node->style.justify_content = static_cast<css_justify_t>( mJustifyContent );
789 mRootNode.node->style.align_items = static_cast<css_align_t>( mAlignItems );
790 mRootNode.node->style.align_content = static_cast<css_align_t>( mAlignContent );
793 mRootNode.node->get_child = GetChildNodeAtIndex;
794 mRootNode.node->is_dirty = IsNodeDirty;
796 // Make self as keyboard focusable and focus group
797 self.SetKeyboardFocusable( true );
798 SetAsKeyboardFocusGroup( true );
801 } // namespace Internal
803 } // namespace Toolkit