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_CONTROL_ACTIVATED = "control-activated";
68 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
72 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_CONTROL_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_CONTROL_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::UNSIGNED_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( 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 );
287 } // unnamed namespace
292 class Control::Impl : public ConnectionTracker
296 // Construction & Destruction
297 Impl(Control& controlImpl)
298 : mControlImpl( controlImpl ),
301 mStartingPinchScale( NULL ),
303 mPinchGestureDetector(),
304 mPanGestureDetector(),
305 mTapGestureDetector(),
306 mLongPressGestureDetector(),
307 mFlags( Control::ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
308 mIsKeyboardNavigationSupported( false ),
309 mIsKeyboardFocusGroup( false ),
310 mAddRemoveBackgroundChild( false )
316 // All gesture detectors will be destroyed so no need to disconnect.
318 delete mStartingPinchScale;
321 // Gesture Detection Methods
323 void PinchDetected(Actor actor, const PinchGesture& pinch)
325 mControlImpl.OnPinch(pinch);
328 void PanDetected(Actor actor, const PanGesture& pan)
330 mControlImpl.OnPan(pan);
333 void TapDetected(Actor actor, const TapGesture& tap)
335 mControlImpl.OnTap(tap);
338 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
340 mControlImpl.OnLongPress(longPress);
343 // Background Methods
346 * Only creates an instance of the background if we actually use it.
347 * @return A reference to the Background structure.
349 Background& GetBackground()
353 mBackground = new Background;
361 * Called when a property of an object of this type is set.
362 * @param[in] object The object whose property is set.
363 * @param[in] index The property index.
364 * @param[in] value The new property value.
366 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
368 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
372 Control& controlImpl( GetImplementation( control ) );
376 case Toolkit::Control::Property::STYLE_NAME:
378 controlImpl.SetStyleName( value.Get< std::string >() );
382 case Toolkit::Control::Property::BACKGROUND_COLOR:
384 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
388 case Toolkit::Control::Property::BACKGROUND_IMAGE:
390 Image image = Scripting::NewImage( value );
393 controlImpl.SetBackgroundImage( image );
397 // An empty map means the background is no longer required
398 controlImpl.ClearBackground();
403 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
405 if ( value.Get< bool >() )
407 controlImpl.SetKeyInputFocus();
411 controlImpl.ClearKeyInputFocus();
420 * Called to retrieve a property of an object of this type.
421 * @param[in] object The object whose property is to be retrieved.
422 * @param[in] index The property index.
423 * @return The current value of the property.
425 static Property::Value GetProperty( BaseObject* object, Property::Index index )
427 Property::Value value;
429 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
433 Control& controlImpl( GetImplementation( control ) );
437 case Toolkit::Control::Property::STYLE_NAME:
439 value = controlImpl.GetStyleName();
443 case Toolkit::Control::Property::BACKGROUND_COLOR:
445 value = controlImpl.GetBackgroundColor();
449 case Toolkit::Control::Property::BACKGROUND_IMAGE:
453 Background* back = controlImpl.mImpl->mBackground;
454 if ( back && back->actor )
456 if( back->actor.GetRendererCount() > 0 && back->actor.GetRendererAt(0).GetMaterial().GetNumberOfSamplers() > 0 )
458 Image image = back->actor.GetRendererAt(0).GetMaterial().GetSamplerAt(0).GetImage();
461 Scripting::CreatePropertyMap( image, map );
466 ImageActor imageActor = ImageActor::DownCast( back->actor );
469 Image image = imageActor.GetImage();
470 Scripting::CreatePropertyMap( image, map );
479 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
481 value = controlImpl.HasKeyInputFocus();
492 Control& mControlImpl;
493 std::string mStyleName;
494 Background* mBackground; ///< Only create the background if we use it
495 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
496 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
497 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
498 Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
501 PinchGestureDetector mPinchGestureDetector;
502 PanGestureDetector mPanGestureDetector;
503 TapGestureDetector mTapGestureDetector;
504 LongPressGestureDetector mLongPressGestureDetector;
506 ControlBehaviour mFlags :CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
507 bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control.
508 bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group.
509 bool mAddRemoveBackgroundChild:1; ///< Flag to know when we are adding or removing our own actor to avoid call to OnControlChildAdd
511 // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
512 static PropertyRegistration PROPERTY_1;
513 static PropertyRegistration PROPERTY_2;
514 static PropertyRegistration PROPERTY_3;
515 static PropertyRegistration PROPERTY_4;
518 // Properties registered without macro to use specific member variables.
519 PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "style-name", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
520 PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "background-color", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
521 PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "background-image", Toolkit::Control::Property::BACKGROUND_IMAGE, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
522 PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "key-input-focus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
524 Toolkit::Control Control::New()
526 // Create the implementation, temporarily owned on stack
527 IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) );
529 // Pass ownership to handle
530 Toolkit::Control handle( *controlImpl );
532 // Second-phase init of the implementation
533 // This can only be done after the CustomActor connection has been made...
534 controlImpl->Initialize();
544 void Control::SetStyleName( const std::string& styleName )
546 if( styleName != mImpl->mStyleName )
548 mImpl->mStyleName = styleName;
551 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
552 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
556 const std::string& Control::GetStyleName() const
558 return mImpl->mStyleName;
561 void Control::SetBackgroundColor( const Vector4& color )
563 Background& background( mImpl->GetBackground() );
565 if ( background.actor )
567 if( background.actor.GetRendererCount() > 0 )
569 Shader shader = background.actor.GetRendererAt(0).GetMaterial().GetShader();
570 shader.SetProperty( shader.GetPropertyIndex("uBackgroundColor"), color );
574 background.actor.SetColor( color );
580 Actor actor = CreateBackground(Self(), color );
581 background.actor = actor;
582 mImpl->mAddRemoveBackgroundChild = true;
583 // use insert to guarantee its the first child (so that OVERLAY mode works)
584 Self().Insert( 0, actor );
585 mImpl->mAddRemoveBackgroundChild = false;
588 background.color = color;
591 Vector4 Control::GetBackgroundColor() const
593 if ( mImpl->mBackground )
595 return mImpl->mBackground->color;
597 return Color::TRANSPARENT;
600 void Control::SetBackgroundImage( Image image )
602 Background& background( mImpl->GetBackground() );
604 if ( background.actor )
606 // Remove Current actor, unset AFTER removal
607 mImpl->mAddRemoveBackgroundChild = true;
608 Self().Remove( background.actor );
609 mImpl->mAddRemoveBackgroundChild = false;
610 background.actor.Reset();
613 Actor actor = CreateBackground(Self(), background.color, image);
615 // Set the background actor before adding so that we do not inform derived classes
616 background.actor = actor;
617 mImpl->mAddRemoveBackgroundChild = true;
618 // use insert to guarantee its the first child (so that OVERLAY mode works)
619 Self().Insert( 0, actor );
620 mImpl->mAddRemoveBackgroundChild = false;
623 void Control::ClearBackground()
625 if ( mImpl->mBackground )
627 Background& background( mImpl->GetBackground() );
628 mImpl->mAddRemoveBackgroundChild = true;
629 Self().Remove( background.actor );
630 mImpl->mAddRemoveBackgroundChild = false;
632 delete mImpl->mBackground;
633 mImpl->mBackground = NULL;
637 void Control::EnableGestureDetection(Gesture::Type type)
639 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
641 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
642 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
643 mImpl->mPinchGestureDetector.Attach(Self());
646 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
648 mImpl->mPanGestureDetector = PanGestureDetector::New();
649 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
650 mImpl->mPanGestureDetector.Attach(Self());
653 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
655 mImpl->mTapGestureDetector = TapGestureDetector::New();
656 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
657 mImpl->mTapGestureDetector.Attach(Self());
660 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
662 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
663 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
664 mImpl->mLongPressGestureDetector.Attach(Self());
668 void Control::DisableGestureDetection(Gesture::Type type)
670 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
672 mImpl->mPinchGestureDetector.Detach(Self());
673 mImpl->mPinchGestureDetector.Reset();
676 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
678 mImpl->mPanGestureDetector.Detach(Self());
679 mImpl->mPanGestureDetector.Reset();
682 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
684 mImpl->mTapGestureDetector.Detach(Self());
685 mImpl->mTapGestureDetector.Reset();
688 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
690 mImpl->mLongPressGestureDetector.Detach(Self());
691 mImpl->mLongPressGestureDetector.Reset();
695 PinchGestureDetector Control::GetPinchGestureDetector() const
697 return mImpl->mPinchGestureDetector;
700 PanGestureDetector Control::GetPanGestureDetector() const
702 return mImpl->mPanGestureDetector;
705 TapGestureDetector Control::GetTapGestureDetector() const
707 return mImpl->mTapGestureDetector;
710 LongPressGestureDetector Control::GetLongPressGestureDetector() const
712 return mImpl->mLongPressGestureDetector;
715 void Control::SetKeyboardNavigationSupport(bool isSupported)
717 mImpl->mIsKeyboardNavigationSupported = isSupported;
720 bool Control::IsKeyboardNavigationSupported()
722 return mImpl->mIsKeyboardNavigationSupported;
725 void Control::SetKeyInputFocus()
727 if( Self().OnStage() )
729 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
733 bool Control::HasKeyInputFocus()
736 if( Self().OnStage() )
738 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
743 void Control::ClearKeyInputFocus()
745 if( Self().OnStage() )
747 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
751 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
753 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
755 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
756 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
759 bool Control::IsKeyboardFocusGroup()
761 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
764 void Control::AccessibilityActivate()
766 // Inform deriving classes
767 OnAccessibilityActivated();
770 bool Control::OnAccessibilityActivated()
772 return false; // Accessibility activation is not handled by default
775 bool Control::OnAccessibilityPan(PanGesture gesture)
777 return false; // Accessibility pan gesture is not handled by default
780 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
782 return false; // Accessibility touch event is not handled by default
785 bool Control::OnAccessibilityValueChange(bool isIncrease)
787 return false; // Accessibility value change action is not handled by default
790 bool Control::OnAccessibilityZoom()
792 return false; // Accessibility zoom action is not handled by default
795 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
800 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
804 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
806 return mImpl->mKeyEventSignal;
809 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusGainedSignal()
811 return mImpl->mKeyInputFocusGainedSignal;
814 Toolkit::Control::KeyInputFocusSignalType& Control:: KeyInputFocusLostSignal()
816 return mImpl->mKeyInputFocusLostSignal;
819 bool Control::EmitKeyEventSignal( const KeyEvent& event )
821 // Guard against destruction during signal emission
822 Dali::Toolkit::Control handle( GetOwner() );
824 bool consumed = false;
826 // signals are allocated dynamically when someone connects
827 if ( !mImpl->mKeyEventSignal.Empty() )
829 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
834 // Notification for derived classes
835 consumed = OnKeyEvent(event);
841 Control::Control( ControlBehaviour behaviourFlags )
842 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
843 mImpl(new Impl(*this))
845 mImpl->mFlags = behaviourFlags;
848 void Control::Initialize()
850 // Call deriving classes so initialised before styling is applied to them.
853 if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
855 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
857 // Register for style changes
858 styleManager.StyleChangeSignal().Connect( this, &Control::OnStyleChange );
860 // Apply the current style
861 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
864 if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
866 SetKeyboardNavigationSupport( true );
870 void Control::OnInitialize()
874 void Control::OnControlChildAdd( Actor& child )
878 void Control::OnControlChildRemove( Actor& child )
882 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
884 // By default the control is only interested in theme (not font) changes
885 if( change == StyleChange::THEME_CHANGE )
887 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
891 void Control::OnPinch(const PinchGesture& pinch)
893 if( !( mImpl->mStartingPinchScale ) )
896 mImpl->mStartingPinchScale = new Vector3;
899 if( pinch.state == Gesture::Started )
901 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
904 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
907 void Control::OnPan( const PanGesture& pan )
911 void Control::OnTap(const TapGesture& tap)
915 void Control::OnLongPress( const LongPressGesture& longPress )
919 void Control::EmitKeyInputFocusSignal( bool focusGained )
921 Dali::Toolkit::Control handle( GetOwner() );
925 // signals are allocated dynamically when someone connects
926 if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
928 mImpl->mKeyInputFocusGainedSignal.Emit( handle );
933 // signals are allocated dynamically when someone connects
934 if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
936 mImpl->mKeyInputFocusLostSignal.Emit( handle );
941 void Control::OnStageConnection( unsigned int depth )
943 unsigned int controlRendererCount = Self().GetRendererCount();
944 for( unsigned int i(0); i<controlRendererCount; ++i )
946 Renderer controlRenderer = Self().GetRendererAt(i);
947 if( controlRenderer )
949 controlRenderer.SetDepthIndex( CONTENT_DEPTH_INDEX+depth );
953 if( mImpl->mBackground )
955 if(mImpl->mBackground->actor.GetRendererCount() > 0 )
957 Renderer backgroundRenderer = mImpl->mBackground->actor.GetRendererAt( 0 );
958 if(backgroundRenderer)
960 backgroundRenderer.SetDepthIndex( BACKGROUND_DEPTH_INDEX+depth );
966 ImageActor imageActor = ImageActor::DownCast( mImpl->mBackground->actor );
969 imageActor.SetSortModifier( BACKGROUND_DEPTH_INDEX+depth );
975 void Control::OnStageDisconnection()
979 void Control::OnKeyInputFocusGained()
981 EmitKeyInputFocusSignal( true );
984 void Control::OnKeyInputFocusLost()
986 EmitKeyInputFocusSignal( false );
989 void Control::OnChildAdd(Actor& child)
991 // If this is the background actor, then we do not want to inform deriving classes
992 if ( mImpl->mAddRemoveBackgroundChild )
997 // Notify derived classes.
998 OnControlChildAdd( child );
1001 void Control::OnChildRemove(Actor& child)
1003 // If this is the background actor, then we do not want to inform deriving classes
1004 if ( mImpl->mAddRemoveBackgroundChild )
1009 // Notify derived classes.
1010 OnControlChildRemove( child );
1013 void Control::OnSizeSet(const Vector3& targetSize)
1015 // Background is resized through size negotiation
1018 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1020 // @todo size negotiate background to new size, animate as well?
1023 bool Control::OnTouchEvent(const TouchEvent& event)
1025 return false; // Do not consume
1028 bool Control::OnHoverEvent(const HoverEvent& event)
1030 return false; // Do not consume
1033 bool Control::OnKeyEvent(const KeyEvent& event)
1035 return false; // Do not consume
1038 bool Control::OnWheelEvent(const WheelEvent& event)
1040 return false; // Do not consume
1043 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
1045 for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
1047 container.Add( Self().GetChildAt( i ), size );
1051 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
1055 Vector3 Control::GetNaturalSize()
1057 //Control's natural size is the size of its background image if it has been set, or ZERO otherwise
1058 Vector3 naturalSize = Vector3::ZERO;
1059 if( mImpl->mBackground )
1061 if( mImpl->mBackground->actor.GetRendererCount() > 0 )
1063 Material backgroundMaterial = mImpl->mBackground->actor.GetRendererAt(0).GetMaterial();
1064 if( backgroundMaterial.GetNumberOfSamplers() > 0 )
1066 Image backgroundImage = backgroundMaterial.GetSamplerAt(0).GetImage();
1067 if( backgroundImage )
1069 naturalSize.x = backgroundImage.GetWidth();
1070 naturalSize.y = backgroundImage.GetHeight();
1076 return mImpl->mBackground->actor.GetNaturalSize();
1083 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
1085 return CalculateChildSizeBase( child, dimension );
1088 float Control::GetHeightForWidth( float width )
1090 if( mImpl->mBackground )
1092 Actor actor = mImpl->mBackground->actor;
1095 return actor.GetHeightForWidth( width );
1098 return GetHeightForWidthBase( width );
1101 float Control::GetWidthForHeight( float height )
1103 if( mImpl->mBackground )
1105 Actor actor = mImpl->mBackground->actor;
1108 return actor.GetWidthForHeight( height );
1111 return GetWidthForHeightBase( height );
1114 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
1116 return RelayoutDependentOnChildrenBase( dimension );
1119 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
1123 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
1127 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1129 mImpl->SignalConnected( slotObserver, callback );
1132 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1134 mImpl->SignalDisconnected( slotObserver, callback );
1137 Control& GetImplementation( Dali::Toolkit::Control& handle )
1139 CustomActorImpl& customInterface = handle.GetImplementation();
1140 // downcast to control
1141 Control& impl = dynamic_cast< Internal::Control& >( customInterface );
1145 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
1147 const CustomActorImpl& customInterface = handle.GetImplementation();
1148 // downcast to control
1149 const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
1153 } // namespace Internal
1155 } // namespace Toolkit