e278390816990b07e38374d15fc941a5374e4b73
[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   friend class Toolkit::DevelControl::AccessibleImpl;
73
74 public:
75   /**
76    * @brief Retrieves the implementation of the internal control class.
77    * @param[in] internalControl A ref to the control whose internal implementation is required
78    * @return The internal implementation
79    */
80   static Control::Impl& Get(Internal::Control& internalControl);
81
82   /**
83    * @copydoc Get( Internal::Control& )
84    */
85   static const Control::Impl& Get(const Internal::Control& internalControl);
86
87   /**
88    * @brief Constructor.
89    * @param[in] controlImpl The control which owns this implementation
90    */
91   Impl(Control& controlImpl);
92
93   /**
94    * @brief Destructor.
95    */
96   ~Impl();
97
98   /**
99    * @brief Called when a pinch is detected.
100    * @param[in] actor The actor the pinch occurred on
101    * @param[in] pinch The pinch gesture details
102    */
103   void PinchDetected(Actor actor, const PinchGesture& pinch);
104
105   /**
106    * @brief Called when a pan is detected.
107    * @param[in] actor The actor the pan occurred on
108    * @param[in] pinch The pan gesture details
109    */
110   void PanDetected(Actor actor, const PanGesture& pan);
111
112   /**
113    * @brief Called when a tap is detected.
114    * @param[in] actor The actor the tap occurred on
115    * @param[in] pinch The tap gesture details
116    */
117   void TapDetected(Actor actor, const TapGesture& tap);
118
119   /**
120    * @brief Called when a long-press is detected.
121    * @param[in] actor The actor the long-press occurred on
122    * @param[in] pinch The long-press gesture details
123    */
124   void LongPressDetected(Actor actor, const LongPressGesture& longPress);
125
126   /**
127    * @brief Called when a resource is ready.
128    * @param[in] object The visual whose resources are ready
129    * @note Overriding method in Visual::EventObserver.
130    */
131   void ResourceReady(Visual::Base& object) override;
132
133   /**
134    * @brief Called when an event occurs.
135    * @param[in] object The visual whose events occur
136    * @param[in] signalId The signal to emit. See Visual to find supported signals
137    * @note Overriding method in Visual::EventObserver.
138    */
139   void NotifyVisualEvent(Visual::Base& object, Property::Index signalId) override;
140
141   /**
142    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
143    */
144   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual);
145
146   /**
147    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
148    */
149   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, int depthIndex);
150
151   /**
152    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
153    */
154   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, bool enabled);
155
156   /**
157    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
158    */
159   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, bool enabled, int depthIndex);
160
161   /**
162    * @copydoc Dali::Toolkit::DevelControl::UnregisterVisual()
163    */
164   void UnregisterVisual(Property::Index index);
165
166   /**
167    * @copydoc Dali::Toolkit::DevelControl::GetVisual()
168    */
169   Toolkit::Visual::Base GetVisual(Property::Index index) const;
170
171   /**
172    * @copydoc Dali::Toolkit::DevelControl::EnableVisual()
173    */
174   void EnableVisual(Property::Index index, bool enable);
175
176   /**
177    * @copydoc Dali::Toolkit::DevelControl::IsVisualEnabled()
178    */
179   bool IsVisualEnabled(Property::Index index) const;
180
181   /**
182    * @brief Stops observing the given visual.
183    * @param[in] visual The visual to stop observing
184    */
185   void StopObservingVisual(Toolkit::Visual::Base& visual);
186
187   /**
188    * @brief Starts observing the given visual.
189    * @param[in] visual The visual to start observing
190    */
191   void StartObservingVisual(Toolkit::Visual::Base& visual);
192
193   /**
194    * @copydoc Dali::Toolkit::DevelControl::GetVisualResourceStatus()
195    */
196   Toolkit::Visual::ResourceStatus GetVisualResourceStatus(Property::Index index) const;
197
198   /**
199    * @param[in,out] animation Handle to existing animation, or an empty handle that
200    * can be set to a New animation if createAnimation is true
201    * @param[in] transitionData The transition data describing the animation
202    * @param[in] createAnimation True if the animation should be created
203    */
204   void AddTransitions(Dali::Animation&               animation,
205                       const Toolkit::TransitionData& transitionData,
206                       bool                           createAnimation = false);
207
208   /**
209    * @copydoc Dali::Toolkit::DevelControl::CreateTransition()
210    */
211   Dali::Animation CreateTransition(const Toolkit::TransitionData& transitionData);
212
213   /**
214    * @copydoc Dali::Toolkit::DevelControl::DoAction()
215    */
216   void DoAction(Dali::Property::Index visualIndex, Dali::Property::Index actionId, const Dali::Property::Value attributes);
217
218   /**
219    * @brief Function used to set control properties.
220    * @param[in] object The object whose property to set
221    * @param[in] index The index of the property to set
222    * @param[in] value The value of the property to set
223    */
224   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
225
226   /**
227    * @brief Function used to retrieve the value of control properties.
228    * @param[in] object The object whose property to get
229    * @param[in] index The index of the property to get
230    * @return The value of the property
231    */
232   static Property::Value GetProperty(BaseObject* object, Property::Index index);
233
234   /**
235    * @brief Sets the state of the control.
236    * @param[in] newState The state to set
237    * @param[in] withTransitions Whether to show a transition when changing to the new state
238    */
239   void SetState(DevelControl::State newState, bool withTransitions = true);
240
241   /**
242    * @brief Sets the sub-state of the control.
243    * @param[in] newState The sub-state to set
244    * @param[in] withTransitions Whether to show a transition when changing to the new sub-state
245    */
246   void SetSubState(const std::string& subStateName, bool withTransitions = true);
247
248   /**
249    * @brief Replaces visuals and properties from the old state to the new state.
250    * @param[in] oldState The old state
251    * @param[in] newState The new state
252    * @param[in] subState The current sub state
253    */
254   void ReplaceStateVisualsAndProperties(const StylePtr oldState, const StylePtr newState, const std::string& subState);
255
256   /**
257    * @brief Removes a visual from the control's container.
258    * @param[in] visuals The container of visuals
259    * @param[in] visualName The name of the visual to remove
260    */
261   void RemoveVisual(RegisteredVisualContainer& visuals, const std::string& visualName);
262
263   /**
264    * @brief Removes several visuals from the control's container.
265    * @param[in] visuals The container of visuals
266    * @param[in] removeVisuals The visuals to remove
267    */
268   void RemoveVisuals(RegisteredVisualContainer& visuals, DictionaryKeys& removeVisuals);
269
270   /**
271    * @brief Copies the visual properties that are specific to the control instance into the instancedProperties container.
272    * @param[in] visuals The control's visual container
273    * @param[out] instancedProperties The instanced properties are added to this container
274    */
275   void CopyInstancedProperties(RegisteredVisualContainer& visuals, Dictionary<Property::Map>& instancedProperties);
276
277   /**
278    * @brief On state change, ensures visuals are moved or created appropriately.
279    *
280    * Go through the list of visuals that are common to both states.
281    * If they are different types, or are both image types with different
282    * URLs, then the existing visual needs moving and the new visual needs creating
283    *
284    * @param[in] stateVisualsToChange The visuals to change
285    * @param[in] instancedProperties The instanced properties @see CopyInstancedProperties
286    */
287   void RecreateChangedVisuals(Dictionary<Property::Map>& stateVisualsToChange, Dictionary<Property::Map>& instancedProperties);
288
289   /**
290    * @brief Whether the resource is ready
291    * @return True if the resource is read.
292    */
293   bool IsResourceReady() const;
294
295   /**
296    * @copydoc CustomActorImpl::OnSceneDisconnection()
297    */
298   void OnSceneDisconnection();
299
300   /**
301    * @brief Sets the margin.
302    * @param[in] margin Margin is a collections of extent ( start, end, top, bottom )
303    */
304   void SetMargin(Extents margin);
305
306   /**
307    * @brief Returns the value of margin
308    * @return The value of margin
309    */
310   Extents GetMargin() const;
311
312   /**
313    * @brief Sets the padding.
314    * @param[in] padding Padding is a collections of extent ( start, end, top, bottom ).
315    */
316   void SetPadding(Extents padding);
317
318   /**
319    * @brief Returns the value of padding
320    * @return The value of padding
321    */
322   Extents GetPadding() const;
323
324   /**
325    * @brief Set the input method context.
326    * @param[in] inputMethodContext The input method context.
327    */
328   void SetInputMethodContext(InputMethodContext& inputMethodContext);
329
330   /**
331    * @brief Filter an key event.
332    * @param[in] event The key to be filtered.
333    * @return True if the key handled, otherwise false.
334    */
335   bool FilterKeyEvent(const KeyEvent& event);
336
337   /**
338    * @brief Adds accessibility attribute
339    * @param[in] key Attribute name to set
340    * @param[in] value Attribute value to set
341    *
342    * Attribute is added if not existed previously or updated
343    * if existed.
344    */
345   void AppendAccessibilityAttribute(const std::string& key, const std::string value);
346
347   /**
348    * @brief Removes accessibility attribute
349    * @param[in] key Attribute name to remove
350    *
351    * Function does nothing if attribute doesn't exist.
352    */
353   void RemoveAccessibilityAttribute(const std::string& key);
354
355   /**
356    * @brief Removes all accessibility attributes
357    */
358   void ClearAccessibilityAttributes();
359
360   /**
361    * @brief Sets reading info type attributes
362    * @param[in] types info type attributes to set
363    *
364    * This function sets, which part of object will be read out
365    * by screen-reader.
366    */
367   void SetAccessibilityReadingInfoType(const Dali::Accessibility::ReadingInfoTypes types);
368
369   /**
370    * @brief Gets currently active reading info type attributes
371    */
372   Dali::Accessibility::ReadingInfoTypes GetAccessibilityReadingInfoType() const;
373
374   /**
375    * @copydoc DevelControl::VisualEventSignal()
376    */
377   DevelControl::VisualEventSignalType& VisualEventSignal();
378
379   /**
380    * @brief Sets the shadow with a property map.
381    * @param[in] map The shadow property map
382    */
383   void SetShadow(const Property::Map& map);
384
385   /**
386    * @brief Clear the shadow.
387    */
388   void ClearShadow();
389
390   /**
391    * @copydoc DevelControl::GetVisualProperty()
392    */
393   Dali::Property GetVisualProperty(Dali::Property::Index index, Dali::Property::Key visualPropertyKey);
394
395   /**
396    * @brief Make visual transition from source control to this control about specific Property::Index
397    * If both of source and this control have Property::Index property, than create animation between them.
398    *
399    * @param[in] animation Return animation from source to this control.
400    * @param[in] source Source control to be used property animation.
401    * @param[in] visualIndex Property::Index to make animation.
402    * @param[in] alphaFunction alpha function of the animation.
403    * @param[in] timePeriod time period of the animation.
404    */
405   void MakeVisualTransition(Dali::Animation& animation, Dali::Toolkit::Control source, Dali::Property::Index visualIndex, AlphaFunction alphaFunction, TimePeriod timePeriod);
406
407   /**
408    * @brief Gets the current control's accessible object.
409    *
410    * @return The handle to Accessible object
411    */
412   Dali::Accessibility::Accessible* GetAccessibilityObject();
413
414   /**
415    * @brief Gets Accessible object handle.
416    *
417    * The method acquires Accessible handle from Actor object
418    * @param  actor Actor object
419    * @return The handle to Accessible object
420    */
421   static Dali::Accessibility::Accessible* GetAccessibilityObject(Dali::Actor actor);
422
423 private:
424   /**
425    * Used as an alternative to boolean so that it is obvious whether a visual is enabled/disabled.
426    */
427   struct VisualState
428   {
429     enum Type
430     {
431       DISABLED = 0, ///< Visual disabled.
432       ENABLED  = 1  ///< Visual enabled.
433     };
434   };
435
436   /**
437    * 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.
438    */
439   struct DepthIndexValue
440   {
441     enum Type
442     {
443       NOT_SET = 0, ///< Visual depth value not set by caller.
444       SET     = 1  ///< Visual depth value set by caller.
445     };
446   };
447
448   /**
449    * @brief Adds the visual to the list of registered visuals.
450    * @param[in] index The Property index of the visual, used to reference visual
451    * @param[in,out] visual The visual to register, which can be altered in this function
452    * @param[in] enabled false if derived class wants to control when visual is set on stage
453    * @param[in] depthIndexValueSet Set to true if the depthIndex has actually been set manually
454    * @param[in] depthIndex The visual's depth-index is set to this
455    *
456    * @note Registering a visual with an index that already has a registered visual will replace it. The replacement will
457    *       occur once the replacement visual is ready (loaded).
458    */
459   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex = 0);
460
461   /**
462    * @brief Emits the resource ready signal.
463    */
464   void EmitResourceReadySignal();
465
466   /**
467    * @brief Callbacks called on idle.
468    */
469   void OnIdleCallback();
470
471 public:
472   Control&            mControlImpl;
473   DevelControl::State mState;
474   std::string         mSubStateName;
475   Property::Map       mAccessibilityAttributes;
476
477   int mLeftFocusableActorId;  ///< Actor ID of Left focusable control.
478   int mRightFocusableActorId; ///< Actor ID of Right focusable control.
479   int mUpFocusableActorId;    ///< Actor ID of Up focusable control.
480   int mDownFocusableActorId;  ///< Actor ID of Down focusable control.
481
482   RegisteredVisualContainer                                      mVisuals; ///< Stores visuals needed by the control, non trivial type so std::vector used.
483   std::string                                                    mStyleName;
484   Vector4                                                        mBackgroundColor;    ///< The color of the background visual
485   Vector3*                                                       mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
486   Extents                                                        mMargin;             ///< The margin values
487   Extents                                                        mPadding;            ///< The padding values
488   Toolkit::Control::KeyEventSignalType                           mKeyEventSignal;
489   Toolkit::Control::KeyInputFocusSignalType                      mKeyInputFocusGainedSignal;
490   Toolkit::Control::KeyInputFocusSignalType                      mKeyInputFocusLostSignal;
491   Toolkit::Control::ResourceReadySignalType                      mResourceReadySignal;
492   DevelControl::VisualEventSignalType                            mVisualEventSignal;
493
494   // Accessibility
495   Toolkit::DevelControl::AccessibilityActivateSignalType         mAccessibilityActivateSignal;
496   Toolkit::DevelControl::AccessibilityReadingSkippedSignalType   mAccessibilityReadingSkippedSignal;
497   Toolkit::DevelControl::AccessibilityReadingPausedSignalType    mAccessibilityReadingPausedSignal;
498   Toolkit::DevelControl::AccessibilityReadingResumedSignalType   mAccessibilityReadingResumedSignal;
499   Toolkit::DevelControl::AccessibilityReadingCancelledSignalType mAccessibilityReadingCancelledSignal;
500   Toolkit::DevelControl::AccessibilityReadingStoppedSignalType   mAccessibilityReadingStoppedSignal;
501
502   Toolkit::DevelControl::AccessibilityGetNameSignalType          mAccessibilityGetNameSignal;
503   Toolkit::DevelControl::AccessibilityGetDescriptionSignalType   mAccessibilityGetDescriptionSignal;
504   Toolkit::DevelControl::AccessibilityDoGestureSignalType        mAccessibilityDoGestureSignal;
505
506   std::string mAccessibilityName;
507   std::string mAccessibilityDescription;
508   std::string mAccessibilityTranslationDomain;
509   bool        mAccessibilityNameSet = false;
510   bool        mAccessibilityDescriptionSet = false;
511   bool        mAccessibilityTranslationDomainSet = false;
512
513   bool mAccessibilityHighlightable    = false;
514   bool mAccessibilityHighlightableSet = false;
515
516   Dali::Accessibility::Role mAccessibilityRole = Dali::Accessibility::Role::UNKNOWN;
517
518   std::vector<std::vector<Accessibility::Address>>                       mAccessibilityRelations;
519   std::function<std::unique_ptr<Dali::Accessibility::Accessible>(Actor)> mAccessibilityConstructor;
520   std::unique_ptr<Dali::Accessibility::Accessible>                       mAccessibilityObject;
521
522   // Gesture Detection
523   PinchGestureDetector     mPinchGestureDetector;
524   PanGestureDetector       mPanGestureDetector;
525   TapGestureDetector       mTapGestureDetector;
526   LongPressGestureDetector mLongPressGestureDetector;
527
528   // Tooltip
529   TooltipPtr mTooltip;
530
531   InputMethodContext mInputMethodContext;
532   CallbackBase*      mIdleCallback; ///< The idle callback to emit the resource ready signal.
533
534   ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
535   bool             mIsKeyboardNavigationSupported : 1;    ///< Stores whether keyboard navigation is supported by the control.
536   bool             mIsKeyboardFocusGroup : 1;             ///< Stores whether the control is a focus group.
537   bool             mIsEmittingResourceReadySignal : 1;    ///< True during ResourceReady().
538   bool             mNeedToEmitResourceReady : 1;          ///< True if need to emit the resource ready signal again.
539
540   RegisteredVisualContainer mRemoveVisuals; ///< List of visuals that are being replaced by another visual once ready
541
542   // Properties - these need to be members of Internal::Control::Impl as they access private methods/data of Internal::Control and Internal::Control::Impl.
543   static const PropertyRegistration PROPERTY_1;
544   static const PropertyRegistration PROPERTY_2;
545   static const PropertyRegistration PROPERTY_3;
546   static const PropertyRegistration PROPERTY_4;
547   static const PropertyRegistration PROPERTY_5;
548   static const PropertyRegistration PROPERTY_6;
549   static const PropertyRegistration PROPERTY_7;
550   static const PropertyRegistration PROPERTY_8;
551   static const PropertyRegistration PROPERTY_9;
552   static const PropertyRegistration PROPERTY_10;
553   static const PropertyRegistration PROPERTY_11;
554   static const PropertyRegistration PROPERTY_12;
555   static const PropertyRegistration PROPERTY_13;
556   static const PropertyRegistration PROPERTY_14;
557   static const PropertyRegistration PROPERTY_15;
558   static const PropertyRegistration PROPERTY_16;
559   static const PropertyRegistration PROPERTY_17;
560   static const PropertyRegistration PROPERTY_18;
561   static const PropertyRegistration PROPERTY_19;
562   static const PropertyRegistration PROPERTY_20;
563   static const PropertyRegistration PROPERTY_21;
564   static const PropertyRegistration PROPERTY_22;
565 };
566
567 } // namespace Internal
568
569 } // namespace Toolkit
570
571 } // namespace Dali
572
573 #endif // DALI_TOOLKIT_CONTROL_DATA_IMPL_H