Merge "Changed all property & signal names to lowerCamelCase" 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/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 #include <dali-toolkit/internal/controls/renderers/image/image-renderer.h>
44
45 namespace Dali
46 {
47
48 namespace Toolkit
49 {
50
51 namespace
52 {
53
54 /**
55  * Creates control through type registry
56  */
57 BaseHandle Create()
58 {
59   return Internal::Control::New();
60 }
61
62 /**
63  * Performs actions as requested using the action name.
64  * @param[in] object The object on which to perform the action.
65  * @param[in] actionName The action to perform.
66  * @param[in] attributes The attributes with which to perfrom this action.
67  * @return true if action has been accepted by this control
68  */
69 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
70 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
71 {
72   bool ret = false;
73
74   if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
75   {
76     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
77     if( control )
78     {
79       // if cast succeeds there is an implementation so no need to check
80       ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
81     }
82   }
83
84   return ret;
85 }
86
87 /**
88  * Connects a callback function with the object's signals.
89  * @param[in] object The object providing the signal.
90  * @param[in] tracker Used to disconnect the signal.
91  * @param[in] signalName The signal to connect to.
92  * @param[in] functor A newly allocated FunctorDelegate.
93  * @return True if the signal was connected.
94  * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
95  */
96 const char* SIGNAL_KEY_EVENT = "keyEvent";
97 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
98 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
99 const char* SIGNAL_TAPPED = "tapped";
100 const char* SIGNAL_PANNED = "panned";
101 const char* SIGNAL_PINCHED = "pinched";
102 const char* SIGNAL_LONG_PRESSED = "longPressed";
103 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
104 {
105   Dali::BaseHandle handle( object );
106
107   bool connected( false );
108   Toolkit::Control control = Toolkit::Control::DownCast( handle );
109   if ( control )
110   {
111     Internal::Control& controlImpl( Internal::GetImplementation( control ) );
112     connected = true;
113
114     if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
115     {
116       controlImpl.KeyEventSignal().Connect( tracker, functor );
117     }
118     else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
119     {
120       controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
121     }
122     else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
123     {
124       controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
125     }
126     else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
127     {
128       controlImpl.EnableGestureDetection( Gesture::Tap );
129       controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
130     }
131     else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
132     {
133       controlImpl.EnableGestureDetection( Gesture::Pan );
134       controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
135     }
136     else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
137     {
138       controlImpl.EnableGestureDetection( Gesture::Pinch );
139       controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
140     }
141     else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
142     {
143       controlImpl.EnableGestureDetection( Gesture::LongPress );
144       controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
145     }
146   }
147   return connected;
148 }
149
150 // Setup signals and actions using the type-registry.
151 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
152
153 // Note: Properties are registered separately below.
154
155 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
156 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
157 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
158 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
159 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
160 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
161 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
162
163 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
164
165 DALI_TYPE_REGISTRATION_END()
166
167 const char * const BACKGROUND_COLOR_NAME("color");
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
407   if( mImpl->mBackgroundRenderer )
408   {
409     factory.ResetRenderer( mImpl->mBackgroundRenderer, self, color );
410   }
411   else
412   {
413     mImpl->mBackgroundRenderer = factory.GetControlRenderer( color );
414
415     if( self.OnStage() )
416     {
417       mImpl->mBackgroundRenderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX );
418       mImpl->mBackgroundRenderer.SetOnStage( self );
419     }
420   }
421 }
422
423 Vector4 Control::GetBackgroundColor() const
424 {
425   return Color::TRANSPARENT;
426 }
427
428 void Control::SetBackground(const Property::Map& map)
429 {
430   const Property::Value* colorValue = map.Find( BACKGROUND_COLOR_NAME );
431   Vector4 color;
432   if( colorValue && colorValue->Get(color))
433   {
434     SetBackgroundColor( color );
435     return;
436   }
437
438   Actor self( Self() );
439   mImpl->mBackgroundRenderer.RemoveAndReset( self );
440
441   Toolkit::RendererFactory factory = Toolkit::RendererFactory::Get();
442   mImpl->mBackgroundRenderer = factory.GetControlRenderer( map );
443
444   // mBackgroundRenderer might be empty, if an invalid map is provided, no background.
445   if( self.OnStage() && mImpl->mBackgroundRenderer)
446   {
447     mImpl->mBackgroundRenderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX );
448     mImpl->mBackgroundRenderer.SetOnStage( self );
449   }
450 }
451
452 void Control::SetBackgroundImage( Image image )
453 {
454   Actor self( Self() );
455   Toolkit::RendererFactory factory = Toolkit::RendererFactory::Get();
456
457   if(  mImpl->mBackgroundRenderer  )
458   {
459     factory.ResetRenderer( mImpl->mBackgroundRenderer, self, image );
460   }
461   else
462   {
463     mImpl->mBackgroundRenderer = factory.GetControlRenderer( image );
464
465     if( self.OnStage() )
466     {
467       mImpl->mBackgroundRenderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX );
468       mImpl->mBackgroundRenderer.SetOnStage( self );
469     }
470   }
471 }
472
473 void Control::ClearBackground()
474 {
475   Actor self( Self() );
476   mImpl->mBackgroundRenderer.RemoveAndReset( self );
477 }
478
479 void Control::EnableGestureDetection(Gesture::Type type)
480 {
481   if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
482   {
483     mImpl->mPinchGestureDetector = PinchGestureDetector::New();
484     mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
485     mImpl->mPinchGestureDetector.Attach(Self());
486   }
487
488   if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
489   {
490     mImpl->mPanGestureDetector = PanGestureDetector::New();
491     mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
492     mImpl->mPanGestureDetector.Attach(Self());
493   }
494
495   if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
496   {
497     mImpl->mTapGestureDetector = TapGestureDetector::New();
498     mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
499     mImpl->mTapGestureDetector.Attach(Self());
500   }
501
502   if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
503   {
504     mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
505     mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
506     mImpl->mLongPressGestureDetector.Attach(Self());
507   }
508 }
509
510 void Control::DisableGestureDetection(Gesture::Type type)
511 {
512   if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
513   {
514     mImpl->mPinchGestureDetector.Detach(Self());
515     mImpl->mPinchGestureDetector.Reset();
516   }
517
518   if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
519   {
520     mImpl->mPanGestureDetector.Detach(Self());
521     mImpl->mPanGestureDetector.Reset();
522   }
523
524   if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
525   {
526     mImpl->mTapGestureDetector.Detach(Self());
527     mImpl->mTapGestureDetector.Reset();
528   }
529
530   if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
531   {
532     mImpl->mLongPressGestureDetector.Detach(Self());
533     mImpl->mLongPressGestureDetector.Reset();
534   }
535 }
536
537 PinchGestureDetector Control::GetPinchGestureDetector() const
538 {
539   return mImpl->mPinchGestureDetector;
540 }
541
542 PanGestureDetector Control::GetPanGestureDetector() const
543 {
544   return mImpl->mPanGestureDetector;
545 }
546
547 TapGestureDetector Control::GetTapGestureDetector() const
548 {
549   return mImpl->mTapGestureDetector;
550 }
551
552 LongPressGestureDetector Control::GetLongPressGestureDetector() const
553 {
554   return mImpl->mLongPressGestureDetector;
555 }
556
557 void Control::SetKeyboardNavigationSupport(bool isSupported)
558 {
559   mImpl->mIsKeyboardNavigationSupported = isSupported;
560 }
561
562 bool Control::IsKeyboardNavigationSupported()
563 {
564   return mImpl->mIsKeyboardNavigationSupported;
565 }
566
567 void Control::SetKeyInputFocus()
568 {
569   if( Self().OnStage() )
570   {
571     Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
572   }
573 }
574
575 bool Control::HasKeyInputFocus()
576 {
577   bool result = false;
578   if( Self().OnStage() )
579   {
580     result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
581   }
582   return result;
583 }
584
585 void Control::ClearKeyInputFocus()
586 {
587   if( Self().OnStage() )
588   {
589     Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
590   }
591 }
592
593 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
594 {
595   mImpl->mIsKeyboardFocusGroup = isFocusGroup;
596
597   // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
598   Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
599 }
600
601 bool Control::IsKeyboardFocusGroup()
602 {
603   return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
604 }
605
606 void Control::AccessibilityActivate()
607 {
608   // Inform deriving classes
609   OnAccessibilityActivated();
610 }
611
612 void Control::KeyboardEnter()
613 {
614   // Inform deriving classes
615   OnKeyboardEnter();
616 }
617
618 bool Control::OnAccessibilityActivated()
619 {
620   return false; // Accessibility activation is not handled by default
621 }
622
623 bool Control::OnKeyboardEnter()
624 {
625   return false; // Keyboard enter is not handled by default
626 }
627
628 bool Control::OnAccessibilityPan(PanGesture gesture)
629 {
630   return false; // Accessibility pan gesture is not handled by default
631 }
632
633 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
634 {
635   return false; // Accessibility touch event is not handled by default
636 }
637
638 bool Control::OnAccessibilityValueChange(bool isIncrease)
639 {
640   return false; // Accessibility value change action is not handled by default
641 }
642
643 bool Control::OnAccessibilityZoom()
644 {
645   return false; // Accessibility zoom action is not handled by default
646 }
647
648 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
649 {
650   return Actor();
651 }
652
653 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
654 {
655 }
656
657 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
658 {
659   return mImpl->mKeyEventSignal;
660 }
661
662 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusGainedSignal()
663 {
664   return mImpl->mKeyInputFocusGainedSignal;
665 }
666
667 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusLostSignal()
668 {
669   return mImpl->mKeyInputFocusLostSignal;
670 }
671
672 bool Control::EmitKeyEventSignal( const KeyEvent& event )
673 {
674   // Guard against destruction during signal emission
675   Dali::Toolkit::Control handle( GetOwner() );
676
677   bool consumed = false;
678
679   // signals are allocated dynamically when someone connects
680   if ( !mImpl->mKeyEventSignal.Empty() )
681   {
682     consumed = mImpl->mKeyEventSignal.Emit( handle, event );
683   }
684
685   if (!consumed)
686   {
687     // Notification for derived classes
688     consumed = OnKeyEvent(event);
689   }
690
691   return consumed;
692 }
693
694 Control::Control( ControlBehaviour behaviourFlags )
695 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
696   mImpl(new Impl(*this))
697 {
698   mImpl->mFlags = behaviourFlags;
699 }
700
701 void Control::Initialize()
702 {
703   // Call deriving classes so initialised before styling is applied to them.
704   OnInitialize();
705
706   if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
707   {
708     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
709     // if stylemanager is available
710     if( styleManager )
711     {
712       // Register for style changes
713       styleManager.StyleChangeSignal().Connect( this, &Control::OnStyleChange );
714
715       // Apply the current style
716       GetImpl( styleManager ).ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
717     }
718   }
719
720   if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
721   {
722     SetKeyboardNavigationSupport( true );
723   }
724 }
725
726 void Control::OnInitialize()
727 {
728 }
729
730 void Control::OnControlChildAdd( Actor& child )
731 {
732 }
733
734 void Control::OnControlChildRemove( Actor& child )
735 {
736 }
737
738 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
739 {
740   // By default the control is only interested in theme (not font) changes
741   if( styleManager && change == StyleChange::THEME_CHANGE )
742   {
743     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
744   }
745 }
746
747 void Control::OnPinch(const PinchGesture& pinch)
748 {
749   if( !( mImpl->mStartingPinchScale ) )
750   {
751     // lazy allocate
752     mImpl->mStartingPinchScale = new Vector3;
753   }
754
755   if( pinch.state == Gesture::Started )
756   {
757     *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
758   }
759
760   Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
761 }
762
763 void Control::OnPan( const PanGesture& pan )
764 {
765 }
766
767 void Control::OnTap(const TapGesture& tap)
768 {
769 }
770
771 void Control::OnLongPress( const LongPressGesture& longPress )
772 {
773 }
774
775 void Control::EmitKeyInputFocusSignal( bool focusGained )
776 {
777   Dali::Toolkit::Control handle( GetOwner() );
778
779   if ( focusGained )
780   {
781     // signals are allocated dynamically when someone connects
782     if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
783     {
784       mImpl->mKeyInputFocusGainedSignal.Emit( handle );
785     }
786   }
787   else
788   {
789     // signals are allocated dynamically when someone connects
790     if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
791     {
792       mImpl->mKeyInputFocusLostSignal.Emit( handle );
793     }
794   }
795 }
796
797 void Control::OnStageConnection( int depth )
798 {
799   if( mImpl->mBackgroundRenderer)
800   {
801     mImpl->mBackgroundRenderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX );
802     Actor self(Self());
803     mImpl->mBackgroundRenderer.SetOnStage( self );
804   }
805 }
806
807 void Control::OnStageDisconnection()
808 {
809   if( mImpl->mBackgroundRenderer)
810   {
811     Actor self(Self());
812     mImpl->mBackgroundRenderer.SetOffStage( self );
813   }
814 }
815
816 void Control::OnKeyInputFocusGained()
817 {
818   EmitKeyInputFocusSignal( true );
819 }
820
821 void Control::OnKeyInputFocusLost()
822 {
823   EmitKeyInputFocusSignal( false );
824 }
825
826 void Control::OnChildAdd(Actor& child)
827 {
828   // If this is the background actor, then we do not want to inform deriving classes
829   if ( mImpl->mAddRemoveBackgroundChild )
830   {
831     return;
832   }
833
834   // Notify derived classes.
835   OnControlChildAdd( child );
836 }
837
838 void Control::OnChildRemove(Actor& child)
839 {
840   // If this is the background actor, then we do not want to inform deriving classes
841   if ( mImpl->mAddRemoveBackgroundChild )
842   {
843     return;
844   }
845
846   // Notify derived classes.
847   OnControlChildRemove( child );
848 }
849
850 void Control::OnSizeSet(const Vector3& targetSize)
851 {
852   // Background is resized through size negotiation
853 }
854
855 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
856 {
857   // @todo size negotiate background to new size, animate as well?
858 }
859
860 bool Control::OnTouchEvent(const TouchEvent& event)
861 {
862   return false; // Do not consume
863 }
864
865 bool Control::OnHoverEvent(const HoverEvent& event)
866 {
867   return false; // Do not consume
868 }
869
870 bool Control::OnKeyEvent(const KeyEvent& event)
871 {
872   return false; // Do not consume
873 }
874
875 bool Control::OnWheelEvent(const WheelEvent& event)
876 {
877   return false; // Do not consume
878 }
879
880 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
881 {
882   for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
883   {
884     container.Add( Self().GetChildAt( i ), size );
885   }
886 }
887
888 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
889 {
890 }
891
892 Vector3 Control::GetNaturalSize()
893 {
894   if( mImpl->mBackgroundRenderer )
895   {
896     Vector2 naturalSize;
897     mImpl->mBackgroundRenderer.GetNaturalSize(naturalSize);
898     return Vector3(naturalSize);
899   }
900   return Vector3::ZERO;
901 }
902
903 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
904 {
905   return CalculateChildSizeBase( child, dimension );
906 }
907
908 float Control::GetHeightForWidth( float width )
909 {
910   return GetHeightForWidthBase( width );
911 }
912
913 float Control::GetWidthForHeight( float height )
914 {
915   return GetWidthForHeightBase( height );
916 }
917
918 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
919 {
920   return RelayoutDependentOnChildrenBase( dimension );
921 }
922
923 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
924 {
925 }
926
927 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
928 {
929 }
930
931 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
932 {
933   mImpl->SignalConnected( slotObserver, callback );
934 }
935
936 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
937 {
938   mImpl->SignalDisconnected( slotObserver, callback );
939 }
940
941 Control& GetImplementation( Dali::Toolkit::Control& handle )
942 {
943   CustomActorImpl& customInterface = handle.GetImplementation();
944   // downcast to control
945   Control& impl = dynamic_cast< Internal::Control& >( customInterface );
946   return impl;
947 }
948
949 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
950 {
951   const CustomActorImpl& customInterface = handle.GetImplementation();
952   // downcast to control
953   const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
954   return impl;
955 }
956
957 } // namespace Internal
958
959 } // namespace Toolkit
960
961 } // namespace Dali