2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/public-api/controls/control-impl.h>
22 #include <cstring> // for strcmp
25 #include <dali/public-api/animation/constraint.h>
26 #include <dali/public-api/animation/constraints.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/public-api/object/type-registry-helper.h>
29 #include <dali/public-api/rendering/renderer.h>
30 #include <dali/public-api/size-negotiation/relayout-container.h>
31 #include <dali/devel-api/common/owner-container.h>
32 #include <dali/devel-api/scripting/scripting.h>
33 #include <dali/integration-api/debug.h>
36 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
37 #include <dali-toolkit/public-api/controls/control.h>
38 #include <dali-toolkit/public-api/styling/style-manager.h>
39 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
40 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
41 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
42 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
43 #include <dali-toolkit/internal/styling/style-manager-impl.h>
44 #include <dali-toolkit/internal/visuals/color/color-visual.h>
45 #include <dali-toolkit/internal/visuals/transition-data-impl.h>
56 #if defined(DEBUG_ENABLED)
57 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::General, false, "LOG_CONTROL_VISUALS");
61 * Struct used to store Visual within the control, index is a unique key for each visual.
63 struct RegisteredVisual
65 Property::Index index;
66 Toolkit::Visual::Base visual;
70 RegisteredVisual( Property::Index aIndex, Toolkit::Visual::Base &aVisual, Actor &aPlacementActor, bool aEnabled) :
71 index(aIndex), visual(aVisual), placementActor(aPlacementActor), enabled(aEnabled) {}
76 Handle handle; ///< a handle to the target object
77 Property::Index index; ///< The index of a property provided by the referenced object
81 index( Property::INVALID_INDEX )
85 VisualProperty( Handle& handle, Property::Index index )
93 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
96 * Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
98 bool FindVisual( Property::Index targetIndex, RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
100 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
102 if ( (*iter)->index == targetIndex )
110 VisualProperty GetVisualProperty(
111 Internal::Control& controlImpl,
112 RegisteredVisualContainer& visuals,
113 const std::string& visualName,
114 Property::Key propertyKey )
116 #if defined(DEBUG_ENABLED)
117 std::ostringstream oss;
118 oss << "Control::GetVisualProperty(" << visualName << ", " << propertyKey << ")" << std::endl;
119 DALI_LOG_INFO( gLogFilter, Debug::General, oss.str().c_str() );
122 // Find visualName in the control
123 RegisteredVisualContainer::Iterator iter;
124 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
126 if ( (*iter)->visual.GetName() == visualName )
132 // Does either it's renderer or placement actor have an associated property?
133 if( iter != visuals.End() )
135 Actor placementActor = (*iter)->placementActor;
136 if( !placementActor )
138 placementActor = controlImpl.Self();
141 Property::Index index = placementActor.GetPropertyIndex( propertyKey );
142 if( index != Property::INVALID_INDEX )
144 // It's a placement actor property:
145 return VisualProperty( placementActor, index );
149 // Check if it is a renderer property:
150 if( placementActor.GetRendererCount() > 0 )
152 // @todo Need to use correct renderer index when placement actors
154 Renderer renderer = placementActor.GetRendererAt(0);
155 Property::Index index = renderer.GetPropertyIndex( propertyKey );
156 if( index != Property::INVALID_INDEX )
158 // It's a renderer property:
159 return VisualProperty( renderer, index );
164 std::ostringstream oss;
166 DALI_LOG_WARNING( "Control::GetVisualProperty(%s, %s) No renderers\n", visualName.c_str(), oss.str().c_str() );
171 return VisualProperty( handle, Property::INVALID_INDEX );
176 * Creates control through type registry
180 return Internal::Control::New();
184 * Performs actions as requested using the action name.
185 * @param[in] object The object on which to perform the action.
186 * @param[in] actionName The action to perform.
187 * @param[in] attributes The attributes with which to perfrom this action.
188 * @return true if action has been accepted by this control
190 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
191 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
195 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
197 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
200 // if cast succeeds there is an implementation so no need to check
201 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
209 * Connects a callback function with the object's signals.
210 * @param[in] object The object providing the signal.
211 * @param[in] tracker Used to disconnect the signal.
212 * @param[in] signalName The signal to connect to.
213 * @param[in] functor A newly allocated FunctorDelegate.
214 * @return True if the signal was connected.
215 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
217 const char* SIGNAL_KEY_EVENT = "keyEvent";
218 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
219 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
220 const char* SIGNAL_TAPPED = "tapped";
221 const char* SIGNAL_PANNED = "panned";
222 const char* SIGNAL_PINCHED = "pinched";
223 const char* SIGNAL_LONG_PRESSED = "longPressed";
224 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
226 Dali::BaseHandle handle( object );
228 bool connected( false );
229 Toolkit::Control control = Toolkit::Control::DownCast( handle );
232 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
235 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
237 controlImpl.KeyEventSignal().Connect( tracker, functor );
239 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
241 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
243 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
245 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
247 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
249 controlImpl.EnableGestureDetection( Gesture::Tap );
250 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
252 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
254 controlImpl.EnableGestureDetection( Gesture::Pan );
255 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
257 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
259 controlImpl.EnableGestureDetection( Gesture::Pinch );
260 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
262 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
264 controlImpl.EnableGestureDetection( Gesture::LongPress );
265 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
271 // Setup signals and actions using the type-registry.
272 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
274 // Note: Properties are registered separately below.
276 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
277 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
278 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
279 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
280 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
281 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
282 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
284 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
286 DALI_TYPE_REGISTRATION_END()
288 } // unnamed namespace
293 class Control::Impl : public ConnectionTracker
297 // Construction & Destruction
298 Impl(Control& controlImpl)
299 : mControlImpl( controlImpl ),
302 mBackgroundColor(Color::TRANSPARENT),
303 mStartingPinchScale( NULL ),
305 mPinchGestureDetector(),
306 mPanGestureDetector(),
307 mTapGestureDetector(),
308 mLongPressGestureDetector(),
309 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
310 mIsKeyboardNavigationSupported( false ),
311 mIsKeyboardFocusGroup( false )
317 // All gesture detectors will be destroyed so no need to disconnect.
318 delete mStartingPinchScale;
321 // Gesture Detection Methods
323 void PinchDetected(Actor actor, const PinchGesture& pinch)
325 mControlImpl.OnPinch(pinch);
328 void PanDetected(Actor actor, const PanGesture& pan)
330 mControlImpl.OnPan(pan);
333 void TapDetected(Actor actor, const TapGesture& tap)
335 mControlImpl.OnTap(tap);
338 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
340 mControlImpl.OnLongPress(longPress);
346 * Called when a property of an object of this type is set.
347 * @param[in] object The object whose property is set.
348 * @param[in] index The property index.
349 * @param[in] value The new property value.
351 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
353 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
357 Control& controlImpl( GetImplementation( control ) );
361 case Toolkit::Control::Property::STYLE_NAME:
363 controlImpl.SetStyleName( value.Get< std::string >() );
367 case Toolkit::Control::Property::BACKGROUND_COLOR:
369 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
370 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
374 case Toolkit::Control::Property::BACKGROUND_IMAGE:
376 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
377 Image image = Scripting::NewImage( value );
380 controlImpl.SetBackgroundImage( image );
384 // An empty map means the background is no longer required
385 controlImpl.ClearBackground();
390 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
392 if ( value.Get< bool >() )
394 controlImpl.SetKeyInputFocus();
398 controlImpl.ClearKeyInputFocus();
403 case Toolkit::Control::Property::BACKGROUND:
405 const Property::Map* map = value.GetMap();
408 controlImpl.SetBackground( *map );
412 // The background is not a property map, so we should clear the background
413 controlImpl.ClearBackground();
422 * Called to retrieve a property of an object of this type.
423 * @param[in] object The object whose property is to be retrieved.
424 * @param[in] index The property index.
425 * @return The current value of the property.
427 static Property::Value GetProperty( BaseObject* object, Property::Index index )
429 Property::Value value;
431 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
435 Control& controlImpl( GetImplementation( control ) );
439 case Toolkit::Control::Property::STYLE_NAME:
441 value = controlImpl.GetStyleName();
445 case Toolkit::Control::Property::BACKGROUND_COLOR:
447 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
448 value = controlImpl.GetBackgroundColor();
452 case Toolkit::Control::Property::BACKGROUND_IMAGE:
454 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
456 if( controlImpl.mImpl->mBackgroundVisual )
458 controlImpl.mImpl->mBackgroundVisual.CreatePropertyMap( map );
464 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
466 value = controlImpl.HasKeyInputFocus();
470 case Toolkit::Control::Property::BACKGROUND:
473 if( controlImpl.mImpl->mBackgroundVisual )
475 (controlImpl.mImpl->mBackgroundVisual).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 Toolkit::Visual::Base mBackgroundVisual; ///< The visual to render the background
494 Vector4 mBackgroundColor; ///< The color of the background visual
495 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
496 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
497 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
498 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
501 PinchGestureDetector mPinchGestureDetector;
502 PanGestureDetector mPanGestureDetector;
503 TapGestureDetector mTapGestureDetector;
504 LongPressGestureDetector mLongPressGestureDetector;
506 ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
507 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
508 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
510 // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
511 static const PropertyRegistration PROPERTY_1;
512 static const PropertyRegistration PROPERTY_2;
513 static const PropertyRegistration PROPERTY_3;
514 static const PropertyRegistration PROPERTY_4;
515 static const PropertyRegistration PROPERTY_5;
518 // Properties registered without macro to use specific member variables.
519 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
520 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
521 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
522 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
523 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
525 Toolkit::Control Control::New()
527 // Create the implementation, temporarily owned on stack
528 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
530 // Pass ownership to handle
531 Toolkit::Control handle( *controlImpl );
533 // Second-phase init of the implementation
534 // This can only be done after the CustomActor connection has been made...
535 controlImpl->Initialize();
545 void Control::SetStyleName( const std::string& styleName )
547 if( styleName != mImpl->mStyleName )
549 mImpl->mStyleName = styleName;
551 // Apply new style, if stylemanager is available
552 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
555 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
560 const std::string& Control::GetStyleName() const
562 return mImpl->mStyleName;
565 void Control::SetBackgroundColor( const Vector4& color )
567 Actor self( Self() );
568 mImpl->mBackgroundColor = color;
570 map[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::COLOR;
571 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
572 mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( map );
573 RegisterVisual( Toolkit::Control::Property::BACKGROUND, self, mImpl->mBackgroundVisual );
574 if( mImpl->mBackgroundVisual )
576 mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
580 Vector4 Control::GetBackgroundColor() const
582 return mImpl->mBackgroundColor;
585 void Control::SetBackground( const Property::Map& map )
587 Actor self( Self() );
588 mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( map );
589 RegisterVisual( Toolkit::Control::Property::BACKGROUND, self, mImpl->mBackgroundVisual );
590 if( mImpl->mBackgroundVisual )
592 mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
596 void Control::SetBackgroundImage( Image image )
598 Actor self( Self() );
599 mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
600 RegisterVisual( Toolkit::Control::Property::BACKGROUND, self, mImpl->mBackgroundVisual );
601 if( mImpl->mBackgroundVisual )
603 mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
607 void Control::ClearBackground()
609 Actor self( Self() );
610 mImpl->mBackgroundVisual.RemoveAndReset( self );
611 mImpl->mBackgroundColor = Color::TRANSPARENT;
614 void Control::EnableGestureDetection(Gesture::Type type)
616 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
618 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
619 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
620 mImpl->mPinchGestureDetector.Attach(Self());
623 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
625 mImpl->mPanGestureDetector = PanGestureDetector::New();
626 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
627 mImpl->mPanGestureDetector.Attach(Self());
630 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
632 mImpl->mTapGestureDetector = TapGestureDetector::New();
633 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
634 mImpl->mTapGestureDetector.Attach(Self());
637 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
639 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
640 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
641 mImpl->mLongPressGestureDetector.Attach(Self());
645 void Control::DisableGestureDetection(Gesture::Type type)
647 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
649 mImpl->mPinchGestureDetector.Detach(Self());
650 mImpl->mPinchGestureDetector.Reset();
653 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
655 mImpl->mPanGestureDetector.Detach(Self());
656 mImpl->mPanGestureDetector.Reset();
659 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
661 mImpl->mTapGestureDetector.Detach(Self());
662 mImpl->mTapGestureDetector.Reset();
665 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
667 mImpl->mLongPressGestureDetector.Detach(Self());
668 mImpl->mLongPressGestureDetector.Reset();
672 PinchGestureDetector Control::GetPinchGestureDetector() const
674 return mImpl->mPinchGestureDetector;
677 PanGestureDetector Control::GetPanGestureDetector() const
679 return mImpl->mPanGestureDetector;
682 TapGestureDetector Control::GetTapGestureDetector() const
684 return mImpl->mTapGestureDetector;
687 LongPressGestureDetector Control::GetLongPressGestureDetector() const
689 return mImpl->mLongPressGestureDetector;
692 void Control::SetKeyboardNavigationSupport(bool isSupported)
694 mImpl->mIsKeyboardNavigationSupported = isSupported;
697 bool Control::IsKeyboardNavigationSupported()
699 return mImpl->mIsKeyboardNavigationSupported;
702 void Control::SetKeyInputFocus()
704 if( Self().OnStage() )
706 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
710 bool Control::HasKeyInputFocus()
713 if( Self().OnStage() )
715 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
720 void Control::ClearKeyInputFocus()
722 if( Self().OnStage() )
724 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
728 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
730 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
732 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
733 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
736 bool Control::IsKeyboardFocusGroup()
738 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
741 void Control::AccessibilityActivate()
743 // Inform deriving classes
744 OnAccessibilityActivated();
747 void Control::KeyboardEnter()
749 // Inform deriving classes
753 void Control::RegisterVisual( Property::Index index, Actor& placementActor, Toolkit::Visual::Base& visual )
755 RegisterVisual( index, placementActor, visual, true );
758 void Control::RegisterVisual( Property::Index index, Actor& placementActor, Toolkit::Visual::Base& visual, bool enabled )
760 bool visualReplaced ( false );
761 Actor actorToRegister; // Null actor, replaced if placement actor not Self
764 if ( placementActor != self ) // Prevent increasing ref count if actor self
766 actorToRegister = placementActor;
769 if ( !mImpl->mVisuals.Empty() )
771 RegisteredVisualContainer::Iterator iter;
772 // Check if visual (index) is already registered. Replace if so.
773 if ( FindVisual( index, mImpl->mVisuals, iter ) )
775 if( (*iter)->visual && self.OnStage() )
777 if( (*iter)->placementActor )
779 (*iter)->visual.SetOffStage( (*iter)->placementActor );
783 (*iter)->visual.SetOffStage( self );
786 (*iter)->visual = visual;
787 (*iter)->placementActor = actorToRegister;
788 visualReplaced = true;
792 if ( !visualReplaced ) // New registration entry
794 mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, actorToRegister, enabled ) );
797 if( visual && self.OnStage() && enabled )
799 visual.SetOnStage( placementActor );
803 void Control::UnregisterVisual( Property::Index index )
805 RegisteredVisualContainer::Iterator iter;
806 if ( FindVisual( index, mImpl->mVisuals, iter ) )
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 )
833 (*iter)->enabled = enable;
834 Actor parentActor = Self();
835 if ( (*iter)->placementActor )
837 parentActor = (*iter)->placementActor;
840 if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
845 (*iter)->visual.SetOnStage( parentActor );
849 (*iter)->visual.SetOffStage( parentActor ); // No need to call if control not staged.
855 bool Control::IsVisualEnabled( Property::Index index ) const
857 RegisteredVisualContainer::Iterator iter;
858 if ( FindVisual( index, mImpl->mVisuals, iter ) )
860 return (*iter)->enabled;
865 Actor Control::GetPlacementActor( Property::Index index ) const
867 RegisteredVisualContainer::Iterator iter;
868 if ( FindVisual( index, mImpl->mVisuals, iter ) )
870 if( (*iter)->placementActor )
872 return (*iter)->placementActor;
883 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
885 Dali::Animation transition;
886 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
888 if( transitionData.Count() > 0 )
890 // Setup a Transition from TransitionData.
891 TransitionData::Iterator end = transitionData.End();
892 for( TransitionData::Iterator iter = transitionData.Begin() ;
893 iter != end; ++iter )
895 TransitionData::Animator* animator = (*iter);
896 VisualProperty visualProperty;
898 // Attempt to find the object name as a child actor
899 Actor child = Self().FindChildByName( animator->objectName );
902 Property::Index propertyIndex = child.GetPropertyIndex( animator->propertyKey );
903 visualProperty = VisualProperty( child, propertyIndex );
907 // Is it a placement actor/visual pair?;
908 visualProperty = GetVisualProperty( *this, mImpl->mVisuals,
909 animator->objectName,
910 animator->propertyKey );
913 if( visualProperty.handle && visualProperty.index != Property::INVALID_INDEX )
915 if( animator->animate == false )
917 if( animator->targetValue.GetType() != Property::NONE )
919 visualProperty.handle.SetProperty( visualProperty.index, animator->targetValue );
924 if( animator->initialValue.GetType() != Property::NONE )
926 visualProperty.handle.SetProperty( visualProperty.index, animator->initialValue );
931 // Create an animation with a default .1 second duration - the animators
932 // will automatically force it to the 'right' duration.
933 transition = Dali::Animation::New( 0.1f );
936 transition.AnimateTo( Property( visualProperty.handle, visualProperty.index ),
937 animator->targetValue,
938 animator->alphaFunction,
939 TimePeriod( animator->timePeriodDelay,
940 animator->timePeriodDuration ) );
950 bool Control::OnAccessibilityActivated()
952 return false; // Accessibility activation is not handled by default
955 bool Control::OnKeyboardEnter()
957 return false; // Keyboard enter is not handled by default
960 bool Control::OnAccessibilityPan(PanGesture gesture)
962 return false; // Accessibility pan gesture is not handled by default
965 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
967 return false; // Accessibility touch event is not handled by default
970 bool Control::OnAccessibilityValueChange(bool isIncrease)
972 return false; // Accessibility value change action is not handled by default
975 bool Control::OnAccessibilityZoom()
977 return false; // Accessibility zoom action is not handled by default
980 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
985 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
989 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
991 return mImpl->mKeyEventSignal;
994 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
996 return mImpl->mKeyInputFocusGainedSignal;
999 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
1001 return mImpl->mKeyInputFocusLostSignal;
1004 bool Control::EmitKeyEventSignal( const KeyEvent& event )
1006 // Guard against destruction during signal emission
1007 Dali::Toolkit::Control handle( GetOwner() );
1009 bool consumed = false;
1011 // signals are allocated dynamically when someone connects
1012 if ( !mImpl->mKeyEventSignal.Empty() )
1014 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
1019 // Notification for derived classes
1020 consumed = OnKeyEvent(event);
1026 Control::Control( ControlBehaviour behaviourFlags )
1027 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
1028 mImpl(new Impl(*this))
1030 mImpl->mFlags = behaviourFlags;
1033 void Control::Initialize()
1035 // Call deriving classes so initialised before styling is applied to them.
1038 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
1039 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
1041 Toolkit::StyleManager styleManager = StyleManager::Get();
1043 // if stylemanager is available
1046 StyleManager& styleManagerImpl = GetImpl( styleManager );
1048 // Register for style changes
1049 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
1051 // Apply the current style
1052 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
1056 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
1058 SetKeyboardNavigationSupport( true );
1062 void Control::OnInitialize()
1066 void Control::OnControlChildAdd( Actor& child )
1070 void Control::OnControlChildRemove( Actor& child )
1074 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
1076 // By default the control is only interested in theme (not font) changes
1077 if( styleManager && change == StyleChange::THEME_CHANGE )
1079 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1084 void Control::OnPinch(const PinchGesture& pinch)
1086 if( !( mImpl->mStartingPinchScale ) )
1089 mImpl->mStartingPinchScale = new Vector3;
1092 if( pinch.state == Gesture::Started )
1094 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1097 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1100 void Control::OnPan( const PanGesture& pan )
1104 void Control::OnTap(const TapGesture& tap)
1108 void Control::OnLongPress( const LongPressGesture& longPress )
1112 void Control::EmitKeyInputFocusSignal( bool focusGained )
1114 Dali::Toolkit::Control handle( GetOwner() );
1118 // signals are allocated dynamically when someone connects
1119 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
1121 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
1126 // signals are allocated dynamically when someone connects
1127 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
1129 mImpl->mKeyInputFocusLostSignal.Emit( handle );
1134 void Control::OnStageConnection( int depth )
1136 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1138 // Check whether the visual is empty, as it is allowed to register a placement actor without visual.
1139 if( (*iter)->visual && (*iter)->enabled )
1141 if( (*iter)->placementActor )
1143 (*iter)->visual.SetOnStage( (*iter)->placementActor );
1147 Actor self( Self() );
1148 (*iter)->visual.SetOnStage( self );
1154 void Control::OnStageDisconnection()
1156 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
1158 // Check whether the visual is empty, as it is allowed to register a placement actor without visual.
1159 if( (*iter)->visual )
1161 if( (*iter)->placementActor )
1163 (*iter)->visual.SetOffStage( (*iter)->placementActor );
1167 Actor self( Self() );
1168 (*iter)->visual.SetOffStage( self );
1174 void Control::OnKeyInputFocusGained()
1176 EmitKeyInputFocusSignal( true );
1179 void Control::OnKeyInputFocusLost()
1181 EmitKeyInputFocusSignal( false );
1184 void Control::OnChildAdd(Actor& child)
1186 // Notify derived classes.
1187 OnControlChildAdd( child );
1190 void Control::OnChildRemove(Actor& child)
1192 // Notify derived classes.
1193 OnControlChildRemove( child );
1196 void Control::OnSizeSet(const Vector3& targetSize)
1198 if( mImpl->mBackgroundVisual )
1200 Vector2 size( targetSize );
1201 mImpl->mBackgroundVisual.SetSize( size );
1205 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1207 // @todo size negotiate background to new size, animate as well?
1210 bool Control::OnTouchEvent(const TouchEvent& event)
1212 return false; // Do not consume
1215 bool Control::OnHoverEvent(const HoverEvent& event)
1217 return false; // Do not consume
1220 bool Control::OnKeyEvent(const KeyEvent& event)
1222 return false; // Do not consume
1225 bool Control::OnWheelEvent(const WheelEvent& event)
1227 return false; // Do not consume
1230 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1232 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1234 container.Add( Self().GetChildAt( i ), size );
1238 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1242 Vector3 Control::GetNaturalSize()
1244 if( mImpl->mBackgroundVisual )
1246 Vector2 naturalSize;
1247 mImpl->mBackgroundVisual.GetNaturalSize(naturalSize);
1248 return Vector3(naturalSize);
1250 return Vector3::ZERO;
1253 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1255 return CalculateChildSizeBase( child, dimension );
1258 float Control::GetHeightForWidth( float width )
1260 return GetHeightForWidthBase( width );
1263 float Control::GetWidthForHeight( float height )
1265 return GetWidthForHeightBase( height );
1268 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1270 return RelayoutDependentOnChildrenBase( dimension );
1273 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1277 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1281 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1283 mImpl->SignalConnected( slotObserver, callback );
1286 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1288 mImpl->SignalDisconnected( slotObserver, callback );
1291 Control& GetImplementation( Dali::Toolkit::Control& handle )
1293 CustomActorImpl& customInterface = handle.GetImplementation();
1294 // downcast to control
1295 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1299 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1301 const CustomActorImpl& customInterface = handle.GetImplementation();
1302 // downcast to control
1303 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1307 } // namespace Internal
1309 } // namespace Toolkit