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