2 * Copyright (c) 2017 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
26 #include <dali/public-api/animation/constraint.h>
27 #include <dali/public-api/animation/constraints.h>
28 #include <dali/public-api/object/type-registry.h>
29 #include <dali/public-api/object/type-registry-helper.h>
30 #include <dali/public-api/rendering/renderer.h>
31 #include <dali/public-api/size-negotiation/relayout-container.h>
32 #include <dali/devel-api/common/owner-container.h>
33 #include <dali/devel-api/object/handle-devel.h>
34 #include <dali/devel-api/scripting/enum-helper.h>
35 #include <dali/devel-api/scripting/scripting.h>
36 #include <dali/integration-api/debug.h>
39 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
40 #include <dali-toolkit/public-api/controls/control.h>
41 #include <dali-toolkit/public-api/styling/style-manager.h>
42 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
43 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
44 #include <dali-toolkit/devel-api/controls/control-devel.h>
45 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
46 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
47 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
48 #include <dali-toolkit/internal/styling/style-manager-impl.h>
49 #include <dali-toolkit/internal/visuals/color/color-visual.h>
50 #include <dali-toolkit/internal/visuals/transition-data-impl.h>
51 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
52 #include <dali-toolkit/devel-api/align-enums.h>
53 #include <dali-toolkit/internal/controls/tooltip/tooltip.h>
64 #if defined(DEBUG_ENABLED)
65 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
68 DALI_ENUM_TO_STRING_TABLE_BEGIN( CLIPPING_MODE )
69 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, DISABLED )
70 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, CLIP_CHILDREN )
71 DALI_ENUM_TO_STRING_TABLE_END( CLIPPING_MODE )
74 * Struct used to store Visual within the control, index is a unique key for each visual.
76 struct RegisteredVisual
78 Property::Index index;
79 Toolkit::Visual::Base visual;
82 RegisteredVisual( Property::Index aIndex, Toolkit::Visual::Base &aVisual, bool aEnabled)
83 : index(aIndex), visual(aVisual), enabled(aEnabled)
88 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
91 * Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
93 bool FindVisual( Property::Index targetIndex, RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
95 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
97 if ( (*iter)->index == targetIndex )
105 Toolkit::Visual::Base GetVisualByName(
106 RegisteredVisualContainer& visuals,
107 const std::string& visualName )
109 Toolkit::Visual::Base visualHandle;
111 RegisteredVisualContainer::Iterator iter;
112 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
114 Toolkit::Visual::Base visual = (*iter)->visual;
115 if( visual && visual.GetName() == visualName )
117 visualHandle = visual;
125 * Creates control through type registry
129 return Internal::Control::New();
133 * Performs actions as requested using the action name.
134 * @param[in] object The object on which to perform the action.
135 * @param[in] actionName The action to perform.
136 * @param[in] attributes The attributes with which to perfrom this action.
137 * @return true if action has been accepted by this control
139 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
140 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
144 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
146 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
149 // if cast succeeds there is an implementation so no need to check
150 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
158 * Connects a callback function with the object's signals.
159 * @param[in] object The object providing the signal.
160 * @param[in] tracker Used to disconnect the signal.
161 * @param[in] signalName The signal to connect to.
162 * @param[in] functor A newly allocated FunctorDelegate.
163 * @return True if the signal was connected.
164 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
166 const char* SIGNAL_KEY_EVENT = "keyEvent";
167 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
168 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
169 const char* SIGNAL_TAPPED = "tapped";
170 const char* SIGNAL_PANNED = "panned";
171 const char* SIGNAL_PINCHED = "pinched";
172 const char* SIGNAL_LONG_PRESSED = "longPressed";
173 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
175 Dali::BaseHandle handle( object );
177 bool connected( false );
178 Toolkit::Control control = Toolkit::Control::DownCast( handle );
181 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
184 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
186 controlImpl.KeyEventSignal().Connect( tracker, functor );
188 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
190 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
192 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
194 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
196 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
198 controlImpl.EnableGestureDetection( Gesture::Tap );
199 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
201 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
203 controlImpl.EnableGestureDetection( Gesture::Pan );
204 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
206 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
208 controlImpl.EnableGestureDetection( Gesture::Pinch );
209 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
211 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
213 controlImpl.EnableGestureDetection( Gesture::LongPress );
214 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
220 // Setup signals and actions using the type-registry.
221 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
223 // Note: Properties are registered separately below.
225 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
226 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
227 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
228 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
229 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
230 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
231 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
233 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
235 DALI_TYPE_REGISTRATION_END()
237 } // unnamed namespace
242 class Control::Impl : public ConnectionTracker
246 // Construction & Destruction
247 Impl(Control& controlImpl)
248 : mControlImpl( controlImpl ),
250 mBackgroundColor(Color::TRANSPARENT),
251 mStartingPinchScale( NULL ),
253 mPinchGestureDetector(),
254 mPanGestureDetector(),
255 mTapGestureDetector(),
256 mLongPressGestureDetector(),
257 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
258 mIsKeyboardNavigationSupported( false ),
259 mIsKeyboardFocusGroup( false )
265 // All gesture detectors will be destroyed so no need to disconnect.
266 delete mStartingPinchScale;
269 // Gesture Detection Methods
271 void PinchDetected(Actor actor, const PinchGesture& pinch)
273 mControlImpl.OnPinch(pinch);
276 void PanDetected(Actor actor, const PanGesture& pan)
278 mControlImpl.OnPan(pan);
281 void TapDetected(Actor actor, const TapGesture& tap)
283 mControlImpl.OnTap(tap);
286 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
288 mControlImpl.OnLongPress(longPress);
294 * Called when a property of an object of this type is set.
295 * @param[in] object The object whose property is set.
296 * @param[in] index The property index.
297 * @param[in] value The new property value.
299 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
301 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
305 Control& controlImpl( GetImplementation( control ) );
309 case Toolkit::Control::Property::STYLE_NAME:
311 controlImpl.SetStyleName( value.Get< std::string >() );
315 case Toolkit::Control::Property::BACKGROUND_COLOR:
317 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
318 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
322 case Toolkit::Control::Property::BACKGROUND_IMAGE:
324 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
325 Image image = Scripting::NewImage( value );
328 controlImpl.SetBackgroundImage( image );
332 // An empty image means the background is no longer required
333 controlImpl.ClearBackground();
338 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
340 if ( value.Get< bool >() )
342 controlImpl.SetKeyInputFocus();
346 controlImpl.ClearKeyInputFocus();
351 case Toolkit::Control::Property::BACKGROUND:
354 const Property::Map* map = value.GetMap();
355 if( map && !map->Empty() )
357 controlImpl.SetBackground( *map );
359 else if( value.Get( url ) )
361 // don't know the size to load
362 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
365 controlImpl.RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
366 visual.SetDepthIndex( DepthIndex::BACKGROUND );
371 // The background is an empty property map, so we should clear the background
372 controlImpl.ClearBackground();
377 case Toolkit::DevelControl::Property::TOOLTIP:
379 TooltipPtr& tooltipPtr = controlImpl.mImpl->mTooltip;
382 tooltipPtr = Tooltip::New( control );
384 tooltipPtr->SetProperties( value );
391 * Called to retrieve a property of an object of this type.
392 * @param[in] object The object whose property is to be retrieved.
393 * @param[in] index The property index.
394 * @return The current value of the property.
396 static Property::Value GetProperty( BaseObject* object, Property::Index index )
398 Property::Value value;
400 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
404 Control& controlImpl( GetImplementation( control ) );
408 case Toolkit::Control::Property::STYLE_NAME:
410 value = controlImpl.GetStyleName();
414 case Toolkit::Control::Property::BACKGROUND_COLOR:
416 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
417 value = controlImpl.GetBackgroundColor();
421 case Toolkit::Control::Property::BACKGROUND_IMAGE:
423 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
425 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
428 visual.CreatePropertyMap( map );
434 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
436 value = controlImpl.HasKeyInputFocus();
440 case Toolkit::Control::Property::BACKGROUND:
443 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
446 visual.CreatePropertyMap( map );
453 case Toolkit::DevelControl::Property::TOOLTIP:
456 if( controlImpl.mImpl->mTooltip )
458 controlImpl.mImpl->mTooltip->CreatePropertyMap( map );
472 Control& mControlImpl;
473 RegisteredVisualContainer mVisuals; // Stores visuals needed by the control, non trivial type so std::vector used.
474 std::string mStyleName;
475 Vector4 mBackgroundColor; ///< The color of the background visual
476 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
477 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
478 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
479 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
482 PinchGestureDetector mPinchGestureDetector;
483 PanGestureDetector mPanGestureDetector;
484 TapGestureDetector mTapGestureDetector;
485 LongPressGestureDetector mLongPressGestureDetector;
490 ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
491 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
492 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
494 // Properties - these need to be members of Internal::Control::Impl as they access private methods/data of Internal::Control and Internal::Control::Impl.
495 static const PropertyRegistration PROPERTY_1;
496 static const PropertyRegistration PROPERTY_2;
497 static const PropertyRegistration PROPERTY_3;
498 static const PropertyRegistration PROPERTY_4;
499 static const PropertyRegistration PROPERTY_5;
500 static const PropertyRegistration PROPERTY_6;
503 // Properties registered without macro to use specific member variables.
504 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
505 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
506 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
507 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
508 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
509 const PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "tooltip", Toolkit::DevelControl::Property::TOOLTIP, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
511 Toolkit::Control Control::New()
513 // Create the implementation, temporarily owned on stack
514 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
516 // Pass ownership to handle
517 Toolkit::Control handle( *controlImpl );
519 // Second-phase init of the implementation
520 // This can only be done after the CustomActor connection has been made...
521 controlImpl->Initialize();
526 void Control::SetStyleName( const std::string& styleName )
528 if( styleName != mImpl->mStyleName )
530 mImpl->mStyleName = styleName;
532 // Apply new style, if stylemanager is available
533 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
536 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
541 const std::string& Control::GetStyleName() const
543 return mImpl->mStyleName;
546 void Control::SetBackgroundColor( const Vector4& color )
548 mImpl->mBackgroundColor = color;
550 map[ Toolkit::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
551 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
553 SetBackground( map );
556 Vector4 Control::GetBackgroundColor() const
558 return mImpl->mBackgroundColor;
561 void Control::SetBackground( const Property::Map& map )
563 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
566 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
567 visual.SetDepthIndex( DepthIndex::BACKGROUND );
569 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
574 void Control::SetBackgroundImage( Image image )
576 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
579 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
580 visual.SetDepthIndex( DepthIndex::BACKGROUND );
584 void Control::ClearBackground()
586 UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
587 mImpl->mBackgroundColor = Color::TRANSPARENT;
589 // Trigger a size negotiation request that may be needed when unregistering a visual.
593 void Control::EnableGestureDetection(Gesture::Type type)
595 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
597 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
598 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
599 mImpl->mPinchGestureDetector.Attach(Self());
602 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
604 mImpl->mPanGestureDetector = PanGestureDetector::New();
605 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
606 mImpl->mPanGestureDetector.Attach(Self());
609 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
611 mImpl->mTapGestureDetector = TapGestureDetector::New();
612 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
613 mImpl->mTapGestureDetector.Attach(Self());
616 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
618 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
619 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
620 mImpl->mLongPressGestureDetector.Attach(Self());
624 void Control::DisableGestureDetection(Gesture::Type type)
626 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
628 mImpl->mPinchGestureDetector.Detach(Self());
629 mImpl->mPinchGestureDetector.Reset();
632 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
634 mImpl->mPanGestureDetector.Detach(Self());
635 mImpl->mPanGestureDetector.Reset();
638 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
640 mImpl->mTapGestureDetector.Detach(Self());
641 mImpl->mTapGestureDetector.Reset();
644 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
646 mImpl->mLongPressGestureDetector.Detach(Self());
647 mImpl->mLongPressGestureDetector.Reset();
651 PinchGestureDetector Control::GetPinchGestureDetector() const
653 return mImpl->mPinchGestureDetector;
656 PanGestureDetector Control::GetPanGestureDetector() const
658 return mImpl->mPanGestureDetector;
661 TapGestureDetector Control::GetTapGestureDetector() const
663 return mImpl->mTapGestureDetector;
666 LongPressGestureDetector Control::GetLongPressGestureDetector() const
668 return mImpl->mLongPressGestureDetector;
671 void Control::SetKeyboardNavigationSupport(bool isSupported)
673 mImpl->mIsKeyboardNavigationSupported = isSupported;
676 bool Control::IsKeyboardNavigationSupported()
678 return mImpl->mIsKeyboardNavigationSupported;
681 void Control::SetKeyInputFocus()
683 if( Self().OnStage() )
685 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
689 bool Control::HasKeyInputFocus()
692 if( Self().OnStage() )
694 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
699 void Control::ClearKeyInputFocus()
701 if( Self().OnStage() )
703 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
707 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
709 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
711 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
712 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
715 bool Control::IsKeyboardFocusGroup()
717 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
720 void Control::AccessibilityActivate()
722 // Inform deriving classes
723 OnAccessibilityActivated();
726 void Control::KeyboardEnter()
728 // Inform deriving classes
732 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
734 RegisterVisual( index, visual, true );
737 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
739 bool visualReplaced ( false );
742 if( !mImpl->mVisuals.Empty() )
744 RegisteredVisualContainer::Iterator iter;
745 // Check if visual (index) is already registered. Replace if so.
746 if ( FindVisual( index, mImpl->mVisuals, iter ) )
748 if( (*iter)->visual && self.OnStage() )
750 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
752 (*iter)->visual = visual;
753 visualReplaced = true;
757 // If not set, set the name of the visual to the same name as the control's property.
758 // ( If the control has been type registered )
759 if( visual.GetName().empty() )
761 // Check if the control has been type registered:
762 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(*this) );
765 // Check if the property index has been registered:
766 Property::IndexContainer indices;
767 typeInfo.GetPropertyIndices( indices );
768 Property::IndexContainer::Iterator iter = std::find( indices.Begin(), indices.End(), index );
769 if( iter != indices.End() )
771 // If it has, then get it's name and use that for the visual
772 std::string visualName = typeInfo.GetPropertyName( index );
773 visual.SetName( visualName );
778 if( !visualReplaced ) // New registration entry
780 mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
783 if( visual && self.OnStage() && enabled )
785 Toolkit::GetImplementation(visual).SetOnStage( self );
788 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n", visual.GetName().c_str(), index, enabled?"T":"F" );
791 void Control::UnregisterVisual( Property::Index index )
793 RegisteredVisualContainer::Iterator iter;
794 if ( FindVisual( index, mImpl->mVisuals, iter ) )
796 Actor self( Self() );
797 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
798 (*iter)->visual.Reset();
799 mImpl->mVisuals.Erase( iter );
803 Toolkit::Visual::Base Control::GetVisual( Property::Index index ) const
805 RegisteredVisualContainer::Iterator iter;
806 if ( FindVisual( index, mImpl->mVisuals, iter ) )
808 return (*iter)->visual;
811 return Toolkit::Visual::Base();
814 void Control::EnableVisual( Property::Index index, bool enable )
816 RegisteredVisualContainer::Iterator iter;
817 if ( FindVisual( index, mImpl->mVisuals, iter ) )
819 if ( (*iter)->enabled == enable )
821 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual %s(%d) already %s\n", (*iter)->visual.GetName().c_str(), index, enable?"enabled":"disabled");
825 (*iter)->enabled = enable;
826 Actor parentActor = Self();
827 if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
831 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) on stage \n", (*iter)->visual.GetName().c_str(), index );
832 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
836 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) off stage \n", (*iter)->visual.GetName().c_str(), index );
837 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
843 bool Control::IsVisualEnabled( Property::Index index ) const
845 RegisteredVisualContainer::Iterator iter;
846 if ( FindVisual( index, mImpl->mVisuals, iter ) )
848 return (*iter)->enabled;
853 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
855 Dali::Animation transition;
856 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
858 if( transitionData.Count() > 0 )
860 // Setup a Transition from TransitionData.
861 TransitionData::Iterator end = transitionData.End();
862 for( TransitionData::Iterator iter = transitionData.Begin() ;
863 iter != end; ++iter )
865 TransitionData::Animator* animator = (*iter);
867 Toolkit::Visual::Base visual = GetVisualByName( mImpl->mVisuals, animator->objectName );
871 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
872 visualImpl.AnimateProperty( transition, *animator );
876 // Otherwise, try any actor children of control (Including the control)
877 Actor child = Self().FindChildByName( animator->objectName );
880 Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
881 if( propertyIndex != Property::INVALID_INDEX )
883 if( animator->animate == false )
885 if( animator->targetValue.GetType() != Property::NONE )
887 child.SetProperty( propertyIndex, animator->targetValue );
890 else // animate the property
892 if( animator->initialValue.GetType() != Property::NONE )
894 child.SetProperty( propertyIndex, animator->initialValue );
899 transition = Dali::Animation::New( 0.1f );
902 transition.AnimateTo( Property( child, propertyIndex ),
903 animator->targetValue,
904 animator->alphaFunction,
905 TimePeriod( animator->timePeriodDelay,
906 animator->timePeriodDuration ) );
917 bool Control::OnAccessibilityActivated()
919 return false; // Accessibility activation is not handled by default
922 bool Control::OnKeyboardEnter()
924 return false; // Keyboard enter is not handled by default
927 bool Control::OnAccessibilityPan(PanGesture gesture)
929 return false; // Accessibility pan gesture is not handled by default
932 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
934 return false; // Accessibility touch event is not handled by default
937 bool Control::OnAccessibilityValueChange(bool isIncrease)
939 return false; // Accessibility value change action is not handled by default
942 bool Control::OnAccessibilityZoom()
944 return false; // Accessibility zoom action is not handled by default
947 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
952 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
956 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
958 return mImpl->mKeyEventSignal;
961 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
963 return mImpl->mKeyInputFocusGainedSignal;
966 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
968 return mImpl->mKeyInputFocusLostSignal;
971 bool Control::EmitKeyEventSignal( const KeyEvent& event )
973 // Guard against destruction during signal emission
974 Dali::Toolkit::Control handle( GetOwner() );
976 bool consumed = false;
978 // signals are allocated dynamically when someone connects
979 if ( !mImpl->mKeyEventSignal.Empty() )
981 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
986 // Notification for derived classes
987 consumed = OnKeyEvent(event);
993 Control::Control( ControlBehaviour behaviourFlags )
994 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
995 mImpl(new Impl(*this))
997 mImpl->mFlags = behaviourFlags;
1005 void Control::Initialize()
1007 // Call deriving classes so initialised before styling is applied to them.
1010 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
1011 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
1013 Toolkit::StyleManager styleManager = StyleManager::Get();
1015 // if stylemanager is available
1018 StyleManager& styleManagerImpl = GetImpl( styleManager );
1020 // Register for style changes
1021 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
1023 // Apply the current style
1024 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
1028 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
1030 SetKeyboardNavigationSupport( true );
1034 void Control::OnInitialize()
1038 void Control::OnControlChildAdd( Actor& child )
1042 void Control::OnControlChildRemove( Actor& child )
1046 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
1048 // By default the control is only interested in theme (not font) changes
1049 if( styleManager && change == StyleChange::THEME_CHANGE )
1051 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1056 void Control::OnPinch(const PinchGesture& pinch)
1058 if( !( mImpl->mStartingPinchScale ) )
1061 mImpl->mStartingPinchScale = new Vector3;
1064 if( pinch.state == Gesture::Started )
1066 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1069 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1072 void Control::OnPan( const PanGesture& pan )
1076 void Control::OnTap(const TapGesture& tap)
1080 void Control::OnLongPress( const LongPressGesture& longPress )
1084 void Control::EmitKeyInputFocusSignal( bool focusGained )
1086 Dali::Toolkit::Control handle( GetOwner() );
1090 // signals are allocated dynamically when someone connects
1091 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
1093 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
1098 // signals are allocated dynamically when someone connects
1099 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
1101 mImpl->mKeyInputFocusLostSignal.Emit( handle );
1106 void Control::OnStageConnection( int depth )
1108 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
1110 Actor self( Self() );
1112 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1114 // Check whether the visual is empty and enabled
1115 if( (*iter)->visual && (*iter)->enabled )
1117 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
1118 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
1122 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() )
1124 Property::Value clippingValue = self.GetProperty( Actor::Property::CLIPPING_MODE );
1125 int clippingMode = ClippingMode::DISABLED;
1126 if( clippingValue.Get( clippingMode ) )
1128 // Add a transparent background if we do not have any renderers or visuals so we clip our children
1130 if( clippingMode == ClippingMode::CLIP_CHILDREN )
1132 // Create a transparent background visual which will also get staged.
1133 SetBackgroundColor( Color::TRANSPARENT );
1139 void Control::OnStageDisconnection()
1141 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1143 // Check whether the visual is empty
1144 if( (*iter)->visual )
1146 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
1147 Actor self( Self() );
1148 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
1153 void Control::OnKeyInputFocusGained()
1155 EmitKeyInputFocusSignal( true );
1158 void Control::OnKeyInputFocusLost()
1160 EmitKeyInputFocusSignal( false );
1163 void Control::OnChildAdd(Actor& child)
1165 // Notify derived classes.
1166 OnControlChildAdd( child );
1169 void Control::OnChildRemove(Actor& child)
1171 // Notify derived classes.
1172 OnControlChildRemove( child );
1175 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
1177 Actor self( Self() );
1178 if( index == Actor::Property::CLIPPING_MODE )
1180 // Only set the background if we're already on the stage and have no renderers or visuals
1182 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() && self.OnStage() )
1184 ClippingMode::Type clippingMode = ClippingMode::DISABLED;
1185 if( Scripting::GetEnumerationProperty< ClippingMode::Type >( propertyValue, CLIPPING_MODE_TABLE, CLIPPING_MODE_TABLE_COUNT, clippingMode ) )
1187 // Add a transparent background if we do not have one so we clip children
1189 if( clippingMode == ClippingMode::CLIP_CHILDREN )
1191 SetBackgroundColor( Color::TRANSPARENT );
1198 void Control::OnSizeSet(const Vector3& targetSize)
1200 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1203 Vector2 size( targetSize );
1204 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
1208 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1210 // @todo size negotiate background to new size, animate as well?
1213 bool Control::OnTouchEvent(const TouchEvent& event)
1215 return false; // Do not consume
1218 bool Control::OnHoverEvent(const HoverEvent& event)
1220 return false; // Do not consume
1223 bool Control::OnKeyEvent(const KeyEvent& event)
1225 return false; // Do not consume
1228 bool Control::OnWheelEvent(const WheelEvent& event)
1230 return false; // Do not consume
1233 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1235 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1237 container.Add( Self().GetChildAt( i ), size );
1240 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1243 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
1247 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1251 Vector3 Control::GetNaturalSize()
1253 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1256 Vector2 naturalSize;
1257 visual.GetNaturalSize( naturalSize );
1258 return Vector3( naturalSize );
1260 return Vector3::ZERO;
1263 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1265 return CalculateChildSizeBase( child, dimension );
1268 float Control::GetHeightForWidth( float width )
1270 return GetHeightForWidthBase( width );
1273 float Control::GetWidthForHeight( float height )
1275 return GetWidthForHeightBase( height );
1278 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1280 return RelayoutDependentOnChildrenBase( dimension );
1283 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1287 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1291 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1293 mImpl->SignalConnected( slotObserver, callback );
1296 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1298 mImpl->SignalDisconnected( slotObserver, callback );
1301 Control& GetImplementation( Dali::Toolkit::Control& handle )
1303 CustomActorImpl& customInterface = handle.GetImplementation();
1304 // downcast to control
1305 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1309 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1311 const CustomActorImpl& customInterface = handle.GetImplementation();
1312 // downcast to control
1313 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1317 } // namespace Internal
1319 } // namespace Toolkit