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