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 "control-data-impl.h"
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/devel-api/object/handle-devel.h>
25 #include <dali/devel-api/scripting/enum-helper.h>
26 #include <dali/devel-api/scripting/scripting.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/public-api/object/type-registry-helper.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
34 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
35 #include <dali-toolkit/internal/styling/style-manager-impl.h>
36 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
37 #include <dali-toolkit/public-api/visuals/visual-properties.h>
38 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
39 #include <dali-toolkit/devel-api/controls/control-devel.h>
40 #include <dali-toolkit/devel-api/controls/control-wrapper-impl.h>
51 extern const Dali::Scripting::StringEnum ControlStateTable[];
52 extern const unsigned int ControlStateTableCount;
55 // Not static or anonymous - shared with other translation units
56 const Scripting::StringEnum ControlStateTable[] = {
57 { "NORMAL", Toolkit::DevelControl::NORMAL },
58 { "FOCUSED", Toolkit::DevelControl::FOCUSED },
59 { "DISABLED", Toolkit::DevelControl::DISABLED },
61 const unsigned int ControlStateTableCount = sizeof( ControlStateTable ) / sizeof( ControlStateTable[0] );
68 #if defined(DEBUG_ENABLED)
69 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
74 void Remove( Dictionary<T>& keyValues, const std::string& name )
76 keyValues.Remove(name);
79 void Remove( DictionaryKeys& keys, const std::string& name )
81 DictionaryKeys::iterator iter = std::find( keys.begin(), keys.end(), name );
82 if( iter != keys.end())
88 Toolkit::Visual::Type GetVisualTypeFromMap( const Property::Map& map )
90 Property::Value* typeValue = map.Find( Toolkit::Visual::Property::TYPE, VISUAL_TYPE );
91 Toolkit::Visual::Type type = Toolkit::Visual::IMAGE;
94 Scripting::GetEnumerationProperty( *typeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, type );
100 * Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
102 bool FindVisual( Property::Index targetIndex, const RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
104 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
106 if ( (*iter)->index == targetIndex )
114 void FindChangableVisuals( Dictionary<Property::Map>& stateVisualsToAdd,
115 Dictionary<Property::Map>& stateVisualsToChange,
116 DictionaryKeys& stateVisualsToRemove)
118 DictionaryKeys copyOfStateVisualsToRemove = stateVisualsToRemove;
120 for( DictionaryKeys::iterator iter = copyOfStateVisualsToRemove.begin();
121 iter != copyOfStateVisualsToRemove.end(); ++iter )
123 const std::string& visualName = (*iter);
124 Property::Map* toMap = stateVisualsToAdd.Find( visualName );
127 stateVisualsToChange.Add( visualName, *toMap );
128 stateVisualsToAdd.Remove( visualName );
129 Remove( stateVisualsToRemove, visualName );
134 Toolkit::Visual::Base GetVisualByName(
135 const RegisteredVisualContainer& visuals,
136 const std::string& visualName )
138 Toolkit::Visual::Base visualHandle;
140 RegisteredVisualContainer::Iterator iter;
141 for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
143 Toolkit::Visual::Base visual = (*iter)->visual;
144 if( visual && visual.GetName() == visualName )
146 visualHandle = visual;
154 * Move visual from source to destination container
156 void MoveVisual( RegisteredVisualContainer::Iterator sourceIter, RegisteredVisualContainer& source, RegisteredVisualContainer& destination )
158 Toolkit::Visual::Base visual = (*sourceIter)->visual;
161 RegisteredVisual* rv = source.Release( sourceIter );
162 destination.PushBack( rv );
167 * Performs actions as requested using the action name.
168 * @param[in] object The object on which to perform the action.
169 * @param[in] actionName The action to perform.
170 * @param[in] attributes The attributes with which to perfrom this action.
171 * @return true if action has been accepted by this control
173 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
174 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
178 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
180 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
183 // if cast succeeds there is an implementation so no need to check
184 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
192 * Connects a callback function with the object's signals.
193 * @param[in] object The object providing the signal.
194 * @param[in] tracker Used to disconnect the signal.
195 * @param[in] signalName The signal to connect to.
196 * @param[in] functor A newly allocated FunctorDelegate.
197 * @return True if the signal was connected.
198 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
200 const char* SIGNAL_KEY_EVENT = "keyEvent";
201 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
202 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
203 const char* SIGNAL_TAPPED = "tapped";
204 const char* SIGNAL_PANNED = "panned";
205 const char* SIGNAL_PINCHED = "pinched";
206 const char* SIGNAL_LONG_PRESSED = "longPressed";
207 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
209 Dali::BaseHandle handle( object );
211 bool connected( false );
212 Toolkit::Control control = Toolkit::Control::DownCast( handle );
215 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
218 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
220 controlImpl.KeyEventSignal().Connect( tracker, functor );
222 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
224 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
226 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
228 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
230 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
232 controlImpl.EnableGestureDetection( Gesture::Tap );
233 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
235 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
237 controlImpl.EnableGestureDetection( Gesture::Pan );
238 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
240 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
242 controlImpl.EnableGestureDetection( Gesture::Pinch );
243 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
245 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
247 controlImpl.EnableGestureDetection( Gesture::LongPress );
248 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
255 * Creates control through type registry
259 return Internal::Control::New();
261 // Setup signals and actions using the type-registry.
262 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
264 // Note: Properties are registered separately below.
266 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
267 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
268 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
269 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
270 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
271 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
272 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
274 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
276 DALI_TYPE_REGISTRATION_END()
279 * @brief Iterate through given container and setOffStage any visual found
281 * @param[in] container Container of visuals
282 * @param[in] parent Parent actor to remove visuals from
284 void SetVisualsOffStage( const RegisteredVisualContainer& container, Actor parent )
286 for( auto iter = container.Begin(), end = container.End() ; iter!= end; iter++)
288 if( (*iter)->visual )
290 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::SetOffStage Setting visual(%d) off stage\n", (*iter)->index );
291 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parent );
296 } // unnamed namespace
299 // Properties registered without macro to use specific member variables.
300 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
301 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
302 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
303 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
304 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
305 const PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "margin", Toolkit::Control::Property::MARGIN, Property::EXTENTS, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
306 const PropertyRegistration Control::Impl::PROPERTY_7( typeRegistration, "padding", Toolkit::Control::Property::PADDING, Property::EXTENTS, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
307 const PropertyRegistration Control::Impl::PROPERTY_8( typeRegistration, "tooltip", Toolkit::DevelControl::Property::TOOLTIP, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
308 const PropertyRegistration Control::Impl::PROPERTY_9( typeRegistration, "state", Toolkit::DevelControl::Property::STATE, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
309 const PropertyRegistration Control::Impl::PROPERTY_10( typeRegistration, "subState", Toolkit::DevelControl::Property::SUB_STATE, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
310 const PropertyRegistration Control::Impl::PROPERTY_11( typeRegistration, "leftFocusableActorId", Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
311 const PropertyRegistration Control::Impl::PROPERTY_12( typeRegistration, "rightFocusableActorId", Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID,Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
312 const PropertyRegistration Control::Impl::PROPERTY_13( typeRegistration, "upFocusableActorId", Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
313 const PropertyRegistration Control::Impl::PROPERTY_14( typeRegistration, "downFocusableActorId", Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
316 Control::Impl::Impl( Control& controlImpl )
317 : mControlImpl( controlImpl ),
318 mState( Toolkit::DevelControl::NORMAL ),
320 mLeftFocusableActorId( -1 ),
321 mRightFocusableActorId( -1 ),
322 mUpFocusableActorId( -1 ),
323 mDownFocusableActorId( -1 ),
325 mBackgroundColor(Color::TRANSPARENT),
326 mStartingPinchScale( NULL ),
327 mMargin( 0, 0, 0, 0 ),
328 mPadding( 0, 0, 0, 0 ),
330 mPinchGestureDetector(),
331 mPanGestureDetector(),
332 mTapGestureDetector(),
333 mLongPressGestureDetector(),
334 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
335 mIsKeyboardNavigationSupported( false ),
336 mIsKeyboardFocusGroup( false )
341 Control::Impl::~Impl()
343 // All gesture detectors will be destroyed so no need to disconnect.
344 delete mStartingPinchScale;
347 Control::Impl& Control::Impl::Get( Internal::Control& internalControl )
349 return *internalControl.mImpl;
352 const Control::Impl& Control::Impl::Get( const Internal::Control& internalControl )
354 return *internalControl.mImpl;
357 // Gesture Detection Methods
358 void Control::Impl::PinchDetected(Actor actor, const PinchGesture& pinch)
360 mControlImpl.OnPinch(pinch);
363 void Control::Impl::PanDetected(Actor actor, const PanGesture& pan)
365 mControlImpl.OnPan(pan);
368 void Control::Impl::TapDetected(Actor actor, const TapGesture& tap)
370 mControlImpl.OnTap(tap);
373 void Control::Impl::LongPressDetected(Actor actor, const LongPressGesture& longPress)
375 mControlImpl.OnLongPress(longPress);
378 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
380 RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::NOT_SET );
383 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, int depthIndex )
385 RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::SET, depthIndex );
388 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
390 RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::NOT_SET );
393 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled, int depthIndex )
395 RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::SET, depthIndex );
398 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex )
400 DALI_LOG_INFO( gLogFilter, Debug::Concise, "RegisterVisual:%d \n", index );
402 bool visualReplaced ( false );
403 Actor self = mControlImpl.Self();
405 // Set the depth index, if not set by caller this will be either the current visual depth, max depth of all visuals
407 int requiredDepthIndex = visual.GetDepthIndex();
409 if( depthIndexValueSet == DepthIndexValue::SET )
411 requiredDepthIndex = depthIndex;
414 // Visual replacement, existing visual should only be removed from stage when replacement ready.
415 if( !mVisuals.Empty() )
417 RegisteredVisualContainer::Iterator registeredVisualsiter;
418 // Check if visual (index) is already registered, this is the current visual.
419 if( FindVisual( index, mVisuals, registeredVisualsiter ) )
421 Toolkit::Visual::Base& currentRegisteredVisual = (*registeredVisualsiter)->visual;
422 if( currentRegisteredVisual )
424 // Store current visual depth index as may need to set the replacement visual to same depth
425 const int currentDepthIndex = (*registeredVisualsiter)->visual.GetDepthIndex();
427 // No longer required to know if the replaced visual's resources are ready
428 StopObservingVisual( currentRegisteredVisual );
430 // If control staged and visual enabled then visuals will be swapped once ready
431 if( self.OnStage() && enabled )
433 // Check if visual is currently in the process of being replaced ( is in removal container )
434 RegisteredVisualContainer::Iterator visualQueuedForRemoval;
435 if ( FindVisual( index, mRemoveVisuals, visualQueuedForRemoval ) )
437 // Visual with same index is already in removal container so current visual pending
438 // Only the the last requested visual will be displayed so remove current visual which is staged but not ready.
439 Toolkit::GetImplementation( currentRegisteredVisual ).SetOffStage( self );
440 mVisuals.Erase( registeredVisualsiter );
444 // current visual not already in removal container so add now.
445 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RegisterVisual Move current registered visual to removal Queue: %d \n", index );
446 MoveVisual( registeredVisualsiter, mVisuals, mRemoveVisuals );
451 // Control not staged or visual disabled so can just erase from registered visuals and new visual will be added later.
452 mVisuals.Erase( registeredVisualsiter );
455 // If we've not set the depth-index value and the new visual does not have a depth index applied to it, then use the previously set depth-index for this index
456 if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
457 ( visual.GetDepthIndex() == 0 ) )
459 requiredDepthIndex = currentDepthIndex;
463 visualReplaced = true;
467 // If not set, set the name of the visual to the same name as the control's property.
468 // ( If the control has been type registered )
469 if( visual.GetName().empty() )
473 std::string visualName = self.GetPropertyName( index );
474 if( !visualName.empty() )
476 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Setting visual name for property %d to %s\n",
477 index, visualName.c_str() );
478 visual.SetName( visualName );
481 catch( Dali::DaliException e )
483 DALI_LOG_WARNING( "Attempting to register visual without a registered property, index: %d\n", index );
487 if( !visualReplaced ) // New registration entry
489 // monitor when the visual resources are ready
490 StartObservingVisual( visual );
492 // If we've not set the depth-index value, we have more than one visual and the visual does not have a depth index, then set it to be the highest
493 if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
494 ( mVisuals.Size() > 0 ) &&
495 ( visual.GetDepthIndex() == 0 ) )
497 int maxDepthIndex = std::numeric_limits< int >::min();
499 RegisteredVisualContainer::ConstIterator iter;
500 const RegisteredVisualContainer::ConstIterator endIter = mVisuals.End();
501 for ( iter = mVisuals.Begin(); iter != endIter; iter++ )
503 const int visualDepthIndex = (*iter)->visual.GetDepthIndex();
504 if ( visualDepthIndex > maxDepthIndex )
506 maxDepthIndex = visualDepthIndex;
509 ++maxDepthIndex; // Add one to the current maximum depth index so that our added visual appears on top
510 requiredDepthIndex = std::max( 0, maxDepthIndex ); // Start at zero if maxDepth index belongs to a background
516 // Set determined depth index
517 visual.SetDepthIndex( requiredDepthIndex );
519 // Monitor when the visual resources are ready
520 StartObservingVisual( visual );
522 DALI_LOG_INFO( gLogFilter, Debug::Concise, "New Visual registration index[%d] depth[%d]\n", index, requiredDepthIndex );
523 RegisteredVisual* newRegisteredVisual = new RegisteredVisual( index, visual,
524 ( enabled == VisualState::ENABLED ? true : false ),
525 ( visualReplaced && enabled ) ) ;
526 mVisuals.PushBack( newRegisteredVisual );
528 // Put on stage if enabled and the control is already on the stage
529 // Visual must be set on stage for the renderer to be created and the ResourceReady triggered.
530 if( ( enabled == VisualState::ENABLED ) && self.OnStage() )
532 Toolkit::GetImplementation(visual).SetOnStage( self );
536 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n", visual.GetName().c_str(), index, enabled?"true":"false" );
539 void Control::Impl::UnregisterVisual( Property::Index index )
541 RegisteredVisualContainer::Iterator iter;
542 if ( FindVisual( index, mVisuals, iter ) )
544 // stop observing visual
545 StopObservingVisual( (*iter)->visual );
547 Actor self( mControlImpl.Self() );
548 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
549 (*iter)->visual.Reset();
550 mVisuals.Erase( iter );
553 if( FindVisual( index, mRemoveVisuals, iter ) )
555 Actor self( mControlImpl.Self() );
556 Toolkit::GetImplementation( (*iter)->visual ).SetOffStage( self );
557 (*iter)->pending = false;
558 (*iter)->visual.Reset();
559 mRemoveVisuals.Erase( iter );
563 Toolkit::Visual::Base Control::Impl::GetVisual( Property::Index index ) const
565 RegisteredVisualContainer::Iterator iter;
566 if ( FindVisual( index, mVisuals, iter ) )
568 return (*iter)->visual;
571 return Toolkit::Visual::Base();
574 void Control::Impl::EnableVisual( Property::Index index, bool enable )
576 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual (%d)\n", index);
578 RegisteredVisualContainer::Iterator iter;
579 if ( FindVisual( index, mVisuals, iter ) )
581 if ( (*iter)->enabled == enable )
583 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual %s(%d) already %s\n", (*iter)->visual.GetName().c_str(), index, enable?"enabled":"disabled");
587 (*iter)->enabled = enable;
588 Actor parentActor = mControlImpl.Self();
589 if ( mControlImpl.Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
593 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) on stage \n", (*iter)->visual.GetName().c_str(), index );
594 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
598 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) off stage \n", (*iter)->visual.GetName().c_str(), index );
599 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
605 bool Control::Impl::IsVisualEnabled( Property::Index index ) const
607 RegisteredVisualContainer::Iterator iter;
608 if ( FindVisual( index, mVisuals, iter ) )
610 return (*iter)->enabled;
615 void Control::Impl::StopObservingVisual( Toolkit::Visual::Base& visual )
617 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
619 // Stop observing the visual
620 visualImpl.RemoveResourceObserver( *this );
623 void Control::Impl::StartObservingVisual( Toolkit::Visual::Base& visual)
625 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
627 // start observing the visual for resource ready
628 visualImpl.AddResourceObserver( *this );
631 // Called by a Visual when it's resource is ready
632 void Control::Impl::ResourceReady( Visual::Base& object)
634 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady replacements pending[%d]\n", mRemoveVisuals.Count() );
636 Actor self = mControlImpl.Self();
638 // A resource is ready, find resource in the registered visuals container and get its index
639 for( auto registeredIter = mVisuals.Begin(), end = mVisuals.End(); registeredIter != end; ++registeredIter )
641 Internal::Visual::Base& registeredVisualImpl = Toolkit::GetImplementation( (*registeredIter)->visual );
643 if( &object == ®isteredVisualImpl )
645 RegisteredVisualContainer::Iterator visualToRemoveIter;
646 // Find visual with the same index in the removal container
647 // Set if off stage as it's replacement is now ready.
648 // Remove if from removal list as now removed from stage.
649 // Set Pending flag on the ready visual to false as now ready.
650 if( FindVisual( (*registeredIter)->index, mRemoveVisuals, visualToRemoveIter ) )
652 (*registeredIter)->pending = false;
653 Toolkit::GetImplementation( (*visualToRemoveIter)->visual ).SetOffStage( self );
654 mRemoveVisuals.Erase( visualToRemoveIter );
660 // A visual is ready so control may need relayouting
661 mControlImpl.RelayoutRequest();
663 // Emit signal if all enabled visuals registered by the control are ready.
664 if( IsResourceReady() )
666 Dali::Toolkit::Control handle( mControlImpl.GetOwner() );
667 mResourceReadySignal.Emit( handle );
671 bool Control::Impl::IsResourceReady() const
673 // Iterate through and check all the enabled visuals are ready
674 for( auto visualIter = mVisuals.Begin();
675 visualIter != mVisuals.End(); ++visualIter )
677 const Toolkit::Visual::Base visual = (*visualIter)->visual;
678 const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
680 // one of the enabled visuals is not ready
681 if( !visualImpl.IsResourceReady() && (*visualIter)->enabled )
690 Dali::Animation Control::Impl::CreateTransition( const Toolkit::TransitionData& handle )
692 Dali::Animation transition;
693 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
695 if( transitionData.Count() > 0 )
697 // Setup a Transition from TransitionData.
698 TransitionData::Iterator end = transitionData.End();
699 for( TransitionData::Iterator iter = transitionData.Begin() ;
700 iter != end; ++iter )
702 TransitionData::Animator* animator = (*iter);
704 Toolkit::Visual::Base visual = GetVisualByName( mVisuals, animator->objectName );
708 #if defined(DEBUG_ENABLED)
709 Dali::TypeInfo typeInfo;
710 ControlWrapper* controlWrapperImpl = dynamic_cast<ControlWrapper*>(&mControlImpl);
711 if( controlWrapperImpl )
713 typeInfo = controlWrapperImpl->GetTypeInfo();
716 DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Found %s visual for %s\n",
717 visual.GetName().c_str(), typeInfo?typeInfo.GetName().c_str():"Unknown" );
719 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
720 visualImpl.AnimateProperty( transition, *animator );
724 DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Could not find visual. Trying actors");
725 // Otherwise, try any actor children of control (Including the control)
726 Actor child = mControlImpl.Self().FindChildByName( animator->objectName );
729 Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
730 if( propertyIndex != Property::INVALID_INDEX )
732 if( animator->animate == false )
734 if( animator->targetValue.GetType() != Property::NONE )
736 child.SetProperty( propertyIndex, animator->targetValue );
739 else // animate the property
741 if( animator->initialValue.GetType() != Property::NONE )
743 child.SetProperty( propertyIndex, animator->initialValue );
748 transition = Dali::Animation::New( 0.1f );
751 transition.AnimateTo( Property( child, propertyIndex ),
752 animator->targetValue,
753 animator->alphaFunction,
754 TimePeriod( animator->timePeriodDelay,
755 animator->timePeriodDuration ) );
766 void Control::Impl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
768 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
772 Control& controlImpl( GetImplementation( control ) );
776 case Toolkit::Control::Property::STYLE_NAME:
778 controlImpl.SetStyleName( value.Get< std::string >() );
782 case Toolkit::DevelControl::Property::STATE:
784 bool withTransitions=true;
785 const Property::Value* valuePtr=&value;
786 Property::Map* map = value.GetMap();
789 Property::Value* value2 = map->Find("withTransitions");
792 withTransitions = value2->Get<bool>();
795 valuePtr = map->Find("state");
800 Toolkit::DevelControl::State state( controlImpl.mImpl->mState );
801 if( Scripting::GetEnumerationProperty< Toolkit::DevelControl::State >( *valuePtr, ControlStateTable, ControlStateTableCount, state ) )
803 controlImpl.mImpl->SetState( state, withTransitions );
809 case Toolkit::DevelControl::Property::SUB_STATE:
811 std::string subState;
812 if( value.Get( subState ) )
814 controlImpl.mImpl->SetSubState( subState );
819 case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
822 if( value.Get( focusId ) )
824 controlImpl.mImpl->mLeftFocusableActorId = focusId;
829 case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
832 if( value.Get( focusId ) )
834 controlImpl.mImpl->mRightFocusableActorId = focusId;
839 case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
842 if( value.Get( focusId ) )
844 controlImpl.mImpl->mUpFocusableActorId = focusId;
849 case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
852 if( value.Get( focusId ) )
854 controlImpl.mImpl->mDownFocusableActorId = focusId;
859 case Toolkit::Control::Property::BACKGROUND_COLOR:
861 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
862 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
866 case Toolkit::Control::Property::BACKGROUND_IMAGE:
868 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
869 Image image = Scripting::NewImage( value );
872 controlImpl.SetBackgroundImage( image );
876 // An empty image means the background is no longer required
877 controlImpl.ClearBackground();
882 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
884 if ( value.Get< bool >() )
886 controlImpl.SetKeyInputFocus();
890 controlImpl.ClearKeyInputFocus();
895 case Toolkit::Control::Property::BACKGROUND:
899 const Property::Map* map = value.GetMap();
900 if( map && !map->Empty() )
902 controlImpl.SetBackground( *map );
904 else if( value.Get( url ) )
906 // don't know the size to load
907 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
910 controlImpl.mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
913 else if( value.Get( color ) )
915 controlImpl.SetBackgroundColor(color);
919 // The background is an empty property map, so we should clear the background
920 controlImpl.ClearBackground();
925 case Toolkit::Control::Property::MARGIN:
928 if( value.Get( margin ) )
930 controlImpl.mImpl->SetMargin( margin );
935 case Toolkit::Control::Property::PADDING:
938 if( value.Get( padding ) )
940 controlImpl.mImpl->SetPadding( padding );
945 case Toolkit::DevelControl::Property::TOOLTIP:
947 TooltipPtr& tooltipPtr = controlImpl.mImpl->mTooltip;
950 tooltipPtr = Tooltip::New( control );
952 tooltipPtr->SetProperties( value );
960 Property::Value Control::Impl::GetProperty( BaseObject* object, Property::Index index )
962 Property::Value value;
964 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
968 Control& controlImpl( GetImplementation( control ) );
972 case Toolkit::Control::Property::STYLE_NAME:
974 value = controlImpl.GetStyleName();
978 case Toolkit::DevelControl::Property::STATE:
980 value = controlImpl.mImpl->mState;
984 case Toolkit::DevelControl::Property::SUB_STATE:
986 value = controlImpl.mImpl->mSubStateName;
990 case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
992 value = controlImpl.mImpl->mLeftFocusableActorId;
996 case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
998 value = controlImpl.mImpl->mRightFocusableActorId;
1002 case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
1004 value = controlImpl.mImpl->mUpFocusableActorId;
1008 case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
1010 value = controlImpl.mImpl->mDownFocusableActorId;
1014 case Toolkit::Control::Property::BACKGROUND_COLOR:
1016 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
1017 value = controlImpl.GetBackgroundColor();
1021 case Toolkit::Control::Property::BACKGROUND_IMAGE:
1023 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
1025 Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
1028 visual.CreatePropertyMap( map );
1034 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
1036 value = controlImpl.HasKeyInputFocus();
1040 case Toolkit::Control::Property::BACKGROUND:
1043 Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
1046 visual.CreatePropertyMap( map );
1053 case Toolkit::Control::Property::MARGIN:
1055 value = controlImpl.mImpl->GetMargin();
1059 case Toolkit::Control::Property::PADDING:
1061 value = controlImpl.mImpl->GetPadding();
1065 case Toolkit::DevelControl::Property::TOOLTIP:
1068 if( controlImpl.mImpl->mTooltip )
1070 controlImpl.mImpl->mTooltip->CreatePropertyMap( map );
1082 void Control::Impl::CopyInstancedProperties( RegisteredVisualContainer& visuals, Dictionary<Property::Map>& instancedProperties )
1084 for(RegisteredVisualContainer::Iterator iter = visuals.Begin(); iter!= visuals.End(); iter++)
1086 if( (*iter)->visual )
1088 Property::Map instanceMap;
1089 Toolkit::GetImplementation((*iter)->visual).CreateInstancePropertyMap(instanceMap);
1090 instancedProperties.Add( (*iter)->visual.GetName(), instanceMap );
1096 void Control::Impl::RemoveVisual( RegisteredVisualContainer& visuals, const std::string& visualName )
1098 Actor self( mControlImpl.Self() );
1100 for ( RegisteredVisualContainer::Iterator visualIter = visuals.Begin();
1101 visualIter != visuals.End(); ++visualIter )
1103 Toolkit::Visual::Base visual = (*visualIter)->visual;
1104 if( visual && visual.GetName() == visualName )
1106 Toolkit::GetImplementation(visual).SetOffStage( self );
1107 (*visualIter)->visual.Reset();
1108 visuals.Erase( visualIter );
1114 void Control::Impl::RemoveVisuals( RegisteredVisualContainer& visuals, DictionaryKeys& removeVisuals )
1116 Actor self( mControlImpl.Self() );
1117 for( DictionaryKeys::iterator iter = removeVisuals.begin(); iter != removeVisuals.end(); ++iter )
1119 const std::string visualName = *iter;
1120 RemoveVisual( visuals, visualName );
1124 void Control::Impl::RecreateChangedVisuals( Dictionary<Property::Map>& stateVisualsToChange,
1125 Dictionary<Property::Map>& instancedProperties )
1127 Dali::CustomActor handle( mControlImpl.GetOwner() );
1128 for( Dictionary<Property::Map>::iterator iter = stateVisualsToChange.Begin();
1129 iter != stateVisualsToChange.End(); ++iter )
1131 const std::string& visualName = (*iter).key;
1132 const Property::Map& toMap = (*iter).entry;
1134 // is it a candidate for re-creation?
1135 bool recreate = false;
1137 Toolkit::Visual::Base visual = GetVisualByName( mVisuals, visualName );
1140 Property::Map fromMap;
1141 visual.CreatePropertyMap( fromMap );
1143 Toolkit::Visual::Type fromType = GetVisualTypeFromMap( fromMap );
1144 Toolkit::Visual::Type toType = GetVisualTypeFromMap( toMap );
1146 if( fromType != toType )
1152 if( fromType == Toolkit::Visual::IMAGE || fromType == Toolkit::Visual::N_PATCH
1153 || fromType == Toolkit::Visual::SVG || fromType == Toolkit::Visual::ANIMATED_IMAGE )
1155 Property::Value* fromUrl = fromMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
1156 Property::Value* toUrl = toMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
1158 if( fromUrl && toUrl )
1160 std::string fromUrlString;
1161 std::string toUrlString;
1162 fromUrl->Get(fromUrlString);
1163 toUrl->Get(toUrlString);
1165 if( fromUrlString != toUrlString )
1173 const Property::Map* instancedMap = instancedProperties.FindConst( visualName );
1174 if( recreate || instancedMap )
1176 RemoveVisual( mVisuals, visualName );
1177 Style::ApplyVisual( handle, visualName, toMap, instancedMap );
1181 // @todo check to see if we can apply toMap without recreating the visual
1182 // e.g. by setting only animatable properties
1183 // For now, recreate all visuals, but merge in instance data.
1184 RemoveVisual( mVisuals, visualName );
1185 Style::ApplyVisual( handle, visualName, toMap, instancedMap );
1191 void Control::Impl::ReplaceStateVisualsAndProperties( const StylePtr oldState, const StylePtr newState, const std::string& subState )
1193 // Collect all old visual names
1194 DictionaryKeys stateVisualsToRemove;
1197 oldState->visuals.GetKeys( stateVisualsToRemove );
1198 if( ! subState.empty() )
1200 const StylePtr* oldSubState = oldState->subStates.FindConst(subState);
1203 DictionaryKeys subStateVisualsToRemove;
1204 (*oldSubState)->visuals.GetKeys( subStateVisualsToRemove );
1205 Merge( stateVisualsToRemove, subStateVisualsToRemove );
1210 // Collect all new visual properties
1211 Dictionary<Property::Map> stateVisualsToAdd;
1214 stateVisualsToAdd = newState->visuals;
1215 if( ! subState.empty() )
1217 const StylePtr* newSubState = newState->subStates.FindConst(subState);
1220 stateVisualsToAdd.Merge( (*newSubState)->visuals );
1225 // If a name is in both add/remove, move it to change list.
1226 Dictionary<Property::Map> stateVisualsToChange;
1227 FindChangableVisuals( stateVisualsToAdd, stateVisualsToChange, stateVisualsToRemove);
1229 // Copy instanced properties (e.g. text label) of current visuals
1230 Dictionary<Property::Map> instancedProperties;
1231 CopyInstancedProperties( mVisuals, instancedProperties );
1233 // For each visual in remove list, remove from mVisuals
1234 RemoveVisuals( mVisuals, stateVisualsToRemove );
1236 // For each visual in add list, create and add to mVisuals
1237 Dali::CustomActor handle( mControlImpl.GetOwner() );
1238 Style::ApplyVisuals( handle, stateVisualsToAdd, instancedProperties );
1240 // For each visual in change list, if it requires a new visual,
1241 // remove old visual, create and add to mVisuals
1242 RecreateChangedVisuals( stateVisualsToChange, instancedProperties );
1245 void Control::Impl::SetState( DevelControl::State newState, bool withTransitions )
1247 DevelControl::State oldState = mState;
1248 Dali::CustomActor handle( mControlImpl.GetOwner() );
1249 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Control::Impl::SetState: %s\n",
1250 (mState == DevelControl::NORMAL ? "NORMAL" :(
1251 mState == DevelControl::FOCUSED ?"FOCUSED" : (
1252 mState == DevelControl::DISABLED?"DISABLED":"NONE" ))));
1254 if( mState != newState )
1256 // If mState was Disabled, and new state is Focused, should probably
1257 // store that fact, e.g. in another property that FocusManager can access.
1260 // Trigger state change and transitions
1261 // Apply new style, if stylemanager is available
1262 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1265 const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1269 std::string oldStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( oldState, ControlStateTable, ControlStateTableCount );
1270 std::string newStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( newState, ControlStateTable, ControlStateTableCount );
1272 const StylePtr* newStateStyle = stylePtr->subStates.Find( newStateName );
1273 const StylePtr* oldStateStyle = stylePtr->subStates.Find( oldStateName );
1274 if( oldStateStyle && newStateStyle )
1276 // Only change if both state styles exist
1277 ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, mSubStateName );
1284 void Control::Impl::SetSubState( const std::string& subStateName, bool withTransitions )
1286 if( mSubStateName != subStateName )
1288 // Get existing sub-state visuals, and unregister them
1289 Dali::CustomActor handle( mControlImpl.GetOwner() );
1291 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1294 const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1298 std::string stateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( mState, ControlStateTable, ControlStateTableCount );
1300 const StylePtr* state = stylePtr->subStates.Find( stateName );
1303 StylePtr stateStyle(*state);
1305 const StylePtr* newStateStyle = stateStyle->subStates.Find( subStateName );
1306 const StylePtr* oldStateStyle = stateStyle->subStates.Find( mSubStateName );
1307 if( oldStateStyle && newStateStyle )
1310 ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, empty );
1316 mSubStateName = subStateName;
1320 void Control::Impl::OnStageDisconnection()
1322 Actor self = mControlImpl.Self();
1324 // Any visuals set for replacement but not yet ready should still be registered.
1325 // Reason: If a request was made to register a new visual but the control removed from stage before visual was ready
1326 // then when this control appears back on stage it should use that new visual.
1328 // Iterate through all registered visuals and set off stage
1329 SetVisualsOffStage( mVisuals, self );
1331 // Visuals pending replacement can now be taken out of the removal list and set off stage
1332 // Iterate through all replacement visuals and add to a move queue then set off stage
1333 for( auto removalIter = mRemoveVisuals.Begin(), end = mRemoveVisuals.End(); removalIter != end; removalIter++ )
1335 Toolkit::GetImplementation((*removalIter)->visual).SetOffStage( self );
1338 for( auto replacedIter = mVisuals.Begin(), end = mVisuals.End(); replacedIter != end; replacedIter++ )
1340 (*replacedIter)->pending = false;
1343 mRemoveVisuals.Clear();
1346 void Control::Impl::SetMargin( Extents margin )
1348 mControlImpl.mImpl->mMargin = margin;
1351 Extents Control::Impl::GetMargin() const
1353 return mControlImpl.mImpl->mMargin;
1356 void Control::Impl::SetPadding( Extents padding )
1358 mControlImpl.mImpl->mPadding = padding;
1361 Extents Control::Impl::GetPadding() const
1363 return mControlImpl.mImpl->mPadding;
1366 } // namespace Internal
1368 } // namespace Toolkit