Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-toolkit.git] / 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 #include <dali/public-api/images/resource-image.h>
31
32 // INTERNAL INCLUDES
33 #include <dali-toolkit/public-api/controls/buttons/button.h>
34 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
35 #include <dali-toolkit/public-api/controls/control-impl.h>
36 #include <dali-toolkit/internal/controls/relayout-helper.h>
37 #include <dali-toolkit/internal/focus-manager/keyboard-focus-manager-impl.h>
38 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
39
40 using namespace Dali;
41
42 namespace
43 {
44 const float CONTENT_DEPTH = 1.0f;                                 ///< 3D Effect of buttons/title etc. appearing off the popup.
45 const float POPUP_ANIMATION_DURATION = 0.5f;                      ///< Duration of hide/show animations
46 const float BACKING_DEPTH = -1.0f;                                ///< Depth of backing (positioned just behind dialog, so dialog catches hit events first)
47
48 const float POPUP_WIDTH = 720.0f;                             ///< Width of Popup
49 const float POPUP_OUT_MARGIN_WIDTH = 16.f;                    ///< Space between the screen edge and the popup edge in the horizontal dimension.
50 const float POPUP_OUT_MARGIN_HEIGHT = 36.f;                   ///< Space between the screen edge and the popup edge in the vertical dimension.
51 const float POPUP_TITLE_WIDTH = 648.0f;                       ///<Width of Popup Title
52 const float POPUP_BUTTON_BG_HEIGHT = 96.f;                    ///< Height of Button Background.
53 const Vector3 DEFAULT_DIALOG_SIZE = Vector3(POPUP_TITLE_WIDTH/POPUP_WIDTH, 0.5f, 0.0f);
54 const Vector3 DEFAULT_BOTTOM_SIZE = Vector3(1.0f, 0.2f, 0.0f);
55
56 // Signals
57
58 const char* const SIGNAL_TOUCHED_OUTSIDE = "touched-outside";
59 const char* const SIGNAL_HIDDEN =          "hidden";
60
61 // Properties
62
63 const char* const PROPERTY_TITLE =         "title";
64 const char* const PROPERTY_STATE =         "state";
65
66 /**
67  * The background size should be at least as big as the Dialog.
68  * In some cases a background may have graphics which are visible
69  * outside of the Dialog, e.g. A Shadow. For this we need to alter
70  * the size of Background.
71  *
72  * @param[in] outerBorder The border to extend beyond parent's Size.
73  * @param[in] parentSize  The parent's size
74  */
75 Vector3 BackgroundSize(const Vector4& outerBoarder, const Vector3& parentSize)
76 {
77   Vector3 size( parentSize );
78   size.width += outerBoarder.x + outerBoarder.y;
79   size.height += outerBoarder.z + outerBoarder.w;
80
81   return size;
82 }
83
84   /**
85    * sets button area size to parent's size plus a border.
86    *
87    * @param[in] outerBorder The border to extend beyond parent's Size.
88    * @param[in] parentSize  The parent's size
89    */
90 Vector3 ButtonAreaSize( const Vector4& outBoarder, const Vector3& parentSize )
91 {
92   Vector3 size( parentSize );
93   size.width += outBoarder.x + outBoarder.y;
94   size.width -= (POPUP_OUT_MARGIN_WIDTH + POPUP_OUT_MARGIN_WIDTH);
95   size.height = POPUP_BUTTON_BG_HEIGHT;
96
97   return size;
98 }
99
100 } // unnamed namespace
101
102 namespace Dali
103 {
104
105 namespace Toolkit
106 {
107
108 namespace Internal
109 {
110
111 namespace
112 {
113
114 BaseHandle Create()
115 {
116   return Toolkit::Popup::New();
117 }
118
119 TypeRegistration typeRegistration( typeid( Toolkit::Popup ), typeid( Toolkit::Control ), Create );
120
121 SignalConnectorType signalConnector1( typeRegistration, SIGNAL_TOUCHED_OUTSIDE, &Popup::DoConnectSignal );
122 SignalConnectorType signalConnector2( typeRegistration, SIGNAL_HIDDEN, &Popup::DoConnectSignal );
123
124
125 }
126
127
128 ///////////////////////////////////////////////////////////////////////////////////////////////////
129 // Popup
130 ///////////////////////////////////////////////////////////////////////////////////////////////////
131
132 Dali::Toolkit::Popup Popup::New()
133 {
134   PopupStylePtr style = PopupStyleDefault::New();
135
136   // Create the implementation
137   PopupPtr popup(new Popup(*style));
138
139   // Pass ownership to CustomActor via derived handle
140   Dali::Toolkit::Popup handle(*popup);
141
142   // Second-phase init of the implementation
143   // This can only be done after the CustomActor connection has been made...
144   popup->Initialize();
145
146   return handle;
147 }
148
149 Popup::Popup(PopupStyle& style)
150 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
151   mShowing(false),
152   mState(Toolkit::Popup::POPUP_NONE), // Initially, the popup state should not be set, it's set in OnInitialize
153   mAlterAddedChild(false),
154   mPopupStyle(PopupStylePtr(&style)),
155   mPropertyTitle(Property::INVALID_INDEX),
156   mPropertyState(Property::INVALID_INDEX)
157 {
158   SetKeyboardNavigationSupport( true );
159 }
160
161 void Popup::OnInitialize()
162 {
163   Actor self = Self();
164   self.SetSensitive(false);
165
166   // Create Layer
167   mLayer = Layer::New();
168   mLayer.SetParentOrigin(ParentOrigin::CENTER);
169   mLayer.SetAnchorPoint(AnchorPoint::CENTER);
170   mLayer.RaiseToTop();
171   self.Add(mLayer);
172
173   mPopupBg = Actor::New();
174   mPopupBg.SetParentOrigin(ParentOrigin::CENTER);
175   mPopupBg.SetAnchorPoint(AnchorPoint::CENTER);
176   mLayer.Add(mPopupBg);
177
178   // Any content after this point which is added to Self() will be reparented to
179   // mContent.
180   mAlterAddedChild = true;
181
182   // Add Backing (Dim effect)
183   CreateBacking();
184
185   // Add Dialog ( background image, title, content container, button container and tail )
186   CreateDialog();
187
188   // Default content.
189   ShowTail(ParentOrigin::BOTTOM_CENTER);
190
191   // Hide content by default.
192   SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
193
194   mPropertyTitle = self.RegisterProperty( PROPERTY_TITLE, "", Property::READ_WRITE );
195   mPropertyState = self.RegisterProperty( PROPERTY_STATE, "POPUP_HIDE", Property::READ_WRITE );
196
197   // Make self as keyboard focusable and focus group
198   self.SetKeyboardFocusable(true);
199   SetAsKeyboardFocusGroup(true);
200 }
201
202 void Popup::OnPropertySet( Property::Index index, Property::Value propertyValue )
203 {
204   if( index == mPropertyTitle )
205   {
206     SetTitle(propertyValue.Get<std::string>());
207   }
208   else if ( index == mPropertyState )
209   {
210     std::string value( propertyValue.Get<std::string>() );
211     if(value == "POPUP_SHOW")
212     {
213       SetState( Toolkit::Popup::POPUP_SHOW, 0.0f );
214     }
215     else if( value == "POPUP_HIDE")
216     {
217       SetState( Toolkit::Popup::POPUP_HIDE, 0.0f );
218     }
219   }
220 }
221
222 Popup::~Popup()
223 {
224 }
225
226 size_t Popup::GetButtonCount() const
227 {
228   return mButtons.size();
229 }
230
231 void Popup::SetBackgroundImage( Actor image )
232 {
233   // Removes any previous background.
234   if( mBackgroundImage && mPopupBg )
235   {
236     mPopupBg.Remove( mBackgroundImage );
237   }
238
239   // Adds new background to the dialog.
240   mBackgroundImage = image;
241
242   // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
243   mBackgroundImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
244
245   mPopupBg.Add( mBackgroundImage );
246 }
247
248 void Popup::SetButtonAreaImage( Actor image )
249 {
250   // Removes any previous area image.
251   if( mButtonAreaImage && mPopupBg )
252   {
253     mPopupBg.Remove( mButtonAreaImage );
254   }
255
256   // Adds new area image to the dialog.
257   mButtonAreaImage = image;
258
259   // OnDialogTouched only consume the event. It prevents the touch event to be caught by the backing.
260   mButtonAreaImage.TouchedSignal().Connect( this, &Popup::OnDialogTouched );
261
262   mPopupBg.Add( mButtonAreaImage );
263 }
264
265 void Popup::SetTitle( const std::string& text )
266 {
267 }
268
269 const std::string& Popup::GetTitle() const
270 {
271   static std::string temp("");
272   return temp;
273 }
274
275 void Popup::AddButton( Toolkit::Button button )
276 {
277   mButtons.push_back( button );
278   mBottomBg.Add( button );
279
280   RelayoutRequest();
281 }
282
283 void Popup::SetState( Toolkit::Popup::PopupState state )
284 {
285   SetState( state, POPUP_ANIMATION_DURATION );
286 }
287
288 void Popup::SetState( Toolkit::Popup::PopupState state, float duration )
289 {
290   // default animation behaviour.
291   HandleStateChange(state, duration);
292 }
293
294 Toolkit::Popup::PopupState Popup::GetState() const
295 {
296   return mState;
297 }
298
299 void Popup::ShowTail(const Vector3& position)
300 {
301   // Replaces the tail actor.
302   if(mTailImage && mTailImage.GetParent())
303   {
304     mTailImage.GetParent().Remove( mTailImage );
305     mTailImage.Reset();
306   }
307
308   std::string image = "";
309
310   // depending on position of tail around ParentOrigin, a different tail image is used...
311   if(position.y < Math::MACHINE_EPSILON_1)
312   {
313     image = mPopupStyle->tailUpImage;
314   }
315   else if(position.y > 1.0f - Math::MACHINE_EPSILON_1)
316   {
317     image = mPopupStyle->tailDownImage;
318   }
319   else if(position.x < Math::MACHINE_EPSILON_1)
320   {
321     image = mPopupStyle->tailLeftImage;
322   }
323   else if(position.x > 1.0f - Math::MACHINE_EPSILON_1)
324   {
325     image = mPopupStyle->tailRightImage;
326   }
327
328   if(image != "")
329   {
330     Image tail = ResourceImage::New( image );
331     mTailImage = ImageActor::New(tail);
332     const Vector3 anchorPoint = AnchorPoint::FRONT_BOTTOM_RIGHT - position;
333
334     mTailImage.SetParentOrigin(position);
335     mTailImage.SetAnchorPoint(anchorPoint);
336
337     mBottomBg.Add(mTailImage);
338   }
339 }
340
341 void Popup::HideTail()
342 {
343   ShowTail(ParentOrigin::CENTER);
344 }
345
346 void Popup::SetStyle(PopupStyle& style)
347 {
348   mPopupStyle = PopupStylePtr(&style);
349   // update //
350 }
351
352 PopupStylePtr Popup::GetStyle() const
353 {
354   return mPopupStyle;
355 }
356
357 void Popup::SetDefaultBackgroundImage()
358 {
359   Image bg = ResourceImage::New( mPopupStyle->backgroundImage );
360   ImageActor bgImage = ImageActor::New( bg );
361   bgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
362   bgImage.SetNinePatchBorder( mPopupStyle->backgroundScale9Border );
363
364   Image buttonBg = ResourceImage::New( mPopupStyle->buttonAreaImage );
365   ImageActor buttonBgImage = ImageActor::New( buttonBg );
366   buttonBgImage.SetStyle( ImageActor::STYLE_NINE_PATCH );
367   buttonBgImage.SetNinePatchBorder( mPopupStyle->buttonArea9PatchBorder );
368
369   SetBackgroundImage( bgImage );
370   SetButtonAreaImage( buttonBgImage );
371 }
372
373 void Popup::CreateBacking()
374 {
375   mBacking = Dali::Toolkit::CreateSolidColorActor( mPopupStyle->backingColor );
376
377   mBacking.SetPositionInheritanceMode(DONT_INHERIT_POSITION);
378   mBacking.SetSensitive(true);
379
380   mLayer.Add(mBacking);
381   mBacking.SetOpacity(0.0f);
382   mBacking.SetPosition(0.0f, 0.0f, BACKING_DEPTH);
383   mBacking.TouchedSignal().Connect( this, &Popup::OnBackingTouched );
384   mBacking.MouseWheelEventSignal().Connect(this, &Popup::OnBackingMouseWheelEvent);
385 }
386
387 void Popup::CreateDialog()
388 {
389   // Adds default background image.
390   SetDefaultBackgroundImage();
391
392   // Adds bottom background
393   mBottomBg = Actor::New();
394   mPopupBg.Add( mBottomBg );
395 }
396
397 void Popup::HandleStateChange( Toolkit::Popup::PopupState state, float duration )
398 {
399   const Vector2& stageSize( Stage::GetCurrent().GetSize() );
400
401   Vector3 targetSize;
402   float targetBackingAlpha;
403   Vector3 targetBackingSize;
404
405   if(mState == state)
406   {
407     return;
408   }
409   mState = state;
410   switch(state)
411   {
412     case Toolkit::Popup::POPUP_HIDE:
413     {
414       targetSize = Vector3(0.0f, 0.0f, 1.0f);
415       targetBackingAlpha = 0.0f;
416       targetBackingSize = Vector3(0.0f, 0.0f, 1.0f);
417       mShowing = false;
418       ClearKeyInputFocus();
419
420       // Retore the keyboard focus when popup is hidden
421       if(mPreviousFocusedActor && mPreviousFocusedActor.IsKeyboardFocusable() )
422       {
423         Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
424         if( keyboardFocusManager )
425         {
426           keyboardFocusManager.SetCurrentFocusActor(mPreviousFocusedActor);
427         }
428       }
429
430       break;
431     }
432
433     case Toolkit::Popup::POPUP_SHOW:
434     default:
435     {
436       targetSize = Vector3(1.0f, 1.0f, 1.0f);
437       targetBackingAlpha = 1.0f;
438       float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
439       targetBackingSize = Vector3( length, length, 1.0f );
440       mShowing = true;
441
442       // Add contents to stage for showing.
443       if( !mLayer.GetParent() )
444       {
445         mAlterAddedChild = false;
446         Self().Add(mLayer);
447         mAlterAddedChild = true;
448       }
449       Self().SetSensitive(true);
450       SetKeyInputFocus();
451
452       // Handle the keyboard focus when popup is shown
453       Dali::Toolkit::KeyboardFocusManager keyboardFocusManager = Dali::Toolkit::KeyboardFocusManager::Get();
454       if( keyboardFocusManager )
455       {
456         mPreviousFocusedActor = keyboardFocusManager.GetCurrentFocusActor();
457
458         if( mContent && mContent.IsKeyboardFocusable() )
459         {
460           // If content is focusable, move the focus to content
461           keyboardFocusManager.SetCurrentFocusActor(mContent);
462         }
463         else if( !mButtons.empty() )
464         {
465           // Otherwise, movethe focus to the first button
466           keyboardFocusManager.SetCurrentFocusActor(mButtons[0]);
467         }
468         else
469         {
470           DALI_LOG_WARNING("There is no focusable in popup\n");
471         }
472       }
473       break;
474     }
475   }
476
477   mBacking.SetSize( targetBackingSize );
478
479   if(duration > Math::MACHINE_EPSILON_1)
480   {
481     if ( mAnimation )
482     {
483       mAnimation.Stop();
484       mAnimation.Clear();
485       mAnimation.Reset();
486     }
487     mAnimation = Animation::New(duration);
488
489     if(mShowing)
490     {
491       mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
492       mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(duration * 0.5f, duration * 0.5f) );
493     }
494     else
495     {
496       mAnimation.AnimateTo( Property(mBacking, Actor::COLOR_ALPHA), targetBackingAlpha, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
497       mAnimation.AnimateTo( Property(mPopupBg, Actor::SCALE), targetSize, AlphaFunctions::EaseInOut, TimePeriod(0.0f, duration * 0.5f) );
498     }
499     mAnimation.Play();
500     mAnimation.FinishedSignal().Connect(this, &Popup::OnStateAnimationFinished);
501   }
502   else
503   {
504     mBacking.SetOpacity( targetBackingAlpha );
505     mPopupBg.SetScale( targetSize );
506
507     HandleStateChangeComplete();
508   }
509 }
510
511 void Popup::HandleStateChangeComplete()
512 {
513   // Remove contents from stage if completely hidden.
514   if( (mState == Toolkit::Popup::POPUP_HIDE) && (mLayer.GetParent()) )
515   {
516     Self().Remove(mLayer);
517     Self().SetSensitive( false );
518
519     // Guard against destruction during signal emission
520     Toolkit::Popup handle( GetOwner() );
521     mHiddenSignal.Emit();
522   }
523 }
524
525 Toolkit::Popup::TouchedOutsideSignalType& Popup::OutsideTouchedSignal()
526 {
527   return mTouchedOutsideSignal;
528 }
529
530 Toolkit::Popup::HiddenSignalType& Popup::HiddenSignal()
531 {
532   return mHiddenSignal;
533 }
534
535 bool Popup::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
536 {
537   Dali::BaseHandle handle( object );
538
539   bool connected( true );
540   Toolkit::Popup popup = Toolkit::Popup::DownCast( handle );
541
542   if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED_OUTSIDE ) )
543   {
544     popup.OutsideTouchedSignal().Connect( tracker, functor );
545   }
546   else if( 0 == strcmp( signalName.c_str(), SIGNAL_HIDDEN ) )
547   {
548     popup.HiddenSignal().Connect( tracker, functor );
549   }
550   else
551   {
552     // signalName does not match any signal
553     connected = false;
554   }
555
556   return connected;
557 }
558
559 void Popup::OnStateAnimationFinished( Animation& source )
560 {
561   HandleStateChangeComplete();
562 }
563
564 bool Popup::OnBackingTouched(Actor actor, const TouchEvent& event)
565 {
566   if(event.GetPointCount()>0)
567   {
568     const TouchPoint& point = event.GetPoint(0);
569
570     if(point.state == TouchPoint::Down)
571     {
572       // Guard against destruction during signal emission
573       Toolkit::Popup handle( GetOwner() );
574
575       mTouchedOutsideSignal.Emit();
576     }
577   }
578
579   return true;
580 }
581
582 bool Popup::OnBackingMouseWheelEvent(Actor actor, const MouseWheelEvent& event)
583 {
584   // consume mouse wheel event in dimmed backing actor
585   return true;
586 }
587
588 bool Popup::OnDialogTouched(Actor actor, const TouchEvent& event)
589 {
590   // consume event (stops backing actor receiving touch events)
591   return true;
592 }
593
594 void Popup::OnControlChildAdd( Actor& child )
595 {
596   // reparent any children added by user to the body layer.
597   if( mAlterAddedChild )
598   {
599     // Removes previously added content.
600     if( mContent )
601     {
602       mPopupBg.Remove( mContent );
603     }
604
605     // Reparent new content.
606     Self().Remove( child );
607
608     // keep a handle to the new content.
609     mContent = child;
610
611     mPopupBg.Add( mContent );
612   }
613 }
614
615 void Popup::OnControlSizeSet( const Vector3& targetSize )
616 {
617   mLayer.SetSize( targetSize );
618   mPopupBg.SetSize( targetSize );
619
620   const Vector4 outerBorder = mPopupStyle->backgroundOuterBorder;
621   if( mBackgroundImage )
622   {
623     mBackgroundImage.SetSize( BackgroundSize( outerBorder,targetSize ) );
624   }
625   if( mButtonAreaImage )
626   {
627     mButtonAreaImage.SetSize( ButtonAreaSize( outerBorder, targetSize ) );
628   }
629
630 }
631
632 void Popup::OnRelayout( const Vector2& size, ActorSizeContainer& container )
633 {
634   // Set the popup size
635   Vector2 popupSize;
636   popupSize.width  = size.width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
637   popupSize.height = size.height - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
638
639   // Update sizes of all popup's components.
640
641   // Relayout background image.
642   // Adjust background position and size relative to parent to cater to outer Border.
643   // Some backgrounds are intended to over-spill. That is some content
644   // should appear outside the Dialog on all sides i.e. Shadows, glow effects.
645   const Vector4 outerBorder = mPopupStyle->backgroundOuterBorder;
646
647   if( mBackgroundImage )
648   {
649     mBackgroundImage.SetSize(BackgroundSize(outerBorder, Vector3(size)));
650     mBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
651     mBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
652     mBackgroundImage.SetPosition( -outerBorder.x, -outerBorder.y, 0.0f );
653   }
654
655   if( mPopupBg && mButtonAreaImage )
656   {
657     // If there are no buttons, button background is also removed.
658     if ( mButtons.size() == 0 )
659     {
660       mPopupBg.Remove( mButtonAreaImage );
661     }
662     else
663     {
664       mButtonAreaImage.SetSize( ButtonAreaSize(outerBorder, Vector3(size)) );
665       mButtonAreaImage.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
666       mButtonAreaImage.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
667       mButtonAreaImage.SetY( -outerBorder.z - POPUP_OUT_MARGIN_HEIGHT );
668
669       mPopupBg.Add( mButtonAreaImage );
670     }
671   }
672
673   // Relayout title
674   Vector3 positionOffset( 0.0f, mPopupStyle->margin + POPUP_OUT_MARGIN_WIDTH, CONTENT_DEPTH );
675   // TODO
676
677   // Relayout content
678   if( mContent )
679   {
680     // If the content width is greater than popup width then scale it down/wrap text as needed
681     Vector2 contentSize( RelayoutHelper::GetNaturalSize( mContent ) );
682     if( contentSize.width > popupSize.width )
683     {
684       contentSize.width = popupSize.width;
685       contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, contentSize.width );
686     }
687
688     mContent.SetSize( contentSize );
689     Relayout( mContent, contentSize, container );
690
691     mContent.SetParentOrigin(ParentOrigin::TOP_CENTER);
692     mContent.SetAnchorPoint(AnchorPoint::TOP_CENTER);
693
694     mContent.SetPosition( positionOffset );
695
696     positionOffset.y += contentSize.height + mPopupStyle->margin;
697   }
698
699   // Relayout Button Area
700   if( mBottomBg )
701   {
702     mBottomBg.SetSize( popupSize.width, mPopupStyle->bottomSize.height );
703
704     mBottomBg.SetParentOrigin(ParentOrigin::TOP_CENTER);
705     mBottomBg.SetAnchorPoint(AnchorPoint::TOP_CENTER);
706
707     mBottomBg.SetPosition( positionOffset );
708   }
709
710   // Relayout All buttons
711   if ( !mButtons.empty() )
712   {
713     // All buttons should be the same size and fill the button area. The button spacing needs to be accounted for as well.
714     Vector2 buttonSize( ( ( popupSize.width - mPopupStyle->buttonSpacing * ( mButtons.size() - 1 ) ) / mButtons.size() ),
715                         mPopupStyle->bottomSize.height - mPopupStyle->margin );
716
717     Vector3 buttonPosition;
718
719     for ( ActorIter iter = mButtons.begin(), endIter = mButtons.end();
720           iter != endIter;
721           ++iter, buttonPosition.x += mPopupStyle->buttonSpacing + buttonSize.width )
722     {
723       iter->SetPosition( buttonPosition );
724
725       // If there is only one button, it needs to be laid out on center.
726       if ( mButtons.size() == 1 )
727       {
728         iter->SetAnchorPoint( AnchorPoint::CENTER );
729         iter->SetParentOrigin( ParentOrigin::CENTER );
730       }
731       else
732       {
733         iter->SetAnchorPoint( AnchorPoint::CENTER_LEFT );
734         iter->SetParentOrigin( ParentOrigin::CENTER_LEFT );
735       }
736
737       Relayout( *iter, buttonSize, container );
738     }
739   }
740
741   if( mShowing && mBacking )
742   {
743     Vector2 stageSize = Stage::GetCurrent().GetSize();
744     float length = (stageSize.width > stageSize.height) ? stageSize.width : stageSize.height;
745     Vector3 targetBackingSize = Vector3( length, length, 1.0f );
746
747     mBacking.SetSize( targetBackingSize );
748   }
749 }
750
751 bool Popup::OnKeyEvent(const KeyEvent& event)
752 {
753   bool consumed = false;
754
755   if(event.state == KeyEvent::Down)
756   {
757     if (event.keyCode == Dali::DALI_KEY_ESCAPE || event.keyCode == Dali::DALI_KEY_BACK)
758     {
759       SetState(Toolkit::Popup::POPUP_HIDE);
760       consumed = true;
761     }
762   }
763
764   return consumed;
765 }
766
767 Vector3 Popup::GetNaturalSize()
768 {
769   float margin = 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin );
770   const float maxWidth = Stage::GetCurrent().GetSize().width - margin;
771
772   Vector3 naturalSize( 0.0f, 0.0f, 0.0f );
773
774   if( mContent )
775   {
776     Vector3 contentSize = RelayoutHelper::GetNaturalSize( mContent );
777     // Choose the biggest width
778     naturalSize.width = std::max( naturalSize.width, contentSize.width );
779     if( naturalSize.width > maxWidth )
780     {
781       naturalSize.width = maxWidth;
782       contentSize.height = RelayoutHelper::GetHeightForWidth( mContent, maxWidth );
783     }
784     naturalSize.height += contentSize.height + mPopupStyle->margin;
785   }
786
787   if( !mButtons.empty() )
788   {
789     naturalSize.height += mPopupStyle->bottomSize.height;
790   }
791
792   // Add the margins
793   naturalSize.width += margin;
794   naturalSize.height += margin;
795
796   return naturalSize;
797 }
798
799 float Popup::GetHeightForWidth( float width )
800 {
801   float height( 0.0f );
802   float popupWidth( width - 2.f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
803
804   if( mContent )
805   {
806     height += RelayoutHelper::GetHeightForWidth( mContent, popupWidth ) + mPopupStyle->margin;
807   }
808
809   if( !mButtons.empty() )
810   {
811     height += mPopupStyle->bottomSize.height;
812   }
813
814   // Add the margins
815   float margin( 2.0f * ( POPUP_OUT_MARGIN_WIDTH + mPopupStyle->margin ) );
816   height += margin;
817
818   return height;
819 }
820
821 float Popup::GetWidthForHeight( float height )
822 {
823   return GetNaturalSize().width;
824 }
825
826 Actor Popup::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
827 {
828   Actor nextFocusableActor( currentFocusedActor );
829
830   // TODO: Needs to be optimised
831
832   if ( !currentFocusedActor || ( currentFocusedActor && KeyboardFocusManager::Get().GetFocusGroup(currentFocusedActor) != Self() ) )
833   {
834     // The current focused actor is not within popup
835     if( mContent && mContent.IsKeyboardFocusable() )
836     {
837       // If content is focusable, move the focus to content
838       nextFocusableActor = mContent;
839     }
840     else if( !mButtons.empty() )
841     {
842       // Otherwise, movethe focus to the first button
843       nextFocusableActor = mButtons[0];
844     }
845   }
846   else
847   {
848     // Rebuild the focus chain because button or content can be added or removed dynamically
849     ActorContainer focusableActors;
850     if( mContent && mContent.IsKeyboardFocusable() )
851     {
852       focusableActors.push_back(mContent);
853     }
854
855     for(unsigned int i = 0; i < mButtons.size(); i++)
856     {
857       if( mButtons[i] && mButtons[i].IsKeyboardFocusable() )
858       {
859         focusableActors.push_back(mButtons[i]);
860       }
861     }
862
863     for ( ActorContainer::iterator iter = focusableActors.begin(), end = focusableActors.end(); iter != end; ++iter )
864     {
865       if ( currentFocusedActor == *iter )
866       {
867         switch ( direction )
868         {
869           case Toolkit::Control::Left:
870           {
871             if ( iter == focusableActors.begin() )
872             {
873               nextFocusableActor = *( focusableActors.end() - 1 );
874             }
875             else
876             {
877               nextFocusableActor = *( iter - 1 );
878             }
879             break;
880           }
881           case Toolkit::Control::Right:
882           {
883             if ( iter == focusableActors.end() - 1 )
884             {
885               nextFocusableActor = *( focusableActors.begin() );
886             }
887             else
888             {
889               nextFocusableActor = *( iter + 1 );
890             }
891             break;
892           }
893
894           case Toolkit::Control::Up:
895           {
896             if ( *iter == mContent )
897             {
898               nextFocusableActor = *( focusableActors.end() - 1 );
899             }
900             else
901             {
902               if ( mContent && mContent.IsKeyboardFocusable() )
903               {
904                 nextFocusableActor = mContent;
905               }
906               else
907               {
908                 if ( iter == focusableActors.begin() )
909                 {
910                   nextFocusableActor = *( focusableActors.end() - 1 );
911                 }
912                 else
913                 {
914                   nextFocusableActor = *( iter - 1 );
915                 }
916               }
917             }
918             break;
919           }
920
921           case Toolkit::Control::Down:
922           {
923             if ( mContent && mContent.IsKeyboardFocusable() )
924             {
925               nextFocusableActor = mContent;
926             }
927             else
928             {
929               if ( iter == focusableActors.end() - 1 )
930               {
931                 nextFocusableActor = *( focusableActors.begin() );
932               }
933               else
934               {
935                 nextFocusableActor = *( iter + 1 );
936               }
937             }
938
939             if ( *iter == mContent && !mButtons.empty() )
940             {
941               nextFocusableActor = mButtons[0];
942             }
943             break;
944           }
945         }
946
947         if(!nextFocusableActor)
948         {
949           DALI_LOG_WARNING("Can not decide next focusable actor\n");
950         }
951
952         break;
953       }
954     }
955   }
956
957   return nextFocusableActor;
958 }
959
960 } // namespace Internal
961
962 } // namespace Toolkit
963
964 } // namespace Dali