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