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