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