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 );
128 visual.SetDepthIndex( DepthIndex::BACKGROUND );
130 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
135 void Control::SetBackgroundImage( Image image )
137 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
140 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
141 visual.SetDepthIndex( DepthIndex::BACKGROUND );
145 void Control::ClearBackground()
147 mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
148 mImpl->mBackgroundColor = Color::TRANSPARENT;
150 // Trigger a size negotiation request that may be needed when unregistering a visual.
154 void Control::EnableGestureDetection(Gesture::Type type)
156 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
158 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
159 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
160 mImpl->mPinchGestureDetector.Attach(Self());
163 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
165 mImpl->mPanGestureDetector = PanGestureDetector::New();
166 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
167 mImpl->mPanGestureDetector.Attach(Self());
170 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
172 mImpl->mTapGestureDetector = TapGestureDetector::New();
173 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
174 mImpl->mTapGestureDetector.Attach(Self());
177 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
179 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
180 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
181 mImpl->mLongPressGestureDetector.Attach(Self());
185 void Control::DisableGestureDetection(Gesture::Type type)
187 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
189 mImpl->mPinchGestureDetector.Detach(Self());
190 mImpl->mPinchGestureDetector.Reset();
193 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
195 mImpl->mPanGestureDetector.Detach(Self());
196 mImpl->mPanGestureDetector.Reset();
199 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
201 mImpl->mTapGestureDetector.Detach(Self());
202 mImpl->mTapGestureDetector.Reset();
205 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
207 mImpl->mLongPressGestureDetector.Detach(Self());
208 mImpl->mLongPressGestureDetector.Reset();
212 PinchGestureDetector Control::GetPinchGestureDetector() const
214 return mImpl->mPinchGestureDetector;
217 PanGestureDetector Control::GetPanGestureDetector() const
219 return mImpl->mPanGestureDetector;
222 TapGestureDetector Control::GetTapGestureDetector() const
224 return mImpl->mTapGestureDetector;
227 LongPressGestureDetector Control::GetLongPressGestureDetector() const
229 return mImpl->mLongPressGestureDetector;
232 void Control::SetKeyboardNavigationSupport(bool isSupported)
234 mImpl->mIsKeyboardNavigationSupported = isSupported;
237 bool Control::IsKeyboardNavigationSupported()
239 return mImpl->mIsKeyboardNavigationSupported;
242 void Control::SetKeyInputFocus()
244 if( Self().OnStage() )
246 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
250 bool Control::HasKeyInputFocus()
253 if( Self().OnStage() )
255 Toolkit::Control control = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
256 if( Self() == control )
264 void Control::ClearKeyInputFocus()
266 if( Self().OnStage() )
268 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
272 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
274 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
276 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
277 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
280 bool Control::IsKeyboardFocusGroup()
282 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
285 void Control::AccessibilityActivate()
287 // Inform deriving classes
288 OnAccessibilityActivated();
291 void Control::KeyboardEnter()
293 // Inform deriving classes
297 bool Control::OnAccessibilityActivated()
299 return false; // Accessibility activation is not handled by default
302 bool Control::OnKeyboardEnter()
304 return false; // Keyboard enter is not handled by default
307 bool Control::OnAccessibilityPan(PanGesture gesture)
309 return false; // Accessibility pan gesture is not handled by default
312 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
314 return false; // Accessibility touch event is not handled by default
317 bool Control::OnAccessibilityValueChange(bool isIncrease)
319 return false; // Accessibility value change action is not handled by default
322 bool Control::OnAccessibilityZoom()
324 return false; // Accessibility zoom action is not handled by default
327 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
332 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
336 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
338 return mImpl->mKeyEventSignal;
341 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
343 return mImpl->mKeyInputFocusGainedSignal;
346 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
348 return mImpl->mKeyInputFocusLostSignal;
351 bool Control::EmitKeyEventSignal( const KeyEvent& event )
353 // Guard against destruction during signal emission
354 Dali::Toolkit::Control handle( GetOwner() );
356 bool consumed = false;
358 // signals are allocated dynamically when someone connects
359 if ( !mImpl->mKeyEventSignal.Empty() )
361 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
366 // Notification for derived classes
367 consumed = OnKeyEvent(event);
373 Control::Control( ControlBehaviour behaviourFlags )
374 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
375 mImpl(new Impl(*this))
377 mImpl->mFlags = behaviourFlags;
385 void Control::Initialize()
387 // Call deriving classes so initialised before styling is applied to them.
390 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
391 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
393 Toolkit::StyleManager styleManager = StyleManager::Get();
395 // if stylemanager is available
398 StyleManager& styleManagerImpl = GetImpl( styleManager );
400 // Register for style changes
401 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
403 // Apply the current style
404 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
408 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
410 SetKeyboardNavigationSupport( true );
414 void Control::OnInitialize()
418 void Control::OnControlChildAdd( Actor& child )
422 void Control::OnControlChildRemove( Actor& child )
426 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
428 // By default the control is only interested in theme (not font) changes
429 if( styleManager && change == StyleChange::THEME_CHANGE )
431 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
436 void Control::OnPinch(const PinchGesture& pinch)
438 if( !( mImpl->mStartingPinchScale ) )
441 mImpl->mStartingPinchScale = new Vector3;
444 if( pinch.state == Gesture::Started )
446 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
449 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
452 void Control::OnPan( const PanGesture& pan )
456 void Control::OnTap(const TapGesture& tap)
460 void Control::OnLongPress( const LongPressGesture& longPress )
464 void Control::EmitKeyInputFocusSignal( bool focusGained )
466 Dali::Toolkit::Control handle( GetOwner() );
470 // signals are allocated dynamically when someone connects
471 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
473 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
478 // signals are allocated dynamically when someone connects
479 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
481 mImpl->mKeyInputFocusLostSignal.Emit( handle );
486 void Control::OnStageConnection( int depth )
488 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
490 Actor self( Self() );
492 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
494 // Check whether the visual is empty and enabled
495 if( (*iter)->visual && (*iter)->enabled )
497 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
498 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
502 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() )
504 Property::Value clippingValue = self.GetProperty( Actor::Property::CLIPPING_MODE );
505 int clippingMode = ClippingMode::DISABLED;
506 if( clippingValue.Get( clippingMode ) )
508 // Add a transparent background if we do not have any renderers or visuals so we clip our children
510 if( clippingMode == ClippingMode::CLIP_CHILDREN )
512 // Create a transparent background visual which will also get staged.
513 SetBackgroundColor( Color::TRANSPARENT );
519 void Control::OnStageDisconnection()
521 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
523 // Check whether the visual is empty
524 if( (*iter)->visual )
526 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
527 Actor self( Self() );
528 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
533 void Control::OnKeyInputFocusGained()
535 EmitKeyInputFocusSignal( true );
538 void Control::OnKeyInputFocusLost()
540 EmitKeyInputFocusSignal( false );
543 void Control::OnChildAdd(Actor& child)
545 // Notify derived classes.
546 OnControlChildAdd( child );
549 void Control::OnChildRemove(Actor& child)
551 // Notify derived classes.
552 OnControlChildRemove( child );
555 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
557 Actor self( Self() );
558 if( index == Actor::Property::CLIPPING_MODE )
560 // Only set the background if we're already on the stage and have no renderers or visuals
562 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() && self.OnStage() )
564 ClippingMode::Type clippingMode = ClippingMode::DISABLED;
565 if( Scripting::GetEnumerationProperty< ClippingMode::Type >( propertyValue, CLIPPING_MODE_TABLE, CLIPPING_MODE_TABLE_COUNT, clippingMode ) )
567 // Add a transparent background if we do not have one so we clip children
569 if( clippingMode == ClippingMode::CLIP_CHILDREN )
571 SetBackgroundColor( Color::TRANSPARENT );
578 void Control::OnSizeSet(const Vector3& targetSize)
580 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
583 Vector2 size( targetSize );
584 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
588 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
590 // @todo size negotiate background to new size, animate as well?
593 bool Control::OnTouchEvent(const TouchEvent& event)
595 return false; // Do not consume
598 bool Control::OnHoverEvent(const HoverEvent& event)
600 return false; // Do not consume
603 bool Control::OnKeyEvent(const KeyEvent& event)
605 return false; // Do not consume
608 bool Control::OnWheelEvent(const WheelEvent& event)
610 return false; // Do not consume
613 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
615 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
617 container.Add( Self().GetChildAt( i ), size );
620 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
623 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
627 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
631 Vector3 Control::GetNaturalSize()
633 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
637 visual.GetNaturalSize( naturalSize );
638 return Vector3( naturalSize );
640 return Vector3::ZERO;
643 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
645 return CalculateChildSizeBase( child, dimension );
648 float Control::GetHeightForWidth( float width )
650 return GetHeightForWidthBase( width );
653 float Control::GetWidthForHeight( float height )
655 return GetWidthForHeightBase( height );
658 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
660 return RelayoutDependentOnChildrenBase( dimension );
663 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
667 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
671 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
673 mImpl->SignalConnected( slotObserver, callback );
676 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
678 mImpl->SignalDisconnected( slotObserver, callback );
681 Control& GetImplementation( Dali::Toolkit::Control& handle )
683 CustomActorImpl& customInterface = handle.GetImplementation();
684 // downcast to control
685 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
689 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
691 const CustomActorImpl& customInterface = handle.GetImplementation();
692 // downcast to control
693 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
697 } // namespace Internal
699 } // namespace Toolkit