Merge "TableView: Enum cleanup and Complete the SetCellAlignment() func" into devel...
[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           Image image = Scripting::NewImage( value );
352           if ( image )
353           {
354             controlImpl.SetBackgroundImage( image );
355           }
356           else
357           {
358             // An empty map means the background is no longer required
359             controlImpl.ClearBackground();
360           }
361           break;
362         }
363
364         case Toolkit::Control::Property::KEY_INPUT_FOCUS:
365         {
366           if ( value.Get< bool >() )
367           {
368             controlImpl.SetKeyInputFocus();
369           }
370           else
371           {
372             controlImpl.ClearKeyInputFocus();
373           }
374           break;
375         }
376       }
377     }
378   }
379
380   /**
381    * Called to retrieve a property of an object of this type.
382    * @param[in] object The object whose property is to be retrieved.
383    * @param[in] index The property index.
384    * @return The current value of the property.
385    */
386   static Property::Value GetProperty( BaseObject* object, Property::Index index )
387   {
388     Property::Value value;
389
390     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
391
392     if ( control )
393     {
394       Control& controlImpl( GetImplementation( control ) );
395
396       switch ( index )
397       {
398         case Toolkit::Control::Property::STYLE_NAME:
399         {
400           value = controlImpl.GetStyleName();
401           break;
402         }
403
404         case Toolkit::Control::Property::BACKGROUND_COLOR:
405         {
406           value = controlImpl.GetBackgroundColor();
407           break;
408         }
409
410         case Toolkit::Control::Property::BACKGROUND_IMAGE:
411         {
412           Property::Map map;
413
414           Background* back = controlImpl.mImpl->mBackground;
415           if( back )
416           {
417             ImageActor imageActor = ImageActor::DownCast( back->actor );
418             if ( imageActor )
419             {
420               Image image = imageActor.GetImage();
421               Scripting::CreatePropertyMap( image, map );
422             }
423           }
424
425           value = map;
426           break;
427         }
428
429         case Toolkit::Control::Property::KEY_INPUT_FOCUS:
430         {
431           value = controlImpl.HasKeyInputFocus();
432           break;
433         }
434       }
435     }
436
437     return value;
438   }
439
440   // Data
441
442   Control& mControlImpl;
443   std::string mStyleName;
444   Background* mBackground;           ///< Only create the background if we use it
445   Vector3* mStartingPinchScale;      ///< The scale when a pinch gesture starts, TODO: consider removing this
446   Toolkit::Control::KeyEventSignalType mKeyEventSignal;
447   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
448   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
449
450   // Gesture Detection
451   PinchGestureDetector mPinchGestureDetector;
452   PanGestureDetector mPanGestureDetector;
453   TapGestureDetector mTapGestureDetector;
454   LongPressGestureDetector mLongPressGestureDetector;
455
456   ControlBehaviour mFlags :CONTROL_BEHAVIOUR_FLAG_COUNT;    ///< Flags passed in from constructor.
457   bool mIsKeyboardNavigationSupported :1;  ///< Stores whether keyboard navigation is supported by the control.
458   bool mIsKeyboardFocusGroup :1;           ///< Stores whether the control is a focus group.
459   bool mAddRemoveBackgroundChild:1;        ///< Flag to know when we are adding or removing our own actor to avoid call to OnControlChildAdd
460
461   // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
462   static PropertyRegistration PROPERTY_1;
463   static PropertyRegistration PROPERTY_2;
464   static PropertyRegistration PROPERTY_3;
465   static PropertyRegistration PROPERTY_4;
466 };
467
468 // Properties registered without macro to use specific member variables.
469 PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "style-name",       Toolkit::Control::Property::STYLE_NAME,       Property::STRING,  &Control::Impl::SetProperty, &Control::Impl::GetProperty );
470 PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "background-color", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
471 PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "background-image", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
472 PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "key-input-focus",  Toolkit::Control::Property::KEY_INPUT_FOCUS,  Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
473
474 Toolkit::Control Control::New()
475 {
476   // Create the implementation, temporarily owned on stack
477   IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) );
478
479   // Pass ownership to handle
480   Toolkit::Control handle( *controlImpl );
481
482   // Second-phase init of the implementation
483   // This can only be done after the CustomActor connection has been made...
484   controlImpl->Initialize();
485
486   return handle;
487 }
488
489 Control::~Control()
490 {
491   delete mImpl;
492 }
493
494 void Control::SetStyleName( const std::string& styleName )
495 {
496   if( styleName != mImpl->mStyleName )
497   {
498     mImpl->mStyleName = styleName;
499
500     // Apply new style
501     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
502     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
503   }
504 }
505
506 const std::string& Control::GetStyleName() const
507 {
508   return mImpl->mStyleName;
509 }
510
511 void Control::SetBackgroundColor( const Vector4& color )
512 {
513   Background& background( mImpl->GetBackground() );
514
515   if ( background.actor )
516   {
517     // Just set the actor color
518     background.actor.SetColor( color );
519   }
520   else
521   {
522     // Create Mesh Actor
523     MeshActor meshActor = MeshActor::New( CreateMesh() );
524
525     SetupBackgroundActorConstrained( meshActor, Actor::Property::SCALE, color );
526
527     background.actor = meshActor;
528     // Set the flag to avoid notifying children
529     mImpl->mAddRemoveBackgroundChild = true;
530     // use insert to guarantee its the first child (so that OVERLAY mode works)
531     Self().Insert( 0, meshActor );
532     mImpl->mAddRemoveBackgroundChild = false;
533   }
534
535   background.color = color;
536 }
537
538 Vector4 Control::GetBackgroundColor() const
539 {
540   if ( mImpl->mBackground )
541   {
542     return mImpl->mBackground->color;
543   }
544   return Color::TRANSPARENT;
545 }
546
547 void Control::SetBackgroundImage( Image image )
548 {
549   Background& background( mImpl->GetBackground() );
550
551   if ( background.actor )
552   {
553     // Remove Current actor, unset AFTER removal
554     mImpl->mAddRemoveBackgroundChild = true;
555     Self().Remove( background.actor );
556     mImpl->mAddRemoveBackgroundChild = false;
557     background.actor.Reset();
558   }
559
560   ImageActor imageActor = ImageActor::New( image );
561   SetupBackgroundActor( imageActor, background.color );
562
563   // Set the background actor before adding so that we do not inform derived classes
564   background.actor = imageActor;
565   mImpl->mAddRemoveBackgroundChild = true;
566   // use insert to guarantee its the first child (so that OVERLAY mode works)
567   Self().Insert( 0, imageActor );
568   mImpl->mAddRemoveBackgroundChild = false;
569 }
570
571 void Control::ClearBackground()
572 {
573   if ( mImpl->mBackground )
574   {
575     Background& background( mImpl->GetBackground() );
576     mImpl->mAddRemoveBackgroundChild = true;
577     Self().Remove( background.actor );
578     mImpl->mAddRemoveBackgroundChild = false;
579
580     delete mImpl->mBackground;
581     mImpl->mBackground = NULL;
582   }
583 }
584
585 void Control::EnableGestureDetection(Gesture::Type type)
586 {
587   if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
588   {
589     mImpl->mPinchGestureDetector = PinchGestureDetector::New();
590     mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
591     mImpl->mPinchGestureDetector.Attach(Self());
592   }
593
594   if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
595   {
596     mImpl->mPanGestureDetector = PanGestureDetector::New();
597     mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
598     mImpl->mPanGestureDetector.Attach(Self());
599   }
600
601   if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
602   {
603     mImpl->mTapGestureDetector = TapGestureDetector::New();
604     mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
605     mImpl->mTapGestureDetector.Attach(Self());
606   }
607
608   if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
609   {
610     mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
611     mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
612     mImpl->mLongPressGestureDetector.Attach(Self());
613   }
614 }
615
616 void Control::DisableGestureDetection(Gesture::Type type)
617 {
618   if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
619   {
620     mImpl->mPinchGestureDetector.Detach(Self());
621     mImpl->mPinchGestureDetector.Reset();
622   }
623
624   if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
625   {
626     mImpl->mPanGestureDetector.Detach(Self());
627     mImpl->mPanGestureDetector.Reset();
628   }
629
630   if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
631   {
632     mImpl->mTapGestureDetector.Detach(Self());
633     mImpl->mTapGestureDetector.Reset();
634   }
635
636   if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
637   {
638     mImpl->mLongPressGestureDetector.Detach(Self());
639     mImpl->mLongPressGestureDetector.Reset();
640   }
641 }
642
643 PinchGestureDetector Control::GetPinchGestureDetector() const
644 {
645   return mImpl->mPinchGestureDetector;
646 }
647
648 PanGestureDetector Control::GetPanGestureDetector() const
649 {
650   return mImpl->mPanGestureDetector;
651 }
652
653 TapGestureDetector Control::GetTapGestureDetector() const
654 {
655   return mImpl->mTapGestureDetector;
656 }
657
658 LongPressGestureDetector Control::GetLongPressGestureDetector() const
659 {
660   return mImpl->mLongPressGestureDetector;
661 }
662
663 void Control::SetKeyboardNavigationSupport(bool isSupported)
664 {
665   mImpl->mIsKeyboardNavigationSupported = isSupported;
666 }
667
668 bool Control::IsKeyboardNavigationSupported()
669 {
670   return mImpl->mIsKeyboardNavigationSupported;
671 }
672
673 void Control::SetKeyInputFocus()
674 {
675   if( Self().OnStage() )
676   {
677     Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
678   }
679 }
680
681 bool Control::HasKeyInputFocus()
682 {
683   bool result = false;
684   if( Self().OnStage() )
685   {
686     result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
687   }
688   return result;
689 }
690
691 void Control::ClearKeyInputFocus()
692 {
693   if( Self().OnStage() )
694   {
695     Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
696   }
697 }
698
699 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
700 {
701   mImpl->mIsKeyboardFocusGroup = isFocusGroup;
702
703   // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
704   Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
705 }
706
707 bool Control::IsKeyboardFocusGroup()
708 {
709   return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
710 }
711
712 void Control::AccessibilityActivate()
713 {
714   // Inform deriving classes
715   OnAccessibilityActivated();
716 }
717
718 bool Control::OnAccessibilityActivated()
719 {
720   return false; // Accessibility activation is not handled by default
721 }
722
723 bool Control::OnAccessibilityPan(PanGesture gesture)
724 {
725   return false; // Accessibility pan gesture is not handled by default
726 }
727
728 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
729 {
730   return false; // Accessibility touch event is not handled by default
731 }
732
733 bool Control::OnAccessibilityValueChange(bool isIncrease)
734 {
735   return false; // Accessibility value change action is not handled by default
736 }
737
738 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
739 {
740   return Actor();
741 }
742
743 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
744 {
745 }
746
747 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
748 {
749   return mImpl->mKeyEventSignal;
750 }
751
752 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusGainedSignal()
753 {
754   return mImpl->mKeyInputFocusGainedSignal;
755 }
756
757 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusLostSignal()
758 {
759   return mImpl->mKeyInputFocusLostSignal;
760 }
761
762 bool Control::EmitKeyEventSignal( const KeyEvent& event )
763 {
764   // Guard against destruction during signal emission
765   Dali::Toolkit::Control handle( GetOwner() );
766
767   bool consumed = false;
768
769   // signals are allocated dynamically when someone connects
770   if ( !mImpl->mKeyEventSignal.Empty() )
771   {
772     consumed = mImpl->mKeyEventSignal.Emit( handle, event );
773   }
774
775   if (!consumed)
776   {
777     // Notification for derived classes
778     consumed = OnKeyEvent(event);
779   }
780
781   return consumed;
782 }
783
784 Control::Control( ControlBehaviour behaviourFlags )
785 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
786   mImpl(new Impl(*this))
787 {
788   mImpl->mFlags = behaviourFlags;
789 }
790
791 void Control::Initialize()
792 {
793   // Call deriving classes so initialised before styling is applied to them.
794   OnInitialize();
795
796   if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
797   {
798     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
799
800     // Register for style changes
801     styleManager.StyleChangeSignal().Connect( this, &Control::OnStyleChange );
802
803     // Apply the current style
804     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
805   }
806
807   if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
808   {
809     SetKeyboardNavigationSupport( true );
810   }
811 }
812
813 void Control::OnInitialize()
814 {
815 }
816
817 void Control::OnControlChildAdd( Actor& child )
818 {
819 }
820
821 void Control::OnControlChildRemove( Actor& child )
822 {
823 }
824
825 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
826 {
827   // By default the control is only interested in theme (not font) changes
828   if( change == StyleChange::THEME_CHANGE )
829   {
830     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
831   }
832 }
833
834 void Control::OnPinch(const PinchGesture& pinch)
835 {
836   if( !( mImpl->mStartingPinchScale ) )
837   {
838     // lazy allocate
839     mImpl->mStartingPinchScale = new Vector3;
840   }
841
842   if( pinch.state == Gesture::Started )
843   {
844     *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
845   }
846
847   Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
848 }
849
850 void Control::OnPan( const PanGesture& pan )
851 {
852 }
853
854 void Control::OnTap(const TapGesture& tap)
855 {
856 }
857
858 void Control::OnLongPress( const LongPressGesture& longPress )
859 {
860 }
861
862 void Control::EmitKeyInputFocusSignal( bool focusGained )
863 {
864   Dali::Toolkit::Control handle( GetOwner() );
865
866   if ( focusGained )
867   {
868      // signals are allocated dynamically when someone connects
869      if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
870      {
871       mImpl->mKeyInputFocusGainedSignal.Emit( handle );
872      }
873   }
874   else
875   {
876     // signals are allocated dynamically when someone connects
877     if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
878     {
879       mImpl->mKeyInputFocusLostSignal.Emit( handle );
880     }
881   }
882 }
883
884 void Control::OnStageConnection()
885 {
886 }
887
888 void Control::OnStageDisconnection()
889 {
890 }
891
892 void Control::OnKeyInputFocusGained()
893 {
894   EmitKeyInputFocusSignal( true );
895 }
896
897 void Control::OnKeyInputFocusLost()
898 {
899   EmitKeyInputFocusSignal( false );
900 }
901
902 void Control::OnChildAdd(Actor& child)
903 {
904   // If this is the background actor, then we do not want to inform deriving classes
905   if ( mImpl->mAddRemoveBackgroundChild )
906   {
907     return;
908   }
909
910   // Notify derived classes.
911   OnControlChildAdd( child );
912 }
913
914 void Control::OnChildRemove(Actor& child)
915 {
916   // If this is the background actor, then we do not want to inform deriving classes
917   if ( mImpl->mAddRemoveBackgroundChild )
918   {
919     return;
920   }
921
922   // Notify derived classes.
923   OnControlChildRemove( child );
924 }
925
926 void Control::OnSizeSet(const Vector3& targetSize)
927 {
928   // Background is resized through size negotiation
929 }
930
931 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
932 {
933   // @todo size negotiate background to new size, animate as well?
934 }
935
936 bool Control::OnTouchEvent(const TouchEvent& event)
937 {
938   return false; // Do not consume
939 }
940
941 bool Control::OnHoverEvent(const HoverEvent& event)
942 {
943   return false; // Do not consume
944 }
945
946 bool Control::OnKeyEvent(const KeyEvent& event)
947 {
948   return false; // Do not consume
949 }
950
951 bool Control::OnWheelEvent(const WheelEvent& event)
952 {
953   return false; // Do not consume
954 }
955
956 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
957 {
958   for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
959   {
960     container.Add( Self().GetChildAt( i ), size );
961   }
962 }
963
964 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
965 {
966 }
967
968 Vector3 Control::GetNaturalSize()
969 {
970   if( mImpl->mBackground )
971   {
972     Actor actor = mImpl->mBackground->actor;
973     if( actor )
974     {
975       return actor.GetNaturalSize();
976     }
977   }
978   return Vector3();
979 }
980
981 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
982 {
983   return CalculateChildSizeBase( child, dimension );
984 }
985
986 float Control::GetHeightForWidth( float width )
987 {
988   if( mImpl->mBackground )
989   {
990     Actor actor = mImpl->mBackground->actor;
991     if( actor )
992     {
993       return actor.GetHeightForWidth( width );
994     }
995   }
996   return GetHeightForWidthBase( width );
997 }
998
999 float Control::GetWidthForHeight( float height )
1000 {
1001   if( mImpl->mBackground )
1002   {
1003     Actor actor = mImpl->mBackground->actor;
1004     if( actor )
1005     {
1006       return actor.GetWidthForHeight( height );
1007     }
1008   }
1009   return GetWidthForHeightBase( height );
1010 }
1011
1012 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1013 {
1014   return RelayoutDependentOnChildrenBase( dimension );
1015 }
1016
1017 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1018 {
1019 }
1020
1021 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1022 {
1023 }
1024
1025 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1026 {
1027   mImpl->SignalConnected( slotObserver, callback );
1028 }
1029
1030 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1031 {
1032   mImpl->SignalDisconnected( slotObserver, callback );
1033 }
1034
1035 Control& GetImplementation( Dali::Toolkit::Control& handle )
1036 {
1037   CustomActorImpl& customInterface = handle.GetImplementation();
1038   // downcast to control
1039   Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1040   return impl;
1041 }
1042
1043 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1044 {
1045   const CustomActorImpl& customInterface = handle.GetImplementation();
1046   // downcast to control
1047   const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1048   return impl;
1049 }
1050
1051 } // namespace Internal
1052
1053 } // namespace Toolkit
1054
1055 } // namespace Dali