81e788717923b55bc2e85ba271ae778f4ac2312e
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / popup / popup-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali-toolkit/internal/controls/popup/popup-impl.h>
19
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>
23
24 #include <dali-toolkit/internal/controls/relayout-helper.h>
25 #include <dali-toolkit/internal/focus-manager/keyboard-focus-manager-impl.h>
26
27 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
28 #include <dali/integration-api/debug.h>
29
30 using namespace Dali;
31 using namespace std;
32
33 namespace
34 {
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)
38
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);
46
47 const char* const PROPERTY_TITLE = "title";
48 const char* const PROPERTY_STATE = "state";
49
50 // Constraints ///////////////////////////////////////////////////////////////////////////
51
52 /**
53  * BackgroundSizeConstraint
54  *
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.
59  */
60 struct BackgroundSizeConstraint
61 {
62   /**
63    * Constraint that sets size to parent's size plus a border.
64    *
65    * @param[in] outerBorder The border to extend beyond parent's Size.
66    */
67   BackgroundSizeConstraint( Vector4 outerBorder )
68   : mOuterBorder( outerBorder )
69   {
70   }
71
72   /**
73    * (render thread code)
74    * @param[in] current The current size.
75    * @param[in] parentSizeProperty The parent's size
76    */
77   Vector3 operator()( const Vector3& current,
78                       const PropertyInput& parentSizeProperty )
79   {
80     Vector3 size = parentSizeProperty.GetVector3();
81
82     size.width += mOuterBorder.x + mOuterBorder.y;
83     size.height += mOuterBorder.z + mOuterBorder.w;
84
85     return size;
86   }
87
88   const Vector4 mOuterBorder;  ///< The size of the outer-border (Set to 0.0, 0.0f, 0.0f, 0.0f if doesn't exist).
89 };
90
91 struct ButtonAreaSizeConstraint
92 {
93   /**
94    * Constraint that sets size to parent's size plus a border.
95    *
96    * @param[in] outerBorder The border to extend beyond parent's Size.
97    */
98   ButtonAreaSizeConstraint( Vector4 outerBorder )
99   : mOuterBorder( outerBorder )
100   {
101   }
102
103   /**
104    * (render thread code)
105    * @param[in] current The current size.
106    * @param[in] parentSizeProperty The parent's size
107    */
108   Vector3 operator()( const Vector3& current,
109                       const PropertyInput& parentSizeProperty )
110   {
111     Vector3 size = parentSizeProperty.GetVector3();
112
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;
116
117     return size;
118   }
119
120   const Vector4 mOuterBorder;  ///< The size of the outer-border (Set to 0.0, 0.0f, 0.0f, 0.0f if doesn't exist).
121 };
122
123 } // unnamed namespace
124
125 namespace Dali
126 {
127
128 namespace Toolkit
129 {
130
131 namespace Internal
132 {
133
134 namespace
135 {
136
137 BaseHandle Create()
138 {
139   return Toolkit::Popup::New();
140 }
141
142 TypeRegistration typeRegistration( typeid(Toolkit::Popup), typeid(Toolkit::Control), Create );
143
144 SignalConnectorType signalConnector1( typeRegistration, Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE, &Popup::DoConnectSignal );
145 SignalConnectorType signalConnector2( typeRegistration, Toolkit::Popup::SIGNAL_HIDDEN, &Popup::DoConnectSignal );
146
147
148 }
149
150
151 ///////////////////////////////////////////////////////////////////////////////////////////////////
152 // Popup
153 ///////////////////////////////////////////////////////////////////////////////////////////////////
154
155 Dali::Toolkit::Popup Popup::New()
156 {
157   PopupStylePtr style = PopupStyleDefault::New();
158
159   // Create the implementation
160   PopupPtr popup(new Popup(*style));
161
162   // Pass ownership to CustomActor via derived handle
163   Dali::Toolkit::Popup handle(*popup);
164
165   // Second-phase init of the implementation
166   // This can only be done after the CustomActor connection has been made...
167   popup->Initialize();
168
169   return handle;
170 }
171
172 Popup::Popup(PopupStyle& style)
173 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
174   mShowing(false),
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))
178 {
179   SetKeyboardNavigationSupport( true );
180 }
181
182 void Popup::OnInitialize()
183 {
184   Actor self = Self();
185   self.SetSensitive(false);
186
187   // Create Layer
188   mLayer = Layer::New();
189   mLayer.SetParentOrigin(ParentOrigin::CENTER);
190   mLayer.SetAnchorPoint(AnchorPoint::CENTER);
191   mLayer.RaiseToTop();
192   mLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
193   self.Add(mLayer);
194
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);
200
201   // Any content after this point which is added to Self() will be reparented to
202   // mContent.
203   mAlterAddedChild = true;
204
205   // Add Backing (Dim effect)
206   CreateBacking();
207
208   // Add Dialog ( background image, title, content container, button container and tail )
209   CreateDialog();
210
211   // Default content.
212   ShowTail(ParentOrigin::BOTTOM_CENTER);
213
214   // Hide content by default.
215   SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
216
217   mPropertyTitle = self.RegisterProperty( PROPERTY_TITLE, "", Property::READ_WRITE );
218   mPropertyState = self.RegisterProperty( PROPERTY_STATE, "POPUP_HIDE", Property::READ_WRITE );
219
220   // Make self as keyboard focusable and focus group
221   self.SetKeyboardFocusable(true);
222   SetAsKeyboardFocusGroup(true);
223 }
224
225 void Popup::OnPropertySet( Property::Index index, Property::Value propertyValue )
226 {
227   if( index == mPropertyTitle )
228   {
229     SetTitle(propertyValue.Get<std::string>());
230   }
231   else if ( index == mPropertyState )
232   {
233     std::string value( propertyValue.Get<std::string>() );
234     if(value == "POPUP_SHOW")
235     {
236       SetState( Toolkit::Popup::POPUP_SHOW, 0.0f );
237     }
238     else if( value == "POPUP_HIDE")
239     {
240       SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
241     }
242   }
243 }
244
245 Popup::~Popup()
246 {
247 }
248
249 size_t Popup::GetButtonCount() const
250 {
251   return mButtons.size();
252 }
253
254 void Popup::SetBackgroundImage( Actor image )
255 {
256   // Removes any previous background.
257   if( mBackgroundImage && mPopupBg )
258   {
259     mPopupBg.Remove( mBackgroundImage );
260   }
261
262   // Adds new background to the dialog.
263   mBackgroundImage = image;
264
265   // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
266   mBackgroundImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
267
268   mPopupBg.Add( mBackgroundImage );
269 }
270
271 void Popup::SetButtonAreaImage( Actor image )
272 {
273   // Removes any previous area image.
274   if( mButtonAreaImage && mPopupBg )
275   {
276     mPopupBg.Remove( mButtonAreaImage );
277   }
278
279   // Adds new area image to the dialog.
280   mButtonAreaImage = image;
281
282   // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
283   mButtonAreaImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
284
285   mPopupBg.Add( mButtonAreaImage );
286 }
287
288 void Popup::SetTitle( const std::string& text )
289 {
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 );
296
297   SetTitle( titleActor );
298 }
299
300 void Popup::SetTitle( Toolkit::TextView titleActor )
301 {
302   // Replaces the current title actor.
303   if( mTitle && mPopupBg )
304   {
305     mPopupBg.Remove( mTitle );
306   }
307   mTitle = titleActor;
308
309   mPopupBg.Add( mTitle );
310
311   RelayoutRequest();
312 }
313
314 Toolkit::TextView Popup::GetTitle() const
315 {
316   return mTitle;
317 }
318
319 void Popup::AddButton( Toolkit::Button button )
320 {
321   mButtons.push_back( button );
322   mBottomBg.Add( button );
323
324   RelayoutRequest();
325 }
326
327 void Popup::SetState( Toolkit::Popup::PopupState state )
328 {
329   SetState( state, POPUP_ANIMATION_DURATION );
330 }
331
332 void Popup::SetState( Toolkit::Popup::PopupState state, float duration )
333 {
334   // default animation behaviour.
335   HandleStateChange(state, duration);
336 }
337
338 Toolkit::Popup::PopupState Popup::GetState() const
339 {
340   return mState;
341 }
342
343 void Popup::ShowTail(const Vector3& position)
344 {
345   // Replaces the tail actor.
346   if(mTailImage && mTailImage.GetParent())
347   {
348     mTailImage.GetParent().Remove( mTailImage );
349     mTailImage.Reset();
350   }
351
352   std::string image = "";
353
354   // depending on position of tail around ParentOrigin, a different tail image is used...
355   if(position.y < Math::MACHINE_EPSILON_1)
356   {
357     image = mPopupStyle->tailUpImage;
358   }
359   else if(position.y > 1.0f - Math::MACHINE_EPSILON_1)
360   {
361     image = mPopupStyle->tailDownImage;
362   }
363   else if(position.x < Math::MACHINE_EPSILON_1)
364   {
365     image = mPopupStyle->tailLeftImage;
366   }
367   else if(position.x > 1.0f - Math::MACHINE_EPSILON_1)
368   {
369     image = mPopupStyle->tailRightImage;
370   }
371
372   if(image != "")
373   {
374     Image tail = Image::New( image );
375     mTailImage = ImageActor::New(tail);
376     const Vector3 anchorPoint = AnchorPoint::FRONT_BOTTOM_RIGHT - position;
377
378     mTailImage.SetParentOrigin(position);
379     mTailImage.SetAnchorPoint(anchorPoint);
380
381     mBottomBg.Add(mTailImage);
382   }
383 }
384
385 void Popup::HideTail()
386 {
387   ShowTail(ParentOrigin::CENTER);
388 }
389
390 void Popup::SetStyle(PopupStyle& style)
391 {
392   mPopupStyle = PopupStylePtr(&style);
393   // update //
394 }
395
396 PopupStylePtr Popup::GetStyle() const
397 {
398   return mPopupStyle;
399 }
400
401 void Popup::SetDefaultBackgroundImage()
402 {
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 );
407
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 );
412
413   SetBackgroundImage( bgImage );
414   SetButtonAreaImage( buttonBgImage );
415 }
416
417 void Popup::CreateBacking()
418 {
419   mBacking = Dali::Toolkit::CreateSolidColorActor( mPopupStyle->backingColor );
420
421   mBacking.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
422   mBacking.SetSensitive(true);
423
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);
429 }
430
431 void Popup::CreateDialog()
432 {
433   // Adds default background image.
434   SetDefaultBackgroundImage();
435
436   // Adds bottom background
437   mBottomBg = Actor::New();
438   mPopupBg.Add( mBottomBg );
439 }
440
441 void Popup::HandleStateChange( Toolkit::Popup::PopupState state, float duration )
442 {
443   const Vector2& stageSize( Stage::GetCurrent().GetSize() );
444
445   Vector3 targetSize;
446   float targetBackingAlpha;
447   Vector3 targetBackingSize;
448
449   if(mState == state)
450   {
451     return;
452   }
453   mState = state;
454   switch(state)
455   {
456     case Toolkit::Popup::POPUP_HIDE:
457     {
458       targetSize = Vector3(0.0f, 0.0f, 1.0f);
459       targetBackingAlpha = 0.0f;
460       targetBackingSize = Vector3(0.0f, 0.0f, 1.0f);
461       mShowing = false;
462       ClearKeyInputFocus();
463
464       // Retore the keyboard focus when popup is hidden
465       if(mPreviousFocusedActor && mPreviousFocusedActor.IsKeyboardFocusable() )
466       {
467         Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
468         if( keyboardFocusManager )
469         {
470           keyboardFocusManager.SetCurrentFocusActor(mPreviousFocusedActor);
471         }
472       }
473
474       break;
475     }
476
477     case Toolkit::Popup::POPUP_SHOW:
478     default:
479     {
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 );
484       mShowing = true;
485
486       // Add contents to stage for showing.
487       if( !mLayer.GetParent() )
488       {
489         mAlterAddedChild = false;
490         Self().Add(mLayer);
491         mAlterAddedChild = true;
492       }
493       Self().SetSensitive(true);
494       SetKeyInputFocus();
495
496       // Handle the keyboard focus when popup is shown
497       Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
498       if( keyboardFocusManager )
499       {
500         mPreviousFocusedActor = keyboardFocusManager.GetCurrentFocusActor();
501
502         if( mContent && mContent.IsKeyboardFocusable() )
503         {
504           // If content is focusable, move the focus to content
505           keyboardFocusManager.SetCurrentFocusActor(mContent);
506         }
507         else if( !mButtons.empty() )
508         {
509           // Otherwise, movethe focus to the first button
510           keyboardFocusManager.SetCurrentFocusActor(mButtons[0]);
511         }
512         else
513         {
514           DALI_LOG_WARNING("There is no focusable in popup\n");
515         }
516       }
517       break;
518     }
519   }
520
521   mBacking.SetSize( targetBackingSize );
522
523   if(duration > Math::MACHINE_EPSILON_1)
524   {
525     if ( mAnimation )
526     {
527       mAnimation.Stop();
528       mAnimation.Clear();
529       mAnimation.Reset();
530     }
531     mAnimation = Animation::New(duration);
532
533     if(mShowing)
534     {
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) );
537     }
538     else
539     {
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) );
542     }
543     mAnimation.Play();
544     mAnimation.FinishedSignal().Connect(this, &Popup::OnStateAnimationFinished);
545   }
546   else
547   {
548     mBacking.SetOpacity( targetBackingAlpha );
549     mPopupBg.SetScale( targetSize );
550
551     HandleStateChangeComplete();
552   }
553 }
554
555 void Popup::HandleStateChangeComplete()
556 {
557   // Remove contents from stage if completely hidden.
558   if( (mState == Toolkit::Popup::POPUP_HIDE) && (mLayer.GetParent()) )
559   {
560     Self().Remove(mLayer);
561     Self().SetSensitive( false );
562
563     // Guard against destruction during signal emission
564     Toolkit::Popup handle( GetOwner() );
565     mHiddenSignalV2.Emit();
566   }
567 }
568
569 Toolkit::Popup::TouchedOutsideSignalV2& Popup::OutsideTouchedSignal()
570 {
571   return mTouchedOutsideSignalV2;
572 }
573
574 Toolkit::Popup::HiddenSignalV2& Popup::HiddenSignal()
575 {
576   return mHiddenSignalV2;
577 }
578
579 bool Popup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
580 {
581   Dali::BaseHandle handle( object );
582
583   bool connected( true );
584   Toolkit::Popup popup = Toolkit::Popup::DownCast(handle);
585
586   if( Dali::Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE == signalName )
587   {
588     popup.OutsideTouchedSignal().Connect( tracker, functor );
589   }
590   else if( Dali::Toolkit::Popup::SIGNAL_HIDDEN == signalName )
591   {
592     popup.HiddenSignal().Connect( tracker, functor );
593   }
594   else
595   {
596     // signalName does not match any signal
597     connected = false;
598   }
599
600   return connected;
601 }
602
603 void Popup::OnStateAnimationFinished( Animation& source )
604 {
605   HandleStateChangeComplete();
606 }
607
608 bool Popup::OnBackingTouched(Actor actor, const TouchEvent& event)
609 {
610   if(event.GetPointCount()>0)
611   {
612     const TouchPoint& point = event.GetPoint(0);
613
614     if(point.state == TouchPoint::Down)
615     {
616       // Guard against destruction during signal emission
617       Toolkit::Popup handle( GetOwner() );
618
619       mTouchedOutsideSignalV2.Emit();
620     }
621   }
622
623   return true;
624 }
625
626 bool Popup::OnBackingMouseWheelEvent(Actor actor, const MouseWheelEvent& event)
627 {
628   // consume mouse wheel event in dimmed backing actor
629   return true;
630 }
631
632 bool Popup::OnDialogTouched(Actor actor, const TouchEvent& event)
633 {
634   // consume event (stops backing actor receiving touch events)
635   return true;
636 }
637
638 void Popup::OnControlChildAdd( Actor& child )
639 {
640   // reparent any children added by user to the body layer.
641   if( mAlterAddedChild )
642   {
643     // Removes previously added content.
644     if( mContent )
645     {
646       mPopupBg.Remove( mContent );
647     }
648
649     // Reparent new content.
650     Self().Remove( child );
651
652     // keep a handle to the new content.
653     mContent = child;
654
655     mPopupBg.Add( mContent );
656   }
657 }
658
659 void Popup::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
660 {
661   // Set the popup size
662   Vector2 popupSize;
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 );
665
666   // Update sizes of all popup's components.
667
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;
673
674   if( mBackgroundImage )
675   {
676     Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
677                                                       ParentSource( Actor::SIZE ),
678                                                       BackgroundSizeConstraint(outerBorder) );
679
680     mBackgroundImage.RemoveConstraints();
681     mBackgroundImage.ApplyConstraint( constraint );
682
683     mBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
684     mBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
685     mBackgroundImage.SetPosition( -outerBorder.x, -outerBorder.y, 0.0f );
686   }
687
688   if( mPopupBg && mButtonAreaImage )
689   {
690     // If there are no buttons, button background is also removed.
691     if ( mButtons.size() == 0 )
692     {
693       mPopupBg.Remove( mButtonAreaImage );
694     }
695     else
696     {
697       Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
698                                                       ParentSource( Actor::SIZE ),
699                                                       ButtonAreaSizeConstraint(outerBorder) );
700
701       mButtonAreaImage.RemoveConstraints();
702       mButtonAreaImage.ApplyConstraint( constraint );
703
704       mButtonAreaImage.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
705       mButtonAreaImage.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
706       mButtonAreaImage.SetY( -outerBorder.z - POPUP_OUT_MARGIN_HEIGHT );
707
708       mPopupBg.Add( mButtonAreaImage );
709     }
710   }
711
712   // Relayout title
713   Vector3 positionOffset( 0.0f, mPopupStyle->margin + POPUP_OUT_MARGIN_WIDTH, CONTENT_DEPTH );
714   if( mTitle )
715   {
716     Vector2 titleSize;
717     titleSize.width  = popupSize.width;
718     titleSize.height = mTitle.GetHeightForWidth( titleSize.width );
719
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 );
725
726     mTitle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
727     mTitle.SetParentOrigin( ParentOrigin::TOP_CENTER );
728     mTitle.SetPosition( positionOffset );
729
730     positionOffset.y += titleSize.height + mPopupStyle->margin;
731   }
732
733   // Relayout content
734   if( mContent )
735   {
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 )
739     {
740       contentSize.width = popupSize.width;
741       contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, contentSize.width );
742     }
743
744     mContent.SetSize( contentSize );
745     Relayout( mContent, contentSize, container );
746
747     mContent.SetParentOrigin(ParentOrigin::TOP_CENTER);
748     mContent.SetAnchorPoint(AnchorPoint::TOP_CENTER);
749
750     mContent.SetPosition( positionOffset );
751
752     positionOffset.y += contentSize.height + mPopupStyle->margin;
753   }
754
755   // Relayout Button Area
756   if( mBottomBg )
757   {
758     mBottomBg.SetSize( popupSize.width, mPopupStyle->bottomSize.height );
759
760     mBottomBg.SetParentOrigin(ParentOrigin::TOP_CENTER);
761     mBottomBg.SetAnchorPoint(AnchorPoint::TOP_CENTER);
762
763     mBottomBg.SetPosition( positionOffset );
764   }
765
766   // Relayout All buttons
767   if ( !mButtons.empty() )
768   {
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 );
772
773     Vector3 buttonPosition;
774
775     for ( ActorIter iter = mButtons.begin(), endIter = mButtons.end();
776           iter != endIter;
777           ++iter, buttonPosition.x += mPopupStyle->buttonSpacing + buttonSize.width )
778     {
779       iter->SetPosition( buttonPosition );
780
781       // If there is only one button, it needs to be laid out on center.
782       if ( mButtons.size() == 1 )
783       {
784         iter->SetAnchorPoint( AnchorPoint::CENTER );
785         iter->SetParentOrigin( ParentOrigin::CENTER );
786       }
787       else
788       {
789         iter->SetAnchorPoint( AnchorPoint::CENTER_LEFT );
790         iter->SetParentOrigin( ParentOrigin::CENTER_LEFT );
791       }
792
793       Relayout( *iter, buttonSize, container );
794     }
795   }
796
797   if( mShowing && mBacking )
798   {
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 );
802
803     mBacking.SetSize( targetBackingSize );
804   }
805 }
806
807 bool Popup::OnKeyEvent(const KeyEvent& event)
808 {
809   bool consumed = false;
810
811   if(event.state == KeyEvent::Down)
812   {
813     if (event.keyCode == Dali::DALI_KEY_ESCAPE || event.keyCode == Dali::DALI_KEY_BACK)
814     {
815       SetState(Toolkit::Popup::POPUP_HIDE);
816       consumed = true;
817     }
818   }
819
820   return consumed;
821 }
822
823 Vector3 Popup::GetNaturalSize()
824 {
825   float margin = 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
826   const float maxWidth = Stage::GetCurrent().GetSize().width - margin;
827
828   Vector3 naturalSize( 0.0f, 0.0f, 0.0f );
829
830   if ( mTitle )
831   {
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;
837
838     // As TextView GetNaturalSize does not take wrapping into account, limit the width
839     // to that of the stage
840     if( titleNaturalSize.width >= maxWidth)
841     {
842       naturalSize.width = maxWidth;
843       naturalSize.height = mTitle.GetImplementation().GetHeightForWidth( naturalSize.width );
844     }
845     else
846     {
847       naturalSize += titleNaturalSize;
848     }
849
850     naturalSize.height += mPopupStyle->margin;
851   }
852
853   if( mContent )
854   {
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;
859   }
860
861   if( !mButtons.empty() )
862   {
863     naturalSize.height += mPopupStyle->bottomSize.height;
864   }
865
866   // Add the margins
867   naturalSize.width += margin;
868   naturalSize.height += margin;
869
870   return naturalSize;
871 }
872
873 float Popup::GetHeightForWidth( float width )
874 {
875   float height( 0.0f );
876   float popupWidth( width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
877
878   if ( mTitle )
879   {
880     height += mTitle.GetImplementation().GetHeightForWidth( popupWidth );
881     height += mPopupStyle->margin;
882   }
883
884   if( mContent )
885   {
886     height += RelayoutHelper::GetHeightForWidth( mContent, popupWidth ) + mPopupStyle->margin;
887   }
888
889   if( !mButtons.empty() )
890   {
891     height += mPopupStyle->bottomSize.height;
892   }
893
894   // Add the margins
895   float margin( 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
896   height += margin;
897
898   return height;
899 }
900
901 float Popup::GetWidthForHeight( float height )
902 {
903   return GetNaturalSize().width;
904 }
905
906 Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
907 {
908   Actor nextFocusableActor( currentFocusedActor );
909
910   // TODO: Needs to be optimised
911
912   if ( !currentFocusedActor || ( currentFocusedActor && KeyboardFocusManager::Get().GetFocusGroup(currentFocusedActor) != Self() ) )
913   {
914     // The current focused actor is not within popup
915     if( mContent && mContent.IsKeyboardFocusable() )
916     {
917       // If content is focusable, move the focus to content
918       nextFocusableActor = mContent;
919     }
920     else if( !mButtons.empty() )
921     {
922       // Otherwise, movethe focus to the first button
923       nextFocusableActor = mButtons[0];
924     }
925   }
926   else
927   {
928     // Rebuild the focus chain because button or content can be added or removed dynamically
929     ActorContainer focusableActors;
930     if( mContent && mContent.IsKeyboardFocusable() )
931     {
932       focusableActors.push_back(mContent);
933     }
934
935     for(unsigned int i = 0; i < mButtons.size(); i++)
936     {
937       if( mButtons[i] && mButtons[i].IsKeyboardFocusable() )
938       {
939         focusableActors.push_back(mButtons[i]);
940       }
941     }
942
943     for ( ActorContainer::iterator iter = focusableActors.begin(), end = focusableActors.end(); iter != end; ++iter )
944     {
945       if ( currentFocusedActor == *iter )
946       {
947         switch ( direction )
948         {
949           case Toolkit::Control::Left:
950           {
951             if ( iter == focusableActors.begin() )
952             {
953               nextFocusableActor = *( focusableActors.end() - 1 );
954             }
955             else
956             {
957               nextFocusableActor = *( iter - 1 );
958             }
959             break;
960           }
961           case Toolkit::Control::Right:
962           {
963             if ( iter == focusableActors.end() - 1 )
964             {
965               nextFocusableActor = *( focusableActors.begin() );
966             }
967             else
968             {
969               nextFocusableActor = *( iter + 1 );
970             }
971             break;
972           }
973
974           case Toolkit::Control::Up:
975           {
976             if ( *iter == mContent )
977             {
978               nextFocusableActor = *( focusableActors.end() - 1 );
979             }
980             else
981             {
982               if ( mContent && mContent.IsKeyboardFocusable() )
983               {
984                 nextFocusableActor = mContent;
985               }
986               else
987               {
988                 if ( iter == focusableActors.begin() )
989                 {
990                   nextFocusableActor = *( focusableActors.end() - 1 );
991                 }
992                 else
993                 {
994                   nextFocusableActor = *( iter - 1 );
995                 }
996               }
997             }
998             break;
999           }
1000
1001           case Toolkit::Control::Down:
1002           {
1003             if ( mContent && mContent.IsKeyboardFocusable() )
1004             {
1005               nextFocusableActor = mContent;
1006             }
1007             else
1008             {
1009               if ( iter == focusableActors.end() - 1 )
1010               {
1011                 nextFocusableActor = *( focusableActors.begin() );
1012               }
1013               else
1014               {
1015                 nextFocusableActor = *( iter + 1 );
1016               }
1017             }
1018
1019             if ( *iter == mContent && !mButtons.empty() )
1020             {
1021               nextFocusableActor = mButtons[0];
1022             }
1023             break;
1024           }
1025         }
1026
1027         if(!nextFocusableActor)
1028         {
1029           DALI_LOG_WARNING("Can not decide next focusable actor\n");
1030         }
1031
1032         break;
1033       }
1034     }
1035   }
1036
1037   return nextFocusableActor;
1038 }
1039
1040 } // namespace Internal
1041
1042 } // namespace Toolkit
1043
1044 } // namespace Dali