• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 public:
58     /**
59      * @brief Enumerates the states of a check box.
60      *
61      * @since 1.0
62      * @version 1.0
63      */
64     enum UICheckBoxState : uint8_t {
65         /* An enum constant representing the state selected option */
66         SELECTED,
67         /* An enum constant representing the state unselected option */
68         UNSELECTED,
69         /* Max num of state */
70         MAX_STATUS_NUM,
71     };
72 
73     /**
74      * @brief A constructor used to create a <b>UICheckBox</b> instance.
75      *
76      * @since 1.0
77      * @version 1.0
78      */
79     UICheckBox();
80 
81     /**
82      * @brief A destructor used to delete the <b>UICheckBox</b> instance.
83      *
84      * @since 1.0
85      * @version 1.0
86      */
~UICheckBox()87     virtual ~UICheckBox() {};
88 
89     /**
90      * @brief Represents a listener for changes of a check box.
91      *
92      * This is an inner class of <b>UICheckBox</b>. It contains a callback function to be invoked when the check box
93      * state changes.
94      *
95      * @see UICheckBox
96      * @since 1.0
97      * @version 1.0
98      */
99     class OnChangeListener : public HeapBase {
100     public:
101         /**
102          * @brief Called when the state of this check box is switched. This is a virtual function, which needs your
103          * implementation.
104          *
105          * @param state Indicates the current state of this check box. For details, see {@link UICheckBoxState}.
106          * @since 1.0
107          * @version 1.0
108          */
109         virtual bool OnChange(UICheckBoxState state) = 0;
110 
111         /**
112          * @brief A destructor used to delete the <b>OnChangeListener</b> instance.
113          *
114          * @since 1.0
115          * @version 1.0
116          */
~OnChangeListener()117         virtual ~OnChangeListener() {}
118     };
119 
120     /**
121      * @brief Obtains the component type.
122      *
123      * @return Returns the component type, as defined in {@link UIViewType}.
124      * @since 1.0
125      * @version 1.0
126      */
GetViewType()127     UIViewType GetViewType() const override
128     {
129         return UI_CHECK_BOX;
130     }
131 
132     /**
133      * @brief Sets the listener for this check box.
134      *
135      * The listener is triggered to invoke the callback function upon click events.
136      *
137      * @param listener Indicates the listener to set. For details, see {@link OnChangeListener}.
138      * @since 1.0
139      * @version 1.0
140      */
SetOnChangeListener(OnChangeListener * onStateChangeListener)141     void SetOnChangeListener(OnChangeListener* onStateChangeListener)
142     {
143         onStateChangeListener_ = onStateChangeListener;
144     }
145 
146     /**
147      * @fn  virtual bool UICheckBox::OnPreDraw(Rect& invalidatedArea) override
148      *
149      * @brief Do something before draw, this function will be invoked mainly to check if this view need
150      *        to cover invalidate area so render manager can decide which layer to draw firstly.
151      * @param [in] invalidate area.
152      * @returns True if need cover.
153      */
OnPreDraw(Rect & invalidatedArea)154     bool OnPreDraw(Rect& invalidatedArea) const override
155     {
156         return false;
157     }
158 
159     /**
160      * @fn  virtual void UICheckBox::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
161      *
162      * @brief   Executes the draw action
163      *          Ondraw invokes the rendering function provided by the underlying layer to draw pictures
164      *          based on the selected status of the checkbox.
165      *
166      * @param [in] invalidatedArea The invalidated area.
167      */
168     void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
169 
170     /**
171      * @fn  virtual void UICheckBox::OnClickEvent(const ClickEvent& event) override;
172      *
173      * @brief   Executes the click event action
174      *          OnClickEvent will reverse the selected state of checkbox.
175      *          Example: If the check box is selected, the checkbox status is changed to
176      *          Unselected after the click action is taken.
177      *
178      * @param [in] event   The event that passed when OnClickEvent is invoked.
179      * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise.
180      */
181     bool OnClickEvent(const ClickEvent& event) override;
182 
183     /**
184      * @brief Sets the images for this check box.
185      *
186      * @param selectedImageSrc Indicates the image for this check box when selected.
187      * @param unselectedImageSrc Indicates the image for this check box when unselected.
188      * @since 1.0
189      * @version 1.0
190      */
191     virtual void SetImages(const char* selectedImageSrc, const char* unselectedImageSrc);
192 
193     /**
194      * @brief Sets the images for this check box.
195      *
196      * @param selectedImageSrc Indicates the image for this check box when selected.
197      * @param unselectedImageSrc Indicates the image for this check box when unselected.
198      * @since 1.0
199      * @version 1.0
200      */
201     virtual void SetImages(const ImageInfo* selectedImageSrc, const ImageInfo* unselectedImageSrc);
202 
203     /**
204      * @brief Obtains the state of this check box.
205      *
206      * @return Returns the state of this check box, as defined in {@link UICheckBoxState}.
207      * @since 1.0
208      * @version 1.0
209      */
GetState()210     UICheckBoxState GetState() const
211     {
212         return state_;
213     };
214 
215     /**
216      * @brief Sets the state for this check box.
217      *
218      * @param state Indicates the state of this check box. For details, see {@link UICheckBoxState}.
219      * @param needAnimater Whether the state change process can be animated, the setting takes effect when the
220      *        DEFAULT_ANIMATION is 1, {@link DEFAULT_ANIMATION}
221      * @since 1.0
222      * @version 1.0
223      */
224     void SetState(UICheckBoxState state, bool needAnimater = false);
225 
226     /**
227      * @brief Sets the selected state color for this check box.
228      *
229      * @param color Indicates the selected state color of this check box.
230      *
231      * @since 5.0
232      * @version 3.0
233      */
234     void SetSelectedStateColor(ColorType color);
235 
236     /**
237      * @brief Obtains the selected state color of this check box.
238      *
239      * @return Returns the selected state color of this check box
240      * @since 5.0
241      * @version 3.0
242      */
243     ColorType GetSelectedStateColor() const;
244 
245 protected:
246     void ReverseState();
247     virtual void CalculateSize();
248     void SelectedStateSoftwareDrawing(BufferInfo& gfxDstBuffer,
249                                       Rect rect,
250                                       Rect trunc,
251                                       int16_t borderRadius,
252                                       int16_t rectLineWidth);
253     void UnSelectedStateSoftwareDrawing(BufferInfo& gfxDstBuffer,
254                                         Rect rect,
255                                         Rect trunc,
256                                         int16_t borderRadius,
257                                         int16_t rectLineWidth);
258 #if DEFAULT_ANIMATION
259     virtual void ResetCallback();
260     void Callback(UIView* view) override;
261     void OnStop(UIView& view) override;
262 #endif
263     static constexpr int16_t DEFAULT_HOT_WIDTH = 46;
264     static constexpr int16_t DEFAULT_HOT_HEIGHT = 46;
265     static constexpr int16_t DEFAULT_BORDER_WIDTH = 22;
266 
267     UICheckBoxState state_;
268     OnChangeListener* onStateChangeListener_;
269     int16_t width_;
270     int16_t height_;
271     int16_t borderWidth_;
272     Image image_[MAX_STATUS_NUM];
273     uint8_t backgroundOpacity_;
274 #if DEFAULT_ANIMATION
275     Animator checkBoxAnimator_;
276     uint32_t runTime_;
277 #endif
278     ColorType selectedStateColor_;
279 };
280 } // namespace OHOS
281 #endif // GRAPHIC_LITE_UI_CHECKBOX_H
282