Changed Control::SetLayout to handle empty layouts
[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) 2018 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/layouting/layout-item-impl.h>
31 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
32 #include <dali-toolkit/internal/controls/tooltip/tooltip.h>
33 #include <dali-toolkit/internal/builder/style.h>
34 #include <dali-toolkit/internal/builder/dictionary.h>
35 #include <dali-toolkit/public-api/visuals/visual-properties.h>
36
37 namespace Dali
38 {
39
40 namespace Toolkit
41 {
42
43 namespace Internal
44 {
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), visual(aVisual), enabled(aEnabled), pending( aPendingReplacement )
58   {
59   }
60 };
61
62 typedef Dali::OwnerContainer< RegisteredVisual* > RegisteredVisualContainer;
63
64
65 /**
66  * @brief Holds the Implementation for the internal control class
67  */
68 class Control::Impl : public ConnectionTracker, public Visual::ResourceObserver
69 {
70
71 public:
72
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::ResourceObserver.
128    */
129   virtual void ResourceReady( Visual::Base& object );
130
131   /**
132    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
133    */
134   void RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual );
135
136   /**
137    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
138    */
139   void RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, int depthIndex );
140
141   /**
142    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
143    */
144   void RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled );
145
146   /**
147    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
148    */
149   void RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled, int depthIndex );
150
151   /**
152    * @copydoc Dali::Toolkit::DevelControl::UnregisterVisual()
153    */
154   void UnregisterVisual( Property::Index index );
155
156   /**
157    * @copydoc Dali::Toolkit::DevelControl::GetVisual()
158    */
159   Toolkit::Visual::Base GetVisual( Property::Index index ) const;
160
161   /**
162    * @copydoc Dali::Toolkit::DevelControl::EnableVisual()
163    */
164   void EnableVisual( Property::Index index, bool enable );
165
166   /**
167    * @copydoc Dali::Toolkit::DevelControl::IsVisualEnabled()
168    */
169   bool IsVisualEnabled( Property::Index index ) const;
170
171   /**
172    * @brief Stops observing the given visual.
173    * @param[in] visual The visual to stop observing
174    */
175   void StopObservingVisual( Toolkit::Visual::Base& visual );
176
177   /**
178    * @brief Starts observing the given visual.
179    * @param[in] visual The visual to start observing
180    */
181   void StartObservingVisual( Toolkit::Visual::Base& visual);
182
183   /**
184    * @copydoc Dali::Toolkit::DevelControl::GetVisualResourceStatus()
185    */
186   Toolkit::Visual::ResourceStatus GetVisualResourceStatus( Property::Index index ) const;
187
188   /**
189    * @param[in,out] animation Handle to existing animation, or an empty handle that
190    * can be set to a New animation if createAnimation is true
191    * @param[in] transitionData The transition data describing the animation
192    * @param[in] createAnimation True if the animation should be created
193    */
194   void AddTransitions( Dali::Animation& animation,
195                        const Toolkit::TransitionData& transitionData,
196                        bool createAnimation = false );
197
198   /**
199    * @copydoc Dali::Toolkit::DevelControl::CreateTransition()
200    */
201   Dali::Animation CreateTransition( const Toolkit::TransitionData& transitionData );
202
203   /**
204    * @copydoc Dali::Toolkit::DevelControl::DoAction()
205    */
206   void DoAction( Dali::Property::Index visualIndex, Dali::Property::Index actionId, const Dali::Property::Value attributes );
207
208   /**
209    * @brief Function used to set control properties.
210    * @param[in] object The object whose property to set
211    * @param[in] index The index of the property to set
212    * @param[in] value The value of the property to set
213    */
214   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
215
216   /**
217    * @brief Function used to retrieve the value of control properties.
218    * @param[in] object The object whose property to get
219    * @param[in] index The index of the property to get
220    * @return The value of the property
221    */
222   static Property::Value GetProperty( BaseObject* object, Property::Index index );
223
224   /**
225    * @brief Sets the state of the control.
226    * @param[in] newState The state to set
227    * @param[in] withTransitions Whether to show a transition when changing to the new state
228    */
229   void SetState( DevelControl::State newState, bool withTransitions=true );
230
231   /**
232    * @brief Sets the sub-state of the control.
233    * @param[in] newState The sub-state to set
234    * @param[in] withTransitions Whether to show a transition when changing to the new sub-state
235    */
236   void SetSubState( const std::string& subStateName, bool withTransitions=true );
237
238   /**
239    * @brief Replaces visuals and properties from the old state to the new state.
240    * @param[in] oldState The old state
241    * @param[in] newState The new state
242    * @param[in] subState The current sub state
243    */
244   void ReplaceStateVisualsAndProperties( const StylePtr oldState, const StylePtr newState, const std::string& subState );
245
246   /**
247    * @brief Removes a visual from the control's container.
248    * @param[in] visuals The container of visuals
249    * @param[in] visualName The name of the visual to remove
250    */
251   void RemoveVisual( RegisteredVisualContainer& visuals, const std::string& visualName );
252
253   /**
254    * @brief Removes several visuals from the control's container.
255    * @param[in] visuals The container of visuals
256    * @param[in] removeVisuals The visuals to remove
257    */
258   void RemoveVisuals( RegisteredVisualContainer& visuals, DictionaryKeys& removeVisuals );
259
260   /**
261    * @brief Copies the visual properties that are specific to the control instance into the instancedProperties container.
262    * @param[in] visuals The control's visual container
263    * @param[out] instancedProperties The instanced properties are added to this container
264    */
265   void CopyInstancedProperties( RegisteredVisualContainer& visuals, Dictionary<Property::Map>& instancedProperties );
266
267   /**
268    * @brief On state change, ensures visuals are moved or created appropriately.
269    *
270    * Go through the list of visuals that are common to both states.
271    * If they are different types, or are both image types with different
272    * URLs, then the existing visual needs moving and the new visual needs creating
273    *
274    * @param[in] stateVisualsToChange The visuals to change
275    * @param[in] instancedProperties The instanced properties @see CopyInstancedProperties
276    */
277   void RecreateChangedVisuals( Dictionary<Property::Map>& stateVisualsToChange, Dictionary<Property::Map>& instancedProperties );
278
279   /**
280    * @brief Whether the resource is ready
281    * @return True if the resource is read.
282    */
283   bool IsResourceReady() const;
284
285   /**
286    * @copydoc CustomActorImpl::OnStageDisconnection()
287    */
288   void OnStageDisconnection();
289
290   /**
291    * @brief Sets the margin.
292    * @param[in] margin Margin is a collections of extent ( start, end, top, bottom )
293    */
294   void SetMargin( Extents margin );
295
296   /**
297    * @brief Returns the value of margin
298    * @return The value of margin
299    */
300   Extents GetMargin() const;
301
302   /**
303    * @brief Sets the padding.
304    * @param[in] padding Padding is a collections of extent ( start, end, top, bottom ).
305    */
306   void SetPadding( Extents padding );
307
308   /**
309    * @brief Returns the value of padding
310    * @return The value of padding
311    */
312   Extents GetPadding() const;
313
314   /**
315    * @brief Set the input method context.
316    * @param[in] inputMethodContext The input method context.
317    */
318   void SetInputMethodContext( InputMethodContext& inputMethodContext );
319
320   /**
321    * @brief Filter an key event.
322    * @param[in] event The key to be filtered.
323    * @return True if the key handled, otherwise false.
324    */
325   bool FilterKeyEvent( const KeyEvent& event );
326
327   /**
328    * @brief Get the layout associated with this control, if any.
329    *
330    * @return A pointer to the layout, or NULL.
331    */
332   Toolkit::Internal::LayoutItemPtr GetLayout() const;
333
334   /**
335    * @brief Set the layout on this control.
336    * @param[in] layout Pointer to the layout
337    */
338   void SetLayout( Toolkit::Internal::LayoutItem& layout );
339
340   /**
341    * @brief Remove the layout from this control
342    *
343    * @note This does not remove any children from this control, nor does it strip
344    * layouts from them but it does remove them from the layout hierarchy.
345    */
346   void RemoveLayout();
347
348 private:
349
350   /**
351    * Used as an alternative to boolean so that it is obvious whether a visual is enabled/disabled.
352    */
353   struct VisualState
354   {
355     enum Type
356     {
357       DISABLED = 0, ///< Visual disabled.
358       ENABLED = 1   ///< Visual enabled.
359     };
360   };
361
362   /**
363    * 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.
364    */
365   struct DepthIndexValue
366   {
367     enum Type
368     {
369       NOT_SET = 0, ///< Visual depth value not set by caller.
370       SET = 1      ///< Visual depth value set by caller.
371     };
372   };
373
374   /**
375    * @brief Adds the visual to the list of registered visuals.
376    * @param[in] index The Property index of the visual, used to reference visual
377    * @param[in,out] visual The visual to register, which can be altered in this function
378    * @param[in] enabled false if derived class wants to control when visual is set on stage
379    * @param[in] depthIndexValueSet Set to true if the depthIndex has actually been set manually
380    * @param[in] depthIndex The visual's depth-index is set to this
381    *
382    * @note Registering a visual with an index that already has a registered visual will replace it. The replacement will
383    *       occur once the replacement visual is ready (loaded).
384    */
385   void RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex = 0 );
386
387 public:
388
389   Control& mControlImpl;
390   DevelControl::State mState;
391   std::string mSubStateName;
392
393   // Layout
394   Toolkit::Internal::LayoutItemPtr mLayout;
395
396   int mLeftFocusableActorId;       ///< Actor ID of Left focusable control.
397   int mRightFocusableActorId;      ///< Actor ID of Right focusable control.
398   int mUpFocusableActorId;         ///< Actor ID of Up focusable control.
399   int mDownFocusableActorId;       ///< Actor ID of Down focusable control.
400
401   RegisteredVisualContainer mVisuals;     ///< Stores visuals needed by the control, non trivial type so std::vector used.
402   std::string mStyleName;
403   Vector4 mBackgroundColor;               ///< The color of the background visual
404   Vector3* mStartingPinchScale;           ///< The scale when a pinch gesture starts, TODO: consider removing this
405   Extents mMargin;                        ///< The margin values
406   Extents mPadding;                       ///< The padding values
407   Toolkit::Control::KeyEventSignalType mKeyEventSignal;
408   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
409   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
410   Toolkit::Control::ResourceReadySignalType mResourceReadySignal;
411
412   // Gesture Detection
413   PinchGestureDetector mPinchGestureDetector;
414   PanGestureDetector mPanGestureDetector;
415   TapGestureDetector mTapGestureDetector;
416   LongPressGestureDetector mLongPressGestureDetector;
417
418   // Tooltip
419   TooltipPtr mTooltip;
420
421   InputMethodContext mInputMethodContext;
422
423   ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT;    ///< Flags passed in from constructor.
424   bool mIsKeyboardNavigationSupported :1;  ///< Stores whether keyboard navigation is supported by the control.
425   bool mIsKeyboardFocusGroup :1;           ///< Stores whether the control is a focus group.
426
427   RegisteredVisualContainer mRemoveVisuals;         ///< List of visuals that are being replaced by another visual once ready
428
429
430   // Properties - these need to be members of Internal::Control::Impl as they access private methods/data of Internal::Control and Internal::Control::Impl.
431   static const PropertyRegistration PROPERTY_1;
432   static const PropertyRegistration PROPERTY_2;
433   static const PropertyRegistration PROPERTY_3;
434   static const PropertyRegistration PROPERTY_4;
435   static const PropertyRegistration PROPERTY_5;
436   static const PropertyRegistration PROPERTY_6;
437   static const PropertyRegistration PROPERTY_7;
438   static const PropertyRegistration PROPERTY_8;
439   static const PropertyRegistration PROPERTY_9;
440   static const PropertyRegistration PROPERTY_10;
441   static const PropertyRegistration PROPERTY_11;
442   static const PropertyRegistration PROPERTY_12;
443   static const PropertyRegistration PROPERTY_13;
444   static const PropertyRegistration PROPERTY_14;
445 };
446
447
448 } // namespace Internal
449
450 } // namespace Toolkit
451
452 } // namespace Dali
453
454 #endif // DALI_TOOLKIT_CONTROL_DATA_IMPL_H