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