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