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