2 * Copyright (c) 2016 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
25 #include <dali/public-api/animation/constraint.h>
26 #include <dali/public-api/animation/constraints.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/public-api/object/type-registry-helper.h>
29 #include <dali/public-api/rendering/renderer.h>
30 #include <dali/public-api/size-negotiation/relayout-container.h>
31 #include <dali/devel-api/scripting/scripting.h>
32 #include <dali/integration-api/debug.h>
35 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/public-api/styling/style-manager.h>
38 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
39 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
40 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
41 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
42 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
43 #include <dali-toolkit/internal/styling/style-manager-impl.h>
44 #include <dali-toolkit/internal/visuals/color/color-visual.h>
45 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
46 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
47 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
61 #if defined(DEBUG_ENABLED)
62 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
65 } // unnamed namespace
68 Toolkit::Control Control::New()
70 // Create the implementation, temporarily owned on stack
71 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
73 // Pass ownership to handle
74 Toolkit::Control handle( *controlImpl );
76 // Second-phase init of the implementation
77 // This can only be done after the CustomActor connection has been made...
78 controlImpl->Initialize();
83 void Control::SetStyleName( const std::string& styleName )
85 if( styleName != mImpl->mStyleName )
87 mImpl->mStyleName = styleName;
89 // Apply new style, if stylemanager is available
90 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
93 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
98 const std::string& Control::GetStyleName() const
100 return mImpl->mStyleName;
103 void Control::SetBackgroundColor( const Vector4& color )
105 mImpl->mBackgroundColor = color;
107 map[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::COLOR;
108 map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
110 SetBackground( map );
113 Vector4 Control::GetBackgroundColor() const
115 return mImpl->mBackgroundColor;
118 void Control::SetBackground( const Property::Map& map )
120 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
123 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
124 visual.SetDepthIndex( DepthIndex::BACKGROUND );
126 // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
131 void Control::SetBackgroundImage( Image image )
133 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
136 mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
137 visual.SetDepthIndex( DepthIndex::BACKGROUND );
141 void Control::ClearBackground()
143 mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
144 mImpl->mBackgroundColor = Color::TRANSPARENT;
146 // Trigger a size negotiation request that may be needed when unregistering a visual.
150 void Control::EnableGestureDetection(Gesture::Type type)
152 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
154 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
155 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
156 mImpl->mPinchGestureDetector.Attach(Self());
159 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
161 mImpl->mPanGestureDetector = PanGestureDetector::New();
162 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
163 mImpl->mPanGestureDetector.Attach(Self());
166 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
168 mImpl->mTapGestureDetector = TapGestureDetector::New();
169 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
170 mImpl->mTapGestureDetector.Attach(Self());
173 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
175 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
176 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
177 mImpl->mLongPressGestureDetector.Attach(Self());
181 void Control::DisableGestureDetection(Gesture::Type type)
183 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
185 mImpl->mPinchGestureDetector.Detach(Self());
186 mImpl->mPinchGestureDetector.Reset();
189 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
191 mImpl->mPanGestureDetector.Detach(Self());
192 mImpl->mPanGestureDetector.Reset();
195 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
197 mImpl->mTapGestureDetector.Detach(Self());
198 mImpl->mTapGestureDetector.Reset();
201 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
203 mImpl->mLongPressGestureDetector.Detach(Self());
204 mImpl->mLongPressGestureDetector.Reset();
208 PinchGestureDetector Control::GetPinchGestureDetector() const
210 return mImpl->mPinchGestureDetector;
213 PanGestureDetector Control::GetPanGestureDetector() const
215 return mImpl->mPanGestureDetector;
218 TapGestureDetector Control::GetTapGestureDetector() const
220 return mImpl->mTapGestureDetector;
223 LongPressGestureDetector Control::GetLongPressGestureDetector() const
225 return mImpl->mLongPressGestureDetector;
228 void Control::SetKeyboardNavigationSupport(bool isSupported)
230 mImpl->mIsKeyboardNavigationSupported = isSupported;
233 bool Control::IsKeyboardNavigationSupported()
235 return mImpl->mIsKeyboardNavigationSupported;
238 void Control::SetKeyInputFocus()
240 if( Self().OnStage() )
242 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
246 bool Control::HasKeyInputFocus()
249 if( Self().OnStage() )
251 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
256 void Control::ClearKeyInputFocus()
258 if( Self().OnStage() )
260 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
264 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
266 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
268 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
269 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
272 bool Control::IsKeyboardFocusGroup()
274 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
277 void Control::AccessibilityActivate()
279 // Inform deriving classes
280 OnAccessibilityActivated();
283 void Control::KeyboardEnter()
285 // Inform deriving classes
289 bool Control::OnAccessibilityActivated()
291 return false; // Accessibility activation is not handled by default
294 bool Control::OnKeyboardEnter()
296 return false; // Keyboard enter is not handled by default
299 bool Control::OnAccessibilityPan(PanGesture gesture)
301 return false; // Accessibility pan gesture is not handled by default
304 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
306 return false; // Accessibility touch event is not handled by default
309 bool Control::OnAccessibilityValueChange(bool isIncrease)
311 return false; // Accessibility value change action is not handled by default
314 bool Control::OnAccessibilityZoom()
316 return false; // Accessibility zoom action is not handled by default
319 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
324 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
328 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
330 return mImpl->mKeyEventSignal;
333 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
335 return mImpl->mKeyInputFocusGainedSignal;
338 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
340 return mImpl->mKeyInputFocusLostSignal;
343 bool Control::EmitKeyEventSignal( const KeyEvent& event )
345 // Guard against destruction during signal emission
346 Dali::Toolkit::Control handle( GetOwner() );
348 bool consumed = false;
350 // signals are allocated dynamically when someone connects
351 if ( !mImpl->mKeyEventSignal.Empty() )
353 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
358 // Notification for derived classes
359 consumed = OnKeyEvent(event);
365 Control::Control( ControlBehaviour behaviourFlags )
366 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
367 mImpl(new Impl(*this))
369 mImpl->mFlags = behaviourFlags;
377 void Control::Initialize()
379 // Call deriving classes so initialised before styling is applied to them.
382 if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
383 !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
385 Toolkit::StyleManager styleManager = StyleManager::Get();
387 // if stylemanager is available
390 StyleManager& styleManagerImpl = GetImpl( styleManager );
392 // Register for style changes
393 styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
395 // Apply the current style
396 styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
400 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
402 SetKeyboardNavigationSupport( true );
406 void Control::OnInitialize()
410 void Control::OnControlChildAdd( Actor& child )
414 void Control::OnControlChildRemove( Actor& child )
418 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
420 // By default the control is only interested in theme (not font) changes
421 if( styleManager && change == StyleChange::THEME_CHANGE )
423 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
428 void Control::OnPinch(const PinchGesture& pinch)
430 if( !( mImpl->mStartingPinchScale ) )
433 mImpl->mStartingPinchScale = new Vector3;
436 if( pinch.state == Gesture::Started )
438 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
441 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
444 void Control::OnPan( const PanGesture& pan )
448 void Control::OnTap(const TapGesture& tap)
452 void Control::OnLongPress( const LongPressGesture& longPress )
456 void Control::EmitKeyInputFocusSignal( bool focusGained )
458 Dali::Toolkit::Control handle( GetOwner() );
462 // signals are allocated dynamically when someone connects
463 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
465 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
470 // signals are allocated dynamically when someone connects
471 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
473 mImpl->mKeyInputFocusLostSignal.Emit( handle );
478 void Control::OnStageConnection( int depth )
480 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n", mImpl->mVisuals.Size() );
482 Actor self( Self() );
484 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
486 // Check whether the visual is empty and enabled
487 if( (*iter)->visual && (*iter)->enabled )
489 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
490 Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
494 if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() )
496 Property::Value clippingValue = self.GetProperty( Actor::Property::CLIPPING_MODE );
497 int clippingMode = ClippingMode::DISABLED;
498 if( clippingValue.Get( clippingMode ) )
500 // Add a transparent background if we do not have any renderers or visuals so we clip our children
502 if( clippingMode == ClippingMode::CLIP_CHILDREN )
504 // Create a transparent background visual which will also get staged.
505 SetBackgroundColor( Color::TRANSPARENT );
511 void Control::OnStageDisconnection()
513 for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
515 // Check whether the visual is empty
516 if( (*iter)->visual )
518 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
519 Actor self( Self() );
520 Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
525 void Control::OnKeyInputFocusGained()
527 EmitKeyInputFocusSignal( true );
530 void Control::OnKeyInputFocusLost()
532 EmitKeyInputFocusSignal( false );
535 void Control::OnChildAdd(Actor& child)
537 // Notify derived classes.
538 OnControlChildAdd( child );
541 void Control::OnChildRemove(Actor& child)
543 // Notify derived classes.
544 OnControlChildRemove( child );
547 void Control::OnSizeSet(const Vector3& targetSize)
549 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
552 Vector2 size( targetSize );
553 visual.SetSize( size );
557 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
559 // @todo size negotiate background to new size, animate as well?
562 bool Control::OnTouchEvent(const TouchEvent& event)
564 return false; // Do not consume
567 bool Control::OnHoverEvent(const HoverEvent& event)
569 return false; // Do not consume
572 bool Control::OnKeyEvent(const KeyEvent& event)
574 return false; // Do not consume
577 bool Control::OnWheelEvent(const WheelEvent& event)
579 return false; // Do not consume
582 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
584 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
586 container.Add( Self().GetChildAt( i ), size );
589 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
592 visual.SetSize( size );
596 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
600 Vector3 Control::GetNaturalSize()
602 Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
606 visual.GetNaturalSize( naturalSize );
607 return Vector3( naturalSize );
609 return Vector3::ZERO;
612 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
614 return CalculateChildSizeBase( child, dimension );
617 float Control::GetHeightForWidth( float width )
619 return GetHeightForWidthBase( width );
622 float Control::GetWidthForHeight( float height )
624 return GetWidthForHeightBase( height );
627 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
629 return RelayoutDependentOnChildrenBase( dimension );
632 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
636 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
640 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
642 mImpl->SignalConnected( slotObserver, callback );
645 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
647 mImpl->SignalDisconnected( slotObserver, callback );
650 Control& GetImplementation( Dali::Toolkit::Control& handle )
652 CustomActorImpl& customInterface = handle.GetImplementation();
653 // downcast to control
654 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
658 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
660 const CustomActorImpl& customInterface = handle.GetImplementation();
661 // downcast to control
662 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
666 } // namespace Internal
668 } // namespace Toolkit