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