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/devel-api/visuals/visual-properties-devel.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::DevelVisual::Type GetVisualTypeFromMap( const Property::Map& map )
90 Property::Value* typeValue = map.Find( Toolkit::DevelVisual::Property::TYPE, VISUAL_TYPE );
91 Toolkit::DevelVisual::Type type = Toolkit::DevelVisual::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()
278 } // unnamed namespace
281 // Properties registered without macro to use specific member variables.
282 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
283 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
284 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
285 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
286 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
287 const PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "tooltip", Toolkit::DevelControl::Property::TOOLTIP, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
288 const PropertyRegistration Control::Impl::PROPERTY_7( typeRegistration, "state", Toolkit::DevelControl::Property::STATE, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
289 const PropertyRegistration Control::Impl::PROPERTY_8( typeRegistration, "subState", Toolkit::DevelControl::Property::SUB_STATE, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
290 const PropertyRegistration Control::Impl::PROPERTY_9( typeRegistration, "leftFocusableActorId", Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
291 const PropertyRegistration Control::Impl::PROPERTY_10( typeRegistration, "rightFocusableActorId", Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID,Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
292 const PropertyRegistration Control::Impl::PROPERTY_11( typeRegistration, "upFocusableActorId", Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
293 const PropertyRegistration Control::Impl::PROPERTY_12( typeRegistration, "downFocusableActorId", Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
297 Control::Impl::Impl( Control& controlImpl )
298 : mControlImpl( controlImpl ),
299 mState( Toolkit::DevelControl::NORMAL ),
301 mLeftFocusableActorId( -1 ),
302 mRightFocusableActorId( -1 ),
303 mUpFocusableActorId( -1 ),
304 mDownFocusableActorId( -1 ),
306 mBackgroundColor(Color::TRANSPARENT),
307 mStartingPinchScale( NULL ),
309 mPinchGestureDetector(),
310 mPanGestureDetector(),
311 mTapGestureDetector(),
312 mLongPressGestureDetector(),
313 mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
314 mIsKeyboardNavigationSupported( false ),
315 mIsKeyboardFocusGroup( false )
320 Control::Impl::~Impl()
322 // All gesture detectors will be destroyed so no need to disconnect.
323 delete mStartingPinchScale;
326 Control::Impl& Control::Impl::Get( Internal::Control& internalControl )
328 return *internalControl.mImpl;
331 const Control::Impl& Control::Impl::Get( const Internal::Control& internalControl )
333 return *internalControl.mImpl;
336 // Gesture Detection Methods
337 void Control::Impl::PinchDetected(Actor actor, const PinchGesture& pinch)
339 mControlImpl.OnPinch(pinch);
342 void Control::Impl::PanDetected(Actor actor, const PanGesture& pan)
344 mControlImpl.OnPan(pan);
347 void Control::Impl::TapDetected(Actor actor, const TapGesture& tap)
349 mControlImpl.OnTap(tap);
352 void Control::Impl::LongPressDetected(Actor actor, const LongPressGesture& longPress)
354 mControlImpl.OnLongPress(longPress);
357 // Called by a Visual when it's resource is ready
358 void Control::Impl::ResourceReady( Visual::Base& object)
360 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady \n");
362 // A resource is ready, check if is in the replacement visual container
363 // Iterate through all visuals in replacement container and store indexes of ready visuals
364 Dali::Vector <Property::Index> readyVisuals;
365 Actor self = mControlImpl.Self();
367 for( auto replacementVisualIter = mReplacementVisuals.Begin();
368 replacementVisualIter < mReplacementVisuals.End(); ++replacementVisualIter )
370 const Toolkit::Visual::Base replacementVisual = (*replacementVisualIter)->visual;
371 const Internal::Visual::Base& replacementVisualImpl = Toolkit::GetImplementation( replacementVisual );
373 if( replacementVisualImpl.IsResourceReady() )
375 // Check if new replacement visual (index) is already queued for replacement and swap old for new.
376 RegisteredVisualContainer::Iterator registeredVisualsIter;
377 if( FindVisual( (*replacementVisualIter)->index, mVisuals, registeredVisualsIter ) )
379 Property::Index readyVisualIndex = (*replacementVisualIter)->index;
380 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady: %d Ready to replace\n", readyVisualIndex );
381 readyVisuals.PushBack( readyVisualIndex );
382 // Remove current shown visual from stage and from registered visuals container
383 Toolkit::GetImplementation((*registeredVisualsIter)->visual).SetOffStage( self );
384 mVisuals.Erase( registeredVisualsIter );
389 for( auto readyVisualsIter = readyVisuals.Begin(); readyVisualsIter != readyVisuals.End(); readyVisualsIter++ )
391 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady: %d Matched\n", (*readyVisualsIter) );
392 // Move new visual to be shown from replacement container into the control's registered visuals container
393 // Replacement visual has already been set on stage when it was added to replacement container
394 RegisteredVisualContainer::Iterator readyReplacementVisual;
395 if( FindVisual( (*readyVisualsIter) , mReplacementVisuals, readyReplacementVisual ) )
397 MoveVisual( readyReplacementVisual, mReplacementVisuals, mVisuals ); // Erases visual from replacement queue
399 // A visual has been replaced so control will most likely need relayouting
400 mControlImpl.RelayoutRequest();
403 // go through and check if all the visuals are ready, if they are emit a signal
404 for( auto visualIter = mVisuals.Begin();
405 visualIter != mVisuals.End(); ++visualIter )
407 const Toolkit::Visual::Base visual = (*visualIter)->visual;
408 const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
410 // one of the visuals is not ready
411 if( !visualImpl.IsResourceReady() )
417 // all the visuals are ready
418 Dali::Toolkit::Control handle( mControlImpl.GetOwner() );
419 mResourceReadySignal.Emit( handle );
422 bool Control::Impl::IsResourceReady() const
424 // go through and check all the visuals are ready
425 for ( RegisteredVisualContainer::ConstIterator visualIter = mVisuals.Begin();
426 visualIter != mVisuals.End(); ++visualIter )
428 const Toolkit::Visual::Base visual = (*visualIter)->visual;
429 const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
431 // one of the visuals is not ready
432 if( !visualImpl.IsResourceReady() )
440 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
442 RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::NOT_SET );
445 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, int depthIndex )
447 RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::SET, depthIndex );
450 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
452 RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::NOT_SET );
455 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled, int depthIndex )
457 RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::SET, depthIndex );
460 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex )
462 DALI_LOG_INFO( gLogFilter, Debug::Concise, "RegisterVisual:%d \n", index );
464 bool visualReplaced ( false );
465 Actor self = mControlImpl.Self();
467 if( !mVisuals.Empty() )
469 RegisteredVisualContainer::Iterator registeredVisualsiter;
470 // Check if visual (index) is already registered. Replace if so.
471 if( FindVisual( index, mVisuals, registeredVisualsiter ) )
473 if( (*registeredVisualsiter)->visual )
475 // Store current visual depth index as may need to set the replacement visual to same depth
476 const int currentDepthIndex = (*registeredVisualsiter)->visual.GetDepthIndex();
478 // Monitor when the visuals resources are ready
479 StopObservingVisual( (*registeredVisualsiter)->visual );
480 StartObservingVisual( visual );
484 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RegisterVisual Adding visual to replacement Queue: %d \n", index );
485 // Check if visual is currently in the process of being replaced
486 RegisteredVisualContainer::Iterator queuedReplacementVisual;
487 if ( FindVisual( index, mReplacementVisuals, queuedReplacementVisual ) )
489 // If visual on replacement queue is going to be replaced before it's ready then will be removed from queue (and stage)
490 // Only the the last requested visual will be queued and then displayed.
491 Toolkit::GetImplementation( (*queuedReplacementVisual)->visual ).SetOffStage( self );
492 mReplacementVisuals.Erase(queuedReplacementVisual);
494 // Add to replacement list
495 mReplacementVisuals.PushBack( new RegisteredVisual( index, visual, ( enabled == VisualState::ENABLED ? true : false ) ) );
499 // Not staged so can just replace registered visual
500 (*registeredVisualsiter)->visual = visual;
501 (*registeredVisualsiter)->enabled = ( enabled == VisualState::ENABLED ) ? true : false;
504 // 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
505 if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
506 ( visual.GetDepthIndex() == 0 ) )
508 visual.SetDepthIndex( currentDepthIndex );
512 visualReplaced = true;
516 // If not set, set the name of the visual to the same name as the control's property.
517 // ( If the control has been type registered )
518 if( visual.GetName().empty() )
522 std::string visualName = self.GetPropertyName( index );
523 if( !visualName.empty() )
525 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Setting visual name for property %d to %s\n",
526 index, visualName.c_str() );
527 visual.SetName( visualName );
530 catch( Dali::DaliException e )
532 DALI_LOG_WARNING( "Attempting to register visual without a registered property, index: %d\n", index );
536 if( !visualReplaced ) // New registration entry
538 DALI_LOG_INFO( gLogFilter, Debug::Concise, "New Visual registration %d\n", index);
539 mVisuals.PushBack( new RegisteredVisual( index, visual, ( enabled == VisualState::ENABLED ? true : false ) ) );
541 // monitor when the visuals resources are ready
542 StartObservingVisual( visual );
544 // 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
545 if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
546 ( mVisuals.Size() > 1 ) &&
547 ( visual.GetDepthIndex() == 0 ) )
549 int maxDepthIndex = std::numeric_limits< int >::min();
551 RegisteredVisualContainer::ConstIterator iter;
552 const RegisteredVisualContainer::ConstIterator endIter = mVisuals.End();
553 for ( iter = mVisuals.Begin(); iter != endIter; iter++ )
555 const int visualDepthIndex = (*iter)->visual.GetDepthIndex();
556 if ( visualDepthIndex > maxDepthIndex )
558 maxDepthIndex = visualDepthIndex;
562 ++maxDepthIndex; // Add one to the current maximum depth index so that our added visual appears on top
563 visual.SetDepthIndex( maxDepthIndex );
569 // If the caller has set the depth-index, then set it here
570 if( depthIndexValueSet == DepthIndexValue::SET )
572 visual.SetDepthIndex( depthIndex );
575 // Put on stage if enabled and the control is already on the stage
576 if( ( enabled == VisualState::ENABLED ) && self.OnStage() )
578 // Visual must be set on stage for the renderer to be created and the ResourceReady triggered.
579 Toolkit::GetImplementation(visual).SetOnStage( self );
583 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n", visual.GetName().c_str(), index, enabled?"T":"F" );
586 void Control::Impl::UnregisterVisual( Property::Index index )
588 RegisteredVisualContainer::Iterator iter;
589 if ( FindVisual( index, mVisuals, iter ) )
591 // stop observing visual
592 StopObservingVisual( (*iter)->visual );
594 Actor self( mControlImpl.Self() );
595 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
596 (*iter)->visual.Reset();
597 mVisuals.Erase( iter );
601 Toolkit::Visual::Base Control::Impl::GetVisual( Property::Index index ) const
603 RegisteredVisualContainer::Iterator iter;
604 if ( FindVisual( index, mVisuals, iter ) )
606 return (*iter)->visual;
609 return Toolkit::Visual::Base();
612 void Control::Impl::EnableVisual( Property::Index index, bool enable )
614 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual (%d)\n", index);
616 RegisteredVisualContainer::Iterator iter;
617 if ( FindVisual( index, mVisuals, iter ) )
619 if ( (*iter)->enabled == enable )
621 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual %s(%d) already %s\n", (*iter)->visual.GetName().c_str(), index, enable?"enabled":"disabled");
625 (*iter)->enabled = enable;
626 Actor parentActor = mControlImpl.Self();
627 if ( mControlImpl.Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
631 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) on stage \n", (*iter)->visual.GetName().c_str(), index );
632 Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
636 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) off stage \n", (*iter)->visual.GetName().c_str(), index );
637 Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor ); // No need to call if control not staged.
643 bool Control::Impl::IsVisualEnabled( Property::Index index ) const
645 RegisteredVisualContainer::Iterator iter;
646 if ( FindVisual( index, mVisuals, iter ) )
648 return (*iter)->enabled;
653 void Control::Impl::StopObservingVisual( Toolkit::Visual::Base& visual )
655 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
657 // Stop observing the visual
658 visualImpl.RemoveResourceObserver( *this );
661 void Control::Impl::StartObservingVisual( Toolkit::Visual::Base& visual)
663 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
665 // start observing the visual for resource ready
666 visualImpl.AddResourceObserver( *this );
669 Dali::Animation Control::Impl::CreateTransition( const Toolkit::TransitionData& handle )
671 Dali::Animation transition;
672 const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
674 if( transitionData.Count() > 0 )
676 // Setup a Transition from TransitionData.
677 TransitionData::Iterator end = transitionData.End();
678 for( TransitionData::Iterator iter = transitionData.Begin() ;
679 iter != end; ++iter )
681 TransitionData::Animator* animator = (*iter);
683 Toolkit::Visual::Base visual = GetVisualByName( mVisuals, animator->objectName );
687 #if defined(DEBUG_ENABLED)
688 Dali::TypeInfo typeInfo;
689 ControlWrapper* controlWrapperImpl = dynamic_cast<ControlWrapper*>(&mControlImpl);
690 if( controlWrapperImpl )
692 typeInfo = controlWrapperImpl->GetTypeInfo();
695 DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Found %s visual for %s\n",
696 visual.GetName().c_str(), typeInfo?typeInfo.GetName().c_str():"Unknown" );
698 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
699 visualImpl.AnimateProperty( transition, *animator );
703 DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Could not find visual. Trying actors");
704 // Otherwise, try any actor children of control (Including the control)
705 Actor child = mControlImpl.Self().FindChildByName( animator->objectName );
708 Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
709 if( propertyIndex != Property::INVALID_INDEX )
711 if( animator->animate == false )
713 if( animator->targetValue.GetType() != Property::NONE )
715 child.SetProperty( propertyIndex, animator->targetValue );
718 else // animate the property
720 if( animator->initialValue.GetType() != Property::NONE )
722 child.SetProperty( propertyIndex, animator->initialValue );
727 transition = Dali::Animation::New( 0.1f );
730 transition.AnimateTo( Property( child, propertyIndex ),
731 animator->targetValue,
732 animator->alphaFunction,
733 TimePeriod( animator->timePeriodDelay,
734 animator->timePeriodDuration ) );
745 void Control::Impl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
747 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
751 Control& controlImpl( GetImplementation( control ) );
755 case Toolkit::Control::Property::STYLE_NAME:
757 controlImpl.SetStyleName( value.Get< std::string >() );
761 case Toolkit::DevelControl::Property::STATE:
763 bool withTransitions=true;
764 const Property::Value* valuePtr=&value;
765 Property::Map* map = value.GetMap();
768 Property::Value* value2 = map->Find("withTransitions");
771 withTransitions = value2->Get<bool>();
774 valuePtr = map->Find("state");
779 Toolkit::DevelControl::State state( controlImpl.mImpl->mState );
780 if( Scripting::GetEnumerationProperty< Toolkit::DevelControl::State >( *valuePtr, ControlStateTable, ControlStateTableCount, state ) )
782 controlImpl.mImpl->SetState( state, withTransitions );
788 case Toolkit::DevelControl::Property::SUB_STATE:
790 std::string subState;
791 if( value.Get( subState ) )
793 controlImpl.mImpl->SetSubState( subState );
798 case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
801 if( value.Get( focusId ) )
803 controlImpl.mImpl->mLeftFocusableActorId = focusId;
808 case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
811 if( value.Get( focusId ) )
813 controlImpl.mImpl->mRightFocusableActorId = focusId;
818 case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
821 if( value.Get( focusId ) )
823 controlImpl.mImpl->mUpFocusableActorId = focusId;
828 case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
831 if( value.Get( focusId ) )
833 controlImpl.mImpl->mDownFocusableActorId = focusId;
838 case Toolkit::Control::Property::BACKGROUND_COLOR:
840 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
841 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
845 case Toolkit::Control::Property::BACKGROUND_IMAGE:
847 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
848 Image image = Scripting::NewImage( value );
851 controlImpl.SetBackgroundImage( image );
855 // An empty image means the background is no longer required
856 controlImpl.ClearBackground();
861 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
863 if ( value.Get< bool >() )
865 controlImpl.SetKeyInputFocus();
869 controlImpl.ClearKeyInputFocus();
874 case Toolkit::Control::Property::BACKGROUND:
878 const Property::Map* map = value.GetMap();
879 if( map && !map->Empty() )
881 controlImpl.SetBackground( *map );
883 else if( value.Get( url ) )
885 // don't know the size to load
886 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
889 controlImpl.mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
892 else if( value.Get( color ) )
894 controlImpl.SetBackgroundColor(color);
898 // The background is an empty property map, so we should clear the background
899 controlImpl.ClearBackground();
904 case Toolkit::DevelControl::Property::TOOLTIP:
906 TooltipPtr& tooltipPtr = controlImpl.mImpl->mTooltip;
909 tooltipPtr = Tooltip::New( control );
911 tooltipPtr->SetProperties( value );
917 Property::Value Control::Impl::GetProperty( BaseObject* object, Property::Index index )
919 Property::Value value;
921 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
925 Control& controlImpl( GetImplementation( control ) );
929 case Toolkit::Control::Property::STYLE_NAME:
931 value = controlImpl.GetStyleName();
935 case Toolkit::DevelControl::Property::STATE:
937 value = controlImpl.mImpl->mState;
941 case Toolkit::DevelControl::Property::SUB_STATE:
943 value = controlImpl.mImpl->mSubStateName;
947 case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
949 value = controlImpl.mImpl->mLeftFocusableActorId;
953 case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
955 value = controlImpl.mImpl->mRightFocusableActorId;
959 case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
961 value = controlImpl.mImpl->mUpFocusableActorId;
965 case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
967 value = controlImpl.mImpl->mDownFocusableActorId;
971 case Toolkit::Control::Property::BACKGROUND_COLOR:
973 DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
974 value = controlImpl.GetBackgroundColor();
978 case Toolkit::Control::Property::BACKGROUND_IMAGE:
980 DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
982 Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
985 visual.CreatePropertyMap( map );
991 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
993 value = controlImpl.HasKeyInputFocus();
997 case Toolkit::Control::Property::BACKGROUND:
1000 Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
1003 visual.CreatePropertyMap( map );
1010 case Toolkit::DevelControl::Property::TOOLTIP:
1013 if( controlImpl.mImpl->mTooltip )
1015 controlImpl.mImpl->mTooltip->CreatePropertyMap( map );
1028 void Control::Impl::CopyInstancedProperties( RegisteredVisualContainer& visuals, Dictionary<Property::Map>& instancedProperties )
1030 for(RegisteredVisualContainer::Iterator iter = visuals.Begin(); iter!= visuals.End(); iter++)
1032 if( (*iter)->visual )
1034 Property::Map instanceMap;
1035 Toolkit::GetImplementation((*iter)->visual).CreateInstancePropertyMap(instanceMap);
1036 instancedProperties.Add( (*iter)->visual.GetName(), instanceMap );
1042 void Control::Impl::RemoveVisual( RegisteredVisualContainer& visuals, const std::string& visualName )
1044 Actor self( mControlImpl.Self() );
1046 for ( RegisteredVisualContainer::Iterator visualIter = visuals.Begin();
1047 visualIter != visuals.End(); ++visualIter )
1049 Toolkit::Visual::Base visual = (*visualIter)->visual;
1050 if( visual && visual.GetName() == visualName )
1052 Toolkit::GetImplementation(visual).SetOffStage( self );
1053 (*visualIter)->visual.Reset();
1054 visuals.Erase( visualIter );
1060 void Control::Impl::RemoveVisuals( RegisteredVisualContainer& visuals, DictionaryKeys& removeVisuals )
1062 Actor self( mControlImpl.Self() );
1063 for( DictionaryKeys::iterator iter = removeVisuals.begin(); iter != removeVisuals.end(); ++iter )
1065 const std::string visualName = *iter;
1066 RemoveVisual( visuals, visualName );
1070 void Control::Impl::RecreateChangedVisuals( Dictionary<Property::Map>& stateVisualsToChange,
1071 Dictionary<Property::Map>& instancedProperties )
1073 Dali::CustomActor handle( mControlImpl.GetOwner() );
1074 for( Dictionary<Property::Map>::iterator iter = stateVisualsToChange.Begin();
1075 iter != stateVisualsToChange.End(); ++iter )
1077 const std::string& visualName = (*iter).key;
1078 const Property::Map& toMap = (*iter).entry;
1080 // is it a candidate for re-creation?
1081 bool recreate = false;
1083 Toolkit::Visual::Base visual = GetVisualByName( mVisuals, visualName );
1086 Property::Map fromMap;
1087 visual.CreatePropertyMap( fromMap );
1089 Toolkit::DevelVisual::Type fromType = GetVisualTypeFromMap( fromMap );
1090 Toolkit::DevelVisual::Type toType = GetVisualTypeFromMap( toMap );
1092 if( fromType != toType )
1098 if( fromType == Toolkit::DevelVisual::IMAGE || fromType == Toolkit::DevelVisual::N_PATCH
1099 || fromType == Toolkit::DevelVisual::SVG || fromType == Toolkit::DevelVisual::ANIMATED_IMAGE )
1101 Property::Value* fromUrl = fromMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
1102 Property::Value* toUrl = toMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
1104 if( fromUrl && toUrl )
1106 std::string fromUrlString;
1107 std::string toUrlString;
1108 fromUrl->Get(fromUrlString);
1109 toUrl->Get(toUrlString);
1111 if( fromUrlString != toUrlString )
1119 const Property::Map* instancedMap = instancedProperties.FindConst( visualName );
1120 if( recreate || instancedMap )
1122 RemoveVisual( mVisuals, visualName );
1123 Style::ApplyVisual( handle, visualName, toMap, instancedMap );
1127 // @todo check to see if we can apply toMap without recreating the visual
1128 // e.g. by setting only animatable properties
1129 // For now, recreate all visuals, but merge in instance data.
1130 RemoveVisual( mVisuals, visualName );
1131 Style::ApplyVisual( handle, visualName, toMap, instancedMap );
1137 void Control::Impl::ReplaceStateVisualsAndProperties( const StylePtr oldState, const StylePtr newState, const std::string& subState )
1139 // Collect all old visual names
1140 DictionaryKeys stateVisualsToRemove;
1143 oldState->visuals.GetKeys( stateVisualsToRemove );
1144 if( ! subState.empty() )
1146 const StylePtr* oldSubState = oldState->subStates.FindConst(subState);
1149 DictionaryKeys subStateVisualsToRemove;
1150 (*oldSubState)->visuals.GetKeys( subStateVisualsToRemove );
1151 Merge( stateVisualsToRemove, subStateVisualsToRemove );
1156 // Collect all new visual properties
1157 Dictionary<Property::Map> stateVisualsToAdd;
1160 stateVisualsToAdd = newState->visuals;
1161 if( ! subState.empty() )
1163 const StylePtr* newSubState = newState->subStates.FindConst(subState);
1166 stateVisualsToAdd.Merge( (*newSubState)->visuals );
1171 // If a name is in both add/remove, move it to change list.
1172 Dictionary<Property::Map> stateVisualsToChange;
1173 FindChangableVisuals( stateVisualsToAdd, stateVisualsToChange, stateVisualsToRemove);
1175 // Copy instanced properties (e.g. text label) of current visuals
1176 Dictionary<Property::Map> instancedProperties;
1177 CopyInstancedProperties( mVisuals, instancedProperties );
1179 // For each visual in remove list, remove from mVisuals
1180 RemoveVisuals( mVisuals, stateVisualsToRemove );
1182 // For each visual in add list, create and add to mVisuals
1183 Dali::CustomActor handle( mControlImpl.GetOwner() );
1184 Style::ApplyVisuals( handle, stateVisualsToAdd, instancedProperties );
1186 // For each visual in change list, if it requires a new visual,
1187 // remove old visual, create and add to mVisuals
1188 RecreateChangedVisuals( stateVisualsToChange, instancedProperties );
1191 void Control::Impl::SetState( DevelControl::State newState, bool withTransitions )
1193 DevelControl::State oldState = mState;
1194 Dali::CustomActor handle( mControlImpl.GetOwner() );
1195 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Control::Impl::SetState: %s\n",
1196 (mState == DevelControl::NORMAL ? "NORMAL" :(
1197 mState == DevelControl::FOCUSED ?"FOCUSED" : (
1198 mState == DevelControl::DISABLED?"DISABLED":"NONE" ))));
1200 if( mState != newState )
1202 // If mState was Disabled, and new state is Focused, should probably
1203 // store that fact, e.g. in another property that FocusManager can access.
1206 // Trigger state change and transitions
1207 // Apply new style, if stylemanager is available
1208 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1211 const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1215 std::string oldStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( oldState, ControlStateTable, ControlStateTableCount );
1216 std::string newStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( newState, ControlStateTable, ControlStateTableCount );
1218 const StylePtr* newStateStyle = stylePtr->subStates.Find( newStateName );
1219 const StylePtr* oldStateStyle = stylePtr->subStates.Find( oldStateName );
1220 if( oldStateStyle && newStateStyle )
1222 // Only change if both state styles exist
1223 ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, mSubStateName );
1230 void Control::Impl::SetSubState( const std::string& subStateName, bool withTransitions )
1232 if( mSubStateName != subStateName )
1234 // Get existing sub-state visuals, and unregister them
1235 Dali::CustomActor handle( mControlImpl.GetOwner() );
1237 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1240 const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1244 std::string stateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( mState, ControlStateTable, ControlStateTableCount );
1246 const StylePtr* state = stylePtr->subStates.Find( stateName );
1249 StylePtr stateStyle(*state);
1251 const StylePtr* newStateStyle = stateStyle->subStates.Find( subStateName );
1252 const StylePtr* oldStateStyle = stateStyle->subStates.Find( mSubStateName );
1253 if( oldStateStyle && newStateStyle )
1256 ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, empty );
1262 mSubStateName = subStateName;
1266 } // namespace Internal
1268 } // namespace Toolkit