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/devel-api/align-enums.h>
52 #include <dali-toolkit/internal/controls/tooltip/tooltip.h>
63 #if defined(DEBUG_ENABLED)
64 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
67 DALI_ENUM_TO_STRING_TABLE_BEGIN( CLIPPING_MODE )
68 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, DISABLED )
69 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, CLIP_CHILDREN )
70 DALI_ENUM_TO_STRING_TABLE_END( CLIPPING_MODE )
73 * Struct used to store Visual within the control, index is a unique key for each visual.
75 struct RegisteredVisual
77 Property::Index index;
78 Toolkit::Visual::Base visual;
81 RegisteredVisual( Property::Index aIndex, Toolkit::Visual::Base &aVisual, bool aEnabled) :
82 index(aIndex), visual(aVisual), enabled(aEnabled) {}
87 Handle handle; ///< a handle to the target object
88 Property::Index index; ///< The index of a property provided by the referenced object
92 index( Property::INVALID_INDEX )
96 HandleIndex( Handle& handle, Property::Index index )
104 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
107 * Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
109 bool FindVisual( Property::Index targetIndex, RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
111 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
113 if ( (*iter)->index == targetIndex )
121 HandleIndex GetVisualProperty(
122 Internal::Control& controlImpl,
123 RegisteredVisualContainer& visuals,
124 const std::string& visualName,
125 Property::Key propertyKey )
127 #if defined(DEBUG_ENABLED)
128 std::ostringstream oss;
129 oss << "Control::GetVisualProperty(" << visualName << ", " << propertyKey << ")" << std::endl;
130 DALI_LOG_INFO( gLogFilter, Debug::General, oss.str().c_str() );
133 // Find visualName in the control
134 RegisteredVisualContainer::Iterator iter;
135 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
137 Toolkit::Visual::Base visual = (*iter)->visual;
138 if( visual && visual.GetName() == visualName )
140 Internal::Visual::Base& visualImpl = GetImplementation(visual);
141 Renderer renderer = visualImpl.GetRenderer();
144 Property::Index index = DevelHandle::GetPropertyIndex( renderer, propertyKey );
145 if( index != Property::INVALID_INDEX )
147 return HandleIndex( renderer, index );
153 std::ostringstream noRenderers;
154 noRenderers << propertyKey;
155 DALI_LOG_WARNING( "Control::GetVisualProperty(%s, %s) No renderers\n", visualName.c_str(), noRenderers.str().c_str() );
157 return HandleIndex( handle, Property::INVALID_INDEX );
160 void SetDefaultTransform( Property::Map& propertyMap )
164 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET, Vector2(0.0f, 0.0f) )
165 .Add( Toolkit::DevelVisual::Transform::Property::SIZE, Vector2(1.0f, 1.0f) )
166 .Add( Toolkit::DevelVisual::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
167 .Add( Toolkit::DevelVisual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER )
168 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET_SIZE_MODE, Vector4::ZERO );
172 * Creates control through type registry
176 return Internal::Control::New();
180 * Performs actions as requested using the action name.
181 * @param[in] object The object on which to perform the action.
182 * @param[in] actionName The action to perform.
183 * @param[in] attributes The attributes with which to perfrom this action.
184 * @return true if action has been accepted by this control
186 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
187 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
191 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
193 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
196 // if cast succeeds there is an implementation so no need to check
197 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
205 * Connects a callback function with the object's signals.
206 * @param[in] object The object providing the signal.
207 * @param[in] tracker Used to disconnect the signal.
208 * @param[in] signalName The signal to connect to.
209 * @param[in] functor A newly allocated FunctorDelegate.
210 * @return True if the signal was connected.
211 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
213 const char* SIGNAL_KEY_EVENT = "keyEvent";
214 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
215 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
216 const char* SIGNAL_TAPPED = "tapped";
217 const char* SIGNAL_PANNED = "panned";
218 const char* SIGNAL_PINCHED = "pinched";
219 const char* SIGNAL_LONG_PRESSED = "longPressed";
220 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
222 Dali::BaseHandle handle( object );
224 bool connected( false );
225 Toolkit::Control control = Toolkit::Control::DownCast( handle );
228 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
231 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
233 controlImpl.KeyEventSignal().Connect( tracker, functor );
235 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
237 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
239 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
241 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
243 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
245 controlImpl.EnableGestureDetection( Gesture::Tap );
246 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
248 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
250 controlImpl.EnableGestureDetection( Gesture::Pan );
251 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
253 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
255 controlImpl.EnableGestureDetection( Gesture::Pinch );
256 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
258 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
260 controlImpl.EnableGestureDetection( Gesture::LongPress );
261 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
267 // Setup signals and actions using the type-registry.
268 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
270 // Note: Properties are registered separately below.
272 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
273 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
274 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
275 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
276 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
277 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
278 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
280 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
282 DALI_TYPE_REGISTRATION_END()
284 } // unnamed namespace
289 class Control::Impl : public ConnectionTracker
293 // Construction & Destruction
294 Impl(Control& controlImpl)
295 : mControlImpl( controlImpl ),
297 mBackgroundColor(Color::TRANSPARENT),
298 mStartingPinchScale( NULL ),
300 mPinchGestureDetector(),
301 mPanGestureDetector(),
302 mTapGestureDetector(),
303 mLongPressGestureDetector(),
304 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
305 mIsKeyboardNavigationSupported( false ),
306 mIsKeyboardFocusGroup( false )
312 // All gesture detectors will be destroyed so no need to disconnect.
313 delete mStartingPinchScale;
316 // Gesture Detection Methods
318 void PinchDetected(Actor actor, const PinchGesture& pinch)
320 mControlImpl.OnPinch(pinch);
323 void PanDetected(Actor actor, const PanGesture& pan)
325 mControlImpl.OnPan(pan);
328 void TapDetected(Actor actor, const TapGesture& tap)
330 mControlImpl.OnTap(tap);
333 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
335 mControlImpl.OnLongPress(longPress);
341 * Called when a property of an object of this type is set.
342 * @param[in] object The object whose property is set.
343 * @param[in] index The property index.
344 * @param[in] value The new property value.
346 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
348 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
352 Control& controlImpl( GetImplementation( control ) );
356 case Toolkit::Control::Property::STYLE_NAME:
358 controlImpl.SetStyleName( value.Get< std::string >() );
362 case Toolkit::Control::Property::BACKGROUND_COLOR:
364 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
365 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
369 case Toolkit::Control::Property::BACKGROUND_IMAGE:
371 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
372 Image image = Scripting::NewImage( value );
375 controlImpl.SetBackgroundImage( image );
379 // An empty image means the background is no longer required
380 controlImpl.ClearBackground();
385 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
387 if ( value.Get< bool >() )
389 controlImpl.SetKeyInputFocus();
393 controlImpl.ClearKeyInputFocus();
398 case Toolkit::Control::Property::BACKGROUND:
401 const Property::Map* map = value.GetMap();
402 if( map && !map->Empty() )
404 controlImpl.SetBackground( *map );
406 else if( value.Get( url ) )
408 // don't know the size to load
409 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
412 controlImpl.RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
413 visual.SetDepthIndex( DepthIndex::BACKGROUND );
418 // The background is an empty property map, so we should clear the background
419 controlImpl.ClearBackground();
424 case Toolkit::DevelControl::Property::TOOLTIP:
426 TooltipPtr& tooltipPtr = controlImpl.mImpl->mTooltip;
429 tooltipPtr = Tooltip::New( control );
431 tooltipPtr->SetProperties( value );
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 );
500 case Toolkit::DevelControl::Property::TOOLTIP:
503 if( controlImpl.mImpl->mTooltip )
505 controlImpl.mImpl->mTooltip->CreatePropertyMap( map );
519 Control& mControlImpl;
520 RegisteredVisualContainer mVisuals; // Stores visuals needed by the control, non trivial type so std::vector used.
521 std::string mStyleName;
522 Vector4 mBackgroundColor; ///< The color of the background visual
523 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
524 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
525 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
526 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
529 PinchGestureDetector mPinchGestureDetector;
530 PanGestureDetector mPanGestureDetector;
531 TapGestureDetector mTapGestureDetector;
532 LongPressGestureDetector mLongPressGestureDetector;
537 ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
538 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
539 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
541 // Properties - these need to be members of Internal::Control::Impl as they access private methods/data of Internal::Control and Internal::Control::Impl.
542 static const PropertyRegistration PROPERTY_1;
543 static const PropertyRegistration PROPERTY_2;
544 static const PropertyRegistration PROPERTY_3;
545 static const PropertyRegistration PROPERTY_4;
546 static const PropertyRegistration PROPERTY_5;
547 static const PropertyRegistration PROPERTY_6;
550 // Properties registered without macro to use specific member variables.
551 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
552 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
553 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
554 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
555 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
556 const PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "tooltip", Toolkit::DevelControl::Property::TOOLTIP, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
558 Toolkit::Control Control::New()
560 // Create the implementation, temporarily owned on stack
561 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
563 // Pass ownership to handle
564 Toolkit::Control handle( *controlImpl );
566 // Second-phase init of the implementation
567 // This can only be done after the CustomActor connection has been made...
568 controlImpl->Initialize();
573 void Control::SetStyleName( const std::string& styleName )
575 if( styleName != mImpl->mStyleName )
577 mImpl->mStyleName = styleName;
579 // Apply new style, if stylemanager is available
580 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
583 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
588 const std::string& Control::GetStyleName() const
590 return mImpl->mStyleName;
593 void Control::SetBackgroundColor( const Vector4& color )
595 mImpl->mBackgroundColor = color;
597 map[ Toolkit::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
598 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
600 SetBackground( map );
603 Vector4 Control::GetBackgroundColor() const
605 return mImpl->mBackgroundColor;
608 void Control::SetBackground( const Property::Map& map )
610 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
613 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
614 visual.SetDepthIndex( DepthIndex::BACKGROUND );
616 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
621 void Control::SetBackgroundImage( Image image )
623 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
626 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
627 visual.SetDepthIndex( DepthIndex::BACKGROUND );
631 void Control::ClearBackground()
633 UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
634 mImpl->mBackgroundColor = Color::TRANSPARENT;
636 // Trigger a size negotiation request that may be needed when unregistering a visual.
640 void Control::EnableGestureDetection(Gesture::Type type)
642 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
644 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
645 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
646 mImpl->mPinchGestureDetector.Attach(Self());
649 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
651 mImpl->mPanGestureDetector = PanGestureDetector::New();
652 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
653 mImpl->mPanGestureDetector.Attach(Self());
656 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
658 mImpl->mTapGestureDetector = TapGestureDetector::New();
659 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
660 mImpl->mTapGestureDetector.Attach(Self());
663 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
665 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
666 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
667 mImpl->mLongPressGestureDetector.Attach(Self());
671 void Control::DisableGestureDetection(Gesture::Type type)
673 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
675 mImpl->mPinchGestureDetector.Detach(Self());
676 mImpl->mPinchGestureDetector.Reset();
679 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
681 mImpl->mPanGestureDetector.Detach(Self());
682 mImpl->mPanGestureDetector.Reset();
685 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
687 mImpl->mTapGestureDetector.Detach(Self());
688 mImpl->mTapGestureDetector.Reset();
691 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
693 mImpl->mLongPressGestureDetector.Detach(Self());
694 mImpl->mLongPressGestureDetector.Reset();
698 PinchGestureDetector Control::GetPinchGestureDetector() const
700 return mImpl->mPinchGestureDetector;
703 PanGestureDetector Control::GetPanGestureDetector() const
705 return mImpl->mPanGestureDetector;
708 TapGestureDetector Control::GetTapGestureDetector() const
710 return mImpl->mTapGestureDetector;
713 LongPressGestureDetector Control::GetLongPressGestureDetector() const
715 return mImpl->mLongPressGestureDetector;
718 void Control::SetKeyboardNavigationSupport(bool isSupported)
720 mImpl->mIsKeyboardNavigationSupported = isSupported;
723 bool Control::IsKeyboardNavigationSupported()
725 return mImpl->mIsKeyboardNavigationSupported;
728 void Control::SetKeyInputFocus()
730 if( Self().OnStage() )
732 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
736 bool Control::HasKeyInputFocus()
739 if( Self().OnStage() )
741 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
746 void Control::ClearKeyInputFocus()
748 if( Self().OnStage() )
750 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
754 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
756 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
758 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
759 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
762 bool Control::IsKeyboardFocusGroup()
764 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
767 void Control::AccessibilityActivate()
769 // Inform deriving classes
770 OnAccessibilityActivated();
773 void Control::KeyboardEnter()
775 // Inform deriving classes
779 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
781 RegisterVisual( index, visual, true );
784 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
786 bool visualReplaced ( false );
789 if( !mImpl->mVisuals.Empty() )
791 RegisteredVisualContainer::Iterator iter;
792 // Check if visual (index) is already registered. Replace if so.
793 if ( FindVisual( index, mImpl->mVisuals, iter ) )
795 if( (*iter)->visual && self.OnStage() )
797 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
799 (*iter)->visual = visual;
800 visualReplaced = true;
804 // If not set, set the name of the visual to the same name as the control's property.
805 // ( If the control has been type registered )
806 if( visual.GetName().empty() )
808 // Check if the control has been type registered:
809 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(*this) );
812 // Check if the property index has been registered:
813 Property::IndexContainer indices;
814 typeInfo.GetPropertyIndices( indices );
815 Property::IndexContainer::Iterator iter = std::find( indices.Begin(), indices.End(), index );
816 if( iter != indices.End() )
818 // If it has, then get it's name and use that for the visual
819 std::string visualName = typeInfo.GetPropertyName( index );
820 visual.SetName( visualName );
825 if( !visualReplaced ) // New registration entry
827 mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
830 if( visual && self.OnStage() && enabled )
832 Toolkit::GetImplementation(visual).SetOnStage( self );
834 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
838 void Control::UnregisterVisual( Property::Index index )
840 RegisteredVisualContainer::Iterator iter;
841 if ( FindVisual( index, mImpl->mVisuals, iter ) )
843 Actor self( Self() );
844 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
845 (*iter)->visual.Reset();
846 mImpl->mVisuals.Erase( iter );
850 Toolkit::Visual::Base Control::GetVisual( Property::Index index ) const
852 RegisteredVisualContainer::Iterator iter;
853 if ( FindVisual( index, mImpl->mVisuals, iter ) )
855 return (*iter)->visual;
858 return Toolkit::Visual::Base();
861 void Control::EnableVisual( Property::Index index, bool enable )
863 RegisteredVisualContainer::Iterator iter;
864 if ( FindVisual( index, mImpl->mVisuals, iter ) )
866 if ( (*iter)->enabled == enable )
868 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual Already enabled set (%s) \n", enable?"enabled":"disabled");
872 (*iter)->enabled = enable;
873 Actor parentActor = Self();
874 if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
878 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) on stage \n", index );
879 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
883 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) off stage \n", index );
884 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
890 bool Control::IsVisualEnabled( Property::Index index ) const
892 RegisteredVisualContainer::Iterator iter;
893 if ( FindVisual( index, mImpl->mVisuals, iter ) )
895 return (*iter)->enabled;
900 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
902 Dali::Animation transition;
903 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
905 if( transitionData.Count() > 0 )
907 // Setup a Transition from TransitionData.
908 TransitionData::Iterator end = transitionData.End();
909 for( TransitionData::Iterator iter = transitionData.Begin() ;
910 iter != end; ++iter )
912 TransitionData::Animator* animator = (*iter);
913 HandleIndex handleIndex;
915 // Attempt to find the object name as a child actor
916 Actor child = Self().FindChildByName( animator->objectName );
919 Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
920 handleIndex = HandleIndex( child, propertyIndex );
924 handleIndex = GetVisualProperty( *this, mImpl->mVisuals,
925 animator->objectName,
926 animator->propertyKey );
929 if( handleIndex.handle && handleIndex.index != Property::INVALID_INDEX )
931 if( animator->animate == false )
933 if( animator->targetValue.GetType() != Property::NONE )
935 handleIndex.handle.SetProperty( handleIndex.index, animator->targetValue );
940 if( animator->initialValue.GetType() != Property::NONE )
942 handleIndex.handle.SetProperty( handleIndex.index, animator->initialValue );
947 // Create an animation with a default .1 second duration - the animators
948 // will automatically force it to the 'right' duration.
949 transition = Dali::Animation::New( 0.1f );
952 transition.AnimateTo( Property( handleIndex.handle, handleIndex.index ),
953 animator->targetValue,
954 animator->alphaFunction,
955 TimePeriod( animator->timePeriodDelay,
956 animator->timePeriodDuration ) );
965 bool Control::OnAccessibilityActivated()
967 return false; // Accessibility activation is not handled by default
970 bool Control::OnKeyboardEnter()
972 return false; // Keyboard enter is not handled by default
975 bool Control::OnAccessibilityPan(PanGesture gesture)
977 return false; // Accessibility pan gesture is not handled by default
980 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
982 return false; // Accessibility touch event is not handled by default
985 bool Control::OnAccessibilityValueChange(bool isIncrease)
987 return false; // Accessibility value change action is not handled by default
990 bool Control::OnAccessibilityZoom()
992 return false; // Accessibility zoom action is not handled by default
995 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1000 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1004 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
1006 return mImpl->mKeyEventSignal;
1009 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
1011 return mImpl->mKeyInputFocusGainedSignal;
1014 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
1016 return mImpl->mKeyInputFocusLostSignal;
1019 bool Control::EmitKeyEventSignal( const KeyEvent& event )
1021 // Guard against destruction during signal emission
1022 Dali::Toolkit::Control handle( GetOwner() );
1024 bool consumed = false;
1026 // signals are allocated dynamically when someone connects
1027 if ( !mImpl->mKeyEventSignal.Empty() )
1029 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
1034 // Notification for derived classes
1035 consumed = OnKeyEvent(event);
1041 Control::Control( ControlBehaviour behaviourFlags )
1042 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
1043 mImpl(new Impl(*this))
1045 mImpl->mFlags = behaviourFlags;
1053 void Control::Initialize()
1055 // Call deriving classes so initialised before styling is applied to them.
1058 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
1059 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
1061 Toolkit::StyleManager styleManager = StyleManager::Get();
1063 // if stylemanager is available
1066 StyleManager& styleManagerImpl = GetImpl( styleManager );
1068 // Register for style changes
1069 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
1071 // Apply the current style
1072 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
1076 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
1078 SetKeyboardNavigationSupport( true );
1082 void Control::OnInitialize()
1086 void Control::OnControlChildAdd( Actor& child )
1090 void Control::OnControlChildRemove( Actor& child )
1094 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
1096 // By default the control is only interested in theme (not font) changes
1097 if( styleManager && change == StyleChange::THEME_CHANGE )
1099 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1104 void Control::OnPinch(const PinchGesture& pinch)
1106 if( !( mImpl->mStartingPinchScale ) )
1109 mImpl->mStartingPinchScale = new Vector3;
1112 if( pinch.state == Gesture::Started )
1114 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1117 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1120 void Control::OnPan( const PanGesture& pan )
1124 void Control::OnTap(const TapGesture& tap)
1128 void Control::OnLongPress( const LongPressGesture& longPress )
1132 void Control::EmitKeyInputFocusSignal( bool focusGained )
1134 Dali::Toolkit::Control handle( GetOwner() );
1138 // signals are allocated dynamically when someone connects
1139 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
1141 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
1146 // signals are allocated dynamically when someone connects
1147 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
1149 mImpl->mKeyInputFocusLostSignal.Emit( handle );
1154 void Control::OnStageConnection( int depth )
1156 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
1158 Actor self( Self() );
1160 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1162 // Check whether the visual is empty and enabled
1163 if( (*iter)->visual && (*iter)->enabled )
1165 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
1166 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
1170 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() )
1172 Property::Value clippingValue = self.GetProperty( Actor::Property::CLIPPING_MODE );
1173 int clippingMode = ClippingMode::DISABLED;
1174 if( clippingValue.Get( clippingMode ) )
1176 // Add a transparent background if we do not have any renderers or visuals so we clip our children
1178 if( clippingMode == ClippingMode::CLIP_CHILDREN )
1180 // Create a transparent background visual which will also get staged.
1181 SetBackgroundColor( Color::TRANSPARENT );
1187 void Control::OnStageDisconnection()
1189 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1191 // Check whether the visual is empty
1192 if( (*iter)->visual )
1194 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
1195 Actor self( Self() );
1196 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
1201 void Control::OnKeyInputFocusGained()
1203 EmitKeyInputFocusSignal( true );
1206 void Control::OnKeyInputFocusLost()
1208 EmitKeyInputFocusSignal( false );
1211 void Control::OnChildAdd(Actor& child)
1213 // Notify derived classes.
1214 OnControlChildAdd( child );
1217 void Control::OnChildRemove(Actor& child)
1219 // Notify derived classes.
1220 OnControlChildRemove( child );
1223 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
1225 Actor self( Self() );
1226 if( index == Actor::Property::CLIPPING_MODE )
1228 // Only set the background if we're already on the stage and have no renderers or visuals
1230 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() && self.OnStage() )
1232 ClippingMode::Type clippingMode = ClippingMode::DISABLED;
1233 if( Scripting::GetEnumerationProperty< ClippingMode::Type >( propertyValue, CLIPPING_MODE_TABLE, CLIPPING_MODE_TABLE_COUNT, clippingMode ) )
1235 // Add a transparent background if we do not have one so we clip children
1237 if( clippingMode == ClippingMode::CLIP_CHILDREN )
1239 SetBackgroundColor( Color::TRANSPARENT );
1246 void Control::OnSizeSet(const Vector3& targetSize)
1248 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1251 Vector2 size( targetSize );
1252 Property::Map transformMap;
1253 SetDefaultTransform( transformMap );
1254 visual.SetTransformAndSize( transformMap, size );
1258 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1260 // @todo size negotiate background to new size, animate as well?
1263 bool Control::OnTouchEvent(const TouchEvent& event)
1265 return false; // Do not consume
1268 bool Control::OnHoverEvent(const HoverEvent& event)
1270 return false; // Do not consume
1273 bool Control::OnKeyEvent(const KeyEvent& event)
1275 return false; // Do not consume
1278 bool Control::OnWheelEvent(const WheelEvent& event)
1280 return false; // Do not consume
1283 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1285 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1287 container.Add( Self().GetChildAt( i ), size );
1290 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1293 Vector2 controlSize( size );
1294 Property::Map transformMap;
1295 SetDefaultTransform( transformMap );
1296 visual.SetTransformAndSize( transformMap, controlSize );
1300 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1304 Vector3 Control::GetNaturalSize()
1306 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1309 Vector2 naturalSize;
1310 visual.GetNaturalSize( naturalSize );
1311 return Vector3( naturalSize );
1313 return Vector3::ZERO;
1316 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1318 return CalculateChildSizeBase( child, dimension );
1321 float Control::GetHeightForWidth( float width )
1323 return GetHeightForWidthBase( width );
1326 float Control::GetWidthForHeight( float height )
1328 return GetWidthForHeightBase( height );
1331 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1333 return RelayoutDependentOnChildrenBase( dimension );
1336 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1340 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1344 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1346 mImpl->SignalConnected( slotObserver, callback );
1349 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1351 mImpl->SignalDisconnected( slotObserver, callback );
1354 Control& GetImplementation( Dali::Toolkit::Control& handle )
1356 CustomActorImpl& customInterface = handle.GetImplementation();
1357 // downcast to control
1358 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1362 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1364 const CustomActorImpl& customInterface = handle.GetImplementation();
1365 // downcast to control
1366 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1370 } // namespace Internal
1372 } // namespace Toolkit