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