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/devel-api/visuals/visual-properties-devel.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/devel-api/align-enums.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");
63 DALI_ENUM_TO_STRING_TABLE_BEGIN( CLIPPING_MODE )
64 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, DISABLED )
65 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, CLIP_CHILDREN )
66 DALI_ENUM_TO_STRING_TABLE_END( CLIPPING_MODE )
68 } // unnamed namespace
72 Toolkit::Control Control::New()
74 // Create the implementation, temporarily owned on stack
75 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
77 // Pass ownership to handle
78 Toolkit::Control handle( *controlImpl );
80 // Second-phase init of the implementation
81 // This can only be done after the CustomActor connection has been made...
82 controlImpl->Initialize();
87 void Control::SetStyleName( const std::string& styleName )
89 if( styleName != mImpl->mStyleName )
91 mImpl->mStyleName = styleName;
93 // Apply new style, if stylemanager is available
94 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
97 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
102 const std::string& Control::GetStyleName() const
104 return mImpl->mStyleName;
107 void Control::SetBackgroundColor( const Vector4& color )
109 mImpl->mBackgroundColor = color;
111 map[ Toolkit::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
112 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
114 SetBackground( map );
117 Vector4 Control::GetBackgroundColor() const
119 return mImpl->mBackgroundColor;
122 void Control::SetBackground( const Property::Map& map )
124 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
127 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, float( DepthIndex::BACKGROUND ) );
129 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
134 void Control::SetBackgroundImage( Image image )
136 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
139 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, float( DepthIndex::BACKGROUND ) );
143 void Control::ClearBackground()
145 mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
146 mImpl->mBackgroundColor = Color::TRANSPARENT;
148 // Trigger a size negotiation request that may be needed when unregistering a visual.
152 void Control::EnableGestureDetection(Gesture::Type type)
154 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
156 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
157 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
158 mImpl->mPinchGestureDetector.Attach(Self());
161 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
163 mImpl->mPanGestureDetector = PanGestureDetector::New();
164 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
165 mImpl->mPanGestureDetector.Attach(Self());
168 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
170 mImpl->mTapGestureDetector = TapGestureDetector::New();
171 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
172 mImpl->mTapGestureDetector.Attach(Self());
175 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
177 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
178 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
179 mImpl->mLongPressGestureDetector.Attach(Self());
183 void Control::DisableGestureDetection(Gesture::Type type)
185 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
187 mImpl->mPinchGestureDetector.Detach(Self());
188 mImpl->mPinchGestureDetector.Reset();
191 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
193 mImpl->mPanGestureDetector.Detach(Self());
194 mImpl->mPanGestureDetector.Reset();
197 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
199 mImpl->mTapGestureDetector.Detach(Self());
200 mImpl->mTapGestureDetector.Reset();
203 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
205 mImpl->mLongPressGestureDetector.Detach(Self());
206 mImpl->mLongPressGestureDetector.Reset();
210 PinchGestureDetector Control::GetPinchGestureDetector() const
212 return mImpl->mPinchGestureDetector;
215 PanGestureDetector Control::GetPanGestureDetector() const
217 return mImpl->mPanGestureDetector;
220 TapGestureDetector Control::GetTapGestureDetector() const
222 return mImpl->mTapGestureDetector;
225 LongPressGestureDetector Control::GetLongPressGestureDetector() const
227 return mImpl->mLongPressGestureDetector;
230 void Control::SetKeyboardNavigationSupport(bool isSupported)
232 mImpl->mIsKeyboardNavigationSupported = isSupported;
235 bool Control::IsKeyboardNavigationSupported()
237 return mImpl->mIsKeyboardNavigationSupported;
240 void Control::SetKeyInputFocus()
242 if( Self().OnStage() )
244 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
248 bool Control::HasKeyInputFocus()
251 if( Self().OnStage() )
253 Toolkit::Control control = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
254 if( Self() == control )
262 void Control::ClearKeyInputFocus()
264 if( Self().OnStage() )
266 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
270 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
272 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
274 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
275 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
278 bool Control::IsKeyboardFocusGroup()
280 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
283 void Control::AccessibilityActivate()
285 // Inform deriving classes
286 OnAccessibilityActivated();
289 void Control::KeyboardEnter()
291 // Inform deriving classes
295 bool Control::OnAccessibilityActivated()
297 return false; // Accessibility activation is not handled by default
300 bool Control::OnKeyboardEnter()
302 return false; // Keyboard enter is not handled by default
305 bool Control::OnAccessibilityPan(PanGesture gesture)
307 return false; // Accessibility pan gesture is not handled by default
310 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
312 return false; // Accessibility touch event is not handled by default
315 bool Control::OnAccessibilityValueChange(bool isIncrease)
317 return false; // Accessibility value change action is not handled by default
320 bool Control::OnAccessibilityZoom()
322 return false; // Accessibility zoom action is not handled by default
325 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
330 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
334 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
336 return mImpl->mKeyEventSignal;
339 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
341 return mImpl->mKeyInputFocusGainedSignal;
344 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
346 return mImpl->mKeyInputFocusLostSignal;
349 bool Control::EmitKeyEventSignal( const KeyEvent& event )
351 // Guard against destruction during signal emission
352 Dali::Toolkit::Control handle( GetOwner() );
354 bool consumed = false;
356 // signals are allocated dynamically when someone connects
357 if ( !mImpl->mKeyEventSignal.Empty() )
359 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
364 // Notification for derived classes
365 consumed = OnKeyEvent(event);
371 Control::Control( ControlBehaviour behaviourFlags )
372 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
373 mImpl(new Impl(*this))
375 mImpl->mFlags = behaviourFlags;
383 void Control::Initialize()
385 // Call deriving classes so initialised before styling is applied to them.
388 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
389 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
391 Toolkit::StyleManager styleManager = StyleManager::Get();
393 // if stylemanager is available
396 StyleManager& styleManagerImpl = GetImpl( styleManager );
398 // Register for style changes
399 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
401 // Apply the current style
402 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
406 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
408 SetKeyboardNavigationSupport( true );
412 void Control::OnInitialize()
416 void Control::OnControlChildAdd( Actor& child )
420 void Control::OnControlChildRemove( Actor& child )
424 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
426 // By default the control is only interested in theme (not font) changes
427 if( styleManager && change == StyleChange::THEME_CHANGE )
429 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
434 void Control::OnPinch(const PinchGesture& pinch)
436 if( !( mImpl->mStartingPinchScale ) )
439 mImpl->mStartingPinchScale = new Vector3;
442 if( pinch.state == Gesture::Started )
444 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
447 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
450 void Control::OnPan( const PanGesture& pan )
454 void Control::OnTap(const TapGesture& tap)
458 void Control::OnLongPress( const LongPressGesture& longPress )
462 void Control::EmitKeyInputFocusSignal( bool focusGained )
464 Dali::Toolkit::Control handle( GetOwner() );
468 // signals are allocated dynamically when someone connects
469 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
471 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
476 // signals are allocated dynamically when someone connects
477 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
479 mImpl->mKeyInputFocusLostSignal.Emit( handle );
484 void Control::OnStageConnection( int depth )
486 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
488 Actor self( Self() );
490 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
492 // Check whether the visual is empty and enabled
493 if( (*iter)->visual && (*iter)->enabled )
495 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
496 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
500 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() )
502 Property::Value clippingValue = self.GetProperty( Actor::Property::CLIPPING_MODE );
503 int clippingMode = ClippingMode::DISABLED;
504 if( clippingValue.Get( clippingMode ) )
506 // Add a transparent background if we do not have any renderers or visuals so we clip our children
508 if( clippingMode == ClippingMode::CLIP_CHILDREN )
510 // Create a transparent background visual which will also get staged.
511 SetBackgroundColor( Color::TRANSPARENT );
517 void Control::OnStageDisconnection()
519 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
521 // Check whether the visual is empty
522 if( (*iter)->visual )
524 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
525 Actor self( Self() );
526 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
531 void Control::OnKeyInputFocusGained()
533 EmitKeyInputFocusSignal( true );
536 void Control::OnKeyInputFocusLost()
538 EmitKeyInputFocusSignal( false );
541 void Control::OnChildAdd(Actor& child)
543 // Notify derived classes.
544 OnControlChildAdd( child );
547 void Control::OnChildRemove(Actor& child)
549 // Notify derived classes.
550 OnControlChildRemove( child );
553 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
555 Actor self( Self() );
556 if( index == Actor::Property::CLIPPING_MODE )
558 // Only set the background if we're already on the stage and have no renderers or visuals
560 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() && self.OnStage() )
562 ClippingMode::Type clippingMode = ClippingMode::DISABLED;
563 if( Scripting::GetEnumerationProperty< ClippingMode::Type >( propertyValue, CLIPPING_MODE_TABLE, CLIPPING_MODE_TABLE_COUNT, clippingMode ) )
565 // Add a transparent background if we do not have one so we clip children
567 if( clippingMode == ClippingMode::CLIP_CHILDREN )
569 SetBackgroundColor( Color::TRANSPARENT );
576 void Control::OnSizeSet(const Vector3& targetSize)
578 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
581 Vector2 size( targetSize );
582 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
586 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
588 // @todo size negotiate background to new size, animate as well?
591 bool Control::OnTouchEvent(const TouchEvent& event)
593 return false; // Do not consume
596 bool Control::OnHoverEvent(const HoverEvent& event)
598 return false; // Do not consume
601 bool Control::OnKeyEvent(const KeyEvent& event)
603 return false; // Do not consume
606 bool Control::OnWheelEvent(const WheelEvent& event)
608 return false; // Do not consume
611 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
613 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
615 container.Add( Self().GetChildAt( i ), size );
618 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
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::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
629 Vector3 Control::GetNaturalSize()
631 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
635 visual.GetNaturalSize( naturalSize );
636 return Vector3( naturalSize );
638 return Vector3::ZERO;
641 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
643 return CalculateChildSizeBase( child, dimension );
646 float Control::GetHeightForWidth( float width )
648 return GetHeightForWidthBase( width );
651 float Control::GetWidthForHeight( float height )
653 return GetWidthForHeightBase( height );
656 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
658 return RelayoutDependentOnChildrenBase( dimension );
661 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
665 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
669 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
671 mImpl->SignalConnected( slotObserver, callback );
674 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
676 mImpl->SignalDisconnected( slotObserver, callback );
679 Control& GetImplementation( Dali::Toolkit::Control& handle )
681 CustomActorImpl& customInterface = handle.GetImplementation();
682 // downcast to control
683 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
687 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
689 const CustomActorImpl& customInterface = handle.GetImplementation();
690 // downcast to control
691 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
695 } // namespace Internal
697 } // namespace Toolkit