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>
32 #include <dali-toolkit/public-api/controls/buttons/button.h>
33 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
34 #include <dali-toolkit/public-api/controls/control-impl.h>
35 #include <dali-toolkit/internal/controls/relayout-helper.h>
36 #include <dali-toolkit/internal/focus-manager/keyboard-focus-manager-impl.h>
37 #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);
56 const char* const PROPERTY_TITLE = "title";
57 const char* const PROPERTY_STATE = "state";
59 // Constraints ///////////////////////////////////////////////////////////////////////////
62 * BackgroundSizeConstraint
64 * The background size should be at least as big as the Dialog.
65 * In some cases a background may have graphics which are visible
66 * outside of the Dialog, e.g. A Shadow. For this we need to alter
67 * the size of Background.
69 struct BackgroundSizeConstraint
72 * Constraint that sets size to parent's size plus a border.
74 * @param[in] outerBorder The border to extend beyond parent's Size.
76 BackgroundSizeConstraint( Vector4 outerBorder )
77 : mOuterBorder( outerBorder )
82 * (render thread code)
83 * @param[in] current The current size.
84 * @param[in] parentSizeProperty The parent's size
86 Vector3 operator()( const Vector3& current,
87 const PropertyInput& parentSizeProperty )
89 Vector3 size = parentSizeProperty.GetVector3();
91 size.width += mOuterBorder.x + mOuterBorder.y;
92 size.height += mOuterBorder.z + mOuterBorder.w;
97 const Vector4 mOuterBorder; ///< The size of the outer-border (Set to 0.0, 0.0f, 0.0f, 0.0f if doesn't exist).
100 struct ButtonAreaSizeConstraint
103 * Constraint that sets size to parent's size plus a border.
105 * @param[in] outerBorder The border to extend beyond parent's Size.
107 ButtonAreaSizeConstraint( Vector4 outerBorder )
108 : mOuterBorder( outerBorder )
113 * (render thread code)
114 * @param[in] current The current size.
115 * @param[in] parentSizeProperty The parent's size
117 Vector3 operator()( const Vector3& current,
118 const PropertyInput& parentSizeProperty )
120 Vector3 size = parentSizeProperty.GetVector3();
122 size.width += mOuterBorder.x + mOuterBorder.y;
123 size.width -= (POPUP_OUT_MARGIN_WIDTH + POPUP_OUT_MARGIN_WIDTH);
124 size.height = POPUP_BUTTON_BG_HEIGHT;
129 const Vector4 mOuterBorder; ///< The size of the outer-border (Set to 0.0, 0.0f, 0.0f, 0.0f if doesn't exist).
132 } // unnamed namespace
148 return Toolkit::Popup::New();
151 TypeRegistration typeRegistration( typeid(Toolkit::Popup), typeid(Toolkit::Control), Create );
153 SignalConnectorType signalConnector1( typeRegistration, Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE, &Popup::DoConnectSignal );
154 SignalConnectorType signalConnector2( typeRegistration, Toolkit::Popup::SIGNAL_HIDDEN, &Popup::DoConnectSignal );
160 ///////////////////////////////////////////////////////////////////////////////////////////////////
162 ///////////////////////////////////////////////////////////////////////////////////////////////////
164 Dali::Toolkit::Popup Popup::New()
166 PopupStylePtr style = PopupStyleDefault::New();
168 // Create the implementation
169 PopupPtr popup(new Popup(*style));
171 // Pass ownership to CustomActor via derived handle
172 Dali::Toolkit::Popup handle(*popup);
174 // Second-phase init of the implementation
175 // This can only be done after the CustomActor connection has been made...
181 Popup::Popup(PopupStyle& style)
182 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
184 mState(Toolkit::Popup::POPUP_NONE), // Initially, the popup state should not be set, it's set in OnInitialize
185 mAlterAddedChild(false),
186 mPopupStyle(PopupStylePtr(&style)),
187 mPropertyTitle(Property::INVALID_INDEX),
188 mPropertyState(Property::INVALID_INDEX)
190 SetKeyboardNavigationSupport( true );
193 void Popup::OnInitialize()
196 self.SetSensitive(false);
199 mLayer = Layer::New();
200 mLayer.SetParentOrigin(ParentOrigin::CENTER);
201 mLayer.SetAnchorPoint(AnchorPoint::CENTER);
203 mLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
206 mPopupBg = Actor::New();
207 mPopupBg.SetParentOrigin(ParentOrigin::CENTER);
208 mPopupBg.SetAnchorPoint(AnchorPoint::CENTER);
209 mPopupBg.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
210 mLayer.Add(mPopupBg);
212 // Any content after this point which is added to Self() will be reparented to
214 mAlterAddedChild = true;
216 // Add Backing (Dim effect)
219 // Add Dialog ( background image, title, content container, button container and tail )
223 ShowTail(ParentOrigin::BOTTOM_CENTER);
225 // Hide content by default.
226 SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
228 mPropertyTitle = self.RegisterProperty( PROPERTY_TITLE, "", Property::READ_WRITE );
229 mPropertyState = self.RegisterProperty( PROPERTY_STATE, "POPUP_HIDE", Property::READ_WRITE );
231 // Make self as keyboard focusable and focus group
232 self.SetKeyboardFocusable(true);
233 SetAsKeyboardFocusGroup(true);
236 void Popup::OnPropertySet( Property::Index index, Property::Value propertyValue )
238 if( index == mPropertyTitle )
240 SetTitle(propertyValue.Get<std::string>());
242 else if ( index == mPropertyState )
244 std::string value( propertyValue.Get<std::string>() );
245 if(value == "POPUP_SHOW")
247 SetState( Toolkit::Popup::POPUP_SHOW, 0.0f );
249 else if( value == "POPUP_HIDE")
251 SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
260 size_t Popup::GetButtonCount() const
262 return mButtons.size();
265 void Popup::SetBackgroundImage( Actor image )
267 // Removes any previous background.
268 if( mBackgroundImage && mPopupBg )
270 mPopupBg.Remove( mBackgroundImage );
273 // Adds new background to the dialog.
274 mBackgroundImage = image;
276 // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
277 mBackgroundImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
279 mPopupBg.Add( mBackgroundImage );
282 void Popup::SetButtonAreaImage( Actor image )
284 // Removes any previous area image.
285 if( mButtonAreaImage && mPopupBg )
287 mPopupBg.Remove( mButtonAreaImage );
290 // Adds new area image to the dialog.
291 mButtonAreaImage = image;
293 // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
294 mButtonAreaImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
296 mPopupBg.Add( mButtonAreaImage );
299 void Popup::SetTitle( const std::string& text )
301 Toolkit::TextView titleActor = Toolkit::TextView::New();
302 titleActor.SetText( text );
303 titleActor.SetColor( Color::BLACK );
304 titleActor.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
305 titleActor.SetWidthExceedPolicy( Toolkit::TextView::Split );
306 titleActor.SetLineJustification( Toolkit::TextView::Center );
308 SetTitle( titleActor );
311 void Popup::SetTitle( Toolkit::TextView titleActor )
313 // Replaces the current title actor.
314 if( mTitle && mPopupBg )
316 mPopupBg.Remove( mTitle );
320 mPopupBg.Add( mTitle );
325 Toolkit::TextView Popup::GetTitle() const
330 void Popup::AddButton( Toolkit::Button button )
332 mButtons.push_back( button );
333 mBottomBg.Add( button );
338 void Popup::SetState( Toolkit::Popup::PopupState state )
340 SetState( state, POPUP_ANIMATION_DURATION );
343 void Popup::SetState( Toolkit::Popup::PopupState state, float duration )
345 // default animation behaviour.
346 HandleStateChange(state, duration);
349 Toolkit::Popup::PopupState Popup::GetState() const
354 void Popup::ShowTail(const Vector3& position)
356 // Replaces the tail actor.
357 if(mTailImage && mTailImage.GetParent())
359 mTailImage.GetParent().Remove( mTailImage );
363 std::string image = "";
365 // depending on position of tail around ParentOrigin, a different tail image is used...
366 if(position.y < Math::MACHINE_EPSILON_1)
368 image = mPopupStyle->tailUpImage;
370 else if(position.y > 1.0f - Math::MACHINE_EPSILON_1)
372 image = mPopupStyle->tailDownImage;
374 else if(position.x < Math::MACHINE_EPSILON_1)
376 image = mPopupStyle->tailLeftImage;
378 else if(position.x > 1.0f - Math::MACHINE_EPSILON_1)
380 image = mPopupStyle->tailRightImage;
385 Image tail = Image::New( image );
386 mTailImage = ImageActor::New(tail);
387 const Vector3 anchorPoint = AnchorPoint::FRONT_BOTTOM_RIGHT - position;
389 mTailImage.SetParentOrigin(position);
390 mTailImage.SetAnchorPoint(anchorPoint);
392 mBottomBg.Add(mTailImage);
396 void Popup::HideTail()
398 ShowTail(ParentOrigin::CENTER);
401 void Popup::SetStyle(PopupStyle& style)
403 mPopupStyle = PopupStylePtr(&style);
407 PopupStylePtr Popup::GetStyle() const
412 void Popup::SetDefaultBackgroundImage()
414 Image bg = Image::New( mPopupStyle->backgroundImage );
415 ImageActor bgImage = ImageActor::New( bg );
416 bgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
417 bgImage.SetNinePatchBorder( mPopupStyle->backgroundScale9Border );
419 Image buttonBg = Image::New( mPopupStyle->buttonAreaImage );
420 ImageActor buttonBgImage = ImageActor::New( buttonBg );
421 buttonBgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
422 buttonBgImage.SetNinePatchBorder( mPopupStyle->buttonArea9PatchBorder );
424 SetBackgroundImage( bgImage );
425 SetButtonAreaImage( buttonBgImage );
428 void Popup::CreateBacking()
430 mBacking = Dali::Toolkit::CreateSolidColorActor( mPopupStyle->backingColor );
432 mBacking.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
433 mBacking.SetSensitive(true);
435 mLayer.Add(mBacking);
436 mBacking.SetOpacity(0.0f);
437 mBacking.SetPosition(0.0f, 0.0f, BACKING_DEPTH);
438 mBacking.TouchedSignal().Connect( this, &Popup::OnBackingTouched );
439 mBacking.MouseWheelEventSignal().Connect(this, &Popup::OnBackingMouseWheelEvent);
442 void Popup::CreateDialog()
444 // Adds default background image.
445 SetDefaultBackgroundImage();
447 // Adds bottom background
448 mBottomBg = Actor::New();
449 mPopupBg.Add( mBottomBg );
452 void Popup::HandleStateChange( Toolkit::Popup::PopupState state, float duration )
454 const Vector2& stageSize( Stage::GetCurrent().GetSize() );
457 float targetBackingAlpha;
458 Vector3 targetBackingSize;
467 case Toolkit::Popup::POPUP_HIDE:
469 targetSize = Vector3(0.0f, 0.0f, 1.0f);
470 targetBackingAlpha = 0.0f;
471 targetBackingSize = Vector3(0.0f, 0.0f, 1.0f);
473 ClearKeyInputFocus();
475 // Retore the keyboard focus when popup is hidden
476 if(mPreviousFocusedActor && mPreviousFocusedActor.IsKeyboardFocusable() )
478 Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
479 if( keyboardFocusManager )
481 keyboardFocusManager.SetCurrentFocusActor(mPreviousFocusedActor);
488 case Toolkit::Popup::POPUP_SHOW:
491 targetSize = Vector3(1.0f, 1.0f, 1.0f);
492 targetBackingAlpha = 1.0f;
493 float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
494 targetBackingSize = Vector3( length, length, 1.0f );
497 // Add contents to stage for showing.
498 if( !mLayer.GetParent() )
500 mAlterAddedChild = false;
502 mAlterAddedChild = true;
504 Self().SetSensitive(true);
507 // Handle the keyboard focus when popup is shown
508 Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
509 if( keyboardFocusManager )
511 mPreviousFocusedActor = keyboardFocusManager.GetCurrentFocusActor();
513 if( mContent && mContent.IsKeyboardFocusable() )
515 // If content is focusable, move the focus to content
516 keyboardFocusManager.SetCurrentFocusActor(mContent);
518 else if( !mButtons.empty() )
520 // Otherwise, movethe focus to the first button
521 keyboardFocusManager.SetCurrentFocusActor(mButtons[0]);
525 DALI_LOG_WARNING("There is no focusable in popup\n");
532 mBacking.SetSize( targetBackingSize );
534 if(duration > Math::MACHINE_EPSILON_1)
542 mAnimation = Animation::New(duration);
546 mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
547 mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(duration * 0.5f, duration * 0.5f) );
551 mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
552 mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
555 mAnimation.FinishedSignal().Connect(this, &Popup::OnStateAnimationFinished);
559 mBacking.SetOpacity( targetBackingAlpha );
560 mPopupBg.SetScale( targetSize );
562 HandleStateChangeComplete();
566 void Popup::HandleStateChangeComplete()
568 // Remove contents from stage if completely hidden.
569 if( (mState == Toolkit::Popup::POPUP_HIDE) && (mLayer.GetParent()) )
571 Self().Remove(mLayer);
572 Self().SetSensitive( false );
574 // Guard against destruction during signal emission
575 Toolkit::Popup handle( GetOwner() );
576 mHiddenSignalV2.Emit();
580 Toolkit::Popup::TouchedOutsideSignalV2& Popup::OutsideTouchedSignal()
582 return mTouchedOutsideSignalV2;
585 Toolkit::Popup::HiddenSignalV2& Popup::HiddenSignal()
587 return mHiddenSignalV2;
590 bool Popup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
592 Dali::BaseHandle handle( object );
594 bool connected( true );
595 Toolkit::Popup popup = Toolkit::Popup::DownCast(handle);
597 if( Dali::Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE == signalName )
599 popup.OutsideTouchedSignal().Connect( tracker, functor );
601 else if( Dali::Toolkit::Popup::SIGNAL_HIDDEN == signalName )
603 popup.HiddenSignal().Connect( tracker, functor );
607 // signalName does not match any signal
614 void Popup::OnStateAnimationFinished( Animation& source )
616 HandleStateChangeComplete();
619 bool Popup::OnBackingTouched(Actor actor, const TouchEvent& event)
621 if(event.GetPointCount()>0)
623 const TouchPoint& point = event.GetPoint(0);
625 if(point.state == TouchPoint::Down)
627 // Guard against destruction during signal emission
628 Toolkit::Popup handle( GetOwner() );
630 mTouchedOutsideSignalV2.Emit();
637 bool Popup::OnBackingMouseWheelEvent(Actor actor, const MouseWheelEvent& event)
639 // consume mouse wheel event in dimmed backing actor
643 bool Popup::OnDialogTouched(Actor actor, const TouchEvent& event)
645 // consume event (stops backing actor receiving touch events)
649 void Popup::OnControlChildAdd( Actor& child )
651 // reparent any children added by user to the body layer.
652 if( mAlterAddedChild )
654 // Removes previously added content.
657 mPopupBg.Remove( mContent );
660 // Reparent new content.
661 Self().Remove( child );
663 // keep a handle to the new content.
666 mPopupBg.Add( mContent );
670 void Popup::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
672 // Set the popup size
674 popupSize.width = size.width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
675 popupSize.height = size.height - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
677 // Update sizes of all popup's components.
679 // Relayout background image.
680 // Adjust background position and size relative to parent to cater to outer Border.
681 // Some backgrounds are intended to over-spill. That is some content
682 // should appear outside the Dialog on all sides i.e. Shadows, glow effects.
683 const Vector4 outerBorder = mPopupStyle->backgroundOuterBorder;
685 if( mBackgroundImage )
687 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
688 ParentSource( Actor::SIZE ),
689 BackgroundSizeConstraint(outerBorder) );
691 mBackgroundImage.RemoveConstraints();
692 mBackgroundImage.ApplyConstraint( constraint );
694 mBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
695 mBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
696 mBackgroundImage.SetPosition( -outerBorder.x, -outerBorder.y, 0.0f );
699 if( mPopupBg && mButtonAreaImage )
701 // If there are no buttons, button background is also removed.
702 if ( mButtons.size() == 0 )
704 mPopupBg.Remove( mButtonAreaImage );
708 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
709 ParentSource( Actor::SIZE ),
710 ButtonAreaSizeConstraint(outerBorder) );
712 mButtonAreaImage.RemoveConstraints();
713 mButtonAreaImage.ApplyConstraint( constraint );
715 mButtonAreaImage.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
716 mButtonAreaImage.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
717 mButtonAreaImage.SetY( -outerBorder.z - POPUP_OUT_MARGIN_HEIGHT );
719 mPopupBg.Add( mButtonAreaImage );
724 Vector3 positionOffset( 0.0f, mPopupStyle->margin + POPUP_OUT_MARGIN_WIDTH, CONTENT_DEPTH );
728 titleSize.width = popupSize.width;
729 titleSize.height = mTitle.GetHeightForWidth( titleSize.width );
731 // As the default size policy for text-view is Fixed & Fixed, a size needs to be set.
732 // Otherwise size-negotiation algorithm uses the GetNaturalSize() with doesn't take
733 // into account the multiline and exceed policies, giving as result a wrong size.
734 mTitle.SetSize( titleSize );
735 Relayout( mTitle, titleSize, container );
737 mTitle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
738 mTitle.SetParentOrigin( ParentOrigin::TOP_CENTER );
739 mTitle.SetPosition( positionOffset );
741 positionOffset.y += titleSize.height + mPopupStyle->margin;
747 // If the content width is greater than popup width then scale it down/wrap text as needed
748 Vector2 contentSize( RelayoutHelper::GetNaturalSize( mContent ) );
749 if( contentSize.width > popupSize.width )
751 contentSize.width = popupSize.width;
752 contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, contentSize.width );
755 mContent.SetSize( contentSize );
756 Relayout( mContent, contentSize, container );
758 mContent.SetParentOrigin(ParentOrigin::TOP_CENTER);
759 mContent.SetAnchorPoint(AnchorPoint::TOP_CENTER);
761 mContent.SetPosition( positionOffset );
763 positionOffset.y += contentSize.height + mPopupStyle->margin;
766 // Relayout Button Area
769 mBottomBg.SetSize( popupSize.width, mPopupStyle->bottomSize.height );
771 mBottomBg.SetParentOrigin(ParentOrigin::TOP_CENTER);
772 mBottomBg.SetAnchorPoint(AnchorPoint::TOP_CENTER);
774 mBottomBg.SetPosition( positionOffset );
777 // Relayout All buttons
778 if ( !mButtons.empty() )
780 // All buttons should be the same size and fill the button area. The button spacing needs to be accounted for as well.
781 Vector2 buttonSize( ( ( popupSize.width - mPopupStyle->buttonSpacing * ( mButtons.size() - 1 ) ) / mButtons.size() ),
782 mPopupStyle->bottomSize.height - mPopupStyle->margin );
784 Vector3 buttonPosition;
786 for ( ActorIter iter = mButtons.begin(), endIter = mButtons.end();
788 ++iter, buttonPosition.x += mPopupStyle->buttonSpacing + buttonSize.width )
790 iter->SetPosition( buttonPosition );
792 // If there is only one button, it needs to be laid out on center.
793 if ( mButtons.size() == 1 )
795 iter->SetAnchorPoint( AnchorPoint::CENTER );
796 iter->SetParentOrigin( ParentOrigin::CENTER );
800 iter->SetAnchorPoint( AnchorPoint::CENTER_LEFT );
801 iter->SetParentOrigin( ParentOrigin::CENTER_LEFT );
804 Relayout( *iter, buttonSize, container );
808 if( mShowing && mBacking )
810 Vector2 stageSize = Stage::GetCurrent().GetSize();
811 float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
812 Vector3 targetBackingSize = Vector3( length, length, 1.0f );
814 mBacking.SetSize( targetBackingSize );
818 bool Popup::OnKeyEvent(const KeyEvent& event)
820 bool consumed = false;
822 if(event.state == KeyEvent::Down)
824 if (event.keyCode == Dali::DALI_KEY_ESCAPE || event.keyCode == Dali::DALI_KEY_BACK)
826 SetState(Toolkit::Popup::POPUP_HIDE);
834 Vector3 Popup::GetNaturalSize()
836 float margin = 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
837 const float maxWidth = Stage::GetCurrent().GetSize().width - margin;
839 Vector3 naturalSize( 0.0f, 0.0f, 0.0f );
843 Vector3 titleNaturalSize = mTitle.GetImplementation().GetNaturalSize();
844 // Buffer to avoid errors. The width of the popup could potentially be the width of the title text.
845 // It was observed in this case that text wrapping was then inconsistent when seen on device
846 const float titleBuffer = 0.5f;
847 titleNaturalSize.width += titleBuffer;
849 // As TextView GetNaturalSize does not take wrapping into account, limit the width
850 // to that of the stage
851 if( titleNaturalSize.width >= maxWidth)
853 naturalSize.width = maxWidth;
854 naturalSize.height = mTitle.GetImplementation().GetHeightForWidth( naturalSize.width );
858 naturalSize += titleNaturalSize;
861 naturalSize.height += mPopupStyle->margin;
866 Vector3 contentSize = RelayoutHelper::GetNaturalSize( mContent );
867 // Choose the biggest width
868 naturalSize.width = std::max( naturalSize.width, contentSize.width );
869 naturalSize.height += contentSize.height + mPopupStyle->margin;
872 if( !mButtons.empty() )
874 naturalSize.height += mPopupStyle->bottomSize.height;
878 naturalSize.width += margin;
879 naturalSize.height += margin;
884 float Popup::GetHeightForWidth( float width )
886 float height( 0.0f );
887 float popupWidth( width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
891 height += mTitle.GetImplementation().GetHeightForWidth( popupWidth );
892 height += mPopupStyle->margin;
897 height += RelayoutHelper::GetHeightForWidth( mContent, popupWidth ) + mPopupStyle->margin;
900 if( !mButtons.empty() )
902 height += mPopupStyle->bottomSize.height;
906 float margin( 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
912 float Popup::GetWidthForHeight( float height )
914 return GetNaturalSize().width;
917 Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
919 Actor nextFocusableActor( currentFocusedActor );
921 // TODO: Needs to be optimised
923 if ( !currentFocusedActor || ( currentFocusedActor && KeyboardFocusManager::Get().GetFocusGroup(currentFocusedActor) != Self() ) )
925 // The current focused actor is not within popup
926 if( mContent && mContent.IsKeyboardFocusable() )
928 // If content is focusable, move the focus to content
929 nextFocusableActor = mContent;
931 else if( !mButtons.empty() )
933 // Otherwise, movethe focus to the first button
934 nextFocusableActor = mButtons[0];
939 // Rebuild the focus chain because button or content can be added or removed dynamically
940 ActorContainer focusableActors;
941 if( mContent && mContent.IsKeyboardFocusable() )
943 focusableActors.push_back(mContent);
946 for(unsigned int i = 0; i < mButtons.size(); i++)
948 if( mButtons[i] && mButtons[i].IsKeyboardFocusable() )
950 focusableActors.push_back(mButtons[i]);
954 for ( ActorContainer::iterator iter = focusableActors.begin(), end = focusableActors.end(); iter != end; ++iter )
956 if ( currentFocusedActor == *iter )
960 case Toolkit::Control::Left:
962 if ( iter == focusableActors.begin() )
964 nextFocusableActor = *( focusableActors.end() - 1 );
968 nextFocusableActor = *( iter - 1 );
972 case Toolkit::Control::Right:
974 if ( iter == focusableActors.end() - 1 )
976 nextFocusableActor = *( focusableActors.begin() );
980 nextFocusableActor = *( iter + 1 );
985 case Toolkit::Control::Up:
987 if ( *iter == mContent )
989 nextFocusableActor = *( focusableActors.end() - 1 );
993 if ( mContent && mContent.IsKeyboardFocusable() )
995 nextFocusableActor = mContent;
999 if ( iter == focusableActors.begin() )
1001 nextFocusableActor = *( focusableActors.end() - 1 );
1005 nextFocusableActor = *( iter - 1 );
1012 case Toolkit::Control::Down:
1014 if ( mContent && mContent.IsKeyboardFocusable() )
1016 nextFocusableActor = mContent;
1020 if ( iter == focusableActors.end() - 1 )
1022 nextFocusableActor = *( focusableActors.begin() );
1026 nextFocusableActor = *( iter + 1 );
1030 if ( *iter == mContent && !mButtons.empty() )
1032 nextFocusableActor = mButtons[0];
1038 if(!nextFocusableActor)
1040 DALI_LOG_WARNING("Can not decide next focusable actor\n");
1048 return nextFocusableActor;
1051 } // namespace Internal
1053 } // namespace Toolkit