Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/StringBuilder.h>
28#include <Kernel/KeyCode.h>
29#include <LibGUI/Action.h>
30#include <LibGUI/ActionGroup.h>
31#include <LibGUI/Button.h>
32#include <LibGUI/Painter.h>
33#include <LibGfx/Font.h>
34#include <LibGfx/Palette.h>
35#include <LibGfx/StylePainter.h>
36
37namespace GUI {
38
39Button::Button(const StringView& text)
40 : AbstractButton(text)
41{
42}
43
44Button::~Button()
45{
46 if (m_action)
47 m_action->unregister_button({}, *this);
48}
49
50void Button::paint_event(PaintEvent& event)
51{
52 Painter painter(*this);
53 painter.add_clip_rect(event.rect());
54
55 Gfx::StylePainter::paint_button(painter, rect(), palette(), m_button_style, is_being_pressed(), is_hovered(), is_checked(), is_enabled());
56
57 if (text().is_empty() && !m_icon)
58 return;
59
60 auto content_rect = rect().shrunken(8, 2);
61 auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Gfx::Point();
62 if (m_icon && !text().is_empty())
63 icon_location.set_x(content_rect.x());
64 if (is_being_pressed() || is_checked())
65 painter.translate(1, 1);
66 if (m_icon) {
67 if (is_enabled())
68 painter.blit(icon_location, *m_icon, m_icon->rect());
69 else
70 painter.blit_dimmed(icon_location, *m_icon, m_icon->rect());
71 }
72 auto& font = is_checked() ? Gfx::Font::default_bold_font() : this->font();
73 if (m_icon && !text().is_empty()) {
74 content_rect.move_by(m_icon->width() + 4, 0);
75 content_rect.set_width(content_rect.width() - m_icon->width() - 4);
76 }
77
78 Gfx::Rect text_rect { 0, 0, font.width(text()), font.glyph_height() };
79 if (text_rect.width() > content_rect.width())
80 text_rect.set_width(content_rect.width());
81 text_rect.align_within(content_rect, text_alignment());
82 paint_text(painter, text_rect, font, text_alignment());
83}
84
85void Button::click()
86{
87 if (!is_enabled())
88 return;
89 if (is_checkable()) {
90 if (is_checked() && !is_uncheckable())
91 return;
92 set_checked(!is_checked());
93 }
94 if (on_click)
95 on_click(*this);
96 if (m_action)
97 m_action->activate(this);
98}
99
100void Button::set_action(Action& action)
101{
102 m_action = action.make_weak_ptr();
103 action.register_button({}, *this);
104 set_enabled(action.is_enabled());
105 set_checkable(action.is_checkable());
106 if (action.is_checkable())
107 set_checked(action.is_checked());
108}
109
110void Button::set_icon(RefPtr<Gfx::Bitmap>&& icon)
111{
112 if (m_icon == icon)
113 return;
114 m_icon = move(icon);
115 update();
116}
117
118bool Button::is_uncheckable() const
119{
120 if (!m_action)
121 return true;
122 if (!m_action->group())
123 return true;
124 return m_action->group()->is_unchecking_allowed();
125}
126
127}