Merge "Updated @since tags for APIs added since 1.2.0 that are required for Tizen...
[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 Toolkit::Visual::Base Control::GetVisual( Property::Index index )
695 {
696   std::vector< RegisteredVisual >::iterator iter;
697   if ( FindVisual( index, mImpl->mVisuals, iter ) )
698   {
699     return (*iter).visual;
700   }
701
702   return Toolkit::Visual::Base();
703 }
704
705 Actor Control::GetPlacementActor( Property::Index index )
706 {
707   std::vector< RegisteredVisual >::iterator iter;
708   if ( FindVisual( index, mImpl->mVisuals, iter ) )
709   {
710     if( (*iter).placementActor )
711     {
712       return (*iter).placementActor;
713     }
714     else
715     {
716       return Self();
717     }
718   }
719
720   return Actor();
721 }
722
723 bool Control::OnAccessibilityActivated()
724 {
725   return false; // Accessibility activation is not handled by default
726 }
727
728 bool Control::OnKeyboardEnter()
729 {
730   return false; // Keyboard enter is not handled by default
731 }
732
733 bool Control::OnAccessibilityPan(PanGesture gesture)
734 {
735   return false; // Accessibility pan gesture is not handled by default
736 }
737
738 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
739 {
740   return false; // Accessibility touch event is not handled by default
741 }
742
743 bool Control::OnAccessibilityValueChange(bool isIncrease)
744 {
745   return false; // Accessibility value change action is not handled by default
746 }
747
748 bool Control::OnAccessibilityZoom()
749 {
750   return false; // Accessibility zoom action is not handled by default
751 }
752
753 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
754 {
755   return Actor();
756 }
757
758 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
759 {
760 }
761
762 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
763 {
764   return mImpl->mKeyEventSignal;
765 }
766
767 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
768 {
769   return mImpl->mKeyInputFocusGainedSignal;
770 }
771
772 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
773 {
774   return mImpl->mKeyInputFocusLostSignal;
775 }
776
777 bool Control::EmitKeyEventSignal( const KeyEvent& event )
778 {
779   // Guard against destruction during signal emission
780   Dali::Toolkit::Control handle( GetOwner() );
781
782   bool consumed = false;
783
784   // signals are allocated dynamically when someone connects
785   if ( !mImpl->mKeyEventSignal.Empty() )
786   {
787     consumed = mImpl->mKeyEventSignal.Emit( handle, event );
788   }
789
790   if (!consumed)
791   {
792     // Notification for derived classes
793     consumed = OnKeyEvent(event);
794   }
795
796   return consumed;
797 }
798
799 Control::Control( ControlBehaviour behaviourFlags )
800 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
801   mImpl(new Impl(*this))
802 {
803   mImpl->mFlags = behaviourFlags;
804 }
805
806 void Control::Initialize()
807 {
808   // Call deriving classes so initialised before styling is applied to them.
809   OnInitialize();
810
811   if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
812   {
813     Toolkit::StyleManager styleManager = StyleManager::Get();
814
815     // if stylemanager is available
816     if( styleManager )
817     {
818       StyleManager& styleManagerImpl = GetImpl( styleManager );
819
820       // Register for style changes
821       styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
822
823       // Apply the current style
824       styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
825     }
826   }
827
828   if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
829   {
830     SetKeyboardNavigationSupport( true );
831   }
832 }
833
834 void Control::OnInitialize()
835 {
836 }
837
838 void Control::OnControlChildAdd( Actor& child )
839 {
840 }
841
842 void Control::OnControlChildRemove( Actor& child )
843 {
844 }
845
846 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
847 {
848   // By default the control is only interested in theme (not font) changes
849   if( styleManager && change == StyleChange::THEME_CHANGE )
850   {
851     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
852   }
853   RelayoutRequest();
854 }
855
856 void Control::OnPinch(const PinchGesture& pinch)
857 {
858   if( !( mImpl->mStartingPinchScale ) )
859   {
860     // lazy allocate
861     mImpl->mStartingPinchScale = new Vector3;
862   }
863
864   if( pinch.state == Gesture::Started )
865   {
866     *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
867   }
868
869   Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
870 }
871
872 void Control::OnPan( const PanGesture& pan )
873 {
874 }
875
876 void Control::OnTap(const TapGesture& tap)
877 {
878 }
879
880 void Control::OnLongPress( const LongPressGesture& longPress )
881 {
882 }
883
884 void Control::EmitKeyInputFocusSignal( bool focusGained )
885 {
886   Dali::Toolkit::Control handle( GetOwner() );
887
888   if ( focusGained )
889   {
890     // signals are allocated dynamically when someone connects
891     if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
892     {
893       mImpl->mKeyInputFocusGainedSignal.Emit( handle );
894     }
895   }
896   else
897   {
898     // signals are allocated dynamically when someone connects
899     if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
900     {
901       mImpl->mKeyInputFocusLostSignal.Emit( handle );
902     }
903   }
904 }
905
906 void Control::OnStageConnection( int depth )
907 {
908   if( mImpl->mBackgroundVisual)
909   {
910     Actor self( Self() );
911     mImpl->mBackgroundVisual.SetOnStage( self );
912   }
913 }
914
915 void Control::OnStageDisconnection()
916 {
917   if( mImpl->mBackgroundVisual )
918   {
919     Actor self( Self() );
920     mImpl->mBackgroundVisual.SetOffStage( self );
921   }
922 }
923
924 void Control::OnKeyInputFocusGained()
925 {
926   EmitKeyInputFocusSignal( true );
927 }
928
929 void Control::OnKeyInputFocusLost()
930 {
931   EmitKeyInputFocusSignal( false );
932 }
933
934 void Control::OnChildAdd(Actor& child)
935 {
936   // Notify derived classes.
937   OnControlChildAdd( child );
938 }
939
940 void Control::OnChildRemove(Actor& child)
941 {
942   // Notify derived classes.
943   OnControlChildRemove( child );
944 }
945
946 void Control::OnSizeSet(const Vector3& targetSize)
947 {
948   if( mImpl->mBackgroundVisual )
949   {
950     Vector2 size( targetSize );
951     mImpl->mBackgroundVisual.SetSize( size );
952   }
953 }
954
955 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
956 {
957   // @todo size negotiate background to new size, animate as well?
958 }
959
960 bool Control::OnTouchEvent(const TouchEvent& event)
961 {
962   return false; // Do not consume
963 }
964
965 bool Control::OnHoverEvent(const HoverEvent& event)
966 {
967   return false; // Do not consume
968 }
969
970 bool Control::OnKeyEvent(const KeyEvent& event)
971 {
972   return false; // Do not consume
973 }
974
975 bool Control::OnWheelEvent(const WheelEvent& event)
976 {
977   return false; // Do not consume
978 }
979
980 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
981 {
982   for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
983   {
984     container.Add( Self().GetChildAt( i ), size );
985   }
986 }
987
988 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
989 {
990 }
991
992 Vector3 Control::GetNaturalSize()
993 {
994   if( mImpl->mBackgroundVisual )
995   {
996     Vector2 naturalSize;
997     mImpl->mBackgroundVisual.GetNaturalSize(naturalSize);
998     return Vector3(naturalSize);
999   }
1000   return Vector3::ZERO;
1001 }
1002
1003 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1004 {
1005   return CalculateChildSizeBase( child, dimension );
1006 }
1007
1008 float Control::GetHeightForWidth( float width )
1009 {
1010   return GetHeightForWidthBase( width );
1011 }
1012
1013 float Control::GetWidthForHeight( float height )
1014 {
1015   return GetWidthForHeightBase( height );
1016 }
1017
1018 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1019 {
1020   return RelayoutDependentOnChildrenBase( dimension );
1021 }
1022
1023 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1024 {
1025 }
1026
1027 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1028 {
1029 }
1030
1031 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1032 {
1033   mImpl->SignalConnected( slotObserver, callback );
1034 }
1035
1036 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1037 {
1038   mImpl->SignalDisconnected( slotObserver, callback );
1039 }
1040
1041 Control& GetImplementation( Dali::Toolkit::Control& handle )
1042 {
1043   CustomActorImpl& customInterface = handle.GetImplementation();
1044   // downcast to control
1045   Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1046   return impl;
1047 }
1048
1049 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1050 {
1051   const CustomActorImpl& customInterface = handle.GetImplementation();
1052   // downcast to control
1053   const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1054   return impl;
1055 }
1056
1057 } // namespace Internal
1058
1059 } // namespace Toolkit
1060
1061 } // namespace Dali