1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup UI_Components
18  * @{
19  *
20  * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file ui_checkbox.h
28  *
29  * @brief Defines the attributes and common functions of a check box.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_UI_CHECKBOX_H
36 #define GRAPHIC_LITE_UI_CHECKBOX_H
37 
38 #include "animator/animator.h"
39 #include "animator/interpolation.h"
40 #include "common/image.h"
41 #include "components/ui_view.h"
42 
43 namespace OHOS {
44 /**
45  * @brief Represents a check box.
46  *
47  * A check box permits users to make a binary choice.
48  *
49  * @since 1.0
50  * @version 1.0
51  */
52 #if DEFAULT_ANIMATION
53 class UICheckBox : public UIView, public AnimatorCallback {
54 #else
55 class UICheckBox : public UIView {
56 #endif
57 
58 public:
59     /**
60      * @brief Enumerates the states of a check box.
61      *
62      * @since 1.0
63      * @version 1.0
64      */
65     enum UICheckBoxState : uint8_t {
66         /* An enum constant representing the state selected option */
67         SELECTED,
68         /* An enum constant representing the state unselected option */
69         UNSELECTED,
70         /* Max num of state */
71         MAX_STATUS_NUM,
72     };
73 
74     /**
75      * @brief A constructor used to create a <b>UICheckBox</b> instance.
76      *
77      * @since 1.0
78      * @version 1.0
79      */
80     UICheckBox();
81 
82     /**
83      * @brief A destructor used to delete the <b>UICheckBox</b> instance.
84      *
85      * @since 1.0
86      * @version 1.0
87      */
~UICheckBox()88     virtual ~UICheckBox() {}
89 
90     /**
91      * @brief Represents a listener for changes of a check box.
92      *
93      * This is an inner class of <b>UICheckBox</b>. It contains a callback function to be invoked when the check box
94      * state changes.
95      *
96      * @see UICheckBox
97      * @since 1.0
98      * @version 1.0
99      */
100     class OnChangeListener : public HeapBase {
101     public:
102         /**
103          * @brief Called when the state of this check box is switched. This is a virtual function, which needs your
104          * implementation.
105          *
106          * @param state Indicates the current state of this check box. For details, see {@link UICheckBoxState}.
107          * @since 1.0
108          * @version 1.0
109          */
110         virtual bool OnChange(UICheckBoxState state) = 0;
111 
112         /**
113          * @brief A destructor used to delete the <b>OnChangeListener</b> instance.
114          *
115          * @since 1.0
116          * @version 1.0
117          */
~OnChangeListener()118         virtual ~OnChangeListener() {}
119     };
120 
121     /**
122      * @brief Obtains the component type.
123      *
124      * @return Returns the component type, as defined in {@link UIViewType}.
125      * @since 1.0
126      * @version 1.0
127      */
GetViewType()128     UIViewType GetViewType() const override
129     {
130         return UI_CHECK_BOX;
131     }
132 
133     /**
134      * @brief Sets the listener for this check box.
135      *
136      * The listener is triggered to invoke the callback function upon click events.
137      *
138      * @param listener Indicates the listener to set. For details, see {@link OnChangeListener}.
139      * @since 1.0
140      * @version 1.0
141      */
SetOnChangeListener(OnChangeListener * onStateChangeListener)142     void SetOnChangeListener(OnChangeListener* onStateChangeListener)
143     {
144         onStateChangeListener_ = onStateChangeListener;
145     }
146 
147     /**
148      * @fn  virtual bool UICheckBox::OnPreDraw(Rect& invalidatedArea) override
149      *
150      * @brief Do something before draw, this function will be invoked mainly to check if this view need
151      *        to cover invalidate area so render manager can decide which layer to draw firstly.
152      * @param [in] invalidate area.
153      * @returns True if need cover.
154      */
OnPreDraw(Rect & invalidatedArea)155     bool OnPreDraw(Rect& invalidatedArea) const override
156     {
157         return false;
158     }
159 
160     /**
161      * @fn  virtual void UICheckBox::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
162      *
163      * @brief   Executes the draw action
164      *          Ondraw invokes the rendering function provided by the underlying layer to draw pictures
165      *          based on the selected status of the checkbox.
166      *
167      * @param [in] invalidatedArea The invalidated area.
168      */
169     void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
170 
171     /**
172      * @fn  virtual void UICheckBox::OnClickEvent(const ClickEvent& event) override;
173      *
174      * @brief   Executes the click event action
175      *          OnClickEvent will reverse the selected state of checkbox.
176      *          Example: If the check box is selected, the checkbox status is changed to
177      *          Unselected after the click action is taken.
178      *
179      * @param [in] event   The event that passed when OnClickEvent is invoked.
180      * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise.
181      */
182     bool OnClickEvent(const ClickEvent& event) override;
183 
184     /**
185      * @brief Sets the images for this check box.
186      *
187      * @param selectedImageSrc Indicates the image for this check box when selected.
188      * @param unselectedImageSrc Indicates the image for this check box when unselected.
189      * @since 1.0
190      * @version 1.0
191      */
192     virtual void SetImages(const char* selectedImageSrc, const char* unselectedImageSrc);
193 
194     /**
195      * @brief Sets the images for this check box.
196      *
197      * @param selectedImageSrc Indicates the image for this check box when selected.
198      * @param unselectedImageSrc Indicates the image for this check box when unselected.
199      * @since 1.0
200      * @version 1.0
201      */
202     virtual void SetImages(const ImageInfo* selectedImageSrc, const ImageInfo* unselectedImageSrc);
203 
204     /**
205      * @brief Obtains the state of this check box.
206      *
207      * @return Returns the state of this check box, as defined in {@link UICheckBoxState}.
208      * @since 1.0
209      * @version 1.0
210      */
GetState()211     UICheckBoxState GetState() const
212     {
213         return state_;
214     };
215 
216     /**
217      * @brief Sets the state for this check box.
218      *
219      * @param state Indicates the state of this check box. For details, see {@link UICheckBoxState}.
220      * @param needAnimater Whether the state change process can be animated, the setting takes effect when the
221      *        DEFAULT_ANIMATION is 1, {@link DEFAULT_ANIMATION}
222      * @since 1.0
223      * @version 1.0
224      */
225     void SetState(UICheckBoxState state, bool needAnimater = false);
226 
227     /**
228      * @brief Sets the selected state color for this check box.
229      *
230      * @param color Indicates the selected state color of this check box.
231      *
232      * @since 5.0
233      * @version 3.0
234      */
235     void SetSelectedStateColor(ColorType color);
236 
237     /**
238      * @brief Obtains the selected state color of this check box.
239      *
240      * @return Returns the selected state color of this check box
241      * @since 5.0
242      * @version 3.0
243      */
244     ColorType GetSelectedStateColor() const;
245 
246 protected:
247     void ReverseState();
248     virtual void CalculateSize();
249     void SelectedStateSoftwareDrawing(BufferInfo& gfxDstBuffer,
250                                       Rect rect,
251                                       Rect trunc,
252                                       int16_t borderRadius,
253                                       int16_t rectLineWidth);
254     void UnSelectedStateSoftwareDrawing(BufferInfo& gfxDstBuffer,
255                                         Rect rect,
256                                         Rect trunc,
257                                         int16_t borderRadius,
258                                         int16_t rectLineWidth);
259 #if DEFAULT_ANIMATION
260     virtual void ResetCallback();
261     void Callback(UIView* view) override;
262     void OnStop(UIView& view) override;
263 #endif
264     static constexpr int16_t DEFAULT_HOT_WIDTH = 46;
265     static constexpr int16_t DEFAULT_HOT_HEIGHT = 46;
266     static constexpr int16_t DEFAULT_BORDER_WIDTH = 22;
267 
268     UICheckBoxState state_;
269     OnChangeListener* onStateChangeListener_;
270     int16_t width_;
271     int16_t height_;
272     int16_t borderWidth_;
273     Image image_[MAX_STATUS_NUM];
274     uint8_t backgroundOpacity_;
275 #if DEFAULT_ANIMATION
276     Animator checkBoxAnimator_;
277     uint32_t runTime_;
278 #endif
279     ColorType selectedStateColor_;
280 };
281 } // namespace OHOS
282 #endif // GRAPHIC_LITE_UI_CHECKBOX_H
283