Initial removal of Text features
[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 }
301
302 const std::string& Popup::GetTitle() const
303 {
304   static std::string temp("");
305   return temp;
306 }
307
308 void Popup::AddButton( Toolkit::Button button )
309 {
310   mButtons.push_back( button );
311   mBottomBg.Add( button );
312
313   RelayoutRequest();
314 }
315
316 void Popup::SetState( Toolkit::Popup::PopupState state )
317 {
318   SetState( state, POPUP_ANIMATION_DURATION );
319 }
320
321 void Popup::SetState( Toolkit::Popup::PopupState state, float duration )
322 {
323   // default animation behaviour.
324   HandleStateChange(state, duration);
325 }
326
327 Toolkit::Popup::PopupState Popup::GetState() const
328 {
329   return mState;
330 }
331
332 void Popup::ShowTail(const Vector3& position)
333 {
334   // Replaces the tail actor.
335   if(mTailImage && mTailImage.GetParent())
336   {
337     mTailImage.GetParent().Remove( mTailImage );
338     mTailImage.Reset();
339   }
340
341   std::string image = "";
342
343   // depending on position of tail around ParentOrigin, a different tail image is used...
344   if(position.y < Math::MACHINE_EPSILON_1)
345   {
346     image = mPopupStyle->tailUpImage;
347   }
348   else if(position.y > 1.0f - Math::MACHINE_EPSILON_1)
349   {
350     image = mPopupStyle->tailDownImage;
351   }
352   else if(position.x < Math::MACHINE_EPSILON_1)
353   {
354     image = mPopupStyle->tailLeftImage;
355   }
356   else if(position.x > 1.0f - Math::MACHINE_EPSILON_1)
357   {
358     image = mPopupStyle->tailRightImage;
359   }
360
361   if(image != "")
362   {
363     Image tail = Image::New( image );
364     mTailImage = ImageActor::New(tail);
365     const Vector3 anchorPoint = AnchorPoint::FRONT_BOTTOM_RIGHT - position;
366
367     mTailImage.SetParentOrigin(position);
368     mTailImage.SetAnchorPoint(anchorPoint);
369
370     mBottomBg.Add(mTailImage);
371   }
372 }
373
374 void Popup::HideTail()
375 {
376   ShowTail(ParentOrigin::CENTER);
377 }
378
379 void Popup::SetStyle(PopupStyle& style)
380 {
381   mPopupStyle = PopupStylePtr(&style);
382   // update //
383 }
384
385 PopupStylePtr Popup::GetStyle() const
386 {
387   return mPopupStyle;
388 }
389
390 void Popup::SetDefaultBackgroundImage()
391 {
392   Image bg = Image::New( mPopupStyle->backgroundImage );
393   ImageActor bgImage = ImageActor::New( bg );
394   bgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
395   bgImage.SetNinePatchBorder( mPopupStyle->backgroundScale9Border );
396
397   Image buttonBg = Image::New( mPopupStyle->buttonAreaImage );
398   ImageActor buttonBgImage = ImageActor::New( buttonBg );
399   buttonBgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
400   buttonBgImage.SetNinePatchBorder( mPopupStyle->buttonArea9PatchBorder );
401
402   SetBackgroundImage( bgImage );
403   SetButtonAreaImage( buttonBgImage );
404 }
405
406 void Popup::CreateBacking()
407 {
408   mBacking = Dali::Toolkit::CreateSolidColorActor( mPopupStyle->backingColor );
409
410   mBacking.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
411   mBacking.SetSensitive(true);
412
413   mLayer.Add(mBacking);
414   mBacking.SetOpacity(0.0f);
415   mBacking.SetPosition(0.0f, 0.0f, BACKING_DEPTH);
416   mBacking.TouchedSignal().Connect( this, &Popup::OnBackingTouched );
417   mBacking.MouseWheelEventSignal().Connect(this, &Popup::OnBackingMouseWheelEvent);
418 }
419
420 void Popup::CreateDialog()
421 {
422   // Adds default background image.
423   SetDefaultBackgroundImage();
424
425   // Adds bottom background
426   mBottomBg = Actor::New();
427   mPopupBg.Add( mBottomBg );
428 }
429
430 void Popup::HandleStateChange( Toolkit::Popup::PopupState state, float duration )
431 {
432   const Vector2& stageSize( Stage::GetCurrent().GetSize() );
433
434   Vector3 targetSize;
435   float targetBackingAlpha;
436   Vector3 targetBackingSize;
437
438   if(mState == state)
439   {
440     return;
441   }
442   mState = state;
443   switch(state)
444   {
445     case Toolkit::Popup::POPUP_HIDE:
446     {
447       targetSize = Vector3(0.0f, 0.0f, 1.0f);
448       targetBackingAlpha = 0.0f;
449       targetBackingSize = Vector3(0.0f, 0.0f, 1.0f);
450       mShowing = false;
451       ClearKeyInputFocus();
452
453       // Retore the keyboard focus when popup is hidden
454       if(mPreviousFocusedActor && mPreviousFocusedActor.IsKeyboardFocusable() )
455       {
456         Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
457         if( keyboardFocusManager )
458         {
459           keyboardFocusManager.SetCurrentFocusActor(mPreviousFocusedActor);
460         }
461       }
462
463       break;
464     }
465
466     case Toolkit::Popup::POPUP_SHOW:
467     default:
468     {
469       targetSize = Vector3(1.0f, 1.0f, 1.0f);
470       targetBackingAlpha = 1.0f;
471       float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
472       targetBackingSize = Vector3( length, length, 1.0f );
473       mShowing = true;
474
475       // Add contents to stage for showing.
476       if( !mLayer.GetParent() )
477       {
478         mAlterAddedChild = false;
479         Self().Add(mLayer);
480         mAlterAddedChild = true;
481       }
482       Self().SetSensitive(true);
483       SetKeyInputFocus();
484
485       // Handle the keyboard focus when popup is shown
486       Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
487       if( keyboardFocusManager )
488       {
489         mPreviousFocusedActor = keyboardFocusManager.GetCurrentFocusActor();
490
491         if( mContent && mContent.IsKeyboardFocusable() )
492         {
493           // If content is focusable, move the focus to content
494           keyboardFocusManager.SetCurrentFocusActor(mContent);
495         }
496         else if( !mButtons.empty() )
497         {
498           // Otherwise, movethe focus to the first button
499           keyboardFocusManager.SetCurrentFocusActor(mButtons[0]);
500         }
501         else
502         {
503           DALI_LOG_WARNING("There is no focusable in popup\n");
504         }
505       }
506       break;
507     }
508   }
509
510   mBacking.SetSize( targetBackingSize );
511
512   if(duration > Math::MACHINE_EPSILON_1)
513   {
514     if ( mAnimation )
515     {
516       mAnimation.Stop();
517       mAnimation.Clear();
518       mAnimation.Reset();
519     }
520     mAnimation = Animation::New(duration);
521
522     if(mShowing)
523     {
524       mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
525       mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(duration * 0.5f, duration * 0.5f) );
526     }
527     else
528     {
529       mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
530       mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
531     }
532     mAnimation.Play();
533     mAnimation.FinishedSignal().Connect(this, &Popup::OnStateAnimationFinished);
534   }
535   else
536   {
537     mBacking.SetOpacity( targetBackingAlpha );
538     mPopupBg.SetScale( targetSize );
539
540     HandleStateChangeComplete();
541   }
542 }
543
544 void Popup::HandleStateChangeComplete()
545 {
546   // Remove contents from stage if completely hidden.
547   if( (mState == Toolkit::Popup::POPUP_HIDE) && (mLayer.GetParent()) )
548   {
549     Self().Remove(mLayer);
550     Self().SetSensitive( false );
551
552     // Guard against destruction during signal emission
553     Toolkit::Popup handle( GetOwner() );
554     mHiddenSignalV2.Emit();
555   }
556 }
557
558 Toolkit::Popup::TouchedOutsideSignalV2& Popup::OutsideTouchedSignal()
559 {
560   return mTouchedOutsideSignalV2;
561 }
562
563 Toolkit::Popup::HiddenSignalV2& Popup::HiddenSignal()
564 {
565   return mHiddenSignalV2;
566 }
567
568 bool Popup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
569 {
570   Dali::BaseHandle handle( object );
571
572   bool connected( true );
573   Toolkit::Popup popup = Toolkit::Popup::DownCast(handle);
574
575   if( Dali::Toolkit::Popup::SIGNAL_TOUCHED_OUTSIDE == signalName )
576   {
577     popup.OutsideTouchedSignal().Connect( tracker, functor );
578   }
579   else if( Dali::Toolkit::Popup::SIGNAL_HIDDEN == signalName )
580   {
581     popup.HiddenSignal().Connect( tracker, functor );
582   }
583   else
584   {
585     // signalName does not match any signal
586     connected = false;
587   }
588
589   return connected;
590 }
591
592 void Popup::OnStateAnimationFinished( Animation& source )
593 {
594   HandleStateChangeComplete();
595 }
596
597 bool Popup::OnBackingTouched(Actor actor, const TouchEvent& event)
598 {
599   if(event.GetPointCount()>0)
600   {
601     const TouchPoint& point = event.GetPoint(0);
602
603     if(point.state == TouchPoint::Down)
604     {
605       // Guard against destruction during signal emission
606       Toolkit::Popup handle( GetOwner() );
607
608       mTouchedOutsideSignalV2.Emit();
609     }
610   }
611
612   return true;
613 }
614
615 bool Popup::OnBackingMouseWheelEvent(Actor actor, const MouseWheelEvent& event)
616 {
617   // consume mouse wheel event in dimmed backing actor
618   return true;
619 }
620
621 bool Popup::OnDialogTouched(Actor actor, const TouchEvent& event)
622 {
623   // consume event (stops backing actor receiving touch events)
624   return true;
625 }
626
627 void Popup::OnControlChildAdd( Actor& child )
628 {
629   // reparent any children added by user to the body layer.
630   if( mAlterAddedChild )
631   {
632     // Removes previously added content.
633     if( mContent )
634     {
635       mPopupBg.Remove( mContent );
636     }
637
638     // Reparent new content.
639     Self().Remove( child );
640
641     // keep a handle to the new content.
642     mContent = child;
643
644     mPopupBg.Add( mContent );
645   }
646 }
647
648 void Popup::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
649 {
650   // Set the popup size
651   Vector2 popupSize;
652   popupSize.width  = size.width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
653   popupSize.height = size.height - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
654
655   // Update sizes of all popup's components.
656
657   // Relayout background image.
658   // Adjust background position and size relative to parent to cater to outer Border.
659   // Some backgrounds are intended to over-spill. That is some content
660   // should appear outside the Dialog on all sides i.e. Shadows, glow effects.
661   const Vector4 outerBorder = mPopupStyle->backgroundOuterBorder;
662
663   if( mBackgroundImage )
664   {
665     Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
666                                                       ParentSource( Actor::SIZE ),
667                                                       BackgroundSizeConstraint(outerBorder) );
668
669     mBackgroundImage.RemoveConstraints();
670     mBackgroundImage.ApplyConstraint( constraint );
671
672     mBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
673     mBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
674     mBackgroundImage.SetPosition( -outerBorder.x, -outerBorder.y, 0.0f );
675   }
676
677   if( mPopupBg && mButtonAreaImage )
678   {
679     // If there are no buttons, button background is also removed.
680     if ( mButtons.size() == 0 )
681     {
682       mPopupBg.Remove( mButtonAreaImage );
683     }
684     else
685     {
686       Constraint constraint = Constraint::New<Vector3>( Actor::SIZE,
687                                                       ParentSource( Actor::SIZE ),
688                                                       ButtonAreaSizeConstraint(outerBorder) );
689
690       mButtonAreaImage.RemoveConstraints();
691       mButtonAreaImage.ApplyConstraint( constraint );
692
693       mButtonAreaImage.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
694       mButtonAreaImage.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
695       mButtonAreaImage.SetY( -outerBorder.z - POPUP_OUT_MARGIN_HEIGHT );
696
697       mPopupBg.Add( mButtonAreaImage );
698     }
699   }
700
701   // Relayout title
702   Vector3 positionOffset( 0.0f, mPopupStyle->margin + POPUP_OUT_MARGIN_WIDTH, CONTENT_DEPTH );
703   // TODO
704
705   // Relayout content
706   if( mContent )
707   {
708     // If the content width is greater than popup width then scale it down/wrap text as needed
709     Vector2 contentSize( RelayoutHelper::GetNaturalSize( mContent ) );
710     if( contentSize.width > popupSize.width )
711     {
712       contentSize.width = popupSize.width;
713       contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, contentSize.width );
714     }
715
716     mContent.SetSize( contentSize );
717     Relayout( mContent, contentSize, container );
718
719     mContent.SetParentOrigin(ParentOrigin::TOP_CENTER);
720     mContent.SetAnchorPoint(AnchorPoint::TOP_CENTER);
721
722     mContent.SetPosition( positionOffset );
723
724     positionOffset.y += contentSize.height + mPopupStyle->margin;
725   }
726
727   // Relayout Button Area
728   if( mBottomBg )
729   {
730     mBottomBg.SetSize( popupSize.width, mPopupStyle->bottomSize.height );
731
732     mBottomBg.SetParentOrigin(ParentOrigin::TOP_CENTER);
733     mBottomBg.SetAnchorPoint(AnchorPoint::TOP_CENTER);
734
735     mBottomBg.SetPosition( positionOffset );
736   }
737
738   // Relayout All buttons
739   if ( !mButtons.empty() )
740   {
741     // All buttons should be the same size and fill the button area. The button spacing needs to be accounted for as well.
742     Vector2 buttonSize( ( ( popupSize.width - mPopupStyle->buttonSpacing * ( mButtons.size() - 1 ) ) / mButtons.size() ),
743                         mPopupStyle->bottomSize.height - mPopupStyle->margin );
744
745     Vector3 buttonPosition;
746
747     for ( ActorIter iter = mButtons.begin(), endIter = mButtons.end();
748           iter != endIter;
749           ++iter, buttonPosition.x += mPopupStyle->buttonSpacing + buttonSize.width )
750     {
751       iter->SetPosition( buttonPosition );
752
753       // If there is only one button, it needs to be laid out on center.
754       if ( mButtons.size() == 1 )
755       {
756         iter->SetAnchorPoint( AnchorPoint::CENTER );
757         iter->SetParentOrigin( ParentOrigin::CENTER );
758       }
759       else
760       {
761         iter->SetAnchorPoint( AnchorPoint::CENTER_LEFT );
762         iter->SetParentOrigin( ParentOrigin::CENTER_LEFT );
763       }
764
765       Relayout( *iter, buttonSize, container );
766     }
767   }
768
769   if( mShowing && mBacking )
770   {
771     Vector2 stageSize = Stage::GetCurrent().GetSize();
772     float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
773     Vector3 targetBackingSize = Vector3( length, length, 1.0f );
774
775     mBacking.SetSize( targetBackingSize );
776   }
777 }
778
779 bool Popup::OnKeyEvent(const KeyEvent& event)
780 {
781   bool consumed = false;
782
783   if(event.state == KeyEvent::Down)
784   {
785     if (event.keyCode == Dali::DALI_KEY_ESCAPE || event.keyCode == Dali::DALI_KEY_BACK)
786     {
787       SetState(Toolkit::Popup::POPUP_HIDE);
788       consumed = true;
789     }
790   }
791
792   return consumed;
793 }
794
795 Vector3 Popup::GetNaturalSize()
796 {
797   float margin = 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
798
799   Vector3 naturalSize( 0.0f, 0.0f, 0.0f );
800
801   if( mContent )
802   {
803     Vector3 contentSize = RelayoutHelper::GetNaturalSize( mContent );
804     // Choose the biggest width
805     naturalSize.width = std::max( naturalSize.width, contentSize.width );
806     naturalSize.height += contentSize.height + mPopupStyle->margin;
807   }
808
809   if( !mButtons.empty() )
810   {
811     naturalSize.height += mPopupStyle->bottomSize.height;
812   }
813
814   // Add the margins
815   naturalSize.width += margin;
816   naturalSize.height += margin;
817
818   return naturalSize;
819 }
820
821 float Popup::GetHeightForWidth( float width )
822 {
823   float height( 0.0f );
824   float popupWidth( width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
825
826   if( mContent )
827   {
828     height += RelayoutHelper::GetHeightForWidth( mContent, popupWidth ) + mPopupStyle->margin;
829   }
830
831   if( !mButtons.empty() )
832   {
833     height += mPopupStyle->bottomSize.height;
834   }
835
836   // Add the margins
837   float margin( 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
838   height += margin;
839
840   return height;
841 }
842
843 float Popup::GetWidthForHeight( float height )
844 {
845   return GetNaturalSize().width;
846 }
847
848 Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
849 {
850   Actor nextFocusableActor( currentFocusedActor );
851
852   // TODO: Needs to be optimised
853
854   if ( !currentFocusedActor || ( currentFocusedActor && KeyboardFocusManager::Get().GetFocusGroup(currentFocusedActor) != Self() ) )
855   {
856     // The current focused actor is not within popup
857     if( mContent && mContent.IsKeyboardFocusable() )
858     {
859       // If content is focusable, move the focus to content
860       nextFocusableActor = mContent;
861     }
862     else if( !mButtons.empty() )
863     {
864       // Otherwise, movethe focus to the first button
865       nextFocusableActor = mButtons[0];
866     }
867   }
868   else
869   {
870     // Rebuild the focus chain because button or content can be added or removed dynamically
871     ActorContainer focusableActors;
872     if( mContent && mContent.IsKeyboardFocusable() )
873     {
874       focusableActors.push_back(mContent);
875     }
876
877     for(unsigned int i = 0; i < mButtons.size(); i++)
878     {
879       if( mButtons[i] && mButtons[i].IsKeyboardFocusable() )
880       {
881         focusableActors.push_back(mButtons[i]);
882       }
883     }
884
885     for ( ActorContainer::iterator iter = focusableActors.begin(), end = focusableActors.end(); iter != end; ++iter )
886     {
887       if ( currentFocusedActor == *iter )
888       {
889         switch ( direction )
890         {
891           case Toolkit::Control::Left:
892           {
893             if ( iter == focusableActors.begin() )
894             {
895               nextFocusableActor = *( focusableActors.end() - 1 );
896             }
897             else
898             {
899               nextFocusableActor = *( iter - 1 );
900             }
901             break;
902           }
903           case Toolkit::Control::Right:
904           {
905             if ( iter == focusableActors.end() - 1 )
906             {
907               nextFocusableActor = *( focusableActors.begin() );
908             }
909             else
910             {
911               nextFocusableActor = *( iter + 1 );
912             }
913             break;
914           }
915
916           case Toolkit::Control::Up:
917           {
918             if ( *iter == mContent )
919             {
920               nextFocusableActor = *( focusableActors.end() - 1 );
921             }
922             else
923             {
924               if ( mContent && mContent.IsKeyboardFocusable() )
925               {
926                 nextFocusableActor = mContent;
927               }
928               else
929               {
930                 if ( iter == focusableActors.begin() )
931                 {
932                   nextFocusableActor = *( focusableActors.end() - 1 );
933                 }
934                 else
935                 {
936                   nextFocusableActor = *( iter - 1 );
937                 }
938               }
939             }
940             break;
941           }
942
943           case Toolkit::Control::Down:
944           {
945             if ( mContent && mContent.IsKeyboardFocusable() )
946             {
947               nextFocusableActor = mContent;
948             }
949             else
950             {
951               if ( iter == focusableActors.end() - 1 )
952               {
953                 nextFocusableActor = *( focusableActors.begin() );
954               }
955               else
956               {
957                 nextFocusableActor = *( iter + 1 );
958               }
959             }
960
961             if ( *iter == mContent && !mButtons.empty() )
962             {
963               nextFocusableActor = mButtons[0];
964             }
965             break;
966           }
967         }
968
969         if(!nextFocusableActor)
970         {
971           DALI_LOG_WARNING("Can not decide next focusable actor\n");
972         }
973
974         break;
975       }
976     }
977   }
978
979   return nextFocusableActor;
980 }
981
982 } // namespace Internal
983
984 } // namespace Toolkit
985
986 } // namespace Dali