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