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/actors/renderer.h>
27 #include <dali/public-api/animation/constraint.h>
28 #include <dali/public-api/animation/constraints.h>
29 #include <dali/public-api/geometry/geometry.h>
30 #include <dali/public-api/object/type-registry.h>
31 #include <dali/devel-api/object/type-registry-helper.h>
32 #include <dali/devel-api/scripting/scripting.h>
33 #include <dali/public-api/shader-effects/shader.h>
34 #include <dali/public-api/shader-effects/material.h>
35 #include <dali/public-api/size-negotiation/relayout-container.h>
36 #include <dali/integration-api/debug.h>
39 #include <dali-toolkit/public-api/controls/control-depth-index-ranges.h>
40 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
41 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
42 #include <dali-toolkit/public-api/controls/control.h>
43 #include <dali-toolkit/devel-api/styling/style-manager.h>
44 #include <dali-toolkit/internal/styling/style-manager-impl.h>
56 * Creates control through type registry
60 return Internal::Control::New();
64 * Performs actions as requested using the action name.
65 * @param[in] object The object on which to perform the action.
66 * @param[in] actionName The action to perform.
67 * @param[in] attributes The attributes with which to perfrom this action.
68 * @return true if action has been accepted by this control
70 const char* ACTION_CONTROL_ACTIVATED = "control-activated";
71 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
75 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_CONTROL_ACTIVATED ) ) )
77 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
80 // if cast succeeds there is an implementation so no need to check
81 ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
89 * Connects a callback function with the object's signals.
90 * @param[in] object The object providing the signal.
91 * @param[in] tracker Used to disconnect the signal.
92 * @param[in] signalName The signal to connect to.
93 * @param[in] functor A newly allocated FunctorDelegate.
94 * @return True if the signal was connected.
95 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
97 const char* SIGNAL_KEY_EVENT = "key-event";
98 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "key-input-focus-gained";
99 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "key-input-focus-lost";
100 const char* SIGNAL_TAPPED = "tapped";
101 const char* SIGNAL_PANNED = "panned";
102 const char* SIGNAL_PINCHED = "pinched";
103 const char* SIGNAL_LONG_PRESSED = "long-pressed";
104 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
106 Dali::BaseHandle handle( object );
108 bool connected( false );
109 Toolkit::Control control = Toolkit::Control::DownCast( handle );
112 Internal::Control& controlImpl( Internal::GetImplementation( control ) );
115 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
117 controlImpl.KeyEventSignal().Connect( tracker, functor );
119 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
121 controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
123 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
125 controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
127 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
129 controlImpl.EnableGestureDetection( Gesture::Tap );
130 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
132 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
134 controlImpl.EnableGestureDetection( Gesture::Pan );
135 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
137 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
139 controlImpl.EnableGestureDetection( Gesture::Pinch );
140 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
142 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
144 controlImpl.EnableGestureDetection( Gesture::LongPress );
145 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
151 // Setup signals and actions using the type-registry.
152 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
154 // Note: Properties are registered separately below.
156 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
157 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
158 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
159 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
160 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
161 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
162 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
164 TypeAction registerAction( typeRegistration, ACTION_CONTROL_ACTIVATED, &DoAction );
166 DALI_TYPE_REGISTRATION_END()
169 * Structure which holds information about the background of a control
173 Actor actor; ///< Background actor
174 Vector4 color; ///< The color of the actor.
181 color( Color::WHITE )
186 unsigned int gQuadIndex[6] = { 0, 2, 1, 1, 2, 3 };
189 Vector2( -0.5f, -0.5f ),
190 Vector2( 0.5f, -0.5f ),
191 Vector2( -0.5f, 0.5f ),
192 Vector2( 0.5f, 0.5f )
195 struct VertexWithTexture
201 VertexWithTexture gQuadWithTexture[] = {
202 { Vector2( -0.5f, -0.5f ), Vector2( 0.0f, 0.0f ) },
203 { Vector2( 0.5f, -0.5f ), Vector2( 1.0f, 0.0f ) },
204 { Vector2( -0.5f, 0.5f ), Vector2( 0.0f, 1.0f ) },
205 { Vector2( 0.5f, 0.5f ), Vector2( 1.0f, 1.0f ) }
208 const char* VERTEX_SHADER_COLOR = DALI_COMPOSE_SHADER(
209 attribute mediump vec2 aPosition;\n
210 uniform mediump mat4 uMvpMatrix;\n
213 gl_Position = uMvpMatrix * vec4( aPosition, 0.0, 1.0 );\n
217 const char* FRAGMENT_SHADER_COLOR = DALI_COMPOSE_SHADER(
218 uniform lowp vec4 uBackgroundColor;\n
219 uniform lowp vec4 uColor;\n
222 gl_FragColor = uBackgroundColor * uColor;\n
226 const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
227 attribute mediump vec2 aPosition;\n
228 attribute mediump vec2 aTexCoord;\n
229 uniform mediump mat4 uMvpMatrix;\n
230 varying mediump vec2 vTexCoord;\n
233 gl_Position = uMvpMatrix * vec4( aPosition, 0.0, 1.0 );\n
234 vTexCoord = aTexCoord;\n
238 const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
239 uniform lowp vec4 uBackgroundColor;\n
240 uniform lowp vec4 uColor;\n
241 uniform sampler2D sTexture;\n
242 varying mediump vec2 vTexCoord;\n
246 gl_FragColor = texture2D( sTexture, vTexCoord ) * uBackgroundColor * uColor;\n
251 * @brief Create the background actor for the control.
253 * @param[in] actor The parent actor of the background
254 * @param[in] color The background color
255 * @param[in] image If a valid image is supplied this will be the texture used by the background
257 * @return An actor which will render the background
260 Actor CreateBackground( Actor parent, const Vector4& color, Image image = Image() )
262 PropertyBuffer vertexBuffer;
267 Property::Map vertexFormat;
268 vertexFormat["aPosition"] = Property::VECTOR2;
269 vertexFormat["aTexCoord"] = Property::VECTOR2;
271 //Create a vertex buffer for vertex positions and texture coordinates
272 vertexBuffer = PropertyBuffer::New( vertexFormat, 4u );
273 vertexBuffer.SetData( gQuadWithTexture );
275 shader = Shader::New( VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE );
276 Sampler textureSampler = Sampler::New( image, "sTexture" );
277 material = Material::New( shader );
278 material.AddSampler(textureSampler);
282 Property::Map vertexFormat;
283 vertexFormat["aPosition"] = Property::VECTOR2;
285 //Create a vertex buffer for vertex positions
286 vertexBuffer = PropertyBuffer::New( vertexFormat, 4u );
287 vertexBuffer.SetData( gQuad );
289 shader = Shader::New( VERTEX_SHADER_COLOR, FRAGMENT_SHADER_COLOR );
290 material = Material::New( shader );
293 //Create the index buffer
294 Property::Map indexFormat;
295 indexFormat["indices"] = Property::UNSIGNED_INTEGER;
296 PropertyBuffer indexBuffer = PropertyBuffer::New( indexFormat, 6u );
297 indexBuffer.SetData(gQuadIndex);
299 //Create the geometry
300 Geometry mesh = Geometry::New();
301 mesh.AddVertexBuffer( vertexBuffer );
302 mesh.SetIndexBuffer( indexBuffer );
304 //Add uniforms to the shader
305 Property::Index backgroundColorIndex = shader.RegisterProperty( "uBackgroundColor", color );
306 shader.AddUniformMapping( backgroundColorIndex, "uBackgroundColor" );
308 //Create the renderer
309 Renderer renderer = Renderer::New( mesh, material );
310 renderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX );
313 Actor meshActor = Actor::New();
314 meshActor.SetSize( Vector3::ONE );
315 meshActor.AddRenderer( renderer );
316 meshActor.SetPositionInheritanceMode( USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
317 meshActor.SetColorMode( USE_PARENT_COLOR );
319 //Constraint scale of the mesh actor to the size of the control
320 Constraint constraint = Constraint::New<Vector3>( meshActor,
321 Actor::Property::SCALE,
322 EqualToConstraint() );
323 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
329 } // unnamed namespace
334 class Control::Impl : public ConnectionTracker
338 // Construction & Destruction
339 Impl(Control& controlImpl)
340 : mControlImpl( controlImpl ),
343 mStartingPinchScale( NULL ),
345 mPinchGestureDetector(),
346 mPanGestureDetector(),
347 mTapGestureDetector(),
348 mLongPressGestureDetector(),
349 mFlags( Control::ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
350 mIsKeyboardNavigationSupported( false ),
351 mIsKeyboardFocusGroup( false ),
352 mAddRemoveBackgroundChild( false )
358 // All gesture detectors will be destroyed so no need to disconnect.
360 delete mStartingPinchScale;
363 // Gesture Detection Methods
365 void PinchDetected(Actor actor, const PinchGesture& pinch)
367 mControlImpl.OnPinch(pinch);
370 void PanDetected(Actor actor, const PanGesture& pan)
372 mControlImpl.OnPan(pan);
375 void TapDetected(Actor actor, const TapGesture& tap)
377 mControlImpl.OnTap(tap);
380 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
382 mControlImpl.OnLongPress(longPress);
385 // Background Methods
388 * Only creates an instance of the background if we actually use it.
389 * @return A reference to the Background structure.
391 Background& GetBackground()
395 mBackground = new Background;
403 * Called when a property of an object of this type is set.
404 * @param[in] object The object whose property is set.
405 * @param[in] index The property index.
406 * @param[in] value The new property value.
408 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
410 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
414 Control& controlImpl( GetImplementation( control ) );
418 case Toolkit::Control::Property::STYLE_NAME:
420 controlImpl.SetStyleName( value.Get< std::string >() );
424 case Toolkit::Control::Property::BACKGROUND_COLOR:
426 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
430 case Toolkit::Control::Property::BACKGROUND_IMAGE:
432 if ( value.HasKey( "image" ) )
434 Property::Map imageMap = value.GetValue( "image" ).Get< Property::Map >();
435 Image image = Scripting::NewImage( imageMap );
439 controlImpl.SetBackgroundImage( image );
442 else if ( value.Get< Property::Map >().Empty() )
444 // An empty map means the background is no longer required
445 controlImpl.ClearBackground();
450 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
452 if ( value.Get< bool >() )
454 controlImpl.SetKeyInputFocus();
458 controlImpl.ClearKeyInputFocus();
467 * Called to retrieve a property of an object of this type.
468 * @param[in] object The object whose property is to be retrieved.
469 * @param[in] index The property index.
470 * @return The current value of the property.
472 static Property::Value GetProperty( BaseObject* object, Property::Index index )
474 Property::Value value;
476 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
480 Control& controlImpl( GetImplementation( control ) );
484 case Toolkit::Control::Property::STYLE_NAME:
486 value = controlImpl.GetStyleName();
490 case Toolkit::Control::Property::BACKGROUND_COLOR:
492 value = controlImpl.GetBackgroundColor();
496 case Toolkit::Control::Property::BACKGROUND_IMAGE:
500 Background* back = controlImpl.mImpl->mBackground;
501 if ( back && back->actor && back->actor.GetRendererAt(0).GetMaterial().GetNumberOfSamplers() > 0)
503 Image image = back->actor.GetRendererAt(0).GetMaterial().GetSamplerAt(0).GetImage();
506 Property::Map imageMap;
507 Scripting::CreatePropertyMap( image, imageMap );
508 map[ "image" ] = imageMap;
516 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
518 value = controlImpl.HasKeyInputFocus();
529 Control& mControlImpl;
530 std::string mStyleName;
531 Background* mBackground; ///< Only create the background if we use it
532 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
533 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
534 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
535 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
538 PinchGestureDetector mPinchGestureDetector;
539 PanGestureDetector mPanGestureDetector;
540 TapGestureDetector mTapGestureDetector;
541 LongPressGestureDetector mLongPressGestureDetector;
543 ControlBehaviour mFlags :CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
544 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
545 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
546 bool mAddRemoveBackgroundChild:1; ///< Flag to know when we are adding or removing our own actor to avoid call to OnControlChildAdd
548 // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
549 static PropertyRegistration PROPERTY_1;
550 static PropertyRegistration PROPERTY_2;
551 static PropertyRegistration PROPERTY_3;
552 static PropertyRegistration PROPERTY_4;
555 // Properties registered without macro to use specific member variables.
556 PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "style-name", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
557 PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "background-color", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
558 PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "background-image", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
559 PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "key-input-focus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
561 Toolkit::Control Control::New()
563 // Create the implementation, temporarily owned on stack
564 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) );
566 // Pass ownership to handle
567 Toolkit::Control handle( *controlImpl );
569 // Second-phase init of the implementation
570 // This can only be done after the CustomActor connection has been made...
571 controlImpl->Initialize();
581 void Control::SetStyleName( const std::string& styleName )
583 if( styleName != mImpl->mStyleName )
585 mImpl->mStyleName = styleName;
588 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
589 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
593 const std::string& Control::GetStyleName() const
595 return mImpl->mStyleName;
598 void Control::SetBackgroundColor( const Vector4& color )
600 Background& background( mImpl->GetBackground() );
602 if ( background.actor )
604 Shader shader = background.actor.GetRendererAt(0).GetMaterial().GetShader();
605 shader.SetProperty( shader.GetPropertyIndex("uBackgroundColor"), color );
610 Actor actor = CreateBackground(Self(), color );
611 background.actor = actor;
612 mImpl->mAddRemoveBackgroundChild = true;
613 // use insert to guarantee its the first child (so that OVERLAY mode works)
614 Self().Insert( 0, actor );
615 mImpl->mAddRemoveBackgroundChild = false;
618 background.color = color;
621 Vector4 Control::GetBackgroundColor() const
623 if ( mImpl->mBackground )
625 return mImpl->mBackground->color;
627 return Color::TRANSPARENT;
630 void Control::SetBackgroundImage( Image image )
632 Background& background( mImpl->GetBackground() );
633 if( !background.actor || background.actor.GetRendererAt(0).GetMaterial().GetNumberOfSamplers() == 0)
635 // Remove current actor as it has not texture coordinates
636 if( background.actor )
638 mImpl->mAddRemoveBackgroundChild = true;
639 Self().Remove( background.actor );
640 mImpl->mAddRemoveBackgroundChild = false;
641 background.actor.Reset();
644 //Create a new actor with texture coordinates
645 Actor actor = CreateBackground(Self(), background.color, image);
646 mImpl->mAddRemoveBackgroundChild = true;
647 // use insert to guarantee its the first child (so that OVERLAY mode works)
648 Self().Insert( 0, actor );
649 mImpl->mAddRemoveBackgroundChild = false;
650 background.actor = actor;
654 //Change sampler image
655 Sampler sampler = background.actor.GetRendererAt(0).GetMaterial().GetSamplerAt(0);
656 sampler.SetImage(image);
660 void Control::ClearBackground()
662 if ( mImpl->mBackground )
664 Background& background( mImpl->GetBackground() );
665 mImpl->mAddRemoveBackgroundChild = true;
666 Self().Remove( background.actor );
667 mImpl->mAddRemoveBackgroundChild = false;
669 delete mImpl->mBackground;
670 mImpl->mBackground = NULL;
674 void Control::EnableGestureDetection(Gesture::Type type)
676 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
678 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
679 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
680 mImpl->mPinchGestureDetector.Attach(Self());
683 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
685 mImpl->mPanGestureDetector = PanGestureDetector::New();
686 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
687 mImpl->mPanGestureDetector.Attach(Self());
690 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
692 mImpl->mTapGestureDetector = TapGestureDetector::New();
693 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
694 mImpl->mTapGestureDetector.Attach(Self());
697 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
699 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
700 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
701 mImpl->mLongPressGestureDetector.Attach(Self());
705 void Control::DisableGestureDetection(Gesture::Type type)
707 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
709 mImpl->mPinchGestureDetector.Detach(Self());
710 mImpl->mPinchGestureDetector.Reset();
713 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
715 mImpl->mPanGestureDetector.Detach(Self());
716 mImpl->mPanGestureDetector.Reset();
719 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
721 mImpl->mTapGestureDetector.Detach(Self());
722 mImpl->mTapGestureDetector.Reset();
725 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
727 mImpl->mLongPressGestureDetector.Detach(Self());
728 mImpl->mLongPressGestureDetector.Reset();
732 PinchGestureDetector Control::GetPinchGestureDetector() const
734 return mImpl->mPinchGestureDetector;
737 PanGestureDetector Control::GetPanGestureDetector() const
739 return mImpl->mPanGestureDetector;
742 TapGestureDetector Control::GetTapGestureDetector() const
744 return mImpl->mTapGestureDetector;
747 LongPressGestureDetector Control::GetLongPressGestureDetector() const
749 return mImpl->mLongPressGestureDetector;
752 void Control::SetKeyboardNavigationSupport(bool isSupported)
754 mImpl->mIsKeyboardNavigationSupported = isSupported;
757 bool Control::IsKeyboardNavigationSupported()
759 return mImpl->mIsKeyboardNavigationSupported;
762 void Control::SetKeyInputFocus()
764 if( Self().OnStage() )
766 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
770 bool Control::HasKeyInputFocus()
773 if( Self().OnStage() )
775 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
780 void Control::ClearKeyInputFocus()
782 if( Self().OnStage() )
784 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
788 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
790 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
792 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
793 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
796 bool Control::IsKeyboardFocusGroup()
798 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
801 void Control::AccessibilityActivate()
803 // Inform deriving classes
804 OnAccessibilityActivated();
807 bool Control::OnAccessibilityActivated()
809 return false; // Accessibility activation is not handled by default
812 bool Control::OnAccessibilityPan(PanGesture gesture)
814 return false; // Accessibility pan gesture is not handled by default
817 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
819 return false; // Accessibility touch event is not handled by default
822 bool Control::OnAccessibilityValueChange(bool isIncrease)
824 return false; // Accessibility value change action is not handled by default
827 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
832 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
836 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
838 return mImpl->mKeyEventSignal;
841 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusGainedSignal()
843 return mImpl->mKeyInputFocusGainedSignal;
846 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusLostSignal()
848 return mImpl->mKeyInputFocusLostSignal;
851 bool Control::EmitKeyEventSignal( const KeyEvent& event )
853 // Guard against destruction during signal emission
854 Dali::Toolkit::Control handle( GetOwner() );
856 bool consumed = false;
858 // signals are allocated dynamically when someone connects
859 if ( !mImpl->mKeyEventSignal.Empty() )
861 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
866 // Notification for derived classes
867 consumed = OnKeyEvent(event);
873 Control::Control( ControlBehaviour behaviourFlags )
874 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
875 mImpl(new Impl(*this))
877 mImpl->mFlags = behaviourFlags;
880 void Control::Initialize()
882 if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
884 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
886 // Register for style changes
887 styleManager.StyleChangeSignal().Connect( this, &Control::OnStyleChange );
890 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
893 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
895 SetKeyboardNavigationSupport( true );
898 // Calling deriving classes
902 void Control::OnInitialize()
906 void Control::OnControlChildAdd( Actor& child )
910 void Control::OnControlChildRemove( Actor& child )
914 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
916 // By default the control is only interested in theme (not font) changes
917 if( change == StyleChange::THEME_CHANGE )
919 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
923 void Control::OnPinch(const PinchGesture& pinch)
925 if( !( mImpl->mStartingPinchScale ) )
928 mImpl->mStartingPinchScale = new Vector3;
931 if( pinch.state == Gesture::Started )
933 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
936 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
939 void Control::OnPan( const PanGesture& pan )
943 void Control::OnTap(const TapGesture& tap)
947 void Control::OnLongPress( const LongPressGesture& longPress )
951 void Control::EmitKeyInputFocusSignal( bool focusGained )
953 Dali::Toolkit::Control handle( GetOwner() );
957 // signals are allocated dynamically when someone connects
958 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
960 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
965 // signals are allocated dynamically when someone connects
966 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
968 mImpl->mKeyInputFocusLostSignal.Emit( handle );
973 void Control::OnStageConnection( unsigned int depth )
975 unsigned int controlRendererCount = Self().GetRendererCount();
976 for( unsigned int i(0); i<controlRendererCount; ++i )
978 Renderer controlRenderer = Self().GetRendererAt(i);
979 if( controlRenderer )
981 controlRenderer.SetDepthIndex( CONTENT_DEPTH_INDEX+depth );
985 if( mImpl->mBackground )
987 Renderer backgroundRenderer = mImpl->mBackground->actor.GetRendererAt( 0 );
988 if(backgroundRenderer)
990 backgroundRenderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX+depth );
995 void Control::OnStageDisconnection()
999 void Control::OnKeyInputFocusGained()
1001 EmitKeyInputFocusSignal( true );
1004 void Control::OnKeyInputFocusLost()
1006 EmitKeyInputFocusSignal( false );
1009 void Control::OnChildAdd(Actor& child)
1011 // If this is the background actor, then we do not want to inform deriving classes
1012 if ( mImpl->mAddRemoveBackgroundChild )
1017 // Notify derived classes.
1018 OnControlChildAdd( child );
1021 void Control::OnChildRemove(Actor& child)
1023 // If this is the background actor, then we do not want to inform deriving classes
1024 if ( mImpl->mAddRemoveBackgroundChild )
1029 // Notify derived classes.
1030 OnControlChildRemove( child );
1033 void Control::OnSizeSet(const Vector3& targetSize)
1035 // Background is resized through size negotiation
1038 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1040 // @todo size negotiate background to new size, animate as well?
1043 bool Control::OnTouchEvent(const TouchEvent& event)
1045 return false; // Do not consume
1048 bool Control::OnHoverEvent(const HoverEvent& event)
1050 return false; // Do not consume
1053 bool Control::OnKeyEvent(const KeyEvent& event)
1055 return false; // Do not consume
1058 bool Control::OnWheelEvent(const WheelEvent& event)
1060 return false; // Do not consume
1063 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1065 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1067 container.Add( Self().GetChildAt( i ), size );
1071 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1075 Vector3 Control::GetNaturalSize()
1077 if( mImpl->mBackground )
1079 Actor actor = mImpl->mBackground->actor;
1082 return actor.GetNaturalSize();
1088 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1090 return CalculateChildSizeBase( child, dimension );
1093 float Control::GetHeightForWidth( float width )
1095 if( mImpl->mBackground )
1097 Actor actor = mImpl->mBackground->actor;
1100 return actor.GetHeightForWidth( width );
1103 return GetHeightForWidthBase( width );
1106 float Control::GetWidthForHeight( float height )
1108 if( mImpl->mBackground )
1110 Actor actor = mImpl->mBackground->actor;
1113 return actor.GetWidthForHeight( height );
1116 return GetWidthForHeightBase( height );
1119 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1121 return RelayoutDependentOnChildrenBase( dimension );
1124 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1128 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1132 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1134 mImpl->SignalConnected( slotObserver, callback );
1137 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1139 mImpl->SignalDisconnected( slotObserver, callback );
1142 Control& GetImplementation( Dali::Toolkit::Control& handle )
1144 CustomActorImpl& customInterface = handle.GetImplementation();
1145 // downcast to control
1146 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1150 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1152 const CustomActorImpl& customInterface = handle.GetImplementation();
1153 // downcast to control
1154 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1158 } // namespace Internal
1160 } // namespace Toolkit