2 * Copyright (c) 2014 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/popup/popup-impl.h>
22 #include <dali/public-api/adaptor-framework/key.h>
23 #include <dali/public-api/adaptor-framework/physical-keyboard.h>
24 #include <dali/public-api/animation/constraints.h>
25 #include <dali/public-api/common/stage.h>
26 #include <dali/public-api/events/key-event.h>
27 #include <dali/public-api/events/touch-event.h>
28 #include <dali/public-api/object/type-registry.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/public-api/images/resource-image.h>
33 #include <dali-toolkit/public-api/controls/buttons/button.h>
34 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
35 #include <dali-toolkit/public-api/controls/control-impl.h>
36 #include <dali-toolkit/internal/controls/relayout-helper.h>
37 #include <dali-toolkit/internal/focus-manager/keyboard-focus-manager-impl.h>
38 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
44 const float CONTENT_DEPTH = 1.0f; ///< 3D Effect of buttons/title etc. appearing off the popup.
45 const float POPUP_ANIMATION_DURATION = 0.5f; ///< Duration of hide/show animations
46 const float BACKING_DEPTH = -1.0f; ///< Depth of backing (positioned just behind dialog, so dialog catches hit events first)
48 const float POPUP_WIDTH = 720.0f; ///< Width of Popup
49 const float POPUP_OUT_MARGIN_WIDTH = 16.f; ///< Space between the screen edge and the popup edge in the horizontal dimension.
50 const float POPUP_OUT_MARGIN_HEIGHT = 36.f; ///< Space between the screen edge and the popup edge in the vertical dimension.
51 const float POPUP_TITLE_WIDTH = 648.0f; ///<Width of Popup Title
52 const float POPUP_BUTTON_BG_HEIGHT = 96.f; ///< Height of Button Background.
53 const Vector3 DEFAULT_DIALOG_SIZE = Vector3(POPUP_TITLE_WIDTH/POPUP_WIDTH, 0.5f, 0.0f);
54 const Vector3 DEFAULT_BOTTOM_SIZE = Vector3(1.0f, 0.2f, 0.0f);
58 const char* const SIGNAL_TOUCHED_OUTSIDE = "touched-outside";
59 const char* const SIGNAL_HIDDEN = "hidden";
63 const char* const PROPERTY_TITLE = "title";
64 const char* const PROPERTY_STATE = "state";
67 * The background size should be at least as big as the Dialog.
68 * In some cases a background may have graphics which are visible
69 * outside of the Dialog, e.g. A Shadow. For this we need to alter
70 * the size of Background.
72 * @param[in] outerBorder The border to extend beyond parent's Size.
73 * @param[in] parentSize The parent's size
75 Vector3 BackgroundSize(const Vector4& outerBoarder, const Vector3& parentSize)
77 Vector3 size( parentSize );
78 size.width += outerBoarder.x + outerBoarder.y;
79 size.height += outerBoarder.z + outerBoarder.w;
85 * sets button area size to parent's size plus a border.
87 * @param[in] outerBorder The border to extend beyond parent's Size.
88 * @param[in] parentSize The parent's size
90 Vector3 ButtonAreaSize( const Vector4& outBoarder, const Vector3& parentSize )
92 Vector3 size( parentSize );
93 size.width += outBoarder.x + outBoarder.y;
94 size.width -= (POPUP_OUT_MARGIN_WIDTH + POPUP_OUT_MARGIN_WIDTH);
95 size.height = POPUP_BUTTON_BG_HEIGHT;
100 } // unnamed namespace
116 return Toolkit::Popup::New();
119 TypeRegistration typeRegistration( typeid( Toolkit::Popup ), typeid( Toolkit::Control ), Create );
121 SignalConnectorType signalConnector1( typeRegistration, SIGNAL_TOUCHED_OUTSIDE, &Popup::DoConnectSignal );
122 SignalConnectorType signalConnector2( typeRegistration, SIGNAL_HIDDEN, &Popup::DoConnectSignal );
128 ///////////////////////////////////////////////////////////////////////////////////////////////////
130 ///////////////////////////////////////////////////////////////////////////////////////////////////
132 Dali::Toolkit::Popup Popup::New()
134 PopupStylePtr style = PopupStyleDefault::New();
136 // Create the implementation
137 PopupPtr popup(new Popup(*style));
139 // Pass ownership to CustomActor via derived handle
140 Dali::Toolkit::Popup handle(*popup);
142 // Second-phase init of the implementation
143 // This can only be done after the CustomActor connection has been made...
149 Popup::Popup(PopupStyle& style)
150 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
152 mState(Toolkit::Popup::POPUP_NONE), // Initially, the popup state should not be set, it's set in OnInitialize
153 mAlterAddedChild(false),
154 mPopupStyle(PopupStylePtr(&style)),
155 mPropertyTitle(Property::INVALID_INDEX),
156 mPropertyState(Property::INVALID_INDEX)
158 SetKeyboardNavigationSupport( true );
161 void Popup::OnInitialize()
164 self.SetSensitive(false);
167 mLayer = Layer::New();
168 mLayer.SetParentOrigin(ParentOrigin::CENTER);
169 mLayer.SetAnchorPoint(AnchorPoint::CENTER);
173 mPopupBg = Actor::New();
174 mPopupBg.SetParentOrigin(ParentOrigin::CENTER);
175 mPopupBg.SetAnchorPoint(AnchorPoint::CENTER);
176 mLayer.Add(mPopupBg);
178 // Any content after this point which is added to Self() will be reparented to
180 mAlterAddedChild = true;
182 // Add Backing (Dim effect)
185 // Add Dialog ( background image, title, content container, button container and tail )
189 ShowTail(ParentOrigin::BOTTOM_CENTER);
191 // Hide content by default.
192 SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
194 mPropertyTitle = self.RegisterProperty( PROPERTY_TITLE, "", Property::READ_WRITE );
195 mPropertyState = self.RegisterProperty( PROPERTY_STATE, "POPUP_HIDE", Property::READ_WRITE );
197 // Make self as keyboard focusable and focus group
198 self.SetKeyboardFocusable(true);
199 SetAsKeyboardFocusGroup(true);
202 void Popup::OnPropertySet( Property::Index index, Property::Value propertyValue )
204 if( index == mPropertyTitle )
206 SetTitle(propertyValue.Get<std::string>());
208 else if ( index == mPropertyState )
210 std::string value( propertyValue.Get<std::string>() );
211 if(value == "POPUP_SHOW")
213 SetState( Toolkit::Popup::POPUP_SHOW, 0.0f );
215 else if( value == "POPUP_HIDE")
217 SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
226 size_t Popup::GetButtonCount() const
228 return mButtons.size();
231 void Popup::SetBackgroundImage( Actor image )
233 // Removes any previous background.
234 if( mBackgroundImage && mPopupBg )
236 mPopupBg.Remove( mBackgroundImage );
239 // Adds new background to the dialog.
240 mBackgroundImage = image;
242 // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
243 mBackgroundImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
245 mPopupBg.Add( mBackgroundImage );
248 void Popup::SetButtonAreaImage( Actor image )
250 // Removes any previous area image.
251 if( mButtonAreaImage && mPopupBg )
253 mPopupBg.Remove( mButtonAreaImage );
256 // Adds new area image to the dialog.
257 mButtonAreaImage = image;
259 // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
260 mButtonAreaImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
262 mPopupBg.Add( mButtonAreaImage );
265 void Popup::SetTitle( const std::string& text )
267 Toolkit::TextView titleActor = Toolkit::TextView::New();
268 titleActor.SetText( text );
269 titleActor.SetColor( Color::BLACK );
270 titleActor.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
271 titleActor.SetWidthExceedPolicy( Toolkit::TextView::Split );
272 titleActor.SetLineJustification( Toolkit::TextView::Center );
274 SetTitle( titleActor );
277 void Popup::SetTitle( Toolkit::TextView titleActor )
279 // Replaces the current title actor.
280 if( mTitle && mPopupBg )
282 mPopupBg.Remove( mTitle );
286 mPopupBg.Add( mTitle );
291 Toolkit::TextView Popup::GetTitle() const
296 void Popup::AddButton( Toolkit::Button button )
298 mButtons.push_back( button );
299 mBottomBg.Add( button );
304 void Popup::SetState( Toolkit::Popup::PopupState state )
306 SetState( state, POPUP_ANIMATION_DURATION );
309 void Popup::SetState( Toolkit::Popup::PopupState state, float duration )
311 // default animation behaviour.
312 HandleStateChange(state, duration);
315 Toolkit::Popup::PopupState Popup::GetState() const
320 void Popup::ShowTail(const Vector3& position)
322 // Replaces the tail actor.
323 if(mTailImage && mTailImage.GetParent())
325 mTailImage.GetParent().Remove( mTailImage );
329 std::string image = "";
331 // depending on position of tail around ParentOrigin, a different tail image is used...
332 if(position.y < Math::MACHINE_EPSILON_1)
334 image = mPopupStyle->tailUpImage;
336 else if(position.y > 1.0f - Math::MACHINE_EPSILON_1)
338 image = mPopupStyle->tailDownImage;
340 else if(position.x < Math::MACHINE_EPSILON_1)
342 image = mPopupStyle->tailLeftImage;
344 else if(position.x > 1.0f - Math::MACHINE_EPSILON_1)
346 image = mPopupStyle->tailRightImage;
351 Image tail = ResourceImage::New( image );
352 mTailImage = ImageActor::New(tail);
353 const Vector3 anchorPoint = AnchorPoint::BOTTOM_RIGHT - position;
355 mTailImage.SetParentOrigin(position);
356 mTailImage.SetAnchorPoint(anchorPoint);
358 mBottomBg.Add(mTailImage);
362 void Popup::HideTail()
364 ShowTail(ParentOrigin::CENTER);
367 void Popup::SetStyle(PopupStyle& style)
369 mPopupStyle = PopupStylePtr(&style);
373 PopupStylePtr Popup::GetStyle() const
378 void Popup::SetDefaultBackgroundImage()
380 Image bg = ResourceImage::New( mPopupStyle->backgroundImage );
381 ImageActor bgImage = ImageActor::New( bg );
382 bgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
383 bgImage.SetNinePatchBorder( mPopupStyle->backgroundScale9Border );
385 Image buttonBg = ResourceImage::New( mPopupStyle->buttonAreaImage );
386 ImageActor buttonBgImage = ImageActor::New( buttonBg );
387 buttonBgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
388 buttonBgImage.SetNinePatchBorder( mPopupStyle->buttonArea9PatchBorder );
390 SetBackgroundImage( bgImage );
391 SetButtonAreaImage( buttonBgImage );
394 void Popup::CreateBacking()
396 mBacking = Dali::Toolkit::CreateSolidColorActor( mPopupStyle->backingColor );
398 mBacking.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
399 mBacking.SetSensitive(true);
401 mLayer.Add(mBacking);
402 mBacking.SetOpacity(0.0f);
403 mBacking.SetPosition(0.0f, 0.0f, BACKING_DEPTH);
404 mBacking.TouchedSignal().Connect( this, &Popup::OnBackingTouched );
405 mBacking.MouseWheelEventSignal().Connect(this, &Popup::OnBackingMouseWheelEvent);
408 void Popup::CreateDialog()
410 // Adds default background image.
411 SetDefaultBackgroundImage();
413 // Adds bottom background
414 mBottomBg = Actor::New();
415 mPopupBg.Add( mBottomBg );
418 void Popup::HandleStateChange( Toolkit::Popup::PopupState state, float duration )
420 const Vector2& stageSize( Stage::GetCurrent().GetSize() );
423 float targetBackingAlpha;
424 Vector3 targetBackingSize;
433 case Toolkit::Popup::POPUP_HIDE:
435 targetSize = Vector3(0.0f, 0.0f, 1.0f);
436 targetBackingAlpha = 0.0f;
437 targetBackingSize = Vector3(0.0f, 0.0f, 1.0f);
439 ClearKeyInputFocus();
441 // Retore the keyboard focus when popup is hidden
442 if(mPreviousFocusedActor && mPreviousFocusedActor.IsKeyboardFocusable() )
444 Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
445 if( keyboardFocusManager )
447 keyboardFocusManager.SetCurrentFocusActor(mPreviousFocusedActor);
454 case Toolkit::Popup::POPUP_SHOW:
457 targetSize = Vector3(1.0f, 1.0f, 1.0f);
458 targetBackingAlpha = 1.0f;
459 float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
460 targetBackingSize = Vector3( length, length, 1.0f );
463 // Add contents to stage for showing.
464 if( !mLayer.GetParent() )
466 mAlterAddedChild = false;
468 mAlterAddedChild = true;
470 Self().SetSensitive(true);
473 // Handle the keyboard focus when popup is shown
474 Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
475 if( keyboardFocusManager )
477 mPreviousFocusedActor = keyboardFocusManager.GetCurrentFocusActor();
479 if( mContent && mContent.IsKeyboardFocusable() )
481 // If content is focusable, move the focus to content
482 keyboardFocusManager.SetCurrentFocusActor(mContent);
484 else if( !mButtons.empty() )
486 // Otherwise, movethe focus to the first button
487 keyboardFocusManager.SetCurrentFocusActor(mButtons[0]);
491 DALI_LOG_WARNING("There is no focusable in popup\n");
498 mBacking.SetSize( targetBackingSize );
500 if(duration > Math::MACHINE_EPSILON_1)
508 mAnimation = Animation::New(duration);
512 mAnimation.AnimateTo( Property(mBacking, Actor::Property::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
513 mAnimation.AnimateTo( Property(mPopupBg, Actor::Property::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(duration * 0.5f, duration * 0.5f) );
517 mAnimation.AnimateTo( Property(mBacking, Actor::Property::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
518 mAnimation.AnimateTo( Property(mPopupBg, Actor::Property::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
521 mAnimation.FinishedSignal().Connect(this, &Popup::OnStateAnimationFinished);
525 mBacking.SetOpacity( targetBackingAlpha );
526 mPopupBg.SetScale( targetSize );
528 HandleStateChangeComplete();
532 void Popup::HandleStateChangeComplete()
534 // Remove contents from stage if completely hidden.
535 if( (mState == Toolkit::Popup::POPUP_HIDE) && (mLayer.GetParent()) )
537 Self().Remove(mLayer);
538 Self().SetSensitive( false );
540 // Guard against destruction during signal emission
541 Toolkit::Popup handle( GetOwner() );
542 mHiddenSignal.Emit();
546 Toolkit::Popup::TouchedOutsideSignalType& Popup::OutsideTouchedSignal()
548 return mTouchedOutsideSignal;
551 Toolkit::Popup::HiddenSignalType& Popup::HiddenSignal()
553 return mHiddenSignal;
556 bool Popup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
558 Dali::BaseHandle handle( object );
560 bool connected( true );
561 Toolkit::Popup popup = Toolkit::Popup::DownCast( handle );
563 if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED_OUTSIDE ) )
565 popup.OutsideTouchedSignal().Connect( tracker, functor );
567 else if( 0 == strcmp( signalName.c_str(), SIGNAL_HIDDEN ) )
569 popup.HiddenSignal().Connect( tracker, functor );
573 // signalName does not match any signal
580 void Popup::OnStateAnimationFinished( Animation& source )
582 HandleStateChangeComplete();
585 bool Popup::OnBackingTouched(Actor actor, const TouchEvent& event)
587 if(event.GetPointCount()>0)
589 const TouchPoint& point = event.GetPoint(0);
591 if(point.state == TouchPoint::Down)
593 // Guard against destruction during signal emission
594 Toolkit::Popup handle( GetOwner() );
596 mTouchedOutsideSignal.Emit();
603 bool Popup::OnBackingMouseWheelEvent(Actor actor, const MouseWheelEvent& event)
605 // consume mouse wheel event in dimmed backing actor
609 bool Popup::OnDialogTouched(Actor actor, const TouchEvent& event)
611 // consume event (stops backing actor receiving touch events)
615 void Popup::OnControlChildAdd( Actor& child )
617 // reparent any children added by user to the body layer.
618 if( mAlterAddedChild )
620 // Removes previously added content.
623 mPopupBg.Remove( mContent );
626 // Reparent new content.
627 Self().Remove( child );
629 // keep a handle to the new content.
632 mPopupBg.Add( mContent );
636 void Popup::OnControlSizeSet( const Vector3& targetSize )
638 mLayer.SetSize( targetSize );
639 mPopupBg.SetSize( targetSize );
641 const Vector4 outerBorder = mPopupStyle->backgroundOuterBorder;
642 if( mBackgroundImage )
644 mBackgroundImage.SetSize( BackgroundSize( outerBorder,targetSize ) );
646 if( mButtonAreaImage )
648 mButtonAreaImage.SetSize( ButtonAreaSize( outerBorder, targetSize ) );
653 void Popup::OnRelayout( const Vector2& size, ActorSizeContainer& container )
655 // Set the popup size
657 popupSize.width = size.width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
658 popupSize.height = size.height - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
660 // Update sizes of all popup's components.
662 // Relayout background image.
663 // Adjust background position and size relative to parent to cater to outer Border.
664 // Some backgrounds are intended to over-spill. That is some content
665 // should appear outside the Dialog on all sides i.e. Shadows, glow effects.
666 const Vector4 outerBorder = mPopupStyle->backgroundOuterBorder;
668 if( mBackgroundImage )
670 mBackgroundImage.SetSize(BackgroundSize(outerBorder, Vector3(size)));
671 mBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
672 mBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
673 mBackgroundImage.SetPosition( -outerBorder.x, -outerBorder.y, 0.0f );
676 if( mPopupBg && mButtonAreaImage )
678 // If there are no buttons, button background is also removed.
679 if ( mButtons.size() == 0 )
681 mPopupBg.Remove( mButtonAreaImage );
685 mButtonAreaImage.SetSize( ButtonAreaSize(outerBorder, Vector3(size)) );
686 mButtonAreaImage.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
687 mButtonAreaImage.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
688 mButtonAreaImage.SetY( -outerBorder.z - POPUP_OUT_MARGIN_HEIGHT );
690 mPopupBg.Add( mButtonAreaImage );
695 Vector3 positionOffset( 0.0f, mPopupStyle->margin + POPUP_OUT_MARGIN_WIDTH, CONTENT_DEPTH );
699 titleSize.width = popupSize.width;
700 titleSize.height = mTitle.GetHeightForWidth( titleSize.width );
702 // As the default size policy for text-view is Fixed & Fixed, a size needs to be set.
703 // Otherwise size-negotiation algorithm uses the GetNaturalSize() with doesn't take
704 // into account the multiline and exceed policies, giving as result a wrong size.
705 mTitle.SetSize( titleSize );
706 Relayout( mTitle, titleSize, container );
708 mTitle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
709 mTitle.SetParentOrigin( ParentOrigin::TOP_CENTER );
710 mTitle.SetPosition( positionOffset );
712 positionOffset.y += titleSize.height + mPopupStyle->margin;
718 // If the content width is greater than popup width then scale it down/wrap text as needed
719 Vector2 contentSize( RelayoutHelper::GetNaturalSize( mContent ) );
720 if( contentSize.width > popupSize.width )
722 contentSize.width = popupSize.width;
723 contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, contentSize.width );
726 mContent.SetSize( contentSize );
727 Relayout( mContent, contentSize, container );
729 mContent.SetParentOrigin(ParentOrigin::TOP_CENTER);
730 mContent.SetAnchorPoint(AnchorPoint::TOP_CENTER);
732 mContent.SetPosition( positionOffset );
734 positionOffset.y += contentSize.height + mPopupStyle->margin;
737 // Relayout Button Area
740 mBottomBg.SetSize( popupSize.width, mPopupStyle->bottomSize.height );
742 mBottomBg.SetParentOrigin(ParentOrigin::TOP_CENTER);
743 mBottomBg.SetAnchorPoint(AnchorPoint::TOP_CENTER);
745 mBottomBg.SetPosition( positionOffset );
748 // Relayout All buttons
749 if ( !mButtons.empty() )
751 // All buttons should be the same size and fill the button area. The button spacing needs to be accounted for as well.
752 Vector2 buttonSize( ( ( popupSize.width - mPopupStyle->buttonSpacing * ( mButtons.size() - 1 ) ) / mButtons.size() ),
753 mPopupStyle->bottomSize.height - mPopupStyle->margin );
755 Vector3 buttonPosition;
757 for ( ActorIter iter = mButtons.begin(), endIter = mButtons.end();
759 ++iter, buttonPosition.x += mPopupStyle->buttonSpacing + buttonSize.width )
761 iter->SetPosition( buttonPosition );
763 // If there is only one button, it needs to be laid out on center.
764 if ( mButtons.size() == 1 )
766 iter->SetAnchorPoint( AnchorPoint::CENTER );
767 iter->SetParentOrigin( ParentOrigin::CENTER );
771 iter->SetAnchorPoint( AnchorPoint::CENTER_LEFT );
772 iter->SetParentOrigin( ParentOrigin::CENTER_LEFT );
775 Relayout( *iter, buttonSize, container );
779 if( mShowing && mBacking )
781 Vector2 stageSize = Stage::GetCurrent().GetSize();
782 float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
783 Vector3 targetBackingSize = Vector3( length, length, 1.0f );
785 mBacking.SetSize( targetBackingSize );
789 bool Popup::OnKeyEvent(const KeyEvent& event)
791 bool consumed = false;
793 if(event.state == KeyEvent::Down)
795 if (event.keyCode == Dali::DALI_KEY_ESCAPE || event.keyCode == Dali::DALI_KEY_BACK)
797 SetState(Toolkit::Popup::POPUP_HIDE);
805 Vector3 Popup::GetNaturalSize()
807 float margin = 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
808 const float maxWidth = Stage::GetCurrent().GetSize().width - margin;
810 Vector3 naturalSize( 0.0f, 0.0f, 0.0f );
814 Vector3 titleNaturalSize = mTitle.GetImplementation().GetNaturalSize();
815 // Buffer to avoid errors. The width of the popup could potentially be the width of the title text.
816 // It was observed in this case that text wrapping was then inconsistent when seen on device
817 const float titleBuffer = 0.5f;
818 titleNaturalSize.width += titleBuffer;
820 // As TextView GetNaturalSize does not take wrapping into account, limit the width
821 // to that of the stage
822 if( titleNaturalSize.width >= maxWidth)
824 naturalSize.width = maxWidth;
825 naturalSize.height = mTitle.GetImplementation().GetHeightForWidth( naturalSize.width );
829 naturalSize += titleNaturalSize;
832 naturalSize.height += mPopupStyle->margin;
837 Vector3 contentSize = RelayoutHelper::GetNaturalSize( mContent );
838 // Choose the biggest width
839 naturalSize.width = std::max( naturalSize.width, contentSize.width );
840 if( naturalSize.width > maxWidth )
842 naturalSize.width = maxWidth;
843 contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, maxWidth );
845 naturalSize.height += contentSize.height + mPopupStyle->margin;
848 if( !mButtons.empty() )
850 naturalSize.height += mPopupStyle->bottomSize.height;
854 naturalSize.width += margin;
855 naturalSize.height += margin;
860 float Popup::GetHeightForWidth( float width )
862 float height( 0.0f );
863 float popupWidth( width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
867 height += mTitle.GetImplementation().GetHeightForWidth( popupWidth );
868 height += mPopupStyle->margin;
873 height += RelayoutHelper::GetHeightForWidth( mContent, popupWidth ) + mPopupStyle->margin;
876 if( !mButtons.empty() )
878 height += mPopupStyle->bottomSize.height;
882 float margin( 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
888 float Popup::GetWidthForHeight( float height )
890 return GetNaturalSize().width;
893 Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
895 Actor nextFocusableActor( currentFocusedActor );
897 // TODO: Needs to be optimised
899 if ( !currentFocusedActor || ( currentFocusedActor && KeyboardFocusManager::Get().GetFocusGroup(currentFocusedActor) != Self() ) )
901 // The current focused actor is not within popup
902 if( mContent && mContent.IsKeyboardFocusable() )
904 // If content is focusable, move the focus to content
905 nextFocusableActor = mContent;
907 else if( !mButtons.empty() )
909 // Otherwise, movethe focus to the first button
910 nextFocusableActor = mButtons[0];
915 // Rebuild the focus chain because button or content can be added or removed dynamically
916 ActorContainer focusableActors;
917 if( mContent && mContent.IsKeyboardFocusable() )
919 focusableActors.push_back(mContent);
922 for(unsigned int i = 0; i < mButtons.size(); i++)
924 if( mButtons[i] && mButtons[i].IsKeyboardFocusable() )
926 focusableActors.push_back(mButtons[i]);
930 for ( ActorContainer::iterator iter = focusableActors.begin(), end = focusableActors.end(); iter != end; ++iter )
932 if ( currentFocusedActor == *iter )
936 case Toolkit::Control::Left:
938 if ( iter == focusableActors.begin() )
940 nextFocusableActor = *( focusableActors.end() - 1 );
944 nextFocusableActor = *( iter - 1 );
948 case Toolkit::Control::Right:
950 if ( iter == focusableActors.end() - 1 )
952 nextFocusableActor = *( focusableActors.begin() );
956 nextFocusableActor = *( iter + 1 );
961 case Toolkit::Control::Up:
963 if ( *iter == mContent )
965 nextFocusableActor = *( focusableActors.end() - 1 );
969 if ( mContent && mContent.IsKeyboardFocusable() )
971 nextFocusableActor = mContent;
975 if ( iter == focusableActors.begin() )
977 nextFocusableActor = *( focusableActors.end() - 1 );
981 nextFocusableActor = *( iter - 1 );
988 case Toolkit::Control::Down:
990 if ( mContent && mContent.IsKeyboardFocusable() )
992 nextFocusableActor = mContent;
996 if ( iter == focusableActors.end() - 1 )
998 nextFocusableActor = *( focusableActors.begin() );
1002 nextFocusableActor = *( iter + 1 );
1006 if ( *iter == mContent && !mButtons.empty() )
1008 nextFocusableActor = mButtons[0];
1014 if(!nextFocusableActor)
1016 DALI_LOG_WARNING("Can not decide next focusable actor\n");
1024 return nextFocusableActor;
1027 } // namespace Internal
1029 } // namespace Toolkit