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