322285b1dcf0d87fba8b2b3b7ddef60dd81fbb09
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / toggle-button-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_TOGGLE_BUTTON_H
2 #define DALI_TOOLKIT_INTERNAL_TOGGLE_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/common/vector-wrapper.h>
23 #include <dali/public-api/object/property-array.h>
24 #include <dali/public-api/object/property-value.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/devel-api/controls/buttons/toggle-button.h>
28 #include "button-impl.h"
29
30 namespace Dali
31 {
32 namespace Toolkit
33 {
34 namespace Internal
35 {
36 /**
37  * ToggleButton implementation class.
38  *
39  * \sa Dali::Toolkit::ToggleButton
40  */
41 class ToggleButton : public Button
42 {
43 public:
44   /**
45    * Create a new ToggleButton.
46    * @return A smart-pointer to the newly allocated ToggleButton.
47    */
48   static Dali::Toolkit::ToggleButton New();
49
50 protected:
51   /**
52    * Construct a new ToggleButton.
53    */
54   ToggleButton();
55
56   /**
57    * A reference counted object may only be deleted by calling Unreference()
58    */
59   virtual ~ToggleButton();
60
61 public:
62   /**
63    * Called when a property of an object of this type is set.
64    * @param[in] object The object whose property is set.
65    * @param[in] index The property index.
66    * @param[in] value The new property value.
67    */
68   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
69
70   /**
71    * Called to retrieve a property of an object of this type.
72    * @param[in] object The object whose property is to be retrieved.
73    * @param[in] index The property index.
74    * @return The current value of the property.
75    */
76   static Property::Value GetProperty(BaseObject* object, Property::Index propertyIndex);
77
78 private:
79   /**
80    * Called to create all toggle visuals and save them to mToggleVisuals.
81    * @param[in] states The array store toggle states.
82    * @param[out] visuals The created state visual vector.
83    */
84   void CreateVisualsForAllStates(const Property::Array& states, std::vector<Toolkit::Visual::Base>& visuals);
85
86   /**
87    * Called to set toggle states when TOGGLE_STATES is set in SetProperty function.
88    * @param[in] states The array store toggle states.
89    */
90   void SetToggleStates(const Property::Array& states);
91
92   /**
93    * Called to retrieve toggle states.
94    * @return The toggle states array.
95    */
96   Property::Array GetToggleStates() const;
97
98   /**
99    * Called to set toggle tooltips when TOGGLE_TIPS is set in SetProperty function.
100    * @param[in] tips The array store toggle tips.
101    */
102   void SetToggleTooltips(std::vector<std::string>& tips);
103
104   /**
105    * Called to retrieve toggle tips.
106    * @return The toggle tips array.
107    */
108   const std::vector<std::string>& GetToggleTooltips() const;
109
110   /**
111    * Called to prepare visual for next state.
112    * @param[in] index The property index to set.
113    * @param[in] visual The visual to set.
114    */
115   void PrepareVisual(Property::Index index, Toolkit::Visual::Base& visual);
116
117   /**
118    * Called to relayout visual.
119    * @param[in] index The index of visual to relayout.
120    * @param[in] size The size of control.
121    */
122   void RelayoutVisual(Property::Index index, const Vector2& size);
123
124 private: // From Button
125   /**
126    * @copydoc Toolkit::Internal::Button::OnInitialize
127    */
128   void OnInitialize() override;
129
130   /**
131    * @copydoc Toolkit::Internal::Button::OnRelayout
132    */
133   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
134
135   /**
136    * This method is called when the button is pressed.
137    */
138   void OnPressed() override;
139
140 private:
141   // Undefined
142   ToggleButton(const ToggleButton&);
143
144   // Undefined
145   ToggleButton& operator=(const ToggleButton&);
146
147 private:
148   Property::Array                    mToggleStates;                  ///< Toggle states, string or map.
149   std::vector<Toolkit::Visual::Base> mToggleVisuals;                 ///< Save all unselected visuals.
150   std::vector<Toolkit::Visual::Base> mToggleSelectedVisuals;         ///< Save all selected visuals.
151   std::vector<Toolkit::Visual::Base> mToggleDisabledVisuals;         ///< Save all disabled unselected visuals.
152   std::vector<Toolkit::Visual::Base> mToggleDisabledSelectedVisuals; ///< Save all disabled selected visuals.
153   std::vector<std::string>           mToggleTooltips;                ///< Toggle tooltips.
154   unsigned int                       mCurrentToggleIndex;            ///< The index of state.
155 protected:
156   struct AccessibleImpl : public Button::AccessibleImpl
157   {
158     using Button::AccessibleImpl::AccessibleImpl;
159
160     Dali::Accessibility::States CalculateStates() override;
161     std::string                 GetDescriptionRaw() override;
162     Property::Index             GetDescriptionPropertyIndex() override;
163   };
164   void OnStateChange(State newState) override;
165 };
166
167 } // namespace Internal
168
169 // Helpers for public-api forwarding methods
170
171 inline Toolkit::Internal::ToggleButton& GetImplementation(Toolkit::ToggleButton& button)
172 {
173   DALI_ASSERT_ALWAYS(button);
174
175   Dali::RefObject& handle = button.GetImplementation();
176
177   return static_cast<Toolkit::Internal::ToggleButton&>(handle);
178 }
179
180 inline const Toolkit::Internal::ToggleButton& GetImplementation(const Toolkit::ToggleButton& button)
181 {
182   DALI_ASSERT_ALWAYS(button);
183
184   const Dali::RefObject& handle = button.GetImplementation();
185
186   return static_cast<const Toolkit::Internal::ToggleButton&>(handle);
187 }
188
189 } // namespace Toolkit
190
191 } // namespace Dali
192
193 #endif // DALI_TOOLKIT_INTERNAL_TOGGLE_BUTTON_H