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