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/object/type-registry-helper.h>
31 #include <dali/public-api/scripting/scripting.h>
32 #include <dali/integration-api/debug.h>
35 #include <dali-toolkit/internal/controls/relayout-controller.h>
36 #include <dali-toolkit/internal/controls/relayout-helper.h>
37 #include <dali-toolkit/public-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/public-api/styling/style-manager.h>
41 #include <dali-toolkit/internal/styling/style-manager-impl.h>
52 const Scripting::StringEnum< Control::SizePolicy > SIZE_POLICY_STRING_TABLE[] =
54 { "FIXED", Control::Fixed },
55 { "MINIMUM", Control::Minimum },
56 { "MAXIMUM", Control::Maximum },
57 { "RANGE", Control::Range },
58 { "FLEXIBLE", Control::Flexible },
60 const unsigned int SIZE_POLICY_STRING_TABLE_COUNT = sizeof( SIZE_POLICY_STRING_TABLE ) / sizeof( SIZE_POLICY_STRING_TABLE[0] );
62 #if defined(DEBUG_ENABLED)
63 Integration::Log::Filter* gLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_CONTROL");
66 const float MAX_FLOAT_VALUE( std::numeric_limits<float>::max() );
67 const Vector3 MAX_SIZE( MAX_FLOAT_VALUE, MAX_FLOAT_VALUE, MAX_FLOAT_VALUE );
68 const float BACKGROUND_ACTOR_Z_POSITION( -0.1f );
72 return Internal::Control::New();
75 // Setup signals and actions using the type-registry.
76 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
78 // Note: Properties are registered separately below,
80 DALI_SIGNAL_REGISTRATION( Control, "key-event", SIGNAL_KEY_EVENT )
81 DALI_SIGNAL_REGISTRATION( Control, "tapped", SIGNAL_TAPPED )
82 DALI_SIGNAL_REGISTRATION( Control, "panned", SIGNAL_PANNED )
83 DALI_SIGNAL_REGISTRATION( Control, "pinched", SIGNAL_PINCHED )
84 DALI_SIGNAL_REGISTRATION( Control, "long-pressed", SIGNAL_LONG_PRESSED )
86 DALI_ACTION_REGISTRATION( Control, "control-activated", ACTION_CONTROL_ACTIVATED )
88 DALI_TYPE_REGISTRATION_END()
91 * Structure which holds information about the background of a control
95 Actor actor; ///< Either a MeshActor or an ImageActor
96 Vector4 color; ///< The color of the actor.
103 color( Color::WHITE )
109 * Helper function to calculate a dimension given the policy of that dimension; the minimum &
110 * maximum values that dimension can be; and the allocated value for that dimension.
112 * @param[in] policy The size policy for that dimension.
113 * @param[in] minimum The minimum value that dimension can be.
114 * @param[in] maximum The maximum value that dimension can be.
115 * @param[in] allocated The value allocated for that dimension.
117 * @return The value that the dimension should be.
119 * @note This does not handle Control::Fixed policy.
121 float Calculate( Control::SizePolicy policy, float minimum, float maximum, float allocated )
123 float size( allocated );
129 // Use allocated value
133 case Control::Minimum:
135 // Size is always at least the minimum.
136 size = std::max( allocated, minimum );
140 case Control::Maximum:
142 // Size can grow but up to a maximum value.
143 size = std::min( allocated, maximum );
149 // Size is at least the minimum and can grow up to the maximum
150 size = std::max( size, minimum );
151 size = std::min( size, maximum );
155 case Control::Flexible:
157 // Size grows or shrinks with no limits.
164 DALI_ASSERT_DEBUG( false && "This function was not intended to be used by any other policy." );
173 * Creates a white coloured Mesh.
177 Vector3 white( Color::WHITE );
181 // Create vertices with a white color (actual color is set by actor color)
182 MeshData::VertexContainer vertices(4);
183 vertices[ 0 ] = MeshData::Vertex( Vector3( -0.5f, -0.5f, 0.0f ), Vector2::ZERO, white );
184 vertices[ 1 ] = MeshData::Vertex( Vector3( 0.5f, -0.5f, 0.0f ), Vector2::ZERO, white );
185 vertices[ 2 ] = MeshData::Vertex( Vector3( -0.5f, 0.5f, 0.0f ), Vector2::ZERO, white );
186 vertices[ 3 ] = MeshData::Vertex( Vector3( 0.5f, 0.5f, 0.0f ), Vector2::ZERO, white );
188 // Specify all the faces
189 MeshData::FaceIndices faces;
190 faces.reserve( 6 ); // 2 triangles in Quad
191 faces.push_back( 0 ); faces.push_back( 3 ); faces.push_back( 1 );
192 faces.push_back( 0 ); faces.push_back( 2 ); faces.push_back( 3 );
194 // Create the mesh data from the vertices and faces
195 meshData.SetMaterial( Material::New( "ControlMaterial" ) );
196 meshData.SetVertices( vertices );
197 meshData.SetFaceIndices( faces );
198 meshData.SetHasColor( true );
200 return Mesh::New( meshData );
204 * Sets all the required properties for the background actor.
206 * @param[in] actor The actor to set the properties on.
207 * @param[in] constrainingIndex The property index to constrain the parent's size on.
208 * @param[in] color The required color of the actor.
210 void SetupBackgroundActor( Actor actor, Property::Index constrainingIndex, const Vector4& color )
212 actor.SetColor( color );
213 actor.SetPositionInheritanceMode( USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
214 actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
215 actor.SetZ( BACKGROUND_ACTOR_Z_POSITION );
217 Constraint constraint = Constraint::New<Vector3>( constrainingIndex,
218 ParentSource( Actor::Property::SIZE ),
219 EqualToConstraint() );
220 actor.ApplyConstraint( constraint );
223 } // unnamed namespace
228 class Control::Impl : public ConnectionTracker
233 * Size indices for mMinMaxSize array
242 // Construction & Destruction
243 Impl(Control& controlImpl)
244 : mControlImpl( controlImpl ),
247 mStartingPinchScale( NULL ),
249 mPinchGestureDetector(),
250 mPanGestureDetector(),
251 mTapGestureDetector(),
252 mLongPressGestureDetector(),
255 mWidthPolicy( Toolkit::Control::Fixed ),
256 mHeightPolicy( Toolkit::Control::Fixed ),
257 mFlags( Control::CONTROL_BEHAVIOUR_NONE ),
258 mInsideRelayout( false ),
259 mIsKeyboardNavigationSupported( false ),
260 mIsKeyboardFocusGroup( false ),
261 mInitialized( false )
267 // All gesture detectors will be destroyed so no need to disconnect.
269 delete mStartingPinchScale;
272 // Gesture Detection Methods
274 void PinchDetected(Actor actor, const PinchGesture& pinch)
276 mControlImpl.OnPinch(pinch);
279 void PanDetected(Actor actor, const PanGesture& pan)
281 mControlImpl.OnPan(pan);
284 void TapDetected(Actor actor, const TapGesture& tap)
286 mControlImpl.OnTap(tap);
289 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
291 mControlImpl.OnLongPress(longPress);
294 // Background Methods
297 * Only creates an instance of the background if we actually use it.
298 * @return A reference to the Background structure.
300 Background& GetBackground()
304 mBackground = new Background;
312 * Called when a property of an object of this type is set.
313 * @param[in] object The object whose property is set.
314 * @param[in] index The property index.
315 * @param[in] value The new property value.
317 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
319 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
323 Control& controlImpl( control.GetImplementation() );
327 case Toolkit::Control::Property::STYLE_NAME:
329 controlImpl.SetStyleName( value.Get< std::string >() );
333 case Toolkit::Control::Property::BACKGROUND_COLOR:
335 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
339 case Toolkit::Control::Property::BACKGROUND:
341 if ( value.HasKey( "image" ) )
343 Property::Map imageMap = value.GetValue( "image" ).Get< Property::Map >();
344 Image image = Scripting::NewImage( imageMap );
348 controlImpl.SetBackground( image );
351 else if ( value.Get< Property::Map >().Empty() )
353 // An empty map means the background is no longer required
354 controlImpl.ClearBackground();
359 case Toolkit::Control::Property::WIDTH_POLICY:
361 controlImpl.mImpl->mWidthPolicy = Scripting::GetEnumeration< Toolkit::Control::SizePolicy >( value.Get< std::string >().c_str(), SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT );
365 case Toolkit::Control::Property::HEIGHT_POLICY:
367 controlImpl.mImpl->mHeightPolicy = Scripting::GetEnumeration< Toolkit::Control::SizePolicy >( value.Get< std::string >().c_str(), SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT );
371 case Toolkit::Control::Property::MINIMUM_SIZE:
373 controlImpl.SetMinimumSize( value.Get< Vector3 >() );
377 case Toolkit::Control::Property::MAXIMUM_SIZE:
379 controlImpl.SetMaximumSize( value.Get< Vector3 >() );
383 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
385 if ( value.Get< bool >() )
387 controlImpl.SetKeyInputFocus();
391 controlImpl.ClearKeyInputFocus();
400 * Called to retrieve a property of an object of this type.
401 * @param[in] object The object whose property is to be retrieved.
402 * @param[in] index The property index.
403 * @return The current value of the property.
405 static Property::Value GetProperty( BaseObject* object, Property::Index index )
407 Property::Value value;
409 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
413 Control& controlImpl( control.GetImplementation() );
417 case Toolkit::Control::Property::STYLE_NAME:
419 value = controlImpl.GetStyleName();
423 case Toolkit::Control::Property::BACKGROUND_COLOR:
425 value = controlImpl.GetBackgroundColor();
429 case Toolkit::Control::Property::BACKGROUND:
433 Actor actor = controlImpl.GetBackgroundActor();
436 ImageActor imageActor = ImageActor::DownCast( actor );
439 Image image = imageActor.GetImage();
440 Property::Map imageMap;
441 Scripting::CreatePropertyMap( image, imageMap );
442 map[ "image" ] = imageMap;
450 case Toolkit::Control::Property::WIDTH_POLICY:
452 value = std::string( Scripting::GetEnumerationName< Toolkit::Control::SizePolicy >( controlImpl.mImpl->mWidthPolicy, SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT ) );
456 case Toolkit::Control::Property::HEIGHT_POLICY:
458 value = std::string( Scripting::GetEnumerationName< Toolkit::Control::SizePolicy >( controlImpl.mImpl->mHeightPolicy, SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT ) );
462 case Toolkit::Control::Property::MINIMUM_SIZE:
464 value = controlImpl.mImpl->GetMinimumSize();
468 case Toolkit::Control::Property::MAXIMUM_SIZE:
470 value = controlImpl.mImpl->GetMaximumSize();
474 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
476 value = controlImpl.HasKeyInputFocus();
486 * Helper to get minimum size
487 * @return minimum size
489 inline const Vector3& GetMinimumSize()
491 if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
493 return mMinMaxSize[ MIN_SIZE_INDEX ];
497 // its not been allocated so its ZERO
498 return Vector3::ZERO;
502 * Helper to Set minimum size
505 inline void SetMinimumSize( const Vector3& size )
507 if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
509 mMinMaxSize[ MIN_SIZE_INDEX ] = size;
513 // its not been allocated so push the new value there
514 mMinMaxSize.PushBack( size );
519 * Helper to get maximum size
520 * @return maximum size
522 inline const Vector3& GetMaximumSize()
524 if( mMinMaxSize.Count() > MAX_SIZE_INDEX )
526 return mMinMaxSize[ MAX_SIZE_INDEX ];
530 // its not been allocated so its MAX_SIZE
536 * Helper to Set minimum size
539 inline void SetMaximumSize( const Vector3& size )
541 if( mMinMaxSize.Count() > MAX_SIZE_INDEX )
543 mMinMaxSize[ MAX_SIZE_INDEX ] = size;
545 else if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
547 // max has not been allocated, but min has
548 mMinMaxSize.PushBack( size );
552 // min and max both unallocated so allocate both
553 mMinMaxSize.Resize( 2u ); // this will reserve and default construct two Vector3s
554 mMinMaxSize[ MAX_SIZE_INDEX ] = size;
560 Control& mControlImpl;
561 std::string mStyleName;
562 Background* mBackground; ///< Only create the background if we use it
563 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
564 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
567 PinchGestureDetector mPinchGestureDetector;
568 PanGestureDetector mPanGestureDetector;
569 TapGestureDetector mTapGestureDetector;
570 LongPressGestureDetector mLongPressGestureDetector;
571 // @todo change all these to Vector2 when we have a chance to sanitize the public API as well
572 Vector3 mCurrentSize; ///< Stores the current control's size, this is the negotiated size
573 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.
574 Dali::Vector< Vector3 > mMinMaxSize; ///< Stores the minimum and maximum size if they are set
576 Toolkit::Control::SizePolicy mWidthPolicy :3; ///< Stores the width policy. 3 bits covers 8 values
577 Toolkit::Control::SizePolicy mHeightPolicy :3; ///< Stores the height policy. 3 bits covers 8 values
578 ControlBehaviour mFlags :6; ///< Flags passed in from constructor. Need to increase this size when new enums are added
579 bool mInsideRelayout:1; ///< Detect when were in Relayout
580 bool mIsKeyboardNavigationSupported:1; ///< Stores whether keyboard navigation is supported by the control.
581 bool mIsKeyboardFocusGroup:1; ///< Stores whether the control is a focus group.
584 // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
585 static PropertyRegistration PROPERTY_1;
586 static PropertyRegistration PROPERTY_2;
587 static PropertyRegistration PROPERTY_3;
588 static PropertyRegistration PROPERTY_4;
589 static PropertyRegistration PROPERTY_5;
590 static PropertyRegistration PROPERTY_6;
591 static PropertyRegistration PROPERTY_7;
592 static PropertyRegistration PROPERTY_8;
595 // Properties registered without macro to use specific member variables.
596 PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "style-name", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
597 PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "background-color", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
598 PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
599 PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "width-policy", Toolkit::Control::Property::WIDTH_POLICY, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
600 PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "height-policy", Toolkit::Control::Property::HEIGHT_POLICY, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
601 PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "minimum-size", Toolkit::Control::Property::MINIMUM_SIZE, Property::VECTOR3, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
602 PropertyRegistration Control::Impl::PROPERTY_7( typeRegistration, "maximum-size", Toolkit::Control::Property::MAXIMUM_SIZE, Property::VECTOR3, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
603 PropertyRegistration Control::Impl::PROPERTY_8( typeRegistration, "key-input-focus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
605 Toolkit::Control Control::New()
607 // Create the implementation, temporarily owned on stack
608 IntrusivePtr<Control> controlImpl = new Control( CONTROL_BEHAVIOUR_NONE );
610 // Pass ownership to handle
611 Toolkit::Control handle( *controlImpl );
613 // Second-phase init of the implementation
614 // This can only be done after the CustomActor connection has been made...
615 controlImpl->Initialize();
625 void Control::SetSizePolicy( Toolkit::Control::SizePolicy widthPolicy, Toolkit::Control::SizePolicy heightPolicy )
627 bool relayoutRequest( false );
629 if ( ( mImpl->mWidthPolicy != widthPolicy ) || ( mImpl->mHeightPolicy != heightPolicy ) )
631 relayoutRequest = true;
634 mImpl->mWidthPolicy = widthPolicy;
635 mImpl->mHeightPolicy = heightPolicy;
637 // Ensure RelayoutRequest is called AFTER new policies have been set.
638 if ( relayoutRequest )
644 void Control::GetSizePolicy( Toolkit::Control::SizePolicy& widthPolicy, Toolkit::Control::SizePolicy& heightPolicy ) const
646 widthPolicy = mImpl->mWidthPolicy;
647 heightPolicy = mImpl->mHeightPolicy;
650 void Control::SetMinimumSize( const Vector3& size )
652 const Vector3& minSize = mImpl->GetMinimumSize();
653 if ( fabsf( minSize.width - size.width ) > Math::MACHINE_EPSILON_1000 ||
654 fabsf( minSize.height - size.height ) > Math::MACHINE_EPSILON_1000 )
656 mImpl->SetMinimumSize( size );
658 // Only relayout if our control is using the minimum or range policy.
659 if ( ( mImpl->mHeightPolicy == Toolkit::Control::Minimum ) || ( mImpl->mWidthPolicy == Toolkit::Control::Minimum ) ||
660 ( mImpl->mHeightPolicy == Toolkit::Control::Range ) || ( mImpl->mWidthPolicy == Toolkit::Control::Range ) )
667 const Vector3& Control::GetMinimumSize() const
669 return mImpl->GetMinimumSize();
672 void Control::SetMaximumSize( const Vector3& size )
674 const Vector3& maxSize = mImpl->GetMaximumSize();
675 if ( fabsf( maxSize.width - size.width ) > Math::MACHINE_EPSILON_1000 ||
676 fabsf( maxSize.height - size.height ) > Math::MACHINE_EPSILON_1000 )
678 mImpl->SetMaximumSize( size );
680 // Only relayout if our control is using the maximum or range policy.
681 if ( ( mImpl->mHeightPolicy == Toolkit::Control::Maximum ) || ( mImpl->mWidthPolicy == Toolkit::Control::Maximum ) ||
682 ( mImpl->mHeightPolicy == Toolkit::Control::Range ) || ( mImpl->mWidthPolicy == Toolkit::Control::Range ) )
689 const Vector3& Control::GetMaximumSize() const
691 return mImpl->GetMaximumSize();
694 Vector3 Control::GetNaturalSize()
696 // could be overridden in derived classes.
697 return mImpl->mNaturalSize;
700 float Control::GetHeightForWidth( float width )
702 // could be overridden in derived classes.
703 float height( 0.0f );
704 if ( mImpl->mNaturalSize.width > 0.0f )
706 height = mImpl->mNaturalSize.height * width / mImpl->mNaturalSize.width;
711 float Control::GetWidthForHeight( float height )
713 // could be overridden in derived classes.
715 if ( mImpl->mNaturalSize.height > 0.0f )
717 width = mImpl->mNaturalSize.width * height / mImpl->mNaturalSize.height;
722 const Vector3& Control::GetControlSize() const
724 return mImpl->mCurrentSize;
727 const Vector3& Control::GetSizeSet() const
729 return mImpl->mNaturalSize;
732 void Control::SetKeyInputFocus()
734 if( Self().OnStage() )
736 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
740 bool Control::HasKeyInputFocus()
743 if( Self().OnStage() )
745 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
750 void Control::ClearKeyInputFocus()
752 if( Self().OnStage() )
754 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
758 PinchGestureDetector Control::GetPinchGestureDetector() const
760 return mImpl->mPinchGestureDetector;
763 PanGestureDetector Control::GetPanGestureDetector() const
765 return mImpl->mPanGestureDetector;
768 TapGestureDetector Control::GetTapGestureDetector() const
770 return mImpl->mTapGestureDetector;
773 LongPressGestureDetector Control::GetLongPressGestureDetector() const
775 return mImpl->mLongPressGestureDetector;
778 void Control::SetStyleName( const std::string& styleName )
780 if( styleName != mImpl->mStyleName )
782 mImpl->mStyleName = styleName;
785 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
786 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
790 const std::string& Control::GetStyleName() const
792 return mImpl->mStyleName;
795 void Control::SetBackgroundColor( const Vector4& color )
797 Background& background( mImpl->GetBackground() );
799 if ( background.actor )
801 // Just set the actor color
802 background.actor.SetColor( color );
807 MeshActor meshActor = MeshActor::New( CreateMesh() );
809 SetupBackgroundActor( meshActor, Actor::Property::SCALE, color );
811 // Set the background actor before adding so that we do not inform deriving classes
812 background.actor = meshActor;
813 Self().Add( meshActor );
816 background.color = color;
819 Vector4 Control::GetBackgroundColor() const
821 if ( mImpl->mBackground )
823 return mImpl->mBackground->color;
825 return Color::TRANSPARENT;
828 void Control::SetBackground( Image image )
830 Background& background( mImpl->GetBackground() );
832 if ( background.actor )
834 // Remove Current actor, unset AFTER removal so that we do not inform deriving classes
835 Self().Remove( background.actor );
836 background.actor.Reset();
839 ImageActor imageActor = ImageActor::New( image );
840 SetupBackgroundActor( imageActor, Actor::Property::SIZE, background.color );
842 // Set the background actor before adding so that we do not inform derived classes
843 background.actor = imageActor;
844 Self().Add( imageActor );
847 void Control::ClearBackground()
849 if ( mImpl->mBackground )
851 Background& background( mImpl->GetBackground() );
852 Self().Remove( background.actor );
854 delete mImpl->mBackground;
855 mImpl->mBackground = NULL;
859 Actor Control::GetBackgroundActor() const
861 if ( mImpl->mBackground )
863 return mImpl->mBackground->actor;
869 void Control::SetKeyboardNavigationSupport(bool isSupported)
871 mImpl->mIsKeyboardNavigationSupported = isSupported;
874 bool Control::IsKeyboardNavigationSupported()
876 return mImpl->mIsKeyboardNavigationSupported;
879 void Control::Activate()
881 // Inform deriving classes
885 bool Control::OnAccessibilityPan(PanGesture gesture)
887 return false; // Accessibility pan gesture is not handled by default
890 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
892 return false; // Accessibility touch event is not handled by default
895 bool Control::OnAccessibilityValueChange(bool isIncrease)
897 return false; // Accessibility value change action is not handled by default
900 void Control::NegotiateSize( const Vector2& allocatedSize, ActorSizeContainer& container )
904 if ( mImpl->mWidthPolicy == Toolkit::Control::Fixed )
906 if ( mImpl->mHeightPolicy == Toolkit::Control::Fixed )
908 // If a control says it has a fixed size, then use the size set by the application / control.
909 Vector2 setSize( mImpl->mNaturalSize );
910 if ( setSize != Vector2::ZERO )
914 // Policy is set to Fixed, so if the application / control has not set one of the dimensions,
915 // then we should use the natural size of the control rather than the full allocation.
916 if ( EqualsZero( size.width ) )
918 size.width = GetWidthForHeight( size.height );
920 else if ( EqualsZero( size.height ) )
922 size.height = GetHeightForWidth( size.width );
927 // If that is not set then set the size to the control's natural size
928 size = Vector2( GetNaturalSize() );
933 // Width is fixed so if the application / control has set it, then use that.
934 if ( !EqualsZero( mImpl->mNaturalSize.width ) )
936 size.width = mImpl->mNaturalSize.width;
940 // Otherwise, set the width to what has been allocated.
941 size.width = allocatedSize.width;
944 // Height is flexible so ask control what the height should be for our width.
945 size.height = GetHeightForWidth( size.width );
947 // Ensure height is within our policy rules
948 size.height = Calculate( mImpl->mHeightPolicy, GetMinimumSize().height, GetMaximumSize().height, size.height );
953 if ( mImpl->mHeightPolicy == Toolkit::Control::Fixed )
955 // Height is fixed so if the application / control has set it, then use that.
956 if ( !EqualsZero( mImpl->mNaturalSize.height ) )
958 size.height = mImpl->mNaturalSize.height;
962 // Otherwise, set the height to what has been allocated.
963 size.height = allocatedSize.height;
966 // Width is flexible so ask control what the width should be for our height.
967 size.width = GetWidthForHeight( size.height );
969 // Ensure width is within our policy rules
970 size.width = Calculate( mImpl->mWidthPolicy, mImpl->GetMinimumSize().width, mImpl->GetMaximumSize().width, size.width );
974 // Width and height are BOTH flexible.
975 // Calculate the width and height using the policy rules.
976 size.width = Calculate( mImpl->mWidthPolicy, mImpl->GetMinimumSize().width, mImpl->GetMaximumSize().width, allocatedSize.width );
977 size.height = Calculate( mImpl->mHeightPolicy, mImpl->GetMinimumSize().height, mImpl->GetMaximumSize().height, allocatedSize.height );
981 // If the width has not been set, then set to the allocated width.
982 // Also if the width set is greater than the allocated, then set to allocated (no exceed support).
983 if ( EqualsZero( size.width ) || ( size.width > allocatedSize.width ) )
985 size.width = allocatedSize.width;
988 // If the height has not been set, then set to the allocated height.
989 // Also if the height set is greater than the allocated, then set to allocated (no exceed support).
990 if ( EqualsZero( size.height ) || ( size.height > allocatedSize.height ) )
992 size.height = allocatedSize.height;
995 DALI_LOG_INFO( gLogFilter, Debug::Verbose,
996 "%p: Natural: [%.2f, %.2f] Allocated: [%.2f, %.2f] Set: [%.2f, %.2f]\n",
997 Self().GetObjectPtr(),
998 GetNaturalSize().x, GetNaturalSize().y,
999 allocatedSize.x, allocatedSize.y,
1002 // Avoids relayout again when OnSizeSet callback arrives as a function of us or deriving class calling SetSize()
1003 mImpl->mInsideRelayout = true;
1004 Self().SetSize( size );
1005 // Only relayout controls which requested to be relaid out.
1006 OnRelayout( size, container );
1007 mImpl->mInsideRelayout = false;
1010 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
1012 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
1014 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
1015 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
1018 bool Control::IsKeyboardFocusGroup()
1020 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
1023 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
1028 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1032 bool Control::DoAction(BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes)
1036 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_CONTROL_ACTIVATED ) ) )
1038 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
1041 // if cast succeeds there is an implementation so no need to check
1042 control.GetImplementation().OnActivated();
1049 bool Control::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1051 Dali::BaseHandle handle( object );
1053 bool connected( false );
1054 Toolkit::Control control = Toolkit::Control::DownCast( handle );
1057 Control& controlImpl( control.GetImplementation() );
1060 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
1062 controlImpl.KeyEventSignal().Connect( tracker, functor );
1064 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
1066 controlImpl.EnableGestureDetection( Gesture::Tap );
1067 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
1069 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
1071 controlImpl.EnableGestureDetection( Gesture::Pan );
1072 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
1074 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
1076 controlImpl.EnableGestureDetection( Gesture::Pinch );
1077 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
1079 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
1081 controlImpl.EnableGestureDetection( Gesture::LongPress );
1082 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
1086 // signalName does not match any signal
1093 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
1095 return mImpl->mKeyEventSignal;
1098 bool Control::EmitKeyEventSignal( const KeyEvent& event )
1100 // Guard against destruction during signal emission
1101 Dali::Toolkit::Control handle( GetOwner() );
1103 bool consumed = false;
1105 // signals are allocated dynamically when someone connects
1106 if ( !mImpl->mKeyEventSignal.Empty() )
1108 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
1113 // Notification for derived classes
1114 consumed = OnKeyEvent(event);
1120 Control::Control( ControlBehaviour behaviourFlags )
1121 : CustomActorImpl( behaviourFlags & REQUIRES_TOUCH_EVENTS ),
1122 mImpl(new Impl(*this))
1124 mImpl->mFlags = behaviourFlags;
1127 void Control::Initialize()
1129 // Calling deriving classes
1132 if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
1134 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1136 // Register for style changes
1137 styleManager.StyleChangeSignal().Connect( this, &Control::OnStyleChange );
1140 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1143 SetRequiresHoverEvents(mImpl->mFlags & REQUIRES_HOVER_EVENTS);
1144 SetRequiresMouseWheelEvents(mImpl->mFlags & REQUIRES_MOUSE_WHEEL_EVENTS);
1146 mImpl->mInitialized = true;
1149 void Control::EnableGestureDetection(Gesture::Type type)
1151 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
1153 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
1154 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
1155 mImpl->mPinchGestureDetector.Attach(Self());
1158 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
1160 mImpl->mPanGestureDetector = PanGestureDetector::New();
1161 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
1162 mImpl->mPanGestureDetector.Attach(Self());
1165 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
1167 mImpl->mTapGestureDetector = TapGestureDetector::New();
1168 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
1169 mImpl->mTapGestureDetector.Attach(Self());
1172 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
1174 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
1175 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
1176 mImpl->mLongPressGestureDetector.Attach(Self());
1180 void Control::DisableGestureDetection(Gesture::Type type)
1182 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
1184 mImpl->mPinchGestureDetector.Detach(Self());
1185 mImpl->mPinchGestureDetector.Reset();
1188 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
1190 mImpl->mPanGestureDetector.Detach(Self());
1191 mImpl->mPanGestureDetector.Reset();
1194 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
1196 mImpl->mTapGestureDetector.Detach(Self());
1197 mImpl->mTapGestureDetector.Reset();
1200 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
1202 mImpl->mLongPressGestureDetector.Detach(Self());
1203 mImpl->mLongPressGestureDetector.Reset();
1207 void Control::RelayoutRequest()
1209 // unfortunate double negative but thats to guarantee new controls get size negotiation
1210 // by default and have to "opt-out" if they dont want it
1211 if( !(mImpl->mFlags & NO_SIZE_NEGOTIATION) )
1213 Internal::RelayoutController::Request();
1217 void Control::Relayout( Actor actor, const Vector2& size, ActorSizeContainer& container )
1221 Toolkit::Control control( Toolkit::Control::DownCast( actor ) );
1224 control.GetImplementation().NegotiateSize( size, container );
1228 container.push_back( ActorSizePair( actor, size ) );
1233 void Control::OnInitialize()
1237 void Control::OnActivated()
1241 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
1243 // By default the control is only interested in theme (not font) changes
1244 if( change.themeChange )
1246 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1250 void Control::OnPinch(const PinchGesture& pinch)
1252 if( !( mImpl->mStartingPinchScale ) )
1255 mImpl->mStartingPinchScale = new Vector3;
1258 if( pinch.state == Gesture::Started )
1260 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1263 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1266 void Control::OnPan( const PanGesture& pan )
1270 void Control::OnTap(const TapGesture& tap)
1274 void Control::OnLongPress( const LongPressGesture& longPress )
1278 void Control::OnControlStageConnection()
1282 void Control::OnControlStageDisconnection()
1286 void Control::OnControlChildAdd( Actor& child )
1290 void Control::OnControlChildRemove( Actor& child )
1294 void Control::OnControlSizeSet( const Vector3& size )
1298 void Control::OnRelayout( const Vector2& size, ActorSizeContainer& container )
1300 unsigned int numChildren = Self().GetChildCount();
1302 for( unsigned int i=0; i<numChildren; ++i )
1304 container.push_back( ActorSizePair( Self().GetChildAt(i), size ) );
1308 void Control::OnKeyInputFocusGained()
1313 void Control::OnKeyInputFocusLost()
1318 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1320 // @todo consider animating negotiated child sizes to target size
1323 bool Control::OnTouchEvent(const TouchEvent& event)
1325 return false; // Do not consume
1328 bool Control::OnHoverEvent(const HoverEvent& event)
1330 return false; // Do not consume
1333 bool Control::OnKeyEvent(const KeyEvent& event)
1335 return false; // Do not consume
1338 bool Control::OnMouseWheelEvent(const MouseWheelEvent& event)
1340 return false; // Do not consume
1343 void Control::OnStageConnection()
1347 // Notify derived classes.
1348 OnControlStageConnection();
1351 void Control::OnStageDisconnection()
1353 // Notify derived classes
1354 OnControlStageDisconnection();
1357 void Control::OnChildAdd(Actor& child)
1359 // If this is the background actor, then we do not want to relayout or inform deriving classes
1360 if ( mImpl->mBackground && ( child == mImpl->mBackground->actor ) )
1365 // Request for relayout as we may need to position the new child and old ones
1368 // Notify derived classes.
1369 OnControlChildAdd( child );
1372 void Control::OnChildRemove(Actor& child)
1374 // If this is the background actor, then we do not want to relayout or inform deriving classes
1375 if ( mImpl->mBackground && ( child == mImpl->mBackground->actor ) )
1380 // Request for relayout as we may need to re-position the old child
1383 // Notify derived classes.
1384 OnControlChildRemove( child );
1387 void Control::OnSizeSet(const Vector3& targetSize)
1389 if( ( !mImpl->mInsideRelayout ) && ( targetSize != mImpl->mNaturalSize ) )
1391 // Only updates size if set through Actor's API
1392 mImpl->mNaturalSize = targetSize;
1395 if( targetSize != mImpl->mCurrentSize )
1397 // Update control size.
1398 mImpl->mCurrentSize = targetSize;
1400 // Notify derived classes.
1401 OnControlSizeSet( targetSize );
1405 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1407 mImpl->SignalConnected( slotObserver, callback );
1410 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1412 mImpl->SignalDisconnected( slotObserver, callback );
1415 } // namespace Internal
1417 } // namespace Toolkit