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 <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/focus-manager/keyboard-focus-manager.h>
34 #include <dali-toolkit/public-api/controls/control.h>
35 #include <dali-toolkit/public-api/styling/style-manager.h>
36 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
37 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
38 #include <dali-toolkit/devel-api/controls/control-devel.h>
39 #include <dali-toolkit/public-api/visuals/visual-properties.h>
40 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
41 #include <dali-toolkit/internal/styling/style-manager-impl.h>
42 #include <dali-toolkit/internal/visuals/color/color-visual.h>
43 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
44 #include <dali-toolkit/public-api/align-enumerations.h>
45 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
59 #if defined(DEBUG_ENABLED)
60 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
64 * @brief Creates a clipping renderer if required.
65 * (EG. If no renders exist and clipping is enabled).
66 * @param[in] controlImpl The control implementation.
68 void CreateClippingRenderer( Control& controlImpl )
70 // We want to add a transparent background if we do not have one for clipping.
71 Actor self( controlImpl.Self() );
72 int clippingMode = ClippingMode::DISABLED;
73 if( self.GetProperty( Actor::Property::CLIPPING_MODE ).Get( clippingMode ) )
75 Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
77 if( ( clippingMode == ClippingMode::CLIP_CHILDREN ) &&
78 controlDataImpl.mVisuals.Empty() &&
79 ( self.GetRendererCount() == 0u ) )
81 controlImpl.SetBackgroundColor( Color::TRANSPARENT );
87 * @brief Sets Control::Property::BACKGROUND visual
88 * @param[in] controlImpl The control implementation
89 * @param[in] visual The control background visual
90 * @param[in] size The current size
92 void SetBackgroundVisual( Control::Impl& controlImpl, Toolkit::Visual::Base& visual, const Vector2& size )
94 Property::Map transformMap = Property::Map();
96 Vector2 newSize( 0.f, 0.f );
97 newSize.width = size.width + ( controlImpl.mPadding.start + controlImpl.mPadding.end );
98 newSize.height = size.height + ( controlImpl.mPadding.top + controlImpl.mPadding.bottom );
100 if( ( controlImpl.mMargin.start != 0 ) ||
101 ( controlImpl.mMargin.end != 0 ) ||
102 ( controlImpl.mMargin.top != 0 ) ||
103 ( controlImpl.mMargin.bottom != 0 ) )
105 transformMap.Add( Toolkit::Visual::Transform::Property::SIZE, newSize )
106 .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
107 .Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2( controlImpl.mMargin.start, controlImpl.mMargin.top ) )
108 .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
109 .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
110 .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN );
112 else if( ( controlImpl.mPadding.start != 0 ) ||
113 ( controlImpl.mPadding.end != 0 ) ||
114 ( controlImpl.mPadding.top != 0 ) ||
115 ( controlImpl.mPadding.bottom != 0 ) )
117 transformMap.Add( Toolkit::Visual::Transform::Property::SIZE, newSize )
118 .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
119 .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
120 .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN );
123 visual.SetTransformAndSize( transformMap, newSize ); // Send an empty map as we do not want to modify the visual's set transform
126 } // unnamed namespace
129 Toolkit::Control Control::New()
131 // Create the implementation, temporarily owned on stack
132 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
134 // Pass ownership to handle
135 Toolkit::Control handle( *controlImpl );
137 // Second-phase init of the implementation
138 // This can only be done after the CustomActor connection has been made...
139 controlImpl->Initialize();
144 void Control::SetStyleName( const std::string& styleName )
146 if( styleName != mImpl->mStyleName )
148 mImpl->mStyleName = styleName;
150 // Apply new style, if stylemanager is available
151 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
154 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
159 const std::string& Control::GetStyleName() const
161 return mImpl->mStyleName;
164 void Control::SetBackgroundColor( const Vector4& color )
166 mImpl->mBackgroundColor = color;
168 map[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::COLOR;
169 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
171 SetBackground( map );
174 Vector4 Control::GetBackgroundColor() const
176 return mImpl->mBackgroundColor;
179 void Control::SetBackground( const Property::Map& map )
181 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
182 visual.SetName("background");
185 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
187 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
192 void Control::SetBackgroundImage( Image image )
194 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
197 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
201 void Control::ClearBackground()
203 mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
204 mImpl->mBackgroundColor = Color::TRANSPARENT;
206 // Trigger a size negotiation request that may be needed when unregistering a visual.
210 void Control::EnableGestureDetection(Gesture::Type type)
212 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
214 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
215 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
216 mImpl->mPinchGestureDetector.Attach(Self());
219 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
221 mImpl->mPanGestureDetector = PanGestureDetector::New();
222 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
223 mImpl->mPanGestureDetector.Attach(Self());
226 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
228 mImpl->mTapGestureDetector = TapGestureDetector::New();
229 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
230 mImpl->mTapGestureDetector.Attach(Self());
233 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
235 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
236 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
237 mImpl->mLongPressGestureDetector.Attach(Self());
241 void Control::DisableGestureDetection(Gesture::Type type)
243 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
245 mImpl->mPinchGestureDetector.Detach(Self());
246 mImpl->mPinchGestureDetector.Reset();
249 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
251 mImpl->mPanGestureDetector.Detach(Self());
252 mImpl->mPanGestureDetector.Reset();
255 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
257 mImpl->mTapGestureDetector.Detach(Self());
258 mImpl->mTapGestureDetector.Reset();
261 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
263 mImpl->mLongPressGestureDetector.Detach(Self());
264 mImpl->mLongPressGestureDetector.Reset();
268 PinchGestureDetector Control::GetPinchGestureDetector() const
270 return mImpl->mPinchGestureDetector;
273 PanGestureDetector Control::GetPanGestureDetector() const
275 return mImpl->mPanGestureDetector;
278 TapGestureDetector Control::GetTapGestureDetector() const
280 return mImpl->mTapGestureDetector;
283 LongPressGestureDetector Control::GetLongPressGestureDetector() const
285 return mImpl->mLongPressGestureDetector;
288 void Control::SetKeyboardNavigationSupport(bool isSupported)
290 mImpl->mIsKeyboardNavigationSupported = isSupported;
293 bool Control::IsKeyboardNavigationSupported()
295 return mImpl->mIsKeyboardNavigationSupported;
298 void Control::SetKeyInputFocus()
300 if( Self().OnStage() )
302 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
306 bool Control::HasKeyInputFocus()
309 if( Self().OnStage() )
311 Toolkit::Control control = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
312 if( Self() == control )
320 void Control::ClearKeyInputFocus()
322 if( Self().OnStage() )
324 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
328 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
330 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
332 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
333 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
336 bool Control::IsKeyboardFocusGroup()
338 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
341 void Control::AccessibilityActivate()
343 // Inform deriving classes
344 OnAccessibilityActivated();
347 void Control::KeyboardEnter()
349 // Inform deriving classes
353 bool Control::OnAccessibilityActivated()
355 return false; // Accessibility activation is not handled by default
358 bool Control::OnKeyboardEnter()
360 return false; // Keyboard enter is not handled by default
363 bool Control::OnAccessibilityPan(PanGesture gesture)
365 return false; // Accessibility pan gesture is not handled by default
368 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
370 return false; // Accessibility touch event is not handled by default
373 bool Control::OnAccessibilityValueChange(bool isIncrease)
375 return false; // Accessibility value change action is not handled by default
378 bool Control::OnAccessibilityZoom()
380 return false; // Accessibility zoom action is not handled by default
383 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
388 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
392 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
394 return mImpl->mKeyEventSignal;
397 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
399 return mImpl->mKeyInputFocusGainedSignal;
402 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
404 return mImpl->mKeyInputFocusLostSignal;
407 bool Control::EmitKeyEventSignal( const KeyEvent& event )
409 // Guard against destruction during signal emission
410 Dali::Toolkit::Control handle( GetOwner() );
412 bool consumed = false;
414 // signals are allocated dynamically when someone connects
415 if ( !mImpl->mKeyEventSignal.Empty() )
417 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
422 // Notification for derived classes
423 consumed = OnKeyEvent(event);
429 Control::Control( ControlBehaviour behaviourFlags )
430 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
431 mImpl(new Impl(*this))
433 mImpl->mFlags = behaviourFlags;
441 void Control::Initialize()
443 // Call deriving classes so initialised before styling is applied to them.
446 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
447 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
449 Toolkit::StyleManager styleManager = StyleManager::Get();
451 // if stylemanager is available
454 StyleManager& styleManagerImpl = GetImpl( styleManager );
456 // Register for style changes
457 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
459 // Apply the current style
460 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
464 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
466 SetKeyboardNavigationSupport( true );
470 void Control::OnInitialize()
474 void Control::OnControlChildAdd( Actor& child )
478 void Control::OnControlChildRemove( Actor& child )
482 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
484 // By default the control is only interested in theme (not font) changes
485 if( styleManager && change == StyleChange::THEME_CHANGE )
487 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
492 void Control::OnPinch(const PinchGesture& pinch)
494 if( !( mImpl->mStartingPinchScale ) )
497 mImpl->mStartingPinchScale = new Vector3;
500 if( pinch.state == Gesture::Started )
502 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
505 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
508 void Control::OnPan( const PanGesture& pan )
512 void Control::OnTap(const TapGesture& tap)
516 void Control::OnLongPress( const LongPressGesture& longPress )
520 void Control::EmitKeyInputFocusSignal( bool focusGained )
522 Dali::Toolkit::Control handle( GetOwner() );
526 // signals are allocated dynamically when someone connects
527 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
529 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
534 // signals are allocated dynamically when someone connects
535 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
537 mImpl->mKeyInputFocusLostSignal.Emit( handle );
542 void Control::OnStageConnection( int depth )
544 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
546 Actor self( Self() );
548 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
550 // Check whether the visual is empty and enabled
551 if( (*iter)->visual && (*iter)->enabled )
553 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
554 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
558 // The clipping renderer is only created if required.
559 CreateClippingRenderer( *this );
562 void Control::OnStageDisconnection()
564 mImpl->OnStageDisconnection();
567 void Control::OnKeyInputFocusGained()
569 EmitKeyInputFocusSignal( true );
572 void Control::OnKeyInputFocusLost()
574 EmitKeyInputFocusSignal( false );
577 void Control::OnChildAdd(Actor& child)
579 // Notify derived classes.
580 OnControlChildAdd( child );
583 void Control::OnChildRemove(Actor& child)
585 // Notify derived classes.
586 OnControlChildRemove( child );
589 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
591 // If the clipping mode has been set, we may need to create a renderer.
592 // Only do this if we are already on-stage as the OnStageConnection will handle the off-stage clipping controls.
593 if( ( index == Actor::Property::CLIPPING_MODE ) && Self().OnStage() )
595 // Note: This method will handle whether creation of the renderer is required.
596 CreateClippingRenderer( *this );
600 void Control::OnSizeSet(const Vector3& targetSize)
602 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
605 Vector2 size( targetSize );
606 SetBackgroundVisual( *mImpl, visual, size );
611 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
613 // @todo size negotiate background to new size, animate as well?
616 bool Control::OnTouchEvent(const TouchEvent& event)
618 return false; // Do not consume
621 bool Control::OnHoverEvent(const HoverEvent& event)
623 return false; // Do not consume
626 bool Control::OnKeyEvent(const KeyEvent& event)
628 return false; // Do not consume
631 bool Control::OnWheelEvent(const WheelEvent& event)
633 return false; // Do not consume
636 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
638 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
640 Actor child = Self().GetChildAt( i );
641 Vector2 newChildSize( size );
643 // When set the padding or margin on the control, child should be resized and repositioned.
644 if( ( mImpl->mPadding.start != 0 ) || ( mImpl->mPadding.end != 0 ) || ( mImpl->mPadding.top != 0 ) || ( mImpl->mPadding.bottom != 0 ) ||
645 ( mImpl->mMargin.start != 0 ) || ( mImpl->mMargin.end != 0 ) || ( mImpl->mMargin.top != 0 ) || ( mImpl->mMargin.bottom != 0 ) )
647 newChildSize.width = size.width - ( mImpl->mPadding.start + mImpl->mPadding.end );
648 newChildSize.height = size.height - ( mImpl->mPadding.top + mImpl->mPadding.bottom );
650 Vector3 childPosition = child.GetTargetSize();
651 childPosition.x += ( mImpl->mMargin.start + mImpl->mPadding.start );
652 childPosition.y += ( mImpl->mMargin.top + mImpl->mPadding.top );
654 child.SetPosition( childPosition );
657 container.Add( child, newChildSize );
660 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
663 SetBackgroundVisual( *mImpl, visual, size );
667 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
671 Vector3 Control::GetNaturalSize()
673 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
677 visual.GetNaturalSize( naturalSize );
678 naturalSize.width += ( mImpl->mPadding.start + mImpl->mPadding.end );
679 naturalSize.height += ( mImpl->mPadding.top + mImpl->mPadding.bottom );
680 return Vector3( naturalSize );
682 return Vector3::ZERO;
685 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
687 return CalculateChildSizeBase( child, dimension );
690 float Control::GetHeightForWidth( float width )
692 return GetHeightForWidthBase( width );
695 float Control::GetWidthForHeight( float height )
697 return GetWidthForHeightBase( height );
700 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
702 return RelayoutDependentOnChildrenBase( dimension );
705 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
709 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
713 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
715 mImpl->SignalConnected( slotObserver, callback );
718 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
720 mImpl->SignalDisconnected( slotObserver, callback );
723 Control& GetImplementation( Dali::Toolkit::Control& handle )
725 CustomActorImpl& customInterface = handle.GetImplementation();
726 // downcast to control
727 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
731 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
733 const CustomActorImpl& customInterface = handle.GetImplementation();
734 // downcast to control
735 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
739 } // namespace Internal
741 } // namespace Toolkit