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