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