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