Set focusable to true when touched in default.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / button-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_BUTTON_H
2 #define DALI_TOOLKIT_INTERNAL_BUTTON_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/public-api/adaptor-framework/timer.h>
23 #include <dali/public-api/animation/animation.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
27 #include <dali-toolkit/devel-api/controls/control-devel.h>
28 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
29 #include <dali-toolkit/public-api/controls/control-impl.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 class Button;
36
37 namespace Internal
38 {
39 /**
40  * @copydoc Toolkit::Button
41  *
42  * Button is the base class implementation for all buttons.
43  *
44  * @note
45  *
46  * All Foreground/Icon visuals expected to be the same size.
47  * Background visuals will take the size of the control.
48  * Padding and struts take size precedence over visuals when available space is limited.
49  * Icon/Foreground visuals take size precedence over Labels when available space is limited.
50  */
51 class Button : public Control
52 {
53 public:
54   /**
55    * Enum describing the position the text label can be in relation to the control (and foreground/icon)
56    */
57   enum Align
58   {
59     BEGIN, // At the start of the control before the foreground/icon
60     END,   // At the end of the control after the foreground/icon
61     TOP,   // At the top of the control above the foreground/icon
62     BOTTOM // At the bottom of the control below the foreground/icon
63   };
64
65 public:
66   /**
67    * @brief Sets the button as \e disabled.
68    * @param[in] disabled Disabled property
69    */
70   void SetDisabled(bool disabled);
71
72   /**
73    * @brief Returns if the button is disabled.
74    * @return \e true if the button is \e disabled
75    */
76   bool IsDisabled() const;
77
78   /**
79    * @brief Sets the \e autorepeating property.
80    * @param[in] autoRepeating \e autorepeating property
81    */
82   void SetAutoRepeating(bool autoRepeating);
83
84   /**
85    * @brief Sets the initial autorepeating delay.
86    * @param[in] initialAutoRepeatingDelay in seconds
87    */
88   void SetInitialAutoRepeatingDelay(float initialAutoRepeatingDelay);
89
90   /**
91    * @brief Sets the next autorepeating delay.
92    * @param[in] nextAutoRepeatingDelay in seconds
93    */
94   void SetNextAutoRepeatingDelay(float nextAutoRepeatingDelay);
95
96   /**
97    * @brief Sets the \e togglable property.
98    * @param[in] togglable Togglable property
99    */
100   void SetTogglableButton(bool togglable);
101
102   /**
103    * @brief Sets the button as selected or unselected.
104    * @param[in] selected Selected property
105    */
106   void SetSelected(bool selected);
107
108   /**
109    * @brief Returns if the selected property is set and the button is togglable.
110    * @return \e true if the button is \e selected
111    */
112   bool IsSelected() const;
113
114   /**
115    * @brief Produces a Property::Map of Text properties to create a Text Visual, merging existing properties with supplied map
116    * If the label does not exist yet, it is created.
117    * The derived buttons are notified if any properties are changed.
118    * @param[in] properties A Property::Map of key-value pairs of properties to set.
119    * @param[out] properties A Property::Map of text visual properties to set after merging inMap with existing maps
120    */
121   void MergeWithExistingLabelProperties(const Property::Map& inMap, Property::Map& outMap);
122
123   /**
124    * Performs actions as requested using the action name.
125    * @param[in] object The object on which to perform the action.
126    * @param[in] actionName The action to perform.
127    * @param[in] attributes The attributes with which to perfrom this action.
128    * @return true if action has been accepted by this control
129    */
130   static bool DoAction(BaseObject* object, const std::string& actionName, const Property::Map& attributes);
131
132 public:
133   /**
134    * Button's state
135    */
136   enum State
137   {
138     UNSELECTED_STATE,          ///< The button is unselected.
139     SELECTED_STATE,            ///< The button is selected.
140     DISABLED_UNSELECTED_STATE, ///< The button is disabled and unselected.
141     DISABLED_SELECTED_STATE,   ///< The button is disabled and selected.
142     STATE_COUNT,               ///< Number of States
143   };
144
145   /**
146    * Enum to distinguish the different style-able components of the button
147    */
148   enum Visuals
149   {
150     UNSELECTED_FOREGROUND = 0,
151     SELECTED_FOREGROUND,
152     DISABLED_SELECTED_FOREGROUND,
153     DISABLED_UNSELECTED_FOREGROUND,
154     UNSELECTED_BACKGROUND,
155     SELECTED_BACKGROUND,
156     DISABLED_UNSELECTED_BACKGROUND,
157     DISABLED_SELECTED_BACKGROUND,
158     VISUALS_COUNT
159   };
160
161   /**
162    * Enum to list types of visual a state can have.
163    */
164   enum VisualState
165   {
166     BACKGROUND = 0,
167     FOREGROUND,
168     VISUAL_STATE_COUNT
169   };
170
171 protected:
172   /**
173    * Button press state which is not the same as the actual button's state.
174    * For example An UNSELECTED button can be DEPRESSED, but until released, the actual button state doesn't change to SELECTED
175    */
176   enum PressState
177   {
178     DEPRESSED,        ///< The button is up.
179     UNPRESSED,        ///< The button is down.
180     TOGGLE_DEPRESSED, ///< The button has been pressed down and will stay depressed when released.
181   };
182
183   /**
184    * Construct a new Button.
185    */
186   Button();
187
188   /**
189    * A reference counted object may only be deleted by calling Unreference()
190    */
191   virtual ~Button();
192   /**
193    * @return A reference to the label actor.
194    */
195   Actor& GetLabelActor();
196
197   /**
198    * @return A reference to the unselected button image.
199    */
200   Actor GetUnselectedImage();
201
202   /**
203    * @return A reference to the selected image.
204    */
205   Actor GetSelectedImage();
206
207 private:
208   /**
209    * Perform the click action to click the button.
210    * @param[in] attributes The attributes to perfrom this action.
211    * @return true if this control can perform action.
212    */
213   bool DoClickAction(const Property::Map& attributes);
214
215   /**
216    * This method is called when the button is a Toggle button and released
217    * Could be reimplemented in subclasses to provide specific behaviour.
218    * @return bool returns true if state changed.
219    */
220   virtual bool OnToggleReleased();
221
222   /**
223    * This method is called when touch leaves the boundary of the button or several touch points are received.
224    * Could be reimplemented in subclasses to provide specific behaviour.
225    */
226   virtual void OnTouchPointLeave();
227
228   /**
229    * This method is called when the touch is interrupted.
230    * Could be reimplemented in subclasses to provide specific behaviour.
231    */
232   virtual void OnTouchPointInterrupted();
233
234   /**
235    * This method is called when the \e selected property is changed.
236    */
237   virtual void OnStateChange(State newState)
238   {
239   }
240
241   /**
242    * This method is called when the \e disabled property is changed.
243    */
244   virtual void OnDisabled()
245   {
246   }
247
248   /**
249    * This method is called when the button is pressed.
250    */
251   virtual void OnPressed()
252   {
253   }
254
255   /**
256    * This method is called when the button is released.
257    */
258   virtual void OnReleased()
259   {
260   }
261
262 public:
263   /**
264    * @copydoc Dali::Toolkit::PushButton::PressedSignal()
265    */
266   Toolkit::Button::ButtonSignalType& PressedSignal();
267
268   /**
269    * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
270    */
271   Toolkit::Button::ButtonSignalType& ReleasedSignal();
272
273   /**
274    * @copydoc Dali::Toolkit::Button::ClickedSignal()
275    */
276   Toolkit::Button::ButtonSignalType& ClickedSignal();
277
278   /**
279    * @copydoc Dali::Toolkit::Button::StateChangedSignal()
280    */
281   Toolkit::Button::ButtonSignalType& StateChangedSignal();
282
283   /**
284    * Connects a callback function with the object's signals.
285    * @param[in] object The object providing the signal.
286    * @param[in] tracker Used to disconnect the signal.
287    * @param[in] signalName The signal to connect to.
288    * @param[in] functor A newly allocated FunctorDelegate.
289    * @return True if the signal was connected.
290    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
291    */
292   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
293
294   // Properties
295
296   /**
297    * Called when a property of an object of this type is set.
298    * @param[in] object The object whose property is set.
299    * @param[in] index The property index.
300    * @param[in] value The new property value.
301    */
302   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
303
304   /**
305    * Called to retrieve a property of an object of this type.
306    * @param[in] object The object whose property is to be retrieved.
307    * @param[in] index The property index.
308    * @return The current value of the property.
309    */
310   static Property::Value GetProperty(BaseObject* object, Property::Index propertyIndex);
311
312 protected: // From Control
313   /**
314    * @copydoc Toolkit::Control::OnInitialize()
315    * @note If overridden by deriving button classes, then an up-call to Button::OnInitialize MUST be made at the start.
316    */
317   void OnInitialize() override;
318
319   /**
320    * @copydoc Toolkit::Control::OnAccessibilityActivated()
321    */
322   bool OnAccessibilityActivated() override;
323
324   /**
325    * @copydoc Toolkit::Control::OnKeyboardEnter()
326    */
327   bool OnKeyboardEnter() override;
328
329   /**
330    * @copydoc Toolkit::Control::OnSceneDisconnection()
331    * @note If overridden by deriving button classes, then an up-call to Button::OnSceneDisconnection MUST be made at the end.
332    */
333   void OnSceneDisconnection() override;
334
335   /**
336    * @copydoc Toolkit::Control::OnSceneConnection()
337    */
338   void OnSceneConnection(int depth) override;
339
340   /**
341    * @copydoc Toolkit::Control::GetNaturalSize
342    */
343   Vector3 GetNaturalSize() override;
344
345   /**
346    * @copydoc Toolkit::Control::OnSetResizePolicy
347    */
348   void OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension) override;
349
350   /**
351    * @copydoc Toolkit::Control::OnRelayout
352    */
353   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
354
355 private:
356   /**
357    * @brief Handler for touch data
358    * @param[in]  actor  The touched actor.
359    * @param[in]  touch  The touch info.
360    * @return true, if consumed, false otherwise.
361    */
362   bool OnTouch(Actor actor, const TouchEvent& touch);
363
364   /**
365    * Handler for tap events.
366    * We do not actually do anything when we receive a tap as the button handles tap event through
367    * the touch event system itself as it requires more than just tap handling (e.g. leave events).
368    * This stops any of our parents receiving a tap gesture when it occurs within our area.
369    * @param[in]  actor  The tapped actor.
370    * @param[in]  tap    The tap gesture.
371    */
372   void OnTap(Actor actor, const TapGesture& tap);
373
374   /**
375    * Sets up the autorepeating timer.
376    * @param[in] delay The delay time in seconds.
377    */
378   void SetUpTimer(float delay);
379
380   /**
381    * Button has been pressed
382    */
383   void Pressed();
384
385   /**
386    * This method is called the button is down.
387    */
388   void ButtonDown();
389
390   /**
391    * This method is called when the button is up.
392    */
393   void ButtonUp();
394
395   /**
396    * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
397    */
398   bool AutoRepeatingSlot();
399
400   /**
401    *  Check the requested state is an allowed transition.
402    *  Some states can not be transitioned to from certain states.
403    *  @param[in] requestedState check if can transition to this state
404    *  @return bool true if state change valid
405    */
406   bool ValidateState(State requestedState);
407
408   /**
409    * Changes the button state when an action occurs on it
410    * @param[in] requestedState the state to change to
411    */
412   void ChangeState(State requestedState);
413
414   /**
415    * This method is called when the button is released.
416    */
417   void Released();
418
419 protected:
420   /**
421    * Set Text Label Padding
422    * @param[in] padding BEGIN END BOTTOM TOP
423    */
424   void SetLabelPadding(const Padding& padding);
425
426   /**
427    * Get Text Label padding
428    * @return Padding
429    */
430   Padding GetLabelPadding();
431
432   /**
433    * Set Foreground/icon Padding
434    * @param[in] padding BEGIN END BOTTOM TOP
435    */
436   void SetForegroundPadding(const Padding& padding);
437
438   /**
439    * Get Foreground padding
440    * @ return Padding
441    */
442   Padding GetForegroundPadding();
443
444   /**
445    * @brief Setup the button components for example foregrounds and background
446    * @param[in] index the index of the visual to set
447    * @param[in] value the value to set on the component
448    * @param[in] visualDepth the depth of the visual if overlapping another
449    */
450   void CreateVisualsForComponent(Property::Index index, const Property::Value& value, const int visualDepth);
451
452   /**
453    * @brief Get the Property map for the given Visual
454    * @param[in] visualIndex visual index of the required visual
455    * @param[out] retreivedMap the property map used to construct the required visual
456    * @return bool success flag, true if visual found
457    */
458   bool GetPropertyMapForVisual(Property::Index visualIndex, Property::Map& retreivedMap) const;
459   /**
460    * Returns the animation to be used for transition, creating the animation if needed.
461    * @return The initialised transition animation.
462    */
463   Dali::Animation GetTransitionAnimation();
464
465   /**
466    * @brief Set the position of the label relative to foreground/icon, if both present
467    * @param[in] labelAlignment given alignment setting
468    */
469   void SetLabelAlignment(Align labelAlignment);
470
471   /**
472    * @brief Get set alignment of label in relation to foreground/icon
473    * @return Set alignment value
474    */
475   Align GetLabelAlignment();
476
477   /**
478    * Removes the visual from the button (un-staged)
479    * If the derived button does not want the visual removed then use this virtual function to
480    * define the required behaviour.
481    * Can decide to only remove specified visuals via index
482    */
483   virtual void OnButtonVisualRemoval(Property::Index visualIndex);
484
485 private:
486   /**
487    * Removes the visual from the button and prepares it to be transitioned out
488    * @param[in] visualIndex the visual to remove
489    */
490   void RemoveVisual(Property::Index visualIndex);
491
492   /**
493    * Adds the required visual to the button.
494    * @param[in] visualIndex The Property index of the visual required
495    */
496   void SelectRequiredVisual(Property::Index visualIndex);
497
498   // Undefined
499   Button(const Button&);
500
501   // Undefined
502   Button& operator=(const Button&);
503
504 private:
505   // Signals
506   Toolkit::Button::ButtonSignalType mPressedSignal;      ///< Signal emitted when the button is pressed.
507   Toolkit::Button::ButtonSignalType mReleasedSignal;     ///< Signal emitted when the button is released.
508   Toolkit::Button::ButtonSignalType mClickedSignal;      ///< Signal emitted when the button is clicked.
509   Toolkit::Button::ButtonSignalType mStateChangedSignal; ///< Signal emitted when the button's state is changed.
510
511   Timer mAutoRepeatingTimer;
512
513   Actor   mLabel;             ///< Stores the button text label.
514   Padding mLabelPadding;      ///< The padding around the label (if present).
515   Padding mForegroundPadding; ///< The padding around the foreground/icon visual (if present).
516
517   Align mTextLabelAlignment; ///< Position of text label in relation to foreground/icon when both are present.
518
519   TapGestureDetector mTapDetector;
520
521   bool mAutoRepeating;     ///< Stores the autorepeating property.
522   bool mTogglableButton;   ///< Stores the togglable property as a flag.
523   bool mTextStringSetFlag; ///< Stores if text has been set. Required in relayout but don't want to calculate there.
524
525   float mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
526   float mNextAutoRepeatingDelay;    ///< Stores the next autorepeating delay in seconds.
527
528   float mAnimationTime;
529
530   PressState mButtonPressedState; ///< In relation to the button being pressed/released
531   State      mButtonState;
532   State      mPreviousButtonState; ///< During a transition between two states, this stores the previous state so Visuals can be removed.
533
534   // Actions
535   bool mClickActionPerforming; ///< Used to manage signal emissions during action
536
537 protected:
538   struct AccessibleImpl : public DevelControl::AccessibleImpl
539   {
540     using DevelControl::AccessibleImpl::AccessibleImpl;
541
542     Dali::Accessibility::States CalculateStates() override;
543     std::string                 GetNameRaw() override;
544     Property::Index             GetNamePropertyIndex() override;
545   };
546 };
547
548 } // namespace Internal
549
550 // Helpers for public-api forwarding methods
551
552 inline Toolkit::Internal::Button& GetImplementation(Toolkit::Button& button)
553 {
554   DALI_ASSERT_ALWAYS(button);
555
556   Dali::RefObject& handle = button.GetImplementation();
557
558   return static_cast<Toolkit::Internal::Button&>(handle);
559 }
560
561 inline const Toolkit::Internal::Button& GetImplementation(const Toolkit::Button& button)
562 {
563   DALI_ASSERT_ALWAYS(button);
564
565   const Dali::RefObject& handle = button.GetImplementation();
566
567   return static_cast<const Toolkit::Internal::Button&>(handle);
568 }
569
570 } // namespace Toolkit
571
572 } // namespace Dali
573
574 #endif // DALI_TOOLKIT_INTERNAL_BUTTON_H