Remove mesh from text input.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / control-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/public-api/controls/control-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <stack>
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>
30
31 // INTERNAL INCLUDES
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>
39
40 namespace Dali
41 {
42
43 namespace Toolkit
44 {
45
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;
53
54 namespace
55 {
56
57 // Signals
58
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";
64
65 // Actions
66
67 const char* const ACTION_CONTROL_ACTIVATED = "control-activated";
68
69 const Scripting::StringEnum< Control::SizePolicy > SIZE_POLICY_STRING_TABLE[] =
70 {
71   { "FIXED",      Control::Fixed      },
72   { "MINIMUM",    Control::Minimum    },
73   { "MAXIMUM",    Control::Maximum    },
74   { "RANGE",      Control::Range      },
75   { "FLEXIBLE",   Control::Flexible   },
76 };
77 const unsigned int SIZE_POLICY_STRING_TABLE_COUNT = sizeof( SIZE_POLICY_STRING_TABLE ) / sizeof( SIZE_POLICY_STRING_TABLE[0] );
78
79 #if defined(DEBUG_ENABLED)
80 Integration::Log::Filter* gLogFilter  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_CONTROL");
81 #endif
82
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 );
86
87 BaseHandle Create()
88 {
89   return Internal::Control::New();
90 }
91
92 TypeRegistration CONTROL_TYPE( typeid(Control), typeid(CustomActor), Create );
93
94 // Property Registration after Internal::Control::Impl definition below
95
96 TypeAction ACTION_TYPE_1( CONTROL_TYPE, ACTION_CONTROL_ACTIVATED, &Internal::Control::DoAction );
97
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 );
103
104 /**
105  * Structure which holds information about the background of a control
106  */
107 struct Background
108 {
109   Actor actor;   ///< Either a MeshActor or an ImageActor
110   Vector4 color; ///< The color of the actor.
111
112   /**
113    * Constructor
114    */
115   Background()
116   : actor(),
117     color( Color::WHITE )
118   {
119   }
120 };
121
122 /**
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.
125  *
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.
130  *
131  * @return The value that the dimension should be.
132  *
133  * @note This does not handle Control::Fixed policy.
134  */
135 float Calculate( Control::SizePolicy policy, float minimum, float maximum, float allocated )
136 {
137   float size( allocated );
138
139   switch( policy )
140   {
141     case Control::Fixed:
142     {
143       // Use allocated value
144       break;
145     }
146
147     case Control::Minimum:
148     {
149       // Size is always at least the minimum.
150       size = std::max( allocated, minimum );
151       break;
152     }
153
154     case Control::Maximum:
155     {
156       // Size can grow but up to a maximum value.
157       size = std::min( allocated, maximum );
158       break;
159     }
160
161     case Control::Range:
162     {
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 );
166      break;
167     }
168
169     case Control::Flexible:
170     {
171       // Size grows or shrinks with no limits.
172       size = allocated;
173       break;
174     }
175
176     default:
177     {
178       DALI_ASSERT_DEBUG( false && "This function was not intended to be used by any other policy." );
179       break;
180     }
181   }
182
183   return size;
184 }
185
186
187 /**
188  * Sets all the required properties for the background actor.
189  *
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.
193  */
194 void SetupBackgroundActor( Actor actor, Property::Index constrainingIndex, const Vector4& color )
195 {
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 );
200
201   Constraint constraint = Constraint::New<Vector3>( constrainingIndex,
202                                                     ParentSource( Actor::Property::Size ),
203                                                     EqualToConstraint() );
204   actor.ApplyConstraint( constraint );
205 }
206
207 } // unnamed namespace
208
209 namespace Internal
210 {
211
212 class Control::Impl : public ConnectionTracker
213 {
214 public:
215
216   /**
217    * Size indices for mMinMaxSize array
218    */
219   enum
220   {
221     MIN_SIZE_INDEX = 0,
222     MAX_SIZE_INDEX = 1
223   };
224
225 public:
226   // Construction & Destruction
227   Impl(Control& controlImpl)
228   : mControlImpl( controlImpl ),
229     mBackground( NULL ),
230     mStartingPinchScale( NULL ),
231     mKeyEventSignal(),
232     mPinchGestureDetector(),
233     mPanGestureDetector(),
234     mTapGestureDetector(),
235     mLongPressGestureDetector(),
236     mCurrentSize(),
237     mNaturalSize(),
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 )
245   {
246   }
247
248   ~Impl()
249   {
250     // All gesture detectors will be destroyed so no need to disconnect.
251     delete mBackground;
252     delete mStartingPinchScale;
253   }
254
255   // Gesture Detection Methods
256
257   void PinchDetected(Actor actor, const PinchGesture& pinch)
258   {
259     mControlImpl.OnPinch(pinch);
260   }
261
262   void PanDetected(Actor actor, const PanGesture& pan)
263   {
264     mControlImpl.OnPan(pan);
265   }
266
267   void TapDetected(Actor actor, const TapGesture& tap)
268   {
269     mControlImpl.OnTap(tap);
270   }
271
272   void LongPressDetected(Actor actor, const LongPressGesture& longPress)
273   {
274     mControlImpl.OnLongPress(longPress);
275   }
276
277   // Background Methods
278
279   /**
280    * Only creates an instance of the background if we actually use it.
281    * @return A reference to the Background structure.
282    */
283   Background& GetBackground()
284   {
285     if ( !mBackground )
286     {
287       mBackground = new Background;
288     }
289     return *mBackground;
290   }
291
292   // Properties
293
294   /**
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.
299    */
300   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
301   {
302     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
303
304     if ( control )
305     {
306       Control& controlImpl( control.GetImplementation() );
307
308       switch ( index )
309       {
310         case Toolkit::Control::PROPERTY_BACKGROUND_COLOR:
311         {
312           controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
313           break;
314         }
315
316         case Toolkit::Control::PROPERTY_BACKGROUND:
317         {
318           if ( value.HasKey( "image" ) )
319           {
320             Property::Map imageMap = value.GetValue( "image" ).Get< Property::Map >();
321             Image image = Scripting::NewImage( imageMap );
322
323             if ( image )
324             {
325               controlImpl.SetBackground( image );
326             }
327           }
328           else if ( value.Get< Property::Map >().Empty() )
329           {
330             // An empty map means the background is no longer required
331             controlImpl.ClearBackground();
332           }
333           break;
334         }
335
336         case Toolkit::Control::PROPERTY_WIDTH_POLICY:
337         {
338           controlImpl.mImpl->mWidthPolicy = Scripting::GetEnumeration< Toolkit::Control::SizePolicy >( value.Get< std::string >().c_str(), SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT );
339           break;
340         }
341
342         case Toolkit::Control::PROPERTY_HEIGHT_POLICY:
343         {
344           controlImpl.mImpl->mHeightPolicy = Scripting::GetEnumeration< Toolkit::Control::SizePolicy >( value.Get< std::string >().c_str(), SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT );
345           break;
346         }
347
348         case Toolkit::Control::PROPERTY_MINIMUM_SIZE:
349         {
350           controlImpl.SetMinimumSize( value.Get< Vector3 >() );
351           break;
352         }
353
354         case Toolkit::Control::PROPERTY_MAXIMUM_SIZE:
355         {
356           controlImpl.SetMaximumSize( value.Get< Vector3 >() );
357           break;
358         }
359
360         case Toolkit::Control::PROPERTY_KEY_INPUT_FOCUS:
361         {
362           if ( value.Get< bool >() )
363           {
364             controlImpl.SetKeyInputFocus();
365           }
366           else
367           {
368             controlImpl.ClearKeyInputFocus();
369           }
370           break;
371         }
372       }
373     }
374   }
375
376   /**
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.
381    */
382   static Property::Value GetProperty( BaseObject* object, Property::Index index )
383   {
384     Property::Value value;
385
386     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
387
388     if ( control )
389     {
390       Control& controlImpl( control.GetImplementation() );
391
392       switch ( index )
393       {
394         case Toolkit::Control::PROPERTY_BACKGROUND_COLOR:
395         {
396           value = controlImpl.GetBackgroundColor();
397           break;
398         }
399
400         case Toolkit::Control::PROPERTY_BACKGROUND:
401         {
402           Property::Map map;
403
404           Actor actor = controlImpl.GetBackgroundActor();
405           if ( actor )
406           {
407             ImageActor imageActor = ImageActor::DownCast( actor );
408             if ( imageActor )
409             {
410               Image image = imageActor.GetImage();
411               Property::Map imageMap;
412               Scripting::CreatePropertyMap( image, imageMap );
413               map[ "image" ] = imageMap;
414             }
415           }
416
417           value = map;
418           break;
419         }
420
421         case Toolkit::Control::PROPERTY_WIDTH_POLICY:
422         {
423           value = std::string( Scripting::GetEnumerationName< Toolkit::Control::SizePolicy >( controlImpl.mImpl->mWidthPolicy, SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT ) );
424           break;
425         }
426
427         case Toolkit::Control::PROPERTY_HEIGHT_POLICY:
428         {
429           value = std::string( Scripting::GetEnumerationName< Toolkit::Control::SizePolicy >( controlImpl.mImpl->mHeightPolicy, SIZE_POLICY_STRING_TABLE, SIZE_POLICY_STRING_TABLE_COUNT ) );
430           break;
431         }
432
433         case Toolkit::Control::PROPERTY_MINIMUM_SIZE:
434         {
435           value = controlImpl.mImpl->GetMinimumSize();
436           break;
437         }
438
439         case Toolkit::Control::PROPERTY_MAXIMUM_SIZE:
440         {
441           value = controlImpl.mImpl->GetMaximumSize();
442           break;
443         }
444
445         case Toolkit::Control::PROPERTY_KEY_INPUT_FOCUS:
446         {
447           value = controlImpl.HasKeyInputFocus();
448           break;
449         }
450       }
451     }
452
453     return value;
454   }
455
456   /**
457    * Helper to get minimum size
458    * @return minimum size
459    */
460   inline const Vector3& GetMinimumSize()
461   {
462     if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
463     {
464       return mMinMaxSize[ MIN_SIZE_INDEX ];
465     }
466     else
467     {
468       // its not been allocated so its ZERO
469       return Vector3::ZERO;
470     }
471   }
472   /**
473    * Helper to Set minimum size
474    * @param size to set
475    */
476   inline void SetMinimumSize( const Vector3& size )
477   {
478     if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
479     {
480       mMinMaxSize[ MIN_SIZE_INDEX ] = size;
481     }
482     else
483     {
484       // its not been allocated so push the new value there
485       mMinMaxSize.PushBack( size );
486     }
487   }
488
489   /**
490    * Helper to get maximum size
491    * @return maximum size
492    */
493   inline const Vector3& GetMaximumSize()
494   {
495     if( mMinMaxSize.Count() > MAX_SIZE_INDEX )
496     {
497       return mMinMaxSize[ MAX_SIZE_INDEX ];
498     }
499     else
500     {
501       // its not been allocated so its MAX_SIZE
502       return MAX_SIZE;
503     }
504   }
505
506   /**
507    * Helper to Set minimum size
508    * @param size to set
509    */
510   inline void SetMaximumSize( const Vector3& size )
511   {
512     if( mMinMaxSize.Count() > MAX_SIZE_INDEX )
513     {
514       mMinMaxSize[ MAX_SIZE_INDEX ] = size;
515     }
516     else if( mMinMaxSize.Count() > MIN_SIZE_INDEX )
517     {
518       // max has not been allocated, but min has
519       mMinMaxSize.PushBack( size );
520     }
521     else
522     {
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;
526     }
527   }
528
529   // Data
530
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;
535
536   // Gesture Detection
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
545
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.
552   bool mInitialized:1;
553
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;
562 };
563
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 );
571
572 Toolkit::Control Control::New()
573 {
574   // Create the implementation, temporarily owned on stack
575   IntrusivePtr<Control> controlImpl = new Control( CONTROL_BEHAVIOUR_NONE );
576
577   // Pass ownership to handle
578   Toolkit::Control handle( *controlImpl );
579
580   // Second-phase init of the implementation
581   // This can only be done after the CustomActor connection has been made...
582   controlImpl->Initialize();
583
584   return handle;
585 }
586
587 Control::~Control()
588 {
589   delete mImpl;
590 }
591
592 void Control::SetSizePolicy( Toolkit::Control::SizePolicy widthPolicy, Toolkit::Control::SizePolicy heightPolicy )
593 {
594   bool relayoutRequest( false );
595
596   if ( ( mImpl->mWidthPolicy != widthPolicy ) || ( mImpl->mHeightPolicy != heightPolicy ) )
597   {
598     relayoutRequest = true;
599   }
600
601   mImpl->mWidthPolicy = widthPolicy;
602   mImpl->mHeightPolicy = heightPolicy;
603
604   // Ensure RelayoutRequest is called AFTER new policies have been set.
605   if ( relayoutRequest )
606   {
607     RelayoutRequest();
608   }
609 }
610
611 void Control::GetSizePolicy( Toolkit::Control::SizePolicy& widthPolicy, Toolkit::Control::SizePolicy& heightPolicy ) const
612 {
613   widthPolicy = mImpl->mWidthPolicy;
614   heightPolicy = mImpl->mHeightPolicy;
615 }
616
617 void Control::SetMinimumSize( const Vector3& size )
618 {
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 )
622   {
623     mImpl->SetMinimumSize( size );
624
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   ) )
628     {
629       RelayoutRequest();
630     }
631   }
632 }
633
634 const Vector3& Control::GetMinimumSize() const
635 {
636   return mImpl->GetMinimumSize();
637 }
638
639 void Control::SetMaximumSize( const Vector3& size )
640 {
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 )
644   {
645     mImpl->SetMaximumSize( size );
646
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   ) )
650     {
651       RelayoutRequest();
652     }
653   }
654 }
655
656 const Vector3& Control::GetMaximumSize() const
657 {
658   return mImpl->GetMaximumSize();
659 }
660
661 Vector3 Control::GetNaturalSize()
662 {
663   // could be overridden in derived classes.
664   return mImpl->mNaturalSize;
665 }
666
667 float Control::GetHeightForWidth( float width )
668 {
669   // could be overridden in derived classes.
670   float height( 0.0f );
671   if ( mImpl->mNaturalSize.width > 0.0f )
672   {
673     height = mImpl->mNaturalSize.height * width / mImpl->mNaturalSize.width;
674   }
675   return height;
676 }
677
678 float Control::GetWidthForHeight( float height )
679 {
680   // could be overridden in derived classes.
681   float width( 0.0f );
682   if ( mImpl->mNaturalSize.height > 0.0f )
683   {
684     width = mImpl->mNaturalSize.width * height / mImpl->mNaturalSize.height;
685   }
686   return width;
687 }
688
689 const Vector3& Control::GetControlSize() const
690 {
691   return mImpl->mCurrentSize;
692 }
693
694 const Vector3& Control::GetSizeSet() const
695 {
696   return mImpl->mNaturalSize;
697 }
698
699 void Control::SetKeyInputFocus()
700 {
701   if( Self().OnStage() )
702   {
703     Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
704   }
705 }
706
707 bool Control::HasKeyInputFocus()
708 {
709   bool result = false;
710   if( Self().OnStage() )
711   {
712     result = Toolkit::KeyInputFocusManager::Get().IsKeyboardListener(Toolkit::Control::DownCast(Self()));
713   }
714   return result;
715 }
716
717 void Control::ClearKeyInputFocus()
718 {
719   if( Self().OnStage() )
720   {
721     Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
722   }
723 }
724
725 PinchGestureDetector Control::GetPinchGestureDetector() const
726 {
727   return mImpl->mPinchGestureDetector;
728 }
729
730 PanGestureDetector Control::GetPanGestureDetector() const
731 {
732   return mImpl->mPanGestureDetector;
733 }
734
735 TapGestureDetector Control::GetTapGestureDetector() const
736 {
737   return mImpl->mTapGestureDetector;
738 }
739
740 LongPressGestureDetector Control::GetLongPressGestureDetector() const
741 {
742   return mImpl->mLongPressGestureDetector;
743 }
744
745 void Control::SetBackgroundColor( const Vector4& color )
746 {
747   Background& background( mImpl->GetBackground() );
748
749   if ( background.actor )
750   {
751     // Just set the actor color
752     background.actor.SetColor( color );
753   }
754
755   background.color = color;
756 }
757
758 Vector4 Control::GetBackgroundColor() const
759 {
760   if ( mImpl->mBackground )
761   {
762     return mImpl->mBackground->color;
763   }
764   return Color::TRANSPARENT;
765 }
766
767 void Control::SetBackground( Image image )
768 {
769   Background& background( mImpl->GetBackground() );
770
771   if ( background.actor )
772   {
773     // Remove Current actor, unset AFTER removal so that we do not inform deriving classes
774     Self().Remove( background.actor );
775     background.actor.Reset();
776   }
777
778   ImageActor imageActor = ImageActor::New( image );
779   SetupBackgroundActor( imageActor, Actor::Property::Size, background.color );
780
781   // Set the background actor before adding so that we do not inform derived classes
782   background.actor = imageActor;
783   Self().Add( imageActor );
784 }
785
786 void Control::ClearBackground()
787 {
788   if ( mImpl->mBackground )
789   {
790     Background& background( mImpl->GetBackground() );
791     Self().Remove( background.actor );
792
793     delete mImpl->mBackground;
794     mImpl->mBackground = NULL;
795   }
796 }
797
798 Actor Control::GetBackgroundActor() const
799 {
800   if ( mImpl->mBackground )
801   {
802     return mImpl->mBackground->actor;
803   }
804
805   return Actor();
806 }
807
808 void Control::SetKeyboardNavigationSupport(bool isSupported)
809 {
810   mImpl->mIsKeyboardNavigationSupported = isSupported;
811 }
812
813 bool Control::IsKeyboardNavigationSupported()
814 {
815   return mImpl->mIsKeyboardNavigationSupported;
816 }
817
818 void Control::Activate()
819 {
820   // Inform deriving classes
821   OnActivated();
822 }
823
824 bool Control::OnAccessibilityPan(PanGesture gesture)
825 {
826   return false; // Accessibility pan gesture is not handled by default
827 }
828
829 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
830 {
831   return false; // Accessibility touch event is not handled by default
832 }
833
834 bool Control::OnAccessibilityValueChange(bool isIncrease)
835 {
836   return false; // Accessibility value change action is not handled by default
837 }
838
839 void Control::NegotiateSize( const Vector2& allocatedSize, ActorSizeContainer& container )
840 {
841   Vector2 size;
842
843   if ( mImpl->mWidthPolicy == Toolkit::Control::Fixed )
844   {
845     if ( mImpl->mHeightPolicy == Toolkit::Control::Fixed )
846     {
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 )
850       {
851         size = setSize;
852
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 ) )
856         {
857           size.width = GetWidthForHeight( size.height );
858         }
859         else if ( EqualsZero( size.height ) )
860         {
861           size.height = GetHeightForWidth( size.width );
862         }
863       }
864       else
865       {
866         // If that is not set then set the size to the control's natural size
867         size = Vector2( GetNaturalSize() );
868       }
869     }
870     else
871     {
872       // Width is fixed so if the application / control has set it, then use that.
873       if ( !EqualsZero( mImpl->mNaturalSize.width ) )
874       {
875         size.width = mImpl->mNaturalSize.width;
876       }
877       else
878       {
879         // Otherwise, set the width to what has been allocated.
880         size.width = allocatedSize.width;
881       }
882
883       // Height is flexible so ask control what the height should be for our width.
884       size.height = GetHeightForWidth( size.width );
885
886       // Ensure height is within our policy rules
887       size.height = Calculate( mImpl->mHeightPolicy, GetMinimumSize().height, GetMaximumSize().height, size.height );
888     }
889   }
890   else
891   {
892     if ( mImpl->mHeightPolicy == Toolkit::Control::Fixed )
893     {
894       // Height is fixed so if the application / control has set it, then use that.
895       if ( !EqualsZero( mImpl->mNaturalSize.height ) )
896       {
897         size.height = mImpl->mNaturalSize.height;
898       }
899       else
900       {
901         // Otherwise, set the height to what has been allocated.
902         size.height = allocatedSize.height;
903       }
904
905       // Width is flexible so ask control what the width should be for our height.
906       size.width = GetWidthForHeight( size.height );
907
908       // Ensure width is within our policy rules
909       size.width = Calculate( mImpl->mWidthPolicy, mImpl->GetMinimumSize().width, mImpl->GetMaximumSize().width, size.width );
910     }
911     else
912     {
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 );
917     }
918   }
919
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 ) )
923   {
924     size.width = allocatedSize.width;
925   }
926
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 ) )
930   {
931     size.height = allocatedSize.height;
932   }
933
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,
939                  size.x, size.y );
940
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;
947 }
948
949 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
950 {
951   mImpl->mIsKeyboardFocusGroup = isFocusGroup;
952
953   // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
954   Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
955 }
956
957 bool Control::IsKeyboardFocusGroup()
958 {
959   return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
960 }
961
962 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
963 {
964   return Actor();
965 }
966
967 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
968 {
969 }
970
971 bool Control::DoAction(BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes)
972 {
973   bool ret = false;
974
975   if( object && ( 0 == strcmp( actionName.c_str(), ACTION_CONTROL_ACTIVATED ) ) )
976   {
977     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
978     if( control )
979     {
980       // if cast succeeds there is an implementation so no need to check
981       control.GetImplementation().OnActivated();
982     }
983   }
984
985   return ret;
986 }
987
988 bool Control::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
989 {
990   Dali::BaseHandle handle( object );
991
992   bool connected( false );
993   Toolkit::Control control = Toolkit::Control::DownCast( handle );
994   if ( control )
995   {
996     Control& controlImpl( control.GetImplementation() );
997     connected = true;
998
999     if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
1000     {
1001       controlImpl.KeyEventSignal().Connect( tracker, functor );
1002     }
1003     else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
1004     {
1005       controlImpl.EnableGestureDetection( Gesture::Tap );
1006       controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
1007     }
1008     else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
1009     {
1010       controlImpl.EnableGestureDetection( Gesture::Pan );
1011       controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
1012     }
1013     else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
1014     {
1015       controlImpl.EnableGestureDetection( Gesture::Pinch );
1016       controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
1017     }
1018     else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
1019     {
1020       controlImpl.EnableGestureDetection( Gesture::LongPress );
1021       controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
1022     }
1023     else
1024     {
1025       // signalName does not match any signal
1026       connected = false;
1027     }
1028   }
1029   return connected;
1030 }
1031
1032 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
1033 {
1034   return mImpl->mKeyEventSignal;
1035 }
1036
1037 bool Control::EmitKeyEventSignal( const KeyEvent& event )
1038 {
1039   // Guard against destruction during signal emission
1040   Dali::Toolkit::Control handle( GetOwner() );
1041
1042   bool consumed = false;
1043
1044   // signals are allocated dynamically when someone connects
1045   if ( !mImpl->mKeyEventSignal.Empty() )
1046   {
1047     consumed = mImpl->mKeyEventSignal.Emit( handle, event );
1048   }
1049
1050   if (!consumed)
1051   {
1052     // Notification for derived classes
1053     consumed = OnKeyEvent(event);
1054   }
1055
1056   return consumed;
1057 }
1058
1059 Control::Control( ControlBehaviour behaviourFlags )
1060 : CustomActorImpl( behaviourFlags & REQUIRES_TOUCH_EVENTS ),
1061   mImpl(new Impl(*this))
1062 {
1063   mImpl->mFlags = behaviourFlags;
1064 }
1065
1066 void Control::Initialize()
1067 {
1068
1069   // Calling deriving classes
1070   OnInitialize();
1071
1072   if( mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS )
1073   {
1074     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1075
1076     // Register for style changes
1077     styleManager.StyleChangeSignal().Connect( this, &Control::DoStyleChange );
1078
1079     // SetTheme
1080     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1081   }
1082
1083   SetRequiresHoverEvents(mImpl->mFlags & REQUIRES_HOVER_EVENTS);
1084   SetRequiresMouseWheelEvents(mImpl->mFlags & REQUIRES_MOUSE_WHEEL_EVENTS);
1085
1086   mImpl->mInitialized = true;
1087 }
1088
1089 void Control::EnableGestureDetection(Gesture::Type type)
1090 {
1091   if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
1092   {
1093     mImpl->mPinchGestureDetector = PinchGestureDetector::New();
1094     mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
1095     mImpl->mPinchGestureDetector.Attach(Self());
1096   }
1097
1098   if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
1099   {
1100     mImpl->mPanGestureDetector = PanGestureDetector::New();
1101     mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
1102     mImpl->mPanGestureDetector.Attach(Self());
1103   }
1104
1105   if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
1106   {
1107     mImpl->mTapGestureDetector = TapGestureDetector::New();
1108     mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
1109     mImpl->mTapGestureDetector.Attach(Self());
1110   }
1111
1112   if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
1113   {
1114     mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
1115     mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
1116     mImpl->mLongPressGestureDetector.Attach(Self());
1117   }
1118 }
1119
1120 void Control::DisableGestureDetection(Gesture::Type type)
1121 {
1122   if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
1123   {
1124     mImpl->mPinchGestureDetector.Detach(Self());
1125     mImpl->mPinchGestureDetector.Reset();
1126   }
1127
1128   if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
1129   {
1130     mImpl->mPanGestureDetector.Detach(Self());
1131     mImpl->mPanGestureDetector.Reset();
1132   }
1133
1134   if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
1135   {
1136     mImpl->mTapGestureDetector.Detach(Self());
1137     mImpl->mTapGestureDetector.Reset();
1138   }
1139
1140   if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
1141   {
1142     mImpl->mLongPressGestureDetector.Detach(Self());
1143     mImpl->mLongPressGestureDetector.Reset();
1144   }
1145 }
1146
1147 void Control::RelayoutRequest()
1148 {
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) )
1152   {
1153     Internal::RelayoutController::Request();
1154   }
1155 }
1156
1157 void Control::Relayout( Actor actor, const Vector2& size, ActorSizeContainer& container )
1158 {
1159   if ( actor )
1160   {
1161     Toolkit::Control control( Toolkit::Control::DownCast( actor ) );
1162     if( control )
1163     {
1164       control.GetImplementation().NegotiateSize( size, container );
1165     }
1166     else
1167     {
1168       container.push_back( ActorSizePair( actor, size ) );
1169     }
1170   }
1171 }
1172
1173 void Control::OnInitialize()
1174 {
1175 }
1176
1177 void Control::OnActivated()
1178 {
1179 }
1180
1181 void Control::OnThemeChange( Toolkit::StyleManager styleManager )
1182 {
1183   GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
1184 }
1185
1186 void Control::OnFontChange( bool defaultFontChange, bool defaultFontSizeChange )
1187 {
1188 }
1189
1190 void Control::OnPinch(const PinchGesture& pinch)
1191 {
1192   if( !( mImpl->mStartingPinchScale ) )
1193   {
1194     // lazy allocate
1195     mImpl->mStartingPinchScale = new Vector3;
1196   }
1197
1198   if( pinch.state == Gesture::Started )
1199   {
1200     *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
1201   }
1202
1203   Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
1204 }
1205
1206 void Control::OnPan( const PanGesture& pan )
1207 {
1208 }
1209
1210 void Control::OnTap(const TapGesture& tap)
1211 {
1212 }
1213
1214 void Control::OnLongPress( const LongPressGesture& longPress )
1215 {
1216 }
1217
1218 void Control::OnControlStageConnection()
1219 {
1220 }
1221
1222 void Control::OnControlStageDisconnection()
1223 {
1224 }
1225
1226 void Control::OnControlChildAdd( Actor& child )
1227 {
1228 }
1229
1230 void Control::OnControlChildRemove( Actor& child )
1231 {
1232 }
1233
1234 void Control::OnControlSizeSet( const Vector3& size )
1235 {
1236 }
1237
1238 void Control::OnRelayout( const Vector2& size, ActorSizeContainer& container )
1239 {
1240   unsigned int numChildren = Self().GetChildCount();
1241
1242   for( unsigned int i=0; i<numChildren; ++i )
1243   {
1244     container.push_back( ActorSizePair( Self().GetChildAt(i), size ) );
1245   }
1246 }
1247
1248 void Control::OnKeyInputFocusGained()
1249 {
1250   // Do Nothing
1251 }
1252
1253 void Control::OnKeyInputFocusLost()
1254 {
1255   // Do Nothing
1256 }
1257
1258 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
1259 {
1260   // @todo consider animating negotiated child sizes to target size
1261 }
1262
1263 bool Control::OnTouchEvent(const TouchEvent& event)
1264 {
1265   return false; // Do not consume
1266 }
1267
1268 bool Control::OnHoverEvent(const HoverEvent& event)
1269 {
1270   return false; // Do not consume
1271 }
1272
1273 bool Control::OnKeyEvent(const KeyEvent& event)
1274 {
1275   return false; // Do not consume
1276 }
1277
1278 bool Control::OnMouseWheelEvent(const MouseWheelEvent& event)
1279 {
1280   return false; // Do not consume
1281 }
1282
1283 Actor Control::GetChildByAlias(const std::string& actorAlias)
1284 {
1285   return Actor();
1286 }
1287
1288 void Control::OnStageConnection()
1289 {
1290   RelayoutRequest();
1291
1292   // Notify derived classes.
1293   OnControlStageConnection();
1294 }
1295
1296 void Control::OnStageDisconnection()
1297 {
1298   // Notify derived classes
1299   OnControlStageDisconnection();
1300 }
1301
1302 void Control::OnChildAdd(Actor& child)
1303 {
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 ) )
1306   {
1307     return;
1308   }
1309
1310   // Request for relayout as we may need to position the new child and old ones
1311   RelayoutRequest();
1312
1313   // Notify derived classes.
1314   OnControlChildAdd( child );
1315 }
1316
1317 void Control::OnChildRemove(Actor& child)
1318 {
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 ) )
1321   {
1322     return;
1323   }
1324
1325   // Request for relayout as we may need to re-position the old child
1326   RelayoutRequest();
1327
1328   // Notify derived classes.
1329   OnControlChildRemove( child );
1330 }
1331
1332 void Control::OnSizeSet(const Vector3& targetSize)
1333 {
1334   if( ( !mImpl->mInsideRelayout ) && ( targetSize != mImpl->mNaturalSize ) )
1335   {
1336     // Only updates size if set through Actor's API
1337     mImpl->mNaturalSize = targetSize;
1338   }
1339
1340   if( targetSize != mImpl->mCurrentSize )
1341   {
1342     // Update control size.
1343     mImpl->mCurrentSize = targetSize;
1344
1345     // Notify derived classes.
1346     OnControlSizeSet( targetSize );
1347   }
1348 }
1349
1350 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
1351 {
1352   mImpl->SignalConnected( slotObserver, callback );
1353 }
1354
1355 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
1356 {
1357   mImpl->SignalDisconnected( slotObserver, callback );
1358 }
1359
1360 void Control::DoStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
1361 {
1362   if( change.themeChange )
1363   {
1364     OnThemeChange( styleManager );
1365   }
1366   else if( change.defaultFontChange || change.defaultFontSizeChange )
1367   {
1368     OnFontChange( change.defaultFontChange, change.defaultFontSizeChange );
1369   }
1370 }
1371
1372 } // namespace Internal
1373
1374 } // namespace Toolkit
1375
1376 } // namespace Dali