b41b6300ae3d247a5eea44f96fe4b2dfb0e134a6
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / control-impl.cpp
1 /*
2  * Copyright (c) 2015 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/public-api/controls/control-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23 #include <limits>
24 #include <stack>
25 #include <dali/public-api/animation/constraint.h>
26 #include <dali/public-api/animation/constraints.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/public-api/size-negotiation/relayout-container.h>
29 #include <dali/devel-api/object/type-registry-helper.h>
30 #include <dali/devel-api/rendering/renderer.h>
31 #include <dali/devel-api/scripting/scripting.h>
32 #include <dali/integration-api/debug.h>
33
34 // INTERNAL INCLUDES
35 #include <dali-toolkit/public-api/controls/control-depth-index-ranges.h>
36 #include <dali-toolkit/devel-api/controls/renderer-factory/renderer-factory.h>
37 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
38 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
39 #include <dali-toolkit/public-api/controls/control.h>
40 #include <dali-toolkit/devel-api/styling/style-manager.h>
41 #include <dali-toolkit/internal/styling/style-manager-impl.h>
42 #include <dali-toolkit/internal/controls/renderers/color/color-renderer.h>
43
44 namespace Dali
45 {
46
47 namespace Toolkit
48 {
49
50 namespace
51 {
52
53 /**
54  * Creates control through type registry
55  */
56 BaseHandle Create()
57 {
58   return Internal::Control::New();
59 }
60
61 /**
62  * Performs actions as requested using the action name.
63  * @param[in] object The object on which to perform the action.
64  * @param[in] actionName The action to perform.
65  * @param[in] attributes The attributes with which to perfrom this action.
66  * @return true if action has been accepted by this control
67  */
68 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
69 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
70 {
71   bool ret = false;
72
73   if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
74   {
75     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
76     if( control )
77     {
78       // if cast succeeds there is an implementation so no need to check
79       ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
80     }
81   }
82
83   return ret;
84 }
85
86 /**
87  * Connects a callback function with the object's signals.
88  * @param[in] object The object providing the signal.
89  * @param[in] tracker Used to disconnect the signal.
90  * @param[in] signalName The signal to connect to.
91  * @param[in] functor A newly allocated FunctorDelegate.
92  * @return True if the signal was connected.
93  * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
94  */
95 const char* SIGNAL_KEY_EVENT = "keyEvent";
96 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
97 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
98 const char* SIGNAL_TAPPED = "tapped";
99 const char* SIGNAL_PANNED = "panned";
100 const char* SIGNAL_PINCHED = "pinched";
101 const char* SIGNAL_LONG_PRESSED = "longPressed";
102 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
103 {
104   Dali::BaseHandle handle( object );
105
106   bool connected( false );
107   Toolkit::Control control = Toolkit::Control::DownCast( handle );
108   if ( control )
109   {
110     Internal::Control& controlImpl( Internal::GetImplementation( control ) );
111     connected = true;
112
113     if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
114     {
115       controlImpl.KeyEventSignal().Connect( tracker, functor );
116     }
117     else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
118     {
119       controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
120     }
121     else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
122     {
123       controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
124     }
125     else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
126     {
127       controlImpl.EnableGestureDetection( Gesture::Tap );
128       controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
129     }
130     else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
131     {
132       controlImpl.EnableGestureDetection( Gesture::Pan );
133       controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
134     }
135     else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
136     {
137       controlImpl.EnableGestureDetection( Gesture::Pinch );
138       controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
139     }
140     else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
141     {
142       controlImpl.EnableGestureDetection( Gesture::LongPress );
143       controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
144     }
145   }
146   return connected;
147 }
148
149 // Setup signals and actions using the type-registry.
150 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
151
152 // Note: Properties are registered separately below.
153
154 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
155 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
156 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
157 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
158 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
159 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
160 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
161
162 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
163
164 DALI_TYPE_REGISTRATION_END()
165
166 const char * const BACKGROUND_COLOR_NAME("color");
167 const char * const COLOR_RENDERER_COLOR_NAME("blendColor");
168
169 } // unnamed namespace
170
171 namespace Internal
172 {
173
174 class Control::Impl : public ConnectionTracker
175 {
176 public:
177
178   // Construction & Destruction
179   Impl(Control& controlImpl)
180 : mControlImpl( controlImpl ),
181   mStyleName(""),
182   mBackgroundRenderer(),
183   mStartingPinchScale( NULL ),
184   mKeyEventSignal(),
185   mPinchGestureDetector(),
186   mPanGestureDetector(),
187   mTapGestureDetector(),
188   mLongPressGestureDetector(),
189   mFlags( Control::ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
190   mIsKeyboardNavigationSupported( false ),
191   mIsKeyboardFocusGroup( false ),
192   mAddRemoveBackgroundChild( false )
193 {
194 }
195
196   ~Impl()
197   {
198     // All gesture detectors will be destroyed so no need to disconnect.
199     delete mStartingPinchScale;
200   }
201
202   // Gesture Detection Methods
203
204   void PinchDetected(Actor actor, const PinchGesture& pinch)
205   {
206     mControlImpl.OnPinch(pinch);
207   }
208
209   void PanDetected(Actor actor, const PanGesture& pan)
210   {
211     mControlImpl.OnPan(pan);
212   }
213
214   void TapDetected(Actor actor, const TapGesture& tap)
215   {
216     mControlImpl.OnTap(tap);
217   }
218
219   void LongPressDetected(Actor actor, const LongPressGesture& longPress)
220   {
221     mControlImpl.OnLongPress(longPress);
222   }
223
224   // Properties
225
226   /**
227    * Called when a property of an object of this type is set.
228    * @param[in] object The object whose property is set.
229    * @param[in] index The property index.
230    * @param[in] value The new property value.
231    */
232   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
233   {
234     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
235
236     if ( control )
237     {
238       Control& controlImpl( GetImplementation( control ) );
239
240       switch ( index )
241       {
242         case Toolkit::Control::Property::STYLE_NAME:
243         {
244           controlImpl.SetStyleName( value.Get< std::string >() );
245           break;
246         }
247         case Toolkit::Control::Property::BACKGROUND:
248         {
249           Image image = Scripting::NewImage( value );
250           if ( image )
251           {
252             controlImpl.SetBackgroundImage( image );
253             break;
254           }
255           const Property::Map* map = value.GetMap();
256           if( map )
257           {
258             controlImpl.SetBackground( *map );
259             break;
260           }
261
262           // The background is neither a valid image nor a property map, so it is no longer required
263           controlImpl.ClearBackground();
264           break;
265         }
266
267         case Toolkit::Control::Property::KEY_INPUT_FOCUS:
268         {
269           if ( value.Get< bool >() )
270           {
271             controlImpl.SetKeyInputFocus();
272           }
273           else
274           {
275             controlImpl.ClearKeyInputFocus();
276           }
277           break;
278         }
279       }
280     }
281   }
282
283   /**
284    * Called to retrieve a property of an object of this type.
285    * @param[in] object The object whose property is to be retrieved.
286    * @param[in] index The property index.
287    * @return The current value of the property.
288    */
289   static Property::Value GetProperty( BaseObject* object, Property::Index index )
290   {
291     Property::Value value;
292
293     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
294
295     if ( control )
296     {
297       Control& controlImpl( GetImplementation( control ) );
298
299       switch ( index )
300       {
301         case Toolkit::Control::Property::STYLE_NAME:
302         {
303           value = controlImpl.GetStyleName();
304           break;
305         }
306
307         case Toolkit::Control::Property::BACKGROUND:
308         {
309           Property::Map map;
310           if( controlImpl.mImpl->mBackgroundRenderer )
311           {
312             (controlImpl.mImpl->mBackgroundRenderer).CreatePropertyMap( map );
313           }
314
315           value = map;
316           break;
317         }
318
319         case Toolkit::Control::Property::KEY_INPUT_FOCUS:
320         {
321           value = controlImpl.HasKeyInputFocus();
322           break;
323         }
324       }
325     }
326
327     return value;
328   }
329
330   // Data
331
332   Control& mControlImpl;
333   std::string mStyleName;
334   Toolkit::ControlRenderer mBackgroundRenderer;   ///< The control renderer to render the background
335   Vector3* mStartingPinchScale;      ///< The scale when a pinch gesture starts, TODO: consider removing this
336   Toolkit::Control::KeyEventSignalType mKeyEventSignal;
337   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
338   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
339
340   // Gesture Detection
341   PinchGestureDetector mPinchGestureDetector;
342   PanGestureDetector mPanGestureDetector;
343   TapGestureDetector mTapGestureDetector;
344   LongPressGestureDetector mLongPressGestureDetector;
345
346   ControlBehaviour mFlags :CONTROL_BEHAVIOUR_FLAG_COUNT;    ///< Flags passed in from constructor.
347   bool mIsKeyboardNavigationSupported :1;  ///< Stores whether keyboard navigation is supported by the control.
348   bool mIsKeyboardFocusGroup :1;           ///< Stores whether the control is a focus group.
349   bool mAddRemoveBackgroundChild:1;        ///< Flag to know when we are adding or removing our own actor to avoid call to OnControlChildAdd
350
351   // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
352   static PropertyRegistration PROPERTY_1;
353   static PropertyRegistration PROPERTY_2;
354   static PropertyRegistration PROPERTY_3;
355 };
356
357 // Properties registered without macro to use specific member variables.
358 PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName",       Toolkit::Control::Property::STYLE_NAME,      Property::STRING,  &Control::Impl::SetProperty, &Control::Impl::GetProperty );
359 PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "background",      Toolkit::Control::Property::BACKGROUND,      Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
360 PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "keyInputFocus",   Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
361
362 Toolkit::Control Control::New()
363 {
364   // Create the implementation, temporarily owned on stack
365   IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) );
366
367   // Pass ownership to handle
368   Toolkit::Control handle( *controlImpl );
369
370   // Second-phase init of the implementation
371   // This can only be done after the CustomActor connection has been made...
372   controlImpl->Initialize();
373
374   return handle;
375 }
376
377 Control::~Control()
378 {
379   delete mImpl;
380 }
381
382 void Control::SetStyleName( const std::string& styleName )
383 {
384   if( styleName != mImpl->mStyleName )
385   {
386     mImpl->mStyleName = styleName;
387
388     // Apply new style, if stylemanager is available
389     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
390     if( styleManager )
391     {
392       GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
393     }
394   }
395 }
396
397 const std::string& Control::GetStyleName() const
398 {
399   return mImpl->mStyleName;
400 }
401
402 void Control::SetBackgroundColor( const Vector4& color )
403 {
404   Actor self( Self() );
405   Toolkit::RendererFactory factory = Toolkit::RendererFactory::Get();
406   factory.ResetRenderer( mImpl->mBackgroundRenderer, self, color );
407   mImpl->mBackgroundRenderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX );
408 }
409
410 Vector4 Control::GetBackgroundColor() const
411 {
412   if( mImpl->mBackgroundRenderer && ( &typeid( GetImplementation(mImpl->mBackgroundRenderer) ) == &typeid( ColorRenderer ) ) )
413   {
414      Property::Map map;
415      mImpl->mBackgroundRenderer.CreatePropertyMap( map );
416      const Property::Value* colorValue = map.Find( COLOR_RENDERER_COLOR_NAME );
417      Vector4 color;
418      if( colorValue && colorValue->Get(color))
419      {
420        return color;
421      }
422   }
423
424   return Color::TRANSPARENT;
425 }
426
427 void Control::SetBackground(const Property::Map& map)
428 {
429   const Property::Value* colorValue = map.Find( BACKGROUND_COLOR_NAME );
430   Vector4 color;
431   if( colorValue && colorValue->Get(color))
432   {
433     SetBackgroundColor( color );
434     return;
435   }
436
437   Actor self( Self() );
438   mImpl->mBackgroundRenderer.RemoveAndReset( self );
439   Toolkit::RendererFactory factory = Toolkit::RendererFactory::Get();
440   mImpl->mBackgroundRenderer = factory.GetControlRenderer( map );
441   if( mImpl->mBackgroundRenderer  && self.OnStage() ) // Request control renderer with a property map might return an empty handle
442   {
443     mImpl->mBackgroundRenderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX );
444     mImpl->mBackgroundRenderer.SetOnStage( self );
445   }
446 }
447
448 void Control::SetBackgroundImage( Image image )
449 {
450   Actor self( Self() );
451   Toolkit::RendererFactory factory = Toolkit::RendererFactory::Get();
452   factory.ResetRenderer( mImpl->mBackgroundRenderer, self, image );
453   mImpl->mBackgroundRenderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX );
454 }
455
456 void Control::ClearBackground()
457 {
458   Actor self( Self() );
459   mImpl->mBackgroundRenderer.RemoveAndReset( self );
460 }
461
462 void Control::EnableGestureDetection(Gesture::Type type)
463 {
464   if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
465   {
466     mImpl->mPinchGestureDetector = PinchGestureDetector::New();
467     mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
468     mImpl->mPinchGestureDetector.Attach(Self());
469   }
470
471   if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
472   {
473     mImpl->mPanGestureDetector = PanGestureDetector::New();
474     mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
475     mImpl->mPanGestureDetector.Attach(Self());
476   }
477
478   if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
479   {
480     mImpl->mTapGestureDetector = TapGestureDetector::New();
481     mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
482     mImpl->mTapGestureDetector.Attach(Self());
483   }
484
485   if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
486   {
487     mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
488     mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
489     mImpl->mLongPressGestureDetector.Attach(Self());
490   }
491 }
492
493 void Control::DisableGestureDetection(Gesture::Type type)
494 {
495   if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
496   {
497     mImpl->mPinchGestureDetector.Detach(Self());
498     mImpl->mPinchGestureDetector.Reset();
499   }
500
501   if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
502   {
503     mImpl->mPanGestureDetector.Detach(Self());
504     mImpl->mPanGestureDetector.Reset();
505   }
506
507   if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
508   {
509     mImpl->mTapGestureDetector.Detach(Self());
510     mImpl->mTapGestureDetector.Reset();
511   }
512
513   if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
514   {
515     mImpl->mLongPressGestureDetector.Detach(Self());
516     mImpl->mLongPressGestureDetector.Reset();
517   }
518 }
519
520 PinchGestureDetector Control::GetPinchGestureDetector() const
521 {
522   return mImpl->mPinchGestureDetector;
523 }
524
525 PanGestureDetector Control::GetPanGestureDetector() const
526 {
527   return mImpl->mPanGestureDetector;
528 }
529
530 TapGestureDetector Control::GetTapGestureDetector() const
531 {
532   return mImpl->mTapGestureDetector;
533 }
534
535 LongPressGestureDetector Control::GetLongPressGestureDetector() const
536 {
537   return mImpl->mLongPressGestureDetector;
538 }
539
540 void Control::SetKeyboardNavigationSupport(bool isSupported)
541 {
542   mImpl->mIsKeyboardNavigationSupported = isSupported;
543 }
544
545 bool Control::IsKeyboardNavigationSupported()
546 {
547   return mImpl->mIsKeyboardNavigationSupported;
548 }
549
550 void Control::SetKeyInputFocus()
551 {
552   if( Self().OnStage() )
553   {
554     Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
555   }
556 }
557
558 bool Control::HasKeyInputFocus()
559 {
560   bool result = false;
561   if( Self().OnStage() )
562   {
563     result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
564   }
565   return result;
566 }
567
568 void Control::ClearKeyInputFocus()
569 {
570   if( Self().OnStage() )
571   {
572     Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
573   }
574 }
575
576 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
577 {
578   mImpl->mIsKeyboardFocusGroup = isFocusGroup;
579
580   // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
581   Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
582 }
583
584 bool Control::IsKeyboardFocusGroup()
585 {
586   return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
587 }
588
589 void Control::AccessibilityActivate()
590 {
591   // Inform deriving classes
592   OnAccessibilityActivated();
593 }
594
595 void Control::KeyboardEnter()
596 {
597   // Inform deriving classes
598   OnKeyboardEnter();
599 }
600
601 bool Control::OnAccessibilityActivated()
602 {
603   return false; // Accessibility activation is not handled by default
604 }
605
606 bool Control::OnKeyboardEnter()
607 {
608   return false; // Keyboard enter is not handled by default
609 }
610
611 bool Control::OnAccessibilityPan(PanGesture gesture)
612 {
613   return false; // Accessibility pan gesture is not handled by default
614 }
615
616 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
617 {
618   return false; // Accessibility touch event is not handled by default
619 }
620
621 bool Control::OnAccessibilityValueChange(bool isIncrease)
622 {
623   return false; // Accessibility value change action is not handled by default
624 }
625
626 bool Control::OnAccessibilityZoom()
627 {
628   return false; // Accessibility zoom action is not handled by default
629 }
630
631 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
632 {
633   return Actor();
634 }
635
636 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
637 {
638 }
639
640 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
641 {
642   return mImpl->mKeyEventSignal;
643 }
644
645 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusGainedSignal()
646 {
647   return mImpl->mKeyInputFocusGainedSignal;
648 }
649
650 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusLostSignal()
651 {
652   return mImpl->mKeyInputFocusLostSignal;
653 }
654
655 bool Control::EmitKeyEventSignal( const KeyEvent& event )
656 {
657   // Guard against destruction during signal emission
658   Dali::Toolkit::Control handle( GetOwner() );
659
660   bool consumed = false;
661
662   // signals are allocated dynamically when someone connects
663   if ( !mImpl->mKeyEventSignal.Empty() )
664   {
665     consumed = mImpl->mKeyEventSignal.Emit( handle, event );
666   }
667
668   if (!consumed)
669   {
670     // Notification for derived classes
671     consumed = OnKeyEvent(event);
672   }
673
674   return consumed;
675 }
676
677 Control::Control( ControlBehaviour behaviourFlags )
678 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
679   mImpl(new Impl(*this))
680 {
681   mImpl->mFlags = behaviourFlags;
682 }
683
684 void Control::Initialize()
685 {
686   // Call deriving classes so initialised before styling is applied to them.
687   OnInitialize();
688
689   if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
690   {
691     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
692     // if stylemanager is available
693     if( styleManager )
694     {
695       // Register for style changes
696       styleManager.StyleChangeSignal().Connect( this, &Control::OnStyleChange );
697
698       // Apply the current style
699       GetImpl( styleManager ).ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
700     }
701   }
702
703   if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
704   {
705     SetKeyboardNavigationSupport( true );
706   }
707 }
708
709 void Control::OnInitialize()
710 {
711 }
712
713 void Control::OnControlChildAdd( Actor& child )
714 {
715 }
716
717 void Control::OnControlChildRemove( Actor& child )
718 {
719 }
720
721 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
722 {
723   // By default the control is only interested in theme (not font) changes
724   if( styleManager && change == StyleChange::THEME_CHANGE )
725   {
726     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
727   }
728 }
729
730 void Control::OnPinch(const PinchGesture& pinch)
731 {
732   if( !( mImpl->mStartingPinchScale ) )
733   {
734     // lazy allocate
735     mImpl->mStartingPinchScale = new Vector3;
736   }
737
738   if( pinch.state == Gesture::Started )
739   {
740     *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
741   }
742
743   Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
744 }
745
746 void Control::OnPan( const PanGesture& pan )
747 {
748 }
749
750 void Control::OnTap(const TapGesture& tap)
751 {
752 }
753
754 void Control::OnLongPress( const LongPressGesture& longPress )
755 {
756 }
757
758 void Control::EmitKeyInputFocusSignal( bool focusGained )
759 {
760   Dali::Toolkit::Control handle( GetOwner() );
761
762   if ( focusGained )
763   {
764     // signals are allocated dynamically when someone connects
765     if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
766     {
767       mImpl->mKeyInputFocusGainedSignal.Emit( handle );
768     }
769   }
770   else
771   {
772     // signals are allocated dynamically when someone connects
773     if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
774     {
775       mImpl->mKeyInputFocusLostSignal.Emit( handle );
776     }
777   }
778 }
779
780 void Control::OnStageConnection( int depth )
781 {
782   if( mImpl->mBackgroundRenderer)
783   {
784     Actor self( Self() );
785     mImpl->mBackgroundRenderer.SetOnStage( self );
786   }
787 }
788
789 void Control::OnStageDisconnection()
790 {
791   if( mImpl->mBackgroundRenderer )
792   {
793     Actor self( Self() );
794     mImpl->mBackgroundRenderer.SetOffStage( self );
795   }
796 }
797
798 void Control::OnKeyInputFocusGained()
799 {
800   EmitKeyInputFocusSignal( true );
801 }
802
803 void Control::OnKeyInputFocusLost()
804 {
805   EmitKeyInputFocusSignal( false );
806 }
807
808 void Control::OnChildAdd(Actor& child)
809 {
810   // If this is the background actor, then we do not want to inform deriving classes
811   if ( mImpl->mAddRemoveBackgroundChild )
812   {
813     return;
814   }
815
816   // Notify derived classes.
817   OnControlChildAdd( child );
818 }
819
820 void Control::OnChildRemove(Actor& child)
821 {
822   // If this is the background actor, then we do not want to inform deriving classes
823   if ( mImpl->mAddRemoveBackgroundChild )
824   {
825     return;
826   }
827
828   // Notify derived classes.
829   OnControlChildRemove( child );
830 }
831
832 void Control::OnSizeSet(const Vector3& targetSize)
833 {
834   // Background is resized through size negotiation
835 }
836
837 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
838 {
839   // @todo size negotiate background to new size, animate as well?
840 }
841
842 bool Control::OnTouchEvent(const TouchEvent& event)
843 {
844   return false; // Do not consume
845 }
846
847 bool Control::OnHoverEvent(const HoverEvent& event)
848 {
849   return false; // Do not consume
850 }
851
852 bool Control::OnKeyEvent(const KeyEvent& event)
853 {
854   return false; // Do not consume
855 }
856
857 bool Control::OnWheelEvent(const WheelEvent& event)
858 {
859   return false; // Do not consume
860 }
861
862 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
863 {
864   for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
865   {
866     container.Add( Self().GetChildAt( i ), size );
867   }
868 }
869
870 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
871 {
872 }
873
874 Vector3 Control::GetNaturalSize()
875 {
876   if( mImpl->mBackgroundRenderer )
877   {
878     Vector2 naturalSize;
879     mImpl->mBackgroundRenderer.GetNaturalSize(naturalSize);
880     return Vector3(naturalSize);
881   }
882   return Vector3::ZERO;
883 }
884
885 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
886 {
887   return CalculateChildSizeBase( child, dimension );
888 }
889
890 float Control::GetHeightForWidth( float width )
891 {
892   return GetHeightForWidthBase( width );
893 }
894
895 float Control::GetWidthForHeight( float height )
896 {
897   return GetWidthForHeightBase( height );
898 }
899
900 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
901 {
902   return RelayoutDependentOnChildrenBase( dimension );
903 }
904
905 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
906 {
907 }
908
909 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
910 {
911 }
912
913 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
914 {
915   mImpl->SignalConnected( slotObserver, callback );
916 }
917
918 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
919 {
920   mImpl->SignalDisconnected( slotObserver, callback );
921 }
922
923 Control& GetImplementation( Dali::Toolkit::Control& handle )
924 {
925   CustomActorImpl& customInterface = handle.GetImplementation();
926   // downcast to control
927   Control& impl = dynamic_cast< Internal::Control& >( customInterface );
928   return impl;
929 }
930
931 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
932 {
933   const CustomActorImpl& customInterface = handle.GetImplementation();
934   // downcast to control
935   const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
936   return impl;
937 }
938
939 } // namespace Internal
940
941 } // namespace Toolkit
942
943 } // namespace Dali