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_label_button.h
28 *
29 * @brief Declares a label button.
30 *
31 * @since 1.0
32 * @version 1.0
33 */
34
35#ifndef GRAPHIC_LITE_UI_LABEL_BUTTON_H
36#define GRAPHIC_LITE_UI_LABEL_BUTTON_H
37
38#include "common/text.h"
39#include "components/ui_button.h"
40#include "gfx_utils/graphic_log.h"
41
42namespace OHOS {
43/**
44 * @brief Provides the functions related to a label button.
45 *
46 * @since 1.0
47 * @version 1.0
48 */
49class UILabelButton : public UIButton {
50public:
51    /**
52     * @brief A constructor used to create a <b>UILabelButton</b> instance.
53     *
54     * @since 1.0
55     * @version 1.0
56     */
57    UILabelButton();
58
59    /**
60     * @brief A destructor used to delete the <b>UILabelButton</b> instance.
61     *
62     * @since 1.0
63     * @version 1.0
64     */
65    virtual ~UILabelButton();
66
67    /**
68     * @brief Draws a label button.
69     *
70     * @since 1.0
71     * @version 1.0
72     */
73    void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
74
75    /**
76     * @brief Obtains the view type.
77     *
78     * @return Returns <b>UI_LABEL_BUTTON</b>, as defined in {@link UIViewType}.
79     * @since 1.0
80     * @version 1.0
81     */
82    UIViewType GetViewType() const override
83    {
84        return UI_LABEL_BUTTON;
85    }
86
87    /**
88     * @brief Sets the text for this label button.
89     *
90     * @param text Indicates the pointer to the text.
91     * @since 1.0
92     * @version 1.0
93     */
94    void SetText(const char* text)
95    {
96        InitLabelButtonText();
97        labelButtonText_->SetText(text);
98    }
99
100    /**
101     * @brief Obtains the text of this label button.
102     *
103     * @return Returns the text.
104     * @since 1.0
105     * @version 1.0
106     */
107    const char* GetText() const
108    {
109        return (labelButtonText_ == nullptr) ? nullptr : labelButtonText_->GetText();
110    }
111
112    /**
113     * @brief Sets the position for this label relative to the button holding it.
114     *
115     * @param x Indicates the offset distance by which this label is moved on the x-axis.
116     * @param y Indicates the offset distance by which this label is moved on the y-axis.
117     * @since 1.0
118     * @version 1.0
119     */
120    void SetLabelPosition(int16_t x, int16_t y)
121    {
122        offset_.x = x;
123        offset_.y = y;
124    }
125
126    /**
127     * @brief Obtains the position of this label relative to the button holding it.
128     *
129     * @return Returns the position of this label.
130     * @since 1.0
131     * @version 1.0
132     */
133    Point GetLabelPosition() const
134    {
135        return offset_;
136    }
137
138    /**
139     * @brief Sets the alignment mode for this text.
140     *
141     * @param align Indicates the text alignment mode to set, as defined in {@link UITextLanguageAlignment}.
142     * @since 1.0
143     * @version 1.0
144     */
145    void SetAlign(UITextLanguageAlignment align)
146    {
147        InitLabelButtonText();
148        labelButtonText_->SetAlign(align, TEXT_ALIGNMENT_CENTER);
149    }
150
151    /**
152     * @brief Obtains the alignment mode of this text.
153     *
154     * @return Returns the text alignment mode, as defined in {@link UITextLanguageAlignment}.
155     * @since 1.0
156     * @version 1.0
157     */
158    UITextLanguageAlignment GetAlign()
159    {
160        InitLabelButtonText();
161        return labelButtonText_->GetHorAlign();
162    }
163
164    /**
165     * @brief Sets the direction for this text.
166     *
167     * @param direct Indicates the text direction to set, as defined in {@link UITextLanguageDirect}.
168     * @since 1.0
169     * @version 1.0
170     */
171    void SetDirect(UITextLanguageDirect direct)
172    {
173        InitLabelButtonText();
174        labelButtonText_->SetDirect(direct);
175    }
176
177    /**
178     * @brief Obtains the direction of this text.
179     *
180     * @return Returns the text direction, as defined in {@link UITextLanguageDirect}.
181     * @since 1.0
182     * @version 1.0
183     */
184    UITextLanguageDirect GetDirect()
185    {
186        InitLabelButtonText();
187        return labelButtonText_->GetDirect();
188    }
189
190    /**
191     * @brief Sets the style for this label.
192     *
193     * @param labelStyle Indicates the label style to set.
194     * @since 1.0
195     * @version 1.0
196     */
197    void SetLabelStyle(Style& labelStyle)
198    {
199        labelStyle_ = labelStyle;
200    }
201
202    /**
203     * @brief Sets a style for this label.
204     *
205     * @param key Indicates the key of the style to set.
206     * @param value Indicates the value matching the key.
207     * @since 1.0
208     * @version 1.0
209     */
210    void SetLabelStyle(uint8_t key, int64_t value)
211    {
212        labelStyle_.SetStyle(key, value);
213    }
214
215    /**
216     * @brief Obtains the style of this label.
217     *
218     * @return Returns the label style.
219     * @since 1.0
220     * @version 1.0
221     */
222    const Style& GetLabelStyle() const
223    {
224        return labelStyle_;
225    }
226
227    /**
228     * @brief Obtains the value of a style of this label.
229     *
230     * @param key Indicates the key of the style.
231     * @return Returns the value of the style.
232     * @since 1.0
233     * @version 1.0
234     */
235    int64_t GetLabelStyle(uint8_t key) const
236    {
237        return labelStyle_.GetStyle(key);
238    }
239
240    /**
241     * @brief Sets the color for this text.
242     *
243     * @param color Indicates the text color to set.
244     * @since 1.0
245     * @version 1.0
246     */
247    void SetTextColor(ColorType color)
248    {
249        labelStyle_.textColor_ = color;
250    }
251
252    /**
253     * @brief Sets the font for this text.
254     *
255     * @param name Indicates the pointer to the font name.
256     * @param size Indicates the font size to set.
257     * @since 1.0
258     * @version 1.0
259     */
260    void SetFont(const char* name, uint8_t size)
261    {
262        InitLabelButtonText();
263        labelButtonText_->SetFont(name, size);
264    }
265
266    /**
267     * @brief Sets the font ID.
268     *
269     * @param fontId Indicates the font ID composed of the font name and size.
270     * @since 1.0
271     * @version 1.0
272     */
273    void SetFontId(uint16_t fontId)
274    {
275        InitLabelButtonText();
276        labelButtonText_->SetFontId(fontId);
277    }
278
279    /**
280     * @brief Obtains the font ID.
281     *
282     * @return Returns the front ID composed of the font name and size.
283     * @since 1.0
284     * @version 1.0
285     */
286    uint16_t GetFontId()
287    {
288        InitLabelButtonText();
289        return labelButtonText_->GetFontId();
290    }
291
292protected:
293    virtual void InitLabelButtonText()
294    {
295        if (labelButtonText_ == nullptr) {
296            labelButtonText_ = new Text();
297            if (labelButtonText_ == nullptr) {
298                GRAPHIC_LOGE("new Text fail");
299                return;
300            }
301            labelButtonText_->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
302        }
303    }
304
305    Text* labelButtonText_;
306
307private:
308    Style labelStyle_;
309    Point offset_; /* Text draw position offset */
310};
311} // namespace OHOS
312#endif // GRAPHIC_LITE_UI_LABEL_BUTTON_H
313