2 * Copyright (c) 2018 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/internal/styling/style-manager-impl.h>
44 #include <dali-toolkit/internal/visuals/color/color-visual.h>
45 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
46 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
60 #if defined(DEBUG_ENABLED)
61 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
65 * @brief Replace the background visual if it's a color visual with the renderIfTransparent property set as required.
66 * @param[in] controlImpl The control implementation
67 * @param[in] renderIfTransaparent Whether we should render if the color is transparent
69 void ChangeBackgroundColorVisual( Control& controlImpl, bool renderIfTransparent )
71 Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
73 Toolkit::Visual::Base backgroundVisual = controlDataImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
74 if( backgroundVisual )
77 backgroundVisual.CreatePropertyMap( map );
78 Property::Value* typeValue = map.Find( Toolkit::Visual::Property::TYPE );
79 if( typeValue && typeValue->Get< int >() == Toolkit::Visual::COLOR )
81 // Only change it if it's a color visual
82 map[ Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT ] = renderIfTransparent;
83 controlImpl.SetBackground( map );
89 * @brief Creates a clipping renderer if required.
90 * (EG. If no renders exist and clipping is enabled).
91 * @param[in] controlImpl The control implementation.
93 void CreateClippingRenderer( Control& controlImpl )
95 // We want to add a transparent background if we do not have one for clipping.
96 Actor self( controlImpl.Self() );
97 int clippingMode = ClippingMode::DISABLED;
98 if( self.GetProperty( Actor::Property::CLIPPING_MODE ).Get( clippingMode ) )
100 switch( clippingMode )
102 case ClippingMode::CLIP_CHILDREN:
104 if( self.GetRendererCount() == 0u )
106 Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
107 if( controlDataImpl.mVisuals.Empty() )
109 controlImpl.SetBackgroundColor( Color::TRANSPARENT );
113 // We have visuals, check if we've set the background and re-create it to
114 // render even if transparent (only if it's a color visual)
115 ChangeBackgroundColorVisual( controlImpl, true );
121 case ClippingMode::DISABLED:
122 case ClippingMode::CLIP_TO_BOUNDING_BOX:
124 // If we have a background visual, check if it's a color visual and remove the render if transparent flag
125 ChangeBackgroundColorVisual( controlImpl, false );
132 } // unnamed namespace
135 Toolkit::Control Control::New()
137 // Create the implementation, temporarily owned on stack
138 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
140 // Pass ownership to handle
141 Toolkit::Control handle( *controlImpl );
143 // Second-phase init of the implementation
144 // This can only be done after the CustomActor connection has been made...
145 controlImpl->Initialize();
150 void Control::SetStyleName( const std::string& styleName )
152 if( styleName != mImpl->mStyleName )
154 mImpl->mStyleName = styleName;
156 // Apply new style, if stylemanager is available
157 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
160 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
165 const std::string& Control::GetStyleName() const
167 return mImpl->mStyleName;
170 void Control::SetBackgroundColor( const Vector4& color )
172 mImpl->mBackgroundColor = color;
174 map[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::COLOR;
175 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
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;
185 SetBackground( map );
188 Vector4 Control::GetBackgroundColor() const
190 return mImpl->mBackgroundColor;
193 void Control::SetBackground( const Property::Map& map )
195 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
196 visual.SetName("background");
199 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
201 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
206 void Control::SetBackgroundImage( Image image )
208 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
211 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
215 void Control::ClearBackground()
217 mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
218 mImpl->mBackgroundColor = Color::TRANSPARENT;
220 // Trigger a size negotiation request that may be needed when unregistering a visual.
224 void Control::EnableGestureDetection(Gesture::Type type)
226 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
228 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
229 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
230 mImpl->mPinchGestureDetector.Attach(Self());
233 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
235 mImpl->mPanGestureDetector = PanGestureDetector::New();
236 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
237 mImpl->mPanGestureDetector.Attach(Self());
240 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
242 mImpl->mTapGestureDetector = TapGestureDetector::New();
243 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
244 mImpl->mTapGestureDetector.Attach(Self());
247 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
249 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
250 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
251 mImpl->mLongPressGestureDetector.Attach(Self());
255 void Control::DisableGestureDetection(Gesture::Type type)
257 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
259 mImpl->mPinchGestureDetector.Detach(Self());
260 mImpl->mPinchGestureDetector.Reset();
263 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
265 mImpl->mPanGestureDetector.Detach(Self());
266 mImpl->mPanGestureDetector.Reset();
269 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
271 mImpl->mTapGestureDetector.Detach(Self());
272 mImpl->mTapGestureDetector.Reset();
275 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
277 mImpl->mLongPressGestureDetector.Detach(Self());
278 mImpl->mLongPressGestureDetector.Reset();
282 PinchGestureDetector Control::GetPinchGestureDetector() const
284 return mImpl->mPinchGestureDetector;
287 PanGestureDetector Control::GetPanGestureDetector() const
289 return mImpl->mPanGestureDetector;
292 TapGestureDetector Control::GetTapGestureDetector() const
294 return mImpl->mTapGestureDetector;
297 LongPressGestureDetector Control::GetLongPressGestureDetector() const
299 return mImpl->mLongPressGestureDetector;
302 void Control::SetKeyboardNavigationSupport(bool isSupported)
304 mImpl->mIsKeyboardNavigationSupported = isSupported;
307 bool Control::IsKeyboardNavigationSupported()
309 return mImpl->mIsKeyboardNavigationSupported;
312 void Control::SetKeyInputFocus()
314 if( Self().OnStage() )
316 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
320 bool Control::HasKeyInputFocus()
323 if( Self().OnStage() )
325 Toolkit::Control control = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
326 if( Self() == control )
334 void Control::ClearKeyInputFocus()
336 if( Self().OnStage() )
338 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
342 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
344 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
346 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
347 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
350 bool Control::IsKeyboardFocusGroup()
352 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
355 void Control::AccessibilityActivate()
357 // Inform deriving classes
358 OnAccessibilityActivated();
361 void Control::KeyboardEnter()
363 // Inform deriving classes
367 bool Control::OnAccessibilityActivated()
369 return false; // Accessibility activation is not handled by default
372 bool Control::OnKeyboardEnter()
374 return false; // Keyboard enter is not handled by default
377 bool Control::OnAccessibilityPan(PanGesture gesture)
379 return false; // Accessibility pan gesture is not handled by default
382 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
384 return false; // Accessibility touch event is not handled by default
387 bool Control::OnAccessibilityValueChange(bool isIncrease)
389 return false; // Accessibility value change action is not handled by default
392 bool Control::OnAccessibilityZoom()
394 return false; // Accessibility zoom action is not handled by default
397 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
402 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
406 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
408 return mImpl->mKeyEventSignal;
411 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
413 return mImpl->mKeyInputFocusGainedSignal;
416 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
418 return mImpl->mKeyInputFocusLostSignal;
421 bool Control::EmitKeyEventSignal( const KeyEvent& event )
423 // Guard against destruction during signal emission
424 Dali::Toolkit::Control handle( GetOwner() );
426 bool consumed = false;
428 consumed = mImpl->FilterKeyEvent( event );
430 // signals are allocated dynamically when someone connects
431 if ( !consumed && !mImpl->mKeyEventSignal.Empty() )
433 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
438 // Notification for derived classes
439 consumed = OnKeyEvent(event);
445 Control::Control( ControlBehaviour behaviourFlags )
446 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
447 mImpl(new Impl(*this))
449 mImpl->mFlags = behaviourFlags;
457 void Control::Initialize()
459 // Call deriving classes so initialised before styling is applied to them.
462 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
463 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
465 Toolkit::StyleManager styleManager = StyleManager::Get();
467 // if stylemanager is available
470 StyleManager& styleManagerImpl = GetImpl( styleManager );
472 // Register for style changes
473 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
475 // Apply the current style
476 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
480 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
482 SetKeyboardNavigationSupport( true );
486 void Control::OnInitialize()
490 void Control::OnControlChildAdd( Actor& child )
494 void Control::OnControlChildRemove( Actor& child )
498 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
500 // By default the control is only interested in theme (not font) changes
501 if( styleManager && change == StyleChange::THEME_CHANGE )
503 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
508 void Control::OnPinch(const PinchGesture& pinch)
510 if( !( mImpl->mStartingPinchScale ) )
513 mImpl->mStartingPinchScale = new Vector3;
516 if( pinch.state == Gesture::Started )
518 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
521 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
524 void Control::OnPan( const PanGesture& pan )
528 void Control::OnTap(const TapGesture& tap)
532 void Control::OnLongPress( const LongPressGesture& longPress )
536 void Control::EmitKeyInputFocusSignal( bool focusGained )
538 Dali::Toolkit::Control handle( GetOwner() );
542 // signals are allocated dynamically when someone connects
543 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
545 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
550 // signals are allocated dynamically when someone connects
551 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
553 mImpl->mKeyInputFocusLostSignal.Emit( handle );
558 void Control::OnStageConnection( int depth )
560 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
562 Actor self( Self() );
564 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
566 // Check whether the visual is empty and enabled
567 if( (*iter)->visual && (*iter)->enabled )
569 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
570 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
574 // The clipping renderer is only created if required.
575 CreateClippingRenderer( *this );
577 // Request to be laid out when the control is connected to the Stage.
578 Toolkit::DevelControl::RequestLayout( *this );
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)
598 // Notify derived classes.
599 OnControlChildAdd( child );
602 void Control::OnChildRemove(Actor& child)
604 // Notify derived classes.
605 OnControlChildRemove( child );
608 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
610 // If the clipping mode has been set, we may need to create a renderer.
611 // Only do this if we are already on-stage as the OnStageConnection will handle the off-stage clipping controls.
612 if( ( index == Actor::Property::CLIPPING_MODE ) && Self().OnStage() )
614 // Note: This method will handle whether creation of the renderer is required.
615 CreateClippingRenderer( *this );
619 void Control::OnSizeSet(const Vector3& targetSize)
621 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
624 Vector2 size( targetSize );
625 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
629 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
631 // @todo size negotiate background to new size, animate as well?
634 bool Control::OnTouchEvent(const TouchEvent& event)
636 return false; // Do not consume
639 bool Control::OnHoverEvent(const HoverEvent& event)
641 return false; // Do not consume
644 bool Control::OnKeyEvent(const KeyEvent& event)
646 return false; // Do not consume
649 bool Control::OnWheelEvent(const WheelEvent& event)
651 return false; // Do not consume
654 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
656 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
658 Actor child = Self().GetChildAt( i );
659 Vector2 newChildSize( size );
661 // When setting the padding or margin on the control child should be resized and repositioned for legacy reasons.
662 if( ( mImpl->mPadding.start != 0 ) || ( mImpl->mPadding.end != 0 ) || ( mImpl->mPadding.top != 0 ) || ( mImpl->mPadding.bottom != 0 ) ||
663 ( mImpl->mMargin.start != 0 ) || ( mImpl->mMargin.end != 0 ) || ( mImpl->mMargin.top != 0 ) || ( mImpl->mMargin.bottom != 0 ) )
665 // Cannot use childs Position property as it can already have margin applied on it,
666 // so we end up cumulatively applying them over and over again.
667 Toolkit::Control childControl = Toolkit::Control::DownCast( child );
669 // If control not a LayoutItem layout then must be the old Relayout algorithm hence account
670 // for margins and padding.
671 // Padding is incorrect but may have to keep this functionality for compatibility.
672 if ( childControl && ! Toolkit::DevelControl::GetLayout( childControl ) )
674 Extents padding = mImpl->mPadding;
676 Dali::CustomActor ownerActor(GetOwner());
677 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( ownerActor.GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
679 if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
681 std::swap( padding.start, padding.end );
684 // Child size should include padding, this is the wrong use of padding but kept for compatibility.
685 newChildSize.width = size.width - ( padding.start + padding.end );
686 newChildSize.height = size.height - ( padding.top + padding.bottom );
688 // Cannot use childs Position property as it can already have padding and margin applied on it,
689 // so we end up cumulatively applying them over and over again.
690 Vector2 childOffset( 0.f, 0.f );
691 childOffset.x += ( mImpl->mMargin.start + padding.start );
692 childOffset.y += ( mImpl->mMargin.top + padding.top );
694 child.SetPosition( childOffset.x, childOffset.y );
698 container.Add( child, size );
701 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
704 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
708 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
712 Vector3 Control::GetNaturalSize()
714 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::GetNaturalSize for %s\n", Self().GetName().c_str() );
715 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
719 visual.GetNaturalSize( naturalSize );
720 naturalSize.width += ( mImpl->mPadding.start + mImpl->mPadding.end );
721 naturalSize.height += ( mImpl->mPadding.top + mImpl->mPadding.bottom );
722 return Vector3( naturalSize );
724 return Vector3::ZERO;
727 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
729 return CalculateChildSizeBase( child, dimension );
732 float Control::GetHeightForWidth( float width )
734 return GetHeightForWidthBase( width );
737 float Control::GetWidthForHeight( float height )
739 return GetWidthForHeightBase( height );
742 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
744 return RelayoutDependentOnChildrenBase( dimension );
747 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
751 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
755 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
757 mImpl->SignalConnected( slotObserver, callback );
760 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
762 mImpl->SignalDisconnected( slotObserver, callback );
765 Control& GetImplementation( Dali::Toolkit::Control& handle )
767 CustomActorImpl& customInterface = handle.GetImplementation();
768 // downcast to control
769 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
773 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
775 const CustomActorImpl& customInterface = handle.GetImplementation();
776 // downcast to control
777 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
781 } // namespace Internal
783 } // namespace Toolkit