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