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 shader.RegisterProperty( "uBackgroundColor", color );
307 //Create the renderer
308 Renderer renderer = Renderer::New( mesh, material );
309 renderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX );
312 Actor meshActor = Actor::New();
313 meshActor.SetSize( Vector3::ONE );
314 meshActor.AddRenderer( renderer );
315 meshActor.SetPositionInheritanceMode( USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
316 meshActor.SetColorMode( USE_PARENT_COLOR );
318 //Constraint scale of the mesh actor to the size of the control
319 Constraint constraint = Constraint::New<Vector3>( meshActor,
320 Actor::Property::SCALE,
321 EqualToConstraint() );
322 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
328 } // unnamed namespace
333 class Control::Impl : public ConnectionTracker
337 // Construction & Destruction
338 Impl(Control& controlImpl)
339 : mControlImpl( controlImpl ),
342 mStartingPinchScale( NULL ),
344 mPinchGestureDetector(),
345 mPanGestureDetector(),
346 mTapGestureDetector(),
347 mLongPressGestureDetector(),
348 mFlags( Control::ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
349 mIsKeyboardNavigationSupported( false ),
350 mIsKeyboardFocusGroup( false ),
351 mAddRemoveBackgroundChild( false )
357 // All gesture detectors will be destroyed so no need to disconnect.
359 delete mStartingPinchScale;
362 // Gesture Detection Methods
364 void PinchDetected(Actor actor, const PinchGesture& pinch)
366 mControlImpl.OnPinch(pinch);
369 void PanDetected(Actor actor, const PanGesture& pan)
371 mControlImpl.OnPan(pan);
374 void TapDetected(Actor actor, const TapGesture& tap)
376 mControlImpl.OnTap(tap);
379 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
381 mControlImpl.OnLongPress(longPress);
384 // Background Methods
387 * Only creates an instance of the background if we actually use it.
388 * @return A reference to the Background structure.
390 Background& GetBackground()
394 mBackground = new Background;
402 * Called when a property of an object of this type is set.
403 * @param[in] object The object whose property is set.
404 * @param[in] index The property index.
405 * @param[in] value The new property value.
407 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
409 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
413 Control& controlImpl( GetImplementation( control ) );
417 case Toolkit::Control::Property::STYLE_NAME:
419 controlImpl.SetStyleName( value.Get< std::string >() );
423 case Toolkit::Control::Property::BACKGROUND_COLOR:
425 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
429 case Toolkit::Control::Property::BACKGROUND_IMAGE:
431 if ( value.HasKey( "image" ) )
433 Property::Map imageMap = value.GetValue( "image" ).Get< Property::Map >();
434 Image image = Scripting::NewImage( imageMap );
438 controlImpl.SetBackgroundImage( image );
441 else if ( value.Get< Property::Map >().Empty() )
443 // An empty map means the background is no longer required
444 controlImpl.ClearBackground();
449 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
451 if ( value.Get< bool >() )
453 controlImpl.SetKeyInputFocus();
457 controlImpl.ClearKeyInputFocus();
466 * Called to retrieve a property of an object of this type.
467 * @param[in] object The object whose property is to be retrieved.
468 * @param[in] index The property index.
469 * @return The current value of the property.
471 static Property::Value GetProperty( BaseObject* object, Property::Index index )
473 Property::Value value;
475 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
479 Control& controlImpl( GetImplementation( control ) );
483 case Toolkit::Control::Property::STYLE_NAME:
485 value = controlImpl.GetStyleName();
489 case Toolkit::Control::Property::BACKGROUND_COLOR:
491 value = controlImpl.GetBackgroundColor();
495 case Toolkit::Control::Property::BACKGROUND_IMAGE:
499 Background* back = controlImpl.mImpl->mBackground;
500 if ( back && back->actor && back->actor.GetRendererAt(0).GetMaterial().GetNumberOfSamplers() > 0)
502 Image image = back->actor.GetRendererAt(0).GetMaterial().GetSamplerAt(0).GetImage();
505 Property::Map imageMap;
506 Scripting::CreatePropertyMap( image, imageMap );
507 map[ "image" ] = imageMap;
515 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
517 value = controlImpl.HasKeyInputFocus();
528 Control& mControlImpl;
529 std::string mStyleName;
530 Background* mBackground; ///< Only create the background if we use it
531 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
532 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
533 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
534 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
537 PinchGestureDetector mPinchGestureDetector;
538 PanGestureDetector mPanGestureDetector;
539 TapGestureDetector mTapGestureDetector;
540 LongPressGestureDetector mLongPressGestureDetector;
542 ControlBehaviour mFlags :CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
543 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
544 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
545 bool mAddRemoveBackgroundChild:1; ///< Flag to know when we are adding or removing our own actor to avoid call to OnControlChildAdd
547 // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
548 static PropertyRegistration PROPERTY_1;
549 static PropertyRegistration PROPERTY_2;
550 static PropertyRegistration PROPERTY_3;
551 static PropertyRegistration PROPERTY_4;
554 // Properties registered without macro to use specific member variables.
555 PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "style-name", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
556 PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "background-color", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
557 PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "background-image", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
558 PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "key-input-focus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
560 Toolkit::Control Control::New()
562 // Create the implementation, temporarily owned on stack
563 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) );
565 // Pass ownership to handle
566 Toolkit::Control handle( *controlImpl );
568 // Second-phase init of the implementation
569 // This can only be done after the CustomActor connection has been made...
570 controlImpl->Initialize();
580 void Control::SetStyleName( const std::string& styleName )
582 if( styleName != mImpl->mStyleName )
584 mImpl->mStyleName = styleName;
587 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
588 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
592 const std::string& Control::GetStyleName() const
594 return mImpl->mStyleName;
597 void Control::SetBackgroundColor( const Vector4& color )
599 Background& background( mImpl->GetBackground() );
601 if ( background.actor )
603 Shader shader = background.actor.GetRendererAt(0).GetMaterial().GetShader();
604 shader.SetProperty( shader.GetPropertyIndex("uBackgroundColor"), color );
609 Actor actor = CreateBackground(Self(), color );
610 background.actor = actor;
611 mImpl->mAddRemoveBackgroundChild = true;
612 // use insert to guarantee its the first child (so that OVERLAY mode works)
613 Self().Insert( 0, actor );
614 mImpl->mAddRemoveBackgroundChild = false;
617 background.color = color;
620 Vector4 Control::GetBackgroundColor() const
622 if ( mImpl->mBackground )
624 return mImpl->mBackground->color;
626 return Color::TRANSPARENT;
629 void Control::SetBackgroundImage( Image image )
631 Background& background( mImpl->GetBackground() );
632 if( !background.actor || background.actor.GetRendererAt(0).GetMaterial().GetNumberOfSamplers() == 0)
634 // Remove current actor as it has not texture coordinates
635 if( background.actor )
637 mImpl->mAddRemoveBackgroundChild = true;
638 Self().Remove( background.actor );
639 mImpl->mAddRemoveBackgroundChild = false;
640 background.actor.Reset();
643 //Create a new actor with texture coordinates
644 Actor actor = CreateBackground(Self(), background.color, image);
645 mImpl->mAddRemoveBackgroundChild = true;
646 // use insert to guarantee its the first child (so that OVERLAY mode works)
647 Self().Insert( 0, actor );
648 mImpl->mAddRemoveBackgroundChild = false;
649 background.actor = actor;
653 //Change sampler image
654 Sampler sampler = background.actor.GetRendererAt(0).GetMaterial().GetSamplerAt(0);
655 sampler.SetImage(image);
659 void Control::ClearBackground()
661 if ( mImpl->mBackground )
663 Background& background( mImpl->GetBackground() );
664 mImpl->mAddRemoveBackgroundChild = true;
665 Self().Remove( background.actor );
666 mImpl->mAddRemoveBackgroundChild = false;
668 delete mImpl->mBackground;
669 mImpl->mBackground = NULL;
673 void Control::EnableGestureDetection(Gesture::Type type)
675 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
677 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
678 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
679 mImpl->mPinchGestureDetector.Attach(Self());
682 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
684 mImpl->mPanGestureDetector = PanGestureDetector::New();
685 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
686 mImpl->mPanGestureDetector.Attach(Self());
689 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
691 mImpl->mTapGestureDetector = TapGestureDetector::New();
692 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
693 mImpl->mTapGestureDetector.Attach(Self());
696 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
698 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
699 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
700 mImpl->mLongPressGestureDetector.Attach(Self());
704 void Control::DisableGestureDetection(Gesture::Type type)
706 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
708 mImpl->mPinchGestureDetector.Detach(Self());
709 mImpl->mPinchGestureDetector.Reset();
712 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
714 mImpl->mPanGestureDetector.Detach(Self());
715 mImpl->mPanGestureDetector.Reset();
718 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
720 mImpl->mTapGestureDetector.Detach(Self());
721 mImpl->mTapGestureDetector.Reset();
724 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
726 mImpl->mLongPressGestureDetector.Detach(Self());
727 mImpl->mLongPressGestureDetector.Reset();
731 PinchGestureDetector Control::GetPinchGestureDetector() const
733 return mImpl->mPinchGestureDetector;
736 PanGestureDetector Control::GetPanGestureDetector() const
738 return mImpl->mPanGestureDetector;
741 TapGestureDetector Control::GetTapGestureDetector() const
743 return mImpl->mTapGestureDetector;
746 LongPressGestureDetector Control::GetLongPressGestureDetector() const
748 return mImpl->mLongPressGestureDetector;
751 void Control::SetKeyboardNavigationSupport(bool isSupported)
753 mImpl->mIsKeyboardNavigationSupported = isSupported;
756 bool Control::IsKeyboardNavigationSupported()
758 return mImpl->mIsKeyboardNavigationSupported;
761 void Control::SetKeyInputFocus()
763 if( Self().OnStage() )
765 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
769 bool Control::HasKeyInputFocus()
772 if( Self().OnStage() )
774 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
779 void Control::ClearKeyInputFocus()
781 if( Self().OnStage() )
783 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
787 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
789 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
791 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
792 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
795 bool Control::IsKeyboardFocusGroup()
797 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
800 void Control::AccessibilityActivate()
802 // Inform deriving classes
803 OnAccessibilityActivated();
806 bool Control::OnAccessibilityActivated()
808 return false; // Accessibility activation is not handled by default
811 bool Control::OnAccessibilityPan(PanGesture gesture)
813 return false; // Accessibility pan gesture is not handled by default
816 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
818 return false; // Accessibility touch event is not handled by default
821 bool Control::OnAccessibilityValueChange(bool isIncrease)
823 return false; // Accessibility value change action is not handled by default
826 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
831 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
835 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
837 return mImpl->mKeyEventSignal;
840 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusGainedSignal()
842 return mImpl->mKeyInputFocusGainedSignal;
845 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusLostSignal()
847 return mImpl->mKeyInputFocusLostSignal;
850 bool Control::EmitKeyEventSignal( const KeyEvent& event )
852 // Guard against destruction during signal emission
853 Dali::Toolkit::Control handle( GetOwner() );
855 bool consumed = false;
857 // signals are allocated dynamically when someone connects
858 if ( !mImpl->mKeyEventSignal.Empty() )
860 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
865 // Notification for derived classes
866 consumed = OnKeyEvent(event);
872 Control::Control( ControlBehaviour behaviourFlags )
873 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
874 mImpl(new Impl(*this))
876 mImpl->mFlags = behaviourFlags;
879 void Control::Initialize()
881 if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
883 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
885 // Register for style changes
886 styleManager.StyleChangeSignal().Connect( this, &Control::OnStyleChange );
889 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
892 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
894 SetKeyboardNavigationSupport( true );
897 // Calling deriving classes
901 void Control::OnInitialize()
905 void Control::OnControlChildAdd( Actor& child )
909 void Control::OnControlChildRemove( Actor& child )
913 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
915 // By default the control is only interested in theme (not font) changes
916 if( change == StyleChange::THEME_CHANGE )
918 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
922 void Control::OnPinch(const PinchGesture& pinch)
924 if( !( mImpl->mStartingPinchScale ) )
927 mImpl->mStartingPinchScale = new Vector3;
930 if( pinch.state == Gesture::Started )
932 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
935 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
938 void Control::OnPan( const PanGesture& pan )
942 void Control::OnTap(const TapGesture& tap)
946 void Control::OnLongPress( const LongPressGesture& longPress )
950 void Control::EmitKeyInputFocusSignal( bool focusGained )
952 Dali::Toolkit::Control handle( GetOwner() );
956 // signals are allocated dynamically when someone connects
957 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
959 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
964 // signals are allocated dynamically when someone connects
965 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
967 mImpl->mKeyInputFocusLostSignal.Emit( handle );
972 void Control::OnStageConnection( unsigned int depth )
974 unsigned int controlRendererCount = Self().GetRendererCount();
975 for( unsigned int i(0); i<controlRendererCount; ++i )
977 Renderer controlRenderer = Self().GetRendererAt(i);
978 if( controlRenderer )
980 controlRenderer.SetDepthIndex( CONTENT_DEPTH_INDEX+depth );
984 if( mImpl->mBackground )
986 Renderer backgroundRenderer = mImpl->mBackground->actor.GetRendererAt( 0 );
987 if(backgroundRenderer)
989 backgroundRenderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX+depth );
994 void Control::OnStageDisconnection()
998 void Control::OnKeyInputFocusGained()
1000 EmitKeyInputFocusSignal( true );
1003 void Control::OnKeyInputFocusLost()
1005 EmitKeyInputFocusSignal( false );
1008 void Control::OnChildAdd(Actor& child)
1010 // If this is the background actor, then we do not want to inform deriving classes
1011 if ( mImpl->mAddRemoveBackgroundChild )
1016 // Notify derived classes.
1017 OnControlChildAdd( child );
1020 void Control::OnChildRemove(Actor& child)
1022 // If this is the background actor, then we do not want to inform deriving classes
1023 if ( mImpl->mAddRemoveBackgroundChild )
1028 // Notify derived classes.
1029 OnControlChildRemove( child );
1032 void Control::OnSizeSet(const Vector3& targetSize)
1034 // Background is resized through size negotiation
1037 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1039 // @todo size negotiate background to new size, animate as well?
1042 bool Control::OnTouchEvent(const TouchEvent& event)
1044 return false; // Do not consume
1047 bool Control::OnHoverEvent(const HoverEvent& event)
1049 return false; // Do not consume
1052 bool Control::OnKeyEvent(const KeyEvent& event)
1054 return false; // Do not consume
1057 bool Control::OnWheelEvent(const WheelEvent& event)
1059 return false; // Do not consume
1062 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1064 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1066 container.Add( Self().GetChildAt( i ), size );
1070 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1074 Vector3 Control::GetNaturalSize()
1076 if( mImpl->mBackground )
1078 Actor actor = mImpl->mBackground->actor;
1081 return actor.GetNaturalSize();
1087 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1089 return CalculateChildSizeBase( child, dimension );
1092 float Control::GetHeightForWidth( float width )
1094 if( mImpl->mBackground )
1096 Actor actor = mImpl->mBackground->actor;
1099 return actor.GetHeightForWidth( width );
1102 return GetHeightForWidthBase( width );
1105 float Control::GetWidthForHeight( float height )
1107 if( mImpl->mBackground )
1109 Actor actor = mImpl->mBackground->actor;
1112 return actor.GetWidthForHeight( height );
1115 return GetWidthForHeightBase( height );
1118 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1120 return RelayoutDependentOnChildrenBase( dimension );
1123 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1127 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1131 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1133 mImpl->SignalConnected( slotObserver, callback );
1136 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1138 mImpl->SignalDisconnected( slotObserver, callback );
1141 Control& GetImplementation( Dali::Toolkit::Control& handle )
1143 CustomActorImpl& customInterface = handle.GetImplementation();
1144 // downcast to control
1145 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1149 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1151 const CustomActorImpl& customInterface = handle.GetImplementation();
1152 // downcast to control
1153 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1157 } // namespace Internal
1159 } // namespace Toolkit