Update keyboard focus direction enum for Control
[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/actors/image-actor.h>
26 #include <dali/devel-api/actors/mesh-actor.h>
27 #include <dali/public-api/animation/constraint.h>
28 #include <dali/public-api/animation/constraints.h>
29 #include <dali/devel-api/geometry/mesh.h>
30 #include <dali/public-api/object/type-registry.h>
31 #include <dali/devel-api/object/type-registry-helper.h>
32 #include <dali/devel-api/scripting/scripting.h>
33 #include <dali/public-api/size-negotiation/relayout-container.h>
34 #include <dali/integration-api/debug.h>
35
36 // INTERNAL INCLUDES
37 #include <dali-toolkit/public-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
43 namespace Dali
44 {
45
46 namespace Toolkit
47 {
48
49 namespace
50 {
51
52 const float BACKGROUND_ACTOR_Z_POSITION( -0.1f );
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_CONTROL_ACTIVATED = "control-activated";
70 static bool DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
71 {
72   bool ret = false;
73
74   if( object && ( 0 == strcmp( actionName.c_str(), ACTION_CONTROL_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_CONTROL_ACTIVATED, &DoAction );
164
165 DALI_TYPE_REGISTRATION_END()
166
167 /**
168  * Structure which holds information about the background of a control
169  */
170 struct Background
171 {
172   Actor actor;   ///< Either a MeshActor or an ImageActor
173   Vector4 color; ///< The color of the actor.
174
175   /**
176    * Constructor
177    */
178   Background()
179   : actor(),
180     color( Color::WHITE )
181   {
182   }
183 };
184
185 /**
186  * Creates a white coloured Mesh.
187  */
188 Mesh CreateMesh()
189 {
190   Vector3 white( Color::WHITE );
191
192   MeshData meshData;
193
194   // Create vertices with a white color (actual color is set by actor color)
195   MeshData::VertexContainer vertices(4);
196   vertices[ 0 ] = MeshData::Vertex( Vector3( -0.5f, -0.5f, 0.0f ), Vector2::ZERO, white );
197   vertices[ 1 ] = MeshData::Vertex( Vector3(  0.5f, -0.5f, 0.0f ), Vector2::ZERO, white );
198   vertices[ 2 ] = MeshData::Vertex( Vector3( -0.5f,  0.5f, 0.0f ), Vector2::ZERO, white );
199   vertices[ 3 ] = MeshData::Vertex( Vector3(  0.5f,  0.5f, 0.0f ), Vector2::ZERO, white );
200
201   // Specify all the faces
202   MeshData::FaceIndices faces;
203   faces.reserve( 6 ); // 2 triangles in Quad
204   faces.push_back( 0 ); faces.push_back( 3 ); faces.push_back( 1 );
205   faces.push_back( 0 ); faces.push_back( 2 ); faces.push_back( 3 );
206
207   // Create the mesh data from the vertices and faces
208   meshData.SetMaterial( Material::New( "ControlMaterial" ) );
209   meshData.SetVertices( vertices );
210   meshData.SetFaceIndices( faces );
211   meshData.SetHasColor( true );
212
213   return Mesh::New( meshData );
214 }
215
216 /**
217  * Sets all the required properties for the background actor.
218  *
219  * @param[in]  actor              The actor to set the properties on.
220  * @param[in]  color              The required color of the actor.
221  */
222 void SetupBackgroundActor( Actor actor, const Vector4& color )
223 {
224   actor.SetColor( color );
225   actor.SetPositionInheritanceMode( USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
226   actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
227   actor.SetZ( BACKGROUND_ACTOR_Z_POSITION );
228   actor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
229 }
230
231 /**
232  * Sets all the required properties for the background actor.
233  *
234  * @param[in]  actor              The actor to set the properties on.
235  * @param[in]  constrainingIndex  The property index to constrain the parent's size on.
236  * @param[in]  color              The required color of the actor.
237  */
238 void SetupBackgroundActorConstrained( Actor actor, Property::Index constrainingIndex, const Vector4& color )
239 {
240   actor.SetColor( color );
241   actor.SetPositionInheritanceMode( USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
242   actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
243   actor.SetZ( BACKGROUND_ACTOR_Z_POSITION );
244
245   Constraint constraint = Constraint::New<Vector3>( actor,
246                                                     constrainingIndex,
247                                                     EqualToConstraint() );
248   constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
249   constraint.Apply();
250 }
251
252 } // unnamed namespace
253
254 namespace Internal
255 {
256
257 class Control::Impl : public ConnectionTracker
258 {
259 public:
260
261   // Construction & Destruction
262   Impl(Control& controlImpl)
263   : mControlImpl( controlImpl ),
264     mStyleName(""),
265     mBackground( NULL ),
266     mStartingPinchScale( NULL ),
267     mKeyEventSignal(),
268     mPinchGestureDetector(),
269     mPanGestureDetector(),
270     mTapGestureDetector(),
271     mLongPressGestureDetector(),
272     mFlags( Control::ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
273     mIsKeyboardNavigationSupported( false ),
274     mIsKeyboardFocusGroup( false )
275   {
276   }
277
278   ~Impl()
279   {
280     // All gesture detectors will be destroyed so no need to disconnect.
281     delete mBackground;
282     delete mStartingPinchScale;
283   }
284
285   // Gesture Detection Methods
286
287   void PinchDetected(Actor actor, const PinchGesture& pinch)
288   {
289     mControlImpl.OnPinch(pinch);
290   }
291
292   void PanDetected(Actor actor, const PanGesture& pan)
293   {
294     mControlImpl.OnPan(pan);
295   }
296
297   void TapDetected(Actor actor, const TapGesture& tap)
298   {
299     mControlImpl.OnTap(tap);
300   }
301
302   void LongPressDetected(Actor actor, const LongPressGesture& longPress)
303   {
304     mControlImpl.OnLongPress(longPress);
305   }
306
307   // Background Methods
308
309   /**
310    * Only creates an instance of the background if we actually use it.
311    * @return A reference to the Background structure.
312    */
313   Background& GetBackground()
314   {
315     if ( !mBackground )
316     {
317       mBackground = new Background;
318     }
319     return *mBackground;
320   }
321
322   // Properties
323
324   /**
325    * Called when a property of an object of this type is set.
326    * @param[in] object The object whose property is set.
327    * @param[in] index The property index.
328    * @param[in] value The new property value.
329    */
330   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
331   {
332     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
333
334     if ( control )
335     {
336       Control& controlImpl( GetImplementation( control ) );
337
338       switch ( index )
339       {
340         case Toolkit::Control::Property::STYLE_NAME:
341         {
342           controlImpl.SetStyleName( value.Get< std::string >() );
343           break;
344         }
345
346         case Toolkit::Control::Property::BACKGROUND_COLOR:
347         {
348           controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
349           break;
350         }
351
352         case Toolkit::Control::Property::BACKGROUND_IMAGE:
353         {
354           if ( value.HasKey( "image" ) )
355           {
356             Property::Map imageMap = value.GetValue( "image" ).Get< Property::Map >();
357             Image image = Scripting::NewImage( imageMap );
358
359             if ( image )
360             {
361               controlImpl.SetBackgroundImage( image );
362             }
363           }
364           else if ( value.Get< Property::Map >().Empty() )
365           {
366             // An empty map means the background is no longer required
367             controlImpl.ClearBackground();
368           }
369           break;
370         }
371
372         case Toolkit::Control::Property::KEY_INPUT_FOCUS:
373         {
374           if ( value.Get< bool >() )
375           {
376             controlImpl.SetKeyInputFocus();
377           }
378           else
379           {
380             controlImpl.ClearKeyInputFocus();
381           }
382           break;
383         }
384       }
385     }
386   }
387
388   /**
389    * Called to retrieve a property of an object of this type.
390    * @param[in] object The object whose property is to be retrieved.
391    * @param[in] index The property index.
392    * @return The current value of the property.
393    */
394   static Property::Value GetProperty( BaseObject* object, Property::Index index )
395   {
396     Property::Value value;
397
398     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
399
400     if ( control )
401     {
402       Control& controlImpl( GetImplementation( control ) );
403
404       switch ( index )
405       {
406         case Toolkit::Control::Property::STYLE_NAME:
407         {
408           value = controlImpl.GetStyleName();
409           break;
410         }
411
412         case Toolkit::Control::Property::BACKGROUND_COLOR:
413         {
414           value = controlImpl.GetBackgroundColor();
415           break;
416         }
417
418         case Toolkit::Control::Property::BACKGROUND_IMAGE:
419         {
420           Property::Map map;
421
422           Background* back = controlImpl.mImpl->mBackground;
423           if( back )
424           {
425             ImageActor imageActor = ImageActor::DownCast( back->actor );
426             if ( imageActor )
427             {
428               Image image = imageActor.GetImage();
429               Property::Map imageMap;
430               Scripting::CreatePropertyMap( image, imageMap );
431               map[ "image" ] = imageMap;
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
470   // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
471   static PropertyRegistration PROPERTY_1;
472   static PropertyRegistration PROPERTY_2;
473   static PropertyRegistration PROPERTY_3;
474   static PropertyRegistration PROPERTY_4;
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-color", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
480 PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "background-image", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
481 PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "key-input-focus",  Toolkit::Control::Property::KEY_INPUT_FOCUS,  Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
482
483 Toolkit::Control Control::New()
484 {
485   // Create the implementation, temporarily owned on stack
486   IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) );
487
488   // Pass ownership to handle
489   Toolkit::Control handle( *controlImpl );
490
491   // Second-phase init of the implementation
492   // This can only be done after the CustomActor connection has been made...
493   controlImpl->Initialize();
494
495   return handle;
496 }
497
498 Control::~Control()
499 {
500   delete mImpl;
501 }
502
503 void Control::SetStyleName( const std::string& styleName )
504 {
505   if( styleName != mImpl->mStyleName )
506   {
507     mImpl->mStyleName = styleName;
508
509     // Apply new style
510     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
511     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
512   }
513 }
514
515 const std::string& Control::GetStyleName() const
516 {
517   return mImpl->mStyleName;
518 }
519
520 void Control::SetBackgroundColor( const Vector4& color )
521 {
522   Background& background( mImpl->GetBackground() );
523
524   if ( background.actor )
525   {
526     // Just set the actor color
527     background.actor.SetColor( color );
528   }
529   else
530   {
531     // Create Mesh Actor
532     MeshActor meshActor = MeshActor::New( CreateMesh() );
533
534     SetupBackgroundActorConstrained( meshActor, Actor::Property::SCALE, color );
535
536     // Set the background actor before adding so that we do not inform deriving classes
537     background.actor = meshActor;
538     Self().Add( meshActor );
539   }
540
541   background.color = color;
542 }
543
544 Vector4 Control::GetBackgroundColor() const
545 {
546   if ( mImpl->mBackground )
547   {
548     return mImpl->mBackground->color;
549   }
550   return Color::TRANSPARENT;
551 }
552
553 void Control::SetBackgroundImage( Image image )
554 {
555   Background& background( mImpl->GetBackground() );
556
557   if ( background.actor )
558   {
559     // Remove Current actor, unset AFTER removal so that we do not inform deriving classes
560     Self().Remove( background.actor );
561     background.actor.Reset();
562   }
563
564   ImageActor imageActor = ImageActor::New( image );
565   SetupBackgroundActor( imageActor, background.color );
566
567   // Set the background actor before adding so that we do not inform derived classes
568   background.actor = imageActor;
569   Self().Add( imageActor );
570 }
571
572 void Control::ClearBackground()
573 {
574   if ( mImpl->mBackground )
575   {
576     Background& background( mImpl->GetBackground() );
577     Self().Remove( background.actor );
578
579     delete mImpl->mBackground;
580     mImpl->mBackground = NULL;
581   }
582 }
583
584 void Control::EnableGestureDetection(Gesture::Type type)
585 {
586   if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
587   {
588     mImpl->mPinchGestureDetector = PinchGestureDetector::New();
589     mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
590     mImpl->mPinchGestureDetector.Attach(Self());
591   }
592
593   if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
594   {
595     mImpl->mPanGestureDetector = PanGestureDetector::New();
596     mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
597     mImpl->mPanGestureDetector.Attach(Self());
598   }
599
600   if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
601   {
602     mImpl->mTapGestureDetector = TapGestureDetector::New();
603     mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
604     mImpl->mTapGestureDetector.Attach(Self());
605   }
606
607   if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
608   {
609     mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
610     mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
611     mImpl->mLongPressGestureDetector.Attach(Self());
612   }
613 }
614
615 void Control::DisableGestureDetection(Gesture::Type type)
616 {
617   if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
618   {
619     mImpl->mPinchGestureDetector.Detach(Self());
620     mImpl->mPinchGestureDetector.Reset();
621   }
622
623   if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
624   {
625     mImpl->mPanGestureDetector.Detach(Self());
626     mImpl->mPanGestureDetector.Reset();
627   }
628
629   if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
630   {
631     mImpl->mTapGestureDetector.Detach(Self());
632     mImpl->mTapGestureDetector.Reset();
633   }
634
635   if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
636   {
637     mImpl->mLongPressGestureDetector.Detach(Self());
638     mImpl->mLongPressGestureDetector.Reset();
639   }
640 }
641
642 PinchGestureDetector Control::GetPinchGestureDetector() const
643 {
644   return mImpl->mPinchGestureDetector;
645 }
646
647 PanGestureDetector Control::GetPanGestureDetector() const
648 {
649   return mImpl->mPanGestureDetector;
650 }
651
652 TapGestureDetector Control::GetTapGestureDetector() const
653 {
654   return mImpl->mTapGestureDetector;
655 }
656
657 LongPressGestureDetector Control::GetLongPressGestureDetector() const
658 {
659   return mImpl->mLongPressGestureDetector;
660 }
661
662 void Control::SetKeyboardNavigationSupport(bool isSupported)
663 {
664   mImpl->mIsKeyboardNavigationSupported = isSupported;
665 }
666
667 bool Control::IsKeyboardNavigationSupported()
668 {
669   return mImpl->mIsKeyboardNavigationSupported;
670 }
671
672 void Control::SetKeyInputFocus()
673 {
674   if( Self().OnStage() )
675   {
676     Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
677   }
678 }
679
680 bool Control::HasKeyInputFocus()
681 {
682   bool result = false;
683   if( Self().OnStage() )
684   {
685     result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
686   }
687   return result;
688 }
689
690 void Control::ClearKeyInputFocus()
691 {
692   if( Self().OnStage() )
693   {
694     Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
695   }
696 }
697
698 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
699 {
700   mImpl->mIsKeyboardFocusGroup = isFocusGroup;
701
702   // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
703   Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
704 }
705
706 bool Control::IsKeyboardFocusGroup()
707 {
708   return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
709 }
710
711 void Control::AccessibilityActivate()
712 {
713   // Inform deriving classes
714   OnAccessibilityActivated();
715 }
716
717 bool Control::OnAccessibilityActivated()
718 {
719   return false; // Accessibility activation is not handled by default
720 }
721
722 bool Control::OnAccessibilityPan(PanGesture gesture)
723 {
724   return false; // Accessibility pan gesture is not handled by default
725 }
726
727 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
728 {
729   return false; // Accessibility touch event is not handled by default
730 }
731
732 bool Control::OnAccessibilityValueChange(bool isIncrease)
733 {
734   return false; // Accessibility value change action is not handled by default
735 }
736
737 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
738 {
739   return Actor();
740 }
741
742 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
743 {
744 }
745
746 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
747 {
748   return mImpl->mKeyEventSignal;
749 }
750
751 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusGainedSignal()
752 {
753   return mImpl->mKeyInputFocusGainedSignal;
754 }
755
756 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusLostSignal()
757 {
758   return mImpl->mKeyInputFocusLostSignal;
759 }
760
761 bool Control::EmitKeyEventSignal( const KeyEvent& event )
762 {
763   // Guard against destruction during signal emission
764   Dali::Toolkit::Control handle( GetOwner() );
765
766   bool consumed = false;
767
768   // signals are allocated dynamically when someone connects
769   if ( !mImpl->mKeyEventSignal.Empty() )
770   {
771     consumed = mImpl->mKeyEventSignal.Emit( handle, event );
772   }
773
774   if (!consumed)
775   {
776     // Notification for derived classes
777     consumed = OnKeyEvent(event);
778   }
779
780   return consumed;
781 }
782
783 Control::Control( ControlBehaviour behaviourFlags )
784 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
785   mImpl(new Impl(*this))
786 {
787   mImpl->mFlags = behaviourFlags;
788 }
789
790 void Control::Initialize()
791 {
792   if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
793   {
794     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
795
796     // Register for style changes
797     styleManager.StyleChangeSignal().Connect( this, &Control::OnStyleChange );
798
799     // SetTheme
800     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
801   }
802
803   if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
804   {
805     SetKeyboardNavigationSupport( true );
806   }
807
808   // Calling deriving classes
809   OnInitialize();
810 }
811
812 void Control::OnInitialize()
813 {
814 }
815
816 void Control::OnControlChildAdd( Actor& child )
817 {
818 }
819
820 void Control::OnControlChildRemove( Actor& child )
821 {
822 }
823
824 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
825 {
826   // By default the control is only interested in theme (not font) changes
827   if( change == StyleChange::THEME_CHANGE )
828   {
829     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
830   }
831 }
832
833 void Control::OnPinch(const PinchGesture& pinch)
834 {
835   if( !( mImpl->mStartingPinchScale ) )
836   {
837     // lazy allocate
838     mImpl->mStartingPinchScale = new Vector3;
839   }
840
841   if( pinch.state == Gesture::Started )
842   {
843     *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
844   }
845
846   Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
847 }
848
849 void Control::OnPan( const PanGesture& pan )
850 {
851 }
852
853 void Control::OnTap(const TapGesture& tap)
854 {
855 }
856
857 void Control::OnLongPress( const LongPressGesture& longPress )
858 {
859 }
860
861 void Control::EmitKeyInputFocusSignal( bool focusGained )
862 {
863   Dali::Toolkit::Control handle( GetOwner() );
864
865   if ( focusGained )
866   {
867      // signals are allocated dynamically when someone connects
868      if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
869      {
870       mImpl->mKeyInputFocusGainedSignal.Emit( handle );
871      }
872   }
873   else
874   {
875     // signals are allocated dynamically when someone connects
876     if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
877     {
878       mImpl->mKeyInputFocusLostSignal.Emit( handle );
879     }
880   }
881 }
882
883 void Control::OnStageConnection()
884 {
885 }
886
887 void Control::OnStageDisconnection()
888 {
889 }
890
891 void Control::OnKeyInputFocusGained()
892 {
893   EmitKeyInputFocusSignal( true );
894 }
895
896 void Control::OnKeyInputFocusLost()
897 {
898   EmitKeyInputFocusSignal( false );
899 }
900
901 void Control::OnChildAdd(Actor& child)
902 {
903   // If this is the background actor, then we do not want to relayout or inform deriving classes
904   if ( mImpl->mBackground && ( child == mImpl->mBackground->actor ) )
905   {
906     return;
907   }
908
909   // Notify derived classes.
910   OnControlChildAdd( child );
911 }
912
913 void Control::OnChildRemove(Actor& child)
914 {
915   // If this is the background actor, then we do not want to relayout or inform deriving classes
916   if ( mImpl->mBackground && ( child == mImpl->mBackground->actor ) )
917   {
918     return;
919   }
920
921   // Notify derived classes.
922   OnControlChildRemove( child );
923 }
924
925 void Control::OnSizeSet(const Vector3& targetSize)
926 {
927   // Background is resized through size negotiation
928 }
929
930 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
931 {
932   // @todo size negotiate background to new size, animate as well?
933 }
934
935 bool Control::OnTouchEvent(const TouchEvent& event)
936 {
937   return false; // Do not consume
938 }
939
940 bool Control::OnHoverEvent(const HoverEvent& event)
941 {
942   return false; // Do not consume
943 }
944
945 bool Control::OnKeyEvent(const KeyEvent& event)
946 {
947   return false; // Do not consume
948 }
949
950 bool Control::OnWheelEvent(const WheelEvent& event)
951 {
952   return false; // Do not consume
953 }
954
955 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
956 {
957   for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
958   {
959     container.Add( Self().GetChildAt( i ), size );
960   }
961 }
962
963 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
964 {
965 }
966
967 Vector3 Control::GetNaturalSize()
968 {
969   if( mImpl->mBackground )
970   {
971     Actor actor = mImpl->mBackground->actor;
972     if( actor )
973     {
974       return actor.GetNaturalSize();
975     }
976   }
977   return Vector3();
978 }
979
980 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
981 {
982   return CalculateChildSizeBase( child, dimension );
983 }
984
985 float Control::GetHeightForWidth( float width )
986 {
987   if( mImpl->mBackground )
988   {
989     Actor actor = mImpl->mBackground->actor;
990     if( actor )
991     {
992       return actor.GetHeightForWidth( width );
993     }
994   }
995   return GetHeightForWidthBase( width );
996 }
997
998 float Control::GetWidthForHeight( float height )
999 {
1000   if( mImpl->mBackground )
1001   {
1002     Actor actor = mImpl->mBackground->actor;
1003     if( actor )
1004     {
1005       return actor.GetWidthForHeight( height );
1006     }
1007   }
1008   return GetWidthForHeightBase( height );
1009 }
1010
1011 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1012 {
1013   return RelayoutDependentOnChildrenBase( dimension );
1014 }
1015
1016 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1017 {
1018 }
1019
1020 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1021 {
1022 }
1023
1024 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1025 {
1026   mImpl->SignalConnected( slotObserver, callback );
1027 }
1028
1029 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1030 {
1031   mImpl->SignalDisconnected( slotObserver, callback );
1032 }
1033
1034 Control& GetImplementation( Dali::Toolkit::Control& handle )
1035 {
1036   CustomActorImpl& customInterface = handle.GetImplementation();
1037   // downcast to control
1038   Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1039   return impl;
1040 }
1041
1042 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1043 {
1044   const CustomActorImpl& customInterface = handle.GetImplementation();
1045   // downcast to control
1046   const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1047   return impl;
1048 }
1049
1050 } // namespace Internal
1051
1052 } // namespace Toolkit
1053
1054 } // namespace Dali