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>
43 const float CONTENT_DEPTH = 1.0f; ///< 3D Effect of buttons/title etc. appearing off the popup.
44 const float POPUP_ANIMATION_DURATION = 0.5f; ///< Duration of hide/show animations
45 const float BACKING_DEPTH = -1.0f; ///< Depth of backing (positioned just behind dialog, so dialog catches hit events first)
47 const float POPUP_WIDTH = 720.0f; ///< Width of Popup
48 const float POPUP_OUT_MARGIN_WIDTH = 16.f; ///< Space between the screen edge and the popup edge in the horizontal dimension.
49 const float POPUP_OUT_MARGIN_HEIGHT = 36.f; ///< Space between the screen edge and the popup edge in the vertical dimension.
50 const float POPUP_TITLE_WIDTH = 648.0f; ///<Width of Popup Title
51 const float POPUP_BUTTON_BG_HEIGHT = 96.f; ///< Height of Button Background.
52 const Vector3 DEFAULT_DIALOG_SIZE = Vector3(POPUP_TITLE_WIDTH/POPUP_WIDTH, 0.5f, 0.0f);
53 const Vector3 DEFAULT_BOTTOM_SIZE = Vector3(1.0f, 0.2f, 0.0f);
55 const char* const PROPERTY_TITLE = "title";
56 const char* const PROPERTY_STATE = "state";
58 // Constraints ///////////////////////////////////////////////////////////////////////////
61 * BackgroundSizeConstraint
63 * The background size should be at least as big as the Dialog.
64 * In some cases a background may have graphics which are visible
65 * outside of the Dialog, e.g. A Shadow. For this we need to alter
66 * the size of Background.
68 struct BackgroundSizeConstraint
71 * Constraint that sets size to parent's size plus a border.
73 * @param[in] outerBorder The border to extend beyond parent's Size.
75 BackgroundSizeConstraint( Vector4 outerBorder )
76 : mOuterBorder( outerBorder )
81 * (render thread code)
82 * @param[in] current The current size.
83 * @param[in] parentSizeProperty The parent's size
85 Vector3 operator()( const Vector3& current,
86 const PropertyInput& parentSizeProperty )
88 Vector3 size = parentSizeProperty.GetVector3();
90 size.width += mOuterBorder.x + mOuterBorder.y;
91 size.height += mOuterBorder.z + mOuterBorder.w;
96 const Vector4 mOuterBorder; ///< The size of the outer-border (Set to 0.0, 0.0f, 0.0f, 0.0f if doesn't exist).
99 struct ButtonAreaSizeConstraint
102 * Constraint that sets size to parent's size plus a border.
104 * @param[in] outerBorder The border to extend beyond parent's Size.
106 ButtonAreaSizeConstraint( Vector4 outerBorder )
107 : mOuterBorder( outerBorder )
112 * (render thread code)
113 * @param[in] current The current size.
114 * @param[in] parentSizeProperty The parent's size
116 Vector3 operator()( const Vector3& current,
117 const PropertyInput& parentSizeProperty )
119 Vector3 size = parentSizeProperty.GetVector3();
121 size.width += mOuterBorder.x + mOuterBorder.y;
122 size.width -= (POPUP_OUT_MARGIN_WIDTH + POPUP_OUT_MARGIN_WIDTH);
123 size.height = POPUP_BUTTON_BG_HEIGHT;
128 const Vector4 mOuterBorder; ///< The size of the outer-border (Set to 0.0, 0.0f, 0.0f, 0.0f if doesn't exist).
131 } // unnamed namespace
147 return Toolkit::Popup::New();
150 TypeRegistration typeRegistration( typeid(Toolkit::Popup), typeid(Toolkit::Control), Create );
152 SignalConnectorType signalConnector1( typeRegistration, Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE, &Popup::DoConnectSignal );
153 SignalConnectorType signalConnector2( typeRegistration, Toolkit::Popup::SIGNAL_HIDDEN, &Popup::DoConnectSignal );
159 ///////////////////////////////////////////////////////////////////////////////////////////////////
161 ///////////////////////////////////////////////////////////////////////////////////////////////////
163 Dali::Toolkit::Popup Popup::New()
165 PopupStylePtr style = PopupStyleDefault::New();
167 // Create the implementation
168 PopupPtr popup(new Popup(*style));
170 // Pass ownership to CustomActor via derived handle
171 Dali::Toolkit::Popup handle(*popup);
173 // Second-phase init of the implementation
174 // This can only be done after the CustomActor connection has been made...
180 Popup::Popup(PopupStyle& style)
181 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
183 mState(Toolkit::Popup::POPUP_NONE), // Initially, the popup state should not be set, it's set in OnInitialize
184 mAlterAddedChild(false),
185 mPopupStyle(PopupStylePtr(&style)),
186 mPropertyTitle(Property::INVALID_INDEX),
187 mPropertyState(Property::INVALID_INDEX)
189 SetKeyboardNavigationSupport( true );
192 void Popup::OnInitialize()
195 self.SetSensitive(false);
198 mLayer = Layer::New();
199 mLayer.SetParentOrigin(ParentOrigin::CENTER);
200 mLayer.SetAnchorPoint(AnchorPoint::CENTER);
202 mLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
205 mPopupBg = Actor::New();
206 mPopupBg.SetParentOrigin(ParentOrigin::CENTER);
207 mPopupBg.SetAnchorPoint(AnchorPoint::CENTER);
208 mPopupBg.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
209 mLayer.Add(mPopupBg);
211 // Any content after this point which is added to Self() will be reparented to
213 mAlterAddedChild = true;
215 // Add Backing (Dim effect)
218 // Add Dialog ( background image, title, content container, button container and tail )
222 ShowTail(ParentOrigin::BOTTOM_CENTER);
224 // Hide content by default.
225 SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
227 mPropertyTitle = self.RegisterProperty( PROPERTY_TITLE, "", Property::READ_WRITE );
228 mPropertyState = self.RegisterProperty( PROPERTY_STATE, "POPUP_HIDE", Property::READ_WRITE );
230 // Make self as keyboard focusable and focus group
231 self.SetKeyboardFocusable(true);
232 SetAsKeyboardFocusGroup(true);
235 void Popup::OnPropertySet( Property::Index index, Property::Value propertyValue )
237 if( index == mPropertyTitle )
239 SetTitle(propertyValue.Get<std::string>());
241 else if ( index == mPropertyState )
243 std::string value( propertyValue.Get<std::string>() );
244 if(value == "POPUP_SHOW")
246 SetState( Toolkit::Popup::POPUP_SHOW, 0.0f );
248 else if( value == "POPUP_HIDE")
250 SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
259 size_t Popup::GetButtonCount() const
261 return mButtons.size();
264 void Popup::SetBackgroundImage( Actor image )
266 // Removes any previous background.
267 if( mBackgroundImage && mPopupBg )
269 mPopupBg.Remove( mBackgroundImage );
272 // Adds new background to the dialog.
273 mBackgroundImage = image;
275 // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
276 mBackgroundImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
278 mPopupBg.Add( mBackgroundImage );
281 void Popup::SetButtonAreaImage( Actor image )
283 // Removes any previous area image.
284 if( mButtonAreaImage && mPopupBg )
286 mPopupBg.Remove( mButtonAreaImage );
289 // Adds new area image to the dialog.
290 mButtonAreaImage = image;
292 // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
293 mButtonAreaImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
295 mPopupBg.Add( mButtonAreaImage );
298 void Popup::SetTitle( const std::string& text )
300 Toolkit::TextView titleActor = Toolkit::TextView::New();
301 titleActor.SetText( text );
302 titleActor.SetColor( Color::BLACK );
303 titleActor.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
304 titleActor.SetWidthExceedPolicy( Toolkit::TextView::Split );
305 titleActor.SetLineJustification( Toolkit::TextView::Center );
307 SetTitle( titleActor );
310 void Popup::SetTitle( Toolkit::TextView titleActor )
312 // Replaces the current title actor.
313 if( mTitle && mPopupBg )
315 mPopupBg.Remove( mTitle );
319 mPopupBg.Add( mTitle );
324 Toolkit::TextView Popup::GetTitle() const
329 void Popup::AddButton( Toolkit::Button button )
331 mButtons.push_back( button );
332 mBottomBg.Add( button );
337 void Popup::SetState( Toolkit::Popup::PopupState state )
339 SetState( state, POPUP_ANIMATION_DURATION );
342 void Popup::SetState( Toolkit::Popup::PopupState state, float duration )
344 // default animation behaviour.
345 HandleStateChange(state, duration);
348 Toolkit::Popup::PopupState Popup::GetState() const
353 void Popup::ShowTail(const Vector3& position)
355 // Replaces the tail actor.
356 if(mTailImage && mTailImage.GetParent())
358 mTailImage.GetParent().Remove( mTailImage );
362 std::string image = "";
364 // depending on position of tail around ParentOrigin, a different tail image is used...
365 if(position.y < Math::MACHINE_EPSILON_1)
367 image = mPopupStyle->tailUpImage;
369 else if(position.y > 1.0f - Math::MACHINE_EPSILON_1)
371 image = mPopupStyle->tailDownImage;
373 else if(position.x < Math::MACHINE_EPSILON_1)
375 image = mPopupStyle->tailLeftImage;
377 else if(position.x > 1.0f - Math::MACHINE_EPSILON_1)
379 image = mPopupStyle->tailRightImage;
384 Image tail = Image::New( image );
385 mTailImage = ImageActor::New(tail);
386 const Vector3 anchorPoint = AnchorPoint::FRONT_BOTTOM_RIGHT - position;
388 mTailImage.SetParentOrigin(position);
389 mTailImage.SetAnchorPoint(anchorPoint);
391 mBottomBg.Add(mTailImage);
395 void Popup::HideTail()
397 ShowTail(ParentOrigin::CENTER);
400 void Popup::SetStyle(PopupStyle& style)
402 mPopupStyle = PopupStylePtr(&style);
406 PopupStylePtr Popup::GetStyle() const
411 void Popup::SetDefaultBackgroundImage()
413 Image bg = Image::New( mPopupStyle->backgroundImage );
414 ImageActor bgImage = ImageActor::New( bg );
415 bgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
416 bgImage.SetNinePatchBorder( mPopupStyle->backgroundScale9Border );
418 Image buttonBg = Image::New( mPopupStyle->buttonAreaImage );
419 ImageActor buttonBgImage = ImageActor::New( buttonBg );
420 buttonBgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
421 buttonBgImage.SetNinePatchBorder( mPopupStyle->buttonArea9PatchBorder );
423 SetBackgroundImage( bgImage );
424 SetButtonAreaImage( buttonBgImage );
427 void Popup::CreateBacking()
429 mBacking = Dali::Toolkit::CreateSolidColorActor( mPopupStyle->backingColor );
431 mBacking.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
432 mBacking.SetSensitive(true);
434 mLayer.Add(mBacking);
435 mBacking.SetOpacity(0.0f);
436 mBacking.SetPosition(0.0f, 0.0f, BACKING_DEPTH);
437 mBacking.TouchedSignal().Connect( this, &Popup::OnBackingTouched );
438 mBacking.MouseWheelEventSignal().Connect(this, &Popup::OnBackingMouseWheelEvent);
441 void Popup::CreateDialog()
443 // Adds default background image.
444 SetDefaultBackgroundImage();
446 // Adds bottom background
447 mBottomBg = Actor::New();
448 mPopupBg.Add( mBottomBg );
451 void Popup::HandleStateChange( Toolkit::Popup::PopupState state, float duration )
453 const Vector2& stageSize( Stage::GetCurrent().GetSize() );
456 float targetBackingAlpha;
457 Vector3 targetBackingSize;
466 case Toolkit::Popup::POPUP_HIDE:
468 targetSize = Vector3(0.0f, 0.0f, 1.0f);
469 targetBackingAlpha = 0.0f;
470 targetBackingSize = Vector3(0.0f, 0.0f, 1.0f);
472 ClearKeyInputFocus();
474 // Retore the keyboard focus when popup is hidden
475 if(mPreviousFocusedActor && mPreviousFocusedActor.IsKeyboardFocusable() )
477 Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
478 if( keyboardFocusManager )
480 keyboardFocusManager.SetCurrentFocusActor(mPreviousFocusedActor);
487 case Toolkit::Popup::POPUP_SHOW:
490 targetSize = Vector3(1.0f, 1.0f, 1.0f);
491 targetBackingAlpha = 1.0f;
492 float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
493 targetBackingSize = Vector3( length, length, 1.0f );
496 // Add contents to stage for showing.
497 if( !mLayer.GetParent() )
499 mAlterAddedChild = false;
501 mAlterAddedChild = true;
503 Self().SetSensitive(true);
506 // Handle the keyboard focus when popup is shown
507 Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
508 if( keyboardFocusManager )
510 mPreviousFocusedActor = keyboardFocusManager.GetCurrentFocusActor();
512 if( mContent && mContent.IsKeyboardFocusable() )
514 // If content is focusable, move the focus to content
515 keyboardFocusManager.SetCurrentFocusActor(mContent);
517 else if( !mButtons.empty() )
519 // Otherwise, movethe focus to the first button
520 keyboardFocusManager.SetCurrentFocusActor(mButtons[0]);
524 DALI_LOG_WARNING("There is no focusable in popup\n");
531 mBacking.SetSize( targetBackingSize );
533 if(duration > Math::MACHINE_EPSILON_1)
541 mAnimation = Animation::New(duration);
545 mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
546 mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(duration * 0.5f, duration * 0.5f) );
550 mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
551 mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
554 mAnimation.FinishedSignal().Connect(this, &Popup::OnStateAnimationFinished);
558 mBacking.SetOpacity( targetBackingAlpha );
559 mPopupBg.SetScale( targetSize );
561 HandleStateChangeComplete();
565 void Popup::HandleStateChangeComplete()
567 // Remove contents from stage if completely hidden.
568 if( (mState == Toolkit::Popup::POPUP_HIDE) && (mLayer.GetParent()) )
570 Self().Remove(mLayer);
571 Self().SetSensitive( false );
573 // Guard against destruction during signal emission
574 Toolkit::Popup handle( GetOwner() );
575 mHiddenSignal.Emit();
579 Toolkit::Popup::TouchedOutsideSignalType& Popup::OutsideTouchedSignal()
581 return mTouchedOutsideSignal;
584 Toolkit::Popup::HiddenSignalType& Popup::HiddenSignal()
586 return mHiddenSignal;
589 bool Popup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
591 Dali::BaseHandle handle( object );
593 bool connected( true );
594 Toolkit::Popup popup = Toolkit::Popup::DownCast(handle);
596 if( Dali::Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE == signalName )
598 popup.OutsideTouchedSignal().Connect( tracker, functor );
600 else if( Dali::Toolkit::Popup::SIGNAL_HIDDEN == signalName )
602 popup.HiddenSignal().Connect( tracker, functor );
606 // signalName does not match any signal
613 void Popup::OnStateAnimationFinished( Animation& source )
615 HandleStateChangeComplete();
618 bool Popup::OnBackingTouched(Actor actor, const TouchEvent& event)
620 if(event.GetPointCount()>0)
622 const TouchPoint& point = event.GetPoint(0);
624 if(point.state == TouchPoint::Down)
626 // Guard against destruction during signal emission
627 Toolkit::Popup handle( GetOwner() );
629 mTouchedOutsideSignal.Emit();
636 bool Popup::OnBackingMouseWheelEvent(Actor actor, const MouseWheelEvent& event)
638 // consume mouse wheel event in dimmed backing actor
642 bool Popup::OnDialogTouched(Actor actor, const TouchEvent& event)
644 // consume event (stops backing actor receiving touch events)
648 void Popup::OnControlChildAdd( Actor& child )
650 // reparent any children added by user to the body layer.
651 if( mAlterAddedChild )
653 // Removes previously added content.
656 mPopupBg.Remove( mContent );
659 // Reparent new content.
660 Self().Remove( child );
662 // keep a handle to the new content.
665 mPopupBg.Add( mContent );
669 void Popup::OnRelayout( const Vector2& size, ActorSizeContainer& container )
671 // Set the popup size
673 popupSize.width = size.width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
674 popupSize.height = size.height - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
676 // Update sizes of all popup's components.
678 // Relayout background image.
679 // Adjust background position and size relative to parent to cater to outer Border.
680 // Some backgrounds are intended to over-spill. That is some content
681 // should appear outside the Dialog on all sides i.e. Shadows, glow effects.
682 const Vector4 outerBorder = mPopupStyle->backgroundOuterBorder;
684 if( mBackgroundImage )
686 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
687 ParentSource( Actor::SIZE ),
688 BackgroundSizeConstraint(outerBorder) );
690 mBackgroundImage.RemoveConstraints();
691 mBackgroundImage.ApplyConstraint( constraint );
693 mBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
694 mBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
695 mBackgroundImage.SetPosition( -outerBorder.x, -outerBorder.y, 0.0f );
698 if( mPopupBg && mButtonAreaImage )
700 // If there are no buttons, button background is also removed.
701 if ( mButtons.size() == 0 )
703 mPopupBg.Remove( mButtonAreaImage );
707 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
708 ParentSource( Actor::SIZE ),
709 ButtonAreaSizeConstraint(outerBorder) );
711 mButtonAreaImage.RemoveConstraints();
712 mButtonAreaImage.ApplyConstraint( constraint );
714 mButtonAreaImage.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
715 mButtonAreaImage.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
716 mButtonAreaImage.SetY( -outerBorder.z - POPUP_OUT_MARGIN_HEIGHT );
718 mPopupBg.Add( mButtonAreaImage );
723 Vector3 positionOffset( 0.0f, mPopupStyle->margin + POPUP_OUT_MARGIN_WIDTH, CONTENT_DEPTH );
727 titleSize.width = popupSize.width;
728 titleSize.height = mTitle.GetHeightForWidth( titleSize.width );
730 // As the default size policy for text-view is Fixed & Fixed, a size needs to be set.
731 // Otherwise size-negotiation algorithm uses the GetNaturalSize() with doesn't take
732 // into account the multiline and exceed policies, giving as result a wrong size.
733 mTitle.SetSize( titleSize );
734 Relayout( mTitle, titleSize, container );
736 mTitle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
737 mTitle.SetParentOrigin( ParentOrigin::TOP_CENTER );
738 mTitle.SetPosition( positionOffset );
740 positionOffset.y += titleSize.height + mPopupStyle->margin;
746 // If the content width is greater than popup width then scale it down/wrap text as needed
747 Vector2 contentSize( RelayoutHelper::GetNaturalSize( mContent ) );
748 if( contentSize.width > popupSize.width )
750 contentSize.width = popupSize.width;
751 contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, contentSize.width );
754 mContent.SetSize( contentSize );
755 Relayout( mContent, contentSize, container );
757 mContent.SetParentOrigin(ParentOrigin::TOP_CENTER);
758 mContent.SetAnchorPoint(AnchorPoint::TOP_CENTER);
760 mContent.SetPosition( positionOffset );
762 positionOffset.y += contentSize.height + mPopupStyle->margin;
765 // Relayout Button Area
768 mBottomBg.SetSize( popupSize.width, mPopupStyle->bottomSize.height );
770 mBottomBg.SetParentOrigin(ParentOrigin::TOP_CENTER);
771 mBottomBg.SetAnchorPoint(AnchorPoint::TOP_CENTER);
773 mBottomBg.SetPosition( positionOffset );
776 // Relayout All buttons
777 if ( !mButtons.empty() )
779 // All buttons should be the same size and fill the button area. The button spacing needs to be accounted for as well.
780 Vector2 buttonSize( ( ( popupSize.width - mPopupStyle->buttonSpacing * ( mButtons.size() - 1 ) ) / mButtons.size() ),
781 mPopupStyle->bottomSize.height - mPopupStyle->margin );
783 Vector3 buttonPosition;
785 for ( ActorIter iter = mButtons.begin(), endIter = mButtons.end();
787 ++iter, buttonPosition.x += mPopupStyle->buttonSpacing + buttonSize.width )
789 iter->SetPosition( buttonPosition );
791 // If there is only one button, it needs to be laid out on center.
792 if ( mButtons.size() == 1 )
794 iter->SetAnchorPoint( AnchorPoint::CENTER );
795 iter->SetParentOrigin( ParentOrigin::CENTER );
799 iter->SetAnchorPoint( AnchorPoint::CENTER_LEFT );
800 iter->SetParentOrigin( ParentOrigin::CENTER_LEFT );
803 Relayout( *iter, buttonSize, container );
807 if( mShowing && mBacking )
809 Vector2 stageSize = Stage::GetCurrent().GetSize();
810 float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
811 Vector3 targetBackingSize = Vector3( length, length, 1.0f );
813 mBacking.SetSize( targetBackingSize );
817 bool Popup::OnKeyEvent(const KeyEvent& event)
819 bool consumed = false;
821 if(event.state == KeyEvent::Down)
823 if (event.keyCode == Dali::DALI_KEY_ESCAPE || event.keyCode == Dali::DALI_KEY_BACK)
825 SetState(Toolkit::Popup::POPUP_HIDE);
833 Vector3 Popup::GetNaturalSize()
835 float margin = 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
836 const float maxWidth = Stage::GetCurrent().GetSize().width - margin;
838 Vector3 naturalSize( 0.0f, 0.0f, 0.0f );
842 Vector3 titleNaturalSize = mTitle.GetImplementation().GetNaturalSize();
843 // Buffer to avoid errors. The width of the popup could potentially be the width of the title text.
844 // It was observed in this case that text wrapping was then inconsistent when seen on device
845 const float titleBuffer = 0.5f;
846 titleNaturalSize.width += titleBuffer;
848 // As TextView GetNaturalSize does not take wrapping into account, limit the width
849 // to that of the stage
850 if( titleNaturalSize.width >= maxWidth)
852 naturalSize.width = maxWidth;
853 naturalSize.height = mTitle.GetImplementation().GetHeightForWidth( naturalSize.width );
857 naturalSize += titleNaturalSize;
860 naturalSize.height += mPopupStyle->margin;
865 Vector3 contentSize = RelayoutHelper::GetNaturalSize( mContent );
866 // Choose the biggest width
867 naturalSize.width = std::max( naturalSize.width, contentSize.width );
868 naturalSize.height += contentSize.height + mPopupStyle->margin;
871 if( !mButtons.empty() )
873 naturalSize.height += mPopupStyle->bottomSize.height;
877 naturalSize.width += margin;
878 naturalSize.height += margin;
883 float Popup::GetHeightForWidth( float width )
885 float height( 0.0f );
886 float popupWidth( width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
890 height += mTitle.GetImplementation().GetHeightForWidth( popupWidth );
891 height += mPopupStyle->margin;
896 height += RelayoutHelper::GetHeightForWidth( mContent, popupWidth ) + mPopupStyle->margin;
899 if( !mButtons.empty() )
901 height += mPopupStyle->bottomSize.height;
905 float margin( 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
911 float Popup::GetWidthForHeight( float height )
913 return GetNaturalSize().width;
916 Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
918 Actor nextFocusableActor( currentFocusedActor );
920 // TODO: Needs to be optimised
922 if ( !currentFocusedActor || ( currentFocusedActor && KeyboardFocusManager::Get().GetFocusGroup(currentFocusedActor) != Self() ) )
924 // The current focused actor is not within popup
925 if( mContent && mContent.IsKeyboardFocusable() )
927 // If content is focusable, move the focus to content
928 nextFocusableActor = mContent;
930 else if( !mButtons.empty() )
932 // Otherwise, movethe focus to the first button
933 nextFocusableActor = mButtons[0];
938 // Rebuild the focus chain because button or content can be added or removed dynamically
939 ActorContainer focusableActors;
940 if( mContent && mContent.IsKeyboardFocusable() )
942 focusableActors.push_back(mContent);
945 for(unsigned int i = 0; i < mButtons.size(); i++)
947 if( mButtons[i] && mButtons[i].IsKeyboardFocusable() )
949 focusableActors.push_back(mButtons[i]);
953 for ( ActorContainer::iterator iter = focusableActors.begin(), end = focusableActors.end(); iter != end; ++iter )
955 if ( currentFocusedActor == *iter )
959 case Toolkit::Control::Left:
961 if ( iter == focusableActors.begin() )
963 nextFocusableActor = *( focusableActors.end() - 1 );
967 nextFocusableActor = *( iter - 1 );
971 case Toolkit::Control::Right:
973 if ( iter == focusableActors.end() - 1 )
975 nextFocusableActor = *( focusableActors.begin() );
979 nextFocusableActor = *( iter + 1 );
984 case Toolkit::Control::Up:
986 if ( *iter == mContent )
988 nextFocusableActor = *( focusableActors.end() - 1 );
992 if ( mContent && mContent.IsKeyboardFocusable() )
994 nextFocusableActor = mContent;
998 if ( iter == focusableActors.begin() )
1000 nextFocusableActor = *( focusableActors.end() - 1 );
1004 nextFocusableActor = *( iter - 1 );
1011 case Toolkit::Control::Down:
1013 if ( mContent && mContent.IsKeyboardFocusable() )
1015 nextFocusableActor = mContent;
1019 if ( iter == focusableActors.end() - 1 )
1021 nextFocusableActor = *( focusableActors.begin() );
1025 nextFocusableActor = *( iter + 1 );
1029 if ( *iter == mContent && !mButtons.empty() )
1031 nextFocusableActor = mButtons[0];
1037 if(!nextFocusableActor)
1039 DALI_LOG_WARNING("Can not decide next focusable actor\n");
1047 return nextFocusableActor;
1050 } // namespace Internal
1052 } // namespace Toolkit