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