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