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