d944114bf4167bad19eb696ffc25de430e52a71d
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / control / control-data-impl.h
1 #ifndef DALI_TOOLKIT_CONTROL_DATA_IMPL_H
2 #define DALI_TOOLKIT_CONTROL_DATA_IMPL_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali-toolkit/devel-api/controls/control-devel.h>
23 #include <dali/devel-api/adaptor-framework/accessibility.h>
24 #include <dali/public-api/object/property-notification.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <string>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
30 #include <dali-toolkit/internal/builder/dictionary.h>
31 #include <dali-toolkit/internal/builder/style.h>
32 #include <dali-toolkit/internal/controls/tooltip/tooltip.h>
33 #include <dali-toolkit/internal/visuals/visual-event-observer.h>
34 #include <dali-toolkit/public-api/controls/control-impl.h>
35 #include <dali-toolkit/public-api/visuals/visual-properties.h>
36 #include <dali/devel-api/common/owner-container.h>
37 #include <dali/integration-api/debug.h>
38 #include <memory>
39
40 namespace Dali
41 {
42 namespace Toolkit
43 {
44 namespace Internal
45 {
46 /**
47   * Struct used to store Visual within the control, index is a unique key for each visual.
48   */
49 struct RegisteredVisual
50 {
51   Property::Index       index;
52   Toolkit::Visual::Base visual;
53   bool                  enabled : 1;
54   bool                  pending : 1;
55
56   RegisteredVisual(Property::Index aIndex, Toolkit::Visual::Base& aVisual, bool aEnabled, bool aPendingReplacement)
57   : index(aIndex),
58     visual(aVisual),
59     enabled(aEnabled),
60     pending(aPendingReplacement)
61   {
62   }
63 };
64
65 typedef Dali::OwnerContainer<RegisteredVisual*> RegisteredVisualContainer;
66
67 /**
68  * @brief Holds the Implementation for the internal control class
69  */
70 class Control::Impl : public ConnectionTracker, public Visual::EventObserver
71 {
72 public:
73   /**
74    * @brief Retrieves the implementation of the internal control class.
75    * @param[in] internalControl A ref to the control whose internal implementation is required
76    * @return The internal implementation
77    */
78   static Control::Impl& Get(Internal::Control& internalControl);
79
80   /**
81    * @copydoc Get( Internal::Control& )
82    */
83   static const Control::Impl& Get(const Internal::Control& internalControl);
84
85   /**
86    * @brief Constructor.
87    * @param[in] controlImpl The control which owns this implementation
88    */
89   Impl(Control& controlImpl);
90
91   /**
92    * @brief Destructor.
93    */
94   ~Impl();
95
96   /**
97    * @brief Called when a pinch is detected.
98    * @param[in] actor The actor the pinch occurred on
99    * @param[in] pinch The pinch gesture details
100    */
101   void PinchDetected(Actor actor, const PinchGesture& pinch);
102
103   /**
104    * @brief Called when a pan is detected.
105    * @param[in] actor The actor the pan occurred on
106    * @param[in] pinch The pan gesture details
107    */
108   void PanDetected(Actor actor, const PanGesture& pan);
109
110   /**
111    * @brief Called when a tap is detected.
112    * @param[in] actor The actor the tap occurred on
113    * @param[in] pinch The tap gesture details
114    */
115   void TapDetected(Actor actor, const TapGesture& tap);
116
117   /**
118    * @brief Called when a long-press is detected.
119    * @param[in] actor The actor the long-press occurred on
120    * @param[in] pinch The long-press gesture details
121    */
122   void LongPressDetected(Actor actor, const LongPressGesture& longPress);
123
124   /**
125    * @brief Called when a resource is ready.
126    * @param[in] object The visual whose resources are ready
127    * @note Overriding method in Visual::EventObserver.
128    */
129   void ResourceReady(Visual::Base& object) override;
130
131   /**
132    * @brief Called when an event occurs.
133    * @param[in] object The visual whose events occur
134    * @param[in] signalId The signal to emit. See Visual to find supported signals
135    * @note Overriding method in Visual::EventObserver.
136    */
137   void NotifyVisualEvent(Visual::Base& object, Property::Index signalId) override;
138
139   /**
140    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
141    */
142   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual);
143
144   /**
145    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
146    */
147   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, int depthIndex);
148
149   /**
150    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
151    */
152   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, bool enabled);
153
154   /**
155    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
156    */
157   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, bool enabled, int depthIndex);
158
159   /**
160    * @copydoc Dali::Toolkit::DevelControl::UnregisterVisual()
161    */
162   void UnregisterVisual(Property::Index index);
163
164   /**
165    * @copydoc Dali::Toolkit::DevelControl::GetVisual()
166    */
167   Toolkit::Visual::Base GetVisual(Property::Index index) const;
168
169   /**
170    * @copydoc Dali::Toolkit::DevelControl::EnableVisual()
171    */
172   void EnableVisual(Property::Index index, bool enable);
173
174   /**
175    * @copydoc Dali::Toolkit::DevelControl::IsVisualEnabled()
176    */
177   bool IsVisualEnabled(Property::Index index) const;
178
179   /**
180    * @brief Stops observing the given visual.
181    * @param[in] visual The visual to stop observing
182    */
183   void StopObservingVisual(Toolkit::Visual::Base& visual);
184
185   /**
186    * @brief Starts observing the given visual.
187    * @param[in] visual The visual to start observing
188    */
189   void StartObservingVisual(Toolkit::Visual::Base& visual);
190
191   /**
192    * @copydoc Dali::Toolkit::DevelControl::GetVisualResourceStatus()
193    */
194   Toolkit::Visual::ResourceStatus GetVisualResourceStatus(Property::Index index) const;
195
196   /**
197    * @param[in,out] animation Handle to existing animation, or an empty handle that
198    * can be set to a New animation if createAnimation is true
199    * @param[in] transitionData The transition data describing the animation
200    * @param[in] createAnimation True if the animation should be created
201    */
202   void AddTransitions(Dali::Animation&               animation,
203                       const Toolkit::TransitionData& transitionData,
204                       bool                           createAnimation = false);
205
206   /**
207    * @copydoc Dali::Toolkit::DevelControl::CreateTransition()
208    */
209   Dali::Animation CreateTransition(const Toolkit::TransitionData& transitionData);
210
211   /**
212    * @copydoc Dali::Toolkit::DevelControl::DoAction()
213    */
214   void DoAction(Dali::Property::Index visualIndex, Dali::Property::Index actionId, const Dali::Property::Value attributes);
215
216   /**
217    * @brief Function used to set control properties.
218    * @param[in] object The object whose property to set
219    * @param[in] index The index of the property to set
220    * @param[in] value The value of the property to set
221    */
222   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
223
224   /**
225    * @brief Function used to retrieve the value of control properties.
226    * @param[in] object The object whose property to get
227    * @param[in] index The index of the property to get
228    * @return The value of the property
229    */
230   static Property::Value GetProperty(BaseObject* object, Property::Index index);
231
232   /**
233    * @brief Sets the state of the control.
234    * @param[in] newState The state to set
235    * @param[in] withTransitions Whether to show a transition when changing to the new state
236    */
237   void SetState(DevelControl::State newState, bool withTransitions = true);
238
239   /**
240    * @brief Sets the sub-state of the control.
241    * @param[in] newState The sub-state to set
242    * @param[in] withTransitions Whether to show a transition when changing to the new sub-state
243    */
244   void SetSubState(const std::string& subStateName, bool withTransitions = true);
245
246   /**
247    * @brief Replaces visuals and properties from the old state to the new state.
248    * @param[in] oldState The old state
249    * @param[in] newState The new state
250    * @param[in] subState The current sub state
251    */
252   void ReplaceStateVisualsAndProperties(const StylePtr oldState, const StylePtr newState, const std::string& subState);
253
254   /**
255    * @brief Removes a visual from the control's container.
256    * @param[in] visuals The container of visuals
257    * @param[in] visualName The name of the visual to remove
258    */
259   void RemoveVisual(RegisteredVisualContainer& visuals, const std::string& visualName);
260
261   /**
262    * @brief Removes several visuals from the control's container.
263    * @param[in] visuals The container of visuals
264    * @param[in] removeVisuals The visuals to remove
265    */
266   void RemoveVisuals(RegisteredVisualContainer& visuals, DictionaryKeys& removeVisuals);
267
268   /**
269    * @brief Copies the visual properties that are specific to the control instance into the instancedProperties container.
270    * @param[in] visuals The control's visual container
271    * @param[out] instancedProperties The instanced properties are added to this container
272    */
273   void CopyInstancedProperties(RegisteredVisualContainer& visuals, Dictionary<Property::Map>& instancedProperties);
274
275   /**
276    * @brief On state change, ensures visuals are moved or created appropriately.
277    *
278    * Go through the list of visuals that are common to both states.
279    * If they are different types, or are both image types with different
280    * URLs, then the existing visual needs moving and the new visual needs creating
281    *
282    * @param[in] stateVisualsToChange The visuals to change
283    * @param[in] instancedProperties The instanced properties @see CopyInstancedProperties
284    */
285   void RecreateChangedVisuals(Dictionary<Property::Map>& stateVisualsToChange, Dictionary<Property::Map>& instancedProperties);
286
287   /**
288    * @brief Whether the resource is ready
289    * @return True if the resource is read.
290    */
291   bool IsResourceReady() const;
292
293   /**
294    * @copydoc CustomActorImpl::OnSceneDisconnection()
295    */
296   void OnSceneDisconnection();
297
298   /**
299    * @brief Sets the margin.
300    * @param[in] margin Margin is a collections of extent ( start, end, top, bottom )
301    */
302   void SetMargin(Extents margin);
303
304   /**
305    * @brief Returns the value of margin
306    * @return The value of margin
307    */
308   Extents GetMargin() const;
309
310   /**
311    * @brief Sets the padding.
312    * @param[in] padding Padding is a collections of extent ( start, end, top, bottom ).
313    */
314   void SetPadding(Extents padding);
315
316   /**
317    * @brief Returns the value of padding
318    * @return The value of padding
319    */
320   Extents GetPadding() const;
321
322   /**
323    * @brief Set the input method context.
324    * @param[in] inputMethodContext The input method context.
325    */
326   void SetInputMethodContext(InputMethodContext& inputMethodContext);
327
328   /**
329    * @brief Filter an key event.
330    * @param[in] event The key to be filtered.
331    * @return True if the key handled, otherwise false.
332    */
333   bool FilterKeyEvent(const KeyEvent& event);
334
335   /**
336    * @brief Adds accessibility attribute
337    * @param[in] key Attribute name to set
338    * @param[in] value Attribute value to set
339    *
340    * Attribute is added if not existed previously or updated
341    * if existed.
342    */
343   void AppendAccessibilityAttribute(const std::string& key,
344                                     const std::string  value);
345
346   /**
347    * @brief Removes accessibility attribute
348    * @param[in] key Attribute name to remove
349    *
350    * Function does nothing if attribute doesn't exist.
351    */
352   void RemoveAccessibilityAttribute(const std::string& key);
353
354   /**
355    * @brief Removes all accessibility attributes
356    */
357   void ClearAccessibilityAttributes();
358
359   /**
360    * @brief Sets reading info type attributes
361    * @param[in] types info type attributes to set
362    *
363    * This function sets, which part of object will be read out
364    * by screen-reader.
365    */
366   void SetAccessibilityReadingInfoType(const Dali::Accessibility::ReadingInfoTypes types);
367
368   /**
369    * @brief Gets currently active reading info type attributes
370    */
371   Dali::Accessibility::ReadingInfoTypes GetAccessibilityReadingInfoType() const;
372
373   /**
374    * @copydoc DevelControl::VisualEventSignal()
375    */
376   DevelControl::VisualEventSignalType& VisualEventSignal();
377
378   /**
379    * @brief Sets the shadow with a property map.
380    * @param[in] map The shadow property map
381    */
382   void SetShadow(const Property::Map& map);
383
384   /**
385    * @brief Clear the shadow.
386    */
387   void ClearShadow();
388
389   /**
390    * @copydoc DevelControl::GetVisualProperty()
391    */
392   Dali::Property GetVisualProperty(Dali::Property::Index index, Dali::Property::Key visualPropertyKey);
393
394 private:
395   /**
396    * Used as an alternative to boolean so that it is obvious whether a visual is enabled/disabled.
397    */
398   struct VisualState
399   {
400     enum Type
401     {
402       DISABLED = 0, ///< Visual disabled.
403       ENABLED  = 1  ///< Visual enabled.
404     };
405   };
406
407   /**
408    * Used as an alternative to boolean so that it is obvious whether a visual's depth value has been set or not by the caller.
409    */
410   struct DepthIndexValue
411   {
412     enum Type
413     {
414       NOT_SET = 0, ///< Visual depth value not set by caller.
415       SET     = 1  ///< Visual depth value set by caller.
416     };
417   };
418
419   /**
420    * @brief Adds the visual to the list of registered visuals.
421    * @param[in] index The Property index of the visual, used to reference visual
422    * @param[in,out] visual The visual to register, which can be altered in this function
423    * @param[in] enabled false if derived class wants to control when visual is set on stage
424    * @param[in] depthIndexValueSet Set to true if the depthIndex has actually been set manually
425    * @param[in] depthIndex The visual's depth-index is set to this
426    *
427    * @note Registering a visual with an index that already has a registered visual will replace it. The replacement will
428    *       occur once the replacement visual is ready (loaded).
429    */
430   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex = 0);
431
432   /**
433    * @brief Emits the resource ready signal.
434    */
435   void EmitResourceReadySignal();
436
437   /**
438    * @brief Callbacks called on idle.
439    */
440   void OnIdleCallback();
441
442 public:
443   Control&            mControlImpl;
444   DevelControl::State mState;
445   std::string         mSubStateName;
446   Property::Map       mAccessibilityAttributes;
447
448   int mLeftFocusableActorId;  ///< Actor ID of Left focusable control.
449   int mRightFocusableActorId; ///< Actor ID of Right focusable control.
450   int mUpFocusableActorId;    ///< Actor ID of Up focusable control.
451   int mDownFocusableActorId;  ///< Actor ID of Down focusable control.
452
453   RegisteredVisualContainer                                      mVisuals; ///< Stores visuals needed by the control, non trivial type so std::vector used.
454   std::string                                                    mStyleName;
455   Vector4                                                        mBackgroundColor;    ///< The color of the background visual
456   Vector3*                                                       mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
457   Extents                                                        mMargin;             ///< The margin values
458   Extents                                                        mPadding;            ///< The padding values
459   Toolkit::Control::KeyEventSignalType                           mKeyEventSignal;
460   Toolkit::Control::KeyInputFocusSignalType                      mKeyInputFocusGainedSignal;
461   Toolkit::Control::KeyInputFocusSignalType                      mKeyInputFocusLostSignal;
462   Toolkit::Control::ResourceReadySignalType                      mResourceReadySignal;
463   DevelControl::VisualEventSignalType                            mVisualEventSignal;
464   Toolkit::DevelControl::AccessibilityActivateSignalType         mAccessibilityActivateSignal;
465   Toolkit::DevelControl::AccessibilityReadingSkippedSignalType   mAccessibilityReadingSkippedSignal;
466   Toolkit::DevelControl::AccessibilityReadingPausedSignalType    mAccessibilityReadingPausedSignal;
467   Toolkit::DevelControl::AccessibilityReadingResumedSignalType   mAccessibilityReadingResumedSignal;
468   Toolkit::DevelControl::AccessibilityReadingCancelledSignalType mAccessibilityReadingCancelledSignal;
469   Toolkit::DevelControl::AccessibilityReadingStoppedSignalType   mAccessibilityReadingStoppedSignal;
470
471   Toolkit::DevelControl::AccessibilityGetNameSignalType        mAccessibilityGetNameSignal;
472   Toolkit::DevelControl::AccessibilityGetDescriptionSignalType mAccessibilityGetDescriptionSignal;
473   Toolkit::DevelControl::AccessibilityDoGestureSignalType      mAccessibilityDoGestureSignal;
474
475   std::string mAccessibilityName;
476   bool        mAccessibilityNameSet = false;
477
478   std::string mAccessibilityDescription;
479   bool        mAccessibilityDescriptionSet = false;
480
481   std::string mAccessibilityTranslationDomain;
482   bool        mAccessibilityTranslationDomainSet = false;
483
484   bool mAccessibilityHighlightable    = false;
485   bool mAccessibilityHighlightableSet = false;
486
487   Dali::Accessibility::Role mAccessibilityRole = Dali::Accessibility::Role::UNKNOWN;
488
489   std::vector<std::vector<Accessibility::Address>> mAccessibilityRelations;
490   bool                                             mAccessibilityAnimated = false;
491
492   // Gesture Detection
493   PinchGestureDetector     mPinchGestureDetector;
494   PanGestureDetector       mPanGestureDetector;
495   TapGestureDetector       mTapGestureDetector;
496   LongPressGestureDetector mLongPressGestureDetector;
497
498   // Tooltip
499   TooltipPtr mTooltip;
500
501   InputMethodContext mInputMethodContext;
502   CallbackBase*      mIdleCallback; ///< The idle callback to emit the resource ready signal.
503
504   ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
505   bool             mIsKeyboardNavigationSupported : 1;    ///< Stores whether keyboard navigation is supported by the control.
506   bool             mIsKeyboardFocusGroup : 1;             ///< Stores whether the control is a focus group.
507   bool             mIsEmittingResourceReadySignal : 1;    ///< True during ResourceReady().
508   bool             mNeedToEmitResourceReady : 1;          ///< True if need to emit the resource ready signal again.
509
510   RegisteredVisualContainer mRemoveVisuals; ///< List of visuals that are being replaced by another visual once ready
511
512   // Properties - these need to be members of Internal::Control::Impl as they access private methods/data of Internal::Control and Internal::Control::Impl.
513   static const PropertyRegistration PROPERTY_1;
514   static const PropertyRegistration PROPERTY_2;
515   static const PropertyRegistration PROPERTY_3;
516   static const PropertyRegistration PROPERTY_4;
517   static const PropertyRegistration PROPERTY_5;
518   static const PropertyRegistration PROPERTY_6;
519   static const PropertyRegistration PROPERTY_7;
520   static const PropertyRegistration PROPERTY_8;
521   static const PropertyRegistration PROPERTY_9;
522   static const PropertyRegistration PROPERTY_10;
523   static const PropertyRegistration PROPERTY_11;
524   static const PropertyRegistration PROPERTY_12;
525   static const PropertyRegistration PROPERTY_13;
526   static const PropertyRegistration PROPERTY_14;
527   static const PropertyRegistration PROPERTY_15;
528   static const PropertyRegistration PROPERTY_16;
529   static const PropertyRegistration PROPERTY_17;
530   static const PropertyRegistration PROPERTY_18;
531   static const PropertyRegistration PROPERTY_19;
532   static const PropertyRegistration PROPERTY_20;
533   static const PropertyRegistration PROPERTY_21;
534   static const PropertyRegistration PROPERTY_22;
535
536   /**
537    * The method acquires Accessible handle from Actor object
538    * @param  actor Actor object
539    * @return       handle to Accessible object
540    */
541   static Dali::Accessibility::Accessible* GetAccessibilityObject(Dali::Actor actor);
542   Dali::Accessibility::Accessible*        GetAccessibilityObject();
543
544   void AccessibilityRegister();
545   void AccessibilityDeregister();
546
547   struct AccessibleImpl : public virtual Dali::Accessibility::Accessible,
548                           public virtual Dali::Accessibility::Component,
549                           public virtual Dali::Accessibility::Collection,
550                           public virtual Dali::Accessibility::Action
551   {
552     Dali::Actor self;
553     bool        modal = false, root = false;
554
555     AccessibleImpl(Dali::Actor self, Dali::Accessibility::Role role, bool modal = false);
556
557     std::string                         GetName() override;
558     virtual std::string                 GetNameRaw();
559     std::string                         GetDescription() override;
560     virtual std::string                 GetDescriptionRaw();
561     Dali::Accessibility::Accessible*    GetParent() override;
562     size_t                              GetChildCount() override;
563     Dali::Accessibility::Accessible*    GetChildAtIndex(size_t index) override;
564     size_t                              GetIndexInParent() override;
565     Dali::Accessibility::Role           GetRole() override;
566     Dali::Accessibility::States         GetStates() override;
567     Dali::Accessibility::Attributes     GetAttributes() override;
568     Dali::Rect<>                        GetExtents(Dali::Accessibility::CoordType ctype) override;
569     Dali::Accessibility::ComponentLayer GetLayer() override;
570     int16_t                             GetMdiZOrder() override;
571     bool                                GrabFocus() override;
572     double                              GetAlpha() override;
573     bool                                GrabHighlight() override;
574     bool                                ClearHighlight() override;
575
576     std::string                                GetActionName(size_t index) override;
577     std::string                                GetLocalizedActionName(size_t index) override;
578     std::string                                GetActionDescription(size_t index) override;
579     size_t                                     GetActionCount() override;
580     std::string                                GetActionKeyBinding(size_t index) override;
581     bool                                       DoAction(size_t index) override;
582     bool                                       DoAction(const std::string& name) override;
583     bool                                       DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo) override;
584     std::vector<Dali::Accessibility::Relation> GetRelationSet() override;
585
586     virtual Dali::Accessibility::States CalculateStates();
587     virtual void                        EnsureChildVisible(Actor child);
588     virtual void                        EnsureSelfVisible();
589     virtual Property::Index             GetNamePropertyIndex();
590     virtual Property::Index             GetDescriptionPropertyIndex();
591   };
592
593   std::function<std::unique_ptr<Dali::Accessibility::Accessible>(Actor)> accessibilityConstructor;
594   std::unique_ptr<Dali::Accessibility::Accessible>                       accessibilityObject;
595   Dali::PropertyNotification                                             accessibilityNotificationPosition, accessibilityNotificationSize, accessibilityNotificationCulled;
596   bool                                                                   accessibilityNotificationSet = false;
597   static void                                                            PositionOrSizeChangedCallback(PropertyNotification&);
598   static void                                                            CulledChangedCallback(PropertyNotification&);
599 };
600
601 } // namespace Internal
602
603 } // namespace Toolkit
604
605 } // namespace Dali
606
607 #endif // DALI_TOOLKIT_CONTROL_DATA_IMPL_H