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/object/handle-devel.h>
33 #include <dali/devel-api/scripting/scripting.h>
34 #include <dali/integration-api/debug.h>
37 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
38 #include <dali-toolkit/public-api/controls/control.h>
39 #include <dali-toolkit/public-api/styling/style-manager.h>
40 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
41 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
42 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
43 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
44 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
45 #include <dali-toolkit/internal/styling/style-manager-impl.h>
46 #include <dali-toolkit/internal/visuals/color/color-visual.h>
47 #include <dali-toolkit/internal/visuals/transition-data-impl.h>
48 #include <dali-toolkit/devel-api/align-enums.h>
59 #if defined(DEBUG_ENABLED)
60 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::General, false, "LOG_CONTROL_VISUALS");
64 * Struct used to store Visual within the control, index is a unique key for each visual.
66 struct RegisteredVisual
68 Property::Index index;
69 Toolkit::Visual::Base visual;
72 RegisteredVisual( Property::Index aIndex, Toolkit::Visual::Base &aVisual, bool aEnabled) :
73 index(aIndex), visual(aVisual), enabled(aEnabled) {}
78 Handle handle; ///< a handle to the target object
79 Property::Index index; ///< The index of a property provided by the referenced object
83 index( Property::INVALID_INDEX )
87 HandleIndex( Handle& handle, Property::Index index )
95 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
98 * Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
100 bool FindVisual( Property::Index targetIndex, RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
102 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
104 if ( (*iter)->index == targetIndex )
112 HandleIndex GetVisualProperty(
113 Internal::Control& controlImpl,
114 RegisteredVisualContainer& visuals,
115 const std::string& visualName,
116 Property::Key propertyKey )
118 #if defined(DEBUG_ENABLED)
119 std::ostringstream oss;
120 oss << "Control::GetHandleIndex(" << visualName << ", " << propertyKey << ")" << std::endl;
121 DALI_LOG_INFO( gLogFilter, Debug::General, oss.str().c_str() );
124 // Find visualName in the control
125 RegisteredVisualContainer::Iterator iter;
126 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
128 if ( (*iter)->visual.GetName() == visualName )
134 // Does it's renderer have an associated property?
135 if( iter != visuals.End() )
137 Actor self = controlImpl.Self();
138 Property::Index index = DevelHandle::GetPropertyIndex( self, propertyKey );
139 if( index != Property::INVALID_INDEX )
141 // It's an actor property:
142 return HandleIndex( self, index );
146 // Check if it is a renderer property:
147 if( self.GetRendererCount() > 0 )
149 // @todo Need to use correct renderer index
150 Renderer renderer = self.GetRendererAt(0);
151 Property::Index index = DevelHandle::GetPropertyIndex( renderer, propertyKey );
152 if( index != Property::INVALID_INDEX )
154 // It's a renderer property:
155 return HandleIndex( renderer, index );
160 std::ostringstream oss;
162 DALI_LOG_WARNING( "Control::GetHandleIndex(%s, %s) No renderers\n", visualName.c_str(), oss.str().c_str() );
167 return HandleIndex( handle, Property::INVALID_INDEX );
170 void SetDefaultTransform( Property::Map& propertyMap )
174 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET, Vector2(0.0f, 0.0f) )
175 .Add( Toolkit::DevelVisual::Transform::Property::SIZE, Vector2(1.0f, 1.0f) )
176 .Add( Toolkit::DevelVisual::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
177 .Add( Toolkit::DevelVisual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER )
178 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET_SIZE_MODE, Vector4::ZERO );
182 * Creates control through type registry
186 return Internal::Control::New();
190 * Performs actions as requested using the action name.
191 * @param[in] object The object on which to perform the action.
192 * @param[in] actionName The action to perform.
193 * @param[in] attributes The attributes with which to perfrom this action.
194 * @return true if action has been accepted by this control
196 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
197 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
201 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
203 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
206 // if cast succeeds there is an implementation so no need to check
207 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
215 * Connects a callback function with the object's signals.
216 * @param[in] object The object providing the signal.
217 * @param[in] tracker Used to disconnect the signal.
218 * @param[in] signalName The signal to connect to.
219 * @param[in] functor A newly allocated FunctorDelegate.
220 * @return True if the signal was connected.
221 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
223 const char* SIGNAL_KEY_EVENT = "keyEvent";
224 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
225 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
226 const char* SIGNAL_TAPPED = "tapped";
227 const char* SIGNAL_PANNED = "panned";
228 const char* SIGNAL_PINCHED = "pinched";
229 const char* SIGNAL_LONG_PRESSED = "longPressed";
230 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
232 Dali::BaseHandle handle( object );
234 bool connected( false );
235 Toolkit::Control control = Toolkit::Control::DownCast( handle );
238 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
241 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
243 controlImpl.KeyEventSignal().Connect( tracker, functor );
245 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
247 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
249 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
251 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
253 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
255 controlImpl.EnableGestureDetection( Gesture::Tap );
256 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
258 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
260 controlImpl.EnableGestureDetection( Gesture::Pan );
261 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
263 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
265 controlImpl.EnableGestureDetection( Gesture::Pinch );
266 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
268 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
270 controlImpl.EnableGestureDetection( Gesture::LongPress );
271 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
277 // Setup signals and actions using the type-registry.
278 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
280 // Note: Properties are registered separately below.
282 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
283 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
284 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
285 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
286 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
287 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
288 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
290 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
292 DALI_TYPE_REGISTRATION_END()
294 } // unnamed namespace
299 class Control::Impl : public ConnectionTracker
303 // Construction & Destruction
304 Impl(Control& controlImpl)
305 : mControlImpl( controlImpl ),
307 mBackgroundColor(Color::TRANSPARENT),
308 mStartingPinchScale( NULL ),
310 mPinchGestureDetector(),
311 mPanGestureDetector(),
312 mTapGestureDetector(),
313 mLongPressGestureDetector(),
314 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
315 mIsKeyboardNavigationSupported( false ),
316 mIsKeyboardFocusGroup( false )
322 // All gesture detectors will be destroyed so no need to disconnect.
323 delete mStartingPinchScale;
326 // Gesture Detection Methods
328 void PinchDetected(Actor actor, const PinchGesture& pinch)
330 mControlImpl.OnPinch(pinch);
333 void PanDetected(Actor actor, const PanGesture& pan)
335 mControlImpl.OnPan(pan);
338 void TapDetected(Actor actor, const TapGesture& tap)
340 mControlImpl.OnTap(tap);
343 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
345 mControlImpl.OnLongPress(longPress);
351 * Called when a property of an object of this type is set.
352 * @param[in] object The object whose property is set.
353 * @param[in] index The property index.
354 * @param[in] value The new property value.
356 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
358 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
362 Control& controlImpl( GetImplementation( control ) );
366 case Toolkit::Control::Property::STYLE_NAME:
368 controlImpl.SetStyleName( value.Get< std::string >() );
372 case Toolkit::Control::Property::BACKGROUND_COLOR:
374 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
375 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
379 case Toolkit::Control::Property::BACKGROUND_IMAGE:
381 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
382 Image image = Scripting::NewImage( value );
385 controlImpl.SetBackgroundImage( image );
389 // An empty image means the background is no longer required
390 controlImpl.ClearBackground();
395 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
397 if ( value.Get< bool >() )
399 controlImpl.SetKeyInputFocus();
403 controlImpl.ClearKeyInputFocus();
408 case Toolkit::Control::Property::BACKGROUND:
411 const Property::Map* map = value.GetMap();
412 if( map && !map->Empty() )
414 controlImpl.SetBackground( *map );
416 else if( value.Get( url ) )
418 // don't know the size to load
419 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
422 controlImpl.RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
423 visual.SetDepthIndex( DepthIndex::BACKGROUND );
428 // The background is an empty property map, so we should clear the background
429 controlImpl.ClearBackground();
438 * Called to retrieve a property of an object of this type.
439 * @param[in] object The object whose property is to be retrieved.
440 * @param[in] index The property index.
441 * @return The current value of the property.
443 static Property::Value GetProperty( BaseObject* object, Property::Index index )
445 Property::Value value;
447 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
451 Control& controlImpl( GetImplementation( control ) );
455 case Toolkit::Control::Property::STYLE_NAME:
457 value = controlImpl.GetStyleName();
461 case Toolkit::Control::Property::BACKGROUND_COLOR:
463 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
464 value = controlImpl.GetBackgroundColor();
468 case Toolkit::Control::Property::BACKGROUND_IMAGE:
470 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
472 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
475 visual.CreatePropertyMap( map );
481 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
483 value = controlImpl.HasKeyInputFocus();
487 case Toolkit::Control::Property::BACKGROUND:
490 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
493 visual.CreatePropertyMap( map );
508 Control& mControlImpl;
509 RegisteredVisualContainer mVisuals; // Stores visuals needed by the control, non trivial type so std::vector used.
510 std::string mStyleName;
511 Vector4 mBackgroundColor; ///< The color of the background visual
512 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
513 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
514 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
515 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
518 PinchGestureDetector mPinchGestureDetector;
519 PanGestureDetector mPanGestureDetector;
520 TapGestureDetector mTapGestureDetector;
521 LongPressGestureDetector mLongPressGestureDetector;
523 ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
524 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
525 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
527 // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
528 static const PropertyRegistration PROPERTY_1;
529 static const PropertyRegistration PROPERTY_2;
530 static const PropertyRegistration PROPERTY_3;
531 static const PropertyRegistration PROPERTY_4;
532 static const PropertyRegistration PROPERTY_5;
535 // Properties registered without macro to use specific member variables.
536 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
537 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
538 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
539 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
540 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
542 Toolkit::Control Control::New()
544 // Create the implementation, temporarily owned on stack
545 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
547 // Pass ownership to handle
548 Toolkit::Control handle( *controlImpl );
550 // Second-phase init of the implementation
551 // This can only be done after the CustomActor connection has been made...
552 controlImpl->Initialize();
557 void Control::SetStyleName( const std::string& styleName )
559 if( styleName != mImpl->mStyleName )
561 mImpl->mStyleName = styleName;
563 // Apply new style, if stylemanager is available
564 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
567 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
572 const std::string& Control::GetStyleName() const
574 return mImpl->mStyleName;
577 void Control::SetBackgroundColor( const Vector4& color )
579 mImpl->mBackgroundColor = color;
581 map[ Toolkit::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
582 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
584 SetBackground( map );
587 Vector4 Control::GetBackgroundColor() const
589 return mImpl->mBackgroundColor;
592 void Control::SetBackground( const Property::Map& map )
594 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
597 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
598 visual.SetDepthIndex( DepthIndex::BACKGROUND );
600 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
605 void Control::SetBackgroundImage( Image image )
607 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
610 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
611 visual.SetDepthIndex( DepthIndex::BACKGROUND );
615 void Control::ClearBackground()
617 UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
618 mImpl->mBackgroundColor = Color::TRANSPARENT;
620 // Trigger a size negotiation request that may be needed when unregistering a visual.
624 void Control::EnableGestureDetection(Gesture::Type type)
626 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
628 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
629 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
630 mImpl->mPinchGestureDetector.Attach(Self());
633 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
635 mImpl->mPanGestureDetector = PanGestureDetector::New();
636 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
637 mImpl->mPanGestureDetector.Attach(Self());
640 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
642 mImpl->mTapGestureDetector = TapGestureDetector::New();
643 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
644 mImpl->mTapGestureDetector.Attach(Self());
647 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
649 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
650 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
651 mImpl->mLongPressGestureDetector.Attach(Self());
655 void Control::DisableGestureDetection(Gesture::Type type)
657 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
659 mImpl->mPinchGestureDetector.Detach(Self());
660 mImpl->mPinchGestureDetector.Reset();
663 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
665 mImpl->mPanGestureDetector.Detach(Self());
666 mImpl->mPanGestureDetector.Reset();
669 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
671 mImpl->mTapGestureDetector.Detach(Self());
672 mImpl->mTapGestureDetector.Reset();
675 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
677 mImpl->mLongPressGestureDetector.Detach(Self());
678 mImpl->mLongPressGestureDetector.Reset();
682 PinchGestureDetector Control::GetPinchGestureDetector() const
684 return mImpl->mPinchGestureDetector;
687 PanGestureDetector Control::GetPanGestureDetector() const
689 return mImpl->mPanGestureDetector;
692 TapGestureDetector Control::GetTapGestureDetector() const
694 return mImpl->mTapGestureDetector;
697 LongPressGestureDetector Control::GetLongPressGestureDetector() const
699 return mImpl->mLongPressGestureDetector;
702 void Control::SetKeyboardNavigationSupport(bool isSupported)
704 mImpl->mIsKeyboardNavigationSupported = isSupported;
707 bool Control::IsKeyboardNavigationSupported()
709 return mImpl->mIsKeyboardNavigationSupported;
712 void Control::SetKeyInputFocus()
714 if( Self().OnStage() )
716 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
720 bool Control::HasKeyInputFocus()
723 if( Self().OnStage() )
725 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
730 void Control::ClearKeyInputFocus()
732 if( Self().OnStage() )
734 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
738 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
740 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
742 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
743 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
746 bool Control::IsKeyboardFocusGroup()
748 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
751 void Control::AccessibilityActivate()
753 // Inform deriving classes
754 OnAccessibilityActivated();
757 void Control::KeyboardEnter()
759 // Inform deriving classes
763 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
765 RegisterVisual( index, visual, true );
768 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
770 bool visualReplaced ( false );
773 if( !mImpl->mVisuals.Empty() )
775 RegisteredVisualContainer::Iterator iter;
776 // Check if visual (index) is already registered. Replace if so.
777 if ( FindVisual( index, mImpl->mVisuals, iter ) )
779 if( (*iter)->visual && self.OnStage() )
781 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
783 (*iter)->visual = visual;
784 visualReplaced = true;
788 if( !visualReplaced ) // New registration entry
790 mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
793 if( visual && self.OnStage() && enabled )
795 Toolkit::GetImplementation(visual).SetOnStage( self );
797 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
801 void Control::UnregisterVisual( Property::Index index )
803 RegisteredVisualContainer::Iterator iter;
804 if ( FindVisual( index, mImpl->mVisuals, iter ) )
806 Actor self( Self() );
807 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
808 (*iter)->visual.Reset();
809 mImpl->mVisuals.Erase( iter );
813 Toolkit::Visual::Base Control::GetVisual( Property::Index index ) const
815 RegisteredVisualContainer::Iterator iter;
816 if ( FindVisual( index, mImpl->mVisuals, iter ) )
818 return (*iter)->visual;
821 return Toolkit::Visual::Base();
824 void Control::EnableVisual( Property::Index index, bool enable )
826 RegisteredVisualContainer::Iterator iter;
827 if ( FindVisual( index, mImpl->mVisuals, iter ) )
829 if ( (*iter)->enabled == enable )
831 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual Already enabled set (%s) \n", enable?"enabled":"disabled");
835 (*iter)->enabled = enable;
836 Actor parentActor = Self();
837 if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
841 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) on stage \n", index );
842 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
846 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) off stage \n", index );
847 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
853 bool Control::IsVisualEnabled( Property::Index index ) const
855 RegisteredVisualContainer::Iterator iter;
856 if ( FindVisual( index, mImpl->mVisuals, iter ) )
858 return (*iter)->enabled;
863 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
865 Dali::Animation transition;
866 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
868 if( transitionData.Count() > 0 )
870 // Setup a Transition from TransitionData.
871 TransitionData::Iterator end = transitionData.End();
872 for( TransitionData::Iterator iter = transitionData.Begin() ;
873 iter != end; ++iter )
875 TransitionData::Animator* animator = (*iter);
876 HandleIndex handleIndex;
878 // Attempt to find the object name as a child actor
879 Actor child = Self().FindChildByName( animator->objectName );
882 Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
883 handleIndex = HandleIndex( child, propertyIndex );
887 handleIndex = GetVisualProperty( *this, mImpl->mVisuals,
888 animator->objectName,
889 animator->propertyKey );
892 if( handleIndex.handle && handleIndex.index != Property::INVALID_INDEX )
894 if( animator->animate == false )
896 if( animator->targetValue.GetType() != Property::NONE )
898 handleIndex.handle.SetProperty( handleIndex.index, animator->targetValue );
903 if( animator->initialValue.GetType() != Property::NONE )
905 handleIndex.handle.SetProperty( handleIndex.index, animator->initialValue );
910 // Create an animation with a default .1 second duration - the animators
911 // will automatically force it to the 'right' duration.
912 transition = Dali::Animation::New( 0.1f );
915 transition.AnimateTo( Property( handleIndex.handle, handleIndex.index ),
916 animator->targetValue,
917 animator->alphaFunction,
918 TimePeriod( animator->timePeriodDelay,
919 animator->timePeriodDuration ) );
928 bool Control::OnAccessibilityActivated()
930 return false; // Accessibility activation is not handled by default
933 bool Control::OnKeyboardEnter()
935 return false; // Keyboard enter is not handled by default
938 bool Control::OnAccessibilityPan(PanGesture gesture)
940 return false; // Accessibility pan gesture is not handled by default
943 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
945 return false; // Accessibility touch event is not handled by default
948 bool Control::OnAccessibilityValueChange(bool isIncrease)
950 return false; // Accessibility value change action is not handled by default
953 bool Control::OnAccessibilityZoom()
955 return false; // Accessibility zoom action is not handled by default
958 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
963 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
967 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
969 return mImpl->mKeyEventSignal;
972 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
974 return mImpl->mKeyInputFocusGainedSignal;
977 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
979 return mImpl->mKeyInputFocusLostSignal;
982 bool Control::EmitKeyEventSignal( const KeyEvent& event )
984 // Guard against destruction during signal emission
985 Dali::Toolkit::Control handle( GetOwner() );
987 bool consumed = false;
989 // signals are allocated dynamically when someone connects
990 if ( !mImpl->mKeyEventSignal.Empty() )
992 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
997 // Notification for derived classes
998 consumed = OnKeyEvent(event);
1004 Control::Control( ControlBehaviour behaviourFlags )
1005 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
1006 mImpl(new Impl(*this))
1008 mImpl->mFlags = behaviourFlags;
1016 void Control::Initialize()
1018 // Call deriving classes so initialised before styling is applied to them.
1021 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
1022 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
1024 Toolkit::StyleManager styleManager = StyleManager::Get();
1026 // if stylemanager is available
1029 StyleManager& styleManagerImpl = GetImpl( styleManager );
1031 // Register for style changes
1032 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
1034 // Apply the current style
1035 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
1039 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
1041 SetKeyboardNavigationSupport( true );
1045 void Control::OnInitialize()
1049 void Control::OnControlChildAdd( Actor& child )
1051 DALI_LOG_WARNING_NOFN("DEPRECATION WARNING: OnControlChildAdd() is deprecated and will be removed from next release. Override OnChildAdd instead.\n" );
1054 void Control::OnControlChildRemove( Actor& child )
1056 DALI_LOG_WARNING_NOFN("DEPRECATION WARNING: OnControlChildRemove() is deprecated and will be removed from next release. Override OnChildRemove instead.\n" );
1059 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
1061 // By default the control is only interested in theme (not font) changes
1062 if( styleManager && change == StyleChange::THEME_CHANGE )
1064 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1069 void Control::OnPinch(const PinchGesture& pinch)
1071 if( !( mImpl->mStartingPinchScale ) )
1074 mImpl->mStartingPinchScale = new Vector3;
1077 if( pinch.state == Gesture::Started )
1079 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1082 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1085 void Control::OnPan( const PanGesture& pan )
1089 void Control::OnTap(const TapGesture& tap)
1093 void Control::OnLongPress( const LongPressGesture& longPress )
1097 void Control::EmitKeyInputFocusSignal( bool focusGained )
1099 Dali::Toolkit::Control handle( GetOwner() );
1103 // signals are allocated dynamically when someone connects
1104 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
1106 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
1111 // signals are allocated dynamically when someone connects
1112 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
1114 mImpl->mKeyInputFocusLostSignal.Emit( handle );
1119 void Control::OnStageConnection( int depth )
1121 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
1123 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1125 // Check whether the visual is empty and enabled
1126 if( (*iter)->visual && (*iter)->enabled )
1128 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
1129 Actor self( Self() );
1130 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
1135 void Control::OnStageDisconnection()
1137 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1139 // Check whether the visual is empty
1140 if( (*iter)->visual )
1142 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
1143 Actor self( Self() );
1144 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
1149 void Control::OnKeyInputFocusGained()
1151 EmitKeyInputFocusSignal( true );
1154 void Control::OnKeyInputFocusLost()
1156 EmitKeyInputFocusSignal( false );
1159 void Control::OnChildAdd(Actor& child)
1161 // Notify derived classes.
1162 OnControlChildAdd( child );
1165 void Control::OnChildRemove(Actor& child)
1167 // Notify derived classes.
1168 OnControlChildRemove( child );
1171 void Control::OnSizeSet(const Vector3& targetSize)
1173 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1176 Vector2 size( targetSize );
1177 Property::Map transformMap;
1178 SetDefaultTransform( transformMap );
1179 visual.SetTransformAndSize( transformMap, size );
1183 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1185 // @todo size negotiate background to new size, animate as well?
1188 bool Control::OnTouchEvent(const TouchEvent& event)
1190 return false; // Do not consume
1193 bool Control::OnHoverEvent(const HoverEvent& event)
1195 return false; // Do not consume
1198 bool Control::OnKeyEvent(const KeyEvent& event)
1200 return false; // Do not consume
1203 bool Control::OnWheelEvent(const WheelEvent& event)
1205 return false; // Do not consume
1208 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1210 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1212 container.Add( Self().GetChildAt( i ), size );
1215 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1218 Vector2 controlSize( size );
1219 Property::Map transformMap;
1220 SetDefaultTransform( transformMap );
1221 visual.SetTransformAndSize( transformMap, controlSize );
1225 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1229 Vector3 Control::GetNaturalSize()
1231 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1234 Vector2 naturalSize;
1235 visual.GetNaturalSize( naturalSize );
1236 return Vector3( naturalSize );
1238 return Vector3::ZERO;
1241 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1243 return CalculateChildSizeBase( child, dimension );
1246 float Control::GetHeightForWidth( float width )
1248 return GetHeightForWidthBase( width );
1251 float Control::GetWidthForHeight( float height )
1253 return GetWidthForHeightBase( height );
1256 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1258 return RelayoutDependentOnChildrenBase( dimension );
1261 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1265 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1269 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1271 mImpl->SignalConnected( slotObserver, callback );
1274 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1276 mImpl->SignalDisconnected( slotObserver, callback );
1279 Control& GetImplementation( Dali::Toolkit::Control& handle )
1281 CustomActorImpl& customInterface = handle.GetImplementation();
1282 // downcast to control
1283 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1287 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1289 const CustomActorImpl& customInterface = handle.GetImplementation();
1290 // downcast to control
1291 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1295 } // namespace Internal
1297 } // namespace Toolkit