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/animation/active-constraint.h>
25 #include <dali/public-api/animation/constraint.h>
26 #include <dali/public-api/animation/constraints.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/public-api/scripting/scripting.h>
29 #include <dali/integration-api/debug.h>
32 #include <dali-toolkit/internal/controls/relayout-controller.h>
33 #include <dali-toolkit/internal/controls/relayout-helper.h>
34 #include <dali-toolkit/public-api/focus-manager/keyinput-focus-manager.h>
35 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/public-api/styling/style-manager.h>
38 #include <dali-toolkit/internal/styling/style-manager-impl.h>
46 const Property::Index Control::PROPERTY_BACKGROUND_COLOR = Internal::Control::CONTROL_PROPERTY_START_INDEX;
47 const Property::Index Control::PROPERTY_BACKGROUND = Internal::Control::CONTROL_PROPERTY_START_INDEX + 1;
48 const Property::Index Control::PROPERTY_WIDTH_POLICY = Internal::Control::CONTROL_PROPERTY_START_INDEX + 2;
49 const Property::Index Control::PROPERTY_HEIGHT_POLICY = Internal::Control::CONTROL_PROPERTY_START_INDEX + 3;
50 const Property::Index Control::PROPERTY_MINIMUM_SIZE = Internal::Control::CONTROL_PROPERTY_START_INDEX + 4;
51 const Property::Index Control::PROPERTY_MAXIMUM_SIZE = Internal::Control::CONTROL_PROPERTY_START_INDEX + 5;
52 const Property::Index Control::PROPERTY_KEY_INPUT_FOCUS = Internal::Control::CONTROL_PROPERTY_START_INDEX + 6;
59 const char* const SIGNAL_KEY_EVENT = "key-event";
60 const char* const SIGNAL_TAPPED = "tapped";
61 const char* const SIGNAL_PANNED = "panned";
62 const char* const SIGNAL_PINCHED = "pinched";
63 const char* const SIGNAL_LONG_PRESSED = "long-pressed";
67 const char* const ACTION_CONTROL_ACTIVATED = "control-activated";
69 const Scripting::StringEnum< Control::SizePolicy > SIZE_POLICY_STRING_TABLE[] =
71 { "FIXED", Control::Fixed },
72 { "MINIMUM", Control::Minimum },
73 { "MAXIMUM", Control::Maximum },
74 { "RANGE", Control::Range },
75 { "FLEXIBLE", Control::Flexible },
77 const unsigned int SIZE_POLICY_STRING_TABLE_COUNT = sizeof( SIZE_POLICY_STRING_TABLE ) / sizeof( SIZE_POLICY_STRING_TABLE[0] );
79 #if defined(DEBUG_ENABLED)
80 Integration::Log::Filter* gLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_CONTROL");
83 const float MAX_FLOAT_VALUE( std::numeric_limits<float>::max() );
84 const Vector3 MAX_SIZE( MAX_FLOAT_VALUE, MAX_FLOAT_VALUE, MAX_FLOAT_VALUE );
85 const float BACKGROUND_ACTOR_Z_POSITION( -0.1f );
89 return Internal::Control::New();
92 TypeRegistration CONTROL_TYPE( typeid(Control), typeid(CustomActor), Create );
94 // Property Registration after Internal::Control::Impl definition below
96 TypeAction ACTION_TYPE_1( CONTROL_TYPE, ACTION_CONTROL_ACTIVATED, &Internal::Control::DoAction );
98 SignalConnectorType SIGNAL_CONNECTOR_1( CONTROL_TYPE, SIGNAL_KEY_EVENT, &Internal::Control::DoConnectSignal );
99 SignalConnectorType SIGNAL_CONNECTOR_2( CONTROL_TYPE, SIGNAL_TAPPED, &Internal::Control::DoConnectSignal );
100 SignalConnectorType SIGNAL_CONNECTOR_3( CONTROL_TYPE, SIGNAL_PANNED, &Internal::Control::DoConnectSignal );
101 SignalConnectorType SIGNAL_CONNECTOR_4( CONTROL_TYPE, SIGNAL_PINCHED, &Internal::Control::DoConnectSignal );
102 SignalConnectorType SIGNAL_CONNECTOR_5( CONTROL_TYPE, SIGNAL_LONG_PRESSED, &Internal::Control::DoConnectSignal );
105 * Structure which holds information about the background of a control
109 Actor actor; ///< Either a MeshActor or an ImageActor
110 Vector4 color; ///< The color of the actor.
117 color( Color::WHITE )
123 * Helper function to calculate a dimension given the policy of that dimension; the minimum &
124 * maximum values that dimension can be; and the allocated value for that dimension.
126 * @param[in] policy The size policy for that dimension.
127 * @param[in] minimum The minimum value that dimension can be.
128 * @param[in] maximum The maximum value that dimension can be.
129 * @param[in] allocated The value allocated for that dimension.
131 * @return The value that the dimension should be.
133 * @note This does not handle Control::Fixed policy.
135 float Calculate( Control::SizePolicy policy, float minimum, float maximum, float allocated )
137 float size( allocated );
143 // Use allocated value
147 case Control::Minimum:
149 // Size is always at least the minimum.
150 size = std::max( allocated, minimum );
154 case Control::Maximum:
156 // Size can grow but up to a maximum value.
157 size = std::min( allocated, maximum );
163 // Size is at least the minimum and can grow up to the maximum
164 size = std::max( size, minimum );
165 size = std::min( size, maximum );
169 case Control::Flexible:
171 // Size grows or shrinks with no limits.
178 DALI_ASSERT_DEBUG( false && "This function was not intended to be used by any other policy." );
188 * Sets all the required properties for the background actor.
190 * @param[in] actor The actor to set the properties on.
191 * @param[in] constrainingIndex The property index to constrain the parent's size on.
192 * @param[in] color The required color of the actor.
194 void SetupBackgroundActor( Actor actor, Property::Index constrainingIndex, const Vector4& color )
196 actor.SetColor( color );
197 actor.SetPositionInheritanceMode( USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
198 actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
199 actor.SetZ( BACKGROUND_ACTOR_Z_POSITION );
201 Constraint constraint = Constraint::New<Vector3>( constrainingIndex,
202 ParentSource( Actor::Property::Size ),
203 EqualToConstraint() );
204 actor.ApplyConstraint( constraint );
207 } // unnamed namespace
212 class Control::Impl : public ConnectionTracker
217 * Size indices for mMinMaxSize array
226 // Construction & Destruction
227 Impl(Control& controlImpl)
228 : mControlImpl( controlImpl ),
230 mStartingPinchScale( NULL ),
232 mPinchGestureDetector(),
233 mPanGestureDetector(),
234 mTapGestureDetector(),
235 mLongPressGestureDetector(),
238 mWidthPolicy( Toolkit::Control::Fixed ),
239 mHeightPolicy( Toolkit::Control::Fixed ),
240 mFlags( Control::CONTROL_BEHAVIOUR_NONE ),
241 mInsideRelayout( false ),
242 mIsKeyboardNavigationSupported( false ),
243 mIsKeyboardFocusGroup( false ),
244 mInitialized( false )
250 // All gesture detectors will be destroyed so no need to disconnect.
252 delete mStartingPinchScale;
255 // Gesture Detection Methods
257 void PinchDetected(Actor actor, const PinchGesture& pinch)
259 mControlImpl.OnPinch(pinch);
262 void PanDetected(Actor actor, const PanGesture& pan)
264 mControlImpl.OnPan(pan);
267 void TapDetected(Actor actor, const TapGesture& tap)
269 mControlImpl.OnTap(tap);
272 void LongPressDetected(Actor actor, const LongPressGesture& longPress)
274 mControlImpl.OnLongPress(longPress);
277 // Background Methods
280 * Only creates an instance of the background if we actually use it.
281 * @return A reference to the Background structure.
283 Background& GetBackground()
287 mBackground = new Background;
295 * Called when a property of an object of this type is set.
296 * @param[in] object The object whose property is set.
297 * @param[in] index The property index.
298 * @param[in] value The new property value.
300 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
302 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
306 Control& controlImpl( control.GetImplementation() );
310 case Toolkit::Control::PROPERTY_BACKGROUND_COLOR:
312 controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
316 case Toolkit::Control::PROPERTY_BACKGROUND:
318 if ( value.HasKey( "image" ) )
320 Property::Map imageMap = value.GetValue( "image" ).Get< Property::Map >();
321 Image image = Scripting::NewImage( imageMap );
325 controlImpl.SetBackground( image );
328 else if ( value.Get< Property::Map >().Empty() )
330 // An empty map means the background is no longer required
331 controlImpl.ClearBackground();
336 case Toolkit::Control::PROPERTY_WIDTH_POLICY:
338 controlImpl.mImpl->mWidthPolicy = Scripting::GetEnumeration< Toolkit::Control::SizePolicy >( value.Get< std::string >().c_str(), SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT );
342 case Toolkit::Control::PROPERTY_HEIGHT_POLICY:
344 controlImpl.mImpl->mHeightPolicy = Scripting::GetEnumeration< Toolkit::Control::SizePolicy >( value.Get< std::string >().c_str(), SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT );
348 case Toolkit::Control::PROPERTY_MINIMUM_SIZE:
350 controlImpl.SetMinimumSize( value.Get< Vector3 >() );
354 case Toolkit::Control::PROPERTY_MAXIMUM_SIZE:
356 controlImpl.SetMaximumSize( value.Get< Vector3 >() );
360 case Toolkit::Control::PROPERTY_KEY_INPUT_FOCUS:
362 if ( value.Get< bool >() )
364 controlImpl.SetKeyInputFocus();
368 controlImpl.ClearKeyInputFocus();
377 * Called to retrieve a property of an object of this type.
378 * @param[in] object The object whose property is to be retrieved.
379 * @param[in] index The property index.
380 * @return The current value of the property.
382 static Property::Value GetProperty( BaseObject* object, Property::Index index )
384 Property::Value value;
386 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
390 Control& controlImpl( control.GetImplementation() );
394 case Toolkit::Control::PROPERTY_BACKGROUND_COLOR:
396 value = controlImpl.GetBackgroundColor();
400 case Toolkit::Control::PROPERTY_BACKGROUND:
404 Actor actor = controlImpl.GetBackgroundActor();
407 ImageActor imageActor = ImageActor::DownCast( actor );
410 Image image = imageActor.GetImage();
411 Property::Map imageMap;
412 Scripting::CreatePropertyMap( image, imageMap );
413 map[ "image" ] = imageMap;
421 case Toolkit::Control::PROPERTY_WIDTH_POLICY:
423 value = std::string( Scripting::GetEnumerationName< Toolkit::Control::SizePolicy >( controlImpl.mImpl->mWidthPolicy, SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT ) );
427 case Toolkit::Control::PROPERTY_HEIGHT_POLICY:
429 value = std::string( Scripting::GetEnumerationName< Toolkit::Control::SizePolicy >( controlImpl.mImpl->mHeightPolicy, SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT ) );
433 case Toolkit::Control::PROPERTY_MINIMUM_SIZE:
435 value = controlImpl.mImpl->GetMinimumSize();
439 case Toolkit::Control::PROPERTY_MAXIMUM_SIZE:
441 value = controlImpl.mImpl->GetMaximumSize();
445 case Toolkit::Control::PROPERTY_KEY_INPUT_FOCUS:
447 value = controlImpl.HasKeyInputFocus();
457 * Helper to get minimum size
458 * @return minimum size
460 inline const Vector3& GetMinimumSize()
462 if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
464 return mMinMaxSize[ MIN_SIZE_INDEX ];
468 // its not been allocated so its ZERO
469 return Vector3::ZERO;
473 * Helper to Set minimum size
476 inline void SetMinimumSize( const Vector3& size )
478 if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
480 mMinMaxSize[ MIN_SIZE_INDEX ] = size;
484 // its not been allocated so push the new value there
485 mMinMaxSize.PushBack( size );
490 * Helper to get maximum size
491 * @return maximum size
493 inline const Vector3& GetMaximumSize()
495 if( mMinMaxSize.Count() > MAX_SIZE_INDEX )
497 return mMinMaxSize[ MAX_SIZE_INDEX ];
501 // its not been allocated so its MAX_SIZE
507 * Helper to Set minimum size
510 inline void SetMaximumSize( const Vector3& size )
512 if( mMinMaxSize.Count() > MAX_SIZE_INDEX )
514 mMinMaxSize[ MAX_SIZE_INDEX ] = size;
516 else if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
518 // max has not been allocated, but min has
519 mMinMaxSize.PushBack( size );
523 // min and max both unallocated so allocate both
524 mMinMaxSize.Resize( 2u ); // this will reserve and default construct two Vector3s
525 mMinMaxSize[ MAX_SIZE_INDEX ] = size;
531 Control& mControlImpl;
532 Background* mBackground; ///< Only create the background if we use it
533 Vector3* mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
534 Toolkit::Control::KeyEventSignalType mKeyEventSignal;
537 PinchGestureDetector mPinchGestureDetector;
538 PanGestureDetector mPanGestureDetector;
539 TapGestureDetector mTapGestureDetector;
540 LongPressGestureDetector mLongPressGestureDetector;
541 // @todo change all these to Vector2 when we have a chance to sanitize the public API as well
542 Vector3 mCurrentSize; ///< Stores the current control's size, this is the negotiated size
543 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.
544 Dali::Vector< Vector3 > mMinMaxSize; ///< Stores the minimum and maximum size if they are set
546 Toolkit::Control::SizePolicy mWidthPolicy :3; ///< Stores the width policy. 3 bits covers 8 values
547 Toolkit::Control::SizePolicy mHeightPolicy :3; ///< Stores the height policy. 3 bits covers 8 values
548 ControlBehaviour mFlags :6; ///< Flags passed in from constructor. Need to increase this size when new enums are added
549 bool mInsideRelayout:1; ///< Detect when were in Relayout
550 bool mIsKeyboardNavigationSupported:1; ///< Stores whether keyboard navigation is supported by the control.
551 bool mIsKeyboardFocusGroup:1; ///< Stores whether the control is a focus group.
554 // Properties - these need to be members of Internal::Control::Impl as they need to functions within this class.
555 static PropertyRegistration PROPERTY_1;
556 static PropertyRegistration PROPERTY_2;
557 static PropertyRegistration PROPERTY_3;
558 static PropertyRegistration PROPERTY_4;
559 static PropertyRegistration PROPERTY_5;
560 static PropertyRegistration PROPERTY_6;
561 static PropertyRegistration PROPERTY_7;
564 PropertyRegistration Control::Impl::PROPERTY_1( CONTROL_TYPE, "background-color", Toolkit::Control::PROPERTY_BACKGROUND_COLOR, Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
565 PropertyRegistration Control::Impl::PROPERTY_2( CONTROL_TYPE, "background", Toolkit::Control::PROPERTY_BACKGROUND, Property::MAP, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
566 PropertyRegistration Control::Impl::PROPERTY_3( CONTROL_TYPE, "width-policy", Toolkit::Control::PROPERTY_WIDTH_POLICY, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
567 PropertyRegistration Control::Impl::PROPERTY_4( CONTROL_TYPE, "height-policy", Toolkit::Control::PROPERTY_HEIGHT_POLICY, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
568 PropertyRegistration Control::Impl::PROPERTY_5( CONTROL_TYPE, "minimum-size", Toolkit::Control::PROPERTY_MINIMUM_SIZE, Property::VECTOR3, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
569 PropertyRegistration Control::Impl::PROPERTY_6( CONTROL_TYPE, "maximum-size", Toolkit::Control::PROPERTY_MAXIMUM_SIZE, Property::VECTOR3, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
570 PropertyRegistration Control::Impl::PROPERTY_7( CONTROL_TYPE, "key-input-focus", Toolkit::Control::PROPERTY_KEY_INPUT_FOCUS, Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
572 Toolkit::Control Control::New()
574 // Create the implementation, temporarily owned on stack
575 IntrusivePtr<Control> controlImpl = new Control( CONTROL_BEHAVIOUR_NONE );
577 // Pass ownership to handle
578 Toolkit::Control handle( *controlImpl );
580 // Second-phase init of the implementation
581 // This can only be done after the CustomActor connection has been made...
582 controlImpl->Initialize();
592 void Control::SetSizePolicy( Toolkit::Control::SizePolicy widthPolicy, Toolkit::Control::SizePolicy heightPolicy )
594 bool relayoutRequest( false );
596 if ( ( mImpl->mWidthPolicy != widthPolicy ) || ( mImpl->mHeightPolicy != heightPolicy ) )
598 relayoutRequest = true;
601 mImpl->mWidthPolicy = widthPolicy;
602 mImpl->mHeightPolicy = heightPolicy;
604 // Ensure RelayoutRequest is called AFTER new policies have been set.
605 if ( relayoutRequest )
611 void Control::GetSizePolicy( Toolkit::Control::SizePolicy& widthPolicy, Toolkit::Control::SizePolicy& heightPolicy ) const
613 widthPolicy = mImpl->mWidthPolicy;
614 heightPolicy = mImpl->mHeightPolicy;
617 void Control::SetMinimumSize( const Vector3& size )
619 const Vector3& minSize = mImpl->GetMinimumSize();
620 if ( fabsf( minSize.width - size.width ) > Math::MACHINE_EPSILON_1000 ||
621 fabsf( minSize.height - size.height ) > Math::MACHINE_EPSILON_1000 )
623 mImpl->SetMinimumSize( size );
625 // Only relayout if our control is using the minimum or range policy.
626 if ( ( mImpl->mHeightPolicy == Toolkit::Control::Minimum ) || ( mImpl->mWidthPolicy == Toolkit::Control::Minimum ) ||
627 ( mImpl->mHeightPolicy == Toolkit::Control::Range ) || ( mImpl->mWidthPolicy == Toolkit::Control::Range ) )
634 const Vector3& Control::GetMinimumSize() const
636 return mImpl->GetMinimumSize();
639 void Control::SetMaximumSize( const Vector3& size )
641 const Vector3& maxSize = mImpl->GetMaximumSize();
642 if ( fabsf( maxSize.width - size.width ) > Math::MACHINE_EPSILON_1000 ||
643 fabsf( maxSize.height - size.height ) > Math::MACHINE_EPSILON_1000 )
645 mImpl->SetMaximumSize( size );
647 // Only relayout if our control is using the maximum or range policy.
648 if ( ( mImpl->mHeightPolicy == Toolkit::Control::Maximum ) || ( mImpl->mWidthPolicy == Toolkit::Control::Maximum ) ||
649 ( mImpl->mHeightPolicy == Toolkit::Control::Range ) || ( mImpl->mWidthPolicy == Toolkit::Control::Range ) )
656 const Vector3& Control::GetMaximumSize() const
658 return mImpl->GetMaximumSize();
661 Vector3 Control::GetNaturalSize()
663 // could be overridden in derived classes.
664 return mImpl->mNaturalSize;
667 float Control::GetHeightForWidth( float width )
669 // could be overridden in derived classes.
670 float height( 0.0f );
671 if ( mImpl->mNaturalSize.width > 0.0f )
673 height = mImpl->mNaturalSize.height * width / mImpl->mNaturalSize.width;
678 float Control::GetWidthForHeight( float height )
680 // could be overridden in derived classes.
682 if ( mImpl->mNaturalSize.height > 0.0f )
684 width = mImpl->mNaturalSize.width * height / mImpl->mNaturalSize.height;
689 const Vector3& Control::GetControlSize() const
691 return mImpl->mCurrentSize;
694 const Vector3& Control::GetSizeSet() const
696 return mImpl->mNaturalSize;
699 void Control::SetKeyInputFocus()
701 if( Self().OnStage() )
703 Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
707 bool Control::HasKeyInputFocus()
710 if( Self().OnStage() )
712 result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
717 void Control::ClearKeyInputFocus()
719 if( Self().OnStage() )
721 Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
725 PinchGestureDetector Control::GetPinchGestureDetector() const
727 return mImpl->mPinchGestureDetector;
730 PanGestureDetector Control::GetPanGestureDetector() const
732 return mImpl->mPanGestureDetector;
735 TapGestureDetector Control::GetTapGestureDetector() const
737 return mImpl->mTapGestureDetector;
740 LongPressGestureDetector Control::GetLongPressGestureDetector() const
742 return mImpl->mLongPressGestureDetector;
745 void Control::SetBackgroundColor( const Vector4& color )
747 Background& background( mImpl->GetBackground() );
749 if ( background.actor )
751 // Just set the actor color
752 background.actor.SetColor( color );
755 background.color = color;
758 Vector4 Control::GetBackgroundColor() const
760 if ( mImpl->mBackground )
762 return mImpl->mBackground->color;
764 return Color::TRANSPARENT;
767 void Control::SetBackground( Image image )
769 Background& background( mImpl->GetBackground() );
771 if ( background.actor )
773 // Remove Current actor, unset AFTER removal so that we do not inform deriving classes
774 Self().Remove( background.actor );
775 background.actor.Reset();
778 ImageActor imageActor = ImageActor::New( image );
779 SetupBackgroundActor( imageActor, Actor::Property::Size, background.color );
781 // Set the background actor before adding so that we do not inform derived classes
782 background.actor = imageActor;
783 Self().Add( imageActor );
786 void Control::ClearBackground()
788 if ( mImpl->mBackground )
790 Background& background( mImpl->GetBackground() );
791 Self().Remove( background.actor );
793 delete mImpl->mBackground;
794 mImpl->mBackground = NULL;
798 Actor Control::GetBackgroundActor() const
800 if ( mImpl->mBackground )
802 return mImpl->mBackground->actor;
808 void Control::SetKeyboardNavigationSupport(bool isSupported)
810 mImpl->mIsKeyboardNavigationSupported = isSupported;
813 bool Control::IsKeyboardNavigationSupported()
815 return mImpl->mIsKeyboardNavigationSupported;
818 void Control::Activate()
820 // Inform deriving classes
824 bool Control::OnAccessibilityPan(PanGesture gesture)
826 return false; // Accessibility pan gesture is not handled by default
829 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
831 return false; // Accessibility touch event is not handled by default
834 bool Control::OnAccessibilityValueChange(bool isIncrease)
836 return false; // Accessibility value change action is not handled by default
839 void Control::NegotiateSize( const Vector2& allocatedSize, ActorSizeContainer& container )
843 if ( mImpl->mWidthPolicy == Toolkit::Control::Fixed )
845 if ( mImpl->mHeightPolicy == Toolkit::Control::Fixed )
847 // If a control says it has a fixed size, then use the size set by the application / control.
848 Vector2 setSize( mImpl->mNaturalSize );
849 if ( setSize != Vector2::ZERO )
853 // Policy is set to Fixed, so if the application / control has not set one of the dimensions,
854 // then we should use the natural size of the control rather than the full allocation.
855 if ( EqualsZero( size.width ) )
857 size.width = GetWidthForHeight( size.height );
859 else if ( EqualsZero( size.height ) )
861 size.height = GetHeightForWidth( size.width );
866 // If that is not set then set the size to the control's natural size
867 size = Vector2( GetNaturalSize() );
872 // Width is fixed so if the application / control has set it, then use that.
873 if ( !EqualsZero( mImpl->mNaturalSize.width ) )
875 size.width = mImpl->mNaturalSize.width;
879 // Otherwise, set the width to what has been allocated.
880 size.width = allocatedSize.width;
883 // Height is flexible so ask control what the height should be for our width.
884 size.height = GetHeightForWidth( size.width );
886 // Ensure height is within our policy rules
887 size.height = Calculate( mImpl->mHeightPolicy, GetMinimumSize().height, GetMaximumSize().height, size.height );
892 if ( mImpl->mHeightPolicy == Toolkit::Control::Fixed )
894 // Height is fixed so if the application / control has set it, then use that.
895 if ( !EqualsZero( mImpl->mNaturalSize.height ) )
897 size.height = mImpl->mNaturalSize.height;
901 // Otherwise, set the height to what has been allocated.
902 size.height = allocatedSize.height;
905 // Width is flexible so ask control what the width should be for our height.
906 size.width = GetWidthForHeight( size.height );
908 // Ensure width is within our policy rules
909 size.width = Calculate( mImpl->mWidthPolicy, mImpl->GetMinimumSize().width, mImpl->GetMaximumSize().width, size.width );
913 // Width and height are BOTH flexible.
914 // Calculate the width and height using the policy rules.
915 size.width = Calculate( mImpl->mWidthPolicy, mImpl->GetMinimumSize().width, mImpl->GetMaximumSize().width, allocatedSize.width );
916 size.height = Calculate( mImpl->mHeightPolicy, mImpl->GetMinimumSize().height, mImpl->GetMaximumSize().height, allocatedSize.height );
920 // If the width has not been set, then set to the allocated width.
921 // Also if the width set is greater than the allocated, then set to allocated (no exceed support).
922 if ( EqualsZero( size.width ) || ( size.width > allocatedSize.width ) )
924 size.width = allocatedSize.width;
927 // If the height has not been set, then set to the allocated height.
928 // Also if the height set is greater than the allocated, then set to allocated (no exceed support).
929 if ( EqualsZero( size.height ) || ( size.height > allocatedSize.height ) )
931 size.height = allocatedSize.height;
934 DALI_LOG_INFO( gLogFilter, Debug::Verbose,
935 "%p: Natural: [%.2f, %.2f] Allocated: [%.2f, %.2f] Set: [%.2f, %.2f]\n",
936 Self().GetObjectPtr(),
937 GetNaturalSize().x, GetNaturalSize().y,
938 allocatedSize.x, allocatedSize.y,
941 // Avoids relayout again when OnSizeSet callback arrives as a function of us or deriving class calling SetSize()
942 mImpl->mInsideRelayout = true;
943 Self().SetSize( size );
944 // Only relayout controls which requested to be relaid out.
945 OnRelayout( size, container );
946 mImpl->mInsideRelayout = false;
949 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
951 mImpl->mIsKeyboardFocusGroup = isFocusGroup;
953 // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
954 Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
957 bool Control::IsKeyboardFocusGroup()
959 return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
962 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
967 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
971 bool Control::DoAction(BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes)
975 if( object && ( 0 == strcmp( actionName.c_str(), ACTION_CONTROL_ACTIVATED ) ) )
977 Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
980 // if cast succeeds there is an implementation so no need to check
981 control.GetImplementation().OnActivated();
988 bool Control::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
990 Dali::BaseHandle handle( object );
992 bool connected( false );
993 Toolkit::Control control = Toolkit::Control::DownCast( handle );
996 Control& controlImpl( control.GetImplementation() );
999 if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
1001 controlImpl.KeyEventSignal().Connect( tracker, functor );
1003 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
1005 controlImpl.EnableGestureDetection( Gesture::Tap );
1006 controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
1008 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
1010 controlImpl.EnableGestureDetection( Gesture::Pan );
1011 controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
1013 else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
1015 controlImpl.EnableGestureDetection( Gesture::Pinch );
1016 controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
1018 else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
1020 controlImpl.EnableGestureDetection( Gesture::LongPress );
1021 controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
1025 // signalName does not match any signal
1032 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
1034 return mImpl->mKeyEventSignal;
1037 bool Control::EmitKeyEventSignal( const KeyEvent& event )
1039 // Guard against destruction during signal emission
1040 Dali::Toolkit::Control handle( GetOwner() );
1042 bool consumed = false;
1044 // signals are allocated dynamically when someone connects
1045 if ( !mImpl->mKeyEventSignal.Empty() )
1047 consumed = mImpl->mKeyEventSignal.Emit( handle, event );
1052 // Notification for derived classes
1053 consumed = OnKeyEvent(event);
1059 Control::Control( ControlBehaviour behaviourFlags )
1060 : CustomActorImpl( behaviourFlags & REQUIRES_TOUCH_EVENTS ),
1061 mImpl(new Impl(*this))
1063 mImpl->mFlags = behaviourFlags;
1066 void Control::Initialize()
1069 // Calling deriving classes
1072 if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
1074 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1076 // Register for style changes
1077 styleManager.StyleChangeSignal().Connect( this, &Control::DoStyleChange );
1080 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1083 SetRequiresHoverEvents(mImpl->mFlags & REQUIRES_HOVER_EVENTS);
1084 SetRequiresMouseWheelEvents(mImpl->mFlags & REQUIRES_MOUSE_WHEEL_EVENTS);
1086 mImpl->mInitialized = true;
1089 void Control::EnableGestureDetection(Gesture::Type type)
1091 if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
1093 mImpl->mPinchGestureDetector = PinchGestureDetector::New();
1094 mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
1095 mImpl->mPinchGestureDetector.Attach(Self());
1098 if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
1100 mImpl->mPanGestureDetector = PanGestureDetector::New();
1101 mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
1102 mImpl->mPanGestureDetector.Attach(Self());
1105 if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
1107 mImpl->mTapGestureDetector = TapGestureDetector::New();
1108 mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
1109 mImpl->mTapGestureDetector.Attach(Self());
1112 if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
1114 mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
1115 mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
1116 mImpl->mLongPressGestureDetector.Attach(Self());
1120 void Control::DisableGestureDetection(Gesture::Type type)
1122 if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
1124 mImpl->mPinchGestureDetector.Detach(Self());
1125 mImpl->mPinchGestureDetector.Reset();
1128 if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
1130 mImpl->mPanGestureDetector.Detach(Self());
1131 mImpl->mPanGestureDetector.Reset();
1134 if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
1136 mImpl->mTapGestureDetector.Detach(Self());
1137 mImpl->mTapGestureDetector.Reset();
1140 if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
1142 mImpl->mLongPressGestureDetector.Detach(Self());
1143 mImpl->mLongPressGestureDetector.Reset();
1147 void Control::RelayoutRequest()
1149 // unfortunate double negative but thats to guarantee new controls get size negotiation
1150 // by default and have to "opt-out" if they dont want it
1151 if( !(mImpl->mFlags & NO_SIZE_NEGOTIATION) )
1153 Internal::RelayoutController::Request();
1157 void Control::Relayout( Actor actor, const Vector2& size, ActorSizeContainer& container )
1161 Toolkit::Control control( Toolkit::Control::DownCast( actor ) );
1164 control.GetImplementation().NegotiateSize( size, container );
1168 container.push_back( ActorSizePair( actor, size ) );
1173 void Control::OnInitialize()
1177 void Control::OnActivated()
1181 void Control::OnThemeChange( Toolkit::StyleManager styleManager )
1183 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1186 void Control::OnFontChange( bool defaultFontChange, bool defaultFontSizeChange )
1190 void Control::OnPinch(const PinchGesture& pinch)
1192 if( !( mImpl->mStartingPinchScale ) )
1195 mImpl->mStartingPinchScale = new Vector3;
1198 if( pinch.state == Gesture::Started )
1200 *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1203 Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1206 void Control::OnPan( const PanGesture& pan )
1210 void Control::OnTap(const TapGesture& tap)
1214 void Control::OnLongPress( const LongPressGesture& longPress )
1218 void Control::OnControlStageConnection()
1222 void Control::OnControlStageDisconnection()
1226 void Control::OnControlChildAdd( Actor& child )
1230 void Control::OnControlChildRemove( Actor& child )
1234 void Control::OnControlSizeSet( const Vector3& size )
1238 void Control::OnRelayout( const Vector2& size, ActorSizeContainer& container )
1240 unsigned int numChildren = Self().GetChildCount();
1242 for( unsigned int i=0; i<numChildren; ++i )
1244 container.push_back( ActorSizePair( Self().GetChildAt(i), size ) );
1248 void Control::OnKeyInputFocusGained()
1253 void Control::OnKeyInputFocusLost()
1258 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1260 // @todo consider animating negotiated child sizes to target size
1263 bool Control::OnTouchEvent(const TouchEvent& event)
1265 return false; // Do not consume
1268 bool Control::OnHoverEvent(const HoverEvent& event)
1270 return false; // Do not consume
1273 bool Control::OnKeyEvent(const KeyEvent& event)
1275 return false; // Do not consume
1278 bool Control::OnMouseWheelEvent(const MouseWheelEvent& event)
1280 return false; // Do not consume
1283 Actor Control::GetChildByAlias(const std::string& actorAlias)
1288 void Control::OnStageConnection()
1292 // Notify derived classes.
1293 OnControlStageConnection();
1296 void Control::OnStageDisconnection()
1298 // Notify derived classes
1299 OnControlStageDisconnection();
1302 void Control::OnChildAdd(Actor& child)
1304 // If this is the background actor, then we do not want to relayout or inform deriving classes
1305 if ( mImpl->mBackground && ( child == mImpl->mBackground->actor ) )
1310 // Request for relayout as we may need to position the new child and old ones
1313 // Notify derived classes.
1314 OnControlChildAdd( child );
1317 void Control::OnChildRemove(Actor& child)
1319 // If this is the background actor, then we do not want to relayout or inform deriving classes
1320 if ( mImpl->mBackground && ( child == mImpl->mBackground->actor ) )
1325 // Request for relayout as we may need to re-position the old child
1328 // Notify derived classes.
1329 OnControlChildRemove( child );
1332 void Control::OnSizeSet(const Vector3& targetSize)
1334 if( ( !mImpl->mInsideRelayout ) && ( targetSize != mImpl->mNaturalSize ) )
1336 // Only updates size if set through Actor's API
1337 mImpl->mNaturalSize = targetSize;
1340 if( targetSize != mImpl->mCurrentSize )
1342 // Update control size.
1343 mImpl->mCurrentSize = targetSize;
1345 // Notify derived classes.
1346 OnControlSizeSet( targetSize );
1350 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1352 mImpl->SignalConnected( slotObserver, callback );
1355 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1357 mImpl->SignalDisconnected( slotObserver, callback );
1360 void Control::DoStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
1362 if( change.themeChange )
1364 OnThemeChange( styleManager );
1366 else if( change.defaultFontChange || change.defaultFontSizeChange )
1368 OnFontChange( change.defaultFontChange, change.defaultFontSizeChange );
1372 } // namespace Internal
1374 } // namespace Toolkit