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 );
588 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
593 void Control::SetBackgroundImage( Image image )
595 DALI_LOG_WARNING( "SetBackgroundImage is for the depreciated Property::BACKGROUND_IMAGE use SetBackground( const Property::Map& map )\n" );
596 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
599 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
600 visual.SetDepthIndex( DepthIndex::BACKGROUND );
604 void Control::ClearBackground()
606 UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
607 mImpl->mBackgroundColor = Color::TRANSPARENT;
609 // Trigger a size negotiation request that may be needed when unregistering a visual.
613 void Control::EnableGestureDetection(Gesture::Type type)
615 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
617 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
618 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
619 mImpl->mPinchGestureDetector.Attach(Self());
622 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
624 mImpl->mPanGestureDetector = PanGestureDetector::New();
625 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
626 mImpl->mPanGestureDetector.Attach(Self());
629 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
631 mImpl->mTapGestureDetector = TapGestureDetector::New();
632 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
633 mImpl->mTapGestureDetector.Attach(Self());
636 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
638 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
639 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
640 mImpl->mLongPressGestureDetector.Attach(Self());
644 void Control::DisableGestureDetection(Gesture::Type type)
646 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
648 mImpl->mPinchGestureDetector.Detach(Self());
649 mImpl->mPinchGestureDetector.Reset();
652 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
654 mImpl->mPanGestureDetector.Detach(Self());
655 mImpl->mPanGestureDetector.Reset();
658 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
660 mImpl->mTapGestureDetector.Detach(Self());
661 mImpl->mTapGestureDetector.Reset();
664 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
666 mImpl->mLongPressGestureDetector.Detach(Self());
667 mImpl->mLongPressGestureDetector.Reset();
671 PinchGestureDetector Control::GetPinchGestureDetector() const
673 return mImpl->mPinchGestureDetector;
676 PanGestureDetector Control::GetPanGestureDetector() const
678 return mImpl->mPanGestureDetector;
681 TapGestureDetector Control::GetTapGestureDetector() const
683 return mImpl->mTapGestureDetector;
686 LongPressGestureDetector Control::GetLongPressGestureDetector() const
688 return mImpl->mLongPressGestureDetector;
691 void Control::SetKeyboardNavigationSupport(bool isSupported)
693 mImpl->mIsKeyboardNavigationSupported = isSupported;
696 bool Control::IsKeyboardNavigationSupported()
698 return mImpl->mIsKeyboardNavigationSupported;
701 void Control::SetKeyInputFocus()
703 if( Self().OnStage() )
705 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
709 bool Control::HasKeyInputFocus()
712 if( Self().OnStage() )
714 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
719 void Control::ClearKeyInputFocus()
721 if( Self().OnStage() )
723 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
727 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
729 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
731 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
732 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
735 bool Control::IsKeyboardFocusGroup()
737 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
740 void Control::AccessibilityActivate()
742 // Inform deriving classes
743 OnAccessibilityActivated();
746 void Control::KeyboardEnter()
748 // Inform deriving classes
752 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
754 RegisterVisual( index, visual, true );
757 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
759 bool visualReplaced ( false );
762 if( !mImpl->mVisuals.Empty() )
764 RegisteredVisualContainer::Iterator iter;
765 // Check if visual (index) is already registered. Replace if so.
766 if ( FindVisual( index, mImpl->mVisuals, iter ) )
768 if( (*iter)->visual && self.OnStage() )
770 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
772 (*iter)->visual = visual;
773 visualReplaced = true;
777 if( !visualReplaced ) // New registration entry
779 mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
782 if( visual && self.OnStage() && enabled )
784 Toolkit::GetImplementation(visual).SetOnStage( self );
788 void Control::UnregisterVisual( Property::Index index )
790 RegisteredVisualContainer::Iterator iter;
791 if ( FindVisual( index, mImpl->mVisuals, iter ) )
793 Actor self( Self() );
794 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
795 (*iter)->visual.Reset();
796 mImpl->mVisuals.Erase( iter );
800 Toolkit::Visual::Base Control::GetVisual( Property::Index index ) const
802 RegisteredVisualContainer::Iterator iter;
803 if ( FindVisual( index, mImpl->mVisuals, iter ) )
805 return (*iter)->visual;
808 return Toolkit::Visual::Base();
811 void Control::EnableVisual( Property::Index index, bool enable )
813 RegisteredVisualContainer::Iterator iter;
814 if ( FindVisual( index, mImpl->mVisuals, iter ) )
816 if ( (*iter)->enabled == enable )
818 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual Already enabled set (%s) \n", enable?"enabled":"disabled");
822 (*iter)->enabled = enable;
823 Actor parentActor = Self();
824 if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
828 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) on stage \n", index );
829 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
833 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) off stage \n", index );
834 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
840 bool Control::IsVisualEnabled( Property::Index index ) const
842 RegisteredVisualContainer::Iterator iter;
843 if ( FindVisual( index, mImpl->mVisuals, iter ) )
845 return (*iter)->enabled;
850 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
852 Dali::Animation transition;
853 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
855 if( transitionData.Count() > 0 )
857 // Setup a Transition from TransitionData.
858 TransitionData::Iterator end = transitionData.End();
859 for( TransitionData::Iterator iter = transitionData.Begin() ;
860 iter != end; ++iter )
862 TransitionData::Animator* animator = (*iter);
863 HandleIndex handleIndex;
865 // Attempt to find the object name as a child actor
866 Actor child = Self().FindChildByName( animator->objectName );
869 Property::Index propertyIndex = child.GetPropertyIndex( animator->propertyKey );
870 handleIndex = HandleIndex( child, propertyIndex );
874 handleIndex = GetVisualProperty( *this, mImpl->mVisuals,
875 animator->objectName,
876 animator->propertyKey );
879 if( handleIndex.handle && handleIndex.index != Property::INVALID_INDEX )
881 if( animator->animate == false )
883 if( animator->targetValue.GetType() != Property::NONE )
885 handleIndex.handle.SetProperty( handleIndex.index, animator->targetValue );
890 if( animator->initialValue.GetType() != Property::NONE )
892 handleIndex.handle.SetProperty( handleIndex.index, animator->initialValue );
897 // Create an animation with a default .1 second duration - the animators
898 // will automatically force it to the 'right' duration.
899 transition = Dali::Animation::New( 0.1f );
902 transition.AnimateTo( Property( handleIndex.handle, handleIndex.index ),
903 animator->targetValue,
904 animator->alphaFunction,
905 TimePeriod( animator->timePeriodDelay,
906 animator->timePeriodDuration ) );
915 bool Control::OnAccessibilityActivated()
917 return false; // Accessibility activation is not handled by default
920 bool Control::OnKeyboardEnter()
922 return false; // Keyboard enter is not handled by default
925 bool Control::OnAccessibilityPan(PanGesture gesture)
927 return false; // Accessibility pan gesture is not handled by default
930 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
932 return false; // Accessibility touch event is not handled by default
935 bool Control::OnAccessibilityValueChange(bool isIncrease)
937 return false; // Accessibility value change action is not handled by default
940 bool Control::OnAccessibilityZoom()
942 return false; // Accessibility zoom action is not handled by default
945 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
950 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
954 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
956 return mImpl->mKeyEventSignal;
959 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
961 return mImpl->mKeyInputFocusGainedSignal;
964 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
966 return mImpl->mKeyInputFocusLostSignal;
969 bool Control::EmitKeyEventSignal( const KeyEvent& event )
971 // Guard against destruction during signal emission
972 Dali::Toolkit::Control handle( GetOwner() );
974 bool consumed = false;
976 // signals are allocated dynamically when someone connects
977 if ( !mImpl->mKeyEventSignal.Empty() )
979 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
984 // Notification for derived classes
985 consumed = OnKeyEvent(event);
991 Control::Control( ControlBehaviour behaviourFlags )
992 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
993 mImpl(new Impl(*this))
995 mImpl->mFlags = behaviourFlags;
1003 void Control::Initialize()
1005 // Call deriving classes so initialised before styling is applied to them.
1008 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
1009 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
1011 Toolkit::StyleManager styleManager = StyleManager::Get();
1013 // if stylemanager is available
1016 StyleManager& styleManagerImpl = GetImpl( styleManager );
1018 // Register for style changes
1019 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
1021 // Apply the current style
1022 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
1026 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
1028 SetKeyboardNavigationSupport( true );
1032 void Control::OnInitialize()
1036 void Control::OnControlChildAdd( Actor& child )
1040 void Control::OnControlChildRemove( Actor& child )
1044 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
1046 // By default the control is only interested in theme (not font) changes
1047 if( styleManager && change == StyleChange::THEME_CHANGE )
1049 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1054 void Control::OnPinch(const PinchGesture& pinch)
1056 if( !( mImpl->mStartingPinchScale ) )
1059 mImpl->mStartingPinchScale = new Vector3;
1062 if( pinch.state == Gesture::Started )
1064 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1067 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1070 void Control::OnPan( const PanGesture& pan )
1074 void Control::OnTap(const TapGesture& tap)
1078 void Control::OnLongPress( const LongPressGesture& longPress )
1082 void Control::EmitKeyInputFocusSignal( bool focusGained )
1084 Dali::Toolkit::Control handle( GetOwner() );
1088 // signals are allocated dynamically when someone connects
1089 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
1091 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
1096 // signals are allocated dynamically when someone connects
1097 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
1099 mImpl->mKeyInputFocusLostSignal.Emit( handle );
1104 void Control::OnStageConnection( int depth )
1106 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
1108 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1110 // Check whether the visual is empty and enabled
1111 if( (*iter)->visual && (*iter)->enabled )
1113 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
1114 Actor self( Self() );
1115 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
1120 void Control::OnStageDisconnection()
1122 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1124 // Check whether the visual is empty
1125 if( (*iter)->visual )
1127 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
1128 Actor self( Self() );
1129 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
1134 void Control::OnKeyInputFocusGained()
1136 EmitKeyInputFocusSignal( true );
1139 void Control::OnKeyInputFocusLost()
1141 EmitKeyInputFocusSignal( false );
1144 void Control::OnChildAdd(Actor& child)
1146 // Notify derived classes.
1147 OnControlChildAdd( child );
1150 void Control::OnChildRemove(Actor& child)
1152 // Notify derived classes.
1153 OnControlChildRemove( child );
1156 void Control::OnSizeSet(const Vector3& targetSize)
1158 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1161 Vector2 size( targetSize );
1162 Property::Map transformMap;
1163 SetDefaultTransform( transformMap );
1164 visual.SetTransformAndSize( transformMap, size );
1168 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1170 // @todo size negotiate background to new size, animate as well?
1173 bool Control::OnTouchEvent(const TouchEvent& event)
1175 return false; // Do not consume
1178 bool Control::OnHoverEvent(const HoverEvent& event)
1180 return false; // Do not consume
1183 bool Control::OnKeyEvent(const KeyEvent& event)
1185 return false; // Do not consume
1188 bool Control::OnWheelEvent(const WheelEvent& event)
1190 return false; // Do not consume
1193 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1195 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1197 container.Add( Self().GetChildAt( i ), size );
1200 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1203 Vector2 controlSize( size );
1204 Property::Map transformMap;
1205 SetDefaultTransform( transformMap );
1206 visual.SetTransformAndSize( transformMap, controlSize );
1210 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1214 Vector3 Control::GetNaturalSize()
1216 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1219 Vector2 naturalSize;
1220 visual.GetNaturalSize( naturalSize );
1221 return Vector3( naturalSize );
1223 return Vector3::ZERO;
1226 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1228 return CalculateChildSizeBase( child, dimension );
1231 float Control::GetHeightForWidth( float width )
1233 return GetHeightForWidthBase( width );
1236 float Control::GetWidthForHeight( float height )
1238 return GetWidthForHeightBase( height );
1241 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1243 return RelayoutDependentOnChildrenBase( dimension );
1246 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1250 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1254 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1256 mImpl->SignalConnected( slotObserver, callback );
1259 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1261 mImpl->SignalDisconnected( slotObserver, callback );
1264 Control& GetImplementation( Dali::Toolkit::Control& handle )
1266 CustomActorImpl& customInterface = handle.GetImplementation();
1267 // downcast to control
1268 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1272 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1274 const CustomActorImpl& customInterface = handle.GetImplementation();
1275 // downcast to control
1276 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1280 } // namespace Internal
1282 } // namespace Toolkit