Control::UnregisterVisual does not remove renderers from actor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / control-impl.cpp
1 /*
2  * Copyright (c) 2016 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/animation/constraint.h>
26 #include <dali/public-api/animation/constraints.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/public-api/object/type-registry-helper.h>
29 #include <dali/public-api/rendering/renderer.h>
30 #include <dali/public-api/size-negotiation/relayout-container.h>
31 #include <dali/devel-api/common/owner-container.h>
32 #include <dali/devel-api/scripting/scripting.h>
33 #include <dali/integration-api/debug.h>
34
35 // INTERNAL INCLUDES
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/public-api/visuals/color-visual-properties.h>
40 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
41 #include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
42 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
43 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
44 #include <dali-toolkit/internal/styling/style-manager-impl.h>
45 #include <dali-toolkit/internal/visuals/color/color-visual.h>
46 #include <dali-toolkit/internal/visuals/transition-data-impl.h>
47
48 namespace Dali
49 {
50
51 namespace Toolkit
52 {
53
54 namespace
55 {
56
57 #if defined(DEBUG_ENABLED)
58 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::General, false, "LOG_CONTROL_VISUALS");
59 #endif
60
61 /**
62  * Struct used to store Visual within the control, index is a unique key for each visual.
63  */
64 struct RegisteredVisual
65 {
66   Property::Index index;
67   Toolkit::Visual::Base visual;
68   bool enabled;
69
70   RegisteredVisual( Property::Index aIndex, Toolkit::Visual::Base &aVisual, bool aEnabled) :
71                    index(aIndex), visual(aVisual), enabled(aEnabled) {}
72 };
73
74 struct HandleIndex
75 {
76   Handle handle; ///< a handle to the target object
77   Property::Index index; ///< The index of a property provided by the referenced object
78
79   HandleIndex( )
80   : handle(),
81     index( Property::INVALID_INDEX )
82   {
83   }
84
85   HandleIndex( Handle& handle, Property::Index index )
86   : handle( handle ),
87     index( index )
88   {
89   }
90 };
91
92
93 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
94
95 /**
96  *  Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
97  */
98 bool FindVisual( Property::Index targetIndex, RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
99 {
100   for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
101   {
102     if ( (*iter)->index ==  targetIndex )
103     {
104       return true;
105     }
106   }
107   return false;
108 }
109
110 HandleIndex GetVisualProperty(
111   Internal::Control& controlImpl,
112   RegisteredVisualContainer& visuals,
113   const std::string& visualName,
114   Property::Key propertyKey )
115 {
116 #if defined(DEBUG_ENABLED)
117   std::ostringstream oss;
118   oss << "Control::GetHandleIndex(" << visualName << ", " << propertyKey << ")" << std::endl;
119   DALI_LOG_INFO( gLogFilter, Debug::General, oss.str().c_str() );
120 #endif
121
122   // Find visualName in the control
123   RegisteredVisualContainer::Iterator iter;
124   for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
125   {
126     if ( (*iter)->visual.GetName() == visualName )
127     {
128       break;
129     }
130   }
131
132   // Does it's renderer have an associated property?
133   if( iter != visuals.End() )
134   {
135     Actor self = controlImpl.Self();
136     Property::Index index = self.GetPropertyIndex( propertyKey );
137     if( index != Property::INVALID_INDEX )
138     {
139       // It's an actor property:
140       return HandleIndex( self, index );
141     }
142     else
143     {
144       // Check if it is a renderer property:
145       if( self.GetRendererCount() > 0 )
146       {
147         // @todo Need to use correct renderer index
148         Renderer renderer = self.GetRendererAt(0);
149         Property::Index index = renderer.GetPropertyIndex( propertyKey );
150         if( index != Property::INVALID_INDEX )
151         {
152           // It's a renderer property:
153           return HandleIndex( renderer, index );
154         }
155       }
156       else
157       {
158         std::ostringstream oss;
159         oss << propertyKey;
160         DALI_LOG_WARNING( "Control::GetHandleIndex(%s, %s) No renderers\n", visualName.c_str(), oss.str().c_str() );
161       }
162     }
163   }
164   Handle handle;
165   return HandleIndex( handle, Property::INVALID_INDEX );
166 }
167
168
169 /**
170  * Creates control through type registry
171  */
172 BaseHandle Create()
173 {
174   return Internal::Control::New();
175 }
176
177 /**
178  * Performs actions as requested using the action name.
179  * @param[in] object The object on which to perform the action.
180  * @param[in] actionName The action to perform.
181  * @param[in] attributes The attributes with which to perfrom this action.
182  * @return true if action has been accepted by this control
183  */
184 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
185 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
186 {
187   bool ret = false;
188
189   if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
190   {
191     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
192     if( control )
193     {
194       // if cast succeeds there is an implementation so no need to check
195       ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
196     }
197   }
198
199   return ret;
200 }
201
202 /**
203  * Connects a callback function with the object's signals.
204  * @param[in] object The object providing the signal.
205  * @param[in] tracker Used to disconnect the signal.
206  * @param[in] signalName The signal to connect to.
207  * @param[in] functor A newly allocated FunctorDelegate.
208  * @return True if the signal was connected.
209  * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
210  */
211 const char* SIGNAL_KEY_EVENT = "keyEvent";
212 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
213 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
214 const char* SIGNAL_TAPPED = "tapped";
215 const char* SIGNAL_PANNED = "panned";
216 const char* SIGNAL_PINCHED = "pinched";
217 const char* SIGNAL_LONG_PRESSED = "longPressed";
218 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
219 {
220   Dali::BaseHandle handle( object );
221
222   bool connected( false );
223   Toolkit::Control control = Toolkit::Control::DownCast( handle );
224   if ( control )
225   {
226     Internal::Control& controlImpl( Internal::GetImplementation( control ) );
227     connected = true;
228
229     if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
230     {
231       controlImpl.KeyEventSignal().Connect( tracker, functor );
232     }
233     else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
234     {
235       controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
236     }
237     else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
238     {
239       controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
240     }
241     else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
242     {
243       controlImpl.EnableGestureDetection( Gesture::Tap );
244       controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
245     }
246     else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
247     {
248       controlImpl.EnableGestureDetection( Gesture::Pan );
249       controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
250     }
251     else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
252     {
253       controlImpl.EnableGestureDetection( Gesture::Pinch );
254       controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
255     }
256     else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
257     {
258       controlImpl.EnableGestureDetection( Gesture::LongPress );
259       controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
260     }
261   }
262   return connected;
263 }
264
265 // Setup signals and actions using the type-registry.
266 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
267
268 // Note: Properties are registered separately below.
269
270 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
271 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
272 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
273 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
274 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
275 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
276 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
277
278 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
279
280 DALI_TYPE_REGISTRATION_END()
281
282 } // unnamed namespace
283
284 namespace Internal
285 {
286
287 class Control::Impl : public ConnectionTracker
288 {
289 public:
290
291   // Construction & Destruction
292   Impl(Control& controlImpl)
293   : mControlImpl( controlImpl ),
294     mStyleName(""),
295     mBackgroundVisual(),
296     mBackgroundColor(Color::TRANSPARENT),
297     mStartingPinchScale( NULL ),
298     mKeyEventSignal(),
299     mPinchGestureDetector(),
300     mPanGestureDetector(),
301     mTapGestureDetector(),
302     mLongPressGestureDetector(),
303     mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
304     mIsKeyboardNavigationSupported( false ),
305     mIsKeyboardFocusGroup( false )
306   {
307   }
308
309   ~Impl()
310   {
311     // All gesture detectors will be destroyed so no need to disconnect.
312     delete mStartingPinchScale;
313   }
314
315   // Gesture Detection Methods
316
317   void PinchDetected(Actor actor, const PinchGesture& pinch)
318   {
319     mControlImpl.OnPinch(pinch);
320   }
321
322   void PanDetected(Actor actor, const PanGesture& pan)
323   {
324     mControlImpl.OnPan(pan);
325   }
326
327   void TapDetected(Actor actor, const TapGesture& tap)
328   {
329     mControlImpl.OnTap(tap);
330   }
331
332   void LongPressDetected(Actor actor, const LongPressGesture& longPress)
333   {
334     mControlImpl.OnLongPress(longPress);
335   }
336
337   // Properties
338
339   /**
340    * Called when a property of an object of this type is set.
341    * @param[in] object The object whose property is set.
342    * @param[in] index The property index.
343    * @param[in] value The new property value.
344    */
345   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
346   {
347     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
348
349     if ( control )
350     {
351       Control& controlImpl( GetImplementation( control ) );
352
353       switch ( index )
354       {
355         case Toolkit::Control::Property::STYLE_NAME:
356         {
357           controlImpl.SetStyleName( value.Get< std::string >() );
358           break;
359         }
360
361         case Toolkit::Control::Property::BACKGROUND_COLOR:
362         {
363           DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
364           controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
365           break;
366         }
367
368         case Toolkit::Control::Property::BACKGROUND_IMAGE:
369         {
370           DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
371           Image image = Scripting::NewImage( value );
372           if ( image )
373           {
374             controlImpl.SetBackgroundImage( image );
375           }
376           else
377           {
378             // An empty map means the background is no longer required
379             controlImpl.ClearBackground();
380           }
381           break;
382         }
383
384         case Toolkit::Control::Property::KEY_INPUT_FOCUS:
385         {
386           if ( value.Get< bool >() )
387           {
388             controlImpl.SetKeyInputFocus();
389           }
390           else
391           {
392             controlImpl.ClearKeyInputFocus();
393           }
394           break;
395         }
396
397         case Toolkit::Control::Property::BACKGROUND:
398         {
399           const Property::Map* map = value.GetMap();
400           if( map )
401           {
402             controlImpl.SetBackground( *map );
403           }
404           else
405           {
406             // The background is not a property map, so we should clear the background
407             controlImpl.ClearBackground();
408           }
409           break;
410         }
411       }
412     }
413   }
414
415   /**
416    * Called to retrieve a property of an object of this type.
417    * @param[in] object The object whose property is to be retrieved.
418    * @param[in] index The property index.
419    * @return The current value of the property.
420    */
421   static Property::Value GetProperty( BaseObject* object, Property::Index index )
422   {
423     Property::Value value;
424
425     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
426
427     if ( control )
428     {
429       Control& controlImpl( GetImplementation( control ) );
430
431       switch ( index )
432       {
433         case Toolkit::Control::Property::STYLE_NAME:
434         {
435           value = controlImpl.GetStyleName();
436           break;
437         }
438
439         case Toolkit::Control::Property::BACKGROUND_COLOR:
440         {
441           DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
442           value = controlImpl.GetBackgroundColor();
443           break;
444         }
445
446         case Toolkit::Control::Property::BACKGROUND_IMAGE:
447         {
448           DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
449           Property::Map map;
450           if( controlImpl.mImpl->mBackgroundVisual )
451           {
452             controlImpl.mImpl->mBackgroundVisual.CreatePropertyMap( map );
453           }
454           value = map;
455           break;
456         }
457
458         case Toolkit::Control::Property::KEY_INPUT_FOCUS:
459         {
460           value = controlImpl.HasKeyInputFocus();
461           break;
462         }
463
464         case Toolkit::Control::Property::BACKGROUND:
465         {
466           Property::Map map;
467           if( controlImpl.mImpl->mBackgroundVisual )
468           {
469             (controlImpl.mImpl->mBackgroundVisual).CreatePropertyMap( map );
470           }
471
472           value = map;
473           break;
474         }
475
476       }
477     }
478
479     return value;
480   }
481
482   // Data
483
484   Control& mControlImpl;
485   RegisteredVisualContainer mVisuals; // Stores visuals needed by the control, non trivial type so std::vector used.
486   std::string mStyleName;
487   Toolkit::Visual::Base mBackgroundVisual;   ///< The visual to render the background
488   Vector4 mBackgroundColor;                       ///< The color of the background visual
489   Vector3* mStartingPinchScale;      ///< The scale when a pinch gesture starts, TODO: consider removing this
490   Toolkit::Control::KeyEventSignalType mKeyEventSignal;
491   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
492   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
493
494   // Gesture Detection
495   PinchGestureDetector mPinchGestureDetector;
496   PanGestureDetector mPanGestureDetector;
497   TapGestureDetector mTapGestureDetector;
498   LongPressGestureDetector mLongPressGestureDetector;
499
500   ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT;    ///< Flags passed in from constructor.
501   bool mIsKeyboardNavigationSupported :1;  ///< Stores whether keyboard navigation is supported by the control.
502   bool mIsKeyboardFocusGroup :1;           ///< Stores whether the control is a focus group.
503
504   // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
505   static const PropertyRegistration PROPERTY_1;
506   static const PropertyRegistration PROPERTY_2;
507   static const PropertyRegistration PROPERTY_3;
508   static const PropertyRegistration PROPERTY_4;
509   static const PropertyRegistration PROPERTY_5;
510 };
511
512 // Properties registered without macro to use specific member variables.
513 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName",       Toolkit::Control::Property::STYLE_NAME,       Property::STRING,  &Control::Impl::SetProperty, &Control::Impl::GetProperty );
514 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
515 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
516 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus",   Toolkit::Control::Property::KEY_INPUT_FOCUS,  Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
517 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background",      Toolkit::Control::Property::BACKGROUND,       Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
518
519 Toolkit::Control Control::New()
520 {
521   // Create the implementation, temporarily owned on stack
522   IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
523
524   // Pass ownership to handle
525   Toolkit::Control handle( *controlImpl );
526
527   // Second-phase init of the implementation
528   // This can only be done after the CustomActor connection has been made...
529   controlImpl->Initialize();
530
531   return handle;
532 }
533
534 Control::~Control()
535 {
536   delete mImpl;
537 }
538
539 void Control::SetStyleName( const std::string& styleName )
540 {
541   if( styleName != mImpl->mStyleName )
542   {
543     mImpl->mStyleName = styleName;
544
545     // Apply new style, if stylemanager is available
546     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
547     if( styleManager )
548     {
549       GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
550     }
551   }
552 }
553
554 const std::string& Control::GetStyleName() const
555 {
556   return mImpl->mStyleName;
557 }
558
559 void Control::SetBackgroundColor( const Vector4& color )
560 {
561   mImpl->mBackgroundColor = color;
562   Property::Map map;
563   map[ Toolkit::VisualProperty::TYPE ] = Toolkit::Visual::COLOR;
564   map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
565   mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( map );
566   RegisterVisual( Toolkit::Control::Property::BACKGROUND, mImpl->mBackgroundVisual );
567   if( mImpl->mBackgroundVisual )
568   {
569     mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
570   }
571 }
572
573 Vector4 Control::GetBackgroundColor() const
574 {
575   return mImpl->mBackgroundColor;
576 }
577
578 void Control::SetBackground( const Property::Map& map )
579 {
580   mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( map );
581   RegisterVisual( Toolkit::Control::Property::BACKGROUND, mImpl->mBackgroundVisual );
582   if( mImpl->mBackgroundVisual )
583   {
584     mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
585   }
586 }
587
588 void Control::SetBackgroundImage( Image image )
589 {
590   mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
591   RegisterVisual( Toolkit::Control::Property::BACKGROUND, mImpl->mBackgroundVisual );
592   if( mImpl->mBackgroundVisual )
593   {
594     mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
595   }
596 }
597
598 void Control::ClearBackground()
599 {
600   if( mImpl->mBackgroundVisual )
601   {
602     UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
603     mImpl->mBackgroundVisual.Reset();
604   }
605   mImpl->mBackgroundColor = Color::TRANSPARENT;
606 }
607
608 void Control::EnableGestureDetection(Gesture::Type type)
609 {
610   if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
611   {
612     mImpl->mPinchGestureDetector = PinchGestureDetector::New();
613     mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
614     mImpl->mPinchGestureDetector.Attach(Self());
615   }
616
617   if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
618   {
619     mImpl->mPanGestureDetector = PanGestureDetector::New();
620     mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
621     mImpl->mPanGestureDetector.Attach(Self());
622   }
623
624   if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
625   {
626     mImpl->mTapGestureDetector = TapGestureDetector::New();
627     mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
628     mImpl->mTapGestureDetector.Attach(Self());
629   }
630
631   if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
632   {
633     mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
634     mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
635     mImpl->mLongPressGestureDetector.Attach(Self());
636   }
637 }
638
639 void Control::DisableGestureDetection(Gesture::Type type)
640 {
641   if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
642   {
643     mImpl->mPinchGestureDetector.Detach(Self());
644     mImpl->mPinchGestureDetector.Reset();
645   }
646
647   if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
648   {
649     mImpl->mPanGestureDetector.Detach(Self());
650     mImpl->mPanGestureDetector.Reset();
651   }
652
653   if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
654   {
655     mImpl->mTapGestureDetector.Detach(Self());
656     mImpl->mTapGestureDetector.Reset();
657   }
658
659   if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
660   {
661     mImpl->mLongPressGestureDetector.Detach(Self());
662     mImpl->mLongPressGestureDetector.Reset();
663   }
664 }
665
666 PinchGestureDetector Control::GetPinchGestureDetector() const
667 {
668   return mImpl->mPinchGestureDetector;
669 }
670
671 PanGestureDetector Control::GetPanGestureDetector() const
672 {
673   return mImpl->mPanGestureDetector;
674 }
675
676 TapGestureDetector Control::GetTapGestureDetector() const
677 {
678   return mImpl->mTapGestureDetector;
679 }
680
681 LongPressGestureDetector Control::GetLongPressGestureDetector() const
682 {
683   return mImpl->mLongPressGestureDetector;
684 }
685
686 void Control::SetKeyboardNavigationSupport(bool isSupported)
687 {
688   mImpl->mIsKeyboardNavigationSupported = isSupported;
689 }
690
691 bool Control::IsKeyboardNavigationSupported()
692 {
693   return mImpl->mIsKeyboardNavigationSupported;
694 }
695
696 void Control::SetKeyInputFocus()
697 {
698   if( Self().OnStage() )
699   {
700     Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
701   }
702 }
703
704 bool Control::HasKeyInputFocus()
705 {
706   bool result = false;
707   if( Self().OnStage() )
708   {
709     result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
710   }
711   return result;
712 }
713
714 void Control::ClearKeyInputFocus()
715 {
716   if( Self().OnStage() )
717   {
718     Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
719   }
720 }
721
722 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
723 {
724   mImpl->mIsKeyboardFocusGroup = isFocusGroup;
725
726   // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
727   Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
728 }
729
730 bool Control::IsKeyboardFocusGroup()
731 {
732   return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
733 }
734
735 void Control::AccessibilityActivate()
736 {
737   // Inform deriving classes
738   OnAccessibilityActivated();
739 }
740
741 void Control::KeyboardEnter()
742 {
743   // Inform deriving classes
744   OnKeyboardEnter();
745 }
746
747 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
748 {
749   RegisterVisual( index, visual, true );
750 }
751
752 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
753 {
754   bool visualReplaced ( false );
755   Actor self = Self();
756
757   if ( !mImpl->mVisuals.Empty() )
758   {
759       RegisteredVisualContainer::Iterator iter;
760       // Check if visual (index) is already registered.  Replace if so.
761       if ( FindVisual( index, mImpl->mVisuals, iter ) )
762       {
763         if( (*iter)->visual && self.OnStage() )
764         {
765           Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
766         }
767         (*iter)->visual = visual;
768         visualReplaced = true;
769       }
770   }
771
772   if ( !visualReplaced ) // New registration entry
773   {
774     mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
775   }
776
777   if( visual && self.OnStage() && enabled )
778   {
779     Toolkit::GetImplementation(visual).SetOnStage( self );
780   }
781 }
782
783 void Control::UnregisterVisual( Property::Index index )
784 {
785    RegisteredVisualContainer::Iterator iter;
786    if ( FindVisual( index, mImpl->mVisuals, iter ) )
787    {
788      Actor self( Self() );
789      Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
790      (*iter)->visual.Reset();
791      mImpl->mVisuals.Erase( iter );
792    }
793 }
794
795 Toolkit::Visual::Base Control::GetVisual( Property::Index index ) const
796 {
797   RegisteredVisualContainer::Iterator iter;
798   if ( FindVisual( index, mImpl->mVisuals, iter ) )
799   {
800     return (*iter)->visual;
801   }
802
803   return Toolkit::Visual::Base();
804 }
805
806 void Control::EnableVisual( Property::Index index, bool enable )
807 {
808   RegisteredVisualContainer::Iterator iter;
809   if ( FindVisual( index, mImpl->mVisuals, iter ) )
810   {
811     if (  (*iter)->enabled == enable )
812     {
813       return;
814     }
815
816     (*iter)->enabled = enable;
817     Actor parentActor = Self();
818     if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
819     {
820       if ( enable )
821       {
822
823         Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
824       }
825       else
826       {
827         Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor );  // No need to call if control not staged.
828       }
829     }
830   }
831 }
832
833 bool Control::IsVisualEnabled( Property::Index index ) const
834 {
835   RegisteredVisualContainer::Iterator iter;
836   if ( FindVisual( index, mImpl->mVisuals, iter ) )
837   {
838     return (*iter)->enabled;
839   }
840   return false;
841 }
842
843 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
844 {
845   Dali::Animation transition;
846   const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
847
848   if( transitionData.Count() > 0 )
849   {
850     // Setup a Transition from TransitionData.
851     TransitionData::Iterator end = transitionData.End();
852     for( TransitionData::Iterator iter = transitionData.Begin() ;
853          iter != end; ++iter )
854     {
855       TransitionData::Animator* animator = (*iter);
856       HandleIndex handleIndex;
857
858       // Attempt to find the object name as a child actor
859       Actor child = Self().FindChildByName( animator->objectName );
860       if( child )
861       {
862         Property::Index propertyIndex = child.GetPropertyIndex( animator->propertyKey );
863         handleIndex = HandleIndex( child, propertyIndex );
864       }
865       else
866       {
867         handleIndex = GetVisualProperty( *this, mImpl->mVisuals,
868                                             animator->objectName,
869                                             animator->propertyKey );
870       }
871
872       if( handleIndex.handle && handleIndex.index != Property::INVALID_INDEX )
873       {
874         if( animator->animate == false )
875         {
876           if( animator->targetValue.GetType() != Property::NONE )
877           {
878             handleIndex.handle.SetProperty( handleIndex.index, animator->targetValue );
879           }
880         }
881         else
882         {
883           if( animator->initialValue.GetType() != Property::NONE )
884           {
885             handleIndex.handle.SetProperty( handleIndex.index, animator->initialValue );
886           }
887
888           if( ! transition )
889           {
890             // Create an animation with a default .1 second duration - the animators
891             // will automatically force it to the 'right' duration.
892             transition = Dali::Animation::New( 0.1f );
893           }
894
895           transition.AnimateTo( Property( handleIndex.handle, handleIndex.index ),
896                                 animator->targetValue,
897                                 animator->alphaFunction,
898                                 TimePeriod( animator->timePeriodDelay,
899                                             animator->timePeriodDuration ) );
900         }
901       }
902     }
903   }
904
905   return transition;
906 }
907
908 bool Control::OnAccessibilityActivated()
909 {
910   return false; // Accessibility activation is not handled by default
911 }
912
913 bool Control::OnKeyboardEnter()
914 {
915   return false; // Keyboard enter is not handled by default
916 }
917
918 bool Control::OnAccessibilityPan(PanGesture gesture)
919 {
920   return false; // Accessibility pan gesture is not handled by default
921 }
922
923 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
924 {
925   return false; // Accessibility touch event is not handled by default
926 }
927
928 bool Control::OnAccessibilityValueChange(bool isIncrease)
929 {
930   return false; // Accessibility value change action is not handled by default
931 }
932
933 bool Control::OnAccessibilityZoom()
934 {
935   return false; // Accessibility zoom action is not handled by default
936 }
937
938 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
939 {
940   return Actor();
941 }
942
943 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
944 {
945 }
946
947 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
948 {
949   return mImpl->mKeyEventSignal;
950 }
951
952 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
953 {
954   return mImpl->mKeyInputFocusGainedSignal;
955 }
956
957 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
958 {
959   return mImpl->mKeyInputFocusLostSignal;
960 }
961
962 bool Control::EmitKeyEventSignal( const KeyEvent& event )
963 {
964   // Guard against destruction during signal emission
965   Dali::Toolkit::Control handle( GetOwner() );
966
967   bool consumed = false;
968
969   // signals are allocated dynamically when someone connects
970   if ( !mImpl->mKeyEventSignal.Empty() )
971   {
972     consumed = mImpl->mKeyEventSignal.Emit( handle, event );
973   }
974
975   if (!consumed)
976   {
977     // Notification for derived classes
978     consumed = OnKeyEvent(event);
979   }
980
981   return consumed;
982 }
983
984 Control::Control( ControlBehaviour behaviourFlags )
985 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
986   mImpl(new Impl(*this))
987 {
988   mImpl->mFlags = behaviourFlags;
989 }
990
991 void Control::Initialize()
992 {
993   // Call deriving classes so initialised before styling is applied to them.
994   OnInitialize();
995
996   if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
997       !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
998   {
999     Toolkit::StyleManager styleManager = StyleManager::Get();
1000
1001     // if stylemanager is available
1002     if( styleManager )
1003     {
1004       StyleManager& styleManagerImpl = GetImpl( styleManager );
1005
1006       // Register for style changes
1007       styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
1008
1009       // Apply the current style
1010       styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
1011     }
1012   }
1013
1014   if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
1015   {
1016     SetKeyboardNavigationSupport( true );
1017   }
1018 }
1019
1020 void Control::OnInitialize()
1021 {
1022 }
1023
1024 void Control::OnControlChildAdd( Actor& child )
1025 {
1026 }
1027
1028 void Control::OnControlChildRemove( Actor& child )
1029 {
1030 }
1031
1032 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
1033 {
1034   // By default the control is only interested in theme (not font) changes
1035   if( styleManager && change == StyleChange::THEME_CHANGE )
1036   {
1037     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1038   }
1039   RelayoutRequest();
1040 }
1041
1042 void Control::OnPinch(const PinchGesture& pinch)
1043 {
1044   if( !( mImpl->mStartingPinchScale ) )
1045   {
1046     // lazy allocate
1047     mImpl->mStartingPinchScale = new Vector3;
1048   }
1049
1050   if( pinch.state == Gesture::Started )
1051   {
1052     *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1053   }
1054
1055   Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1056 }
1057
1058 void Control::OnPan( const PanGesture& pan )
1059 {
1060 }
1061
1062 void Control::OnTap(const TapGesture& tap)
1063 {
1064 }
1065
1066 void Control::OnLongPress( const LongPressGesture& longPress )
1067 {
1068 }
1069
1070 void Control::EmitKeyInputFocusSignal( bool focusGained )
1071 {
1072   Dali::Toolkit::Control handle( GetOwner() );
1073
1074   if ( focusGained )
1075   {
1076     // signals are allocated dynamically when someone connects
1077     if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
1078     {
1079       mImpl->mKeyInputFocusGainedSignal.Emit( handle );
1080     }
1081   }
1082   else
1083   {
1084     // signals are allocated dynamically when someone connects
1085     if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
1086     {
1087       mImpl->mKeyInputFocusLostSignal.Emit( handle );
1088     }
1089   }
1090 }
1091
1092 void Control::OnStageConnection( int depth )
1093 {
1094   for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1095   {
1096     // Check whether the visual is empty and enabled
1097     if( (*iter)->visual && (*iter)->enabled )
1098     {
1099       Actor self( Self() );
1100       Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
1101     }
1102   }
1103 }
1104
1105 void Control::OnStageDisconnection()
1106 {
1107   for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1108   {
1109     // Check whether the visual is empty
1110     if( (*iter)->visual )
1111     {
1112       Actor self( Self() );
1113       Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
1114     }
1115   }
1116 }
1117
1118 void Control::OnKeyInputFocusGained()
1119 {
1120   EmitKeyInputFocusSignal( true );
1121 }
1122
1123 void Control::OnKeyInputFocusLost()
1124 {
1125   EmitKeyInputFocusSignal( false );
1126 }
1127
1128 void Control::OnChildAdd(Actor& child)
1129 {
1130   // Notify derived classes.
1131   OnControlChildAdd( child );
1132 }
1133
1134 void Control::OnChildRemove(Actor& child)
1135 {
1136   // Notify derived classes.
1137   OnControlChildRemove( child );
1138 }
1139
1140 void Control::OnSizeSet(const Vector3& targetSize)
1141 {
1142   if( mImpl->mBackgroundVisual )
1143   {
1144     Vector2 size( targetSize );
1145     mImpl->mBackgroundVisual.SetSize( size );
1146   }
1147 }
1148
1149 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1150 {
1151   // @todo size negotiate background to new size, animate as well?
1152 }
1153
1154 bool Control::OnTouchEvent(const TouchEvent& event)
1155 {
1156   return false; // Do not consume
1157 }
1158
1159 bool Control::OnHoverEvent(const HoverEvent& event)
1160 {
1161   return false; // Do not consume
1162 }
1163
1164 bool Control::OnKeyEvent(const KeyEvent& event)
1165 {
1166   return false; // Do not consume
1167 }
1168
1169 bool Control::OnWheelEvent(const WheelEvent& event)
1170 {
1171   return false; // Do not consume
1172 }
1173
1174 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1175 {
1176   for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1177   {
1178     container.Add( Self().GetChildAt( i ), size );
1179   }
1180 }
1181
1182 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1183 {
1184 }
1185
1186 Vector3 Control::GetNaturalSize()
1187 {
1188   if( mImpl->mBackgroundVisual )
1189   {
1190     Vector2 naturalSize;
1191     mImpl->mBackgroundVisual.GetNaturalSize(naturalSize);
1192     return Vector3(naturalSize);
1193   }
1194   return Vector3::ZERO;
1195 }
1196
1197 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1198 {
1199   return CalculateChildSizeBase( child, dimension );
1200 }
1201
1202 float Control::GetHeightForWidth( float width )
1203 {
1204   return GetHeightForWidthBase( width );
1205 }
1206
1207 float Control::GetWidthForHeight( float height )
1208 {
1209   return GetWidthForHeightBase( height );
1210 }
1211
1212 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1213 {
1214   return RelayoutDependentOnChildrenBase( dimension );
1215 }
1216
1217 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1218 {
1219 }
1220
1221 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1222 {
1223 }
1224
1225 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1226 {
1227   mImpl->SignalConnected( slotObserver, callback );
1228 }
1229
1230 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1231 {
1232   mImpl->SignalDisconnected( slotObserver, callback );
1233 }
1234
1235 Control& GetImplementation( Dali::Toolkit::Control& handle )
1236 {
1237   CustomActorImpl& customInterface = handle.GetImplementation();
1238   // downcast to control
1239   Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1240   return impl;
1241 }
1242
1243 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1244 {
1245   const CustomActorImpl& customInterface = handle.GetImplementation();
1246   // downcast to control
1247   const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1248   return impl;
1249 }
1250
1251 } // namespace Internal
1252
1253 } // namespace Toolkit
1254
1255 } // namespace Dali