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(Gesture::Type type)
233 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
235 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
236 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
237 mImpl->mPinchGestureDetector.Attach(Self());
240 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
242 mImpl->mPanGestureDetector = PanGestureDetector::New();
243 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
244 mImpl->mPanGestureDetector.Attach(Self());
247 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
249 mImpl->mTapGestureDetector = TapGestureDetector::New();
250 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
251 mImpl->mTapGestureDetector.Attach(Self());
254 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
256 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
257 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
258 mImpl->mLongPressGestureDetector.Attach(Self());
262 void Control::DisableGestureDetection(Gesture::Type type)
264 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
266 mImpl->mPinchGestureDetector.Detach(Self());
267 mImpl->mPinchGestureDetector.Reset();
270 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
272 mImpl->mPanGestureDetector.Detach(Self());
273 mImpl->mPanGestureDetector.Reset();
276 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
278 mImpl->mTapGestureDetector.Detach(Self());
279 mImpl->mTapGestureDetector.Reset();
282 if ( (type & Gesture::LongPress) && 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::OnAccessibilityTouch(const TouchEvent& touchEvent)
391 return false; // Accessibility touch event is not handled by default
394 bool Control::OnAccessibilityValueChange(bool isIncrease)
396 return false; // Accessibility value change action is not handled by default
399 bool Control::OnAccessibilityZoom()
401 return false; // Accessibility zoom action is not handled by default
404 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
409 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
413 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
415 return mImpl->mKeyEventSignal;
418 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
420 return mImpl->mKeyInputFocusGainedSignal;
423 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
425 return mImpl->mKeyInputFocusLostSignal;
428 bool Control::EmitKeyEventSignal( const KeyEvent& event )
430 // Guard against destruction during signal emission
431 Dali::Toolkit::Control handle( GetOwner() );
433 bool consumed = false;
435 consumed = mImpl->FilterKeyEvent( event );
437 // signals are allocated dynamically when someone connects
438 if ( !consumed && !mImpl->mKeyEventSignal.Empty() )
440 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
445 // Notification for derived classes
446 consumed = OnKeyEvent(event);
452 Control::Control( ControlBehaviour behaviourFlags )
453 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
454 mImpl(new Impl(*this))
456 mImpl->mFlags = behaviourFlags;
464 void Control::Initialize()
466 // Call deriving classes so initialised before styling is applied to them.
469 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
470 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
472 Toolkit::StyleManager styleManager = StyleManager::Get();
474 // if stylemanager is available
477 StyleManager& styleManagerImpl = GetImpl( styleManager );
479 // Register for style changes
480 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
482 // Apply the current style
483 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
487 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
489 SetKeyboardNavigationSupport( true );
493 void Control::OnInitialize()
497 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
499 // By default the control is only interested in theme (not font) changes
500 if( styleManager && change == StyleChange::THEME_CHANGE )
502 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
507 void Control::OnPinch(const PinchGesture& pinch)
509 if( !( mImpl->mStartingPinchScale ) )
512 mImpl->mStartingPinchScale = new Vector3;
515 if( pinch.state == Gesture::Started )
517 *( mImpl->mStartingPinchScale ) = Self().GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
520 Self().SetProperty( Actor::Property::SCALE, *( mImpl->mStartingPinchScale ) * pinch.scale );
523 void Control::OnPan( const PanGesture& pan )
527 void Control::OnTap(const TapGesture& tap)
531 void Control::OnLongPress( const LongPressGesture& longPress )
535 void Control::EmitKeyInputFocusSignal( bool focusGained )
537 Dali::Toolkit::Control handle( GetOwner() );
541 // signals are allocated dynamically when someone connects
542 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
544 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
549 // signals are allocated dynamically when someone connects
550 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
552 mImpl->mKeyInputFocusLostSignal.Emit( handle );
557 void Control::OnStageConnection( int depth )
559 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
561 Actor self( Self() );
563 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
565 // Check whether the visual is empty and enabled
566 if( (*iter)->visual && (*iter)->enabled )
568 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
569 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
573 // The clipping renderer is only created if required.
574 CreateClippingRenderer( *this );
576 // Request to be laid out when the control is connected to the Stage.
577 // Signal that a Relayout may be needed
581 void Control::OnStageDisconnection()
583 mImpl->OnStageDisconnection();
586 void Control::OnKeyInputFocusGained()
588 EmitKeyInputFocusSignal( true );
591 void Control::OnKeyInputFocusLost()
593 EmitKeyInputFocusSignal( false );
596 void Control::OnChildAdd(Actor& child)
600 void Control::OnChildRemove(Actor& child)
604 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
606 // If the clipping mode has been set, we may need to create a renderer.
607 // Only do this if we are already on-stage as the OnStageConnection will handle the off-stage clipping controls.
608 if( ( index == Actor::Property::CLIPPING_MODE ) && Self().GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
610 // Note: This method will handle whether creation of the renderer is required.
611 CreateClippingRenderer( *this );
615 void Control::OnSizeSet(const Vector3& targetSize)
617 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
620 Vector2 size( targetSize );
621 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
625 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
627 // @todo size negotiate background to new size, animate as well?
630 bool Control::OnTouchEvent(const TouchEvent& event)
632 return false; // Do not consume
635 bool Control::OnHoverEvent(const HoverEvent& event)
637 return false; // Do not consume
640 bool Control::OnKeyEvent(const KeyEvent& event)
642 return false; // Do not consume
645 bool Control::OnWheelEvent(const WheelEvent& event)
647 return false; // Do not consume
650 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
652 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
654 Actor child = Self().GetChildAt( i );
655 Vector2 newChildSize( size );
657 // When set the padding or margin on the control, child should be resized and repositioned.
658 if( ( mImpl->mPadding.start != 0 ) || ( mImpl->mPadding.end != 0 ) || ( mImpl->mPadding.top != 0 ) || ( mImpl->mPadding.bottom != 0 ) ||
659 ( mImpl->mMargin.start != 0 ) || ( mImpl->mMargin.end != 0 ) || ( mImpl->mMargin.top != 0 ) || ( mImpl->mMargin.bottom != 0 ) )
661 Extents padding = mImpl->mPadding;
663 Dali::CustomActor ownerActor(GetOwner());
664 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( ownerActor.GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
666 if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
668 std::swap( padding.start, padding.end );
671 newChildSize.width = size.width - ( padding.start + padding.end );
672 newChildSize.height = size.height - ( padding.top + padding.bottom );
674 // Cannot use childs Position property as it can already have padding and margin applied on it,
675 // so we end up cumulatively applying them over and over again.
676 Vector2 childOffset( 0.f, 0.f );
677 childOffset.x += ( mImpl->mMargin.start + padding.start );
678 childOffset.y += ( mImpl->mMargin.top + padding.top );
680 child.SetProperty( Actor::Property::POSITION, Vector2( childOffset.x, childOffset.y ) );
682 container.Add( child, newChildSize );
685 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
688 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
692 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
696 Vector3 Control::GetNaturalSize()
698 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::GetNaturalSize for %s\n", Self().GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() );
699 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
703 visual.GetNaturalSize( naturalSize );
704 naturalSize.width += ( mImpl->mPadding.start + mImpl->mPadding.end );
705 naturalSize.height += ( mImpl->mPadding.top + mImpl->mPadding.bottom );
706 return Vector3( naturalSize );
708 return Vector3::ZERO;
711 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
713 return CalculateChildSizeBase( child, dimension );
716 float Control::GetHeightForWidth( float width )
718 return GetHeightForWidthBase( width );
721 float Control::GetWidthForHeight( float height )
723 return GetWidthForHeightBase( height );
726 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
728 return RelayoutDependentOnChildrenBase( dimension );
731 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
735 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
739 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
741 mImpl->SignalConnected( slotObserver, callback );
744 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
746 mImpl->SignalDisconnected( slotObserver, callback );
749 Control& GetImplementation( Dali::Toolkit::Control& handle )
751 CustomActorImpl& customInterface = handle.GetImplementation();
752 // downcast to control
753 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
757 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
759 const CustomActorImpl& customInterface = handle.GetImplementation();
760 // downcast to control
761 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
765 } // namespace Internal
767 } // namespace Toolkit