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 );
125 visual.SetName("background");
128 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, 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, DepthIndex::BACKGROUND );
144 void Control::ClearBackground()
146 mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
147 mImpl->mBackgroundColor = Color::TRANSPARENT;
149 // Trigger a size negotiation request that may be needed when unregistering a visual.
153 void Control::EnableGestureDetection(Gesture::Type type)
155 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
157 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
158 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
159 mImpl->mPinchGestureDetector.Attach(Self());
162 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
164 mImpl->mPanGestureDetector = PanGestureDetector::New();
165 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
166 mImpl->mPanGestureDetector.Attach(Self());
169 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
171 mImpl->mTapGestureDetector = TapGestureDetector::New();
172 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
173 mImpl->mTapGestureDetector.Attach(Self());
176 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
178 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
179 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
180 mImpl->mLongPressGestureDetector.Attach(Self());
184 void Control::DisableGestureDetection(Gesture::Type type)
186 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
188 mImpl->mPinchGestureDetector.Detach(Self());
189 mImpl->mPinchGestureDetector.Reset();
192 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
194 mImpl->mPanGestureDetector.Detach(Self());
195 mImpl->mPanGestureDetector.Reset();
198 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
200 mImpl->mTapGestureDetector.Detach(Self());
201 mImpl->mTapGestureDetector.Reset();
204 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
206 mImpl->mLongPressGestureDetector.Detach(Self());
207 mImpl->mLongPressGestureDetector.Reset();
211 PinchGestureDetector Control::GetPinchGestureDetector() const
213 return mImpl->mPinchGestureDetector;
216 PanGestureDetector Control::GetPanGestureDetector() const
218 return mImpl->mPanGestureDetector;
221 TapGestureDetector Control::GetTapGestureDetector() const
223 return mImpl->mTapGestureDetector;
226 LongPressGestureDetector Control::GetLongPressGestureDetector() const
228 return mImpl->mLongPressGestureDetector;
231 void Control::SetKeyboardNavigationSupport(bool isSupported)
233 mImpl->mIsKeyboardNavigationSupported = isSupported;
236 bool Control::IsKeyboardNavigationSupported()
238 return mImpl->mIsKeyboardNavigationSupported;
241 void Control::SetKeyInputFocus()
243 if( Self().OnStage() )
245 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
249 bool Control::HasKeyInputFocus()
252 if( Self().OnStage() )
254 Toolkit::Control control = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
255 if( Self() == control )
263 void Control::ClearKeyInputFocus()
265 if( Self().OnStage() )
267 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
271 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
273 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
275 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
276 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
279 bool Control::IsKeyboardFocusGroup()
281 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
284 void Control::AccessibilityActivate()
286 // Inform deriving classes
287 OnAccessibilityActivated();
290 void Control::KeyboardEnter()
292 // Inform deriving classes
296 bool Control::OnAccessibilityActivated()
298 return false; // Accessibility activation is not handled by default
301 bool Control::OnKeyboardEnter()
303 return false; // Keyboard enter is not handled by default
306 bool Control::OnAccessibilityPan(PanGesture gesture)
308 return false; // Accessibility pan gesture is not handled by default
311 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
313 return false; // Accessibility touch event is not handled by default
316 bool Control::OnAccessibilityValueChange(bool isIncrease)
318 return false; // Accessibility value change action is not handled by default
321 bool Control::OnAccessibilityZoom()
323 return false; // Accessibility zoom action is not handled by default
326 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
331 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
335 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
337 return mImpl->mKeyEventSignal;
340 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
342 return mImpl->mKeyInputFocusGainedSignal;
345 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
347 return mImpl->mKeyInputFocusLostSignal;
350 bool Control::EmitKeyEventSignal( const KeyEvent& event )
352 // Guard against destruction during signal emission
353 Dali::Toolkit::Control handle( GetOwner() );
355 bool consumed = false;
357 // signals are allocated dynamically when someone connects
358 if ( !mImpl->mKeyEventSignal.Empty() )
360 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
365 // Notification for derived classes
366 consumed = OnKeyEvent(event);
372 Control::Control( ControlBehaviour behaviourFlags )
373 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
374 mImpl(new Impl(*this))
376 mImpl->mFlags = behaviourFlags;
384 void Control::Initialize()
386 // Call deriving classes so initialised before styling is applied to them.
389 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
390 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
392 Toolkit::StyleManager styleManager = StyleManager::Get();
394 // if stylemanager is available
397 StyleManager& styleManagerImpl = GetImpl( styleManager );
399 // Register for style changes
400 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
402 // Apply the current style
403 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
407 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
409 SetKeyboardNavigationSupport( true );
413 void Control::OnInitialize()
417 void Control::OnControlChildAdd( Actor& child )
421 void Control::OnControlChildRemove( Actor& child )
425 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
427 // By default the control is only interested in theme (not font) changes
428 if( styleManager && change == StyleChange::THEME_CHANGE )
430 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
435 void Control::OnPinch(const PinchGesture& pinch)
437 if( !( mImpl->mStartingPinchScale ) )
440 mImpl->mStartingPinchScale = new Vector3;
443 if( pinch.state == Gesture::Started )
445 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
448 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
451 void Control::OnPan( const PanGesture& pan )
455 void Control::OnTap(const TapGesture& tap)
459 void Control::OnLongPress( const LongPressGesture& longPress )
463 void Control::EmitKeyInputFocusSignal( bool focusGained )
465 Dali::Toolkit::Control handle( GetOwner() );
469 // signals are allocated dynamically when someone connects
470 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
472 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
477 // signals are allocated dynamically when someone connects
478 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
480 mImpl->mKeyInputFocusLostSignal.Emit( handle );
485 void Control::OnStageConnection( int depth )
487 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
489 Actor self( Self() );
491 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
493 // Check whether the visual is empty and enabled
494 if( (*iter)->visual && (*iter)->enabled )
496 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
497 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
501 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() )
503 Property::Value clippingValue = self.GetProperty( Actor::Property::CLIPPING_MODE );
504 int clippingMode = ClippingMode::DISABLED;
505 if( clippingValue.Get( clippingMode ) )
507 // Add a transparent background if we do not have any renderers or visuals so we clip our children
509 if( clippingMode == ClippingMode::CLIP_CHILDREN )
511 // Create a transparent background visual which will also get staged.
512 SetBackgroundColor( Color::TRANSPARENT );
518 void Control::OnStageDisconnection()
520 mImpl->OnStageDisconnection();
523 void Control::OnKeyInputFocusGained()
525 EmitKeyInputFocusSignal( true );
528 void Control::OnKeyInputFocusLost()
530 EmitKeyInputFocusSignal( false );
533 void Control::OnChildAdd(Actor& child)
535 // Notify derived classes.
536 OnControlChildAdd( child );
539 void Control::OnChildRemove(Actor& child)
541 // Notify derived classes.
542 OnControlChildRemove( child );
545 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
547 Actor self( Self() );
548 if( index == Actor::Property::CLIPPING_MODE )
550 // Only set the background if we're already on the stage and have no renderers or visuals
552 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() && self.OnStage() )
554 ClippingMode::Type clippingMode = ClippingMode::DISABLED;
555 if( Scripting::GetEnumerationProperty< ClippingMode::Type >( propertyValue, CLIPPING_MODE_TABLE, CLIPPING_MODE_TABLE_COUNT, clippingMode ) )
557 // Add a transparent background if we do not have one so we clip children
559 if( clippingMode == ClippingMode::CLIP_CHILDREN )
561 SetBackgroundColor( Color::TRANSPARENT );
568 void Control::OnSizeSet(const Vector3& targetSize)
570 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
573 Vector2 size( targetSize );
574 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
578 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
580 // @todo size negotiate background to new size, animate as well?
583 bool Control::OnTouchEvent(const TouchEvent& event)
585 return false; // Do not consume
588 bool Control::OnHoverEvent(const HoverEvent& event)
590 return false; // Do not consume
593 bool Control::OnKeyEvent(const KeyEvent& event)
595 return false; // Do not consume
598 bool Control::OnWheelEvent(const WheelEvent& event)
600 return false; // Do not consume
603 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
605 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
607 container.Add( Self().GetChildAt( i ), size );
610 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
613 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
617 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
621 Vector3 Control::GetNaturalSize()
623 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
627 visual.GetNaturalSize( naturalSize );
628 return Vector3( naturalSize );
630 return Vector3::ZERO;
633 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
635 return CalculateChildSizeBase( child, dimension );
638 float Control::GetHeightForWidth( float width )
640 return GetHeightForWidthBase( width );
643 float Control::GetWidthForHeight( float height )
645 return GetWidthForHeightBase( height );
648 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
650 return RelayoutDependentOnChildrenBase( dimension );
653 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
657 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
661 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
663 mImpl->SignalConnected( slotObserver, callback );
666 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
668 mImpl->SignalDisconnected( slotObserver, callback );
671 Control& GetImplementation( Dali::Toolkit::Control& handle )
673 CustomActorImpl& customInterface = handle.GetImplementation();
674 // downcast to control
675 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
679 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
681 const CustomActorImpl& customInterface = handle.GetImplementation();
682 // downcast to control
683 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
687 } // namespace Internal
689 } // namespace Toolkit