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))
188 SetKeyboardNavigationSupport( true );
191 void Popup::OnInitialize()
194 self.SetSensitive(false);
197 mLayer = Layer::New();
198 mLayer.SetParentOrigin(ParentOrigin::CENTER);
199 mLayer.SetAnchorPoint(AnchorPoint::CENTER);
201 mLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
204 mPopupBg = Actor::New();
205 mPopupBg.SetParentOrigin(ParentOrigin::CENTER);
206 mPopupBg.SetAnchorPoint(AnchorPoint::CENTER);
207 mPopupBg.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
208 mLayer.Add(mPopupBg);
210 // Any content after this point which is added to Self() will be reparented to
212 mAlterAddedChild = true;
214 // Add Backing (Dim effect)
217 // Add Dialog ( background image, title, content container, button container and tail )
221 ShowTail(ParentOrigin::BOTTOM_CENTER);
223 // Hide content by default.
224 SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
226 mPropertyTitle = self.RegisterProperty( PROPERTY_TITLE, "", Property::READ_WRITE );
227 mPropertyState = self.RegisterProperty( PROPERTY_STATE, "POPUP_HIDE", Property::READ_WRITE );
229 // Make self as keyboard focusable and focus group
230 self.SetKeyboardFocusable(true);
231 SetAsKeyboardFocusGroup(true);
234 void Popup::OnPropertySet( Property::Index index, Property::Value propertyValue )
236 if( index == mPropertyTitle )
238 SetTitle(propertyValue.Get<std::string>());
240 else if ( index == mPropertyState )
242 std::string value( propertyValue.Get<std::string>() );
243 if(value == "POPUP_SHOW")
245 SetState( Toolkit::Popup::POPUP_SHOW, 0.0f );
247 else if( value == "POPUP_HIDE")
249 SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
258 size_t Popup::GetButtonCount() const
260 return mButtons.size();
263 void Popup::SetBackgroundImage( Actor image )
265 // Removes any previous background.
266 if( mBackgroundImage && mPopupBg )
268 mPopupBg.Remove( mBackgroundImage );
271 // Adds new background to the dialog.
272 mBackgroundImage = image;
274 // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
275 mBackgroundImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
277 mPopupBg.Add( mBackgroundImage );
280 void Popup::SetButtonAreaImage( Actor image )
282 // Removes any previous area image.
283 if( mButtonAreaImage && mPopupBg )
285 mPopupBg.Remove( mButtonAreaImage );
288 // Adds new area image to the dialog.
289 mButtonAreaImage = image;
291 // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
292 mButtonAreaImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
294 mPopupBg.Add( mButtonAreaImage );
297 void Popup::SetTitle( const std::string& text )
299 Toolkit::TextView titleActor = Toolkit::TextView::New();
300 titleActor.SetText( text );
301 titleActor.SetColor( Color::BLACK );
302 titleActor.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
303 titleActor.SetWidthExceedPolicy( Toolkit::TextView::Split );
304 titleActor.SetLineJustification( Toolkit::TextView::Center );
306 SetTitle( titleActor );
309 void Popup::SetTitle( Toolkit::TextView titleActor )
311 // Replaces the current title actor.
312 if( mTitle && mPopupBg )
314 mPopupBg.Remove( mTitle );
318 mPopupBg.Add( mTitle );
323 Toolkit::TextView Popup::GetTitle() const
328 void Popup::AddButton( Toolkit::Button button )
330 mButtons.push_back( button );
331 mBottomBg.Add( button );
336 void Popup::SetState( Toolkit::Popup::PopupState state )
338 SetState( state, POPUP_ANIMATION_DURATION );
341 void Popup::SetState( Toolkit::Popup::PopupState state, float duration )
343 // default animation behaviour.
344 HandleStateChange(state, duration);
347 Toolkit::Popup::PopupState Popup::GetState() const
352 void Popup::ShowTail(const Vector3& position)
354 // Replaces the tail actor.
355 if(mTailImage && mTailImage.GetParent())
357 mTailImage.GetParent().Remove( mTailImage );
361 std::string image = "";
363 // depending on position of tail around ParentOrigin, a different tail image is used...
364 if(position.y < Math::MACHINE_EPSILON_1)
366 image = mPopupStyle->tailUpImage;
368 else if(position.y > 1.0f - Math::MACHINE_EPSILON_1)
370 image = mPopupStyle->tailDownImage;
372 else if(position.x < Math::MACHINE_EPSILON_1)
374 image = mPopupStyle->tailLeftImage;
376 else if(position.x > 1.0f - Math::MACHINE_EPSILON_1)
378 image = mPopupStyle->tailRightImage;
383 Image tail = Image::New( image );
384 mTailImage = ImageActor::New(tail);
385 const Vector3 anchorPoint = AnchorPoint::FRONT_BOTTOM_RIGHT - position;
387 mTailImage.SetParentOrigin(position);
388 mTailImage.SetAnchorPoint(anchorPoint);
390 mBottomBg.Add(mTailImage);
394 void Popup::HideTail()
396 ShowTail(ParentOrigin::CENTER);
399 void Popup::SetStyle(PopupStyle& style)
401 mPopupStyle = PopupStylePtr(&style);
405 PopupStylePtr Popup::GetStyle() const
410 void Popup::SetDefaultBackgroundImage()
412 Image bg = Image::New( mPopupStyle->backgroundImage );
413 ImageActor bgImage = ImageActor::New( bg );
414 bgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
415 bgImage.SetNinePatchBorder( mPopupStyle->backgroundScale9Border );
417 Image buttonBg = Image::New( mPopupStyle->buttonAreaImage );
418 ImageActor buttonBgImage = ImageActor::New( buttonBg );
419 buttonBgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
420 buttonBgImage.SetNinePatchBorder( mPopupStyle->buttonArea9PatchBorder );
422 SetBackgroundImage( bgImage );
423 SetButtonAreaImage( buttonBgImage );
426 void Popup::CreateBacking()
428 mBacking = Dali::Toolkit::CreateSolidColorActor( mPopupStyle->backingColor );
430 mBacking.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
431 mBacking.SetSensitive(true);
433 mLayer.Add(mBacking);
434 mBacking.SetOpacity(0.0f);
435 mBacking.SetPosition(0.0f, 0.0f, BACKING_DEPTH);
436 mBacking.TouchedSignal().Connect( this, &Popup::OnBackingTouched );
437 mBacking.MouseWheelEventSignal().Connect(this, &Popup::OnBackingMouseWheelEvent);
440 void Popup::CreateDialog()
442 // Adds default background image.
443 SetDefaultBackgroundImage();
445 // Adds bottom background
446 mBottomBg = Actor::New();
447 mPopupBg.Add( mBottomBg );
450 void Popup::HandleStateChange( Toolkit::Popup::PopupState state, float duration )
452 const Vector2& stageSize( Stage::GetCurrent().GetSize() );
455 float targetBackingAlpha;
456 Vector3 targetBackingSize;
465 case Toolkit::Popup::POPUP_HIDE:
467 targetSize = Vector3(0.0f, 0.0f, 1.0f);
468 targetBackingAlpha = 0.0f;
469 targetBackingSize = Vector3(0.0f, 0.0f, 1.0f);
471 ClearKeyInputFocus();
473 // Retore the keyboard focus when popup is hidden
474 if(mPreviousFocusedActor && mPreviousFocusedActor.IsKeyboardFocusable() )
476 Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
477 if( keyboardFocusManager )
479 keyboardFocusManager.SetCurrentFocusActor(mPreviousFocusedActor);
486 case Toolkit::Popup::POPUP_SHOW:
489 targetSize = Vector3(1.0f, 1.0f, 1.0f);
490 targetBackingAlpha = 1.0f;
491 float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
492 targetBackingSize = Vector3( length, length, 1.0f );
495 // Add contents to stage for showing.
496 if( !mLayer.GetParent() )
498 mAlterAddedChild = false;
500 mAlterAddedChild = true;
502 Self().SetSensitive(true);
505 // Handle the keyboard focus when popup is shown
506 Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
507 if( keyboardFocusManager )
509 mPreviousFocusedActor = keyboardFocusManager.GetCurrentFocusActor();
511 if( mContent && mContent.IsKeyboardFocusable() )
513 // If content is focusable, move the focus to content
514 keyboardFocusManager.SetCurrentFocusActor(mContent);
516 else if( !mButtons.empty() )
518 // Otherwise, movethe focus to the first button
519 keyboardFocusManager.SetCurrentFocusActor(mButtons[0]);
523 DALI_LOG_WARNING("There is no focusable in popup\n");
530 mBacking.SetSize( targetBackingSize );
532 if(duration > Math::MACHINE_EPSILON_1)
540 mAnimation = Animation::New(duration);
544 mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
545 mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(duration * 0.5f, duration * 0.5f) );
549 mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
550 mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
553 mAnimation.FinishedSignal().Connect(this, &Popup::OnStateAnimationFinished);
557 mBacking.SetOpacity( targetBackingAlpha );
558 mPopupBg.SetScale( targetSize );
560 HandleStateChangeComplete();
564 void Popup::HandleStateChangeComplete()
566 // Remove contents from stage if completely hidden.
567 if( (mState == Toolkit::Popup::POPUP_HIDE) && (mLayer.GetParent()) )
569 Self().Remove(mLayer);
570 Self().SetSensitive( false );
572 // Guard against destruction during signal emission
573 Toolkit::Popup handle( GetOwner() );
574 mHiddenSignalV2.Emit();
578 Toolkit::Popup::TouchedOutsideSignalV2& Popup::OutsideTouchedSignal()
580 return mTouchedOutsideSignalV2;
583 Toolkit::Popup::HiddenSignalV2& Popup::HiddenSignal()
585 return mHiddenSignalV2;
588 bool Popup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
590 Dali::BaseHandle handle( object );
592 bool connected( true );
593 Toolkit::Popup popup = Toolkit::Popup::DownCast(handle);
595 if( Dali::Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE == signalName )
597 popup.OutsideTouchedSignal().Connect( tracker, functor );
599 else if( Dali::Toolkit::Popup::SIGNAL_HIDDEN == signalName )
601 popup.HiddenSignal().Connect( tracker, functor );
605 // signalName does not match any signal
612 void Popup::OnStateAnimationFinished( Animation& source )
614 HandleStateChangeComplete();
617 bool Popup::OnBackingTouched(Actor actor, const TouchEvent& event)
619 if(event.GetPointCount()>0)
621 const TouchPoint& point = event.GetPoint(0);
623 if(point.state == TouchPoint::Down)
625 // Guard against destruction during signal emission
626 Toolkit::Popup handle( GetOwner() );
628 mTouchedOutsideSignalV2.Emit();
635 bool Popup::OnBackingMouseWheelEvent(Actor actor, const MouseWheelEvent& event)
637 // consume mouse wheel event in dimmed backing actor
641 bool Popup::OnDialogTouched(Actor actor, const TouchEvent& event)
643 // consume event (stops backing actor receiving touch events)
647 void Popup::OnControlChildAdd( Actor& child )
649 // reparent any children added by user to the body layer.
650 if( mAlterAddedChild )
652 // Removes previously added content.
655 mPopupBg.Remove( mContent );
658 // Reparent new content.
659 Self().Remove( child );
661 // keep a handle to the new content.
664 mPopupBg.Add( mContent );
668 void Popup::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
670 // Set the popup size
672 popupSize.width = size.width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
673 popupSize.height = size.height - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
675 // Update sizes of all popup's components.
677 // Relayout background image.
678 // Adjust background position and size relative to parent to cater to outer Border.
679 // Some backgrounds are intended to over-spill. That is some content
680 // should appear outside the Dialog on all sides i.e. Shadows, glow effects.
681 const Vector4 outerBorder = mPopupStyle->backgroundOuterBorder;
683 if( mBackgroundImage )
685 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
686 ParentSource( Actor::SIZE ),
687 BackgroundSizeConstraint(outerBorder) );
689 mBackgroundImage.RemoveConstraints();
690 mBackgroundImage.ApplyConstraint( constraint );
692 mBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
693 mBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
694 mBackgroundImage.SetPosition( -outerBorder.x, -outerBorder.y, 0.0f );
697 if( mPopupBg && mButtonAreaImage )
699 // If there are no buttons, button background is also removed.
700 if ( mButtons.size() == 0 )
702 mPopupBg.Remove( mButtonAreaImage );
706 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
707 ParentSource( Actor::SIZE ),
708 ButtonAreaSizeConstraint(outerBorder) );
710 mButtonAreaImage.RemoveConstraints();
711 mButtonAreaImage.ApplyConstraint( constraint );
713 mButtonAreaImage.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
714 mButtonAreaImage.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
715 mButtonAreaImage.SetY( -outerBorder.z - POPUP_OUT_MARGIN_HEIGHT );
717 mPopupBg.Add( mButtonAreaImage );
722 Vector3 positionOffset( 0.0f, mPopupStyle->margin + POPUP_OUT_MARGIN_WIDTH, CONTENT_DEPTH );
726 titleSize.width = popupSize.width;
727 titleSize.height = mTitle.GetHeightForWidth( titleSize.width );
729 // As the default size policy for text-view is Fixed & Fixed, a size needs to be set.
730 // Otherwise size-negotiation algorithm uses the GetNaturalSize() with doesn't take
731 // into account the multiline and exceed policies, giving as result a wrong size.
732 mTitle.SetSize( titleSize );
733 Relayout( mTitle, titleSize, container );
735 mTitle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
736 mTitle.SetParentOrigin( ParentOrigin::TOP_CENTER );
737 mTitle.SetPosition( positionOffset );
739 positionOffset.y += titleSize.height + mPopupStyle->margin;
745 // If the content width is greater than popup width then scale it down/wrap text as needed
746 Vector2 contentSize( RelayoutHelper::GetNaturalSize( mContent ) );
747 if( contentSize.width > popupSize.width )
749 contentSize.width = popupSize.width;
750 contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, contentSize.width );
753 mContent.SetSize( contentSize );
754 Relayout( mContent, contentSize, container );
756 mContent.SetParentOrigin(ParentOrigin::TOP_CENTER);
757 mContent.SetAnchorPoint(AnchorPoint::TOP_CENTER);
759 mContent.SetPosition( positionOffset );
761 positionOffset.y += contentSize.height + mPopupStyle->margin;
764 // Relayout Button Area
767 mBottomBg.SetSize( popupSize.width, mPopupStyle->bottomSize.height );
769 mBottomBg.SetParentOrigin(ParentOrigin::TOP_CENTER);
770 mBottomBg.SetAnchorPoint(AnchorPoint::TOP_CENTER);
772 mBottomBg.SetPosition( positionOffset );
775 // Relayout All buttons
776 if ( !mButtons.empty() )
778 // All buttons should be the same size and fill the button area. The button spacing needs to be accounted for as well.
779 Vector2 buttonSize( ( ( popupSize.width - mPopupStyle->buttonSpacing * ( mButtons.size() - 1 ) ) / mButtons.size() ),
780 mPopupStyle->bottomSize.height - mPopupStyle->margin );
782 Vector3 buttonPosition;
784 for ( ActorIter iter = mButtons.begin(), endIter = mButtons.end();
786 ++iter, buttonPosition.x += mPopupStyle->buttonSpacing + buttonSize.width )
788 iter->SetPosition( buttonPosition );
790 // If there is only one button, it needs to be laid out on center.
791 if ( mButtons.size() == 1 )
793 iter->SetAnchorPoint( AnchorPoint::CENTER );
794 iter->SetParentOrigin( ParentOrigin::CENTER );
798 iter->SetAnchorPoint( AnchorPoint::CENTER_LEFT );
799 iter->SetParentOrigin( ParentOrigin::CENTER_LEFT );
802 Relayout( *iter, buttonSize, container );
806 if( mShowing && mBacking )
808 Vector2 stageSize = Stage::GetCurrent().GetSize();
809 float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
810 Vector3 targetBackingSize = Vector3( length, length, 1.0f );
812 mBacking.SetSize( targetBackingSize );
816 bool Popup::OnKeyEvent(const KeyEvent& event)
818 bool consumed = false;
820 if(event.state == KeyEvent::Down)
822 if (event.keyCode == Dali::DALI_KEY_ESCAPE || event.keyCode == Dali::DALI_KEY_BACK)
824 SetState(Toolkit::Popup::POPUP_HIDE);
832 Vector3 Popup::GetNaturalSize()
834 float margin = 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
835 const float maxWidth = Stage::GetCurrent().GetSize().width - margin;
837 Vector3 naturalSize( 0.0f, 0.0f, 0.0f );
841 Vector3 titleNaturalSize = mTitle.GetImplementation().GetNaturalSize();
842 // Buffer to avoid errors. The width of the popup could potentially be the width of the title text.
843 // It was observed in this case that text wrapping was then inconsistent when seen on device
844 const float titleBuffer = 0.5f;
845 titleNaturalSize.width += titleBuffer;
847 // As TextView GetNaturalSize does not take wrapping into account, limit the width
848 // to that of the stage
849 if( titleNaturalSize.width >= maxWidth)
851 naturalSize.width = maxWidth;
852 naturalSize.height = mTitle.GetImplementation().GetHeightForWidth( naturalSize.width );
856 naturalSize += titleNaturalSize;
859 naturalSize.height += mPopupStyle->margin;
864 Vector3 contentSize = RelayoutHelper::GetNaturalSize( mContent );
865 // Choose the biggest width
866 naturalSize.width = std::max( naturalSize.width, contentSize.width );
867 naturalSize.height += contentSize.height + mPopupStyle->margin;
870 if( !mButtons.empty() )
872 naturalSize.height += mPopupStyle->bottomSize.height;
876 naturalSize.width += margin;
877 naturalSize.height += margin;
882 float Popup::GetHeightForWidth( float width )
884 float height( 0.0f );
885 float popupWidth( width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
889 height += mTitle.GetImplementation().GetHeightForWidth( popupWidth );
890 height += mPopupStyle->margin;
895 height += RelayoutHelper::GetHeightForWidth( mContent, popupWidth ) + mPopupStyle->margin;
898 if( !mButtons.empty() )
900 height += mPopupStyle->bottomSize.height;
904 float margin( 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
910 float Popup::GetWidthForHeight( float height )
912 return GetNaturalSize().width;
915 Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
917 Actor nextFocusableActor( currentFocusedActor );
919 // TODO: Needs to be optimised
921 if ( !currentFocusedActor || ( currentFocusedActor && KeyboardFocusManager::Get().GetFocusGroup(currentFocusedActor) != Self() ) )
923 // The current focused actor is not within popup
924 if( mContent && mContent.IsKeyboardFocusable() )
926 // If content is focusable, move the focus to content
927 nextFocusableActor = mContent;
929 else if( !mButtons.empty() )
931 // Otherwise, movethe focus to the first button
932 nextFocusableActor = mButtons[0];
937 // Rebuild the focus chain because button or content can be added or removed dynamically
938 ActorContainer focusableActors;
939 if( mContent && mContent.IsKeyboardFocusable() )
941 focusableActors.push_back(mContent);
944 for(unsigned int i = 0; i < mButtons.size(); i++)
946 if( mButtons[i] && mButtons[i].IsKeyboardFocusable() )
948 focusableActors.push_back(mButtons[i]);
952 for ( ActorContainer::iterator iter = focusableActors.begin(), end = focusableActors.end(); iter != end; ++iter )
954 if ( currentFocusedActor == *iter )
958 case Toolkit::Control::Left:
960 if ( iter == focusableActors.begin() )
962 nextFocusableActor = *( focusableActors.end() - 1 );
966 nextFocusableActor = *( iter - 1 );
970 case Toolkit::Control::Right:
972 if ( iter == focusableActors.end() - 1 )
974 nextFocusableActor = *( focusableActors.begin() );
978 nextFocusableActor = *( iter + 1 );
983 case Toolkit::Control::Up:
985 if ( *iter == mContent )
987 nextFocusableActor = *( focusableActors.end() - 1 );
991 if ( mContent && mContent.IsKeyboardFocusable() )
993 nextFocusableActor = mContent;
997 if ( iter == focusableActors.begin() )
999 nextFocusableActor = *( focusableActors.end() - 1 );
1003 nextFocusableActor = *( iter - 1 );
1010 case Toolkit::Control::Down:
1012 if ( mContent && mContent.IsKeyboardFocusable() )
1014 nextFocusableActor = mContent;
1018 if ( iter == focusableActors.end() - 1 )
1020 nextFocusableActor = *( focusableActors.begin() );
1024 nextFocusableActor = *( iter + 1 );
1028 if ( *iter == mContent && !mButtons.empty() )
1030 nextFocusableActor = mButtons[0];
1036 if(!nextFocusableActor)
1038 DALI_LOG_WARNING("Can not decide next focusable actor\n");
1046 return nextFocusableActor;
1049 } // namespace Internal
1051 } // namespace Toolkit