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 ),
246 mStartingPinchScale( NULL ),
248 mPinchGestureDetector(),
249 mPanGestureDetector(),
250 mTapGestureDetector(),
251 mLongPressGestureDetector(),
254 mWidthPolicy( Toolkit::Control::Fixed ),
255 mHeightPolicy( Toolkit::Control::Fixed ),
256 mFlags( Control::CONTROL_BEHAVIOUR_NONE ),
257 mInsideRelayout( false ),
258 mIsKeyboardNavigationSupported( false ),
259 mIsKeyboardFocusGroup( false ),
260 mInitialized( false )
266 // All gesture detectors will be destroyed so no need to disconnect.
268 delete mStartingPinchScale;
271 // Gesture Detection Methods
273 void PinchDetected(Actor actor, const PinchGesture& pinch)
275 mControlImpl.OnPinch(pinch);
278 void PanDetected(Actor actor, const PanGesture& pan)
280 mControlImpl.OnPan(pan);
283 void TapDetected(Actor actor, const TapGesture& tap)
285 mControlImpl.OnTap(tap);
288 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
290 mControlImpl.OnLongPress(longPress);
293 // Background Methods
296 * Only creates an instance of the background if we actually use it.
297 * @return A reference to the Background structure.
299 Background& GetBackground()
303 mBackground = new Background;
311 * Called when a property of an object of this type is set.
312 * @param[in] object The object whose property is set.
313 * @param[in] index The property index.
314 * @param[in] value The new property value.
316 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
318 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
322 Control& controlImpl( control.GetImplementation() );
326 case Toolkit::Control::Property::BACKGROUND_COLOR:
328 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
332 case Toolkit::Control::Property::BACKGROUND:
334 if ( value.HasKey( "image" ) )
336 Property::Map imageMap = value.GetValue( "image" ).Get< Property::Map >();
337 Image image = Scripting::NewImage( imageMap );
341 controlImpl.SetBackground( image );
344 else if ( value.Get< Property::Map >().Empty() )
346 // An empty map means the background is no longer required
347 controlImpl.ClearBackground();
352 case Toolkit::Control::Property::WIDTH_POLICY:
354 controlImpl.mImpl->mWidthPolicy = Scripting::GetEnumeration< Toolkit::Control::SizePolicy >( value.Get< std::string >().c_str(), SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT );
358 case Toolkit::Control::Property::HEIGHT_POLICY:
360 controlImpl.mImpl->mHeightPolicy = Scripting::GetEnumeration< Toolkit::Control::SizePolicy >( value.Get< std::string >().c_str(), SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT );
364 case Toolkit::Control::Property::MINIMUM_SIZE:
366 controlImpl.SetMinimumSize( value.Get< Vector3 >() );
370 case Toolkit::Control::Property::MAXIMUM_SIZE:
372 controlImpl.SetMaximumSize( value.Get< Vector3 >() );
376 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
378 if ( value.Get< bool >() )
380 controlImpl.SetKeyInputFocus();
384 controlImpl.ClearKeyInputFocus();
393 * Called to retrieve a property of an object of this type.
394 * @param[in] object The object whose property is to be retrieved.
395 * @param[in] index The property index.
396 * @return The current value of the property.
398 static Property::Value GetProperty( BaseObject* object, Property::Index index )
400 Property::Value value;
402 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
406 Control& controlImpl( control.GetImplementation() );
410 case Toolkit::Control::Property::BACKGROUND_COLOR:
412 value = controlImpl.GetBackgroundColor();
416 case Toolkit::Control::Property::BACKGROUND:
420 Actor actor = controlImpl.GetBackgroundActor();
423 ImageActor imageActor = ImageActor::DownCast( actor );
426 Image image = imageActor.GetImage();
427 Property::Map imageMap;
428 Scripting::CreatePropertyMap( image, imageMap );
429 map[ "image" ] = imageMap;
437 case Toolkit::Control::Property::WIDTH_POLICY:
439 value = std::string( Scripting::GetEnumerationName< Toolkit::Control::SizePolicy >( controlImpl.mImpl->mWidthPolicy, SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT ) );
443 case Toolkit::Control::Property::HEIGHT_POLICY:
445 value = std::string( Scripting::GetEnumerationName< Toolkit::Control::SizePolicy >( controlImpl.mImpl->mHeightPolicy, SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT ) );
449 case Toolkit::Control::Property::MINIMUM_SIZE:
451 value = controlImpl.mImpl->GetMinimumSize();
455 case Toolkit::Control::Property::MAXIMUM_SIZE:
457 value = controlImpl.mImpl->GetMaximumSize();
461 case Toolkit::Control::Property::KEY_INPUT_FOCUS:
463 value = controlImpl.HasKeyInputFocus();
473 * Helper to get minimum size
474 * @return minimum size
476 inline const Vector3& GetMinimumSize()
478 if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
480 return mMinMaxSize[ MIN_SIZE_INDEX ];
484 // its not been allocated so its ZERO
485 return Vector3::ZERO;
489 * Helper to Set minimum size
492 inline void SetMinimumSize( const Vector3& size )
494 if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
496 mMinMaxSize[ MIN_SIZE_INDEX ] = size;
500 // its not been allocated so push the new value there
501 mMinMaxSize.PushBack( size );
506 * Helper to get maximum size
507 * @return maximum size
509 inline const Vector3& GetMaximumSize()
511 if( mMinMaxSize.Count() > MAX_SIZE_INDEX )
513 return mMinMaxSize[ MAX_SIZE_INDEX ];
517 // its not been allocated so its MAX_SIZE
523 * Helper to Set minimum size
526 inline void SetMaximumSize( const Vector3& size )
528 if( mMinMaxSize.Count() > MAX_SIZE_INDEX )
530 mMinMaxSize[ MAX_SIZE_INDEX ] = size;
532 else if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
534 // max has not been allocated, but min has
535 mMinMaxSize.PushBack( size );
539 // min and max both unallocated so allocate both
540 mMinMaxSize.Resize( 2u ); // this will reserve and default construct two Vector3s
541 mMinMaxSize[ MAX_SIZE_INDEX ] = size;
547 Control& mControlImpl;
548 Background* mBackground; ///< Only create the background if we use it
549 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
550 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
553 PinchGestureDetector mPinchGestureDetector;
554 PanGestureDetector mPanGestureDetector;
555 TapGestureDetector mTapGestureDetector;
556 LongPressGestureDetector mLongPressGestureDetector;
557 // @todo change all these to Vector2 when we have a chance to sanitize the public API as well
558 Vector3 mCurrentSize; ///< Stores the current control's size, this is the negotiated size
559 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.
560 Dali::Vector< Vector3 > mMinMaxSize; ///< Stores the minimum and maximum size if they are set
562 Toolkit::Control::SizePolicy mWidthPolicy :3; ///< Stores the width policy. 3 bits covers 8 values
563 Toolkit::Control::SizePolicy mHeightPolicy :3; ///< Stores the height policy. 3 bits covers 8 values
564 ControlBehaviour mFlags :6; ///< Flags passed in from constructor. Need to increase this size when new enums are added
565 bool mInsideRelayout:1; ///< Detect when were in Relayout
566 bool mIsKeyboardNavigationSupported:1; ///< Stores whether keyboard navigation is supported by the control.
567 bool mIsKeyboardFocusGroup:1; ///< Stores whether the control is a focus group.
570 // Properties - these need to be members of Internal::Control::Impl as they need to function within this class.
571 static PropertyRegistration PROPERTY_1;
572 static PropertyRegistration PROPERTY_2;
573 static PropertyRegistration PROPERTY_3;
574 static PropertyRegistration PROPERTY_4;
575 static PropertyRegistration PROPERTY_5;
576 static PropertyRegistration PROPERTY_6;
577 static PropertyRegistration PROPERTY_7;
580 // Properties registered without macro to use specific member variables.
581 PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "background-color", Toolkit::Control::Property::BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
582 PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "background", Toolkit::Control::Property::BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
583 PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "width-policy", Toolkit::Control::Property::WIDTH_POLICY, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
584 PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "height-policy", Toolkit::Control::Property::HEIGHT_POLICY, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
585 PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "minimum-size", Toolkit::Control::Property::MINIMUM_SIZE, Property::VECTOR3, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
586 PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "maximum-size", Toolkit::Control::Property::MAXIMUM_SIZE, Property::VECTOR3, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
587 PropertyRegistration Control::Impl::PROPERTY_7( typeRegistration, "key-input-focus", Toolkit::Control::Property::KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
589 Toolkit::Control Control::New()
591 // Create the implementation, temporarily owned on stack
592 IntrusivePtr<Control> controlImpl = new Control( CONTROL_BEHAVIOUR_NONE );
594 // Pass ownership to handle
595 Toolkit::Control handle( *controlImpl );
597 // Second-phase init of the implementation
598 // This can only be done after the CustomActor connection has been made...
599 controlImpl->Initialize();
609 void Control::SetSizePolicy( Toolkit::Control::SizePolicy widthPolicy, Toolkit::Control::SizePolicy heightPolicy )
611 bool relayoutRequest( false );
613 if ( ( mImpl->mWidthPolicy != widthPolicy ) || ( mImpl->mHeightPolicy != heightPolicy ) )
615 relayoutRequest = true;
618 mImpl->mWidthPolicy = widthPolicy;
619 mImpl->mHeightPolicy = heightPolicy;
621 // Ensure RelayoutRequest is called AFTER new policies have been set.
622 if ( relayoutRequest )
628 void Control::GetSizePolicy( Toolkit::Control::SizePolicy& widthPolicy, Toolkit::Control::SizePolicy& heightPolicy ) const
630 widthPolicy = mImpl->mWidthPolicy;
631 heightPolicy = mImpl->mHeightPolicy;
634 void Control::SetMinimumSize( const Vector3& size )
636 const Vector3& minSize = mImpl->GetMinimumSize();
637 if ( fabsf( minSize.width - size.width ) > Math::MACHINE_EPSILON_1000 ||
638 fabsf( minSize.height - size.height ) > Math::MACHINE_EPSILON_1000 )
640 mImpl->SetMinimumSize( size );
642 // Only relayout if our control is using the minimum or range policy.
643 if ( ( mImpl->mHeightPolicy == Toolkit::Control::Minimum ) || ( mImpl->mWidthPolicy == Toolkit::Control::Minimum ) ||
644 ( mImpl->mHeightPolicy == Toolkit::Control::Range ) || ( mImpl->mWidthPolicy == Toolkit::Control::Range ) )
651 const Vector3& Control::GetMinimumSize() const
653 return mImpl->GetMinimumSize();
656 void Control::SetMaximumSize( const Vector3& size )
658 const Vector3& maxSize = mImpl->GetMaximumSize();
659 if ( fabsf( maxSize.width - size.width ) > Math::MACHINE_EPSILON_1000 ||
660 fabsf( maxSize.height - size.height ) > Math::MACHINE_EPSILON_1000 )
662 mImpl->SetMaximumSize( size );
664 // Only relayout if our control is using the maximum or range policy.
665 if ( ( mImpl->mHeightPolicy == Toolkit::Control::Maximum ) || ( mImpl->mWidthPolicy == Toolkit::Control::Maximum ) ||
666 ( mImpl->mHeightPolicy == Toolkit::Control::Range ) || ( mImpl->mWidthPolicy == Toolkit::Control::Range ) )
673 const Vector3& Control::GetMaximumSize() const
675 return mImpl->GetMaximumSize();
678 Vector3 Control::GetNaturalSize()
680 // could be overridden in derived classes.
681 return mImpl->mNaturalSize;
684 float Control::GetHeightForWidth( float width )
686 // could be overridden in derived classes.
687 float height( 0.0f );
688 if ( mImpl->mNaturalSize.width > 0.0f )
690 height = mImpl->mNaturalSize.height * width / mImpl->mNaturalSize.width;
695 float Control::GetWidthForHeight( float height )
697 // could be overridden in derived classes.
699 if ( mImpl->mNaturalSize.height > 0.0f )
701 width = mImpl->mNaturalSize.width * height / mImpl->mNaturalSize.height;
706 const Vector3& Control::GetControlSize() const
708 return mImpl->mCurrentSize;
711 const Vector3& Control::GetSizeSet() const
713 return mImpl->mNaturalSize;
716 void Control::SetKeyInputFocus()
718 if( Self().OnStage() )
720 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
724 bool Control::HasKeyInputFocus()
727 if( Self().OnStage() )
729 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
734 void Control::ClearKeyInputFocus()
736 if( Self().OnStage() )
738 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
742 PinchGestureDetector Control::GetPinchGestureDetector() const
744 return mImpl->mPinchGestureDetector;
747 PanGestureDetector Control::GetPanGestureDetector() const
749 return mImpl->mPanGestureDetector;
752 TapGestureDetector Control::GetTapGestureDetector() const
754 return mImpl->mTapGestureDetector;
757 LongPressGestureDetector Control::GetLongPressGestureDetector() const
759 return mImpl->mLongPressGestureDetector;
762 void Control::SetBackgroundColor( const Vector4& color )
764 Background& background( mImpl->GetBackground() );
766 if ( background.actor )
768 // Just set the actor color
769 background.actor.SetColor( color );
774 MeshActor meshActor = MeshActor::New( CreateMesh() );
776 SetupBackgroundActor( meshActor, Actor::Property::SCALE, color );
778 // Set the background actor before adding so that we do not inform deriving classes
779 background.actor = meshActor;
780 Self().Add( meshActor );
783 background.color = color;
786 Vector4 Control::GetBackgroundColor() const
788 if ( mImpl->mBackground )
790 return mImpl->mBackground->color;
792 return Color::TRANSPARENT;
795 void Control::SetBackground( Image image )
797 Background& background( mImpl->GetBackground() );
799 if ( background.actor )
801 // Remove Current actor, unset AFTER removal so that we do not inform deriving classes
802 Self().Remove( background.actor );
803 background.actor.Reset();
806 ImageActor imageActor = ImageActor::New( image );
807 SetupBackgroundActor( imageActor, Actor::Property::SIZE, background.color );
809 // Set the background actor before adding so that we do not inform derived classes
810 background.actor = imageActor;
811 Self().Add( imageActor );
814 void Control::ClearBackground()
816 if ( mImpl->mBackground )
818 Background& background( mImpl->GetBackground() );
819 Self().Remove( background.actor );
821 delete mImpl->mBackground;
822 mImpl->mBackground = NULL;
826 Actor Control::GetBackgroundActor() const
828 if ( mImpl->mBackground )
830 return mImpl->mBackground->actor;
836 void Control::SetKeyboardNavigationSupport(bool isSupported)
838 mImpl->mIsKeyboardNavigationSupported = isSupported;
841 bool Control::IsKeyboardNavigationSupported()
843 return mImpl->mIsKeyboardNavigationSupported;
846 void Control::Activate()
848 // Inform deriving classes
852 bool Control::OnAccessibilityPan(PanGesture gesture)
854 return false; // Accessibility pan gesture is not handled by default
857 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
859 return false; // Accessibility touch event is not handled by default
862 bool Control::OnAccessibilityValueChange(bool isIncrease)
864 return false; // Accessibility value change action is not handled by default
867 void Control::NegotiateSize( const Vector2& allocatedSize, ActorSizeContainer& container )
871 if ( mImpl->mWidthPolicy == Toolkit::Control::Fixed )
873 if ( mImpl->mHeightPolicy == Toolkit::Control::Fixed )
875 // If a control says it has a fixed size, then use the size set by the application / control.
876 Vector2 setSize( mImpl->mNaturalSize );
877 if ( setSize != Vector2::ZERO )
881 // Policy is set to Fixed, so if the application / control has not set one of the dimensions,
882 // then we should use the natural size of the control rather than the full allocation.
883 if ( EqualsZero( size.width ) )
885 size.width = GetWidthForHeight( size.height );
887 else if ( EqualsZero( size.height ) )
889 size.height = GetHeightForWidth( size.width );
894 // If that is not set then set the size to the control's natural size
895 size = Vector2( GetNaturalSize() );
900 // Width is fixed so if the application / control has set it, then use that.
901 if ( !EqualsZero( mImpl->mNaturalSize.width ) )
903 size.width = mImpl->mNaturalSize.width;
907 // Otherwise, set the width to what has been allocated.
908 size.width = allocatedSize.width;
911 // Height is flexible so ask control what the height should be for our width.
912 size.height = GetHeightForWidth( size.width );
914 // Ensure height is within our policy rules
915 size.height = Calculate( mImpl->mHeightPolicy, GetMinimumSize().height, GetMaximumSize().height, size.height );
920 if ( mImpl->mHeightPolicy == Toolkit::Control::Fixed )
922 // Height is fixed so if the application / control has set it, then use that.
923 if ( !EqualsZero( mImpl->mNaturalSize.height ) )
925 size.height = mImpl->mNaturalSize.height;
929 // Otherwise, set the height to what has been allocated.
930 size.height = allocatedSize.height;
933 // Width is flexible so ask control what the width should be for our height.
934 size.width = GetWidthForHeight( size.height );
936 // Ensure width is within our policy rules
937 size.width = Calculate( mImpl->mWidthPolicy, mImpl->GetMinimumSize().width, mImpl->GetMaximumSize().width, size.width );
941 // Width and height are BOTH flexible.
942 // Calculate the width and height using the policy rules.
943 size.width = Calculate( mImpl->mWidthPolicy, mImpl->GetMinimumSize().width, mImpl->GetMaximumSize().width, allocatedSize.width );
944 size.height = Calculate( mImpl->mHeightPolicy, mImpl->GetMinimumSize().height, mImpl->GetMaximumSize().height, allocatedSize.height );
948 // If the width has not been set, then set to the allocated width.
949 // Also if the width set is greater than the allocated, then set to allocated (no exceed support).
950 if ( EqualsZero( size.width ) || ( size.width > allocatedSize.width ) )
952 size.width = allocatedSize.width;
955 // If the height has not been set, then set to the allocated height.
956 // Also if the height set is greater than the allocated, then set to allocated (no exceed support).
957 if ( EqualsZero( size.height ) || ( size.height > allocatedSize.height ) )
959 size.height = allocatedSize.height;
962 DALI_LOG_INFO( gLogFilter, Debug::Verbose,
963 "%p: Natural: [%.2f, %.2f] Allocated: [%.2f, %.2f] Set: [%.2f, %.2f]\n",
964 Self().GetObjectPtr(),
965 GetNaturalSize().x, GetNaturalSize().y,
966 allocatedSize.x, allocatedSize.y,
969 // Avoids relayout again when OnSizeSet callback arrives as a function of us or deriving class calling SetSize()
970 mImpl->mInsideRelayout = true;
971 Self().SetSize( size );
972 // Only relayout controls which requested to be relaid out.
973 OnRelayout( size, container );
974 mImpl->mInsideRelayout = false;
977 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
979 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
981 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
982 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
985 bool Control::IsKeyboardFocusGroup()
987 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
990 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
995 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
999 bool Control::DoAction(BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes)
1003 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_CONTROL_ACTIVATED ) ) )
1005 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
1008 // if cast succeeds there is an implementation so no need to check
1009 control.GetImplementation().OnActivated();
1016 bool Control::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1018 Dali::BaseHandle handle( object );
1020 bool connected( false );
1021 Toolkit::Control control = Toolkit::Control::DownCast( handle );
1024 Control& controlImpl( control.GetImplementation() );
1027 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
1029 controlImpl.KeyEventSignal().Connect( tracker, functor );
1031 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
1033 controlImpl.EnableGestureDetection( Gesture::Tap );
1034 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
1036 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
1038 controlImpl.EnableGestureDetection( Gesture::Pan );
1039 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
1041 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
1043 controlImpl.EnableGestureDetection( Gesture::Pinch );
1044 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
1046 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
1048 controlImpl.EnableGestureDetection( Gesture::LongPress );
1049 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
1053 // signalName does not match any signal
1060 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
1062 return mImpl->mKeyEventSignal;
1065 bool Control::EmitKeyEventSignal( const KeyEvent& event )
1067 // Guard against destruction during signal emission
1068 Dali::Toolkit::Control handle( GetOwner() );
1070 bool consumed = false;
1072 // signals are allocated dynamically when someone connects
1073 if ( !mImpl->mKeyEventSignal.Empty() )
1075 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
1080 // Notification for derived classes
1081 consumed = OnKeyEvent(event);
1087 Control::Control( ControlBehaviour behaviourFlags )
1088 : CustomActorImpl( behaviourFlags & REQUIRES_TOUCH_EVENTS ),
1089 mImpl(new Impl(*this))
1091 mImpl->mFlags = behaviourFlags;
1094 void Control::Initialize()
1097 // Calling deriving classes
1100 if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
1102 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1104 // Register for style changes
1105 styleManager.StyleChangeSignal().Connect( this, &Control::OnStyleChange );
1108 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1111 SetRequiresHoverEvents(mImpl->mFlags & REQUIRES_HOVER_EVENTS);
1112 SetRequiresMouseWheelEvents(mImpl->mFlags & REQUIRES_MOUSE_WHEEL_EVENTS);
1114 mImpl->mInitialized = true;
1117 void Control::EnableGestureDetection(Gesture::Type type)
1119 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
1121 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
1122 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
1123 mImpl->mPinchGestureDetector.Attach(Self());
1126 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
1128 mImpl->mPanGestureDetector = PanGestureDetector::New();
1129 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
1130 mImpl->mPanGestureDetector.Attach(Self());
1133 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
1135 mImpl->mTapGestureDetector = TapGestureDetector::New();
1136 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
1137 mImpl->mTapGestureDetector.Attach(Self());
1140 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
1142 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
1143 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
1144 mImpl->mLongPressGestureDetector.Attach(Self());
1148 void Control::DisableGestureDetection(Gesture::Type type)
1150 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
1152 mImpl->mPinchGestureDetector.Detach(Self());
1153 mImpl->mPinchGestureDetector.Reset();
1156 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
1158 mImpl->mPanGestureDetector.Detach(Self());
1159 mImpl->mPanGestureDetector.Reset();
1162 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
1164 mImpl->mTapGestureDetector.Detach(Self());
1165 mImpl->mTapGestureDetector.Reset();
1168 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
1170 mImpl->mLongPressGestureDetector.Detach(Self());
1171 mImpl->mLongPressGestureDetector.Reset();
1175 void Control::RelayoutRequest()
1177 // unfortunate double negative but thats to guarantee new controls get size negotiation
1178 // by default and have to "opt-out" if they dont want it
1179 if( !(mImpl->mFlags & NO_SIZE_NEGOTIATION) )
1181 Internal::RelayoutController::Request();
1185 void Control::Relayout( Actor actor, const Vector2& size, ActorSizeContainer& container )
1189 Toolkit::Control control( Toolkit::Control::DownCast( actor ) );
1192 control.GetImplementation().NegotiateSize( size, container );
1196 container.push_back( ActorSizePair( actor, size ) );
1201 void Control::OnInitialize()
1205 void Control::OnActivated()
1209 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
1211 // By default the control is only interested in theme (not font) changes
1212 if( change.themeChange )
1214 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1218 void Control::OnPinch(const PinchGesture& pinch)
1220 if( !( mImpl->mStartingPinchScale ) )
1223 mImpl->mStartingPinchScale = new Vector3;
1226 if( pinch.state == Gesture::Started )
1228 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1231 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1234 void Control::OnPan( const PanGesture& pan )
1238 void Control::OnTap(const TapGesture& tap)
1242 void Control::OnLongPress( const LongPressGesture& longPress )
1246 void Control::OnControlStageConnection()
1250 void Control::OnControlStageDisconnection()
1254 void Control::OnControlChildAdd( Actor& child )
1258 void Control::OnControlChildRemove( Actor& child )
1262 void Control::OnControlSizeSet( const Vector3& size )
1266 void Control::OnRelayout( const Vector2& size, ActorSizeContainer& container )
1268 unsigned int numChildren = Self().GetChildCount();
1270 for( unsigned int i=0; i<numChildren; ++i )
1272 container.push_back( ActorSizePair( Self().GetChildAt(i), size ) );
1276 void Control::OnKeyInputFocusGained()
1281 void Control::OnKeyInputFocusLost()
1286 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1288 // @todo consider animating negotiated child sizes to target size
1291 bool Control::OnTouchEvent(const TouchEvent& event)
1293 return false; // Do not consume
1296 bool Control::OnHoverEvent(const HoverEvent& event)
1298 return false; // Do not consume
1301 bool Control::OnKeyEvent(const KeyEvent& event)
1303 return false; // Do not consume
1306 bool Control::OnMouseWheelEvent(const MouseWheelEvent& event)
1308 return false; // Do not consume
1311 void Control::OnStageConnection()
1315 // Notify derived classes.
1316 OnControlStageConnection();
1319 void Control::OnStageDisconnection()
1321 // Notify derived classes
1322 OnControlStageDisconnection();
1325 void Control::OnChildAdd(Actor& child)
1327 // If this is the background actor, then we do not want to relayout or inform deriving classes
1328 if ( mImpl->mBackground && ( child == mImpl->mBackground->actor ) )
1333 // Request for relayout as we may need to position the new child and old ones
1336 // Notify derived classes.
1337 OnControlChildAdd( child );
1340 void Control::OnChildRemove(Actor& child)
1342 // If this is the background actor, then we do not want to relayout or inform deriving classes
1343 if ( mImpl->mBackground && ( child == mImpl->mBackground->actor ) )
1348 // Request for relayout as we may need to re-position the old child
1351 // Notify derived classes.
1352 OnControlChildRemove( child );
1355 void Control::OnSizeSet(const Vector3& targetSize)
1357 if( ( !mImpl->mInsideRelayout ) && ( targetSize != mImpl->mNaturalSize ) )
1359 // Only updates size if set through Actor's API
1360 mImpl->mNaturalSize = targetSize;
1363 if( targetSize != mImpl->mCurrentSize )
1365 // Update control size.
1366 mImpl->mCurrentSize = targetSize;
1368 // Notify derived classes.
1369 OnControlSizeSet( targetSize );
1373 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1375 mImpl->SignalConnected( slotObserver, callback );
1378 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1380 mImpl->SignalDisconnected( slotObserver, callback );
1383 } // namespace Internal
1385 } // namespace Toolkit