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>
57 extern bool CaseInsensitiveStringCompare( const std::string& a, const std::string& b );
65 extern const Dali::Scripting::StringEnum ControlStateTable[];
66 extern const unsigned int ControlStateTableCount;
69 // Not static or anonymous - shared with other translation units
70 const Scripting::StringEnum ControlStateTable[] = {
71 { "NORMAL", Toolkit::DevelControl::NORMAL },
72 { "FOCUSED", Toolkit::DevelControl::FOCUSED },
73 { "DISABLED", Toolkit::DevelControl::DISABLED },
74 }; const unsigned int ControlStateTableCount = sizeof( ControlStateTable ) / sizeof( ControlStateTable[0] );
76 } // Internal namespace
81 #if defined(DEBUG_ENABLED)
82 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
85 DALI_ENUM_TO_STRING_TABLE_BEGIN( CLIPPING_MODE )
86 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, DISABLED )
87 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, CLIP_CHILDREN )
88 DALI_ENUM_TO_STRING_TABLE_END( CLIPPING_MODE )
91 * Struct used to store Visual within the control, index is a unique key for each visual.
93 struct RegisteredVisual
95 Property::Index index;
96 Toolkit::Visual::Base visual;
99 RegisteredVisual( Property::Index aIndex, Toolkit::Visual::Base &aVisual, bool aEnabled)
100 : index(aIndex), visual(aVisual), enabled(aEnabled)
105 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
108 * Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
110 bool FindVisual( Property::Index targetIndex, RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
112 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
114 if ( (*iter)->index == targetIndex )
122 Toolkit::Visual::Base GetVisualByName(
123 RegisteredVisualContainer& visuals,
124 const std::string& visualName )
126 Toolkit::Visual::Base visualHandle;
128 RegisteredVisualContainer::Iterator iter;
129 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
131 Toolkit::Visual::Base visual = (*iter)->visual;
132 if( visual && visual.GetName() == visualName )
134 visualHandle = visual;
142 * Creates control through type registry
146 return Internal::Control::New();
150 * Performs actions as requested using the action name.
151 * @param[in] object The object on which to perform the action.
152 * @param[in] actionName The action to perform.
153 * @param[in] attributes The attributes with which to perfrom this action.
154 * @return true if action has been accepted by this control
156 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
157 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
161 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
163 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
166 // if cast succeeds there is an implementation so no need to check
167 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
175 * Connects a callback function with the object's signals.
176 * @param[in] object The object providing the signal.
177 * @param[in] tracker Used to disconnect the signal.
178 * @param[in] signalName The signal to connect to.
179 * @param[in] functor A newly allocated FunctorDelegate.
180 * @return True if the signal was connected.
181 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
183 const char* SIGNAL_KEY_EVENT = "keyEvent";
184 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
185 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
186 const char* SIGNAL_TAPPED = "tapped";
187 const char* SIGNAL_PANNED = "panned";
188 const char* SIGNAL_PINCHED = "pinched";
189 const char* SIGNAL_LONG_PRESSED = "longPressed";
190 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
192 Dali::BaseHandle handle( object );
194 bool connected( false );
195 Toolkit::Control control = Toolkit::Control::DownCast( handle );
198 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
201 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
203 controlImpl.KeyEventSignal().Connect( tracker, functor );
205 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
207 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
209 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
211 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
213 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
215 controlImpl.EnableGestureDetection( Gesture::Tap );
216 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
218 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
220 controlImpl.EnableGestureDetection( Gesture::Pan );
221 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
223 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
225 controlImpl.EnableGestureDetection( Gesture::Pinch );
226 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
228 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
230 controlImpl.EnableGestureDetection( Gesture::LongPress );
231 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
237 // Setup signals and actions using the type-registry.
238 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
240 // Note: Properties are registered separately below.
242 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
243 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
244 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
245 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
246 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
247 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
248 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
250 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
252 DALI_TYPE_REGISTRATION_END()
254 } // unnamed namespace
259 class Control::Impl : public ConnectionTracker
263 // Construction & Destruction
264 Impl(Control& controlImpl)
265 : mControlImpl( controlImpl ),
266 mState( Toolkit::DevelControl::NORMAL ),
269 mBackgroundColor(Color::TRANSPARENT),
270 mStartingPinchScale( NULL ),
272 mPinchGestureDetector(),
273 mPanGestureDetector(),
274 mTapGestureDetector(),
275 mLongPressGestureDetector(),
276 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
277 mIsKeyboardNavigationSupported( false ),
278 mIsKeyboardFocusGroup( false )
284 // All gesture detectors will be destroyed so no need to disconnect.
285 delete mStartingPinchScale;
288 // Gesture Detection Methods
290 void PinchDetected(Actor actor, const PinchGesture& pinch)
292 mControlImpl.OnPinch(pinch);
295 void PanDetected(Actor actor, const PanGesture& pan)
297 mControlImpl.OnPan(pan);
300 void TapDetected(Actor actor, const TapGesture& tap)
302 mControlImpl.OnTap(tap);
305 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
307 mControlImpl.OnLongPress(longPress);
313 * Called when a property of an object of this type is set.
314 * @param[in] object The object whose property is set.
315 * @param[in] index The property index.
316 * @param[in] value The new property value.
318 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
320 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
324 Control& controlImpl( GetImplementation( control ) );
328 case Toolkit::Control::Property::STYLE_NAME:
330 controlImpl.SetStyleName( value.Get< std::string >() );
334 case Toolkit::DevelControl::Property::STATE:
336 bool withTransitions=true;
337 const Property::Value* valuePtr=&value;
338 Property::Map* map = value.GetMap();
341 Property::Value* value2 = map->Find("withTransitions");
344 withTransitions = value2->Get<bool>();
347 valuePtr = map->Find("state");
352 Toolkit::DevelControl::State state( controlImpl.mImpl->mState );
353 if( Scripting::GetEnumerationProperty< Toolkit::DevelControl::State >( *valuePtr, ControlStateTable, ControlStateTableCount, state ) )
355 controlImpl.mImpl->SetState( state, withTransitions );
361 case Toolkit::DevelControl::Property::SUB_STATE:
363 std::string subState;
364 if( value.Get( subState ) )
366 controlImpl.mImpl->SetSubState( subState );
371 case Toolkit::Control::Property::BACKGROUND_COLOR:
373 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
374 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
378 case Toolkit::Control::Property::BACKGROUND_IMAGE:
380 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
381 Image image = Scripting::NewImage( value );
384 controlImpl.SetBackgroundImage( image );
388 // An empty image means the background is no longer required
389 controlImpl.ClearBackground();
394 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
396 if ( value.Get< bool >() )
398 controlImpl.SetKeyInputFocus();
402 controlImpl.ClearKeyInputFocus();
407 case Toolkit::Control::Property::BACKGROUND:
410 const Property::Map* map = value.GetMap();
411 if( map && !map->Empty() )
413 controlImpl.SetBackground( *map );
415 else if( value.Get( url ) )
417 // don't know the size to load
418 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
421 controlImpl.RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
422 visual.SetDepthIndex( DepthIndex::BACKGROUND );
427 // The background is an empty property map, so we should clear the background
428 controlImpl.ClearBackground();
433 case Toolkit::DevelControl::Property::TOOLTIP:
435 TooltipPtr& tooltipPtr = controlImpl.mImpl->mTooltip;
438 tooltipPtr = Tooltip::New( control );
440 tooltipPtr->SetProperties( value );
447 * Called to retrieve a property of an object of this type.
448 * @param[in] object The object whose property is to be retrieved.
449 * @param[in] index The property index.
450 * @return The current value of the property.
452 static Property::Value GetProperty( BaseObject* object, Property::Index index )
454 Property::Value value;
456 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
460 Control& controlImpl( GetImplementation( control ) );
464 case Toolkit::Control::Property::STYLE_NAME:
466 value = controlImpl.GetStyleName();
470 case Toolkit::DevelControl::Property::STATE:
472 value = controlImpl.mImpl->mState;
476 case Toolkit::DevelControl::Property::SUB_STATE:
478 value = controlImpl.mImpl->mSubStateName;
482 case Toolkit::Control::Property::BACKGROUND_COLOR:
484 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
485 value = controlImpl.GetBackgroundColor();
489 case Toolkit::Control::Property::BACKGROUND_IMAGE:
491 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
493 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
496 visual.CreatePropertyMap( map );
502 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
504 value = controlImpl.HasKeyInputFocus();
508 case Toolkit::Control::Property::BACKGROUND:
511 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
514 visual.CreatePropertyMap( map );
521 case Toolkit::DevelControl::Property::TOOLTIP:
524 if( controlImpl.mImpl->mTooltip )
526 controlImpl.mImpl->mTooltip->CreatePropertyMap( map );
538 void SetState( DevelControl::State state, bool withTransitions=true )
540 if( mState != state )
542 // If mState was Disabled, and new state is Focused, should probably
543 // store that fact, e.g. in another property that FocusManager can access.
546 // Trigger state change and transitions
547 // Apply new style, if stylemanager is available
548 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
551 const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
554 for( int i=mVisuals.Count()-1; i >= 0; i-- )
556 mControlImpl.UnregisterVisual( mVisuals[i]->index );
559 Dali::CustomActor handle( mControlImpl.GetOwner() );
560 stylePtr->ApplyVisualsAndPropertiesRecursively( handle );
566 void SetSubState( const std::string& subStateName, bool withTransitions=true )
568 if( mSubStateName != subStateName )
570 // Get existing sub-state visuals, and unregister them
571 Dali::CustomActor handle( mControlImpl.GetOwner() );
573 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
576 const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
580 std::string stateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( mState, ControlStateTable, ControlStateTableCount );
582 const StylePtr* state = stylePtr->subStates.Find( stateName );
585 StylePtr stateStyle(*state);
587 // Unregister existing visuals of this substate
588 const StylePtr* subState = stateStyle->subStates.Find( mSubStateName );
591 StylePtr subStateStyle(*subState);
593 for( Dictionary<Property::Map>::iterator iter = subStateStyle->visuals.Begin(); iter != subStateStyle->visuals.End(); ++iter )
595 const std::string& visualName = (*iter).key;
596 Dali::Property::Index index = handle.GetPropertyIndex( visualName );
597 if( index != Property::INVALID_INDEX )
599 mControlImpl.UnregisterVisual( index );
604 // Register visuals of the new substate
605 const StylePtr* newSubState = stateStyle->subStates.Find( subStateName );
608 StylePtr newSubStateStyle(*newSubState);
609 newSubStateStyle->ApplyVisuals( handle );
610 newSubStateStyle->ApplyProperties( handle );
616 mSubStateName = subStateName;
622 Control& mControlImpl;
623 DevelControl::State mState;
624 std::string mSubStateName;
626 RegisteredVisualContainer mVisuals; // Stores visuals needed by the control, non trivial type so std::vector used.
627 std::string mStyleName;
628 Vector4 mBackgroundColor; ///< The color of the background visual
629 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
630 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
631 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
632 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
635 PinchGestureDetector mPinchGestureDetector;
636 PanGestureDetector mPanGestureDetector;
637 TapGestureDetector mTapGestureDetector;
638 LongPressGestureDetector mLongPressGestureDetector;
643 ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
644 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
645 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
647 // Properties - these need to be members of Internal::Control::Impl as they access private methods/data of Internal::Control and Internal::Control::Impl.
648 static const PropertyRegistration PROPERTY_1;
649 static const PropertyRegistration PROPERTY_2;
650 static const PropertyRegistration PROPERTY_3;
651 static const PropertyRegistration PROPERTY_4;
652 static const PropertyRegistration PROPERTY_5;
653 static const PropertyRegistration PROPERTY_6;
654 static const PropertyRegistration PROPERTY_7;
655 static const PropertyRegistration PROPERTY_8;
658 // Properties registered without macro to use specific member variables.
659 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
660 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
661 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
662 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
663 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
664 const PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "tooltip", Toolkit::DevelControl::Property::TOOLTIP, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
665 const PropertyRegistration Control::Impl::PROPERTY_7( typeRegistration, "state", Toolkit::DevelControl::Property::STATE, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
666 const PropertyRegistration Control::Impl::PROPERTY_8( typeRegistration, "subState", Toolkit::DevelControl::Property::SUB_STATE, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
668 Toolkit::Control Control::New()
670 // Create the implementation, temporarily owned on stack
671 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
673 // Pass ownership to handle
674 Toolkit::Control handle( *controlImpl );
676 // Second-phase init of the implementation
677 // This can only be done after the CustomActor connection has been made...
678 controlImpl->Initialize();
683 void Control::SetStyleName( const std::string& styleName )
685 if( styleName != mImpl->mStyleName )
687 mImpl->mStyleName = styleName;
689 // Apply new style, if stylemanager is available
690 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
693 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
698 const std::string& Control::GetStyleName() const
700 return mImpl->mStyleName;
703 void Control::SetBackgroundColor( const Vector4& color )
705 mImpl->mBackgroundColor = color;
707 map[ Toolkit::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
708 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
710 SetBackground( map );
713 Vector4 Control::GetBackgroundColor() const
715 return mImpl->mBackgroundColor;
718 void Control::SetBackground( const Property::Map& map )
720 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
723 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
724 visual.SetDepthIndex( DepthIndex::BACKGROUND );
726 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
731 void Control::SetBackgroundImage( Image image )
733 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
736 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
737 visual.SetDepthIndex( DepthIndex::BACKGROUND );
741 void Control::ClearBackground()
743 UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
744 mImpl->mBackgroundColor = Color::TRANSPARENT;
746 // Trigger a size negotiation request that may be needed when unregistering a visual.
750 void Control::EnableGestureDetection(Gesture::Type type)
752 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
754 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
755 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
756 mImpl->mPinchGestureDetector.Attach(Self());
759 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
761 mImpl->mPanGestureDetector = PanGestureDetector::New();
762 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
763 mImpl->mPanGestureDetector.Attach(Self());
766 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
768 mImpl->mTapGestureDetector = TapGestureDetector::New();
769 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
770 mImpl->mTapGestureDetector.Attach(Self());
773 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
775 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
776 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
777 mImpl->mLongPressGestureDetector.Attach(Self());
781 void Control::DisableGestureDetection(Gesture::Type type)
783 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
785 mImpl->mPinchGestureDetector.Detach(Self());
786 mImpl->mPinchGestureDetector.Reset();
789 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
791 mImpl->mPanGestureDetector.Detach(Self());
792 mImpl->mPanGestureDetector.Reset();
795 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
797 mImpl->mTapGestureDetector.Detach(Self());
798 mImpl->mTapGestureDetector.Reset();
801 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
803 mImpl->mLongPressGestureDetector.Detach(Self());
804 mImpl->mLongPressGestureDetector.Reset();
808 PinchGestureDetector Control::GetPinchGestureDetector() const
810 return mImpl->mPinchGestureDetector;
813 PanGestureDetector Control::GetPanGestureDetector() const
815 return mImpl->mPanGestureDetector;
818 TapGestureDetector Control::GetTapGestureDetector() const
820 return mImpl->mTapGestureDetector;
823 LongPressGestureDetector Control::GetLongPressGestureDetector() const
825 return mImpl->mLongPressGestureDetector;
828 void Control::SetKeyboardNavigationSupport(bool isSupported)
830 mImpl->mIsKeyboardNavigationSupported = isSupported;
833 bool Control::IsKeyboardNavigationSupported()
835 return mImpl->mIsKeyboardNavigationSupported;
838 void Control::SetKeyInputFocus()
840 if( Self().OnStage() )
842 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
846 bool Control::HasKeyInputFocus()
849 if( Self().OnStage() )
851 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
856 void Control::ClearKeyInputFocus()
858 if( Self().OnStage() )
860 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
864 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
866 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
868 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
869 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
872 bool Control::IsKeyboardFocusGroup()
874 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
877 void Control::AccessibilityActivate()
879 // Inform deriving classes
880 OnAccessibilityActivated();
883 void Control::KeyboardEnter()
885 // Inform deriving classes
889 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
891 RegisterVisual( index, visual, true );
894 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
896 bool visualReplaced ( false );
899 if( !mImpl->mVisuals.Empty() )
901 RegisteredVisualContainer::Iterator iter;
902 // Check if visual (index) is already registered. Replace if so.
903 if ( FindVisual( index, mImpl->mVisuals, iter ) )
905 if( (*iter)->visual && self.OnStage() )
907 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
909 (*iter)->visual = visual;
910 visualReplaced = true;
914 // If not set, set the name of the visual to the same name as the control's property.
915 // ( If the control has been type registered )
916 if( visual.GetName().empty() )
918 // Check if the control has been type registered:
919 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(*this) );
922 // Check if the property index has been registered:
923 Property::IndexContainer indices;
924 typeInfo.GetPropertyIndices( indices );
925 Property::IndexContainer::Iterator iter = std::find( indices.Begin(), indices.End(), index );
926 if( iter != indices.End() )
928 // If it has, then get it's name and use that for the visual
929 std::string visualName = typeInfo.GetPropertyName( index );
930 visual.SetName( visualName );
935 if( !visualReplaced ) // New registration entry
937 mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
940 if( visual && self.OnStage() && enabled )
942 Toolkit::GetImplementation(visual).SetOnStage( self );
945 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n", visual.GetName().c_str(), index, enabled?"T":"F" );
948 void Control::UnregisterVisual( Property::Index index )
950 RegisteredVisualContainer::Iterator iter;
951 if ( FindVisual( index, mImpl->mVisuals, iter ) )
953 Actor self( Self() );
954 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
955 (*iter)->visual.Reset();
956 mImpl->mVisuals.Erase( iter );
960 Toolkit::Visual::Base Control::GetVisual( Property::Index index ) const
962 RegisteredVisualContainer::Iterator iter;
963 if ( FindVisual( index, mImpl->mVisuals, iter ) )
965 return (*iter)->visual;
968 return Toolkit::Visual::Base();
971 void Control::EnableVisual( Property::Index index, bool enable )
973 RegisteredVisualContainer::Iterator iter;
974 if ( FindVisual( index, mImpl->mVisuals, iter ) )
976 if ( (*iter)->enabled == enable )
978 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual %s(%d) already %s\n", (*iter)->visual.GetName().c_str(), index, enable?"enabled":"disabled");
982 (*iter)->enabled = enable;
983 Actor parentActor = Self();
984 if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
988 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) on stage \n", (*iter)->visual.GetName().c_str(), index );
989 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
993 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) off stage \n", (*iter)->visual.GetName().c_str(), index );
994 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
1000 bool Control::IsVisualEnabled( Property::Index index ) const
1002 RegisteredVisualContainer::Iterator iter;
1003 if ( FindVisual( index, mImpl->mVisuals, iter ) )
1005 return (*iter)->enabled;
1010 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
1012 Dali::Animation transition;
1013 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
1015 if( transitionData.Count() > 0 )
1017 // Setup a Transition from TransitionData.
1018 TransitionData::Iterator end = transitionData.End();
1019 for( TransitionData::Iterator iter = transitionData.Begin() ;
1020 iter != end; ++iter )
1022 TransitionData::Animator* animator = (*iter);
1024 Toolkit::Visual::Base visual = GetVisualByName( mImpl->mVisuals, animator->objectName );
1028 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
1029 visualImpl.AnimateProperty( transition, *animator );
1033 // Otherwise, try any actor children of control (Including the control)
1034 Actor child = Self().FindChildByName( animator->objectName );
1037 Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
1038 if( propertyIndex != Property::INVALID_INDEX )
1040 if( animator->animate == false )
1042 if( animator->targetValue.GetType() != Property::NONE )
1044 child.SetProperty( propertyIndex, animator->targetValue );
1047 else // animate the property
1049 if( animator->initialValue.GetType() != Property::NONE )
1051 child.SetProperty( propertyIndex, animator->initialValue );
1056 transition = Dali::Animation::New( 0.1f );
1059 transition.AnimateTo( Property( child, propertyIndex ),
1060 animator->targetValue,
1061 animator->alphaFunction,
1062 TimePeriod( animator->timePeriodDelay,
1063 animator->timePeriodDuration ) );
1074 bool Control::OnAccessibilityActivated()
1076 return false; // Accessibility activation is not handled by default
1079 bool Control::OnKeyboardEnter()
1081 return false; // Keyboard enter is not handled by default
1084 bool Control::OnAccessibilityPan(PanGesture gesture)
1086 return false; // Accessibility pan gesture is not handled by default
1089 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
1091 return false; // Accessibility touch event is not handled by default
1094 bool Control::OnAccessibilityValueChange(bool isIncrease)
1096 return false; // Accessibility value change action is not handled by default
1099 bool Control::OnAccessibilityZoom()
1101 return false; // Accessibility zoom action is not handled by default
1104 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1109 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1113 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
1115 return mImpl->mKeyEventSignal;
1118 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
1120 return mImpl->mKeyInputFocusGainedSignal;
1123 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
1125 return mImpl->mKeyInputFocusLostSignal;
1128 bool Control::EmitKeyEventSignal( const KeyEvent& event )
1130 // Guard against destruction during signal emission
1131 Dali::Toolkit::Control handle( GetOwner() );
1133 bool consumed = false;
1135 // signals are allocated dynamically when someone connects
1136 if ( !mImpl->mKeyEventSignal.Empty() )
1138 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
1143 // Notification for derived classes
1144 consumed = OnKeyEvent(event);
1150 Control::Control( ControlBehaviour behaviourFlags )
1151 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
1152 mImpl(new Impl(*this))
1154 mImpl->mFlags = behaviourFlags;
1162 void Control::Initialize()
1164 // Call deriving classes so initialised before styling is applied to them.
1167 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
1168 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
1170 Toolkit::StyleManager styleManager = StyleManager::Get();
1172 // if stylemanager is available
1175 StyleManager& styleManagerImpl = GetImpl( styleManager );
1177 // Register for style changes
1178 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
1180 // Apply the current style
1181 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
1185 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
1187 SetKeyboardNavigationSupport( true );
1191 void Control::OnInitialize()
1195 void Control::OnControlChildAdd( Actor& child )
1199 void Control::OnControlChildRemove( Actor& child )
1203 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
1205 // By default the control is only interested in theme (not font) changes
1206 if( styleManager && change == StyleChange::THEME_CHANGE )
1208 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1213 void Control::OnPinch(const PinchGesture& pinch)
1215 if( !( mImpl->mStartingPinchScale ) )
1218 mImpl->mStartingPinchScale = new Vector3;
1221 if( pinch.state == Gesture::Started )
1223 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1226 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1229 void Control::OnPan( const PanGesture& pan )
1233 void Control::OnTap(const TapGesture& tap)
1237 void Control::OnLongPress( const LongPressGesture& longPress )
1241 void Control::EmitKeyInputFocusSignal( bool focusGained )
1243 Dali::Toolkit::Control handle( GetOwner() );
1247 // signals are allocated dynamically when someone connects
1248 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
1250 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
1255 // signals are allocated dynamically when someone connects
1256 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
1258 mImpl->mKeyInputFocusLostSignal.Emit( handle );
1263 void Control::OnStageConnection( int depth )
1265 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
1267 Actor self( Self() );
1269 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1271 // Check whether the visual is empty and enabled
1272 if( (*iter)->visual && (*iter)->enabled )
1274 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
1275 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
1279 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() )
1281 Property::Value clippingValue = self.GetProperty( Actor::Property::CLIPPING_MODE );
1282 int clippingMode = ClippingMode::DISABLED;
1283 if( clippingValue.Get( clippingMode ) )
1285 // Add a transparent background if we do not have any renderers or visuals so we clip our children
1287 if( clippingMode == ClippingMode::CLIP_CHILDREN )
1289 // Create a transparent background visual which will also get staged.
1290 SetBackgroundColor( Color::TRANSPARENT );
1296 void Control::OnStageDisconnection()
1298 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1300 // Check whether the visual is empty
1301 if( (*iter)->visual )
1303 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
1304 Actor self( Self() );
1305 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
1310 void Control::OnKeyInputFocusGained()
1312 EmitKeyInputFocusSignal( true );
1315 void Control::OnKeyInputFocusLost()
1317 EmitKeyInputFocusSignal( false );
1320 void Control::OnChildAdd(Actor& child)
1322 // Notify derived classes.
1323 OnControlChildAdd( child );
1326 void Control::OnChildRemove(Actor& child)
1328 // Notify derived classes.
1329 OnControlChildRemove( child );
1332 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
1334 Actor self( Self() );
1335 if( index == Actor::Property::CLIPPING_MODE )
1337 // Only set the background if we're already on the stage and have no renderers or visuals
1339 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() && self.OnStage() )
1341 ClippingMode::Type clippingMode = ClippingMode::DISABLED;
1342 if( Scripting::GetEnumerationProperty< ClippingMode::Type >( propertyValue, CLIPPING_MODE_TABLE, CLIPPING_MODE_TABLE_COUNT, clippingMode ) )
1344 // Add a transparent background if we do not have one so we clip children
1346 if( clippingMode == ClippingMode::CLIP_CHILDREN )
1348 SetBackgroundColor( Color::TRANSPARENT );
1355 void Control::OnSizeSet(const Vector3& targetSize)
1357 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1360 Vector2 size( targetSize );
1361 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
1365 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1367 // @todo size negotiate background to new size, animate as well?
1370 bool Control::OnTouchEvent(const TouchEvent& event)
1372 return false; // Do not consume
1375 bool Control::OnHoverEvent(const HoverEvent& event)
1377 return false; // Do not consume
1380 bool Control::OnKeyEvent(const KeyEvent& event)
1382 return false; // Do not consume
1385 bool Control::OnWheelEvent(const WheelEvent& event)
1387 return false; // Do not consume
1390 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1392 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1394 container.Add( Self().GetChildAt( i ), size );
1397 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1400 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
1404 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1408 Vector3 Control::GetNaturalSize()
1410 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1413 Vector2 naturalSize;
1414 visual.GetNaturalSize( naturalSize );
1415 return Vector3( naturalSize );
1417 return Vector3::ZERO;
1420 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1422 return CalculateChildSizeBase( child, dimension );
1425 float Control::GetHeightForWidth( float width )
1427 return GetHeightForWidthBase( width );
1430 float Control::GetWidthForHeight( float height )
1432 return GetWidthForHeightBase( height );
1435 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1437 return RelayoutDependentOnChildrenBase( dimension );
1440 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1444 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1448 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1450 mImpl->SignalConnected( slotObserver, callback );
1453 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1455 mImpl->SignalDisconnected( slotObserver, callback );
1458 Control& GetImplementation( Dali::Toolkit::Control& handle )
1460 CustomActorImpl& customInterface = handle.GetImplementation();
1461 // downcast to control
1462 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1466 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1468 const CustomActorImpl& customInterface = handle.GetImplementation();
1469 // downcast to control
1470 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1474 } // namespace Internal
1476 } // namespace Toolkit