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