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 mKeyInputFocusGainedSignal(),
331 mKeyInputFocusLostSignal(),
332 mResourceReadySignal(),
333 mPinchGestureDetector(),
334 mPanGestureDetector(),
335 mTapGestureDetector(),
336 mLongPressGestureDetector(),
337 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
338 mIsKeyboardNavigationSupported( false ),
339 mIsKeyboardFocusGroup( false )
343 Control::Impl::~Impl()
345 // All gesture detectors will be destroyed so no need to disconnect.
346 delete mStartingPinchScale;
349 Control::Impl& Control::Impl::Get( Internal::Control& internalControl )
351 return *internalControl.mImpl;
354 const Control::Impl& Control::Impl::Get( const Internal::Control& internalControl )
356 return *internalControl.mImpl;
359 // Gesture Detection Methods
360 void Control::Impl::PinchDetected(Actor actor, const PinchGesture& pinch)
362 mControlImpl.OnPinch(pinch);
365 void Control::Impl::PanDetected(Actor actor, const PanGesture& pan)
367 mControlImpl.OnPan(pan);
370 void Control::Impl::TapDetected(Actor actor, const TapGesture& tap)
372 mControlImpl.OnTap(tap);
375 void Control::Impl::LongPressDetected(Actor actor, const LongPressGesture& longPress)
377 mControlImpl.OnLongPress(longPress);
380 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
382 RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::NOT_SET );
385 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, int depthIndex )
387 RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::SET, depthIndex );
390 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
392 RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::NOT_SET );
395 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled, int depthIndex )
397 RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::SET, depthIndex );
400 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex )
402 DALI_LOG_INFO( gLogFilter, Debug::Concise, "RegisterVisual:%d \n", index );
404 bool visualReplaced ( false );
405 Actor self = mControlImpl.Self();
407 // Set the depth index, if not set by caller this will be either the current visual depth, max depth of all visuals
409 int requiredDepthIndex = visual.GetDepthIndex();
411 if( depthIndexValueSet == DepthIndexValue::SET )
413 requiredDepthIndex = depthIndex;
416 // Visual replacement, existing visual should only be removed from stage when replacement ready.
417 if( !mVisuals.Empty() )
419 RegisteredVisualContainer::Iterator registeredVisualsiter;
420 // Check if visual (index) is already registered, this is the current visual.
421 if( FindVisual( index, mVisuals, registeredVisualsiter ) )
423 Toolkit::Visual::Base& currentRegisteredVisual = (*registeredVisualsiter)->visual;
424 if( currentRegisteredVisual )
426 // Store current visual depth index as may need to set the replacement visual to same depth
427 const int currentDepthIndex = (*registeredVisualsiter)->visual.GetDepthIndex();
429 // No longer required to know if the replaced visual's resources are ready
430 StopObservingVisual( currentRegisteredVisual );
432 // If control staged and visual enabled then visuals will be swapped once ready
433 if( self.OnStage() && enabled )
435 // Check if visual is currently in the process of being replaced ( is in removal container )
436 RegisteredVisualContainer::Iterator visualQueuedForRemoval;
437 if ( FindVisual( index, mRemoveVisuals, visualQueuedForRemoval ) )
439 // Visual with same index is already in removal container so current visual pending
440 // Only the the last requested visual will be displayed so remove current visual which is staged but not ready.
441 Toolkit::GetImplementation( currentRegisteredVisual ).SetOffStage( self );
442 mVisuals.Erase( registeredVisualsiter );
446 // current visual not already in removal container so add now.
447 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RegisterVisual Move current registered visual to removal Queue: %d \n", index );
448 MoveVisual( registeredVisualsiter, mVisuals, mRemoveVisuals );
453 // Control not staged or visual disabled so can just erase from registered visuals and new visual will be added later.
454 mVisuals.Erase( registeredVisualsiter );
457 // 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
458 if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
459 ( visual.GetDepthIndex() == 0 ) )
461 requiredDepthIndex = currentDepthIndex;
465 visualReplaced = true;
469 // If not set, set the name of the visual to the same name as the control's property.
470 // ( If the control has been type registered )
471 if( visual.GetName().empty() )
475 std::string visualName = self.GetPropertyName( index );
476 if( !visualName.empty() )
478 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Setting visual name for property %d to %s\n",
479 index, visualName.c_str() );
480 visual.SetName( visualName );
483 catch( Dali::DaliException e )
485 DALI_LOG_WARNING( "Attempting to register visual without a registered property, index: %d\n", index );
489 if( !visualReplaced ) // New registration entry
491 // 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
492 if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
493 ( mVisuals.Size() > 0 ) &&
494 ( visual.GetDepthIndex() == 0 ) )
496 int maxDepthIndex = std::numeric_limits< int >::min();
498 RegisteredVisualContainer::ConstIterator iter;
499 const RegisteredVisualContainer::ConstIterator endIter = mVisuals.End();
500 for ( iter = mVisuals.Begin(); iter != endIter; iter++ )
502 const int visualDepthIndex = (*iter)->visual.GetDepthIndex();
503 if ( visualDepthIndex > maxDepthIndex )
505 maxDepthIndex = visualDepthIndex;
508 ++maxDepthIndex; // Add one to the current maximum depth index so that our added visual appears on top
509 requiredDepthIndex = std::max( 0, maxDepthIndex ); // Start at zero if maxDepth index belongs to a background
515 // Set determined depth index
516 visual.SetDepthIndex( requiredDepthIndex );
518 // Monitor when the visual resources are ready
519 StartObservingVisual( visual );
521 DALI_LOG_INFO( gLogFilter, Debug::Concise, "New Visual registration index[%d] depth[%d]\n", index, requiredDepthIndex );
522 RegisteredVisual* newRegisteredVisual = new RegisteredVisual( index, visual,
523 ( enabled == VisualState::ENABLED ? true : false ),
524 ( visualReplaced && enabled ) ) ;
525 mVisuals.PushBack( newRegisteredVisual );
527 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
528 // Put on stage if enabled and the control is already on the stage
529 if( ( enabled == VisualState::ENABLED ) && self.OnStage() )
531 visualImpl.SetOnStage( self );
533 else if( visualImpl.IsResourceReady() ) // When not being staged, check if visual already 'ResourceReady' before it was Registered. ( Resource may have been loaded already )
535 ResourceReady( visualImpl );
540 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n", visual.GetName().c_str(), index, enabled?"true":"false" );
543 void Control::Impl::UnregisterVisual( Property::Index index )
545 RegisteredVisualContainer::Iterator iter;
546 if ( FindVisual( index, mVisuals, iter ) )
548 // stop observing visual
549 StopObservingVisual( (*iter)->visual );
551 Actor self( mControlImpl.Self() );
552 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
553 (*iter)->visual.Reset();
554 mVisuals.Erase( iter );
557 if( FindVisual( index, mRemoveVisuals, iter ) )
559 Actor self( mControlImpl.Self() );
560 Toolkit::GetImplementation( (*iter)->visual ).SetOffStage( self );
561 (*iter)->pending = false;
562 (*iter)->visual.Reset();
563 mRemoveVisuals.Erase( iter );
567 Toolkit::Visual::Base Control::Impl::GetVisual( Property::Index index ) const
569 RegisteredVisualContainer::Iterator iter;
570 if ( FindVisual( index, mVisuals, iter ) )
572 return (*iter)->visual;
575 return Toolkit::Visual::Base();
578 void Control::Impl::EnableVisual( Property::Index index, bool enable )
580 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual (%d)\n", index);
582 RegisteredVisualContainer::Iterator iter;
583 if ( FindVisual( index, mVisuals, iter ) )
585 if ( (*iter)->enabled == enable )
587 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual %s(%d) already %s\n", (*iter)->visual.GetName().c_str(), index, enable?"enabled":"disabled");
591 (*iter)->enabled = enable;
592 Actor parentActor = mControlImpl.Self();
593 if ( mControlImpl.Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
597 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) on stage \n", (*iter)->visual.GetName().c_str(), index );
598 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
602 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) off stage \n", (*iter)->visual.GetName().c_str(), index );
603 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
609 bool Control::Impl::IsVisualEnabled( Property::Index index ) const
611 RegisteredVisualContainer::Iterator iter;
612 if ( FindVisual( index, mVisuals, iter ) )
614 return (*iter)->enabled;
619 void Control::Impl::StopObservingVisual( Toolkit::Visual::Base& visual )
621 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
623 // Stop observing the visual
624 visualImpl.RemoveResourceObserver( *this );
627 void Control::Impl::StartObservingVisual( Toolkit::Visual::Base& visual)
629 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
631 // start observing the visual for resource ready
632 visualImpl.AddResourceObserver( *this );
635 // Called by a Visual when it's resource is ready
636 void Control::Impl::ResourceReady( Visual::Base& object)
638 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady replacements pending[%d]\n", mRemoveVisuals.Count() );
640 Actor self = mControlImpl.Self();
642 // A resource is ready, find resource in the registered visuals container and get its index
643 for( auto registeredIter = mVisuals.Begin(), end = mVisuals.End(); registeredIter != end; ++registeredIter )
645 Internal::Visual::Base& registeredVisualImpl = Toolkit::GetImplementation( (*registeredIter)->visual );
647 if( &object == ®isteredVisualImpl )
649 RegisteredVisualContainer::Iterator visualToRemoveIter;
650 // Find visual with the same index in the removal container
651 // Set if off stage as it's replacement is now ready.
652 // Remove if from removal list as now removed from stage.
653 // Set Pending flag on the ready visual to false as now ready.
654 if( FindVisual( (*registeredIter)->index, mRemoveVisuals, visualToRemoveIter ) )
656 (*registeredIter)->pending = false;
657 Toolkit::GetImplementation( (*visualToRemoveIter)->visual ).SetOffStage( self );
658 mRemoveVisuals.Erase( visualToRemoveIter );
664 // A visual is ready so control may need relayouting if staged
665 if ( self.OnStage() )
667 mControlImpl.RelayoutRequest();
670 // Emit signal if all enabled visuals registered by the control are ready.
671 if( IsResourceReady() )
673 Dali::Toolkit::Control handle( mControlImpl.GetOwner() );
674 mResourceReadySignal.Emit( handle );
678 bool Control::Impl::IsResourceReady() const
680 // Iterate through and check all the enabled visuals are ready
681 for( auto visualIter = mVisuals.Begin();
682 visualIter != mVisuals.End(); ++visualIter )
684 const Toolkit::Visual::Base visual = (*visualIter)->visual;
685 const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
687 // one of the enabled visuals is not ready
688 if( !visualImpl.IsResourceReady() && (*visualIter)->enabled )
696 Toolkit::Visual::ResourceStatus Control::Impl::GetVisualResourceStatus( Property::Index index ) const
698 RegisteredVisualContainer::Iterator iter;
699 if ( FindVisual( index, mVisuals, iter ) )
701 const Toolkit::Visual::Base visual = (*iter)->visual;
702 const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
703 return visualImpl.GetResourceStatus( );
706 return Toolkit::Visual::ResourceStatus::PREPARING;
709 Dali::Animation Control::Impl::CreateTransition( const Toolkit::TransitionData& handle )
711 Dali::Animation transition;
712 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
714 if( transitionData.Count() > 0 )
716 // Setup a Transition from TransitionData.
717 TransitionData::Iterator end = transitionData.End();
718 for( TransitionData::Iterator iter = transitionData.Begin() ;
719 iter != end; ++iter )
721 TransitionData::Animator* animator = (*iter);
723 Toolkit::Visual::Base visual = GetVisualByName( mVisuals, animator->objectName );
727 #if defined(DEBUG_ENABLED)
728 Dali::TypeInfo typeInfo;
729 ControlWrapper* controlWrapperImpl = dynamic_cast<ControlWrapper*>(&mControlImpl);
730 if( controlWrapperImpl )
732 typeInfo = controlWrapperImpl->GetTypeInfo();
735 DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Found %s visual for %s\n",
736 visual.GetName().c_str(), typeInfo?typeInfo.GetName().c_str():"Unknown" );
738 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
739 visualImpl.AnimateProperty( transition, *animator );
743 DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Could not find visual. Trying actors");
744 // Otherwise, try any actor children of control (Including the control)
745 Actor child = mControlImpl.Self().FindChildByName( animator->objectName );
748 Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
749 if( propertyIndex != Property::INVALID_INDEX )
751 if( animator->animate == false )
753 if( animator->targetValue.GetType() != Property::NONE )
755 child.SetProperty( propertyIndex, animator->targetValue );
758 else // animate the property
760 if( animator->initialValue.GetType() != Property::NONE )
762 child.SetProperty( propertyIndex, animator->initialValue );
767 transition = Dali::Animation::New( 0.1f );
770 transition.AnimateTo( Property( child, propertyIndex ),
771 animator->targetValue,
772 animator->alphaFunction,
773 TimePeriod( animator->timePeriodDelay,
774 animator->timePeriodDuration ) );
785 void Control::Impl::DoAction( Dali::Property::Index visualIndex, Dali::Property::Index actionId, const Dali::Property::Value attributes )
787 RegisteredVisualContainer::Iterator iter;
788 if ( FindVisual( visualIndex, mVisuals, iter ) )
790 Toolkit::GetImplementation((*iter)->visual).DoAction( actionId, attributes );
794 void Control::Impl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
796 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
800 Control& controlImpl( GetImplementation( control ) );
804 case Toolkit::Control::Property::STYLE_NAME:
806 controlImpl.SetStyleName( value.Get< std::string >() );
810 case Toolkit::DevelControl::Property::STATE:
812 bool withTransitions=true;
813 const Property::Value* valuePtr=&value;
814 Property::Map* map = value.GetMap();
817 Property::Value* value2 = map->Find("withTransitions");
820 withTransitions = value2->Get<bool>();
823 valuePtr = map->Find("state");
828 Toolkit::DevelControl::State state( controlImpl.mImpl->mState );
829 if( Scripting::GetEnumerationProperty< Toolkit::DevelControl::State >( *valuePtr, ControlStateTable, ControlStateTableCount, state ) )
831 controlImpl.mImpl->SetState( state, withTransitions );
837 case Toolkit::DevelControl::Property::SUB_STATE:
839 std::string subState;
840 if( value.Get( subState ) )
842 controlImpl.mImpl->SetSubState( subState );
847 case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
850 if( value.Get( focusId ) )
852 controlImpl.mImpl->mLeftFocusableActorId = focusId;
857 case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
860 if( value.Get( focusId ) )
862 controlImpl.mImpl->mRightFocusableActorId = focusId;
867 case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
870 if( value.Get( focusId ) )
872 controlImpl.mImpl->mUpFocusableActorId = focusId;
877 case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
880 if( value.Get( focusId ) )
882 controlImpl.mImpl->mDownFocusableActorId = focusId;
887 case Toolkit::Control::Property::BACKGROUND_COLOR:
889 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
890 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
894 case Toolkit::Control::Property::BACKGROUND_IMAGE:
896 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
897 Image image = Scripting::NewImage( value );
900 controlImpl.SetBackgroundImage( image );
904 // An empty image means the background is no longer required
905 controlImpl.ClearBackground();
910 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
912 if ( value.Get< bool >() )
914 controlImpl.SetKeyInputFocus();
918 controlImpl.ClearKeyInputFocus();
923 case Toolkit::Control::Property::BACKGROUND:
927 const Property::Map* map = value.GetMap();
928 if( map && !map->Empty() )
930 controlImpl.SetBackground( *map );
932 else if( value.Get( url ) )
934 // don't know the size to load
935 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
938 controlImpl.mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
941 else if( value.Get( color ) )
943 controlImpl.SetBackgroundColor(color);
947 // The background is an empty property map, so we should clear the background
948 controlImpl.ClearBackground();
953 case Toolkit::Control::Property::MARGIN:
956 if( value.Get( margin ) )
958 controlImpl.mImpl->SetMargin( margin );
963 case Toolkit::Control::Property::PADDING:
966 if( value.Get( padding ) )
968 controlImpl.mImpl->SetPadding( padding );
973 case Toolkit::DevelControl::Property::TOOLTIP:
975 TooltipPtr& tooltipPtr = controlImpl.mImpl->mTooltip;
978 tooltipPtr = Tooltip::New( control );
980 tooltipPtr->SetProperties( value );
988 Property::Value Control::Impl::GetProperty( BaseObject* object, Property::Index index )
990 Property::Value value;
992 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
996 Control& controlImpl( GetImplementation( control ) );
1000 case Toolkit::Control::Property::STYLE_NAME:
1002 value = controlImpl.GetStyleName();
1006 case Toolkit::DevelControl::Property::STATE:
1008 value = controlImpl.mImpl->mState;
1012 case Toolkit::DevelControl::Property::SUB_STATE:
1014 value = controlImpl.mImpl->mSubStateName;
1018 case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
1020 value = controlImpl.mImpl->mLeftFocusableActorId;
1024 case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
1026 value = controlImpl.mImpl->mRightFocusableActorId;
1030 case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
1032 value = controlImpl.mImpl->mUpFocusableActorId;
1036 case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
1038 value = controlImpl.mImpl->mDownFocusableActorId;
1042 case Toolkit::Control::Property::BACKGROUND_COLOR:
1044 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
1045 value = controlImpl.GetBackgroundColor();
1049 case Toolkit::Control::Property::BACKGROUND_IMAGE:
1051 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
1053 Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
1056 visual.CreatePropertyMap( map );
1062 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
1064 value = controlImpl.HasKeyInputFocus();
1068 case Toolkit::Control::Property::BACKGROUND:
1071 Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
1074 visual.CreatePropertyMap( map );
1081 case Toolkit::Control::Property::MARGIN:
1083 value = controlImpl.mImpl->GetMargin();
1087 case Toolkit::Control::Property::PADDING:
1089 value = controlImpl.mImpl->GetPadding();
1093 case Toolkit::DevelControl::Property::TOOLTIP:
1096 if( controlImpl.mImpl->mTooltip )
1098 controlImpl.mImpl->mTooltip->CreatePropertyMap( map );
1110 void Control::Impl::CopyInstancedProperties( RegisteredVisualContainer& visuals, Dictionary<Property::Map>& instancedProperties )
1112 for(RegisteredVisualContainer::Iterator iter = visuals.Begin(); iter!= visuals.End(); iter++)
1114 if( (*iter)->visual )
1116 Property::Map instanceMap;
1117 Toolkit::GetImplementation((*iter)->visual).CreateInstancePropertyMap(instanceMap);
1118 instancedProperties.Add( (*iter)->visual.GetName(), instanceMap );
1124 void Control::Impl::RemoveVisual( RegisteredVisualContainer& visuals, const std::string& visualName )
1126 Actor self( mControlImpl.Self() );
1128 for ( RegisteredVisualContainer::Iterator visualIter = visuals.Begin();
1129 visualIter != visuals.End(); ++visualIter )
1131 Toolkit::Visual::Base visual = (*visualIter)->visual;
1132 if( visual && visual.GetName() == visualName )
1134 Toolkit::GetImplementation(visual).SetOffStage( self );
1135 (*visualIter)->visual.Reset();
1136 visuals.Erase( visualIter );
1142 void Control::Impl::RemoveVisuals( RegisteredVisualContainer& visuals, DictionaryKeys& removeVisuals )
1144 Actor self( mControlImpl.Self() );
1145 for( DictionaryKeys::iterator iter = removeVisuals.begin(); iter != removeVisuals.end(); ++iter )
1147 const std::string visualName = *iter;
1148 RemoveVisual( visuals, visualName );
1152 void Control::Impl::RecreateChangedVisuals( Dictionary<Property::Map>& stateVisualsToChange,
1153 Dictionary<Property::Map>& instancedProperties )
1155 Dali::CustomActor handle( mControlImpl.GetOwner() );
1156 for( Dictionary<Property::Map>::iterator iter = stateVisualsToChange.Begin();
1157 iter != stateVisualsToChange.End(); ++iter )
1159 const std::string& visualName = (*iter).key;
1160 const Property::Map& toMap = (*iter).entry;
1162 // is it a candidate for re-creation?
1163 bool recreate = false;
1165 Toolkit::Visual::Base visual = GetVisualByName( mVisuals, visualName );
1168 Property::Map fromMap;
1169 visual.CreatePropertyMap( fromMap );
1171 Toolkit::Visual::Type fromType = GetVisualTypeFromMap( fromMap );
1172 Toolkit::Visual::Type toType = GetVisualTypeFromMap( toMap );
1174 if( fromType != toType )
1180 if( fromType == Toolkit::Visual::IMAGE || fromType == Toolkit::Visual::N_PATCH
1181 || fromType == Toolkit::Visual::SVG || fromType == Toolkit::Visual::ANIMATED_IMAGE )
1183 Property::Value* fromUrl = fromMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
1184 Property::Value* toUrl = toMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
1186 if( fromUrl && toUrl )
1188 std::string fromUrlString;
1189 std::string toUrlString;
1190 fromUrl->Get(fromUrlString);
1191 toUrl->Get(toUrlString);
1193 if( fromUrlString != toUrlString )
1201 const Property::Map* instancedMap = instancedProperties.FindConst( visualName );
1202 if( recreate || instancedMap )
1204 RemoveVisual( mVisuals, visualName );
1205 Style::ApplyVisual( handle, visualName, toMap, instancedMap );
1209 // @todo check to see if we can apply toMap without recreating the visual
1210 // e.g. by setting only animatable properties
1211 // For now, recreate all visuals, but merge in instance data.
1212 RemoveVisual( mVisuals, visualName );
1213 Style::ApplyVisual( handle, visualName, toMap, instancedMap );
1219 void Control::Impl::ReplaceStateVisualsAndProperties( const StylePtr oldState, const StylePtr newState, const std::string& subState )
1221 // Collect all old visual names
1222 DictionaryKeys stateVisualsToRemove;
1225 oldState->visuals.GetKeys( stateVisualsToRemove );
1226 if( ! subState.empty() )
1228 const StylePtr* oldSubState = oldState->subStates.FindConst(subState);
1231 DictionaryKeys subStateVisualsToRemove;
1232 (*oldSubState)->visuals.GetKeys( subStateVisualsToRemove );
1233 Merge( stateVisualsToRemove, subStateVisualsToRemove );
1238 // Collect all new visual properties
1239 Dictionary<Property::Map> stateVisualsToAdd;
1242 stateVisualsToAdd = newState->visuals;
1243 if( ! subState.empty() )
1245 const StylePtr* newSubState = newState->subStates.FindConst(subState);
1248 stateVisualsToAdd.Merge( (*newSubState)->visuals );
1253 // If a name is in both add/remove, move it to change list.
1254 Dictionary<Property::Map> stateVisualsToChange;
1255 FindChangableVisuals( stateVisualsToAdd, stateVisualsToChange, stateVisualsToRemove);
1257 // Copy instanced properties (e.g. text label) of current visuals
1258 Dictionary<Property::Map> instancedProperties;
1259 CopyInstancedProperties( mVisuals, instancedProperties );
1261 // For each visual in remove list, remove from mVisuals
1262 RemoveVisuals( mVisuals, stateVisualsToRemove );
1264 // For each visual in add list, create and add to mVisuals
1265 Dali::CustomActor handle( mControlImpl.GetOwner() );
1266 Style::ApplyVisuals( handle, stateVisualsToAdd, instancedProperties );
1268 // For each visual in change list, if it requires a new visual,
1269 // remove old visual, create and add to mVisuals
1270 RecreateChangedVisuals( stateVisualsToChange, instancedProperties );
1273 void Control::Impl::SetState( DevelControl::State newState, bool withTransitions )
1275 DevelControl::State oldState = mState;
1276 Dali::CustomActor handle( mControlImpl.GetOwner() );
1277 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Control::Impl::SetState: %s\n",
1278 (mState == DevelControl::NORMAL ? "NORMAL" :(
1279 mState == DevelControl::FOCUSED ?"FOCUSED" : (
1280 mState == DevelControl::DISABLED?"DISABLED":"NONE" ))));
1282 if( mState != newState )
1284 // If mState was Disabled, and new state is Focused, should probably
1285 // store that fact, e.g. in another property that FocusManager can access.
1288 // Trigger state change and transitions
1289 // Apply new style, if stylemanager is available
1290 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1293 const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1297 std::string oldStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( oldState, ControlStateTable, ControlStateTableCount );
1298 std::string newStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( newState, ControlStateTable, ControlStateTableCount );
1300 const StylePtr* newStateStyle = stylePtr->subStates.Find( newStateName );
1301 const StylePtr* oldStateStyle = stylePtr->subStates.Find( oldStateName );
1302 if( oldStateStyle && newStateStyle )
1304 // Only change if both state styles exist
1305 ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, mSubStateName );
1312 void Control::Impl::SetSubState( const std::string& subStateName, bool withTransitions )
1314 if( mSubStateName != subStateName )
1316 // Get existing sub-state visuals, and unregister them
1317 Dali::CustomActor handle( mControlImpl.GetOwner() );
1319 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1322 const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1326 std::string stateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( mState, ControlStateTable, ControlStateTableCount );
1328 const StylePtr* state = stylePtr->subStates.Find( stateName );
1331 StylePtr stateStyle(*state);
1333 const StylePtr* newStateStyle = stateStyle->subStates.Find( subStateName );
1334 const StylePtr* oldStateStyle = stateStyle->subStates.Find( mSubStateName );
1335 if( oldStateStyle && newStateStyle )
1338 ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, empty );
1344 mSubStateName = subStateName;
1348 void Control::Impl::OnStageDisconnection()
1350 Actor self = mControlImpl.Self();
1352 // Any visuals set for replacement but not yet ready should still be registered.
1353 // Reason: If a request was made to register a new visual but the control removed from stage before visual was ready
1354 // then when this control appears back on stage it should use that new visual.
1356 // Iterate through all registered visuals and set off stage
1357 SetVisualsOffStage( mVisuals, self );
1359 // Visuals pending replacement can now be taken out of the removal list and set off stage
1360 // Iterate through all replacement visuals and add to a move queue then set off stage
1361 for( auto removalIter = mRemoveVisuals.Begin(), end = mRemoveVisuals.End(); removalIter != end; removalIter++ )
1363 Toolkit::GetImplementation((*removalIter)->visual).SetOffStage( self );
1366 for( auto replacedIter = mVisuals.Begin(), end = mVisuals.End(); replacedIter != end; replacedIter++ )
1368 (*replacedIter)->pending = false;
1371 mRemoveVisuals.Clear();
1374 void Control::Impl::SetMargin( Extents margin )
1376 mControlImpl.mImpl->mMargin = margin;
1379 Extents Control::Impl::GetMargin() const
1381 return mControlImpl.mImpl->mMargin;
1384 void Control::Impl::SetPadding( Extents padding )
1386 mControlImpl.mImpl->mPadding = padding;
1389 Extents Control::Impl::GetPadding() const
1391 return mControlImpl.mImpl->mPadding;
1394 void Control::Impl::SetInputMethodContext( InputMethodContext& inputMethodContext )
1396 mInputMethodContext = inputMethodContext;
1399 bool Control::Impl::FilterKeyEvent( const KeyEvent& event )
1401 bool consumed ( false );
1403 if ( mInputMethodContext )
1405 consumed = mInputMethodContext.FilterEventKey( event );
1410 } // namespace Internal
1412 } // namespace Toolkit