df661efd719c1b270a99fe9d5f5947dbf3f42147
[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) 2017 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/public-api/object/type-registry.h>
23 #include <dali-toolkit/devel-api/controls/control-devel.h>
24 #include <string>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/internal/visuals/visual-resource-observer.h>
28 #include <dali-toolkit/public-api/controls/control-impl.h>
29 #include <dali/devel-api/common/owner-container.h>
30 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
31 #include <dali-toolkit/internal/controls/tooltip/tooltip.h>
32 #include <dali-toolkit/internal/builder/style.h>
33 #include <dali-toolkit/internal/builder/dictionary.h>
34
35 namespace Dali
36 {
37
38 namespace Toolkit
39 {
40
41 namespace Internal
42 {
43
44  /**
45   * Struct used to store Visual within the control, index is a unique key for each visual.
46   */
47 struct RegisteredVisual
48 {
49   Property::Index index;
50   Toolkit::Visual::Base visual;
51   bool enabled : 1;
52   bool pending : 1;
53
54   RegisteredVisual( Property::Index aIndex, Toolkit::Visual::Base &aVisual, bool aEnabled, bool aPendingReplacement )
55   : index(aIndex), visual(aVisual), enabled(aEnabled), pending( aPendingReplacement )
56   {
57   }
58 };
59
60 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
61
62
63 /**
64  * @brief Holds the Implementation for the internal control class
65  */
66 class Control::Impl : public ConnectionTracker, public Visual::ResourceObserver
67 {
68
69 public:
70
71   /**
72    * @brief Retrieves the implementation of the internal control class.
73    * @param[in] internalControl A ref to the control whose internal implementation is required
74    * @return The internal implementation
75    */
76   static Control::Impl& Get( Internal::Control& internalControl );
77
78   /**
79    * @copydoc Get( Internal::Control& )
80    */
81   static const Control::Impl& Get( const Internal::Control& internalControl );
82
83   /**
84    * @brief Constructor.
85    * @param[in] controlImpl The control which own this implementation
86    */
87   Impl( Control& controlImpl );
88
89   /**
90    * @brief Destructor.
91    */
92   ~Impl();
93
94   /**
95    * @brief Called when a pinch is detected.
96    * @param[in] actor The actor the pinch occurred on
97    * @param[in] pinch The pinch gesture details
98    */
99   void PinchDetected(Actor actor, const PinchGesture& pinch);
100
101   /**
102    * @brief Called when a pan is detected.
103    * @param[in] actor The actor the pan occurred on
104    * @param[in] pinch The pan gesture details
105    */
106   void PanDetected(Actor actor, const PanGesture& pan);
107
108   /**
109    * @brief Called when a tap is detected.
110    * @param[in] actor The actor the tap occurred on
111    * @param[in] pinch The tap gesture details
112    */
113   void TapDetected(Actor actor, const TapGesture& tap);
114
115   /**
116    * @brief Called when a long-press is detected.
117    * @param[in] actor The actor the long-press occurred on
118    * @param[in] pinch The long-press gesture details
119    */
120   void LongPressDetected(Actor actor, const LongPressGesture& longPress);
121
122   /**
123    * @brief Called when a resource is ready.
124    * @param[in] object The visual whose resources are ready
125    * @note Overriding method in Visual::ResourceObserver.
126    */
127   virtual void ResourceReady( Visual::Base& object );
128
129   /**
130    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
131    */
132   void RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual );
133
134   /**
135    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
136    */
137   void RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, int depthIndex );
138
139   /**
140    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
141    */
142   void RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled );
143
144   /**
145    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
146    */
147   void RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled, int depthIndex );
148
149   /**
150    * @copydoc Dali::Toolkit::DevelControl::UnregisterVisual()
151    */
152   void UnregisterVisual( Property::Index index );
153
154   /**
155    * @copydoc Dali::Toolkit::DevelControl::GetVisual()
156    */
157   Toolkit::Visual::Base GetVisual( Property::Index index ) const;
158
159   /**
160    * @copydoc Dali::Toolkit::DevelControl::EnableVisual()
161    */
162   void EnableVisual( Property::Index index, bool enable );
163
164   /**
165    * @copydoc Dali::Toolkit::DevelControl::IsVisualEnabled()
166    */
167   bool IsVisualEnabled( Property::Index index ) const;
168
169   /**
170    * @brief Stops observing the given visual.
171    * @param[in] visual The visual to stop observing
172    */
173   void StopObservingVisual( Toolkit::Visual::Base& visual );
174
175   /**
176    * @brief Starts observing the given visual.
177    * @param[in] visual The visual to start observing
178    */
179   void StartObservingVisual( Toolkit::Visual::Base& visual);
180
181   /**
182    * @copydoc Dali::Toolkit::DevelControl::GetVisualResourceStatus()
183    */
184   Toolkit::Visual::ResourceStatus GetVisualResourceStatus( Property::Index index ) const;
185
186   /**
187    * @copydoc Dali::Toolkit::DevelControl::CreateTransition()
188    */
189   Dali::Animation CreateTransition( const Toolkit::TransitionData& transitionData );
190
191   /**
192    * @copydoc Dali::Toolkit::DevelControl::DoAction()
193    */
194   void DoAction( Dali::Property::Index visualIndex, Dali::Property::Index actionId, const Dali::Property::Value attributes );
195
196   /**
197    * @brief Function used to set control properties.
198    * @param[in] object The object whose property to set
199    * @param[in] index The index of the property to set
200    * @param[in] value The value of the property to set
201    */
202   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
203
204   /**
205    * @brief Function used to retrieve the value of control properties.
206    * @param[in] object The object whose property to get
207    * @param[in] index The index of the property to get
208    * @return The value of the property
209    */
210   static Property::Value GetProperty( BaseObject* object, Property::Index index );
211
212   /**
213    * @brief Sets the state of the control.
214    * @param[in] newState The state to set
215    * @param[in] withTransitions Whether to show a transition when changing to the new state
216    */
217   void SetState( DevelControl::State newState, bool withTransitions=true );
218
219   /**
220    * @brief Sets the sub-state of the control.
221    * @param[in] newState The sub-state to set
222    * @param[in] withTransitions Whether to show a transition when changing to the new sub-state
223    */
224   void SetSubState( const std::string& subStateName, bool withTransitions=true );
225
226   /**
227    * @brief Replaces visuals and properties from the old state to the new state.
228    * @param[in] oldState The old state
229    * @param[in] newState The new state
230    * @param[in] subState The current sub state
231    */
232   void ReplaceStateVisualsAndProperties( const StylePtr oldState, const StylePtr newState, const std::string& subState );
233
234   /**
235    * @brief Removes a visual from the control's container.
236    * @param[in] visuals The container of visuals
237    * @param[in] visualName The name of the visual to remove
238    */
239   void RemoveVisual( RegisteredVisualContainer& visuals, const std::string& visualName );
240
241   /**
242    * @brief Removes several visuals from the control's container.
243    * @param[in] visuals The container of visuals
244    * @param[in] removeVisuals The visuals to remove
245    */
246   void RemoveVisuals( RegisteredVisualContainer& visuals, DictionaryKeys& removeVisuals );
247
248   /**
249    * @brief Copies the visual properties that are specific to the control instance into the instancedProperties container.
250    * @param[in] visuals The control's visual container
251    * @param[out] instancedProperties The instanced properties are added to this container
252    */
253   void CopyInstancedProperties( RegisteredVisualContainer& visuals, Dictionary<Property::Map>& instancedProperties );
254
255   /**
256    * @brief On state change, ensures visuals are moved or created appropriately.
257    *
258    * Go through the list of visuals that are common to both states.
259    * If they are different types, or are both image types with different
260    * URLs, then the existing visual needs moving and the new visual needs creating
261    *
262    * @param[in] stateVisualsToChange The visuals to change
263    * @param[in] instancedProperties The instanced properties @see CopyInstancedProperties
264    */
265   void RecreateChangedVisuals( Dictionary<Property::Map>& stateVisualsToChange, Dictionary<Property::Map>& instancedProperties );
266
267   /**
268    * @brief Whether the resource is ready
269    * @return True if the resource is read.
270    */
271   bool IsResourceReady() const;
272
273   /**
274    * @copydoc CustomActorImpl::OnStageDisconnection()
275    */
276   void OnStageDisconnection();
277
278   /**
279    * @brief Sets the margin.
280    * @param[in] margin Margin is a collections of extent ( start, end, top, bottom )
281    */
282   void SetMargin( Extents margin );
283
284   /**
285    * @brief Returns the value of margin
286    * @return The value of margin
287    */
288   Extents GetMargin() const;
289
290   /**
291    * @brief Sets the padding.
292    * @param[in] padding Padding is a collections of extent ( start, end, top, bottom ).
293    */
294   void SetPadding( Extents padding );
295
296   /**
297    * @brief Returns the value of padding
298    * @return The value of padding
299    */
300   Extents GetPadding() const;
301
302 private:
303
304   /**
305    * Used as an alternative to boolean so that it is obvious whether a visual is enabled/disabled.
306    */
307   struct VisualState
308   {
309     enum Type
310     {
311       DISABLED = 0, ///< Visual disabled.
312       ENABLED = 1   ///< Visual enabled.
313     };
314   };
315
316   /**
317    * 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.
318    */
319   struct DepthIndexValue
320   {
321     enum Type
322     {
323       NOT_SET = 0, ///< Visual depth value not set by caller.
324       SET = 1      ///< Visual depth value set by caller.
325     };
326   };
327
328   /**
329    * @brief Adds the visual to the list of registered visuals.
330    * @param[in] index The Property index of the visual, used to reference visual
331    * @param[in,out] visual The visual to register, which can be altered in this function
332    * @param[in] enabled false if derived class wants to control when visual is set on stage
333    * @param[in] depthIndexValueSet Set to true if the depthIndex has actually been set manually
334    * @param[in] depthIndex The visual's depth-index is set to this
335    *
336    * @note Registering a visual with an index that already has a registered visual will replace it. The replacement will
337    *       occur once the replacement visual is ready (loaded).
338    */
339   void RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex = 0 );
340
341 public:
342
343   Control& mControlImpl;
344   DevelControl::State mState;
345   std::string mSubStateName;
346
347   int mLeftFocusableActorId;       ///< Actor ID of Left focusable control.
348   int mRightFocusableActorId;      ///< Actor ID of Right focusable control.
349   int mUpFocusableActorId;         ///< Actor ID of Up focusable control.
350   int mDownFocusableActorId;       ///< Actor ID of Down focusable control.
351
352   RegisteredVisualContainer mVisuals;     ///< Stores visuals needed by the control, non trivial type so std::vector used.
353   std::string mStyleName;
354   Vector4 mBackgroundColor;               ///< The color of the background visual
355   Vector3* mStartingPinchScale;           ///< The scale when a pinch gesture starts, TODO: consider removing this
356   Extents mMargin;                        ///< The margin values
357   Extents mPadding;                       ///< The padding values
358   Toolkit::Control::KeyEventSignalType mKeyEventSignal;
359   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
360   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
361   Toolkit::Control::ResourceReadySignalType mResourceReadySignal;
362
363   // Gesture Detection
364   PinchGestureDetector mPinchGestureDetector;
365   PanGestureDetector mPanGestureDetector;
366   TapGestureDetector mTapGestureDetector;
367   LongPressGestureDetector mLongPressGestureDetector;
368
369   // Tooltip
370   TooltipPtr mTooltip;
371
372   ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT;    ///< Flags passed in from constructor.
373   bool mIsKeyboardNavigationSupported :1;  ///< Stores whether keyboard navigation is supported by the control.
374   bool mIsKeyboardFocusGroup :1;           ///< Stores whether the control is a focus group.
375
376   RegisteredVisualContainer mRemoveVisuals;         ///< List of visuals that are being replaced by another visual once ready
377
378   // Properties - these need to be members of Internal::Control::Impl as they access private methods/data of Internal::Control and Internal::Control::Impl.
379   static const PropertyRegistration PROPERTY_1;
380   static const PropertyRegistration PROPERTY_2;
381   static const PropertyRegistration PROPERTY_3;
382   static const PropertyRegistration PROPERTY_4;
383   static const PropertyRegistration PROPERTY_5;
384   static const PropertyRegistration PROPERTY_6;
385   static const PropertyRegistration PROPERTY_7;
386   static const PropertyRegistration PROPERTY_8;
387   static const PropertyRegistration PROPERTY_9;
388   static const PropertyRegistration PROPERTY_10;
389   static const PropertyRegistration PROPERTY_11;
390   static const PropertyRegistration PROPERTY_12;
391   static const PropertyRegistration PROPERTY_13;
392   static const PropertyRegistration PROPERTY_14;
393 };
394
395
396 } // namespace Internal
397
398 } // namespace Toolkit
399
400 } // namespace Dali
401
402 #endif // DALI_TOOLKIT_CONTROL_DATA_IMPL_H