2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/public-api/controls/control-impl.h>
22 #include <cstring> // for strcmp
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/common/owner-container.h>
32 #include <dali/devel-api/scripting/scripting.h>
33 #include <dali/integration-api/debug.h>
36 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
37 #include <dali-toolkit/public-api/controls/control.h>
38 #include <dali-toolkit/public-api/styling/style-manager.h>
39 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
40 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
41 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
42 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
43 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
44 #include <dali-toolkit/internal/styling/style-manager-impl.h>
45 #include <dali-toolkit/internal/visuals/color/color-visual.h>
46 #include <dali-toolkit/internal/visuals/transition-data-impl.h>
47 #include <dali-toolkit/devel-api/align-enums.h>
58 #if defined(DEBUG_ENABLED)
59 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::General, false, "LOG_CONTROL_VISUALS");
63 * Struct used to store Visual within the control, index is a unique key for each visual.
65 struct RegisteredVisual
67 Property::Index index;
68 Toolkit::Visual::Base visual;
71 RegisteredVisual( Property::Index aIndex, Toolkit::Visual::Base &aVisual, bool aEnabled) :
72 index(aIndex), visual(aVisual), enabled(aEnabled) {}
77 Handle handle; ///< a handle to the target object
78 Property::Index index; ///< The index of a property provided by the referenced object
82 index( Property::INVALID_INDEX )
86 HandleIndex( Handle& handle, Property::Index index )
94 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
97 * Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
99 bool FindVisual( Property::Index targetIndex, RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
101 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
103 if ( (*iter)->index == targetIndex )
111 HandleIndex GetVisualProperty(
112 Internal::Control& controlImpl,
113 RegisteredVisualContainer& visuals,
114 const std::string& visualName,
115 Property::Key propertyKey )
117 #if defined(DEBUG_ENABLED)
118 std::ostringstream oss;
119 oss << "Control::GetHandleIndex(" << visualName << ", " << propertyKey << ")" << std::endl;
120 DALI_LOG_INFO( gLogFilter, Debug::General, oss.str().c_str() );
123 // Find visualName in the control
124 RegisteredVisualContainer::Iterator iter;
125 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
127 if ( (*iter)->visual.GetName() == visualName )
133 // Does it's renderer have an associated property?
134 if( iter != visuals.End() )
136 Actor self = controlImpl.Self();
137 Property::Index index = self.GetPropertyIndex( propertyKey );
138 if( index != Property::INVALID_INDEX )
140 // It's an actor property:
141 return HandleIndex( self, index );
145 // Check if it is a renderer property:
146 if( self.GetRendererCount() > 0 )
148 // @todo Need to use correct renderer index
149 Renderer renderer = self.GetRendererAt(0);
150 Property::Index index = renderer.GetPropertyIndex( propertyKey );
151 if( index != Property::INVALID_INDEX )
153 // It's a renderer property:
154 return HandleIndex( renderer, index );
159 std::ostringstream oss;
161 DALI_LOG_WARNING( "Control::GetHandleIndex(%s, %s) No renderers\n", visualName.c_str(), oss.str().c_str() );
166 return HandleIndex( handle, Property::INVALID_INDEX );
169 void SetDefaultTransform( Property::Map& propertyMap )
173 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET, Vector2(0.0f, 0.0f) )
174 .Add( Toolkit::DevelVisual::Transform::Property::SIZE, Vector2(1.0f, 1.0f) )
175 .Add( Toolkit::DevelVisual::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
176 .Add( Toolkit::DevelVisual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER )
177 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET_SIZE_MODE, Vector4::ZERO );
181 * Creates control through type registry
185 return Internal::Control::New();
189 * Performs actions as requested using the action name.
190 * @param[in] object The object on which to perform the action.
191 * @param[in] actionName The action to perform.
192 * @param[in] attributes The attributes with which to perfrom this action.
193 * @return true if action has been accepted by this control
195 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
196 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
200 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
202 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
205 // if cast succeeds there is an implementation so no need to check
206 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
214 * Connects a callback function with the object's signals.
215 * @param[in] object The object providing the signal.
216 * @param[in] tracker Used to disconnect the signal.
217 * @param[in] signalName The signal to connect to.
218 * @param[in] functor A newly allocated FunctorDelegate.
219 * @return True if the signal was connected.
220 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
222 const char* SIGNAL_KEY_EVENT = "keyEvent";
223 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
224 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
225 const char* SIGNAL_TAPPED = "tapped";
226 const char* SIGNAL_PANNED = "panned";
227 const char* SIGNAL_PINCHED = "pinched";
228 const char* SIGNAL_LONG_PRESSED = "longPressed";
229 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
231 Dali::BaseHandle handle( object );
233 bool connected( false );
234 Toolkit::Control control = Toolkit::Control::DownCast( handle );
237 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
240 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
242 controlImpl.KeyEventSignal().Connect( tracker, functor );
244 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
246 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
248 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
250 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
252 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
254 controlImpl.EnableGestureDetection( Gesture::Tap );
255 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
257 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
259 controlImpl.EnableGestureDetection( Gesture::Pan );
260 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
262 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
264 controlImpl.EnableGestureDetection( Gesture::Pinch );
265 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
267 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
269 controlImpl.EnableGestureDetection( Gesture::LongPress );
270 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
276 // Setup signals and actions using the type-registry.
277 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
279 // Note: Properties are registered separately below.
281 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
282 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
283 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
284 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
285 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
286 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
287 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
289 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
291 DALI_TYPE_REGISTRATION_END()
293 } // unnamed namespace
298 class Control::Impl : public ConnectionTracker
302 // Construction & Destruction
303 Impl(Control& controlImpl)
304 : mControlImpl( controlImpl ),
306 mBackgroundColor(Color::TRANSPARENT),
307 mStartingPinchScale( NULL ),
309 mPinchGestureDetector(),
310 mPanGestureDetector(),
311 mTapGestureDetector(),
312 mLongPressGestureDetector(),
313 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
314 mIsKeyboardNavigationSupported( false ),
315 mIsKeyboardFocusGroup( false )
321 // All gesture detectors will be destroyed so no need to disconnect.
322 delete mStartingPinchScale;
325 // Gesture Detection Methods
327 void PinchDetected(Actor actor, const PinchGesture& pinch)
329 mControlImpl.OnPinch(pinch);
332 void PanDetected(Actor actor, const PanGesture& pan)
334 mControlImpl.OnPan(pan);
337 void TapDetected(Actor actor, const TapGesture& tap)
339 mControlImpl.OnTap(tap);
342 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
344 mControlImpl.OnLongPress(longPress);
350 * Called when a property of an object of this type is set.
351 * @param[in] object The object whose property is set.
352 * @param[in] index The property index.
353 * @param[in] value The new property value.
355 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
357 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
361 Control& controlImpl( GetImplementation( control ) );
365 case Toolkit::Control::Property::STYLE_NAME:
367 controlImpl.SetStyleName( value.Get< std::string >() );
371 case Toolkit::Control::Property::BACKGROUND_COLOR:
373 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
374 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
378 case Toolkit::Control::Property::BACKGROUND_IMAGE:
380 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
381 Image image = Scripting::NewImage( value );
384 controlImpl.SetBackgroundImage( image );
388 // An empty image means the background is no longer required
389 controlImpl.ClearBackground();
394 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
396 if ( value.Get< bool >() )
398 controlImpl.SetKeyInputFocus();
402 controlImpl.ClearKeyInputFocus();
407 case Toolkit::Control::Property::BACKGROUND:
409 const Property::Map* map = value.GetMap();
410 if( map && !map->Empty() )
412 controlImpl.SetBackground( *map );
416 // The background is an empty property map, so we should clear the background
417 controlImpl.ClearBackground();
426 * Called to retrieve a property of an object of this type.
427 * @param[in] object The object whose property is to be retrieved.
428 * @param[in] index The property index.
429 * @return The current value of the property.
431 static Property::Value GetProperty( BaseObject* object, Property::Index index )
433 Property::Value value;
435 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
439 Control& controlImpl( GetImplementation( control ) );
443 case Toolkit::Control::Property::STYLE_NAME:
445 value = controlImpl.GetStyleName();
449 case Toolkit::Control::Property::BACKGROUND_COLOR:
451 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
452 value = controlImpl.GetBackgroundColor();
456 case Toolkit::Control::Property::BACKGROUND_IMAGE:
458 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
460 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
463 visual.CreatePropertyMap( map );
469 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
471 value = controlImpl.HasKeyInputFocus();
475 case Toolkit::Control::Property::BACKGROUND:
478 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
481 visual.CreatePropertyMap( map );
496 Control& mControlImpl;
497 RegisteredVisualContainer mVisuals; // Stores visuals needed by the control, non trivial type so std::vector used.
498 std::string mStyleName;
499 Vector4 mBackgroundColor; ///< The color of the background visual
500 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
501 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
502 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
503 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
506 PinchGestureDetector mPinchGestureDetector;
507 PanGestureDetector mPanGestureDetector;
508 TapGestureDetector mTapGestureDetector;
509 LongPressGestureDetector mLongPressGestureDetector;
511 ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
512 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
513 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
515 // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
516 static const PropertyRegistration PROPERTY_1;
517 static const PropertyRegistration PROPERTY_2;
518 static const PropertyRegistration PROPERTY_3;
519 static const PropertyRegistration PROPERTY_4;
520 static const PropertyRegistration PROPERTY_5;
523 // Properties registered without macro to use specific member variables.
524 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
525 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
526 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
527 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
528 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
530 Toolkit::Control Control::New()
532 // Create the implementation, temporarily owned on stack
533 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
535 // Pass ownership to handle
536 Toolkit::Control handle( *controlImpl );
538 // Second-phase init of the implementation
539 // This can only be done after the CustomActor connection has been made...
540 controlImpl->Initialize();
545 void Control::SetStyleName( const std::string& styleName )
547 if( styleName != mImpl->mStyleName )
549 mImpl->mStyleName = styleName;
551 // Apply new style, if stylemanager is available
552 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
555 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
560 const std::string& Control::GetStyleName() const
562 return mImpl->mStyleName;
565 void Control::SetBackgroundColor( const Vector4& color )
567 mImpl->mBackgroundColor = color;
569 map[ Toolkit::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
570 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
572 SetBackground( map );
575 Vector4 Control::GetBackgroundColor() const
577 return mImpl->mBackgroundColor;
580 void Control::SetBackground( const Property::Map& map )
582 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
585 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
586 visual.SetDepthIndex( DepthIndex::BACKGROUND );
590 void Control::SetBackgroundImage( Image image )
592 DALI_LOG_WARNING( "SetBackgroundImage is for the depreciated Property::BACKGROUND_IMAGE use SetBackground( const Property::Map& map )\n" );
593 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
596 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
597 visual.SetDepthIndex( DepthIndex::BACKGROUND );
601 void Control::ClearBackground()
603 UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
604 mImpl->mBackgroundColor = Color::TRANSPARENT;
607 void Control::EnableGestureDetection(Gesture::Type type)
609 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
611 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
612 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
613 mImpl->mPinchGestureDetector.Attach(Self());
616 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
618 mImpl->mPanGestureDetector = PanGestureDetector::New();
619 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
620 mImpl->mPanGestureDetector.Attach(Self());
623 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
625 mImpl->mTapGestureDetector = TapGestureDetector::New();
626 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
627 mImpl->mTapGestureDetector.Attach(Self());
630 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
632 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
633 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
634 mImpl->mLongPressGestureDetector.Attach(Self());
638 void Control::DisableGestureDetection(Gesture::Type type)
640 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
642 mImpl->mPinchGestureDetector.Detach(Self());
643 mImpl->mPinchGestureDetector.Reset();
646 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
648 mImpl->mPanGestureDetector.Detach(Self());
649 mImpl->mPanGestureDetector.Reset();
652 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
654 mImpl->mTapGestureDetector.Detach(Self());
655 mImpl->mTapGestureDetector.Reset();
658 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
660 mImpl->mLongPressGestureDetector.Detach(Self());
661 mImpl->mLongPressGestureDetector.Reset();
665 PinchGestureDetector Control::GetPinchGestureDetector() const
667 return mImpl->mPinchGestureDetector;
670 PanGestureDetector Control::GetPanGestureDetector() const
672 return mImpl->mPanGestureDetector;
675 TapGestureDetector Control::GetTapGestureDetector() const
677 return mImpl->mTapGestureDetector;
680 LongPressGestureDetector Control::GetLongPressGestureDetector() const
682 return mImpl->mLongPressGestureDetector;
685 void Control::SetKeyboardNavigationSupport(bool isSupported)
687 mImpl->mIsKeyboardNavigationSupported = isSupported;
690 bool Control::IsKeyboardNavigationSupported()
692 return mImpl->mIsKeyboardNavigationSupported;
695 void Control::SetKeyInputFocus()
697 if( Self().OnStage() )
699 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
703 bool Control::HasKeyInputFocus()
706 if( Self().OnStage() )
708 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
713 void Control::ClearKeyInputFocus()
715 if( Self().OnStage() )
717 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
721 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
723 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
725 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
726 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
729 bool Control::IsKeyboardFocusGroup()
731 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
734 void Control::AccessibilityActivate()
736 // Inform deriving classes
737 OnAccessibilityActivated();
740 void Control::KeyboardEnter()
742 // Inform deriving classes
746 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
748 RegisterVisual( index, visual, true );
751 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
753 bool visualReplaced ( false );
756 if( !mImpl->mVisuals.Empty() )
758 RegisteredVisualContainer::Iterator iter;
759 // Check if visual (index) is already registered. Replace if so.
760 if ( FindVisual( index, mImpl->mVisuals, iter ) )
762 if( (*iter)->visual && self.OnStage() )
764 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
766 (*iter)->visual = visual;
767 visualReplaced = true;
771 if( !visualReplaced ) // New registration entry
773 mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
776 if( visual && self.OnStage() && enabled )
778 Toolkit::GetImplementation(visual).SetOnStage( self );
782 void Control::UnregisterVisual( Property::Index index )
784 RegisteredVisualContainer::Iterator iter;
785 if ( FindVisual( index, mImpl->mVisuals, iter ) )
787 Actor self( Self() );
788 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
789 (*iter)->visual.Reset();
790 mImpl->mVisuals.Erase( iter );
794 Toolkit::Visual::Base Control::GetVisual( Property::Index index ) const
796 RegisteredVisualContainer::Iterator iter;
797 if ( FindVisual( index, mImpl->mVisuals, iter ) )
799 return (*iter)->visual;
802 return Toolkit::Visual::Base();
805 void Control::EnableVisual( Property::Index index, bool enable )
807 RegisteredVisualContainer::Iterator iter;
808 if ( FindVisual( index, mImpl->mVisuals, iter ) )
810 if ( (*iter)->enabled == enable )
812 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual Already enabled set (%s) \n", enable?"enabled":"disabled");
816 (*iter)->enabled = enable;
817 Actor parentActor = Self();
818 if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
822 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) on stage \n", index );
823 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
827 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) off stage \n", index );
828 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
834 bool Control::IsVisualEnabled( Property::Index index ) const
836 RegisteredVisualContainer::Iterator iter;
837 if ( FindVisual( index, mImpl->mVisuals, iter ) )
839 return (*iter)->enabled;
844 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
846 Dali::Animation transition;
847 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
849 if( transitionData.Count() > 0 )
851 // Setup a Transition from TransitionData.
852 TransitionData::Iterator end = transitionData.End();
853 for( TransitionData::Iterator iter = transitionData.Begin() ;
854 iter != end; ++iter )
856 TransitionData::Animator* animator = (*iter);
857 HandleIndex handleIndex;
859 // Attempt to find the object name as a child actor
860 Actor child = Self().FindChildByName( animator->objectName );
863 Property::Index propertyIndex = child.GetPropertyIndex( animator->propertyKey );
864 handleIndex = HandleIndex( child, propertyIndex );
868 handleIndex = GetVisualProperty( *this, mImpl->mVisuals,
869 animator->objectName,
870 animator->propertyKey );
873 if( handleIndex.handle && handleIndex.index != Property::INVALID_INDEX )
875 if( animator->animate == false )
877 if( animator->targetValue.GetType() != Property::NONE )
879 handleIndex.handle.SetProperty( handleIndex.index, animator->targetValue );
884 if( animator->initialValue.GetType() != Property::NONE )
886 handleIndex.handle.SetProperty( handleIndex.index, animator->initialValue );
891 // Create an animation with a default .1 second duration - the animators
892 // will automatically force it to the 'right' duration.
893 transition = Dali::Animation::New( 0.1f );
896 transition.AnimateTo( Property( handleIndex.handle, handleIndex.index ),
897 animator->targetValue,
898 animator->alphaFunction,
899 TimePeriod( animator->timePeriodDelay,
900 animator->timePeriodDuration ) );
909 bool Control::OnAccessibilityActivated()
911 return false; // Accessibility activation is not handled by default
914 bool Control::OnKeyboardEnter()
916 return false; // Keyboard enter is not handled by default
919 bool Control::OnAccessibilityPan(PanGesture gesture)
921 return false; // Accessibility pan gesture is not handled by default
924 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
926 return false; // Accessibility touch event is not handled by default
929 bool Control::OnAccessibilityValueChange(bool isIncrease)
931 return false; // Accessibility value change action is not handled by default
934 bool Control::OnAccessibilityZoom()
936 return false; // Accessibility zoom action is not handled by default
939 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
944 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
948 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
950 return mImpl->mKeyEventSignal;
953 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
955 return mImpl->mKeyInputFocusGainedSignal;
958 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
960 return mImpl->mKeyInputFocusLostSignal;
963 bool Control::EmitKeyEventSignal( const KeyEvent& event )
965 // Guard against destruction during signal emission
966 Dali::Toolkit::Control handle( GetOwner() );
968 bool consumed = false;
970 // signals are allocated dynamically when someone connects
971 if ( !mImpl->mKeyEventSignal.Empty() )
973 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
978 // Notification for derived classes
979 consumed = OnKeyEvent(event);
985 Control::Control( ControlBehaviour behaviourFlags )
986 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
987 mImpl(new Impl(*this))
989 mImpl->mFlags = behaviourFlags;
997 void Control::Initialize()
999 // Call deriving classes so initialised before styling is applied to them.
1002 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
1003 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
1005 Toolkit::StyleManager styleManager = StyleManager::Get();
1007 // if stylemanager is available
1010 StyleManager& styleManagerImpl = GetImpl( styleManager );
1012 // Register for style changes
1013 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
1015 // Apply the current style
1016 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
1020 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
1022 SetKeyboardNavigationSupport( true );
1026 void Control::OnInitialize()
1030 void Control::OnControlChildAdd( Actor& child )
1034 void Control::OnControlChildRemove( Actor& child )
1038 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
1040 // By default the control is only interested in theme (not font) changes
1041 if( styleManager && change == StyleChange::THEME_CHANGE )
1043 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1048 void Control::OnPinch(const PinchGesture& pinch)
1050 if( !( mImpl->mStartingPinchScale ) )
1053 mImpl->mStartingPinchScale = new Vector3;
1056 if( pinch.state == Gesture::Started )
1058 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1061 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1064 void Control::OnPan( const PanGesture& pan )
1068 void Control::OnTap(const TapGesture& tap)
1072 void Control::OnLongPress( const LongPressGesture& longPress )
1076 void Control::EmitKeyInputFocusSignal( bool focusGained )
1078 Dali::Toolkit::Control handle( GetOwner() );
1082 // signals are allocated dynamically when someone connects
1083 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
1085 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
1090 // signals are allocated dynamically when someone connects
1091 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
1093 mImpl->mKeyInputFocusLostSignal.Emit( handle );
1098 void Control::OnStageConnection( int depth )
1100 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
1102 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1104 // Check whether the visual is empty and enabled
1105 if( (*iter)->visual && (*iter)->enabled )
1107 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
1108 Actor self( Self() );
1109 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
1114 void Control::OnStageDisconnection()
1116 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1118 // Check whether the visual is empty
1119 if( (*iter)->visual )
1121 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
1122 Actor self( Self() );
1123 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
1128 void Control::OnKeyInputFocusGained()
1130 EmitKeyInputFocusSignal( true );
1133 void Control::OnKeyInputFocusLost()
1135 EmitKeyInputFocusSignal( false );
1138 void Control::OnChildAdd(Actor& child)
1140 // Notify derived classes.
1141 OnControlChildAdd( child );
1144 void Control::OnChildRemove(Actor& child)
1146 // Notify derived classes.
1147 OnControlChildRemove( child );
1150 void Control::OnSizeSet(const Vector3& targetSize)
1152 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1155 Vector2 size( targetSize );
1156 Property::Map transformMap;
1157 SetDefaultTransform( transformMap );
1158 visual.SetTransformAndSize( transformMap, size );
1162 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1164 // @todo size negotiate background to new size, animate as well?
1167 bool Control::OnTouchEvent(const TouchEvent& event)
1169 return false; // Do not consume
1172 bool Control::OnHoverEvent(const HoverEvent& event)
1174 return false; // Do not consume
1177 bool Control::OnKeyEvent(const KeyEvent& event)
1179 return false; // Do not consume
1182 bool Control::OnWheelEvent(const WheelEvent& event)
1184 return false; // Do not consume
1187 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1189 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1191 container.Add( Self().GetChildAt( i ), size );
1194 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1197 Vector2 controlSize( size );
1198 Property::Map transformMap;
1199 SetDefaultTransform( transformMap );
1200 visual.SetTransformAndSize( transformMap, controlSize );
1204 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1208 Vector3 Control::GetNaturalSize()
1210 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1213 Vector2 naturalSize;
1214 visual.GetNaturalSize( naturalSize );
1215 return Vector3( naturalSize );
1217 return Vector3::ZERO;
1220 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1222 return CalculateChildSizeBase( child, dimension );
1225 float Control::GetHeightForWidth( float width )
1227 return GetHeightForWidthBase( width );
1230 float Control::GetWidthForHeight( float height )
1232 return GetWidthForHeightBase( height );
1235 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1237 return RelayoutDependentOnChildrenBase( dimension );
1240 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1244 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1248 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1250 mImpl->SignalConnected( slotObserver, callback );
1253 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1255 mImpl->SignalDisconnected( slotObserver, callback );
1258 Control& GetImplementation( Dali::Toolkit::Control& handle )
1260 CustomActorImpl& customInterface = handle.GetImplementation();
1261 // downcast to control
1262 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1266 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1268 const CustomActorImpl& customInterface = handle.GetImplementation();
1269 // downcast to control
1270 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1274 } // namespace Internal
1276 } // namespace Toolkit