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