[AT-SPI] Remove SetAccessibilityConstructor()
[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::Internal::Control::CreateAccessibleObject()
326    */
327   DevelControl::ControlAccessible* CreateAccessibleObject() override;
328
329   /**
330    * @copydoc Toolkit::Control::OnKeyboardEnter()
331    */
332   bool OnKeyboardEnter() override;
333
334   /**
335    * @copydoc Toolkit::Control::OnSceneDisconnection()
336    * @note If overridden by deriving button classes, then an up-call to Button::OnSceneDisconnection MUST be made at the end.
337    */
338   void OnSceneDisconnection() override;
339
340   /**
341    * @copydoc Toolkit::Control::OnSceneConnection()
342    */
343   void OnSceneConnection(int depth) override;
344
345   /**
346    * @copydoc Toolkit::Control::GetNaturalSize
347    */
348   Vector3 GetNaturalSize() override;
349
350   /**
351    * @copydoc Toolkit::Control::OnSetResizePolicy
352    */
353   void OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension) override;
354
355   /**
356    * @copydoc Toolkit::Control::OnRelayout
357    */
358   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
359
360 private:
361   /**
362    * @brief Handler for touch data
363    * @param[in]  actor  The touched actor.
364    * @param[in]  touch  The touch info.
365    * @return true, if consumed, false otherwise.
366    */
367   bool OnTouch(Actor actor, const TouchEvent& touch);
368
369   /**
370    * Handler for tap events.
371    * We do not actually do anything when we receive a tap as the button handles tap event through
372    * the touch event system itself as it requires more than just tap handling (e.g. leave events).
373    * This stops any of our parents receiving a tap gesture when it occurs within our area.
374    * @param[in]  actor  The tapped actor.
375    * @param[in]  tap    The tap gesture.
376    */
377   void OnTap(Actor actor, const TapGesture& tap);
378
379   /**
380    * Sets up the autorepeating timer.
381    * @param[in] delay The delay time in seconds.
382    */
383   void SetUpTimer(float delay);
384
385   /**
386    * Button has been pressed
387    */
388   void Pressed();
389
390   /**
391    * This method is called the button is down.
392    */
393   void ButtonDown();
394
395   /**
396    * This method is called when the button is up.
397    */
398   void ButtonUp();
399
400   /**
401    * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
402    */
403   bool AutoRepeatingSlot();
404
405   /**
406    *  Check the requested state is an allowed transition.
407    *  Some states can not be transitioned to from certain states.
408    *  @param[in] requestedState check if can transition to this state
409    *  @return bool true if state change valid
410    */
411   bool ValidateState(State requestedState);
412
413   /**
414    * Changes the button state when an action occurs on it
415    * @param[in] requestedState the state to change to
416    */
417   void ChangeState(State requestedState);
418
419   /**
420    * This method is called when the button is released.
421    */
422   void Released();
423
424 protected:
425   /**
426    * Set Text Label Padding
427    * @param[in] padding BEGIN END BOTTOM TOP
428    */
429   void SetLabelPadding(const Padding& padding);
430
431   /**
432    * Get Text Label padding
433    * @return Padding
434    */
435   Padding GetLabelPadding();
436
437   /**
438    * Set Foreground/icon Padding
439    * @param[in] padding BEGIN END BOTTOM TOP
440    */
441   void SetForegroundPadding(const Padding& padding);
442
443   /**
444    * Get Foreground padding
445    * @ return Padding
446    */
447   Padding GetForegroundPadding();
448
449   /**
450    * @brief Setup the button components for example foregrounds and background
451    * @param[in] index the index of the visual to set
452    * @param[in] value the value to set on the component
453    * @param[in] visualDepth the depth of the visual if overlapping another
454    */
455   void CreateVisualsForComponent(Property::Index index, const Property::Value& value, const int visualDepth);
456
457   /**
458    * @brief Get the Property map for the given Visual
459    * @param[in] visualIndex visual index of the required visual
460    * @param[out] retreivedMap the property map used to construct the required visual
461    * @return bool success flag, true if visual found
462    */
463   bool GetPropertyMapForVisual(Property::Index visualIndex, Property::Map& retreivedMap) const;
464   /**
465    * Returns the animation to be used for transition, creating the animation if needed.
466    * @return The initialised transition animation.
467    */
468   Dali::Animation GetTransitionAnimation();
469
470   /**
471    * @brief Set the position of the label relative to foreground/icon, if both present
472    * @param[in] labelAlignment given alignment setting
473    */
474   void SetLabelAlignment(Align labelAlignment);
475
476   /**
477    * @brief Get set alignment of label in relation to foreground/icon
478    * @return Set alignment value
479    */
480   Align GetLabelAlignment();
481
482   /**
483    * Removes the visual from the button (un-staged)
484    * If the derived button does not want the visual removed then use this virtual function to
485    * define the required behaviour.
486    * Can decide to only remove specified visuals via index
487    */
488   virtual void OnButtonVisualRemoval(Property::Index visualIndex);
489
490 private:
491   /**
492    * Removes the visual from the button and prepares it to be transitioned out
493    * @param[in] visualIndex the visual to remove
494    */
495   void RemoveVisual(Property::Index visualIndex);
496
497   /**
498    * Adds the required visual to the button.
499    * @param[in] visualIndex The Property index of the visual required
500    */
501   void SelectRequiredVisual(Property::Index visualIndex);
502
503   // Undefined
504   Button(const Button&);
505
506   // Undefined
507   Button& operator=(const Button&);
508
509 private:
510   // Signals
511   Toolkit::Button::ButtonSignalType mPressedSignal;      ///< Signal emitted when the button is pressed.
512   Toolkit::Button::ButtonSignalType mReleasedSignal;     ///< Signal emitted when the button is released.
513   Toolkit::Button::ButtonSignalType mClickedSignal;      ///< Signal emitted when the button is clicked.
514   Toolkit::Button::ButtonSignalType mStateChangedSignal; ///< Signal emitted when the button's state is changed.
515
516   Timer mAutoRepeatingTimer;
517
518   Actor   mLabel;             ///< Stores the button text label.
519   Padding mLabelPadding;      ///< The padding around the label (if present).
520   Padding mForegroundPadding; ///< The padding around the foreground/icon visual (if present).
521
522   Align mTextLabelAlignment; ///< Position of text label in relation to foreground/icon when both are present.
523
524   TapGestureDetector mTapDetector;
525
526   bool mAutoRepeating;     ///< Stores the autorepeating property.
527   bool mTogglableButton;   ///< Stores the togglable property as a flag.
528   bool mTextStringSetFlag; ///< Stores if text has been set. Required in relayout but don't want to calculate there.
529
530   float mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
531   float mNextAutoRepeatingDelay;    ///< Stores the next autorepeating delay in seconds.
532
533   float mAnimationTime;
534
535   PressState mButtonPressedState; ///< In relation to the button being pressed/released
536   State      mButtonState;
537   State      mPreviousButtonState; ///< During a transition between two states, this stores the previous state so Visuals can be removed.
538
539   // Actions
540   bool mClickActionPerforming; ///< Used to manage signal emissions during action
541
542 protected:
543   class ButtonAccessible : public DevelControl::ControlAccessible
544   {
545   public:
546     using DevelControl::ControlAccessible::ControlAccessible;
547
548     /**
549      * @copydoc Dali::Toolkit::DevelControl::ControlAccessible::CalculateStates()
550      */
551     Dali::Accessibility::States CalculateStates() override;
552
553     /**
554      * @copydoc Dali::Toolkit::DevelControl::ControlAccessible::GetNameRaw()
555      */
556     std::string GetNameRaw() const override;
557
558     /**
559      * @copydoc Dali::Toolkit::DevelControl::ControlAccessible::GetNamePropertyIndex()
560      */
561     Property::Index GetNamePropertyIndex() override;
562   };
563 };
564
565 } // namespace Internal
566
567 // Helpers for public-api forwarding methods
568
569 inline Toolkit::Internal::Button& GetImplementation(Toolkit::Button& button)
570 {
571   DALI_ASSERT_ALWAYS(button);
572
573   Dali::RefObject& handle = button.GetImplementation();
574
575   return static_cast<Toolkit::Internal::Button&>(handle);
576 }
577
578 inline const Toolkit::Internal::Button& GetImplementation(const Toolkit::Button& button)
579 {
580   DALI_ASSERT_ALWAYS(button);
581
582   const Dali::RefObject& handle = button.GetImplementation();
583
584   return static_cast<const Toolkit::Internal::Button&>(handle);
585 }
586
587 } // namespace Toolkit
588
589 } // namespace Dali
590
591 #endif // DALI_TOOLKIT_INTERNAL_BUTTON_H