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>
64 #if defined(DEBUG_ENABLED)
65 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
68 DALI_ENUM_TO_STRING_TABLE_BEGIN( CLIPPING_MODE )
69 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, DISABLED )
70 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, CLIP_CHILDREN )
71 DALI_ENUM_TO_STRING_TABLE_END( CLIPPING_MODE )
74 * Struct used to store Visual within the control, index is a unique key for each visual.
76 struct RegisteredVisual
78 Property::Index index;
79 Toolkit::Visual::Base visual;
82 RegisteredVisual( Property::Index aIndex, Toolkit::Visual::Base &aVisual, bool aEnabled) :
83 index(aIndex), visual(aVisual), enabled(aEnabled) {}
86 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
89 * Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
91 bool FindVisual( Property::Index targetIndex, RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
93 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
95 if ( (*iter)->index == targetIndex )
103 Toolkit::Visual::Base GetVisualByName(
104 RegisteredVisualContainer& visuals,
105 const std::string& visualName )
107 Toolkit::Visual::Base visualHandle;
109 RegisteredVisualContainer::Iterator iter;
110 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
112 Toolkit::Visual::Base visual = (*iter)->visual;
113 if( visual && visual.GetName() == visualName )
115 visualHandle = visual;
122 void SetDefaultTransform( Property::Map& propertyMap )
126 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET, Vector2(0.0f, 0.0f) )
127 .Add( Toolkit::DevelVisual::Transform::Property::SIZE, Vector2(1.0f, 1.0f) )
128 .Add( Toolkit::DevelVisual::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
129 .Add( Toolkit::DevelVisual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER )
130 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET_SIZE_MODE, Vector4::ZERO );
134 * Creates control through type registry
138 return Internal::Control::New();
142 * Performs actions as requested using the action name.
143 * @param[in] object The object on which to perform the action.
144 * @param[in] actionName The action to perform.
145 * @param[in] attributes The attributes with which to perfrom this action.
146 * @return true if action has been accepted by this control
148 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
149 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
153 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
155 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
158 // if cast succeeds there is an implementation so no need to check
159 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
167 * Connects a callback function with the object's signals.
168 * @param[in] object The object providing the signal.
169 * @param[in] tracker Used to disconnect the signal.
170 * @param[in] signalName The signal to connect to.
171 * @param[in] functor A newly allocated FunctorDelegate.
172 * @return True if the signal was connected.
173 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
175 const char* SIGNAL_KEY_EVENT = "keyEvent";
176 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
177 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
178 const char* SIGNAL_TAPPED = "tapped";
179 const char* SIGNAL_PANNED = "panned";
180 const char* SIGNAL_PINCHED = "pinched";
181 const char* SIGNAL_LONG_PRESSED = "longPressed";
182 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
184 Dali::BaseHandle handle( object );
186 bool connected( false );
187 Toolkit::Control control = Toolkit::Control::DownCast( handle );
190 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
193 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
195 controlImpl.KeyEventSignal().Connect( tracker, functor );
197 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
199 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
201 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
203 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
205 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
207 controlImpl.EnableGestureDetection( Gesture::Tap );
208 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
210 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
212 controlImpl.EnableGestureDetection( Gesture::Pan );
213 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
215 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
217 controlImpl.EnableGestureDetection( Gesture::Pinch );
218 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
220 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
222 controlImpl.EnableGestureDetection( Gesture::LongPress );
223 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
229 // Setup signals and actions using the type-registry.
230 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
232 // Note: Properties are registered separately below.
234 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
235 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
236 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
237 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
238 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
239 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
240 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
242 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
244 DALI_TYPE_REGISTRATION_END()
246 } // unnamed namespace
251 class Control::Impl : public ConnectionTracker
255 // Construction & Destruction
256 Impl(Control& controlImpl)
257 : mControlImpl( controlImpl ),
259 mBackgroundColor(Color::TRANSPARENT),
260 mStartingPinchScale( NULL ),
262 mPinchGestureDetector(),
263 mPanGestureDetector(),
264 mTapGestureDetector(),
265 mLongPressGestureDetector(),
266 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
267 mIsKeyboardNavigationSupported( false ),
268 mIsKeyboardFocusGroup( false )
274 // All gesture detectors will be destroyed so no need to disconnect.
275 delete mStartingPinchScale;
278 // Gesture Detection Methods
280 void PinchDetected(Actor actor, const PinchGesture& pinch)
282 mControlImpl.OnPinch(pinch);
285 void PanDetected(Actor actor, const PanGesture& pan)
287 mControlImpl.OnPan(pan);
290 void TapDetected(Actor actor, const TapGesture& tap)
292 mControlImpl.OnTap(tap);
295 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
297 mControlImpl.OnLongPress(longPress);
303 * Called when a property of an object of this type is set.
304 * @param[in] object The object whose property is set.
305 * @param[in] index The property index.
306 * @param[in] value The new property value.
308 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
310 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
314 Control& controlImpl( GetImplementation( control ) );
318 case Toolkit::Control::Property::STYLE_NAME:
320 controlImpl.SetStyleName( value.Get< std::string >() );
324 case Toolkit::Control::Property::BACKGROUND_COLOR:
326 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
327 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
331 case Toolkit::Control::Property::BACKGROUND_IMAGE:
333 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
334 Image image = Scripting::NewImage( value );
337 controlImpl.SetBackgroundImage( image );
341 // An empty image means the background is no longer required
342 controlImpl.ClearBackground();
347 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
349 if ( value.Get< bool >() )
351 controlImpl.SetKeyInputFocus();
355 controlImpl.ClearKeyInputFocus();
360 case Toolkit::Control::Property::BACKGROUND:
363 const Property::Map* map = value.GetMap();
364 if( map && !map->Empty() )
366 controlImpl.SetBackground( *map );
368 else if( value.Get( url ) )
370 // don't know the size to load
371 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
374 controlImpl.RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
375 visual.SetDepthIndex( DepthIndex::BACKGROUND );
380 // The background is an empty property map, so we should clear the background
381 controlImpl.ClearBackground();
386 case Toolkit::DevelControl::Property::TOOLTIP:
388 TooltipPtr& tooltipPtr = controlImpl.mImpl->mTooltip;
391 tooltipPtr = Tooltip::New( control );
393 tooltipPtr->SetProperties( value );
400 * Called to retrieve a property of an object of this type.
401 * @param[in] object The object whose property is to be retrieved.
402 * @param[in] index The property index.
403 * @return The current value of the property.
405 static Property::Value GetProperty( BaseObject* object, Property::Index index )
407 Property::Value value;
409 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
413 Control& controlImpl( GetImplementation( control ) );
417 case Toolkit::Control::Property::STYLE_NAME:
419 value = controlImpl.GetStyleName();
423 case Toolkit::Control::Property::BACKGROUND_COLOR:
425 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
426 value = controlImpl.GetBackgroundColor();
430 case Toolkit::Control::Property::BACKGROUND_IMAGE:
432 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
434 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
437 visual.CreatePropertyMap( map );
443 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
445 value = controlImpl.HasKeyInputFocus();
449 case Toolkit::Control::Property::BACKGROUND:
452 Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
455 visual.CreatePropertyMap( map );
462 case Toolkit::DevelControl::Property::TOOLTIP:
465 if( controlImpl.mImpl->mTooltip )
467 controlImpl.mImpl->mTooltip->CreatePropertyMap( map );
481 Control& mControlImpl;
482 RegisteredVisualContainer mVisuals; // Stores visuals needed by the control, non trivial type so std::vector used.
483 std::string mStyleName;
484 Vector4 mBackgroundColor; ///< The color of the background visual
485 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
486 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
487 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
488 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
491 PinchGestureDetector mPinchGestureDetector;
492 PanGestureDetector mPanGestureDetector;
493 TapGestureDetector mTapGestureDetector;
494 LongPressGestureDetector mLongPressGestureDetector;
499 ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
500 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
501 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
503 // Properties - these need to be members of Internal::Control::Impl as they access private methods/data of Internal::Control and Internal::Control::Impl.
504 static const PropertyRegistration PROPERTY_1;
505 static const PropertyRegistration PROPERTY_2;
506 static const PropertyRegistration PROPERTY_3;
507 static const PropertyRegistration PROPERTY_4;
508 static const PropertyRegistration PROPERTY_5;
509 static const PropertyRegistration PROPERTY_6;
512 // Properties registered without macro to use specific member variables.
513 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
514 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
515 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
516 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
517 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
518 const PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "tooltip", Toolkit::DevelControl::Property::TOOLTIP, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
520 Toolkit::Control Control::New()
522 // Create the implementation, temporarily owned on stack
523 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
525 // Pass ownership to handle
526 Toolkit::Control handle( *controlImpl );
528 // Second-phase init of the implementation
529 // This can only be done after the CustomActor connection has been made...
530 controlImpl->Initialize();
535 void Control::SetStyleName( const std::string& styleName )
537 if( styleName != mImpl->mStyleName )
539 mImpl->mStyleName = styleName;
541 // Apply new style, if stylemanager is available
542 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
545 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
550 const std::string& Control::GetStyleName() const
552 return mImpl->mStyleName;
555 void Control::SetBackgroundColor( const Vector4& color )
557 mImpl->mBackgroundColor = color;
559 map[ Toolkit::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
560 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
562 SetBackground( map );
565 Vector4 Control::GetBackgroundColor() const
567 return mImpl->mBackgroundColor;
570 void Control::SetBackground( const Property::Map& map )
572 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
575 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
576 visual.SetDepthIndex( DepthIndex::BACKGROUND );
578 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
583 void Control::SetBackgroundImage( Image image )
585 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
588 RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
589 visual.SetDepthIndex( DepthIndex::BACKGROUND );
593 void Control::ClearBackground()
595 UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
596 mImpl->mBackgroundColor = Color::TRANSPARENT;
598 // Trigger a size negotiation request that may be needed when unregistering a visual.
602 void Control::EnableGestureDetection(Gesture::Type type)
604 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
606 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
607 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
608 mImpl->mPinchGestureDetector.Attach(Self());
611 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
613 mImpl->mPanGestureDetector = PanGestureDetector::New();
614 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
615 mImpl->mPanGestureDetector.Attach(Self());
618 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
620 mImpl->mTapGestureDetector = TapGestureDetector::New();
621 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
622 mImpl->mTapGestureDetector.Attach(Self());
625 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
627 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
628 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
629 mImpl->mLongPressGestureDetector.Attach(Self());
633 void Control::DisableGestureDetection(Gesture::Type type)
635 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
637 mImpl->mPinchGestureDetector.Detach(Self());
638 mImpl->mPinchGestureDetector.Reset();
641 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
643 mImpl->mPanGestureDetector.Detach(Self());
644 mImpl->mPanGestureDetector.Reset();
647 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
649 mImpl->mTapGestureDetector.Detach(Self());
650 mImpl->mTapGestureDetector.Reset();
653 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
655 mImpl->mLongPressGestureDetector.Detach(Self());
656 mImpl->mLongPressGestureDetector.Reset();
660 PinchGestureDetector Control::GetPinchGestureDetector() const
662 return mImpl->mPinchGestureDetector;
665 PanGestureDetector Control::GetPanGestureDetector() const
667 return mImpl->mPanGestureDetector;
670 TapGestureDetector Control::GetTapGestureDetector() const
672 return mImpl->mTapGestureDetector;
675 LongPressGestureDetector Control::GetLongPressGestureDetector() const
677 return mImpl->mLongPressGestureDetector;
680 void Control::SetKeyboardNavigationSupport(bool isSupported)
682 mImpl->mIsKeyboardNavigationSupported = isSupported;
685 bool Control::IsKeyboardNavigationSupported()
687 return mImpl->mIsKeyboardNavigationSupported;
690 void Control::SetKeyInputFocus()
692 if( Self().OnStage() )
694 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
698 bool Control::HasKeyInputFocus()
701 if( Self().OnStage() )
703 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
708 void Control::ClearKeyInputFocus()
710 if( Self().OnStage() )
712 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
716 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
718 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
720 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
721 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
724 bool Control::IsKeyboardFocusGroup()
726 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
729 void Control::AccessibilityActivate()
731 // Inform deriving classes
732 OnAccessibilityActivated();
735 void Control::KeyboardEnter()
737 // Inform deriving classes
741 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
743 RegisterVisual( index, visual, true );
746 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
748 bool visualReplaced ( false );
751 if( !mImpl->mVisuals.Empty() )
753 RegisteredVisualContainer::Iterator iter;
754 // Check if visual (index) is already registered. Replace if so.
755 if ( FindVisual( index, mImpl->mVisuals, iter ) )
757 if( (*iter)->visual && self.OnStage() )
759 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
761 (*iter)->visual = visual;
762 visualReplaced = true;
766 // If not set, set the name of the visual to the same name as the control's property.
767 // ( If the control has been type registered )
768 if( visual.GetName().empty() )
770 // Check if the control has been type registered:
771 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(*this) );
774 // Check if the property index has been registered:
775 Property::IndexContainer indices;
776 typeInfo.GetPropertyIndices( indices );
777 Property::IndexContainer::Iterator iter = std::find( indices.Begin(), indices.End(), index );
778 if( iter != indices.End() )
780 // If it has, then get it's name and use that for the visual
781 std::string visualName = typeInfo.GetPropertyName( index );
782 visual.SetName( visualName );
787 if( !visualReplaced ) // New registration entry
789 mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
792 if( visual && self.OnStage() && enabled )
794 Toolkit::GetImplementation(visual).SetOnStage( self );
796 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
800 void Control::UnregisterVisual( Property::Index index )
802 RegisteredVisualContainer::Iterator iter;
803 if ( FindVisual( index, mImpl->mVisuals, iter ) )
805 Actor self( Self() );
806 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
807 (*iter)->visual.Reset();
808 mImpl->mVisuals.Erase( iter );
812 Toolkit::Visual::Base Control::GetVisual( Property::Index index ) const
814 RegisteredVisualContainer::Iterator iter;
815 if ( FindVisual( index, mImpl->mVisuals, iter ) )
817 return (*iter)->visual;
820 return Toolkit::Visual::Base();
823 void Control::EnableVisual( Property::Index index, bool enable )
825 RegisteredVisualContainer::Iterator iter;
826 if ( FindVisual( index, mImpl->mVisuals, iter ) )
828 if ( (*iter)->enabled == enable )
830 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual Already enabled set (%s) \n", enable?"enabled":"disabled");
834 (*iter)->enabled = enable;
835 Actor parentActor = Self();
836 if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
840 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) on stage \n", index );
841 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
845 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) off stage \n", index );
846 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
852 bool Control::IsVisualEnabled( Property::Index index ) const
854 RegisteredVisualContainer::Iterator iter;
855 if ( FindVisual( index, mImpl->mVisuals, iter ) )
857 return (*iter)->enabled;
862 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
864 Dali::Animation transition;
865 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
867 if( transitionData.Count() > 0 )
869 // Setup a Transition from TransitionData.
870 TransitionData::Iterator end = transitionData.End();
871 for( TransitionData::Iterator iter = transitionData.Begin() ;
872 iter != end; ++iter )
874 TransitionData::Animator* animator = (*iter);
876 Toolkit::Visual::Base visual = GetVisualByName( mImpl->mVisuals, animator->objectName );
880 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
881 visualImpl.AnimateProperty( transition, *animator );
885 // Otherwise, try any actor children of control (Including the control)
886 Actor child = Self().FindChildByName( animator->objectName );
889 Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
890 if( propertyIndex != Property::INVALID_INDEX )
892 if( animator->animate == false )
894 if( animator->targetValue.GetType() != Property::NONE )
896 child.SetProperty( propertyIndex, animator->targetValue );
899 else // animate the property
901 if( animator->initialValue.GetType() != Property::NONE )
903 child.SetProperty( propertyIndex, animator->initialValue );
908 transition = Dali::Animation::New( 0.1f );
911 transition.AnimateTo( Property( child, propertyIndex ),
912 animator->targetValue,
913 animator->alphaFunction,
914 TimePeriod( animator->timePeriodDelay,
915 animator->timePeriodDuration ) );
926 bool Control::OnAccessibilityActivated()
928 return false; // Accessibility activation is not handled by default
931 bool Control::OnKeyboardEnter()
933 return false; // Keyboard enter is not handled by default
936 bool Control::OnAccessibilityPan(PanGesture gesture)
938 return false; // Accessibility pan gesture is not handled by default
941 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
943 return false; // Accessibility touch event is not handled by default
946 bool Control::OnAccessibilityValueChange(bool isIncrease)
948 return false; // Accessibility value change action is not handled by default
951 bool Control::OnAccessibilityZoom()
953 return false; // Accessibility zoom action is not handled by default
956 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
961 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
965 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
967 return mImpl->mKeyEventSignal;
970 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
972 return mImpl->mKeyInputFocusGainedSignal;
975 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
977 return mImpl->mKeyInputFocusLostSignal;
980 bool Control::EmitKeyEventSignal( const KeyEvent& event )
982 // Guard against destruction during signal emission
983 Dali::Toolkit::Control handle( GetOwner() );
985 bool consumed = false;
987 // signals are allocated dynamically when someone connects
988 if ( !mImpl->mKeyEventSignal.Empty() )
990 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
995 // Notification for derived classes
996 consumed = OnKeyEvent(event);
1002 Control::Control( ControlBehaviour behaviourFlags )
1003 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
1004 mImpl(new Impl(*this))
1006 mImpl->mFlags = behaviourFlags;
1014 void Control::Initialize()
1016 // Call deriving classes so initialised before styling is applied to them.
1019 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
1020 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
1022 Toolkit::StyleManager styleManager = StyleManager::Get();
1024 // if stylemanager is available
1027 StyleManager& styleManagerImpl = GetImpl( styleManager );
1029 // Register for style changes
1030 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
1032 // Apply the current style
1033 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
1037 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
1039 SetKeyboardNavigationSupport( true );
1043 void Control::OnInitialize()
1047 void Control::OnControlChildAdd( Actor& child )
1051 void Control::OnControlChildRemove( Actor& child )
1055 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
1057 // By default the control is only interested in theme (not font) changes
1058 if( styleManager && change == StyleChange::THEME_CHANGE )
1060 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1065 void Control::OnPinch(const PinchGesture& pinch)
1067 if( !( mImpl->mStartingPinchScale ) )
1070 mImpl->mStartingPinchScale = new Vector3;
1073 if( pinch.state == Gesture::Started )
1075 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1078 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1081 void Control::OnPan( const PanGesture& pan )
1085 void Control::OnTap(const TapGesture& tap)
1089 void Control::OnLongPress( const LongPressGesture& longPress )
1093 void Control::EmitKeyInputFocusSignal( bool focusGained )
1095 Dali::Toolkit::Control handle( GetOwner() );
1099 // signals are allocated dynamically when someone connects
1100 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
1102 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
1107 // signals are allocated dynamically when someone connects
1108 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
1110 mImpl->mKeyInputFocusLostSignal.Emit( handle );
1115 void Control::OnStageConnection( int depth )
1117 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
1119 Actor self( Self() );
1121 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1123 // Check whether the visual is empty and enabled
1124 if( (*iter)->visual && (*iter)->enabled )
1126 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
1127 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
1131 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() )
1133 Property::Value clippingValue = self.GetProperty( Actor::Property::CLIPPING_MODE );
1134 int clippingMode = ClippingMode::DISABLED;
1135 if( clippingValue.Get( clippingMode ) )
1137 // Add a transparent background if we do not have any renderers or visuals so we clip our children
1139 if( clippingMode == ClippingMode::CLIP_CHILDREN )
1141 // Create a transparent background visual which will also get staged.
1142 SetBackgroundColor( Color::TRANSPARENT );
1148 void Control::OnStageDisconnection()
1150 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1152 // Check whether the visual is empty
1153 if( (*iter)->visual )
1155 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
1156 Actor self( Self() );
1157 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
1162 void Control::OnKeyInputFocusGained()
1164 EmitKeyInputFocusSignal( true );
1167 void Control::OnKeyInputFocusLost()
1169 EmitKeyInputFocusSignal( false );
1172 void Control::OnChildAdd(Actor& child)
1174 // Notify derived classes.
1175 OnControlChildAdd( child );
1178 void Control::OnChildRemove(Actor& child)
1180 // Notify derived classes.
1181 OnControlChildRemove( child );
1184 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
1186 Actor self( Self() );
1187 if( index == Actor::Property::CLIPPING_MODE )
1189 // Only set the background if we're already on the stage and have no renderers or visuals
1191 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() && self.OnStage() )
1193 ClippingMode::Type clippingMode = ClippingMode::DISABLED;
1194 if( Scripting::GetEnumerationProperty< ClippingMode::Type >( propertyValue, CLIPPING_MODE_TABLE, CLIPPING_MODE_TABLE_COUNT, clippingMode ) )
1196 // Add a transparent background if we do not have one so we clip children
1198 if( clippingMode == ClippingMode::CLIP_CHILDREN )
1200 SetBackgroundColor( Color::TRANSPARENT );
1207 void Control::OnSizeSet(const Vector3& targetSize)
1209 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1212 Vector2 size( targetSize );
1213 Property::Map transformMap;
1214 SetDefaultTransform( transformMap );
1215 visual.SetTransformAndSize( transformMap, size );
1219 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1221 // @todo size negotiate background to new size, animate as well?
1224 bool Control::OnTouchEvent(const TouchEvent& event)
1226 return false; // Do not consume
1229 bool Control::OnHoverEvent(const HoverEvent& event)
1231 return false; // Do not consume
1234 bool Control::OnKeyEvent(const KeyEvent& event)
1236 return false; // Do not consume
1239 bool Control::OnWheelEvent(const WheelEvent& event)
1241 return false; // Do not consume
1244 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1246 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1248 container.Add( Self().GetChildAt( i ), size );
1251 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1254 Vector2 controlSize( size );
1255 Property::Map transformMap;
1256 SetDefaultTransform( transformMap );
1257 visual.SetTransformAndSize( transformMap, controlSize );
1261 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1265 Vector3 Control::GetNaturalSize()
1267 Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
1270 Vector2 naturalSize;
1271 visual.GetNaturalSize( naturalSize );
1272 return Vector3( naturalSize );
1274 return Vector3::ZERO;
1277 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1279 return CalculateChildSizeBase( child, dimension );
1282 float Control::GetHeightForWidth( float width )
1284 return GetHeightForWidthBase( width );
1287 float Control::GetWidthForHeight( float height )
1289 return GetWidthForHeightBase( height );
1292 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1294 return RelayoutDependentOnChildrenBase( dimension );
1297 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1301 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1305 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1307 mImpl->SignalConnected( slotObserver, callback );
1310 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1312 mImpl->SignalDisconnected( slotObserver, callback );
1315 Control& GetImplementation( Dali::Toolkit::Control& handle )
1317 CustomActorImpl& customInterface = handle.GetImplementation();
1318 // downcast to control
1319 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1323 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1325 const CustomActorImpl& customInterface = handle.GetImplementation();
1326 // downcast to control
1327 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1331 } // namespace Internal
1333 } // namespace Toolkit