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