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.
18 #include <dali-toolkit/internal/controls/popup/popup-impl.h>
20 #include <dali-toolkit/public-api/controls/buttons/button.h>
21 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
22 #include <dali-toolkit/public-api/controls/control-impl.h>
24 #include <dali-toolkit/internal/controls/relayout-helper.h>
25 #include <dali-toolkit/internal/focus-manager/keyboard-focus-manager-impl.h>
27 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
28 #include <dali/integration-api/debug.h>
35 const float CONTENT_DEPTH = 1.0f; ///< 3D Effect of buttons/title etc. appearing off the popup.
36 const float POPUP_ANIMATION_DURATION = 0.5f; ///< Duration of hide/show animations
37 const float BACKING_DEPTH = -1.0f; ///< Depth of backing (positioned just behind dialog, so dialog catches hit events first)
39 const float POPUP_WIDTH = 720.0f; ///< Width of Popup
40 const float POPUP_OUT_MARGIN_WIDTH = 16.f; ///< Space between the screen edge and the popup edge in the horizontal dimension.
41 const float POPUP_OUT_MARGIN_HEIGHT = 36.f; ///< Space between the screen edge and the popup edge in the vertical dimension.
42 const float POPUP_TITLE_WIDTH = 648.0f; ///<Width of Popup Title
43 const float POPUP_BUTTON_BG_HEIGHT = 96.f; ///< Height of Button Background.
44 const Vector3 DEFAULT_DIALOG_SIZE = Vector3(POPUP_TITLE_WIDTH/POPUP_WIDTH, 0.5f, 0.0f);
45 const Vector3 DEFAULT_BOTTOM_SIZE = Vector3(1.0f, 0.2f, 0.0f);
47 const char* const PROPERTY_TITLE = "title";
48 const char* const PROPERTY_STATE = "state";
50 // Constraints ///////////////////////////////////////////////////////////////////////////
53 * BackgroundSizeConstraint
55 * The background size should be at least as big as the Dialog.
56 * In some cases a background may have graphics which are visible
57 * outside of the Dialog, e.g. A Shadow. For this we need to alter
58 * the size of Background.
60 struct BackgroundSizeConstraint
63 * Constraint that sets size to parent's size plus a border.
65 * @param[in] outerBorder The border to extend beyond parent's Size.
67 BackgroundSizeConstraint( Vector4 outerBorder )
68 : mOuterBorder( outerBorder )
73 * (render thread code)
74 * @param[in] current The current size.
75 * @param[in] parentSizeProperty The parent's size
77 Vector3 operator()( const Vector3& current,
78 const PropertyInput& parentSizeProperty )
80 Vector3 size = parentSizeProperty.GetVector3();
82 size.width += mOuterBorder.x + mOuterBorder.y;
83 size.height += mOuterBorder.z + mOuterBorder.w;
88 const Vector4 mOuterBorder; ///< The size of the outer-border (Set to 0.0, 0.0f, 0.0f, 0.0f if doesn't exist).
91 struct ButtonAreaSizeConstraint
94 * Constraint that sets size to parent's size plus a border.
96 * @param[in] outerBorder The border to extend beyond parent's Size.
98 ButtonAreaSizeConstraint( Vector4 outerBorder )
99 : mOuterBorder( outerBorder )
104 * (render thread code)
105 * @param[in] current The current size.
106 * @param[in] parentSizeProperty The parent's size
108 Vector3 operator()( const Vector3& current,
109 const PropertyInput& parentSizeProperty )
111 Vector3 size = parentSizeProperty.GetVector3();
113 size.width += mOuterBorder.x + mOuterBorder.y;
114 size.width -= (POPUP_OUT_MARGIN_WIDTH + POPUP_OUT_MARGIN_WIDTH);
115 size.height = POPUP_BUTTON_BG_HEIGHT;
120 const Vector4 mOuterBorder; ///< The size of the outer-border (Set to 0.0, 0.0f, 0.0f, 0.0f if doesn't exist).
123 } // unnamed namespace
139 return Toolkit::Popup::New();
142 TypeRegistration typeRegistration( typeid(Toolkit::Popup), typeid(Toolkit::Control), Create );
144 SignalConnectorType signalConnector1( typeRegistration, Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE, &Popup::DoConnectSignal );
145 SignalConnectorType signalConnector2( typeRegistration, Toolkit::Popup::SIGNAL_HIDDEN, &Popup::DoConnectSignal );
151 ///////////////////////////////////////////////////////////////////////////////////////////////////
153 ///////////////////////////////////////////////////////////////////////////////////////////////////
155 Dali::Toolkit::Popup Popup::New()
157 PopupStylePtr style = PopupStyleDefault::New();
159 // Create the implementation
160 PopupPtr popup(new Popup(*style));
162 // Pass ownership to CustomActor via derived handle
163 Dali::Toolkit::Popup handle(*popup);
165 // Second-phase init of the implementation
166 // This can only be done after the CustomActor connection has been made...
172 Popup::Popup(PopupStyle& style)
175 mState(Toolkit::Popup::POPUP_NONE), // Initially, the popup state should not be set, it's set in OnInitialize
176 mAlterAddedChild(false),
177 mPopupStyle(PopupStylePtr(&style))
179 SetKeyboardNavigationSupport( true );
182 void Popup::OnInitialize()
185 self.SetSensitive(false);
188 mLayer = Layer::New();
189 mLayer.SetParentOrigin(ParentOrigin::CENTER);
190 mLayer.SetAnchorPoint(AnchorPoint::CENTER);
192 mLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
195 mPopupBg = Actor::New();
196 mPopupBg.SetParentOrigin(ParentOrigin::CENTER);
197 mPopupBg.SetAnchorPoint(AnchorPoint::CENTER);
198 mPopupBg.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
199 mLayer.Add(mPopupBg);
201 // Any content after this point which is added to Self() will be reparented to
203 mAlterAddedChild = true;
205 // Add Backing (Dim effect)
208 // Add Dialog ( background image, title, content container, button container and tail )
212 ShowTail(ParentOrigin::BOTTOM_CENTER);
214 // Hide content by default.
215 SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
217 mPropertyTitle = self.RegisterProperty( PROPERTY_TITLE, "", Property::READ_WRITE );
218 mPropertyState = self.RegisterProperty( PROPERTY_STATE, "POPUP_HIDE", Property::READ_WRITE );
220 // Make self as keyboard focusable and focus group
221 self.SetKeyboardFocusable(true);
222 SetAsKeyboardFocusGroup(true);
225 void Popup::OnPropertySet( Property::Index index, Property::Value propertyValue )
227 if( index == mPropertyTitle )
229 SetTitle(propertyValue.Get<std::string>());
231 else if ( index == mPropertyState )
233 std::string value( propertyValue.Get<std::string>() );
234 if(value == "POPUP_SHOW")
236 SetState( Toolkit::Popup::POPUP_SHOW, 0.0f );
238 else if( value == "POPUP_HIDE")
240 SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
249 size_t Popup::GetButtonCount() const
251 return mButtons.size();
254 void Popup::SetBackgroundImage( Actor image )
256 // Removes any previous background.
257 if( mBackgroundImage && mPopupBg )
259 mPopupBg.Remove( mBackgroundImage );
262 // Adds new background to the dialog.
263 mBackgroundImage = image;
265 // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
266 mBackgroundImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
268 mPopupBg.Add( mBackgroundImage );
271 void Popup::SetButtonAreaImage( Actor image )
273 // Removes any previous area image.
274 if( mButtonAreaImage && mPopupBg )
276 mPopupBg.Remove( mButtonAreaImage );
279 // Adds new area image to the dialog.
280 mButtonAreaImage = image;
282 // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
283 mButtonAreaImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
285 mPopupBg.Add( mButtonAreaImage );
288 void Popup::SetTitle( const std::string& text )
290 Toolkit::TextView titleActor = Toolkit::TextView::New();
291 titleActor.SetText( text );
292 titleActor.SetColor( Color::BLACK );
293 titleActor.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
294 titleActor.SetWidthExceedPolicy( Toolkit::TextView::Split );
295 titleActor.SetLineJustification( Toolkit::TextView::Center );
297 SetTitle( titleActor );
300 void Popup::SetTitle( Toolkit::TextView titleActor )
302 // Replaces the current title actor.
303 if( mTitle && mPopupBg )
305 mPopupBg.Remove( mTitle );
309 mPopupBg.Add( mTitle );
314 Toolkit::TextView Popup::GetTitle() const
319 void Popup::AddButton( Toolkit::Button button )
321 mButtons.push_back( button );
322 mBottomBg.Add( button );
327 void Popup::SetState( Toolkit::Popup::PopupState state )
329 SetState( state, POPUP_ANIMATION_DURATION );
332 void Popup::SetState( Toolkit::Popup::PopupState state, float duration )
334 // default animation behaviour.
335 HandleStateChange(state, duration);
338 Toolkit::Popup::PopupState Popup::GetState() const
343 void Popup::ShowTail(const Vector3& position)
345 // Replaces the tail actor.
346 if(mTailImage && mTailImage.GetParent())
348 mTailImage.GetParent().Remove( mTailImage );
352 std::string image = "";
354 // depending on position of tail around ParentOrigin, a different tail image is used...
355 if(position.y < Math::MACHINE_EPSILON_1)
357 image = mPopupStyle->tailUpImage;
359 else if(position.y > 1.0f - Math::MACHINE_EPSILON_1)
361 image = mPopupStyle->tailDownImage;
363 else if(position.x < Math::MACHINE_EPSILON_1)
365 image = mPopupStyle->tailLeftImage;
367 else if(position.x > 1.0f - Math::MACHINE_EPSILON_1)
369 image = mPopupStyle->tailRightImage;
374 Image tail = Image::New( image );
375 mTailImage = ImageActor::New(tail);
376 const Vector3 anchorPoint = AnchorPoint::FRONT_BOTTOM_RIGHT - position;
378 mTailImage.SetParentOrigin(position);
379 mTailImage.SetAnchorPoint(anchorPoint);
381 mBottomBg.Add(mTailImage);
385 void Popup::HideTail()
387 ShowTail(ParentOrigin::CENTER);
390 void Popup::SetStyle(PopupStyle& style)
392 mPopupStyle = PopupStylePtr(&style);
396 PopupStylePtr Popup::GetStyle() const
401 void Popup::SetDefaultBackgroundImage()
403 Image bg = Image::New( mPopupStyle->backgroundImage );
404 ImageActor bgImage = ImageActor::New( bg );
405 bgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
406 bgImage.SetNinePatchBorder( mPopupStyle->backgroundScale9Border );
408 Image buttonBg = Image::New( mPopupStyle->buttonAreaImage );
409 ImageActor buttonBgImage = ImageActor::New( buttonBg );
410 buttonBgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
411 buttonBgImage.SetNinePatchBorder( mPopupStyle->buttonArea9PatchBorder );
413 SetBackgroundImage( bgImage );
414 SetButtonAreaImage( buttonBgImage );
417 void Popup::CreateBacking()
419 mBacking = Dali::Toolkit::CreateSolidColorActor( mPopupStyle->backingColor );
421 mBacking.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
422 mBacking.SetSensitive(true);
424 mLayer.Add(mBacking);
425 mBacking.SetOpacity(0.0f);
426 mBacking.SetPosition(0.0f, 0.0f, BACKING_DEPTH);
427 mBacking.TouchedSignal().Connect( this, &Popup::OnBackingTouched );
428 mBacking.MouseWheelEventSignal().Connect(this, &Popup::OnBackingMouseWheelEvent);
431 void Popup::CreateDialog()
433 // Adds default background image.
434 SetDefaultBackgroundImage();
436 // Adds bottom background
437 mBottomBg = Actor::New();
438 mPopupBg.Add( mBottomBg );
441 void Popup::HandleStateChange( Toolkit::Popup::PopupState state, float duration )
443 const Vector2& stageSize( Stage::GetCurrent().GetSize() );
446 float targetBackingAlpha;
447 Vector3 targetBackingSize;
456 case Toolkit::Popup::POPUP_HIDE:
458 targetSize = Vector3(0.0f, 0.0f, 1.0f);
459 targetBackingAlpha = 0.0f;
460 targetBackingSize = Vector3(0.0f, 0.0f, 1.0f);
462 ClearKeyInputFocus();
464 // Retore the keyboard focus when popup is hidden
465 if(mPreviousFocusedActor && mPreviousFocusedActor.IsKeyboardFocusable() )
467 Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
468 if( keyboardFocusManager )
470 keyboardFocusManager.SetCurrentFocusActor(mPreviousFocusedActor);
477 case Toolkit::Popup::POPUP_SHOW:
480 targetSize = Vector3(1.0f, 1.0f, 1.0f);
481 targetBackingAlpha = 1.0f;
482 float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
483 targetBackingSize = Vector3( length, length, 1.0f );
486 // Add contents to stage for showing.
487 if( !mLayer.GetParent() )
489 mAlterAddedChild = false;
491 mAlterAddedChild = true;
493 Self().SetSensitive(true);
496 // Handle the keyboard focus when popup is shown
497 Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
498 if( keyboardFocusManager )
500 mPreviousFocusedActor = keyboardFocusManager.GetCurrentFocusActor();
502 if( mContent && mContent.IsKeyboardFocusable() )
504 // If content is focusable, move the focus to content
505 keyboardFocusManager.SetCurrentFocusActor(mContent);
507 else if( !mButtons.empty() )
509 // Otherwise, movethe focus to the first button
510 keyboardFocusManager.SetCurrentFocusActor(mButtons[0]);
514 DALI_LOG_WARNING("There is no focusable in popup\n");
521 mBacking.SetSize( targetBackingSize );
523 if(duration > Math::MACHINE_EPSILON_1)
531 mAnimation = Animation::New(duration);
535 mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
536 mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(duration * 0.5f, duration * 0.5f) );
540 mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
541 mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
544 mAnimation.FinishedSignal().Connect(this, &Popup::OnStateAnimationFinished);
548 mBacking.SetOpacity( targetBackingAlpha );
549 mPopupBg.SetScale( targetSize );
551 HandleStateChangeComplete();
555 void Popup::HandleStateChangeComplete()
557 // Remove contents from stage if completely hidden.
558 if( (mState == Toolkit::Popup::POPUP_HIDE) && (mLayer.GetParent()) )
560 Self().Remove(mLayer);
561 Self().SetSensitive( false );
563 // Guard against destruction during signal emission
564 Toolkit::Popup handle( GetOwner() );
565 mHiddenSignalV2.Emit();
569 Toolkit::Popup::TouchedOutsideSignalV2& Popup::OutsideTouchedSignal()
571 return mTouchedOutsideSignalV2;
574 Toolkit::Popup::HiddenSignalV2& Popup::HiddenSignal()
576 return mHiddenSignalV2;
579 bool Popup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
581 Dali::BaseHandle handle( object );
583 bool connected( true );
584 Toolkit::Popup popup = Toolkit::Popup::DownCast(handle);
586 if( Dali::Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE == signalName )
588 popup.OutsideTouchedSignal().Connect( tracker, functor );
590 else if( Dali::Toolkit::Popup::SIGNAL_HIDDEN == signalName )
592 popup.HiddenSignal().Connect( tracker, functor );
596 // signalName does not match any signal
603 void Popup::OnStateAnimationFinished( Animation& source )
605 HandleStateChangeComplete();
608 bool Popup::OnBackingTouched(Actor actor, const TouchEvent& event)
610 if(event.GetPointCount()>0)
612 const TouchPoint& point = event.GetPoint(0);
614 if(point.state == TouchPoint::Down)
616 // Guard against destruction during signal emission
617 Toolkit::Popup handle( GetOwner() );
619 mTouchedOutsideSignalV2.Emit();
626 bool Popup::OnBackingMouseWheelEvent(Actor actor, const MouseWheelEvent& event)
628 // consume mouse wheel event in dimmed backing actor
632 bool Popup::OnDialogTouched(Actor actor, const TouchEvent& event)
634 // consume event (stops backing actor receiving touch events)
638 void Popup::OnControlChildAdd( Actor& child )
640 // reparent any children added by user to the body layer.
641 if( mAlterAddedChild )
643 // Removes previously added content.
646 mPopupBg.Remove( mContent );
649 // Reparent new content.
650 Self().Remove( child );
652 // keep a handle to the new content.
655 mPopupBg.Add( mContent );
659 void Popup::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
661 // Set the popup size
663 popupSize.width = size.width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
664 popupSize.height = size.height - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
666 // Update sizes of all popup's components.
668 // Relayout background image.
669 // Adjust background position and size relative to parent to cater to outer Border.
670 // Some backgrounds are intended to over-spill. That is some content
671 // should appear outside the Dialog on all sides i.e. Shadows, glow effects.
672 const Vector4 outerBorder = mPopupStyle->backgroundOuterBorder;
674 if( mBackgroundImage )
676 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
677 ParentSource( Actor::SIZE ),
678 BackgroundSizeConstraint(outerBorder) );
680 mBackgroundImage.RemoveConstraints();
681 mBackgroundImage.ApplyConstraint( constraint );
683 mBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
684 mBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
685 mBackgroundImage.SetPosition( -outerBorder.x, -outerBorder.y, 0.0f );
688 if( mPopupBg && mButtonAreaImage )
690 // If there are no buttons, button background is also removed.
691 if ( mButtons.size() == 0 )
693 mPopupBg.Remove( mButtonAreaImage );
697 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
698 ParentSource( Actor::SIZE ),
699 ButtonAreaSizeConstraint(outerBorder) );
701 mButtonAreaImage.RemoveConstraints();
702 mButtonAreaImage.ApplyConstraint( constraint );
704 mButtonAreaImage.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
705 mButtonAreaImage.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
706 mButtonAreaImage.SetY( -outerBorder.z - POPUP_OUT_MARGIN_HEIGHT );
708 mPopupBg.Add( mButtonAreaImage );
713 Vector3 positionOffset( 0.0f, mPopupStyle->margin + POPUP_OUT_MARGIN_WIDTH, CONTENT_DEPTH );
717 titleSize.width = popupSize.width;
718 titleSize.height = mTitle.GetHeightForWidth( titleSize.width );
720 // As the default size policy for text-view is Fixed & Fixed, a size needs to be set.
721 // Otherwise size-negotiation algorithm uses the GetNaturalSize() with doesn't take
722 // into account the multiline and exceed policies, giving as result a wrong size.
723 mTitle.SetSize( titleSize );
724 Relayout( mTitle, titleSize, container );
726 mTitle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
727 mTitle.SetParentOrigin( ParentOrigin::TOP_CENTER );
728 mTitle.SetPosition( positionOffset );
730 positionOffset.y += titleSize.height + mPopupStyle->margin;
736 // If the content width is greater than popup width then scale it down/wrap text as needed
737 Vector2 contentSize( RelayoutHelper::GetNaturalSize( mContent ) );
738 if( contentSize.width > popupSize.width )
740 contentSize.width = popupSize.width;
741 contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, contentSize.width );
744 mContent.SetSize( contentSize );
745 Relayout( mContent, contentSize, container );
747 mContent.SetParentOrigin(ParentOrigin::TOP_CENTER);
748 mContent.SetAnchorPoint(AnchorPoint::TOP_CENTER);
750 mContent.SetPosition( positionOffset );
752 positionOffset.y += contentSize.height + mPopupStyle->margin;
755 // Relayout Button Area
758 mBottomBg.SetSize( popupSize.width, mPopupStyle->bottomSize.height );
760 mBottomBg.SetParentOrigin(ParentOrigin::TOP_CENTER);
761 mBottomBg.SetAnchorPoint(AnchorPoint::TOP_CENTER);
763 mBottomBg.SetPosition( positionOffset );
766 // Relayout All buttons
767 if ( !mButtons.empty() )
769 // All buttons should be the same size and fill the button area. The button spacing needs to be accounted for as well.
770 Vector2 buttonSize( ( ( popupSize.width - mPopupStyle->buttonSpacing * ( mButtons.size() - 1 ) ) / mButtons.size() ),
771 mPopupStyle->bottomSize.height - mPopupStyle->margin );
773 Vector3 buttonPosition;
775 for ( ActorIter iter = mButtons.begin(), endIter = mButtons.end();
777 ++iter, buttonPosition.x += mPopupStyle->buttonSpacing + buttonSize.width )
779 iter->SetPosition( buttonPosition );
781 // If there is only one button, it needs to be laid out on center.
782 if ( mButtons.size() == 1 )
784 iter->SetAnchorPoint( AnchorPoint::CENTER );
785 iter->SetParentOrigin( ParentOrigin::CENTER );
789 iter->SetAnchorPoint( AnchorPoint::CENTER_LEFT );
790 iter->SetParentOrigin( ParentOrigin::CENTER_LEFT );
793 Relayout( *iter, buttonSize, container );
797 if( mShowing && mBacking )
799 Vector2 stageSize = Stage::GetCurrent().GetSize();
800 float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
801 Vector3 targetBackingSize = Vector3( length, length, 1.0f );
803 mBacking.SetSize( targetBackingSize );
807 bool Popup::OnKeyEvent(const KeyEvent& event)
809 bool consumed = false;
811 if(event.state == KeyEvent::Down)
813 if (event.keyCode == Dali::DALI_KEY_ESCAPE || event.keyCode == Dali::DALI_KEY_BACK)
815 SetState(Toolkit::Popup::POPUP_HIDE);
823 Vector3 Popup::GetNaturalSize()
825 float margin = 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
826 const float maxWidth = Stage::GetCurrent().GetSize().width - margin;
828 Vector3 naturalSize( 0.0f, 0.0f, 0.0f );
832 Vector3 titleNaturalSize = mTitle.GetImplementation().GetNaturalSize();
833 // Buffer to avoid errors. The width of the popup could potentially be the width of the title text.
834 // It was observed in this case that text wrapping was then inconsistent when seen on device
835 const float titleBuffer = 0.5f;
836 titleNaturalSize.width += titleBuffer;
838 // As TextView GetNaturalSize does not take wrapping into account, limit the width
839 // to that of the stage
840 if( titleNaturalSize.width >= maxWidth)
842 naturalSize.width = maxWidth;
843 naturalSize.height = mTitle.GetImplementation().GetHeightForWidth( naturalSize.width );
847 naturalSize += titleNaturalSize;
850 naturalSize.height += mPopupStyle->margin;
855 Vector3 contentSize = RelayoutHelper::GetNaturalSize( mContent );
856 // Choose the biggest width
857 naturalSize.width = std::max( naturalSize.width, contentSize.width );
858 naturalSize.height += contentSize.height + mPopupStyle->margin;
861 if( !mButtons.empty() )
863 naturalSize.height += mPopupStyle->bottomSize.height;
867 naturalSize.width += margin;
868 naturalSize.height += margin;
873 float Popup::GetHeightForWidth( float width )
875 float height( 0.0f );
876 float popupWidth( width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
880 height += mTitle.GetImplementation().GetHeightForWidth( popupWidth );
881 height += mPopupStyle->margin;
886 height += RelayoutHelper::GetHeightForWidth( mContent, popupWidth ) + mPopupStyle->margin;
889 if( !mButtons.empty() )
891 height += mPopupStyle->bottomSize.height;
895 float margin( 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
901 float Popup::GetWidthForHeight( float height )
903 return GetNaturalSize().width;
906 Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
908 Actor nextFocusableActor( currentFocusedActor );
910 // TODO: Needs to be optimised
912 if ( !currentFocusedActor || ( currentFocusedActor && KeyboardFocusManager::Get().GetFocusGroup(currentFocusedActor) != Self() ) )
914 // The current focused actor is not within popup
915 if( mContent && mContent.IsKeyboardFocusable() )
917 // If content is focusable, move the focus to content
918 nextFocusableActor = mContent;
920 else if( !mButtons.empty() )
922 // Otherwise, movethe focus to the first button
923 nextFocusableActor = mButtons[0];
928 // Rebuild the focus chain because button or content can be added or removed dynamically
929 ActorContainer focusableActors;
930 if( mContent && mContent.IsKeyboardFocusable() )
932 focusableActors.push_back(mContent);
935 for(unsigned int i = 0; i < mButtons.size(); i++)
937 if( mButtons[i] && mButtons[i].IsKeyboardFocusable() )
939 focusableActors.push_back(mButtons[i]);
943 for ( ActorContainer::iterator iter = focusableActors.begin(), end = focusableActors.end(); iter != end; ++iter )
945 if ( currentFocusedActor == *iter )
951 if ( iter == focusableActors.begin() )
953 nextFocusableActor = *( focusableActors.end() - 1 );
957 nextFocusableActor = *( iter - 1 );
963 if ( iter == focusableActors.end() - 1 )
965 nextFocusableActor = *( focusableActors.begin() );
969 nextFocusableActor = *( iter + 1 );
976 if ( *iter == mContent )
978 nextFocusableActor = *( focusableActors.end() - 1 );
982 if ( mContent && mContent.IsKeyboardFocusable() )
984 nextFocusableActor = mContent;
988 if ( iter == focusableActors.begin() )
990 nextFocusableActor = *( focusableActors.end() - 1 );
994 nextFocusableActor = *( iter - 1 );
1003 if ( mContent && mContent.IsKeyboardFocusable() )
1005 nextFocusableActor = mContent;
1009 if ( iter == focusableActors.end() - 1 )
1011 nextFocusableActor = *( focusableActors.begin() );
1015 nextFocusableActor = *( iter + 1 );
1019 if ( *iter == mContent && !mButtons.empty() )
1021 nextFocusableActor = mButtons[0];
1027 if(!nextFocusableActor)
1029 DALI_LOG_WARNING("Can not decide next focusable actor\n");
1037 return nextFocusableActor;
1040 } // namespace Internal
1042 } // namespace Toolkit