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
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/scripting.h>
35 #include <dali/integration-api/debug.h>
38 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
39 #include <dali-toolkit/public-api/controls/control.h>
40 #include <dali-toolkit/public-api/styling/style-manager.h>
41 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
42 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
43 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
44 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
45 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
46 #include <dali-toolkit/internal/styling/style-manager-impl.h>
47 #include <dali-toolkit/internal/visuals/color/color-visual.h>
48 #include <dali-toolkit/internal/visuals/transition-data-impl.h>
49 #include <dali-toolkit/devel-api/align-enums.h>
60 #if defined(DEBUG_ENABLED)
61 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
65 * Struct used to store Visual within the control, index is a unique key for each visual.
67 struct RegisteredVisual
69 Property::Index index;
70 Toolkit::Visual::Base visual;
73 RegisteredVisual( Property::Index aIndex, Toolkit::Visual::Base &aVisual, bool aEnabled) :
74 index(aIndex), visual(aVisual), enabled(aEnabled) {}
79 Handle handle; ///< a handle to the target object
80 Property::Index index; ///< The index of a property provided by the referenced object
84 index( Property::INVALID_INDEX )
88 HandleIndex( Handle& handle, Property::Index index )
96 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
99 * Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
101 bool FindVisual( Property::Index targetIndex, RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
103 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
105 if ( (*iter)->index == targetIndex )
113 HandleIndex GetVisualProperty(
114 Internal::Control& controlImpl,
115 RegisteredVisualContainer& visuals,
116 const std::string& visualName,
117 Property::Key propertyKey )
119 #if defined(DEBUG_ENABLED)
120 std::ostringstream oss;
121 oss << "Control::GetVisualProperty(" << visualName << ", " << propertyKey << ")" << std::endl;
122 DALI_LOG_INFO( gLogFilter, Debug::General, oss.str().c_str() );
125 // Find visualName in the control
126 RegisteredVisualContainer::Iterator iter;
127 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
129 Toolkit::Visual::Base visual = (*iter)->visual;
130 if( visual && visual.GetName() == visualName )
132 Internal::Visual::Base& visualImpl = GetImplementation(visual);
133 Renderer renderer = visualImpl.GetRenderer();
136 Property::Index index = DevelHandle::GetPropertyIndex( renderer, propertyKey );
137 if( index != Property::INVALID_INDEX )
139 return HandleIndex( renderer, index );
145 std::ostringstream noRenderers;
146 noRenderers << propertyKey;
147 DALI_LOG_WARNING( "Control::GetVisualProperty(%s, %s) No renderers\n", visualName.c_str(), noRenderers.str().c_str() );
149 return HandleIndex( handle, Property::INVALID_INDEX );
152 void SetDefaultTransform( Property::Map& propertyMap )
156 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET, Vector2(0.0f, 0.0f) )
157 .Add( Toolkit::DevelVisual::Transform::Property::SIZE, Vector2(1.0f, 1.0f) )
158 .Add( Toolkit::DevelVisual::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
159 .Add( Toolkit::DevelVisual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER )
160 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET_SIZE_MODE, Vector4::ZERO );
164 * Creates control through type registry
168 return Internal::Control::New();
172 * Performs actions as requested using the action name.
173 * @param[in] object The object on which to perform the action.
174 * @param[in] actionName The action to perform.
175 * @param[in] attributes The attributes with which to perfrom this action.
176 * @return true if action has been accepted by this control
178 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
179 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
183 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
185 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
188 // if cast succeeds there is an implementation so no need to check
189 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
197 * Connects a callback function with the object's signals.
198 * @param[in] object The object providing the signal.
199 * @param[in] tracker Used to disconnect the signal.
200 * @param[in] signalName The signal to connect to.
201 * @param[in] functor A newly allocated FunctorDelegate.
202 * @return True if the signal was connected.
203 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
205 const char* SIGNAL_KEY_EVENT = "keyEvent";
206 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
207 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
208 const char* SIGNAL_TAPPED = "tapped";
209 const char* SIGNAL_PANNED = "panned";
210 const char* SIGNAL_PINCHED = "pinched";
211 const char* SIGNAL_LONG_PRESSED = "longPressed";
212 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
214 Dali::BaseHandle handle( object );
216 bool connected( false );
217 Toolkit::Control control = Toolkit::Control::DownCast( handle );
220 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
223 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
225 controlImpl.KeyEventSignal().Connect( tracker, functor );
227 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
229 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
231 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
233 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
235 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
237 controlImpl.EnableGestureDetection( Gesture::Tap );
238 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
240 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
242 controlImpl.EnableGestureDetection( Gesture::Pan );
243 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
245 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
247 controlImpl.EnableGestureDetection( Gesture::Pinch );
248 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
250 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
252 controlImpl.EnableGestureDetection( Gesture::LongPress );
253 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
259 // Setup signals and actions using the type-registry.
260 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
262 // Note: Properties are registered separately below.
264 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
265 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
266 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
267 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
268 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
269 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
270 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
272 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
274 DALI_TYPE_REGISTRATION_END()
276 } // unnamed namespace
281 class Control::Impl : public ConnectionTracker
285 // Construction & Destruction
286 Impl(Control& controlImpl)
287 : mControlImpl( controlImpl ),
289 mBackgroundColor(Color::TRANSPARENT),
290 mStartingPinchScale( NULL ),
292 mPinchGestureDetector(),
293 mPanGestureDetector(),
294 mTapGestureDetector(),
295 mLongPressGestureDetector(),
296 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
297 mIsKeyboardNavigationSupported( false ),
298 mIsKeyboardFocusGroup( false )
304 // All gesture detectors will be destroyed so no need to disconnect.
305 delete mStartingPinchScale;
308 // Gesture Detection Methods
310 void PinchDetected(Actor actor, const PinchGesture& pinch)
312 mControlImpl.OnPinch(pinch);
315 void PanDetected(Actor actor, const PanGesture& pan)
317 mControlImpl.OnPan(pan);
320 void TapDetected(Actor actor, const TapGesture& tap)
322 mControlImpl.OnTap(tap);
325 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
327 mControlImpl.OnLongPress(longPress);
333 * Called when a property of an object of this type is set.
334 * @param[in] object The object whose property is set.
335 * @param[in] index The property index.
336 * @param[in] value The new property value.
338 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
340 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
344 Control& controlImpl( GetImplementation( control ) );
348 case Toolkit::Control::Property::STYLE_NAME:
350 controlImpl.SetStyleName( value.Get< std::string >() );
354 case Toolkit::Control::Property::BACKGROUND_COLOR:
356 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
357 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
361 case Toolkit::Control::Property::BACKGROUND_IMAGE:
363 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
364 Image image = Scripting::NewImage( value );
367 controlImpl.SetBackgroundImage( image );
371 // An empty image means the background is no longer required
372 controlImpl.ClearBackground();
377 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
379 if ( value.Get< bool >() )
381 controlImpl.SetKeyInputFocus();
385 controlImpl.ClearKeyInputFocus();
390 case Toolkit::Control::Property::BACKGROUND:
393 const Property::Map* map = value.GetMap();
394 if( map && !map->Empty() )
396 controlImpl.SetBackground( *map );
398 else if( value.Get( url ) )
400 // don't know the size to load
401 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
404 controlImpl.RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
405 visual.SetDepthIndex( DepthIndex::BACKGROUND );
410 // The background is an empty property map, so we should clear the background
411 controlImpl.ClearBackground();
420 * Called to retrieve a property of an object of this type.
421 * @param[in] object The object whose property is to be retrieved.
422 * @param[in] index The property index.
423 * @return The current value of the property.
425 static Property::Value GetProperty( BaseObject* object, Property::Index index )
427 Property::Value value;
429 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
433 Control& controlImpl( GetImplementation( control ) );
437 case Toolkit::Control::Property::STYLE_NAME:
439 value = controlImpl.GetStyleName();
443 case Toolkit::Control::Property::BACKGROUND_COLOR:
445 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
446 value = controlImpl.GetBackgroundColor();
450 case Toolkit::Control::Property::BACKGROUND_IMAGE:
452 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
454 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
457 visual.CreatePropertyMap( map );
463 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
465 value = controlImpl.HasKeyInputFocus();
469 case Toolkit::Control::Property::BACKGROUND:
472 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
475 visual.CreatePropertyMap( map );
490 Control& mControlImpl;
491 RegisteredVisualContainer mVisuals; // Stores visuals needed by the control, non trivial type so std::vector used.
492 std::string mStyleName;
493 Vector4 mBackgroundColor; ///< The color of the background visual
494 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
495 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
496 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
497 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
500 PinchGestureDetector mPinchGestureDetector;
501 PanGestureDetector mPanGestureDetector;
502 TapGestureDetector mTapGestureDetector;
503 LongPressGestureDetector mLongPressGestureDetector;
505 ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
506 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
507 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
509 // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
510 static const PropertyRegistration PROPERTY_1;
511 static const PropertyRegistration PROPERTY_2;
512 static const PropertyRegistration PROPERTY_3;
513 static const PropertyRegistration PROPERTY_4;
514 static const PropertyRegistration PROPERTY_5;
517 // Properties registered without macro to use specific member variables.
518 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
519 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
520 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
521 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
522 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
524 Toolkit::Control Control::New()
526 // Create the implementation, temporarily owned on stack
527 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
529 // Pass ownership to handle
530 Toolkit::Control handle( *controlImpl );
532 // Second-phase init of the implementation
533 // This can only be done after the CustomActor connection has been made...
534 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::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
564 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
566 SetBackground( map );
569 Vector4 Control::GetBackgroundColor() const
571 return mImpl->mBackgroundColor;
574 void Control::SetBackground( const Property::Map& map )
576 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
579 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
580 visual.SetDepthIndex( DepthIndex::BACKGROUND );
582 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
587 void Control::SetBackgroundImage( Image image )
589 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
592 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
593 visual.SetDepthIndex( DepthIndex::BACKGROUND );
597 void Control::ClearBackground()
599 UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
600 mImpl->mBackgroundColor = Color::TRANSPARENT;
602 // Trigger a size negotiation request that may be needed when unregistering a visual.
606 void Control::EnableGestureDetection(Gesture::Type type)
608 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
610 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
611 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
612 mImpl->mPinchGestureDetector.Attach(Self());
615 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
617 mImpl->mPanGestureDetector = PanGestureDetector::New();
618 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
619 mImpl->mPanGestureDetector.Attach(Self());
622 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
624 mImpl->mTapGestureDetector = TapGestureDetector::New();
625 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
626 mImpl->mTapGestureDetector.Attach(Self());
629 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
631 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
632 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
633 mImpl->mLongPressGestureDetector.Attach(Self());
637 void Control::DisableGestureDetection(Gesture::Type type)
639 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
641 mImpl->mPinchGestureDetector.Detach(Self());
642 mImpl->mPinchGestureDetector.Reset();
645 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
647 mImpl->mPanGestureDetector.Detach(Self());
648 mImpl->mPanGestureDetector.Reset();
651 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
653 mImpl->mTapGestureDetector.Detach(Self());
654 mImpl->mTapGestureDetector.Reset();
657 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
659 mImpl->mLongPressGestureDetector.Detach(Self());
660 mImpl->mLongPressGestureDetector.Reset();
664 PinchGestureDetector Control::GetPinchGestureDetector() const
666 return mImpl->mPinchGestureDetector;
669 PanGestureDetector Control::GetPanGestureDetector() const
671 return mImpl->mPanGestureDetector;
674 TapGestureDetector Control::GetTapGestureDetector() const
676 return mImpl->mTapGestureDetector;
679 LongPressGestureDetector Control::GetLongPressGestureDetector() const
681 return mImpl->mLongPressGestureDetector;
684 void Control::SetKeyboardNavigationSupport(bool isSupported)
686 mImpl->mIsKeyboardNavigationSupported = isSupported;
689 bool Control::IsKeyboardNavigationSupported()
691 return mImpl->mIsKeyboardNavigationSupported;
694 void Control::SetKeyInputFocus()
696 if( Self().OnStage() )
698 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
702 bool Control::HasKeyInputFocus()
705 if( Self().OnStage() )
707 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
712 void Control::ClearKeyInputFocus()
714 if( Self().OnStage() )
716 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
720 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
722 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
724 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
725 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
728 bool Control::IsKeyboardFocusGroup()
730 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
733 void Control::AccessibilityActivate()
735 // Inform deriving classes
736 OnAccessibilityActivated();
739 void Control::KeyboardEnter()
741 // Inform deriving classes
745 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
747 RegisterVisual( index, visual, true );
750 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
752 bool visualReplaced ( false );
755 if( !mImpl->mVisuals.Empty() )
757 RegisteredVisualContainer::Iterator iter;
758 // Check if visual (index) is already registered. Replace if so.
759 if ( FindVisual( index, mImpl->mVisuals, iter ) )
761 if( (*iter)->visual && self.OnStage() )
763 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
765 (*iter)->visual = visual;
766 visualReplaced = true;
770 // If not set, set the name of the visual to the same name as the control's property.
771 // ( If the control has been type registered )
772 if( visual.GetName().empty() )
774 // Check if the control has been type registered:
775 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(*this) );
778 // Check if the property index has been registered:
779 Property::IndexContainer indices;
780 typeInfo.GetPropertyIndices( indices );
781 Property::IndexContainer::Iterator iter = std::find( indices.Begin(), indices.End(), index );
782 if( iter != indices.End() )
784 // If it has, then get it's name and use that for the visual
785 std::string visualName = typeInfo.GetPropertyName( index );
786 visual.SetName( visualName );
791 if( !visualReplaced ) // New registration entry
793 mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
796 if( visual && self.OnStage() && enabled )
798 Toolkit::GetImplementation(visual).SetOnStage( self );
800 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
804 void Control::UnregisterVisual( Property::Index index )
806 RegisteredVisualContainer::Iterator iter;
807 if ( FindVisual( index, mImpl->mVisuals, iter ) )
809 Actor self( Self() );
810 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
811 (*iter)->visual.Reset();
812 mImpl->mVisuals.Erase( iter );
816 Toolkit::Visual::Base Control::GetVisual( Property::Index index ) const
818 RegisteredVisualContainer::Iterator iter;
819 if ( FindVisual( index, mImpl->mVisuals, iter ) )
821 return (*iter)->visual;
824 return Toolkit::Visual::Base();
827 void Control::EnableVisual( Property::Index index, bool enable )
829 RegisteredVisualContainer::Iterator iter;
830 if ( FindVisual( index, mImpl->mVisuals, iter ) )
832 if ( (*iter)->enabled == enable )
834 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual Already enabled set (%s) \n", enable?"enabled":"disabled");
838 (*iter)->enabled = enable;
839 Actor parentActor = Self();
840 if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
844 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) on stage \n", index );
845 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
849 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) off stage \n", index );
850 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
856 bool Control::IsVisualEnabled( Property::Index index ) const
858 RegisteredVisualContainer::Iterator iter;
859 if ( FindVisual( index, mImpl->mVisuals, iter ) )
861 return (*iter)->enabled;
866 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
868 Dali::Animation transition;
869 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
871 if( transitionData.Count() > 0 )
873 // Setup a Transition from TransitionData.
874 TransitionData::Iterator end = transitionData.End();
875 for( TransitionData::Iterator iter = transitionData.Begin() ;
876 iter != end; ++iter )
878 TransitionData::Animator* animator = (*iter);
879 HandleIndex handleIndex;
881 // Attempt to find the object name as a child actor
882 Actor child = Self().FindChildByName( animator->objectName );
885 Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
886 handleIndex = HandleIndex( child, propertyIndex );
890 handleIndex = GetVisualProperty( *this, mImpl->mVisuals,
891 animator->objectName,
892 animator->propertyKey );
895 if( handleIndex.handle && handleIndex.index != Property::INVALID_INDEX )
897 if( animator->animate == false )
899 if( animator->targetValue.GetType() != Property::NONE )
901 handleIndex.handle.SetProperty( handleIndex.index, animator->targetValue );
906 if( animator->initialValue.GetType() != Property::NONE )
908 handleIndex.handle.SetProperty( handleIndex.index, animator->initialValue );
913 // Create an animation with a default .1 second duration - the animators
914 // will automatically force it to the 'right' duration.
915 transition = Dali::Animation::New( 0.1f );
918 transition.AnimateTo( Property( handleIndex.handle, handleIndex.index ),
919 animator->targetValue,
920 animator->alphaFunction,
921 TimePeriod( animator->timePeriodDelay,
922 animator->timePeriodDuration ) );
931 bool Control::OnAccessibilityActivated()
933 return false; // Accessibility activation is not handled by default
936 bool Control::OnKeyboardEnter()
938 return false; // Keyboard enter is not handled by default
941 bool Control::OnAccessibilityPan(PanGesture gesture)
943 return false; // Accessibility pan gesture is not handled by default
946 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
948 return false; // Accessibility touch event is not handled by default
951 bool Control::OnAccessibilityValueChange(bool isIncrease)
953 return false; // Accessibility value change action is not handled by default
956 bool Control::OnAccessibilityZoom()
958 return false; // Accessibility zoom action is not handled by default
961 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
966 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
970 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
972 return mImpl->mKeyEventSignal;
975 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
977 return mImpl->mKeyInputFocusGainedSignal;
980 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
982 return mImpl->mKeyInputFocusLostSignal;
985 bool Control::EmitKeyEventSignal( const KeyEvent& event )
987 // Guard against destruction during signal emission
988 Dali::Toolkit::Control handle( GetOwner() );
990 bool consumed = false;
992 // signals are allocated dynamically when someone connects
993 if ( !mImpl->mKeyEventSignal.Empty() )
995 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
1000 // Notification for derived classes
1001 consumed = OnKeyEvent(event);
1007 Control::Control( ControlBehaviour behaviourFlags )
1008 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
1009 mImpl(new Impl(*this))
1011 mImpl->mFlags = behaviourFlags;
1019 void Control::Initialize()
1021 // Call deriving classes so initialised before styling is applied to them.
1024 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
1025 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
1027 Toolkit::StyleManager styleManager = StyleManager::Get();
1029 // if stylemanager is available
1032 StyleManager& styleManagerImpl = GetImpl( styleManager );
1034 // Register for style changes
1035 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
1037 // Apply the current style
1038 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
1042 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
1044 SetKeyboardNavigationSupport( true );
1048 void Control::OnInitialize()
1052 void Control::OnControlChildAdd( Actor& child )
1056 void Control::OnControlChildRemove( Actor& child )
1060 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
1062 // By default the control is only interested in theme (not font) changes
1063 if( styleManager && change == StyleChange::THEME_CHANGE )
1065 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1070 void Control::OnPinch(const PinchGesture& pinch)
1072 if( !( mImpl->mStartingPinchScale ) )
1075 mImpl->mStartingPinchScale = new Vector3;
1078 if( pinch.state == Gesture::Started )
1080 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1083 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1086 void Control::OnPan( const PanGesture& pan )
1090 void Control::OnTap(const TapGesture& tap)
1094 void Control::OnLongPress( const LongPressGesture& longPress )
1098 void Control::EmitKeyInputFocusSignal( bool focusGained )
1100 Dali::Toolkit::Control handle( GetOwner() );
1104 // signals are allocated dynamically when someone connects
1105 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
1107 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
1112 // signals are allocated dynamically when someone connects
1113 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
1115 mImpl->mKeyInputFocusLostSignal.Emit( handle );
1120 void Control::OnStageConnection( int depth )
1122 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
1124 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1126 // Check whether the visual is empty and enabled
1127 if( (*iter)->visual && (*iter)->enabled )
1129 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
1130 Actor self( Self() );
1131 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
1136 void Control::OnStageDisconnection()
1138 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1140 // Check whether the visual is empty
1141 if( (*iter)->visual )
1143 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
1144 Actor self( Self() );
1145 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
1150 void Control::OnKeyInputFocusGained()
1152 EmitKeyInputFocusSignal( true );
1155 void Control::OnKeyInputFocusLost()
1157 EmitKeyInputFocusSignal( false );
1160 void Control::OnChildAdd(Actor& child)
1162 // Notify derived classes.
1163 OnControlChildAdd( child );
1166 void Control::OnChildRemove(Actor& child)
1168 // Notify derived classes.
1169 OnControlChildRemove( child );
1172 void Control::OnSizeSet(const Vector3& targetSize)
1174 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1177 Vector2 size( targetSize );
1178 Property::Map transformMap;
1179 SetDefaultTransform( transformMap );
1180 visual.SetTransformAndSize( transformMap, size );
1184 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1186 // @todo size negotiate background to new size, animate as well?
1189 bool Control::OnTouchEvent(const TouchEvent& event)
1191 return false; // Do not consume
1194 bool Control::OnHoverEvent(const HoverEvent& event)
1196 return false; // Do not consume
1199 bool Control::OnKeyEvent(const KeyEvent& event)
1201 return false; // Do not consume
1204 bool Control::OnWheelEvent(const WheelEvent& event)
1206 return false; // Do not consume
1209 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1211 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1213 container.Add( Self().GetChildAt( i ), size );
1216 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1219 Vector2 controlSize( size );
1220 Property::Map transformMap;
1221 SetDefaultTransform( transformMap );
1222 visual.SetTransformAndSize( transformMap, controlSize );
1226 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1230 Vector3 Control::GetNaturalSize()
1232 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1235 Vector2 naturalSize;
1236 visual.GetNaturalSize( naturalSize );
1237 return Vector3( naturalSize );
1239 return Vector3::ZERO;
1242 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1244 return CalculateChildSizeBase( child, dimension );
1247 float Control::GetHeightForWidth( float width )
1249 return GetHeightForWidthBase( width );
1252 float Control::GetWidthForHeight( float height )
1254 return GetWidthForHeightBase( height );
1257 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1259 return RelayoutDependentOnChildrenBase( dimension );
1262 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1266 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1270 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1272 mImpl->SignalConnected( slotObserver, callback );
1275 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1277 mImpl->SignalDisconnected( slotObserver, callback );
1280 Control& GetImplementation( Dali::Toolkit::Control& handle )
1282 CustomActorImpl& customInterface = handle.GetImplementation();
1283 // downcast to control
1284 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1288 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1290 const CustomActorImpl& customInterface = handle.GetImplementation();
1291 // downcast to control
1292 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1296 } // namespace Internal
1298 } // namespace Toolkit