2 * Copyright (c) 2015 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/actors/image-actor.h>
26 #include <dali/public-api/animation/constraint.h>
27 #include <dali/public-api/animation/constraints.h>
28 #include <dali/public-api/object/type-registry.h>
29 #include <dali/public-api/size-negotiation/relayout-container.h>
30 #include <dali/devel-api/object/type-registry-helper.h>
31 #include <dali/devel-api/rendering/renderer.h>
32 #include <dali/devel-api/scripting/scripting.h>
33 #include <dali/integration-api/debug.h>
36 #include <dali-toolkit/public-api/controls/control-depth-index-ranges.h>
37 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
38 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
39 #include <dali-toolkit/public-api/controls/control.h>
40 #include <dali-toolkit/devel-api/styling/style-manager.h>
41 #include <dali-toolkit/internal/styling/style-manager-impl.h>
53 * Creates control through type registry
57 return Internal::Control::New();
61 * Performs actions as requested using the action name.
62 * @param[in] object The object on which to perform the action.
63 * @param[in] actionName The action to perform.
64 * @param[in] attributes The attributes with which to perfrom this action.
65 * @return true if action has been accepted by this control
67 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibility-activated";
68 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
72 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
74 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
77 // if cast succeeds there is an implementation so no need to check
78 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
86 * Connects a callback function with the object's signals.
87 * @param[in] object The object providing the signal.
88 * @param[in] tracker Used to disconnect the signal.
89 * @param[in] signalName The signal to connect to.
90 * @param[in] functor A newly allocated FunctorDelegate.
91 * @return True if the signal was connected.
92 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
94 const char* SIGNAL_KEY_EVENT = "key-event";
95 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "key-input-focus-gained";
96 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "key-input-focus-lost";
97 const char* SIGNAL_TAPPED = "tapped";
98 const char* SIGNAL_PANNED = "panned";
99 const char* SIGNAL_PINCHED = "pinched";
100 const char* SIGNAL_LONG_PRESSED = "long-pressed";
101 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
103 Dali::BaseHandle handle( object );
105 bool connected( false );
106 Toolkit::Control control = Toolkit::Control::DownCast( handle );
109 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
112 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
114 controlImpl.KeyEventSignal().Connect( tracker, functor );
116 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
118 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
120 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
122 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
124 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
126 controlImpl.EnableGestureDetection( Gesture::Tap );
127 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
129 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
131 controlImpl.EnableGestureDetection( Gesture::Pan );
132 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
134 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
136 controlImpl.EnableGestureDetection( Gesture::Pinch );
137 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
139 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
141 controlImpl.EnableGestureDetection( Gesture::LongPress );
142 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
148 // Setup signals and actions using the type-registry.
149 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
151 // Note: Properties are registered separately below.
153 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
154 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
155 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
156 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
157 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
158 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
159 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
161 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
163 DALI_TYPE_REGISTRATION_END()
166 * Structure which holds information about the background of a control
170 Actor actor; ///< Background actor
171 Vector4 color; ///< The color of the actor.
178 color( Color::WHITE )
183 unsigned int gQuadIndex[6] = { 0, 2, 1, 1, 2, 3 };
186 Vector2( -0.5f, -0.5f ),
187 Vector2( 0.5f, -0.5f ),
188 Vector2( -0.5f, 0.5f ),
189 Vector2( 0.5f, 0.5f )
193 const char* VERTEX_SHADER_COLOR = DALI_COMPOSE_SHADER(
194 attribute mediump vec2 aPosition;\n
195 uniform mediump mat4 uMvpMatrix;\n
198 gl_Position = uMvpMatrix * vec4( aPosition, 0.0, 1.0 );\n
202 const char* FRAGMENT_SHADER_COLOR = DALI_COMPOSE_SHADER(
203 uniform lowp vec4 uBackgroundColor;\n
204 uniform lowp vec4 uColor;\n
207 gl_FragColor = uBackgroundColor * uColor;\n
213 * @brief Create the background actor for the control.
215 * @param[in] actor The parent actor of the background
216 * @param[in] color The background color
217 * @param[in] image If a valid image is supplied this will be the texture used by the background
219 * @return An actor which will render the background
222 Actor CreateBackground( Actor parent, const Vector4& color, Image image = Image() )
226 PropertyBuffer vertexBuffer;
230 Property::Map vertexFormat;
231 vertexFormat["aPosition"] = Property::VECTOR2;
233 //Create a vertex buffer for vertex positions
234 vertexBuffer = PropertyBuffer::New( vertexFormat, 4u );
235 vertexBuffer.SetData( gQuad );
237 shader = Shader::New( VERTEX_SHADER_COLOR, FRAGMENT_SHADER_COLOR );
238 material = Material::New( shader );
240 //Create the index buffer
241 Property::Map indexFormat;
242 indexFormat["indices"] = Property::INTEGER;
243 PropertyBuffer indexBuffer = PropertyBuffer::New( indexFormat, 6u );
244 indexBuffer.SetData(gQuadIndex);
246 //Create the geometry
247 Geometry mesh = Geometry::New();
248 mesh.AddVertexBuffer( vertexBuffer );
249 mesh.SetIndexBuffer( indexBuffer );
251 //Add uniforms to the shader
252 shader.RegisterProperty( "uBackgroundColor", color );
254 //Create the renderer
255 Renderer renderer = Renderer::New( mesh, material );
256 renderer.SetDepthIndex( parent.GetHierarchyDepth() + BACKGROUND_DEPTH_INDEX );
259 Actor meshActor = Actor::New();
260 meshActor.SetSize( Vector3::ONE );
261 meshActor.AddRenderer( renderer );
262 meshActor.SetPositionInheritanceMode( USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
263 meshActor.SetColorMode( USE_PARENT_COLOR );
265 //Constraint scale of the mesh actor to the size of the control
266 Constraint constraint = Constraint::New<Vector3>( meshActor,
267 Actor::Property::SCALE,
268 EqualToConstraint() );
269 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
276 ImageActor imageActor = ImageActor::New( image );
277 imageActor.SetColor( color );
278 imageActor.SetPositionInheritanceMode( USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
279 imageActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
280 imageActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
281 imageActor.SetSortModifier( parent.GetHierarchyDepth() + BACKGROUND_DEPTH_INDEX );
288 } // unnamed namespace
293 class Control::Impl : public ConnectionTracker
297 // Construction & Destruction
298 Impl(Control& controlImpl)
299 : mControlImpl( controlImpl ),
302 mStartingPinchScale( NULL ),
304 mPinchGestureDetector(),
305 mPanGestureDetector(),
306 mTapGestureDetector(),
307 mLongPressGestureDetector(),
308 mFlags( Control::ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
309 mIsKeyboardNavigationSupported( false ),
310 mIsKeyboardFocusGroup( false ),
311 mAddRemoveBackgroundChild( false )
317 // All gesture detectors will be destroyed so no need to disconnect.
319 delete mStartingPinchScale;
322 // Gesture Detection Methods
324 void PinchDetected(Actor actor, const PinchGesture& pinch)
326 mControlImpl.OnPinch(pinch);
329 void PanDetected(Actor actor, const PanGesture& pan)
331 mControlImpl.OnPan(pan);
334 void TapDetected(Actor actor, const TapGesture& tap)
336 mControlImpl.OnTap(tap);
339 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
341 mControlImpl.OnLongPress(longPress);
344 // Background Methods
347 * Only creates an instance of the background if we actually use it.
348 * @return A reference to the Background structure.
350 Background& GetBackground()
354 mBackground = new Background;
362 * Called when a property of an object of this type is set.
363 * @param[in] object The object whose property is set.
364 * @param[in] index The property index.
365 * @param[in] value The new property value.
367 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
369 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
373 Control& controlImpl( GetImplementation( control ) );
377 case Toolkit::Control::Property::STYLE_NAME:
379 controlImpl.SetStyleName( value.Get< std::string >() );
383 case Toolkit::Control::Property::BACKGROUND_COLOR:
385 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
389 case Toolkit::Control::Property::BACKGROUND_IMAGE:
391 Image image = Scripting::NewImage( value );
394 controlImpl.SetBackgroundImage( image );
398 // An empty map means the background is no longer required
399 controlImpl.ClearBackground();
404 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
406 if ( value.Get< bool >() )
408 controlImpl.SetKeyInputFocus();
412 controlImpl.ClearKeyInputFocus();
421 * Called to retrieve a property of an object of this type.
422 * @param[in] object The object whose property is to be retrieved.
423 * @param[in] index The property index.
424 * @return The current value of the property.
426 static Property::Value GetProperty( BaseObject* object, Property::Index index )
428 Property::Value value;
430 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
434 Control& controlImpl( GetImplementation( control ) );
438 case Toolkit::Control::Property::STYLE_NAME:
440 value = controlImpl.GetStyleName();
444 case Toolkit::Control::Property::BACKGROUND_COLOR:
446 value = controlImpl.GetBackgroundColor();
450 case Toolkit::Control::Property::BACKGROUND_IMAGE:
454 Background* back = controlImpl.mImpl->mBackground;
455 if ( back && back->actor )
457 if( back->actor.GetRendererCount() > 0 && back->actor.GetRendererAt(0).GetMaterial().GetNumberOfSamplers() > 0 )
459 Image image = back->actor.GetRendererAt(0).GetMaterial().GetSamplerAt(0).GetImage();
462 Scripting::CreatePropertyMap( image, map );
467 ImageActor imageActor = ImageActor::DownCast( back->actor );
470 Image image = imageActor.GetImage();
471 Scripting::CreatePropertyMap( image, map );
480 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
482 value = controlImpl.HasKeyInputFocus();
493 Control& mControlImpl;
494 std::string mStyleName;
495 Background* mBackground; ///< Only create the background if we use it
496 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
497 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
498 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
499 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
502 PinchGestureDetector mPinchGestureDetector;
503 PanGestureDetector mPanGestureDetector;
504 TapGestureDetector mTapGestureDetector;
505 LongPressGestureDetector mLongPressGestureDetector;
507 ControlBehaviour mFlags :CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
508 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
509 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
510 bool mAddRemoveBackgroundChild:1; ///< Flag to know when we are adding or removing our own actor to avoid call to OnControlChildAdd
512 // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
513 static PropertyRegistration PROPERTY_1;
514 static PropertyRegistration PROPERTY_2;
515 static PropertyRegistration PROPERTY_3;
516 static PropertyRegistration PROPERTY_4;
519 // Properties registered without macro to use specific member variables.
520 PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "style-name", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
521 PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "background-color", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
522 PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "background-image", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
523 PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "key-input-focus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
525 Toolkit::Control Control::New()
527 // Create the implementation, temporarily owned on stack
528 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) );
530 // Pass ownership to handle
531 Toolkit::Control handle( *controlImpl );
533 // Second-phase init of the implementation
534 // This can only be done after the CustomActor connection has been made...
535 controlImpl->Initialize();
545 void Control::SetStyleName( const std::string& styleName )
547 if( styleName != mImpl->mStyleName )
549 mImpl->mStyleName = styleName;
551 // Apply new style, if stylemanager is available
552 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
555 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
560 const std::string& Control::GetStyleName() const
562 return mImpl->mStyleName;
565 void Control::SetBackgroundColor( const Vector4& color )
567 Background& background( mImpl->GetBackground() );
569 if ( background.actor )
571 if( background.actor.GetRendererCount() > 0 )
573 Shader shader = background.actor.GetRendererAt(0).GetMaterial().GetShader();
574 shader.SetProperty( shader.GetPropertyIndex("uBackgroundColor"), color );
578 background.actor.SetColor( color );
584 Actor actor = CreateBackground(Self(), color );
585 background.actor = actor;
586 mImpl->mAddRemoveBackgroundChild = true;
587 // The actor does not need to be inserted to guarantee order.
589 mImpl->mAddRemoveBackgroundChild = false;
592 background.color = color;
595 Vector4 Control::GetBackgroundColor() const
597 if ( mImpl->mBackground )
599 return mImpl->mBackground->color;
601 return Color::TRANSPARENT;
604 void Control::SetBackgroundImage( Image image )
606 Background& background( mImpl->GetBackground() );
608 if ( background.actor )
610 // Remove Current actor, unset AFTER removal
611 mImpl->mAddRemoveBackgroundChild = true;
612 Self().Remove( background.actor );
613 mImpl->mAddRemoveBackgroundChild = false;
614 background.actor.Reset();
617 Actor actor = CreateBackground(Self(), background.color, image);
619 // Set the background actor before adding so that we do not inform derived classes
620 background.actor = actor;
621 mImpl->mAddRemoveBackgroundChild = true;
622 // The actor does not need to be inserted to guarantee order.
624 mImpl->mAddRemoveBackgroundChild = false;
627 void Control::ClearBackground()
629 if ( mImpl->mBackground )
631 Background& background( mImpl->GetBackground() );
632 mImpl->mAddRemoveBackgroundChild = true;
633 Self().Remove( background.actor );
634 mImpl->mAddRemoveBackgroundChild = false;
636 delete mImpl->mBackground;
637 mImpl->mBackground = NULL;
641 void Control::EnableGestureDetection(Gesture::Type type)
643 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
645 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
646 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
647 mImpl->mPinchGestureDetector.Attach(Self());
650 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
652 mImpl->mPanGestureDetector = PanGestureDetector::New();
653 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
654 mImpl->mPanGestureDetector.Attach(Self());
657 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
659 mImpl->mTapGestureDetector = TapGestureDetector::New();
660 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
661 mImpl->mTapGestureDetector.Attach(Self());
664 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
666 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
667 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
668 mImpl->mLongPressGestureDetector.Attach(Self());
672 void Control::DisableGestureDetection(Gesture::Type type)
674 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
676 mImpl->mPinchGestureDetector.Detach(Self());
677 mImpl->mPinchGestureDetector.Reset();
680 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
682 mImpl->mPanGestureDetector.Detach(Self());
683 mImpl->mPanGestureDetector.Reset();
686 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
688 mImpl->mTapGestureDetector.Detach(Self());
689 mImpl->mTapGestureDetector.Reset();
692 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
694 mImpl->mLongPressGestureDetector.Detach(Self());
695 mImpl->mLongPressGestureDetector.Reset();
699 PinchGestureDetector Control::GetPinchGestureDetector() const
701 return mImpl->mPinchGestureDetector;
704 PanGestureDetector Control::GetPanGestureDetector() const
706 return mImpl->mPanGestureDetector;
709 TapGestureDetector Control::GetTapGestureDetector() const
711 return mImpl->mTapGestureDetector;
714 LongPressGestureDetector Control::GetLongPressGestureDetector() const
716 return mImpl->mLongPressGestureDetector;
719 void Control::SetKeyboardNavigationSupport(bool isSupported)
721 mImpl->mIsKeyboardNavigationSupported = isSupported;
724 bool Control::IsKeyboardNavigationSupported()
726 return mImpl->mIsKeyboardNavigationSupported;
729 void Control::SetKeyInputFocus()
731 if( Self().OnStage() )
733 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
737 bool Control::HasKeyInputFocus()
740 if( Self().OnStage() )
742 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
747 void Control::ClearKeyInputFocus()
749 if( Self().OnStage() )
751 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
755 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
757 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
759 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
760 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
763 bool Control::IsKeyboardFocusGroup()
765 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
768 void Control::AccessibilityActivate()
770 // Inform deriving classes
771 OnAccessibilityActivated();
774 void Control::KeyboardEnter()
776 // Inform deriving classes
780 bool Control::OnAccessibilityActivated()
782 return false; // Accessibility activation is not handled by default
785 bool Control::OnKeyboardEnter()
787 return false; // Keyboard enter is not handled by default
790 bool Control::OnAccessibilityPan(PanGesture gesture)
792 return false; // Accessibility pan gesture is not handled by default
795 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
797 return false; // Accessibility touch event is not handled by default
800 bool Control::OnAccessibilityValueChange(bool isIncrease)
802 return false; // Accessibility value change action is not handled by default
805 bool Control::OnAccessibilityZoom()
807 return false; // Accessibility zoom action is not handled by default
810 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
815 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
819 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
821 return mImpl->mKeyEventSignal;
824 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusGainedSignal()
826 return mImpl->mKeyInputFocusGainedSignal;
829 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusLostSignal()
831 return mImpl->mKeyInputFocusLostSignal;
834 bool Control::EmitKeyEventSignal( const KeyEvent& event )
836 // Guard against destruction during signal emission
837 Dali::Toolkit::Control handle( GetOwner() );
839 bool consumed = false;
841 // signals are allocated dynamically when someone connects
842 if ( !mImpl->mKeyEventSignal.Empty() )
844 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
849 // Notification for derived classes
850 consumed = OnKeyEvent(event);
856 Control::Control( ControlBehaviour behaviourFlags )
857 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
858 mImpl(new Impl(*this))
860 mImpl->mFlags = behaviourFlags;
863 void Control::Initialize()
865 // Call deriving classes so initialised before styling is applied to them.
868 if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
870 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
871 // if stylemanager is available
874 // Register for style changes
875 styleManager.StyleChangeSignal().Connect( this, &Control::OnStyleChange );
877 // Apply the current style
878 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
882 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
884 SetKeyboardNavigationSupport( true );
888 void Control::OnInitialize()
892 void Control::OnControlChildAdd( Actor& child )
896 void Control::OnControlChildRemove( Actor& child )
900 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
902 // By default the control is only interested in theme (not font) changes
903 if( styleManager && change == StyleChange::THEME_CHANGE )
905 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
909 void Control::OnPinch(const PinchGesture& pinch)
911 if( !( mImpl->mStartingPinchScale ) )
914 mImpl->mStartingPinchScale = new Vector3;
917 if( pinch.state == Gesture::Started )
919 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
922 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
925 void Control::OnPan( const PanGesture& pan )
929 void Control::OnTap(const TapGesture& tap)
933 void Control::OnLongPress( const LongPressGesture& longPress )
937 void Control::EmitKeyInputFocusSignal( bool focusGained )
939 Dali::Toolkit::Control handle( GetOwner() );
943 // signals are allocated dynamically when someone connects
944 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
946 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
951 // signals are allocated dynamically when someone connects
952 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
954 mImpl->mKeyInputFocusLostSignal.Emit( handle );
959 void Control::OnStageConnection( int depth )
961 unsigned int controlRendererCount = Self().GetRendererCount();
962 for( unsigned int i(0); i<controlRendererCount; ++i )
964 Renderer controlRenderer = Self().GetRendererAt(i);
965 if( controlRenderer )
967 controlRenderer.SetDepthIndex( CONTENT_DEPTH_INDEX+depth );
971 if( mImpl->mBackground )
973 if(mImpl->mBackground->actor.GetRendererCount() > 0 )
975 Renderer backgroundRenderer = mImpl->mBackground->actor.GetRendererAt( 0 );
976 if(backgroundRenderer)
978 backgroundRenderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX+depth );
984 ImageActor imageActor = ImageActor::DownCast( mImpl->mBackground->actor );
987 imageActor.SetSortModifier( BACKGROUND_DEPTH_INDEX+depth );
993 void Control::OnStageDisconnection()
997 void Control::OnKeyInputFocusGained()
999 EmitKeyInputFocusSignal( true );
1002 void Control::OnKeyInputFocusLost()
1004 EmitKeyInputFocusSignal( false );
1007 void Control::OnChildAdd(Actor& child)
1009 // If this is the background actor, then we do not want to inform deriving classes
1010 if ( mImpl->mAddRemoveBackgroundChild )
1015 // Notify derived classes.
1016 OnControlChildAdd( child );
1019 void Control::OnChildRemove(Actor& child)
1021 // If this is the background actor, then we do not want to inform deriving classes
1022 if ( mImpl->mAddRemoveBackgroundChild )
1027 // Notify derived classes.
1028 OnControlChildRemove( child );
1031 void Control::OnSizeSet(const Vector3& targetSize)
1033 // Background is resized through size negotiation
1036 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1038 // @todo size negotiate background to new size, animate as well?
1041 bool Control::OnTouchEvent(const TouchEvent& event)
1043 return false; // Do not consume
1046 bool Control::OnHoverEvent(const HoverEvent& event)
1048 return false; // Do not consume
1051 bool Control::OnKeyEvent(const KeyEvent& event)
1053 return false; // Do not consume
1056 bool Control::OnWheelEvent(const WheelEvent& event)
1058 return false; // Do not consume
1061 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1063 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1065 container.Add( Self().GetChildAt( i ), size );
1069 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1073 Vector3 Control::GetNaturalSize()
1075 //Control's natural size is the size of its background image if it has been set, or ZERO otherwise
1076 Vector3 naturalSize = Vector3::ZERO;
1077 if( mImpl->mBackground )
1079 if( mImpl->mBackground->actor.GetRendererCount() > 0 )
1081 Material backgroundMaterial = mImpl->mBackground->actor.GetRendererAt(0).GetMaterial();
1082 if( backgroundMaterial.GetNumberOfSamplers() > 0 )
1084 Image backgroundImage = backgroundMaterial.GetSamplerAt(0).GetImage();
1085 if( backgroundImage )
1087 naturalSize.x = backgroundImage.GetWidth();
1088 naturalSize.y = backgroundImage.GetHeight();
1094 return mImpl->mBackground->actor.GetNaturalSize();
1101 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1103 return CalculateChildSizeBase( child, dimension );
1106 float Control::GetHeightForWidth( float width )
1108 if( mImpl->mBackground )
1110 Actor actor = mImpl->mBackground->actor;
1113 return actor.GetHeightForWidth( width );
1116 return GetHeightForWidthBase( width );
1119 float Control::GetWidthForHeight( float height )
1121 if( mImpl->mBackground )
1123 Actor actor = mImpl->mBackground->actor;
1126 return actor.GetWidthForHeight( height );
1129 return GetWidthForHeightBase( height );
1132 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1134 return RelayoutDependentOnChildrenBase( dimension );
1137 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1141 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1145 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1147 mImpl->SignalConnected( slotObserver, callback );
1150 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1152 mImpl->SignalDisconnected( slotObserver, callback );
1155 Control& GetImplementation( Dali::Toolkit::Control& handle )
1157 CustomActorImpl& customInterface = handle.GetImplementation();
1158 // downcast to control
1159 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1163 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1165 const CustomActorImpl& customInterface = handle.GetImplementation();
1166 // downcast to control
1167 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1171 } // namespace Internal
1173 } // namespace Toolkit