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