2 * Copyright (c) 2014 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>
23 #include <dali/public-api/actors/image-actor.h>
24 #include <dali/public-api/actors/mesh-actor.h>
25 #include <dali/public-api/animation/active-constraint.h>
26 #include <dali/public-api/animation/constraint.h>
27 #include <dali/public-api/animation/constraints.h>
28 #include <dali/public-api/geometry/mesh.h>
29 #include <dali/public-api/object/type-registry.h>
30 #include <dali/public-api/scripting/scripting.h>
31 #include <dali/integration-api/debug.h>
34 #include <dali-toolkit/internal/controls/relayout-controller.h>
35 #include <dali-toolkit/internal/controls/relayout-helper.h>
36 #include <dali-toolkit/public-api/focus-manager/keyinput-focus-manager.h>
37 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
38 #include <dali-toolkit/public-api/controls/control.h>
39 #include <dali-toolkit/public-api/styling/style-manager.h>
40 #include <dali-toolkit/internal/styling/style-manager-impl.h>
48 const Property::Index Control::PROPERTY_BACKGROUND_COLOR = Internal::Control::CONTROL_PROPERTY_START_INDEX;
49 const Property::Index Control::PROPERTY_BACKGROUND = Internal::Control::CONTROL_PROPERTY_START_INDEX + 1;
50 const Property::Index Control::PROPERTY_WIDTH_POLICY = Internal::Control::CONTROL_PROPERTY_START_INDEX + 2;
51 const Property::Index Control::PROPERTY_HEIGHT_POLICY = Internal::Control::CONTROL_PROPERTY_START_INDEX + 3;
52 const Property::Index Control::PROPERTY_MINIMUM_SIZE = Internal::Control::CONTROL_PROPERTY_START_INDEX + 4;
53 const Property::Index Control::PROPERTY_MAXIMUM_SIZE = Internal::Control::CONTROL_PROPERTY_START_INDEX + 5;
54 const Property::Index Control::PROPERTY_KEY_INPUT_FOCUS = Internal::Control::CONTROL_PROPERTY_START_INDEX + 6;
61 const char* const SIGNAL_KEY_EVENT = "key-event";
62 const char* const SIGNAL_TAPPED = "tapped";
63 const char* const SIGNAL_PANNED = "panned";
64 const char* const SIGNAL_PINCHED = "pinched";
65 const char* const SIGNAL_LONG_PRESSED = "long-pressed";
69 const char* const ACTION_CONTROL_ACTIVATED = "control-activated";
71 const Scripting::StringEnum< Control::SizePolicy > SIZE_POLICY_STRING_TABLE[] =
73 { "FIXED", Control::Fixed },
74 { "MINIMUM", Control::Minimum },
75 { "MAXIMUM", Control::Maximum },
76 { "RANGE", Control::Range },
77 { "FLEXIBLE", Control::Flexible },
79 const unsigned int SIZE_POLICY_STRING_TABLE_COUNT = sizeof( SIZE_POLICY_STRING_TABLE ) / sizeof( SIZE_POLICY_STRING_TABLE[0] );
81 #if defined(DEBUG_ENABLED)
82 Integration::Log::Filter* gLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_CONTROL");
85 const float MAX_FLOAT_VALUE( std::numeric_limits<float>::max() );
86 const Vector3 MAX_SIZE( MAX_FLOAT_VALUE, MAX_FLOAT_VALUE, MAX_FLOAT_VALUE );
87 const float BACKGROUND_ACTOR_Z_POSITION( -0.1f );
91 return Internal::Control::New();
94 TypeRegistration CONTROL_TYPE( typeid(Control), typeid(CustomActor), Create );
96 // Property Registration after Internal::Control::Impl definition below
98 TypeAction ACTION_TYPE_1( CONTROL_TYPE, ACTION_CONTROL_ACTIVATED, &Internal::Control::DoAction );
100 SignalConnectorType SIGNAL_CONNECTOR_1( CONTROL_TYPE, SIGNAL_KEY_EVENT, &Internal::Control::DoConnectSignal );
101 SignalConnectorType SIGNAL_CONNECTOR_2( CONTROL_TYPE, SIGNAL_TAPPED, &Internal::Control::DoConnectSignal );
102 SignalConnectorType SIGNAL_CONNECTOR_3( CONTROL_TYPE, SIGNAL_PANNED, &Internal::Control::DoConnectSignal );
103 SignalConnectorType SIGNAL_CONNECTOR_4( CONTROL_TYPE, SIGNAL_PINCHED, &Internal::Control::DoConnectSignal );
104 SignalConnectorType SIGNAL_CONNECTOR_5( CONTROL_TYPE, SIGNAL_LONG_PRESSED, &Internal::Control::DoConnectSignal );
107 * Structure which holds information about the background of a control
111 Actor actor; ///< Either a MeshActor or an ImageActor
112 Vector4 color; ///< The color of the actor.
119 color( Color::WHITE )
125 * Helper function to calculate a dimension given the policy of that dimension; the minimum &
126 * maximum values that dimension can be; and the allocated value for that dimension.
128 * @param[in] policy The size policy for that dimension.
129 * @param[in] minimum The minimum value that dimension can be.
130 * @param[in] maximum The maximum value that dimension can be.
131 * @param[in] allocated The value allocated for that dimension.
133 * @return The value that the dimension should be.
135 * @note This does not handle Control::Fixed policy.
137 float Calculate( Control::SizePolicy policy, float minimum, float maximum, float allocated )
139 float size( allocated );
145 // Use allocated value
149 case Control::Minimum:
151 // Size is always at least the minimum.
152 size = std::max( allocated, minimum );
156 case Control::Maximum:
158 // Size can grow but up to a maximum value.
159 size = std::min( allocated, maximum );
165 // Size is at least the minimum and can grow up to the maximum
166 size = std::max( size, minimum );
167 size = std::min( size, maximum );
171 case Control::Flexible:
173 // Size grows or shrinks with no limits.
180 DALI_ASSERT_DEBUG( false && "This function was not intended to be used by any other policy." );
189 * Creates a white coloured Mesh.
193 Vector3 white( Color::WHITE );
197 // Create vertices with a white color (actual color is set by actor color)
198 MeshData::VertexContainer vertices(4);
199 vertices[ 0 ] = MeshData::Vertex( Vector3( -0.5f, -0.5f, 0.0f ), Vector2::ZERO, white );
200 vertices[ 1 ] = MeshData::Vertex( Vector3( 0.5f, -0.5f, 0.0f ), Vector2::ZERO, white );
201 vertices[ 2 ] = MeshData::Vertex( Vector3( -0.5f, 0.5f, 0.0f ), Vector2::ZERO, white );
202 vertices[ 3 ] = MeshData::Vertex( Vector3( 0.5f, 0.5f, 0.0f ), Vector2::ZERO, white );
204 // Specify all the faces
205 MeshData::FaceIndices faces;
206 faces.reserve( 6 ); // 2 triangles in Quad
207 faces.push_back( 0 ); faces.push_back( 3 ); faces.push_back( 1 );
208 faces.push_back( 0 ); faces.push_back( 2 ); faces.push_back( 3 );
210 // Create the mesh data from the vertices and faces
211 meshData.SetMaterial( Material::New( "ControlMaterial" ) );
212 meshData.SetVertices( vertices );
213 meshData.SetFaceIndices( faces );
214 meshData.SetHasColor( true );
216 return Mesh::New( meshData );
220 * Sets all the required properties for the background actor.
222 * @param[in] actor The actor to set the properties on.
223 * @param[in] constrainingIndex The property index to constrain the parent's size on.
224 * @param[in] color The required color of the actor.
226 void SetupBackgroundActor( Actor actor, Property::Index constrainingIndex, const Vector4& color )
228 actor.SetColor( color );
229 actor.SetPositionInheritanceMode( USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
230 actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
231 actor.SetZ( BACKGROUND_ACTOR_Z_POSITION );
233 Constraint constraint = Constraint::New<Vector3>( constrainingIndex,
234 ParentSource( Actor::Property::SIZE ),
235 EqualToConstraint() );
236 actor.ApplyConstraint( constraint );
239 } // unnamed namespace
244 class Control::Impl : public ConnectionTracker
249 * Size indices for mMinMaxSize array
258 // Construction & Destruction
259 Impl(Control& controlImpl)
260 : mControlImpl( controlImpl ),
262 mStartingPinchScale( NULL ),
264 mPinchGestureDetector(),
265 mPanGestureDetector(),
266 mTapGestureDetector(),
267 mLongPressGestureDetector(),
270 mWidthPolicy( Toolkit::Control::Fixed ),
271 mHeightPolicy( Toolkit::Control::Fixed ),
272 mFlags( Control::CONTROL_BEHAVIOUR_NONE ),
273 mInsideRelayout( false ),
274 mIsKeyboardNavigationSupported( false ),
275 mIsKeyboardFocusGroup( false ),
276 mInitialized( false )
282 // All gesture detectors will be destroyed so no need to disconnect.
284 delete mStartingPinchScale;
287 // Gesture Detection Methods
289 void PinchDetected(Actor actor, const PinchGesture& pinch)
291 mControlImpl.OnPinch(pinch);
294 void PanDetected(Actor actor, const PanGesture& pan)
296 mControlImpl.OnPan(pan);
299 void TapDetected(Actor actor, const TapGesture& tap)
301 mControlImpl.OnTap(tap);
304 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
306 mControlImpl.OnLongPress(longPress);
309 // Background Methods
312 * Only creates an instance of the background if we actually use it.
313 * @return A reference to the Background structure.
315 Background& GetBackground()
319 mBackground = new Background;
327 * Called when a property of an object of this type is set.
328 * @param[in] object The object whose property is set.
329 * @param[in] index The property index.
330 * @param[in] value The new property value.
332 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
334 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
338 Control& controlImpl( control.GetImplementation() );
342 case Toolkit::Control::PROPERTY_BACKGROUND_COLOR:
344 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
348 case Toolkit::Control::PROPERTY_BACKGROUND:
350 if ( value.HasKey( "image" ) )
352 Property::Map imageMap = value.GetValue( "image" ).Get< Property::Map >();
353 Image image = Scripting::NewImage( imageMap );
357 controlImpl.SetBackground( image );
360 else if ( value.Get< Property::Map >().Empty() )
362 // An empty map means the background is no longer required
363 controlImpl.ClearBackground();
368 case Toolkit::Control::PROPERTY_WIDTH_POLICY:
370 controlImpl.mImpl->mWidthPolicy = Scripting::GetEnumeration< Toolkit::Control::SizePolicy >( value.Get< std::string >().c_str(), SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT );
374 case Toolkit::Control::PROPERTY_HEIGHT_POLICY:
376 controlImpl.mImpl->mHeightPolicy = Scripting::GetEnumeration< Toolkit::Control::SizePolicy >( value.Get< std::string >().c_str(), SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT );
380 case Toolkit::Control::PROPERTY_MINIMUM_SIZE:
382 controlImpl.SetMinimumSize( value.Get< Vector3 >() );
386 case Toolkit::Control::PROPERTY_MAXIMUM_SIZE:
388 controlImpl.SetMaximumSize( value.Get< Vector3 >() );
392 case Toolkit::Control::PROPERTY_KEY_INPUT_FOCUS:
394 if ( value.Get< bool >() )
396 controlImpl.SetKeyInputFocus();
400 controlImpl.ClearKeyInputFocus();
409 * Called to retrieve a property of an object of this type.
410 * @param[in] object The object whose property is to be retrieved.
411 * @param[in] index The property index.
412 * @return The current value of the property.
414 static Property::Value GetProperty( BaseObject* object, Property::Index index )
416 Property::Value value;
418 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
422 Control& controlImpl( control.GetImplementation() );
426 case Toolkit::Control::PROPERTY_BACKGROUND_COLOR:
428 value = controlImpl.GetBackgroundColor();
432 case Toolkit::Control::PROPERTY_BACKGROUND:
436 Actor actor = controlImpl.GetBackgroundActor();
439 ImageActor imageActor = ImageActor::DownCast( actor );
442 Image image = imageActor.GetImage();
443 Property::Map imageMap;
444 Scripting::CreatePropertyMap( image, imageMap );
445 map[ "image" ] = imageMap;
453 case Toolkit::Control::PROPERTY_WIDTH_POLICY:
455 value = std::string( Scripting::GetEnumerationName< Toolkit::Control::SizePolicy >( controlImpl.mImpl->mWidthPolicy, SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT ) );
459 case Toolkit::Control::PROPERTY_HEIGHT_POLICY:
461 value = std::string( Scripting::GetEnumerationName< Toolkit::Control::SizePolicy >( controlImpl.mImpl->mHeightPolicy, SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT ) );
465 case Toolkit::Control::PROPERTY_MINIMUM_SIZE:
467 value = controlImpl.mImpl->GetMinimumSize();
471 case Toolkit::Control::PROPERTY_MAXIMUM_SIZE:
473 value = controlImpl.mImpl->GetMaximumSize();
477 case Toolkit::Control::PROPERTY_KEY_INPUT_FOCUS:
479 value = controlImpl.HasKeyInputFocus();
489 * Helper to get minimum size
490 * @return minimum size
492 inline const Vector3& GetMinimumSize()
494 if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
496 return mMinMaxSize[ MIN_SIZE_INDEX ];
500 // its not been allocated so its ZERO
501 return Vector3::ZERO;
505 * Helper to Set minimum size
508 inline void SetMinimumSize( const Vector3& size )
510 if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
512 mMinMaxSize[ MIN_SIZE_INDEX ] = size;
516 // its not been allocated so push the new value there
517 mMinMaxSize.PushBack( size );
522 * Helper to get maximum size
523 * @return maximum size
525 inline const Vector3& GetMaximumSize()
527 if( mMinMaxSize.Count() > MAX_SIZE_INDEX )
529 return mMinMaxSize[ MAX_SIZE_INDEX ];
533 // its not been allocated so its MAX_SIZE
539 * Helper to Set minimum size
542 inline void SetMaximumSize( const Vector3& size )
544 if( mMinMaxSize.Count() > MAX_SIZE_INDEX )
546 mMinMaxSize[ MAX_SIZE_INDEX ] = size;
548 else if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
550 // max has not been allocated, but min has
551 mMinMaxSize.PushBack( size );
555 // min and max both unallocated so allocate both
556 mMinMaxSize.Resize( 2u ); // this will reserve and default construct two Vector3s
557 mMinMaxSize[ MAX_SIZE_INDEX ] = size;
563 Control& mControlImpl;
564 Background* mBackground; ///< Only create the background if we use it
565 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
566 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
569 PinchGestureDetector mPinchGestureDetector;
570 PanGestureDetector mPanGestureDetector;
571 TapGestureDetector mTapGestureDetector;
572 LongPressGestureDetector mLongPressGestureDetector;
573 // @todo change all these to Vector2 when we have a chance to sanitize the public API as well
574 Vector3 mCurrentSize; ///< Stores the current control's size, this is the negotiated size
575 Vector3 mNaturalSize; ///< Stores the size set through the Actor's API. This is size the actor wants to be. Useful when reset to the initial size is needed.
576 Dali::Vector< Vector3 > mMinMaxSize; ///< Stores the minimum and maximum size if they are set
578 Toolkit::Control::SizePolicy mWidthPolicy :3; ///< Stores the width policy. 3 bits covers 8 values
579 Toolkit::Control::SizePolicy mHeightPolicy :3; ///< Stores the height policy. 3 bits covers 8 values
580 ControlBehaviour mFlags :6; ///< Flags passed in from constructor. Need to increase this size when new enums are added
581 bool mInsideRelayout:1; ///< Detect when were in Relayout
582 bool mIsKeyboardNavigationSupported:1; ///< Stores whether keyboard navigation is supported by the control.
583 bool mIsKeyboardFocusGroup:1; ///< Stores whether the control is a focus group.
586 // Properties - these need to be members of Internal::Control::Impl as they need to functions within this class.
587 static PropertyRegistration PROPERTY_1;
588 static PropertyRegistration PROPERTY_2;
589 static PropertyRegistration PROPERTY_3;
590 static PropertyRegistration PROPERTY_4;
591 static PropertyRegistration PROPERTY_5;
592 static PropertyRegistration PROPERTY_6;
593 static PropertyRegistration PROPERTY_7;
596 PropertyRegistration Control::Impl::PROPERTY_1( CONTROL_TYPE, "background-color", Toolkit::Control::PROPERTY_BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
597 PropertyRegistration Control::Impl::PROPERTY_2( CONTROL_TYPE, "background", Toolkit::Control::PROPERTY_BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
598 PropertyRegistration Control::Impl::PROPERTY_3( CONTROL_TYPE, "width-policy", Toolkit::Control::PROPERTY_WIDTH_POLICY, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
599 PropertyRegistration Control::Impl::PROPERTY_4( CONTROL_TYPE, "height-policy", Toolkit::Control::PROPERTY_HEIGHT_POLICY, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
600 PropertyRegistration Control::Impl::PROPERTY_5( CONTROL_TYPE, "minimum-size", Toolkit::Control::PROPERTY_MINIMUM_SIZE, Property::VECTOR3, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
601 PropertyRegistration Control::Impl::PROPERTY_6( CONTROL_TYPE, "maximum-size", Toolkit::Control::PROPERTY_MAXIMUM_SIZE, Property::VECTOR3, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
602 PropertyRegistration Control::Impl::PROPERTY_7( CONTROL_TYPE, "key-input-focus", Toolkit::Control::PROPERTY_KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
604 Toolkit::Control Control::New()
606 // Create the implementation, temporarily owned on stack
607 IntrusivePtr<Control> controlImpl = new Control( CONTROL_BEHAVIOUR_NONE );
609 // Pass ownership to handle
610 Toolkit::Control handle( *controlImpl );
612 // Second-phase init of the implementation
613 // This can only be done after the CustomActor connection has been made...
614 controlImpl->Initialize();
624 void Control::SetSizePolicy( Toolkit::Control::SizePolicy widthPolicy, Toolkit::Control::SizePolicy heightPolicy )
626 bool relayoutRequest( false );
628 if ( ( mImpl->mWidthPolicy != widthPolicy ) || ( mImpl->mHeightPolicy != heightPolicy ) )
630 relayoutRequest = true;
633 mImpl->mWidthPolicy = widthPolicy;
634 mImpl->mHeightPolicy = heightPolicy;
636 // Ensure RelayoutRequest is called AFTER new policies have been set.
637 if ( relayoutRequest )
643 void Control::GetSizePolicy( Toolkit::Control::SizePolicy& widthPolicy, Toolkit::Control::SizePolicy& heightPolicy ) const
645 widthPolicy = mImpl->mWidthPolicy;
646 heightPolicy = mImpl->mHeightPolicy;
649 void Control::SetMinimumSize( const Vector3& size )
651 const Vector3& minSize = mImpl->GetMinimumSize();
652 if ( fabsf( minSize.width - size.width ) > Math::MACHINE_EPSILON_1000 ||
653 fabsf( minSize.height - size.height ) > Math::MACHINE_EPSILON_1000 )
655 mImpl->SetMinimumSize( size );
657 // Only relayout if our control is using the minimum or range policy.
658 if ( ( mImpl->mHeightPolicy == Toolkit::Control::Minimum ) || ( mImpl->mWidthPolicy == Toolkit::Control::Minimum ) ||
659 ( mImpl->mHeightPolicy == Toolkit::Control::Range ) || ( mImpl->mWidthPolicy == Toolkit::Control::Range ) )
666 const Vector3& Control::GetMinimumSize() const
668 return mImpl->GetMinimumSize();
671 void Control::SetMaximumSize( const Vector3& size )
673 const Vector3& maxSize = mImpl->GetMaximumSize();
674 if ( fabsf( maxSize.width - size.width ) > Math::MACHINE_EPSILON_1000 ||
675 fabsf( maxSize.height - size.height ) > Math::MACHINE_EPSILON_1000 )
677 mImpl->SetMaximumSize( size );
679 // Only relayout if our control is using the maximum or range policy.
680 if ( ( mImpl->mHeightPolicy == Toolkit::Control::Maximum ) || ( mImpl->mWidthPolicy == Toolkit::Control::Maximum ) ||
681 ( mImpl->mHeightPolicy == Toolkit::Control::Range ) || ( mImpl->mWidthPolicy == Toolkit::Control::Range ) )
688 const Vector3& Control::GetMaximumSize() const
690 return mImpl->GetMaximumSize();
693 Vector3 Control::GetNaturalSize()
695 // could be overridden in derived classes.
696 return mImpl->mNaturalSize;
699 float Control::GetHeightForWidth( float width )
701 // could be overridden in derived classes.
702 float height( 0.0f );
703 if ( mImpl->mNaturalSize.width > 0.0f )
705 height = mImpl->mNaturalSize.height * width / mImpl->mNaturalSize.width;
710 float Control::GetWidthForHeight( float height )
712 // could be overridden in derived classes.
714 if ( mImpl->mNaturalSize.height > 0.0f )
716 width = mImpl->mNaturalSize.width * height / mImpl->mNaturalSize.height;
721 const Vector3& Control::GetControlSize() const
723 return mImpl->mCurrentSize;
726 const Vector3& Control::GetSizeSet() const
728 return mImpl->mNaturalSize;
731 void Control::SetKeyInputFocus()
733 if( Self().OnStage() )
735 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
739 bool Control::HasKeyInputFocus()
742 if( Self().OnStage() )
744 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
749 void Control::ClearKeyInputFocus()
751 if( Self().OnStage() )
753 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
757 PinchGestureDetector Control::GetPinchGestureDetector() const
759 return mImpl->mPinchGestureDetector;
762 PanGestureDetector Control::GetPanGestureDetector() const
764 return mImpl->mPanGestureDetector;
767 TapGestureDetector Control::GetTapGestureDetector() const
769 return mImpl->mTapGestureDetector;
772 LongPressGestureDetector Control::GetLongPressGestureDetector() const
774 return mImpl->mLongPressGestureDetector;
777 void Control::SetBackgroundColor( const Vector4& color )
779 Background& background( mImpl->GetBackground() );
781 if ( background.actor )
783 // Just set the actor color
784 background.actor.SetColor( color );
789 MeshActor meshActor = MeshActor::New( CreateMesh() );
791 SetupBackgroundActor( meshActor, Actor::Property::SCALE, color );
793 // Set the background actor before adding so that we do not inform deriving classes
794 background.actor = meshActor;
795 Self().Add( meshActor );
798 background.color = color;
801 Vector4 Control::GetBackgroundColor() const
803 if ( mImpl->mBackground )
805 return mImpl->mBackground->color;
807 return Color::TRANSPARENT;
810 void Control::SetBackground( Image image )
812 Background& background( mImpl->GetBackground() );
814 if ( background.actor )
816 // Remove Current actor, unset AFTER removal so that we do not inform deriving classes
817 Self().Remove( background.actor );
818 background.actor.Reset();
821 ImageActor imageActor = ImageActor::New( image );
822 SetupBackgroundActor( imageActor, Actor::Property::SIZE, background.color );
824 // Set the background actor before adding so that we do not inform derived classes
825 background.actor = imageActor;
826 Self().Add( imageActor );
829 void Control::ClearBackground()
831 if ( mImpl->mBackground )
833 Background& background( mImpl->GetBackground() );
834 Self().Remove( background.actor );
836 delete mImpl->mBackground;
837 mImpl->mBackground = NULL;
841 Actor Control::GetBackgroundActor() const
843 if ( mImpl->mBackground )
845 return mImpl->mBackground->actor;
851 void Control::SetKeyboardNavigationSupport(bool isSupported)
853 mImpl->mIsKeyboardNavigationSupported = isSupported;
856 bool Control::IsKeyboardNavigationSupported()
858 return mImpl->mIsKeyboardNavigationSupported;
861 void Control::Activate()
863 // Inform deriving classes
867 bool Control::OnAccessibilityPan(PanGesture gesture)
869 return false; // Accessibility pan gesture is not handled by default
872 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
874 return false; // Accessibility touch event is not handled by default
877 bool Control::OnAccessibilityValueChange(bool isIncrease)
879 return false; // Accessibility value change action is not handled by default
882 void Control::NegotiateSize( const Vector2& allocatedSize, ActorSizeContainer& container )
886 if ( mImpl->mWidthPolicy == Toolkit::Control::Fixed )
888 if ( mImpl->mHeightPolicy == Toolkit::Control::Fixed )
890 // If a control says it has a fixed size, then use the size set by the application / control.
891 Vector2 setSize( mImpl->mNaturalSize );
892 if ( setSize != Vector2::ZERO )
896 // Policy is set to Fixed, so if the application / control has not set one of the dimensions,
897 // then we should use the natural size of the control rather than the full allocation.
898 if ( EqualsZero( size.width ) )
900 size.width = GetWidthForHeight( size.height );
902 else if ( EqualsZero( size.height ) )
904 size.height = GetHeightForWidth( size.width );
909 // If that is not set then set the size to the control's natural size
910 size = Vector2( GetNaturalSize() );
915 // Width is fixed so if the application / control has set it, then use that.
916 if ( !EqualsZero( mImpl->mNaturalSize.width ) )
918 size.width = mImpl->mNaturalSize.width;
922 // Otherwise, set the width to what has been allocated.
923 size.width = allocatedSize.width;
926 // Height is flexible so ask control what the height should be for our width.
927 size.height = GetHeightForWidth( size.width );
929 // Ensure height is within our policy rules
930 size.height = Calculate( mImpl->mHeightPolicy, GetMinimumSize().height, GetMaximumSize().height, size.height );
935 if ( mImpl->mHeightPolicy == Toolkit::Control::Fixed )
937 // Height is fixed so if the application / control has set it, then use that.
938 if ( !EqualsZero( mImpl->mNaturalSize.height ) )
940 size.height = mImpl->mNaturalSize.height;
944 // Otherwise, set the height to what has been allocated.
945 size.height = allocatedSize.height;
948 // Width is flexible so ask control what the width should be for our height.
949 size.width = GetWidthForHeight( size.height );
951 // Ensure width is within our policy rules
952 size.width = Calculate( mImpl->mWidthPolicy, mImpl->GetMinimumSize().width, mImpl->GetMaximumSize().width, size.width );
956 // Width and height are BOTH flexible.
957 // Calculate the width and height using the policy rules.
958 size.width = Calculate( mImpl->mWidthPolicy, mImpl->GetMinimumSize().width, mImpl->GetMaximumSize().width, allocatedSize.width );
959 size.height = Calculate( mImpl->mHeightPolicy, mImpl->GetMinimumSize().height, mImpl->GetMaximumSize().height, allocatedSize.height );
963 // If the width has not been set, then set to the allocated width.
964 // Also if the width set is greater than the allocated, then set to allocated (no exceed support).
965 if ( EqualsZero( size.width ) || ( size.width > allocatedSize.width ) )
967 size.width = allocatedSize.width;
970 // If the height has not been set, then set to the allocated height.
971 // Also if the height set is greater than the allocated, then set to allocated (no exceed support).
972 if ( EqualsZero( size.height ) || ( size.height > allocatedSize.height ) )
974 size.height = allocatedSize.height;
977 DALI_LOG_INFO( gLogFilter, Debug::Verbose,
978 "%p: Natural: [%.2f, %.2f] Allocated: [%.2f, %.2f] Set: [%.2f, %.2f]\n",
979 Self().GetObjectPtr(),
980 GetNaturalSize().x, GetNaturalSize().y,
981 allocatedSize.x, allocatedSize.y,
984 // Avoids relayout again when OnSizeSet callback arrives as a function of us or deriving class calling SetSize()
985 mImpl->mInsideRelayout = true;
986 Self().SetSize( size );
987 // Only relayout controls which requested to be relaid out.
988 OnRelayout( size, container );
989 mImpl->mInsideRelayout = false;
992 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
994 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
996 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
997 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
1000 bool Control::IsKeyboardFocusGroup()
1002 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
1005 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
1010 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1014 bool Control::DoAction(BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes)
1018 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_CONTROL_ACTIVATED ) ) )
1020 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
1023 // if cast succeeds there is an implementation so no need to check
1024 control.GetImplementation().OnActivated();
1031 bool Control::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1033 Dali::BaseHandle handle( object );
1035 bool connected( false );
1036 Toolkit::Control control = Toolkit::Control::DownCast( handle );
1039 Control& controlImpl( control.GetImplementation() );
1042 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
1044 controlImpl.KeyEventSignal().Connect( tracker, functor );
1046 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
1048 controlImpl.EnableGestureDetection( Gesture::Tap );
1049 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
1051 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
1053 controlImpl.EnableGestureDetection( Gesture::Pan );
1054 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
1056 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
1058 controlImpl.EnableGestureDetection( Gesture::Pinch );
1059 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
1061 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
1063 controlImpl.EnableGestureDetection( Gesture::LongPress );
1064 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
1068 // signalName does not match any signal
1075 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
1077 return mImpl->mKeyEventSignal;
1080 bool Control::EmitKeyEventSignal( const KeyEvent& event )
1082 // Guard against destruction during signal emission
1083 Dali::Toolkit::Control handle( GetOwner() );
1085 bool consumed = false;
1087 // signals are allocated dynamically when someone connects
1088 if ( !mImpl->mKeyEventSignal.Empty() )
1090 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
1095 // Notification for derived classes
1096 consumed = OnKeyEvent(event);
1102 Control::Control( ControlBehaviour behaviourFlags )
1103 : CustomActorImpl( behaviourFlags & REQUIRES_TOUCH_EVENTS ),
1104 mImpl(new Impl(*this))
1106 mImpl->mFlags = behaviourFlags;
1109 void Control::Initialize()
1112 // Calling deriving classes
1115 if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
1117 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1119 // Register for style changes
1120 styleManager.StyleChangeSignal().Connect( this, &Control::DoStyleChange );
1123 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1126 SetRequiresHoverEvents(mImpl->mFlags & REQUIRES_HOVER_EVENTS);
1127 SetRequiresMouseWheelEvents(mImpl->mFlags & REQUIRES_MOUSE_WHEEL_EVENTS);
1129 mImpl->mInitialized = true;
1132 void Control::EnableGestureDetection(Gesture::Type type)
1134 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
1136 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
1137 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
1138 mImpl->mPinchGestureDetector.Attach(Self());
1141 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
1143 mImpl->mPanGestureDetector = PanGestureDetector::New();
1144 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
1145 mImpl->mPanGestureDetector.Attach(Self());
1148 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
1150 mImpl->mTapGestureDetector = TapGestureDetector::New();
1151 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
1152 mImpl->mTapGestureDetector.Attach(Self());
1155 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
1157 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
1158 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
1159 mImpl->mLongPressGestureDetector.Attach(Self());
1163 void Control::DisableGestureDetection(Gesture::Type type)
1165 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
1167 mImpl->mPinchGestureDetector.Detach(Self());
1168 mImpl->mPinchGestureDetector.Reset();
1171 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
1173 mImpl->mPanGestureDetector.Detach(Self());
1174 mImpl->mPanGestureDetector.Reset();
1177 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
1179 mImpl->mTapGestureDetector.Detach(Self());
1180 mImpl->mTapGestureDetector.Reset();
1183 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
1185 mImpl->mLongPressGestureDetector.Detach(Self());
1186 mImpl->mLongPressGestureDetector.Reset();
1190 void Control::RelayoutRequest()
1192 // unfortunate double negative but thats to guarantee new controls get size negotiation
1193 // by default and have to "opt-out" if they dont want it
1194 if( !(mImpl->mFlags & NO_SIZE_NEGOTIATION) )
1196 Internal::RelayoutController::Request();
1200 void Control::Relayout( Actor actor, const Vector2& size, ActorSizeContainer& container )
1204 Toolkit::Control control( Toolkit::Control::DownCast( actor ) );
1207 control.GetImplementation().NegotiateSize( size, container );
1211 container.push_back( ActorSizePair( actor, size ) );
1216 void Control::OnInitialize()
1220 void Control::OnActivated()
1224 void Control::OnThemeChange( Toolkit::StyleManager styleManager )
1226 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1229 void Control::OnFontChange( bool defaultFontChange, bool defaultFontSizeChange )
1233 void Control::OnPinch(const PinchGesture& pinch)
1235 if( !( mImpl->mStartingPinchScale ) )
1238 mImpl->mStartingPinchScale = new Vector3;
1241 if( pinch.state == Gesture::Started )
1243 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1246 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1249 void Control::OnPan( const PanGesture& pan )
1253 void Control::OnTap(const TapGesture& tap)
1257 void Control::OnLongPress( const LongPressGesture& longPress )
1261 void Control::OnControlStageConnection()
1265 void Control::OnControlStageDisconnection()
1269 void Control::OnControlChildAdd( Actor& child )
1273 void Control::OnControlChildRemove( Actor& child )
1277 void Control::OnControlSizeSet( const Vector3& size )
1281 void Control::OnRelayout( const Vector2& size, ActorSizeContainer& container )
1283 unsigned int numChildren = Self().GetChildCount();
1285 for( unsigned int i=0; i<numChildren; ++i )
1287 container.push_back( ActorSizePair( Self().GetChildAt(i), size ) );
1291 void Control::OnKeyInputFocusGained()
1296 void Control::OnKeyInputFocusLost()
1301 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1303 // @todo consider animating negotiated child sizes to target size
1306 bool Control::OnTouchEvent(const TouchEvent& event)
1308 return false; // Do not consume
1311 bool Control::OnHoverEvent(const HoverEvent& event)
1313 return false; // Do not consume
1316 bool Control::OnKeyEvent(const KeyEvent& event)
1318 return false; // Do not consume
1321 bool Control::OnMouseWheelEvent(const MouseWheelEvent& event)
1323 return false; // Do not consume
1326 Actor Control::GetChildByAlias(const std::string& actorAlias)
1331 void Control::OnStageConnection()
1335 // Notify derived classes.
1336 OnControlStageConnection();
1339 void Control::OnStageDisconnection()
1341 // Notify derived classes
1342 OnControlStageDisconnection();
1345 void Control::OnChildAdd(Actor& child)
1347 // If this is the background actor, then we do not want to relayout or inform deriving classes
1348 if ( mImpl->mBackground && ( child == mImpl->mBackground->actor ) )
1353 // Request for relayout as we may need to position the new child and old ones
1356 // Notify derived classes.
1357 OnControlChildAdd( child );
1360 void Control::OnChildRemove(Actor& child)
1362 // If this is the background actor, then we do not want to relayout or inform deriving classes
1363 if ( mImpl->mBackground && ( child == mImpl->mBackground->actor ) )
1368 // Request for relayout as we may need to re-position the old child
1371 // Notify derived classes.
1372 OnControlChildRemove( child );
1375 void Control::OnSizeSet(const Vector3& targetSize)
1377 if( ( !mImpl->mInsideRelayout ) && ( targetSize != mImpl->mNaturalSize ) )
1379 // Only updates size if set through Actor's API
1380 mImpl->mNaturalSize = targetSize;
1383 if( targetSize != mImpl->mCurrentSize )
1385 // Update control size.
1386 mImpl->mCurrentSize = targetSize;
1388 // Notify derived classes.
1389 OnControlSizeSet( targetSize );
1393 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1395 mImpl->SignalConnected( slotObserver, callback );
1398 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1400 mImpl->SignalDisconnected( slotObserver, callback );
1403 void Control::DoStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
1405 if( change.themeChange )
1407 OnThemeChange( styleManager );
1409 else if( change.defaultFontChange || change.defaultFontSizeChange )
1411 OnFontChange( change.defaultFontChange, change.defaultFontSizeChange );
1415 } // namespace Internal
1417 } // namespace Toolkit