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/visual-factory/devel-visual-properties.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>
57 #if defined(DEBUG_ENABLED)
58 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::General, false, "LOG_CONTROL_VISUALS");
62 * Struct used to store Visual within the control, index is a unique key for each visual.
64 struct RegisteredVisual
66 Property::Index index;
67 Toolkit::Visual::Base visual;
70 RegisteredVisual( Property::Index aIndex, Toolkit::Visual::Base &aVisual, bool aEnabled) :
71 index(aIndex), visual(aVisual), enabled(aEnabled) {}
76 Handle handle; ///< a handle to the target object
77 Property::Index index; ///< The index of a property provided by the referenced object
81 index( Property::INVALID_INDEX )
85 HandleIndex( Handle& handle, Property::Index index )
93 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
96 * Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
98 bool FindVisual( Property::Index targetIndex, RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
100 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
102 if ( (*iter)->index == targetIndex )
110 HandleIndex GetVisualProperty(
111 Internal::Control& controlImpl,
112 RegisteredVisualContainer& visuals,
113 const std::string& visualName,
114 Property::Key propertyKey )
116 #if defined(DEBUG_ENABLED)
117 std::ostringstream oss;
118 oss << "Control::GetHandleIndex(" << visualName << ", " << propertyKey << ")" << std::endl;
119 DALI_LOG_INFO( gLogFilter, Debug::General, oss.str().c_str() );
122 // Find visualName in the control
123 RegisteredVisualContainer::Iterator iter;
124 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
126 if ( (*iter)->visual.GetName() == visualName )
132 // Does it's renderer have an associated property?
133 if( iter != visuals.End() )
135 Actor self = controlImpl.Self();
136 Property::Index index = self.GetPropertyIndex( propertyKey );
137 if( index != Property::INVALID_INDEX )
139 // It's an actor property:
140 return HandleIndex( self, index );
144 // Check if it is a renderer property:
145 if( self.GetRendererCount() > 0 )
147 // @todo Need to use correct renderer index
148 Renderer renderer = self.GetRendererAt(0);
149 Property::Index index = renderer.GetPropertyIndex( propertyKey );
150 if( index != Property::INVALID_INDEX )
152 // It's a renderer property:
153 return HandleIndex( renderer, index );
158 std::ostringstream oss;
160 DALI_LOG_WARNING( "Control::GetHandleIndex(%s, %s) No renderers\n", visualName.c_str(), oss.str().c_str() );
165 return HandleIndex( handle, Property::INVALID_INDEX );
170 * Creates control through type registry
174 return Internal::Control::New();
178 * Performs actions as requested using the action name.
179 * @param[in] object The object on which to perform the action.
180 * @param[in] actionName The action to perform.
181 * @param[in] attributes The attributes with which to perfrom this action.
182 * @return true if action has been accepted by this control
184 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
185 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
189 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
191 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
194 // if cast succeeds there is an implementation so no need to check
195 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
203 * Connects a callback function with the object's signals.
204 * @param[in] object The object providing the signal.
205 * @param[in] tracker Used to disconnect the signal.
206 * @param[in] signalName The signal to connect to.
207 * @param[in] functor A newly allocated FunctorDelegate.
208 * @return True if the signal was connected.
209 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
211 const char* SIGNAL_KEY_EVENT = "keyEvent";
212 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
213 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
214 const char* SIGNAL_TAPPED = "tapped";
215 const char* SIGNAL_PANNED = "panned";
216 const char* SIGNAL_PINCHED = "pinched";
217 const char* SIGNAL_LONG_PRESSED = "longPressed";
218 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
220 Dali::BaseHandle handle( object );
222 bool connected( false );
223 Toolkit::Control control = Toolkit::Control::DownCast( handle );
226 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
229 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
231 controlImpl.KeyEventSignal().Connect( tracker, functor );
233 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
235 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
237 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
239 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
241 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
243 controlImpl.EnableGestureDetection( Gesture::Tap );
244 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
246 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
248 controlImpl.EnableGestureDetection( Gesture::Pan );
249 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
251 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
253 controlImpl.EnableGestureDetection( Gesture::Pinch );
254 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
256 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
258 controlImpl.EnableGestureDetection( Gesture::LongPress );
259 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
265 // Setup signals and actions using the type-registry.
266 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
268 // Note: Properties are registered separately below.
270 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
271 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
272 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
273 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
274 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
275 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
276 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
278 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
280 DALI_TYPE_REGISTRATION_END()
282 } // unnamed namespace
287 class Control::Impl : public ConnectionTracker
291 // Construction & Destruction
292 Impl(Control& controlImpl)
293 : mControlImpl( controlImpl ),
296 mBackgroundColor(Color::TRANSPARENT),
297 mStartingPinchScale( NULL ),
299 mPinchGestureDetector(),
300 mPanGestureDetector(),
301 mTapGestureDetector(),
302 mLongPressGestureDetector(),
303 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
304 mIsKeyboardNavigationSupported( false ),
305 mIsKeyboardFocusGroup( false )
311 // All gesture detectors will be destroyed so no need to disconnect.
312 delete mStartingPinchScale;
315 // Gesture Detection Methods
317 void PinchDetected(Actor actor, const PinchGesture& pinch)
319 mControlImpl.OnPinch(pinch);
322 void PanDetected(Actor actor, const PanGesture& pan)
324 mControlImpl.OnPan(pan);
327 void TapDetected(Actor actor, const TapGesture& tap)
329 mControlImpl.OnTap(tap);
332 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
334 mControlImpl.OnLongPress(longPress);
340 * Called when a property of an object of this type is set.
341 * @param[in] object The object whose property is set.
342 * @param[in] index The property index.
343 * @param[in] value The new property value.
345 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
347 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
351 Control& controlImpl( GetImplementation( control ) );
355 case Toolkit::Control::Property::STYLE_NAME:
357 controlImpl.SetStyleName( value.Get< std::string >() );
361 case Toolkit::Control::Property::BACKGROUND_COLOR:
363 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
364 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
368 case Toolkit::Control::Property::BACKGROUND_IMAGE:
370 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
371 Image image = Scripting::NewImage( value );
374 controlImpl.SetBackgroundImage( image );
378 // An empty map means the background is no longer required
379 controlImpl.ClearBackground();
384 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
386 if ( value.Get< bool >() )
388 controlImpl.SetKeyInputFocus();
392 controlImpl.ClearKeyInputFocus();
397 case Toolkit::Control::Property::BACKGROUND:
399 const Property::Map* map = value.GetMap();
402 controlImpl.SetBackground( *map );
406 // The background is not a property map, so we should clear the background
407 controlImpl.ClearBackground();
416 * Called to retrieve a property of an object of this type.
417 * @param[in] object The object whose property is to be retrieved.
418 * @param[in] index The property index.
419 * @return The current value of the property.
421 static Property::Value GetProperty( BaseObject* object, Property::Index index )
423 Property::Value value;
425 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
429 Control& controlImpl( GetImplementation( control ) );
433 case Toolkit::Control::Property::STYLE_NAME:
435 value = controlImpl.GetStyleName();
439 case Toolkit::Control::Property::BACKGROUND_COLOR:
441 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
442 value = controlImpl.GetBackgroundColor();
446 case Toolkit::Control::Property::BACKGROUND_IMAGE:
448 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
450 if( controlImpl.mImpl->mBackgroundVisual )
452 controlImpl.mImpl->mBackgroundVisual.CreatePropertyMap( map );
458 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
460 value = controlImpl.HasKeyInputFocus();
464 case Toolkit::Control::Property::BACKGROUND:
467 if( controlImpl.mImpl->mBackgroundVisual )
469 (controlImpl.mImpl->mBackgroundVisual).CreatePropertyMap( map );
484 Control& mControlImpl;
485 RegisteredVisualContainer mVisuals; // Stores visuals needed by the control, non trivial type so std::vector used.
486 std::string mStyleName;
487 Toolkit::Visual::Base mBackgroundVisual; ///< The visual to render the background
488 Vector4 mBackgroundColor; ///< The color of the background visual
489 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
490 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
491 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
492 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
495 PinchGestureDetector mPinchGestureDetector;
496 PanGestureDetector mPanGestureDetector;
497 TapGestureDetector mTapGestureDetector;
498 LongPressGestureDetector mLongPressGestureDetector;
500 ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
501 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
502 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
504 // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
505 static const PropertyRegistration PROPERTY_1;
506 static const PropertyRegistration PROPERTY_2;
507 static const PropertyRegistration PROPERTY_3;
508 static const PropertyRegistration PROPERTY_4;
509 static const PropertyRegistration PROPERTY_5;
512 // Properties registered without macro to use specific member variables.
513 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
514 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
515 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
516 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
517 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
519 Toolkit::Control Control::New()
521 // Create the implementation, temporarily owned on stack
522 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
524 // Pass ownership to handle
525 Toolkit::Control handle( *controlImpl );
527 // Second-phase init of the implementation
528 // This can only be done after the CustomActor connection has been made...
529 controlImpl->Initialize();
539 void Control::SetStyleName( const std::string& styleName )
541 if( styleName != mImpl->mStyleName )
543 mImpl->mStyleName = styleName;
545 // Apply new style, if stylemanager is available
546 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
549 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
554 const std::string& Control::GetStyleName() const
556 return mImpl->mStyleName;
559 void Control::SetBackgroundColor( const Vector4& color )
561 mImpl->mBackgroundColor = color;
563 map[ Toolkit::VisualProperty::TYPE ] = Toolkit::Visual::COLOR;
564 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
565 mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( map );
566 RegisterVisual( Toolkit::Control::Property::BACKGROUND, mImpl->mBackgroundVisual );
567 if( mImpl->mBackgroundVisual )
569 mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
573 Vector4 Control::GetBackgroundColor() const
575 return mImpl->mBackgroundColor;
578 void Control::SetBackground( const Property::Map& map )
580 mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( map );
581 RegisterVisual( Toolkit::Control::Property::BACKGROUND, mImpl->mBackgroundVisual );
582 if( mImpl->mBackgroundVisual )
584 mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
588 void Control::SetBackgroundImage( Image image )
590 mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
591 RegisterVisual( Toolkit::Control::Property::BACKGROUND, mImpl->mBackgroundVisual );
592 if( mImpl->mBackgroundVisual )
594 mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
598 void Control::ClearBackground()
600 if( mImpl->mBackgroundVisual )
602 Actor self( Self() );
603 Toolkit::GetImplementation( mImpl->mBackgroundVisual ).SetOffStage( self );
604 mImpl->mBackgroundVisual.Reset();
606 mImpl->mBackgroundColor = Color::TRANSPARENT;
609 void Control::EnableGestureDetection(Gesture::Type type)
611 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
613 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
614 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
615 mImpl->mPinchGestureDetector.Attach(Self());
618 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
620 mImpl->mPanGestureDetector = PanGestureDetector::New();
621 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
622 mImpl->mPanGestureDetector.Attach(Self());
625 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
627 mImpl->mTapGestureDetector = TapGestureDetector::New();
628 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
629 mImpl->mTapGestureDetector.Attach(Self());
632 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
634 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
635 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
636 mImpl->mLongPressGestureDetector.Attach(Self());
640 void Control::DisableGestureDetection(Gesture::Type type)
642 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
644 mImpl->mPinchGestureDetector.Detach(Self());
645 mImpl->mPinchGestureDetector.Reset();
648 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
650 mImpl->mPanGestureDetector.Detach(Self());
651 mImpl->mPanGestureDetector.Reset();
654 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
656 mImpl->mTapGestureDetector.Detach(Self());
657 mImpl->mTapGestureDetector.Reset();
660 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
662 mImpl->mLongPressGestureDetector.Detach(Self());
663 mImpl->mLongPressGestureDetector.Reset();
667 PinchGestureDetector Control::GetPinchGestureDetector() const
669 return mImpl->mPinchGestureDetector;
672 PanGestureDetector Control::GetPanGestureDetector() const
674 return mImpl->mPanGestureDetector;
677 TapGestureDetector Control::GetTapGestureDetector() const
679 return mImpl->mTapGestureDetector;
682 LongPressGestureDetector Control::GetLongPressGestureDetector() const
684 return mImpl->mLongPressGestureDetector;
687 void Control::SetKeyboardNavigationSupport(bool isSupported)
689 mImpl->mIsKeyboardNavigationSupported = isSupported;
692 bool Control::IsKeyboardNavigationSupported()
694 return mImpl->mIsKeyboardNavigationSupported;
697 void Control::SetKeyInputFocus()
699 if( Self().OnStage() )
701 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
705 bool Control::HasKeyInputFocus()
708 if( Self().OnStage() )
710 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
715 void Control::ClearKeyInputFocus()
717 if( Self().OnStage() )
719 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
723 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
725 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
727 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
728 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
731 bool Control::IsKeyboardFocusGroup()
733 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
736 void Control::AccessibilityActivate()
738 // Inform deriving classes
739 OnAccessibilityActivated();
742 void Control::KeyboardEnter()
744 // Inform deriving classes
748 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
750 RegisterVisual( index, visual, true );
753 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
755 bool visualReplaced ( false );
758 if ( !mImpl->mVisuals.Empty() )
760 RegisteredVisualContainer::Iterator iter;
761 // Check if visual (index) is already registered. Replace if so.
762 if ( FindVisual( index, mImpl->mVisuals, iter ) )
764 if( (*iter)->visual && self.OnStage() )
766 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
768 (*iter)->visual = visual;
769 visualReplaced = true;
773 if ( !visualReplaced ) // New registration entry
775 mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
778 if( visual && self.OnStage() && enabled )
780 Toolkit::GetImplementation(visual).SetOnStage( self );
784 void Control::UnregisterVisual( Property::Index index )
786 RegisteredVisualContainer::Iterator iter;
787 if ( FindVisual( index, mImpl->mVisuals, iter ) )
789 mImpl->mVisuals.Erase( iter );
793 Toolkit::Visual::Base Control::GetVisual( Property::Index index ) const
795 RegisteredVisualContainer::Iterator iter;
796 if ( FindVisual( index, mImpl->mVisuals, iter ) )
798 return (*iter)->visual;
801 return Toolkit::Visual::Base();
804 void Control::EnableVisual( Property::Index index, bool enable )
806 RegisteredVisualContainer::Iterator iter;
807 if ( FindVisual( index, mImpl->mVisuals, iter ) )
809 if ( (*iter)->enabled == enable )
814 (*iter)->enabled = enable;
815 Actor parentActor = Self();
816 if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
821 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
825 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
831 bool Control::IsVisualEnabled( Property::Index index ) const
833 RegisteredVisualContainer::Iterator iter;
834 if ( FindVisual( index, mImpl->mVisuals, iter ) )
836 return (*iter)->enabled;
841 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
843 Dali::Animation transition;
844 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
846 if( transitionData.Count() > 0 )
848 // Setup a Transition from TransitionData.
849 TransitionData::Iterator end = transitionData.End();
850 for( TransitionData::Iterator iter = transitionData.Begin() ;
851 iter != end; ++iter )
853 TransitionData::Animator* animator = (*iter);
854 HandleIndex handleIndex;
856 // Attempt to find the object name as a child actor
857 Actor child = Self().FindChildByName( animator->objectName );
860 Property::Index propertyIndex = child.GetPropertyIndex( animator->propertyKey );
861 handleIndex = HandleIndex( child, propertyIndex );
865 handleIndex = GetVisualProperty( *this, mImpl->mVisuals,
866 animator->objectName,
867 animator->propertyKey );
870 if( handleIndex.handle && handleIndex.index != Property::INVALID_INDEX )
872 if( animator->animate == false )
874 if( animator->targetValue.GetType() != Property::NONE )
876 handleIndex.handle.SetProperty( handleIndex.index, animator->targetValue );
881 if( animator->initialValue.GetType() != Property::NONE )
883 handleIndex.handle.SetProperty( handleIndex.index, animator->initialValue );
888 // Create an animation with a default .1 second duration - the animators
889 // will automatically force it to the 'right' duration.
890 transition = Dali::Animation::New( 0.1f );
893 transition.AnimateTo( Property( handleIndex.handle, handleIndex.index ),
894 animator->targetValue,
895 animator->alphaFunction,
896 TimePeriod( animator->timePeriodDelay,
897 animator->timePeriodDuration ) );
906 bool Control::OnAccessibilityActivated()
908 return false; // Accessibility activation is not handled by default
911 bool Control::OnKeyboardEnter()
913 return false; // Keyboard enter is not handled by default
916 bool Control::OnAccessibilityPan(PanGesture gesture)
918 return false; // Accessibility pan gesture is not handled by default
921 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
923 return false; // Accessibility touch event is not handled by default
926 bool Control::OnAccessibilityValueChange(bool isIncrease)
928 return false; // Accessibility value change action is not handled by default
931 bool Control::OnAccessibilityZoom()
933 return false; // Accessibility zoom action is not handled by default
936 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
941 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
945 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
947 return mImpl->mKeyEventSignal;
950 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
952 return mImpl->mKeyInputFocusGainedSignal;
955 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
957 return mImpl->mKeyInputFocusLostSignal;
960 bool Control::EmitKeyEventSignal( const KeyEvent& event )
962 // Guard against destruction during signal emission
963 Dali::Toolkit::Control handle( GetOwner() );
965 bool consumed = false;
967 // signals are allocated dynamically when someone connects
968 if ( !mImpl->mKeyEventSignal.Empty() )
970 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
975 // Notification for derived classes
976 consumed = OnKeyEvent(event);
982 Control::Control( ControlBehaviour behaviourFlags )
983 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
984 mImpl(new Impl(*this))
986 mImpl->mFlags = behaviourFlags;
989 void Control::Initialize()
991 // Call deriving classes so initialised before styling is applied to them.
994 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
995 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
997 Toolkit::StyleManager styleManager = StyleManager::Get();
999 // if stylemanager is available
1002 StyleManager& styleManagerImpl = GetImpl( styleManager );
1004 // Register for style changes
1005 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
1007 // Apply the current style
1008 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
1012 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
1014 SetKeyboardNavigationSupport( true );
1018 void Control::OnInitialize()
1022 void Control::OnControlChildAdd( Actor& child )
1026 void Control::OnControlChildRemove( Actor& child )
1030 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
1032 // By default the control is only interested in theme (not font) changes
1033 if( styleManager && change == StyleChange::THEME_CHANGE )
1035 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1040 void Control::OnPinch(const PinchGesture& pinch)
1042 if( !( mImpl->mStartingPinchScale ) )
1045 mImpl->mStartingPinchScale = new Vector3;
1048 if( pinch.state == Gesture::Started )
1050 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1053 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1056 void Control::OnPan( const PanGesture& pan )
1060 void Control::OnTap(const TapGesture& tap)
1064 void Control::OnLongPress( const LongPressGesture& longPress )
1068 void Control::EmitKeyInputFocusSignal( bool focusGained )
1070 Dali::Toolkit::Control handle( GetOwner() );
1074 // signals are allocated dynamically when someone connects
1075 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
1077 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
1082 // signals are allocated dynamically when someone connects
1083 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
1085 mImpl->mKeyInputFocusLostSignal.Emit( handle );
1090 void Control::OnStageConnection( int depth )
1092 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1094 // Check whether the visual is empty and enabled
1095 if( (*iter)->visual && (*iter)->enabled )
1097 Actor self( Self() );
1098 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
1103 void Control::OnStageDisconnection()
1105 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1107 // Check whether the visual is empty
1108 if( (*iter)->visual )
1110 Actor self( Self() );
1111 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
1116 void Control::OnKeyInputFocusGained()
1118 EmitKeyInputFocusSignal( true );
1121 void Control::OnKeyInputFocusLost()
1123 EmitKeyInputFocusSignal( false );
1126 void Control::OnChildAdd(Actor& child)
1128 // Notify derived classes.
1129 OnControlChildAdd( child );
1132 void Control::OnChildRemove(Actor& child)
1134 // Notify derived classes.
1135 OnControlChildRemove( child );
1138 void Control::OnSizeSet(const Vector3& targetSize)
1140 if( mImpl->mBackgroundVisual )
1142 Vector2 size( targetSize );
1143 mImpl->mBackgroundVisual.SetSize( size );
1147 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1149 // @todo size negotiate background to new size, animate as well?
1152 bool Control::OnTouchEvent(const TouchEvent& event)
1154 return false; // Do not consume
1157 bool Control::OnHoverEvent(const HoverEvent& event)
1159 return false; // Do not consume
1162 bool Control::OnKeyEvent(const KeyEvent& event)
1164 return false; // Do not consume
1167 bool Control::OnWheelEvent(const WheelEvent& event)
1169 return false; // Do not consume
1172 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1174 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1176 container.Add( Self().GetChildAt( i ), size );
1180 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1184 Vector3 Control::GetNaturalSize()
1186 if( mImpl->mBackgroundVisual )
1188 Vector2 naturalSize;
1189 mImpl->mBackgroundVisual.GetNaturalSize(naturalSize);
1190 return Vector3(naturalSize);
1192 return Vector3::ZERO;
1195 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1197 return CalculateChildSizeBase( child, dimension );
1200 float Control::GetHeightForWidth( float width )
1202 return GetHeightForWidthBase( width );
1205 float Control::GetWidthForHeight( float height )
1207 return GetWidthForHeightBase( height );
1210 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1212 return RelayoutDependentOnChildrenBase( dimension );
1215 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1219 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1223 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1225 mImpl->SignalConnected( slotObserver, callback );
1228 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1230 mImpl->SignalDisconnected( slotObserver, callback );
1233 Control& GetImplementation( Dali::Toolkit::Control& handle )
1235 CustomActorImpl& customInterface = handle.GetImplementation();
1236 // downcast to control
1237 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1241 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1243 const CustomActorImpl& customInterface = handle.GetImplementation();
1244 // downcast to control
1245 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1249 } // namespace Internal
1251 } // namespace Toolkit