2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/public-api/controls/control-impl.h>
22 #include <cstring> // for strcmp
26 #include <dali/public-api/animation/constraint.h>
27 #include <dali/public-api/object/type-registry-helper.h>
28 #include <dali/public-api/size-negotiation/relayout-container.h>
29 #include <dali/devel-api/scripting/scripting.h>
30 #include <dali/integration-api/debug.h>
33 #include <dali-toolkit/public-api/align-enumerations.h>
34 #include <dali-toolkit/public-api/controls/control.h>
35 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
36 #include <dali-toolkit/public-api/styling/style-manager.h>
37 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
38 #include <dali-toolkit/public-api/visuals/visual-properties.h>
39 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
40 #include <dali-toolkit/devel-api/controls/control-devel.h>
41 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
42 #include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
43 #include <dali-toolkit/devel-api/visuals/color-visual-actions-devel.h>
44 #include <dali-toolkit/internal/styling/style-manager-impl.h>
45 #include <dali-toolkit/internal/visuals/color/color-visual.h>
46 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
47 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
61 #if defined(DEBUG_ENABLED)
62 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
66 * @brief Replace the background visual if it's a color visual with the renderIfTransparent property set as required.
67 * @param[in] controlImpl The control implementation
68 * @param[in] renderIfTransaparent Whether we should render if the color is transparent
70 void ChangeBackgroundColorVisual( Control& controlImpl, bool renderIfTransparent )
72 Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
74 Toolkit::Visual::Base backgroundVisual = controlDataImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
75 if( backgroundVisual && backgroundVisual.GetType() == Toolkit::Visual::COLOR )
78 backgroundVisual.CreatePropertyMap( map );
80 // Only change it if it's a color visual
81 map[ Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT ] = renderIfTransparent;
82 controlImpl.SetBackground( map );
87 * @brief Creates a clipping renderer if required.
88 * (EG. If no renders exist and clipping is enabled).
89 * @param[in] controlImpl The control implementation.
91 void CreateClippingRenderer( Control& controlImpl )
93 // We want to add a transparent background if we do not have one for clipping.
94 Actor self( controlImpl.Self() );
95 int clippingMode = ClippingMode::DISABLED;
96 if( self.GetProperty( Actor::Property::CLIPPING_MODE ).Get( clippingMode ) )
98 switch( clippingMode )
100 case ClippingMode::CLIP_CHILDREN:
102 if( self.GetRendererCount() == 0u )
104 Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
105 if( controlDataImpl.mVisuals.Empty() )
107 controlImpl.SetBackgroundColor( Color::TRANSPARENT );
111 // We have visuals, check if we've set the background and re-create it to
112 // render even if transparent (only if it's a color visual)
113 ChangeBackgroundColorVisual( controlImpl, true );
119 case ClippingMode::DISABLED:
120 case ClippingMode::CLIP_TO_BOUNDING_BOX:
122 // If we have a background visual, check if it's a color visual and remove the render if transparent flag
123 ChangeBackgroundColorVisual( controlImpl, false );
130 } // unnamed namespace
133 Toolkit::Control Control::New()
135 // Create the implementation, temporarily owned on stack
136 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
138 // Pass ownership to handle
139 Toolkit::Control handle( *controlImpl );
141 // Second-phase init of the implementation
142 // This can only be done after the CustomActor connection has been made...
143 controlImpl->Initialize();
148 void Control::SetStyleName( const std::string& styleName )
150 if( styleName != mImpl->mStyleName )
152 mImpl->mStyleName = styleName;
154 // Apply new style, if stylemanager is available
155 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
158 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
163 const std::string& Control::GetStyleName() const
165 return mImpl->mStyleName;
168 void Control::SetBackgroundColor( const Vector4& color )
170 mImpl->mBackgroundColor = color;
173 map[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::COLOR;
174 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
176 bool renderIfTransparent = false;
177 int clippingMode = ClippingMode::DISABLED;
178 if( ( Self().GetProperty( Actor::Property::CLIPPING_MODE ).Get( clippingMode ) ) &&
179 ( clippingMode == ClippingMode::CLIP_CHILDREN ) )
181 // If clipping-mode is set to CLIP_CHILDREN, then force visual to add the render even if transparent
182 map[ Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT ] = true;
183 renderIfTransparent = true;
186 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
187 if( visual && visual.GetType() == Toolkit::Visual::COLOR )
189 Property::Map visualMap;
190 visual.CreatePropertyMap( visualMap );
192 Property::Value* renderValue = visualMap.Find( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT );
193 Property::Value* colorValue = visualMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR );
194 if( renderValue && colorValue )
196 if( ( renderValue->Get< bool >() == true || colorValue->Get< Vector4 >().a > 0.0f )
197 && ( renderIfTransparent || color.a > 0.0f ) )
199 // Update background color only
200 mImpl->DoAction( Toolkit::Control::Property::BACKGROUND, DevelColorVisual::Action::UPDATE_PROPERTY, map );
206 SetBackground( map );
209 void Control::SetBackground( const Property::Map& map )
211 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
212 visual.SetName("background");
215 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
217 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
222 void Control::ClearBackground()
224 mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
225 mImpl->mBackgroundColor = Color::TRANSPARENT;
227 // Trigger a size negotiation request that may be needed when unregistering a visual.
231 void Control::EnableGestureDetection(GestureType::Value type)
233 if ( (type & GestureType::PINCH) && !mImpl->mPinchGestureDetector )
235 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
236 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
237 mImpl->mPinchGestureDetector.Attach(Self());
240 if ( (type & GestureType::PAN) && !mImpl->mPanGestureDetector )
242 mImpl->mPanGestureDetector = PanGestureDetector::New();
243 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
244 mImpl->mPanGestureDetector.Attach(Self());
247 if ( (type & GestureType::TAP) && !mImpl->mTapGestureDetector )
249 mImpl->mTapGestureDetector = TapGestureDetector::New();
250 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
251 mImpl->mTapGestureDetector.Attach(Self());
254 if ( (type & GestureType::LONG_PRESS) && !mImpl->mLongPressGestureDetector )
256 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
257 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
258 mImpl->mLongPressGestureDetector.Attach(Self());
262 void Control::DisableGestureDetection(GestureType::Value type)
264 if ( (type & GestureType::PINCH) && mImpl->mPinchGestureDetector )
266 mImpl->mPinchGestureDetector.Detach(Self());
267 mImpl->mPinchGestureDetector.Reset();
270 if ( (type & GestureType::PAN) && mImpl->mPanGestureDetector )
272 mImpl->mPanGestureDetector.Detach(Self());
273 mImpl->mPanGestureDetector.Reset();
276 if ( (type & GestureType::TAP) && mImpl->mTapGestureDetector )
278 mImpl->mTapGestureDetector.Detach(Self());
279 mImpl->mTapGestureDetector.Reset();
282 if ( (type & GestureType::LONG_PRESS) && mImpl->mLongPressGestureDetector)
284 mImpl->mLongPressGestureDetector.Detach(Self());
285 mImpl->mLongPressGestureDetector.Reset();
289 PinchGestureDetector Control::GetPinchGestureDetector() const
291 return mImpl->mPinchGestureDetector;
294 PanGestureDetector Control::GetPanGestureDetector() const
296 return mImpl->mPanGestureDetector;
299 TapGestureDetector Control::GetTapGestureDetector() const
301 return mImpl->mTapGestureDetector;
304 LongPressGestureDetector Control::GetLongPressGestureDetector() const
306 return mImpl->mLongPressGestureDetector;
309 void Control::SetKeyboardNavigationSupport(bool isSupported)
311 mImpl->mIsKeyboardNavigationSupported = isSupported;
314 bool Control::IsKeyboardNavigationSupported()
316 return mImpl->mIsKeyboardNavigationSupported;
319 void Control::SetKeyInputFocus()
321 if( Self().GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
323 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
327 bool Control::HasKeyInputFocus()
330 if( Self().GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
332 Toolkit::Control control = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
333 if( Self() == control )
341 void Control::ClearKeyInputFocus()
343 if( Self().GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
345 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
349 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
351 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
353 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
354 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
357 bool Control::IsKeyboardFocusGroup()
359 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
362 void Control::AccessibilityActivate()
364 // Inform deriving classes
365 OnAccessibilityActivated();
368 void Control::KeyboardEnter()
370 // Inform deriving classes
374 bool Control::OnAccessibilityActivated()
376 return false; // Accessibility activation is not handled by default
379 bool Control::OnKeyboardEnter()
381 return false; // Keyboard enter is not handled by default
384 bool Control::OnAccessibilityPan(PanGesture gesture)
386 return false; // Accessibility pan gesture is not handled by default
389 bool Control::OnAccessibilityValueChange(bool isIncrease)
391 return false; // Accessibility value change action is not handled by default
394 bool Control::OnAccessibilityZoom()
396 return false; // Accessibility zoom action is not handled by default
399 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
404 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
408 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
410 return mImpl->mKeyEventSignal;
413 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
415 return mImpl->mKeyInputFocusGainedSignal;
418 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
420 return mImpl->mKeyInputFocusLostSignal;
423 bool Control::EmitKeyEventSignal( const KeyEvent& event )
425 // Guard against destruction during signal emission
426 Dali::Toolkit::Control handle( GetOwner() );
428 bool consumed = false;
430 consumed = mImpl->FilterKeyEvent( event );
432 // signals are allocated dynamically when someone connects
433 if ( !consumed && !mImpl->mKeyEventSignal.Empty() )
435 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
440 // Notification for derived classes
441 consumed = OnKeyEvent(event);
447 Control::Control( ControlBehaviour behaviourFlags )
448 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
449 mImpl(new Impl(*this))
451 mImpl->mFlags = behaviourFlags;
459 void Control::Initialize()
461 // Call deriving classes so initialised before styling is applied to them.
464 if( !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
466 Toolkit::StyleManager styleManager = StyleManager::Get();
468 // if stylemanager is available
471 StyleManager& styleManagerImpl = GetImpl( styleManager );
473 // Register for style changes
474 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
476 // Apply the current style
477 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
481 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
483 SetKeyboardNavigationSupport( true );
487 void Control::OnInitialize()
491 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
493 // By default the control is only interested in theme (not font) changes
494 if( styleManager && change == StyleChange::THEME_CHANGE )
496 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
501 void Control::OnPinch(const PinchGesture& pinch)
503 if( !( mImpl->mStartingPinchScale ) )
506 mImpl->mStartingPinchScale = new Vector3;
509 if( pinch.GetState() == GestureState::STARTED )
511 *( mImpl->mStartingPinchScale ) = Self().GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
514 Self().SetProperty( Actor::Property::SCALE, *( mImpl->mStartingPinchScale ) * pinch.GetScale() );
517 void Control::OnPan( const PanGesture& pan )
521 void Control::OnTap(const TapGesture& tap)
525 void Control::OnLongPress( const LongPressGesture& longPress )
529 void Control::EmitKeyInputFocusSignal( bool focusGained )
531 Dali::Toolkit::Control handle( GetOwner() );
535 // signals are allocated dynamically when someone connects
536 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
538 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
543 // signals are allocated dynamically when someone connects
544 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
546 mImpl->mKeyInputFocusLostSignal.Emit( handle );
551 void Control::OnSceneConnection( int depth )
553 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnSceneConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
555 Actor self( Self() );
557 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
559 // Check whether the visual is empty and enabled
560 if( (*iter)->visual && (*iter)->enabled )
562 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnSceneConnection Setting visual(%d) on scene\n", (*iter)->index );
563 Toolkit::GetImplementation((*iter)->visual).SetOnScene( self );
567 // The clipping renderer is only created if required.
568 CreateClippingRenderer( *this );
570 // Request to be laid out when the control is connected to the Scene.
571 // Signal that a Relayout may be needed
575 void Control::OnSceneDisconnection()
577 mImpl->OnSceneDisconnection();
580 void Control::OnKeyInputFocusGained()
582 EmitKeyInputFocusSignal( true );
585 void Control::OnKeyInputFocusLost()
587 EmitKeyInputFocusSignal( false );
590 void Control::OnChildAdd(Actor& child)
594 void Control::OnChildRemove(Actor& child)
598 void Control::OnPropertySet( Property::Index index, const Property::Value& propertyValue )
600 // If the clipping mode has been set, we may need to create a renderer.
601 // Only do this if we are already on-stage as the OnSceneConnection will handle the off-stage clipping controls.
602 if( ( index == Actor::Property::CLIPPING_MODE ) && Self().GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
604 // Note: This method will handle whether creation of the renderer is required.
605 CreateClippingRenderer( *this );
609 void Control::OnSizeSet(const Vector3& targetSize)
611 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
614 Vector2 size( targetSize );
615 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
619 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
621 // @todo size negotiate background to new size, animate as well?
624 bool Control::OnKeyEvent(const KeyEvent& event)
626 return false; // Do not consume
629 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
631 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
633 Actor child = Self().GetChildAt( i );
634 Vector2 newChildSize( size );
636 // When set the padding or margin on the control, child should be resized and repositioned.
637 if( ( mImpl->mPadding.start != 0 ) || ( mImpl->mPadding.end != 0 ) || ( mImpl->mPadding.top != 0 ) || ( mImpl->mPadding.bottom != 0 ) ||
638 ( mImpl->mMargin.start != 0 ) || ( mImpl->mMargin.end != 0 ) || ( mImpl->mMargin.top != 0 ) || ( mImpl->mMargin.bottom != 0 ) )
640 Extents padding = mImpl->mPadding;
642 Dali::CustomActor ownerActor(GetOwner());
643 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( ownerActor.GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
645 if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
647 std::swap( padding.start, padding.end );
650 newChildSize.width = size.width - ( padding.start + padding.end );
651 newChildSize.height = size.height - ( padding.top + padding.bottom );
653 // Cannot use childs Position property as it can already have padding and margin applied on it,
654 // so we end up cumulatively applying them over and over again.
655 Vector2 childOffset( 0.f, 0.f );
656 childOffset.x += ( mImpl->mMargin.start + padding.start );
657 childOffset.y += ( mImpl->mMargin.top + padding.top );
659 child.SetProperty( Actor::Property::POSITION, Vector2( childOffset.x, childOffset.y ) );
661 container.Add( child, newChildSize );
664 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
667 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
671 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
675 Vector3 Control::GetNaturalSize()
677 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::GetNaturalSize for %s\n", Self().GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() );
678 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
682 visual.GetNaturalSize( naturalSize );
683 naturalSize.width += ( mImpl->mPadding.start + mImpl->mPadding.end );
684 naturalSize.height += ( mImpl->mPadding.top + mImpl->mPadding.bottom );
685 return Vector3( naturalSize );
687 return Vector3::ZERO;
690 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
692 return CalculateChildSizeBase( child, dimension );
695 float Control::GetHeightForWidth( float width )
697 return GetHeightForWidthBase( width );
700 float Control::GetWidthForHeight( float height )
702 return GetWidthForHeightBase( height );
705 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
707 return RelayoutDependentOnChildrenBase( dimension );
710 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
714 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
718 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
720 mImpl->SignalConnected( slotObserver, callback );
723 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
725 mImpl->SignalDisconnected( slotObserver, callback );
728 Control& GetImplementation( Dali::Toolkit::Control& handle )
730 CustomActorImpl& customInterface = handle.GetImplementation();
731 // downcast to control
732 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
736 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
738 const CustomActorImpl& customInterface = handle.GetImplementation();
739 // downcast to control
740 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
744 } // namespace Internal
746 } // namespace Toolkit