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