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