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 & REQUIRES_STYLE_CHANGE_SIGNALS) ||
465 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
467 Toolkit::StyleManager styleManager = StyleManager::Get();
469 // if stylemanager is available
472 StyleManager& styleManagerImpl = GetImpl( styleManager );
474 // Register for style changes
475 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
477 // Apply the current style
478 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
482 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
484 SetKeyboardNavigationSupport( true );
488 void Control::OnInitialize()
492 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
494 // By default the control is only interested in theme (not font) changes
495 if( styleManager && change == StyleChange::THEME_CHANGE )
497 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
502 void Control::OnPinch(const PinchGesture& pinch)
504 if( !( mImpl->mStartingPinchScale ) )
507 mImpl->mStartingPinchScale = new Vector3;
510 if( pinch.GetState() == GestureState::STARTED )
512 *( mImpl->mStartingPinchScale ) = Self().GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
515 Self().SetProperty( Actor::Property::SCALE, *( mImpl->mStartingPinchScale ) * pinch.GetScale() );
518 void Control::OnPan( const PanGesture& pan )
522 void Control::OnTap(const TapGesture& tap)
526 void Control::OnLongPress( const LongPressGesture& longPress )
530 void Control::EmitKeyInputFocusSignal( bool focusGained )
532 Dali::Toolkit::Control handle( GetOwner() );
536 // signals are allocated dynamically when someone connects
537 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
539 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
544 // signals are allocated dynamically when someone connects
545 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
547 mImpl->mKeyInputFocusLostSignal.Emit( handle );
552 void Control::OnSceneConnection( int depth )
554 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnSceneConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
556 Actor self( Self() );
558 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
560 // Check whether the visual is empty and enabled
561 if( (*iter)->visual && (*iter)->enabled )
563 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnSceneConnection Setting visual(%d) on scene\n", (*iter)->index );
564 Toolkit::GetImplementation((*iter)->visual).SetOnScene( self );
568 // The clipping renderer is only created if required.
569 CreateClippingRenderer( *this );
571 // Request to be laid out when the control is connected to the Scene.
572 // Signal that a Relayout may be needed
576 void Control::OnSceneDisconnection()
578 mImpl->OnSceneDisconnection();
581 void Control::OnKeyInputFocusGained()
583 EmitKeyInputFocusSignal( true );
586 void Control::OnKeyInputFocusLost()
588 EmitKeyInputFocusSignal( false );
591 void Control::OnChildAdd(Actor& child)
595 void Control::OnChildRemove(Actor& child)
599 void Control::OnPropertySet( Property::Index index, const Property::Value& propertyValue )
601 // If the clipping mode has been set, we may need to create a renderer.
602 // Only do this if we are already on-stage as the OnSceneConnection will handle the off-stage clipping controls.
603 if( ( index == Actor::Property::CLIPPING_MODE ) && Self().GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
605 // Note: This method will handle whether creation of the renderer is required.
606 CreateClippingRenderer( *this );
610 void Control::OnSizeSet(const Vector3& targetSize)
612 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
615 Vector2 size( targetSize );
616 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
620 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
622 // @todo size negotiate background to new size, animate as well?
625 bool Control::OnHoverEvent(const HoverEvent& event)
627 return false; // Do not consume
630 bool Control::OnKeyEvent(const KeyEvent& event)
632 return false; // Do not consume
635 bool Control::OnWheelEvent(const WheelEvent& event)
637 return false; // Do not consume
640 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
642 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
644 Actor child = Self().GetChildAt( i );
645 Vector2 newChildSize( size );
647 // When set the padding or margin on the control, child should be resized and repositioned.
648 if( ( mImpl->mPadding.start != 0 ) || ( mImpl->mPadding.end != 0 ) || ( mImpl->mPadding.top != 0 ) || ( mImpl->mPadding.bottom != 0 ) ||
649 ( mImpl->mMargin.start != 0 ) || ( mImpl->mMargin.end != 0 ) || ( mImpl->mMargin.top != 0 ) || ( mImpl->mMargin.bottom != 0 ) )
651 Extents padding = mImpl->mPadding;
653 Dali::CustomActor ownerActor(GetOwner());
654 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( ownerActor.GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
656 if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
658 std::swap( padding.start, padding.end );
661 newChildSize.width = size.width - ( padding.start + padding.end );
662 newChildSize.height = size.height - ( padding.top + padding.bottom );
664 // Cannot use childs Position property as it can already have padding and margin applied on it,
665 // so we end up cumulatively applying them over and over again.
666 Vector2 childOffset( 0.f, 0.f );
667 childOffset.x += ( mImpl->mMargin.start + padding.start );
668 childOffset.y += ( mImpl->mMargin.top + padding.top );
670 child.SetProperty( Actor::Property::POSITION, Vector2( childOffset.x, childOffset.y ) );
672 container.Add( child, newChildSize );
675 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
678 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
682 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
686 Vector3 Control::GetNaturalSize()
688 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::GetNaturalSize for %s\n", Self().GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() );
689 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
693 visual.GetNaturalSize( naturalSize );
694 naturalSize.width += ( mImpl->mPadding.start + mImpl->mPadding.end );
695 naturalSize.height += ( mImpl->mPadding.top + mImpl->mPadding.bottom );
696 return Vector3( naturalSize );
698 return Vector3::ZERO;
701 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
703 return CalculateChildSizeBase( child, dimension );
706 float Control::GetHeightForWidth( float width )
708 return GetHeightForWidthBase( width );
711 float Control::GetWidthForHeight( float height )
713 return GetWidthForHeightBase( height );
716 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
718 return RelayoutDependentOnChildrenBase( dimension );
721 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
725 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
729 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
731 mImpl->SignalConnected( slotObserver, callback );
734 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
736 mImpl->SignalDisconnected( slotObserver, callback );
739 Control& GetImplementation( Dali::Toolkit::Control& handle )
741 CustomActorImpl& customInterface = handle.GetImplementation();
742 // downcast to control
743 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
747 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
749 const CustomActorImpl& customInterface = handle.GetImplementation();
750 // downcast to control
751 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
755 } // namespace Internal
757 } // namespace Toolkit