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 } // unnamed namespace
66 Toolkit::Control Control::New()
68 // Create the implementation, temporarily owned on stack
69 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
71 // Pass ownership to handle
72 Toolkit::Control handle( *controlImpl );
74 // Second-phase init of the implementation
75 // This can only be done after the CustomActor connection has been made...
76 controlImpl->Initialize();
81 void Control::SetStyleName( const std::string& styleName )
83 if( styleName != mImpl->mStyleName )
85 mImpl->mStyleName = styleName;
87 // Apply new style, if stylemanager is available
88 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
91 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
96 const std::string& Control::GetStyleName() const
98 return mImpl->mStyleName;
101 void Control::SetBackgroundColor( const Vector4& color )
103 mImpl->mBackgroundColor = color;
105 map[ Toolkit::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
106 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
108 SetBackground( map );
111 Vector4 Control::GetBackgroundColor() const
113 return mImpl->mBackgroundColor;
116 void Control::SetBackground( const Property::Map& map )
118 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
119 visual.SetName("background");
122 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
124 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
129 void Control::SetBackgroundImage( Image image )
131 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
134 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
138 void Control::ClearBackground()
140 mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
141 mImpl->mBackgroundColor = Color::TRANSPARENT;
143 // Trigger a size negotiation request that may be needed when unregistering a visual.
147 void Control::EnableGestureDetection(Gesture::Type type)
149 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
151 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
152 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
153 mImpl->mPinchGestureDetector.Attach(Self());
156 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
158 mImpl->mPanGestureDetector = PanGestureDetector::New();
159 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
160 mImpl->mPanGestureDetector.Attach(Self());
163 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
165 mImpl->mTapGestureDetector = TapGestureDetector::New();
166 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
167 mImpl->mTapGestureDetector.Attach(Self());
170 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
172 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
173 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
174 mImpl->mLongPressGestureDetector.Attach(Self());
178 void Control::DisableGestureDetection(Gesture::Type type)
180 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
182 mImpl->mPinchGestureDetector.Detach(Self());
183 mImpl->mPinchGestureDetector.Reset();
186 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
188 mImpl->mPanGestureDetector.Detach(Self());
189 mImpl->mPanGestureDetector.Reset();
192 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
194 mImpl->mTapGestureDetector.Detach(Self());
195 mImpl->mTapGestureDetector.Reset();
198 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
200 mImpl->mLongPressGestureDetector.Detach(Self());
201 mImpl->mLongPressGestureDetector.Reset();
205 PinchGestureDetector Control::GetPinchGestureDetector() const
207 return mImpl->mPinchGestureDetector;
210 PanGestureDetector Control::GetPanGestureDetector() const
212 return mImpl->mPanGestureDetector;
215 TapGestureDetector Control::GetTapGestureDetector() const
217 return mImpl->mTapGestureDetector;
220 LongPressGestureDetector Control::GetLongPressGestureDetector() const
222 return mImpl->mLongPressGestureDetector;
225 void Control::SetKeyboardNavigationSupport(bool isSupported)
227 mImpl->mIsKeyboardNavigationSupported = isSupported;
230 bool Control::IsKeyboardNavigationSupported()
232 return mImpl->mIsKeyboardNavigationSupported;
235 void Control::SetKeyInputFocus()
237 if( Self().OnStage() )
239 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
243 bool Control::HasKeyInputFocus()
246 if( Self().OnStage() )
248 Toolkit::Control control = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
249 if( Self() == control )
257 void Control::ClearKeyInputFocus()
259 if( Self().OnStage() )
261 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
265 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
267 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
269 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
270 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
273 bool Control::IsKeyboardFocusGroup()
275 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
278 void Control::AccessibilityActivate()
280 // Inform deriving classes
281 OnAccessibilityActivated();
284 void Control::KeyboardEnter()
286 // Inform deriving classes
290 bool Control::OnAccessibilityActivated()
292 return false; // Accessibility activation is not handled by default
295 bool Control::OnKeyboardEnter()
297 return false; // Keyboard enter is not handled by default
300 bool Control::OnAccessibilityPan(PanGesture gesture)
302 return false; // Accessibility pan gesture is not handled by default
305 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
307 return false; // Accessibility touch event is not handled by default
310 bool Control::OnAccessibilityValueChange(bool isIncrease)
312 return false; // Accessibility value change action is not handled by default
315 bool Control::OnAccessibilityZoom()
317 return false; // Accessibility zoom action is not handled by default
320 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
325 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
329 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
331 return mImpl->mKeyEventSignal;
334 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
336 return mImpl->mKeyInputFocusGainedSignal;
339 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
341 return mImpl->mKeyInputFocusLostSignal;
344 bool Control::EmitKeyEventSignal( const KeyEvent& event )
346 // Guard against destruction during signal emission
347 Dali::Toolkit::Control handle( GetOwner() );
349 bool consumed = false;
351 // signals are allocated dynamically when someone connects
352 if ( !mImpl->mKeyEventSignal.Empty() )
354 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
359 // Notification for derived classes
360 consumed = OnKeyEvent(event);
366 Control::Control( ControlBehaviour behaviourFlags )
367 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
368 mImpl(new Impl(*this))
370 mImpl->mFlags = behaviourFlags;
378 void Control::Initialize()
380 // Call deriving classes so initialised before styling is applied to them.
383 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
384 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
386 Toolkit::StyleManager styleManager = StyleManager::Get();
388 // if stylemanager is available
391 StyleManager& styleManagerImpl = GetImpl( styleManager );
393 // Register for style changes
394 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
396 // Apply the current style
397 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
401 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
403 SetKeyboardNavigationSupport( true );
407 void Control::OnInitialize()
411 void Control::OnControlChildAdd( Actor& child )
415 void Control::OnControlChildRemove( Actor& child )
419 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
421 // By default the control is only interested in theme (not font) changes
422 if( styleManager && change == StyleChange::THEME_CHANGE )
424 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
429 void Control::OnPinch(const PinchGesture& pinch)
431 if( !( mImpl->mStartingPinchScale ) )
434 mImpl->mStartingPinchScale = new Vector3;
437 if( pinch.state == Gesture::Started )
439 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
442 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
445 void Control::OnPan( const PanGesture& pan )
449 void Control::OnTap(const TapGesture& tap)
453 void Control::OnLongPress( const LongPressGesture& longPress )
457 void Control::EmitKeyInputFocusSignal( bool focusGained )
459 Dali::Toolkit::Control handle( GetOwner() );
463 // signals are allocated dynamically when someone connects
464 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
466 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
471 // signals are allocated dynamically when someone connects
472 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
474 mImpl->mKeyInputFocusLostSignal.Emit( handle );
479 void Control::CreateClippingRenderer()
481 // We want to add a transparent background if we do not have one for clipping.
482 Actor self( Self() );
483 int clippingMode = ClippingMode::DISABLED;
484 if( self.GetProperty( Actor::Property::CLIPPING_MODE ).Get( clippingMode ) )
486 if( ( clippingMode != ClippingMode::DISABLED ) &&
487 mImpl->mVisuals.Empty() &&
488 ( self.GetRendererCount() == 0u ) )
490 SetBackgroundColor( Color::TRANSPARENT );
495 void Control::OnStageConnection( int depth )
497 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
499 Actor self( Self() );
501 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
503 // Check whether the visual is empty and enabled
504 if( (*iter)->visual && (*iter)->enabled )
506 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
507 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
511 // The clipping renderer is only created if required.
512 CreateClippingRenderer();
515 void Control::OnStageDisconnection()
517 mImpl->OnStageDisconnection();
520 void Control::OnKeyInputFocusGained()
522 EmitKeyInputFocusSignal( true );
525 void Control::OnKeyInputFocusLost()
527 EmitKeyInputFocusSignal( false );
530 void Control::OnChildAdd(Actor& child)
532 // Notify derived classes.
533 OnControlChildAdd( child );
536 void Control::OnChildRemove(Actor& child)
538 // Notify derived classes.
539 OnControlChildRemove( child );
542 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
544 // If the clipping mode has been set, we may need to create a renderer.
545 // Only do this if we are already on-stage as the OnStageConnection will handle the off-stage clipping controls.
546 if( ( index == Actor::Property::CLIPPING_MODE ) && Self().OnStage() )
548 // Note: This method will handle whether creation of the renderer is required.
549 CreateClippingRenderer();
553 void Control::OnSizeSet(const Vector3& targetSize)
555 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
558 Vector2 size( targetSize );
559 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
563 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
565 // @todo size negotiate background to new size, animate as well?
568 bool Control::OnTouchEvent(const TouchEvent& event)
570 return false; // Do not consume
573 bool Control::OnHoverEvent(const HoverEvent& event)
575 return false; // Do not consume
578 bool Control::OnKeyEvent(const KeyEvent& event)
580 return false; // Do not consume
583 bool Control::OnWheelEvent(const WheelEvent& event)
585 return false; // Do not consume
588 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
590 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
592 container.Add( Self().GetChildAt( i ), size );
595 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
598 visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
602 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
606 Vector3 Control::GetNaturalSize()
608 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
612 visual.GetNaturalSize( naturalSize );
613 return Vector3( naturalSize );
615 return Vector3::ZERO;
618 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
620 return CalculateChildSizeBase( child, dimension );
623 float Control::GetHeightForWidth( float width )
625 return GetHeightForWidthBase( width );
628 float Control::GetWidthForHeight( float height )
630 return GetWidthForHeightBase( height );
633 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
635 return RelayoutDependentOnChildrenBase( dimension );
638 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
642 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
646 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
648 mImpl->SignalConnected( slotObserver, callback );
651 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
653 mImpl->SignalDisconnected( slotObserver, callback );
656 Control& GetImplementation( Dali::Toolkit::Control& handle )
658 CustomActorImpl& customInterface = handle.GetImplementation();
659 // downcast to control
660 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
664 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
666 const CustomActorImpl& customInterface = handle.GetImplementation();
667 // downcast to control
668 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
672 } // namespace Internal
674 } // namespace Toolkit