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 );
86 } // unnamed namespace
89 Toolkit::Control Control::New()
91 // Create the implementation, temporarily owned on stack
92 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
94 // Pass ownership to handle
95 Toolkit::Control handle( *controlImpl );
97 // Second-phase init of the implementation
98 // This can only be done after the CustomActor connection has been made...
99 controlImpl->Initialize();
104 void Control::SetStyleName( const std::string& styleName )
106 if( styleName != mImpl->mStyleName )
108 mImpl->mStyleName = styleName;
110 // Apply new style, if stylemanager is available
111 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
114 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
119 const std::string& Control::GetStyleName() const
121 return mImpl->mStyleName;
124 void Control::SetBackgroundColor( const Vector4& color )
126 mImpl->mBackgroundColor = color;
128 map[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::COLOR;
129 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
131 SetBackground( map );
134 Vector4 Control::GetBackgroundColor() const
136 return mImpl->mBackgroundColor;
139 void Control::SetBackground( const Property::Map& map )
141 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
142 visual.SetName("background");
145 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
147 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
152 void Control::SetBackgroundImage( Image image )
154 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
157 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
161 void Control::ClearBackground()
163 mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
164 mImpl->mBackgroundColor = Color::TRANSPARENT;
166 // Trigger a size negotiation request that may be needed when unregistering a visual.
170 void Control::EnableGestureDetection(Gesture::Type type)
172 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
174 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
175 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
176 mImpl->mPinchGestureDetector.Attach(Self());
179 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
181 mImpl->mPanGestureDetector = PanGestureDetector::New();
182 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
183 mImpl->mPanGestureDetector.Attach(Self());
186 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
188 mImpl->mTapGestureDetector = TapGestureDetector::New();
189 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
190 mImpl->mTapGestureDetector.Attach(Self());
193 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
195 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
196 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
197 mImpl->mLongPressGestureDetector.Attach(Self());
201 void Control::DisableGestureDetection(Gesture::Type type)
203 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
205 mImpl->mPinchGestureDetector.Detach(Self());
206 mImpl->mPinchGestureDetector.Reset();
209 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
211 mImpl->mPanGestureDetector.Detach(Self());
212 mImpl->mPanGestureDetector.Reset();
215 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
217 mImpl->mTapGestureDetector.Detach(Self());
218 mImpl->mTapGestureDetector.Reset();
221 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
223 mImpl->mLongPressGestureDetector.Detach(Self());
224 mImpl->mLongPressGestureDetector.Reset();
228 PinchGestureDetector Control::GetPinchGestureDetector() const
230 return mImpl->mPinchGestureDetector;
233 PanGestureDetector Control::GetPanGestureDetector() const
235 return mImpl->mPanGestureDetector;
238 TapGestureDetector Control::GetTapGestureDetector() const
240 return mImpl->mTapGestureDetector;
243 LongPressGestureDetector Control::GetLongPressGestureDetector() const
245 return mImpl->mLongPressGestureDetector;
248 void Control::SetKeyboardNavigationSupport(bool isSupported)
250 mImpl->mIsKeyboardNavigationSupported = isSupported;
253 bool Control::IsKeyboardNavigationSupported()
255 return mImpl->mIsKeyboardNavigationSupported;
258 void Control::SetKeyInputFocus()
260 if( Self().OnStage() )
262 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
266 bool Control::HasKeyInputFocus()
269 if( Self().OnStage() )
271 Toolkit::Control control = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
272 if( Self() == control )
280 void Control::ClearKeyInputFocus()
282 if( Self().OnStage() )
284 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
288 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
290 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
292 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
293 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
296 bool Control::IsKeyboardFocusGroup()
298 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
301 void Control::AccessibilityActivate()
303 // Inform deriving classes
304 OnAccessibilityActivated();
307 void Control::KeyboardEnter()
309 // Inform deriving classes
313 bool Control::OnAccessibilityActivated()
315 return false; // Accessibility activation is not handled by default
318 bool Control::OnKeyboardEnter()
320 return false; // Keyboard enter is not handled by default
323 bool Control::OnAccessibilityPan(PanGesture gesture)
325 return false; // Accessibility pan gesture is not handled by default
328 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
330 return false; // Accessibility touch event is not handled by default
333 bool Control::OnAccessibilityValueChange(bool isIncrease)
335 return false; // Accessibility value change action is not handled by default
338 bool Control::OnAccessibilityZoom()
340 return false; // Accessibility zoom action is not handled by default
343 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
348 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
352 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
354 return mImpl->mKeyEventSignal;
357 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
359 return mImpl->mKeyInputFocusGainedSignal;
362 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
364 return mImpl->mKeyInputFocusLostSignal;
367 bool Control::EmitKeyEventSignal( const KeyEvent& event )
369 // Guard against destruction during signal emission
370 Dali::Toolkit::Control handle( GetOwner() );
372 bool consumed = false;
374 // signals are allocated dynamically when someone connects
375 if ( !mImpl->mKeyEventSignal.Empty() )
377 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
382 // Notification for derived classes
383 consumed = OnKeyEvent(event);
389 Control::Control( ControlBehaviour behaviourFlags )
390 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
391 mImpl(new Impl(*this))
393 mImpl->mFlags = behaviourFlags;
401 void Control::Initialize()
403 // Call deriving classes so initialised before styling is applied to them.
406 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
407 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
409 Toolkit::StyleManager styleManager = StyleManager::Get();
411 // if stylemanager is available
414 StyleManager& styleManagerImpl = GetImpl( styleManager );
416 // Register for style changes
417 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
419 // Apply the current style
420 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
424 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
426 SetKeyboardNavigationSupport( true );
430 void Control::OnInitialize()
434 void Control::OnControlChildAdd( Actor& child )
438 void Control::OnControlChildRemove( Actor& child )
442 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
444 // By default the control is only interested in theme (not font) changes
445 if( styleManager && change == StyleChange::THEME_CHANGE )
447 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
452 void Control::OnPinch(const PinchGesture& pinch)
454 if( !( mImpl->mStartingPinchScale ) )
457 mImpl->mStartingPinchScale = new Vector3;
460 if( pinch.state == Gesture::Started )
462 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
465 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
468 void Control::OnPan( const PanGesture& pan )
472 void Control::OnTap(const TapGesture& tap)
476 void Control::OnLongPress( const LongPressGesture& longPress )
480 void Control::EmitKeyInputFocusSignal( bool focusGained )
482 Dali::Toolkit::Control handle( GetOwner() );
486 // signals are allocated dynamically when someone connects
487 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
489 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
494 // signals are allocated dynamically when someone connects
495 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
497 mImpl->mKeyInputFocusLostSignal.Emit( handle );
502 void Control::OnStageConnection( int depth )
504 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
506 Actor self( Self() );
508 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
510 // Check whether the visual is empty and enabled
511 if( (*iter)->visual && (*iter)->enabled )
513 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
514 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
518 // The clipping renderer is only created if required.
519 CreateClippingRenderer( *this );
522 void Control::OnStageDisconnection()
524 mImpl->OnStageDisconnection();
527 void Control::OnKeyInputFocusGained()
529 EmitKeyInputFocusSignal( true );
532 void Control::OnKeyInputFocusLost()
534 EmitKeyInputFocusSignal( false );
537 void Control::OnChildAdd(Actor& child)
539 // Notify derived classes.
540 OnControlChildAdd( child );
543 void Control::OnChildRemove(Actor& child)
545 // Notify derived classes.
546 OnControlChildRemove( child );
549 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
551 // If the clipping mode has been set, we may need to create a renderer.
552 // Only do this if we are already on-stage as the OnStageConnection will handle the off-stage clipping controls.
553 if( ( index == Actor::Property::CLIPPING_MODE ) && Self().OnStage() )
555 // Note: This method will handle whether creation of the renderer is required.
556 CreateClippingRenderer( *this );
560 void Control::OnSizeSet(const Vector3& targetSize)
562 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
565 Vector2 size( targetSize );
566 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
570 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
572 // @todo size negotiate background to new size, animate as well?
575 bool Control::OnTouchEvent(const TouchEvent& event)
577 return false; // Do not consume
580 bool Control::OnHoverEvent(const HoverEvent& event)
582 return false; // Do not consume
585 bool Control::OnKeyEvent(const KeyEvent& event)
587 return false; // Do not consume
590 bool Control::OnWheelEvent(const WheelEvent& event)
592 return false; // Do not consume
595 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
597 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
599 container.Add( Self().GetChildAt( i ), size );
602 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
605 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
609 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
613 Vector3 Control::GetNaturalSize()
615 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
619 visual.GetNaturalSize( naturalSize );
620 return Vector3( naturalSize );
622 return Vector3::ZERO;
625 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
627 return CalculateChildSizeBase( child, dimension );
630 float Control::GetHeightForWidth( float width )
632 return GetHeightForWidthBase( width );
635 float Control::GetWidthForHeight( float height )
637 return GetWidthForHeightBase( height );
640 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
642 return RelayoutDependentOnChildrenBase( dimension );
645 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
649 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
653 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
655 mImpl->SignalConnected( slotObserver, callback );
658 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
660 mImpl->SignalDisconnected( slotObserver, callback );
663 Control& GetImplementation( Dali::Toolkit::Control& handle )
665 CustomActorImpl& customInterface = handle.GetImplementation();
666 // downcast to control
667 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
671 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
673 const CustomActorImpl& customInterface = handle.GetImplementation();
674 // downcast to control
675 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
679 } // namespace Internal
681 } // namespace Toolkit