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