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_radio_button.h
28 *
29 * @brief Defines the attributes and common functions of a radio button.
30 *
31 * @since 1.0
32 * @version 1.0
33 */
34
35#ifndef GRAPHIC_LITE_UI_RADIO_BUTTON_H
36#define GRAPHIC_LITE_UI_RADIO_BUTTON_H
37
38#include "components/ui_checkbox.h"
39
40namespace OHOS {
41/**
42 * @brief Represents a radio button.
43 *
44 * Only one option can be selected with the radio button.
45 *
46 * @see UICheckBox
47 * @since 1.0
48 * @version 1.0
49 */
50class UIRadioButton : public UICheckBox {
51public:
52    /**
53     * @brief A constructor used to create a <b>UIRadioButton</b> instance.
54     *
55     * @since 1.0
56     * @version 1.0
57     */
58    UIRadioButton();
59
60    /**
61     * @fn  UIRadioButton::UIRadioButton(const char* name);
62     *
63     * @brief   Default constructor
64     */
65    explicit UIRadioButton(const char* name);
66
67    /**
68     * @brief A destructor used to delete the <b>UIRadioButton</b> instance.
69     *
70     * @since 1.0
71     * @version 1.0
72     */
73    virtual ~UIRadioButton()
74    {
75        if (name_ != nullptr) {
76            UIFree(name_);
77            name_ = nullptr;
78        }
79    }
80
81    /**
82     * @brief Obtains the component type.
83     *
84     * @return Returns the component type, as defined in {@link UIViewType}.
85     * @since 1.0
86     * @version 1.0
87     */
88    UIViewType GetViewType() const override
89    {
90        return UI_RADIO_BUTTON;
91    }
92
93    bool OnPreDraw(Rect& invalidatedArea) const override
94    {
95        return false;
96    }
97
98    void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
99
100    /**
101     * @fn  virtual void UIRadioButton::OnClickEvent(const ClickEvent& event) override;
102     *
103     * @brief   Executes the click event action
104     *          OnClickEvent will reverse the selected state of checkbox.
105     *          Example: If the check box is selected, the checkbox status is changed to
106     *          Unselected after the click action is taken.
107     *
108     * @param event   The event that passed when OnClickEvent is invoked.
109     * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise.
110     */
111    bool OnClickEvent(const ClickEvent& event) override;
112
113    /**
114     * @brief Sets the name for this radio button.
115     *
116     * @param name Indicates the name to set, which is a character string.
117     * @since 1.0
118     * @version 1.0
119     */
120    void SetName(const char* name);
121
122    /**
123     * @brief Obtains the name of this radio button.
124     *
125     * @return Returns the name of this radio button, which is a defined character string. Radio buttons sharing the
126     * same name are in the same batch from which only one can be selected.
127     * @since 1.0
128     * @version 1.0
129     */
130    const char* GetName() const
131    {
132        return name_;
133    }
134
135protected:
136    void CalculateSize() override;
137#if DEFAULT_ANIMATION
138    void Callback(UIView* view) override;
139#endif
140
141private:
142    void FindRadioButtonAndChangeState(UIView* view);
143    char* name_;
144    uint16_t radiusBig_;
145    uint16_t radiusSmall_;
146    uint16_t currentRadius_;
147    int16_t lineWidth_;
148};
149} // namespace OHOS
150#endif // GRAPHIC_LITE_UI_RADIO_BUTTON_H
151