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