2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-04-04 16:20:02 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-05-15 05:25:53 +03:00
|
|
|
#include <Kernel/KeyCode.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
#include <LibGUI/ActionGroup.h>
|
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-02-15 01:53:11 +03:00
|
|
|
#include <LibGfx/Font.h>
|
|
|
|
#include <LibGfx/Palette.h>
|
|
|
|
#include <LibGfx/StylePainter.h>
|
2018-10-11 02:48:09 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
2020-02-23 14:07:13 +03:00
|
|
|
Button::Button(const StringView& text)
|
|
|
|
: AbstractButton(text)
|
2019-05-24 23:47:01 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Button::~Button()
|
2018-10-11 02:48:09 +03:00
|
|
|
{
|
2019-04-12 03:53:27 +03:00
|
|
|
if (m_action)
|
2019-06-07 12:46:02 +03:00
|
|
|
m_action->unregister_button({}, *this);
|
2018-10-11 02:48:09 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Button::paint_event(PaintEvent& event)
|
2018-10-11 02:48:09 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
Painter painter(*this);
|
2019-03-29 17:01:54 +03:00
|
|
|
painter.add_clip_rect(event.rect());
|
2018-10-12 15:58:16 +03:00
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
Gfx::StylePainter::paint_button(painter, rect(), palette(), m_button_style, is_being_pressed(), is_hovered(), is_checked(), is_enabled());
|
2018-10-12 15:58:16 +03:00
|
|
|
|
2019-05-24 17:32:20 +03:00
|
|
|
if (text().is_empty() && !m_icon)
|
2019-04-04 16:20:02 +03:00
|
|
|
return;
|
|
|
|
|
2019-11-29 17:46:38 +03:00
|
|
|
auto content_rect = rect().shrunken(8, 2);
|
2020-02-06 15:08:32 +03:00
|
|
|
auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Gfx::Point();
|
2019-05-24 17:32:20 +03:00
|
|
|
if (m_icon && !text().is_empty())
|
2019-04-13 17:59:55 +03:00
|
|
|
icon_location.set_x(content_rect.x());
|
2019-11-29 17:46:38 +03:00
|
|
|
if (is_being_pressed() || is_checked())
|
2019-04-13 17:59:55 +03:00
|
|
|
painter.translate(1, 1);
|
2019-04-12 03:53:27 +03:00
|
|
|
if (m_icon) {
|
2020-03-30 20:40:44 +03:00
|
|
|
if (is_enabled()) {
|
|
|
|
if (is_hovered())
|
|
|
|
painter.blit_brightened(icon_location, *m_icon, m_icon->rect());
|
|
|
|
else
|
|
|
|
painter.blit(icon_location, *m_icon, m_icon->rect());
|
|
|
|
} else {
|
2019-04-12 03:53:27 +03:00
|
|
|
painter.blit_dimmed(icon_location, *m_icon, m_icon->rect());
|
2020-03-30 20:40:44 +03:00
|
|
|
}
|
2019-04-12 03:53:27 +03:00
|
|
|
}
|
2020-02-06 13:56:38 +03:00
|
|
|
auto& font = is_checked() ? Gfx::Font::default_bold_font() : this->font();
|
2019-05-24 17:32:20 +03:00
|
|
|
if (m_icon && !text().is_empty()) {
|
2019-04-13 17:59:55 +03:00
|
|
|
content_rect.move_by(m_icon->width() + 4, 0);
|
|
|
|
content_rect.set_width(content_rect.width() - m_icon->width() - 4);
|
|
|
|
}
|
2019-05-24 23:54:37 +03:00
|
|
|
|
2020-02-06 15:02:38 +03:00
|
|
|
Gfx::Rect text_rect { 0, 0, font.width(text()), font.glyph_height() };
|
2019-05-25 05:01:22 +03:00
|
|
|
if (text_rect.width() > content_rect.width())
|
|
|
|
text_rect.set_width(content_rect.width());
|
2019-05-25 21:15:52 +03:00
|
|
|
text_rect.align_within(content_rect, text_alignment());
|
2019-11-29 17:41:53 +03:00
|
|
|
paint_text(painter, text_rect, font, text_alignment());
|
2018-10-11 02:48:09 +03:00
|
|
|
}
|
|
|
|
|
2020-05-12 21:30:33 +03:00
|
|
|
void Button::click(unsigned modifiers)
|
2019-03-19 03:41:00 +03:00
|
|
|
{
|
2019-04-12 03:53:27 +03:00
|
|
|
if (!is_enabled())
|
|
|
|
return;
|
2019-07-09 23:10:03 +03:00
|
|
|
if (is_checkable()) {
|
|
|
|
if (is_checked() && !is_uncheckable())
|
|
|
|
return;
|
2019-06-12 06:57:26 +03:00
|
|
|
set_checked(!is_checked());
|
2019-07-09 23:10:03 +03:00
|
|
|
}
|
2019-03-19 03:41:00 +03:00
|
|
|
if (on_click)
|
2020-05-12 21:30:33 +03:00
|
|
|
on_click(modifiers);
|
2019-08-25 22:42:37 +03:00
|
|
|
if (m_action)
|
2019-12-09 23:25:48 +03:00
|
|
|
m_action->activate(this);
|
2019-03-19 03:41:00 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Button::set_action(Action& action)
|
2019-04-12 03:53:27 +03:00
|
|
|
{
|
|
|
|
m_action = action.make_weak_ptr();
|
2019-06-07 12:46:02 +03:00
|
|
|
action.register_button({}, *this);
|
2019-04-12 03:53:27 +03:00
|
|
|
set_enabled(action.is_enabled());
|
2019-04-26 22:09:56 +03:00
|
|
|
set_checkable(action.is_checkable());
|
|
|
|
if (action.is_checkable())
|
|
|
|
set_checked(action.is_checked());
|
2019-04-12 03:53:27 +03:00
|
|
|
}
|
2019-04-13 17:59:55 +03:00
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
void Button::set_icon(RefPtr<Gfx::Bitmap>&& icon)
|
2019-04-13 17:59:55 +03:00
|
|
|
{
|
2019-04-14 03:36:06 +03:00
|
|
|
if (m_icon == icon)
|
2019-04-13 17:59:55 +03:00
|
|
|
return;
|
|
|
|
m_icon = move(icon);
|
|
|
|
update();
|
|
|
|
}
|
2019-07-09 23:10:03 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
bool Button::is_uncheckable() const
|
2019-07-09 23:10:03 +03:00
|
|
|
{
|
|
|
|
if (!m_action)
|
|
|
|
return true;
|
|
|
|
if (!m_action->group())
|
|
|
|
return true;
|
|
|
|
return m_action->group()->is_unchecking_allowed();
|
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
|
|
|
|
}
|