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