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