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