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