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