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