Merge "Make TextEditor emit ScrollStateChangedSignal after ScrollBar creation" into...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / control-impl.cpp
1 /*
2  * Copyright (c) 2017 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 <cstring> // for strcmp
23 #include <limits>
24 #include <stack>
25 #include <typeinfo>
26 #include <dali/public-api/animation/constraint.h>
27 #include <dali/public-api/object/type-registry-helper.h>
28 #include <dali/public-api/size-negotiation/relayout-container.h>
29 #include <dali/devel-api/scripting/scripting.h>
30 #include <dali/integration-api/debug.h>
31
32 // INTERNAL INCLUDES
33 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
34 #include <dali-toolkit/public-api/controls/control.h>
35 #include <dali-toolkit/public-api/styling/style-manager.h>
36 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
37 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
38 #include <dali-toolkit/devel-api/controls/control-devel.h>
39 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
40 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
41 #include <dali-toolkit/internal/styling/style-manager-impl.h>
42 #include <dali-toolkit/internal/visuals/color/color-visual.h>
43 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
44 #include <dali-toolkit/devel-api/align-enums.h>
45 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
46
47 namespace Dali
48 {
49
50 namespace Toolkit
51 {
52
53 namespace Internal
54 {
55
56 namespace
57 {
58
59 #if defined(DEBUG_ENABLED)
60 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
61 #endif
62
63 DALI_ENUM_TO_STRING_TABLE_BEGIN( CLIPPING_MODE )
64 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, DISABLED )
65 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, CLIP_CHILDREN )
66 DALI_ENUM_TO_STRING_TABLE_END( CLIPPING_MODE )
67
68 } // unnamed namespace
69
70
71
72 Toolkit::Control Control::New()
73 {
74   // Create the implementation, temporarily owned on stack
75   IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
76
77   // Pass ownership to handle
78   Toolkit::Control handle( *controlImpl );
79
80   // Second-phase init of the implementation
81   // This can only be done after the CustomActor connection has been made...
82   controlImpl->Initialize();
83
84   return handle;
85 }
86
87 void Control::SetStyleName( const std::string& styleName )
88 {
89   if( styleName != mImpl->mStyleName )
90   {
91     mImpl->mStyleName = styleName;
92
93     // Apply new style, if stylemanager is available
94     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
95     if( styleManager )
96     {
97       GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
98     }
99   }
100 }
101
102 const std::string& Control::GetStyleName() const
103 {
104   return mImpl->mStyleName;
105 }
106
107 void Control::SetBackgroundColor( const Vector4& color )
108 {
109   mImpl->mBackgroundColor = color;
110   Property::Map map;
111   map[ Toolkit::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
112   map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
113
114   SetBackground( map );
115 }
116
117 Vector4 Control::GetBackgroundColor() const
118 {
119   return mImpl->mBackgroundColor;
120 }
121
122 void Control::SetBackground( const Property::Map& map )
123 {
124   Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
125   visual.SetName("background");
126   if( visual )
127   {
128     mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
129
130     // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
131     RelayoutRequest();
132   }
133 }
134
135 void Control::SetBackgroundImage( Image image )
136 {
137   Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
138   if( visual )
139   {
140     mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
141   }
142 }
143
144 void Control::ClearBackground()
145 {
146    mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
147    mImpl->mBackgroundColor = Color::TRANSPARENT;
148
149    // Trigger a size negotiation request that may be needed when unregistering a visual.
150    RelayoutRequest();
151 }
152
153 void Control::EnableGestureDetection(Gesture::Type type)
154 {
155   if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
156   {
157     mImpl->mPinchGestureDetector = PinchGestureDetector::New();
158     mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
159     mImpl->mPinchGestureDetector.Attach(Self());
160   }
161
162   if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
163   {
164     mImpl->mPanGestureDetector = PanGestureDetector::New();
165     mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
166     mImpl->mPanGestureDetector.Attach(Self());
167   }
168
169   if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
170   {
171     mImpl->mTapGestureDetector = TapGestureDetector::New();
172     mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
173     mImpl->mTapGestureDetector.Attach(Self());
174   }
175
176   if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
177   {
178     mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
179     mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
180     mImpl->mLongPressGestureDetector.Attach(Self());
181   }
182 }
183
184 void Control::DisableGestureDetection(Gesture::Type type)
185 {
186   if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
187   {
188     mImpl->mPinchGestureDetector.Detach(Self());
189     mImpl->mPinchGestureDetector.Reset();
190   }
191
192   if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
193   {
194     mImpl->mPanGestureDetector.Detach(Self());
195     mImpl->mPanGestureDetector.Reset();
196   }
197
198   if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
199   {
200     mImpl->mTapGestureDetector.Detach(Self());
201     mImpl->mTapGestureDetector.Reset();
202   }
203
204   if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
205   {
206     mImpl->mLongPressGestureDetector.Detach(Self());
207     mImpl->mLongPressGestureDetector.Reset();
208   }
209 }
210
211 PinchGestureDetector Control::GetPinchGestureDetector() const
212 {
213   return mImpl->mPinchGestureDetector;
214 }
215
216 PanGestureDetector Control::GetPanGestureDetector() const
217 {
218   return mImpl->mPanGestureDetector;
219 }
220
221 TapGestureDetector Control::GetTapGestureDetector() const
222 {
223   return mImpl->mTapGestureDetector;
224 }
225
226 LongPressGestureDetector Control::GetLongPressGestureDetector() const
227 {
228   return mImpl->mLongPressGestureDetector;
229 }
230
231 void Control::SetKeyboardNavigationSupport(bool isSupported)
232 {
233   mImpl->mIsKeyboardNavigationSupported = isSupported;
234 }
235
236 bool Control::IsKeyboardNavigationSupported()
237 {
238   return mImpl->mIsKeyboardNavigationSupported;
239 }
240
241 void Control::SetKeyInputFocus()
242 {
243   if( Self().OnStage() )
244   {
245     Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
246   }
247 }
248
249 bool Control::HasKeyInputFocus()
250 {
251   bool result = false;
252   if( Self().OnStage() )
253   {
254     Toolkit::Control control = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
255     if( Self() == control )
256     {
257       result = true;
258     }
259   }
260   return result;
261 }
262
263 void Control::ClearKeyInputFocus()
264 {
265   if( Self().OnStage() )
266   {
267     Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
268   }
269 }
270
271 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
272 {
273   mImpl->mIsKeyboardFocusGroup = isFocusGroup;
274
275   // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
276   Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
277 }
278
279 bool Control::IsKeyboardFocusGroup()
280 {
281   return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
282 }
283
284 void Control::AccessibilityActivate()
285 {
286   // Inform deriving classes
287   OnAccessibilityActivated();
288 }
289
290 void Control::KeyboardEnter()
291 {
292   // Inform deriving classes
293   OnKeyboardEnter();
294 }
295
296 bool Control::OnAccessibilityActivated()
297 {
298   return false; // Accessibility activation is not handled by default
299 }
300
301 bool Control::OnKeyboardEnter()
302 {
303   return false; // Keyboard enter is not handled by default
304 }
305
306 bool Control::OnAccessibilityPan(PanGesture gesture)
307 {
308   return false; // Accessibility pan gesture is not handled by default
309 }
310
311 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
312 {
313   return false; // Accessibility touch event is not handled by default
314 }
315
316 bool Control::OnAccessibilityValueChange(bool isIncrease)
317 {
318   return false; // Accessibility value change action is not handled by default
319 }
320
321 bool Control::OnAccessibilityZoom()
322 {
323   return false; // Accessibility zoom action is not handled by default
324 }
325
326 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
327 {
328   return Actor();
329 }
330
331 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
332 {
333 }
334
335 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
336 {
337   return mImpl->mKeyEventSignal;
338 }
339
340 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
341 {
342   return mImpl->mKeyInputFocusGainedSignal;
343 }
344
345 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
346 {
347   return mImpl->mKeyInputFocusLostSignal;
348 }
349
350 bool Control::EmitKeyEventSignal( const KeyEvent& event )
351 {
352   // Guard against destruction during signal emission
353   Dali::Toolkit::Control handle( GetOwner() );
354
355   bool consumed = false;
356
357   // signals are allocated dynamically when someone connects
358   if ( !mImpl->mKeyEventSignal.Empty() )
359   {
360     consumed = mImpl->mKeyEventSignal.Emit( handle, event );
361   }
362
363   if (!consumed)
364   {
365     // Notification for derived classes
366     consumed = OnKeyEvent(event);
367   }
368
369   return consumed;
370 }
371
372 Control::Control( ControlBehaviour behaviourFlags )
373 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
374   mImpl(new Impl(*this))
375 {
376   mImpl->mFlags = behaviourFlags;
377 }
378
379 Control::~Control()
380 {
381   delete mImpl;
382 }
383
384 void Control::Initialize()
385 {
386   // Call deriving classes so initialised before styling is applied to them.
387   OnInitialize();
388
389   if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
390       !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
391   {
392     Toolkit::StyleManager styleManager = StyleManager::Get();
393
394     // if stylemanager is available
395     if( styleManager )
396     {
397       StyleManager& styleManagerImpl = GetImpl( styleManager );
398
399       // Register for style changes
400       styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
401
402       // Apply the current style
403       styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
404     }
405   }
406
407   if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
408   {
409     SetKeyboardNavigationSupport( true );
410   }
411 }
412
413 void Control::OnInitialize()
414 {
415 }
416
417 void Control::OnControlChildAdd( Actor& child )
418 {
419 }
420
421 void Control::OnControlChildRemove( Actor& child )
422 {
423 }
424
425 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
426 {
427   // By default the control is only interested in theme (not font) changes
428   if( styleManager && change == StyleChange::THEME_CHANGE )
429   {
430     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
431     RelayoutRequest();
432   }
433 }
434
435 void Control::OnPinch(const PinchGesture& pinch)
436 {
437   if( !( mImpl->mStartingPinchScale ) )
438   {
439     // lazy allocate
440     mImpl->mStartingPinchScale = new Vector3;
441   }
442
443   if( pinch.state == Gesture::Started )
444   {
445     *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
446   }
447
448   Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
449 }
450
451 void Control::OnPan( const PanGesture& pan )
452 {
453 }
454
455 void Control::OnTap(const TapGesture& tap)
456 {
457 }
458
459 void Control::OnLongPress( const LongPressGesture& longPress )
460 {
461 }
462
463 void Control::EmitKeyInputFocusSignal( bool focusGained )
464 {
465   Dali::Toolkit::Control handle( GetOwner() );
466
467   if ( focusGained )
468   {
469     // signals are allocated dynamically when someone connects
470     if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
471     {
472       mImpl->mKeyInputFocusGainedSignal.Emit( handle );
473     }
474   }
475   else
476   {
477     // signals are allocated dynamically when someone connects
478     if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
479     {
480       mImpl->mKeyInputFocusLostSignal.Emit( handle );
481     }
482   }
483 }
484
485 void Control::OnStageConnection( int depth )
486 {
487   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n",  mImpl->mVisuals.Size() );
488
489   Actor self( Self() );
490
491   for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
492   {
493     // Check whether the visual is empty and enabled
494     if( (*iter)->visual && (*iter)->enabled )
495     {
496       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
497       Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
498     }
499   }
500
501   if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() )
502   {
503     Property::Value clippingValue = self.GetProperty( Actor::Property::CLIPPING_MODE );
504     int clippingMode = ClippingMode::DISABLED;
505     if( clippingValue.Get( clippingMode ) )
506     {
507       // Add a transparent background if we do not have any renderers or visuals so we clip our children
508
509       if( clippingMode == ClippingMode::CLIP_CHILDREN )
510       {
511         // Create a transparent background visual which will also get staged.
512         SetBackgroundColor( Color::TRANSPARENT );
513       }
514     }
515   }
516 }
517
518 void Control::OnStageDisconnection()
519 {
520   for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
521   {
522     // Check whether the visual is empty
523     if( (*iter)->visual )
524     {
525       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
526       Actor self( Self() );
527       Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
528     }
529   }
530 }
531
532 void Control::OnKeyInputFocusGained()
533 {
534   EmitKeyInputFocusSignal( true );
535 }
536
537 void Control::OnKeyInputFocusLost()
538 {
539   EmitKeyInputFocusSignal( false );
540 }
541
542 void Control::OnChildAdd(Actor& child)
543 {
544   // Notify derived classes.
545   OnControlChildAdd( child );
546 }
547
548 void Control::OnChildRemove(Actor& child)
549 {
550   // Notify derived classes.
551   OnControlChildRemove( child );
552 }
553
554 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
555 {
556   Actor self( Self() );
557   if( index == Actor::Property::CLIPPING_MODE )
558   {
559     // Only set the background if we're already on the stage and have no renderers or visuals
560
561     if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() && self.OnStage() )
562     {
563       ClippingMode::Type clippingMode = ClippingMode::DISABLED;
564       if( Scripting::GetEnumerationProperty< ClippingMode::Type >( propertyValue, CLIPPING_MODE_TABLE, CLIPPING_MODE_TABLE_COUNT, clippingMode ) )
565       {
566         // Add a transparent background if we do not have one so we clip children
567
568         if( clippingMode == ClippingMode::CLIP_CHILDREN )
569         {
570           SetBackgroundColor( Color::TRANSPARENT );
571         }
572       }
573     }
574   }
575 }
576
577 void Control::OnSizeSet(const Vector3& targetSize)
578 {
579   Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
580   if( visual )
581   {
582     Vector2 size( targetSize );
583     visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
584   }
585 }
586
587 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
588 {
589   // @todo size negotiate background to new size, animate as well?
590 }
591
592 bool Control::OnTouchEvent(const TouchEvent& event)
593 {
594   return false; // Do not consume
595 }
596
597 bool Control::OnHoverEvent(const HoverEvent& event)
598 {
599   return false; // Do not consume
600 }
601
602 bool Control::OnKeyEvent(const KeyEvent& event)
603 {
604   return false; // Do not consume
605 }
606
607 bool Control::OnWheelEvent(const WheelEvent& event)
608 {
609   return false; // Do not consume
610 }
611
612 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
613 {
614   for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
615   {
616     container.Add( Self().GetChildAt( i ), size );
617   }
618
619   Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
620   if( visual )
621   {
622     visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
623   }
624 }
625
626 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
627 {
628 }
629
630 Vector3 Control::GetNaturalSize()
631 {
632   Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
633   if( visual )
634   {
635     Vector2 naturalSize;
636     visual.GetNaturalSize( naturalSize );
637     return Vector3( naturalSize );
638   }
639   return Vector3::ZERO;
640 }
641
642 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
643 {
644   return CalculateChildSizeBase( child, dimension );
645 }
646
647 float Control::GetHeightForWidth( float width )
648 {
649   return GetHeightForWidthBase( width );
650 }
651
652 float Control::GetWidthForHeight( float height )
653 {
654   return GetWidthForHeightBase( height );
655 }
656
657 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
658 {
659   return RelayoutDependentOnChildrenBase( dimension );
660 }
661
662 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
663 {
664 }
665
666 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
667 {
668 }
669
670 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
671 {
672   mImpl->SignalConnected( slotObserver, callback );
673 }
674
675 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
676 {
677   mImpl->SignalDisconnected( slotObserver, callback );
678 }
679
680 Control& GetImplementation( Dali::Toolkit::Control& handle )
681 {
682   CustomActorImpl& customInterface = handle.GetImplementation();
683   // downcast to control
684   Control& impl = dynamic_cast< Internal::Control& >( customInterface );
685   return impl;
686 }
687
688 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
689 {
690   const CustomActorImpl& customInterface = handle.GetImplementation();
691   // downcast to control
692   const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
693   return impl;
694 }
695
696 } // namespace Internal
697
698 } // namespace Toolkit
699
700 } // namespace Dali