2020-01-18 09:38:21 +01: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 15:20:02 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
#include <LibGUI/ActionGroup.h>
|
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-02-14 23:53:11 +01:00
|
|
|
#include <LibGfx/Font.h>
|
|
|
|
#include <LibGfx/Palette.h>
|
|
|
|
#include <LibGfx/StylePainter.h>
|
2018-10-11 01:48:09 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
2020-02-23 12:07:13 +01:00
|
|
|
Button::Button(const StringView& text)
|
|
|
|
: AbstractButton(text)
|
2019-05-24 22:47:01 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Button::~Button()
|
2018-10-11 01:48:09 +02:00
|
|
|
{
|
2019-04-12 02:53:27 +02:00
|
|
|
if (m_action)
|
2019-06-07 11:46:02 +02:00
|
|
|
m_action->unregister_button({}, *this);
|
2018-10-11 01:48:09 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Button::paint_event(PaintEvent& event)
|
2018-10-11 01:48:09 +02:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
Painter painter(*this);
|
2019-03-29 15:01:54 +01:00
|
|
|
painter.add_clip_rect(event.rect());
|
2018-10-12 14:58:16 +02:00
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::StylePainter::paint_button(painter, rect(), palette(), m_button_style, is_being_pressed(), is_hovered(), is_checked(), is_enabled());
|
2018-10-12 14:58:16 +02:00
|
|
|
|
2019-05-24 16:32:20 +02:00
|
|
|
if (text().is_empty() && !m_icon)
|
2019-04-04 15:20:02 +02:00
|
|
|
return;
|
|
|
|
|
2019-11-29 15:46:38 +01:00
|
|
|
auto content_rect = rect().shrunken(8, 2);
|
2020-06-10 10:57:59 +02:00
|
|
|
auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Gfx::IntPoint();
|
2019-05-24 16:32:20 +02:00
|
|
|
if (m_icon && !text().is_empty())
|
2019-04-13 16:59:55 +02:00
|
|
|
icon_location.set_x(content_rect.x());
|
2020-10-25 15:22:21 +01:00
|
|
|
|
2019-11-29 15:46:38 +01:00
|
|
|
if (is_being_pressed() || is_checked())
|
2019-04-13 16:59:55 +02:00
|
|
|
painter.translate(1, 1);
|
2020-10-25 15:22:21 +01:00
|
|
|
else if (m_icon && is_enabled() && is_hovered() && button_style() == Gfx::ButtonStyle::CoolBar) {
|
|
|
|
auto shadow_color = palette().threed_shadow1();
|
|
|
|
painter.blit_filtered(icon_location, *m_icon, m_icon->rect(), [&shadow_color](auto) {
|
|
|
|
return shadow_color;
|
|
|
|
});
|
|
|
|
icon_location.move_by(-1, -1);
|
|
|
|
}
|
|
|
|
|
2019-04-12 02:53:27 +02:00
|
|
|
if (m_icon) {
|
2020-03-30 19:40:44 +02: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 02:53:27 +02:00
|
|
|
painter.blit_dimmed(icon_location, *m_icon, m_icon->rect());
|
2020-03-30 19:40:44 +02:00
|
|
|
}
|
2019-04-12 02:53:27 +02:00
|
|
|
}
|
2020-02-06 11:56:38 +01:00
|
|
|
auto& font = is_checked() ? Gfx::Font::default_bold_font() : this->font();
|
2019-05-24 16:32:20 +02:00
|
|
|
if (m_icon && !text().is_empty()) {
|
2019-04-13 16:59:55 +02:00
|
|
|
content_rect.move_by(m_icon->width() + 4, 0);
|
|
|
|
content_rect.set_width(content_rect.width() - m_icon->width() - 4);
|
|
|
|
}
|
2019-05-24 22:54:37 +02:00
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntRect text_rect { 0, 0, font.width(text()), font.glyph_height() };
|
2019-05-25 04:01:22 +02:00
|
|
|
if (text_rect.width() > content_rect.width())
|
|
|
|
text_rect.set_width(content_rect.width());
|
2019-05-25 20:15:52 +02:00
|
|
|
text_rect.align_within(content_rect, text_alignment());
|
2019-11-29 15:41:53 +01:00
|
|
|
paint_text(painter, text_rect, font, text_alignment());
|
2018-10-11 01:48:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-12 20:30:33 +02:00
|
|
|
void Button::click(unsigned modifiers)
|
2019-03-19 01:41:00 +01:00
|
|
|
{
|
2019-04-12 02:53:27 +02:00
|
|
|
if (!is_enabled())
|
|
|
|
return;
|
2019-07-09 22:10:03 +02:00
|
|
|
if (is_checkable()) {
|
|
|
|
if (is_checked() && !is_uncheckable())
|
|
|
|
return;
|
2019-06-12 05:57:26 +02:00
|
|
|
set_checked(!is_checked());
|
2019-07-09 22:10:03 +02:00
|
|
|
}
|
2019-03-19 01:41:00 +01:00
|
|
|
if (on_click)
|
2020-05-12 20:30:33 +02:00
|
|
|
on_click(modifiers);
|
2019-08-25 21:42:37 +02:00
|
|
|
if (m_action)
|
2019-12-09 21:25:48 +01:00
|
|
|
m_action->activate(this);
|
2019-03-19 01:41:00 +01:00
|
|
|
}
|
|
|
|
|
2020-05-18 21:25:28 -04:00
|
|
|
void Button::context_menu_event(ContextMenuEvent& context_menu_event)
|
|
|
|
{
|
|
|
|
if (!is_enabled())
|
|
|
|
return;
|
|
|
|
if (on_context_menu_request)
|
|
|
|
on_context_menu_request(context_menu_event);
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Button::set_action(Action& action)
|
2019-04-12 02:53:27 +02:00
|
|
|
{
|
|
|
|
m_action = action.make_weak_ptr();
|
2019-06-07 11:46:02 +02:00
|
|
|
action.register_button({}, *this);
|
2019-04-12 02:53:27 +02:00
|
|
|
set_enabled(action.is_enabled());
|
2019-04-26 21:09:56 +02:00
|
|
|
set_checkable(action.is_checkable());
|
|
|
|
if (action.is_checkable())
|
|
|
|
set_checked(action.is_checked());
|
2019-04-12 02:53:27 +02:00
|
|
|
}
|
2019-04-13 16:59:55 +02:00
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
void Button::set_icon(RefPtr<Gfx::Bitmap>&& icon)
|
2019-04-13 16:59:55 +02:00
|
|
|
{
|
2019-04-14 02:36:06 +02:00
|
|
|
if (m_icon == icon)
|
2019-04-13 16:59:55 +02:00
|
|
|
return;
|
|
|
|
m_icon = move(icon);
|
|
|
|
update();
|
|
|
|
}
|
2019-07-09 22:10:03 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
bool Button::is_uncheckable() const
|
2019-07-09 22:10:03 +02:00
|
|
|
{
|
|
|
|
if (!m_action)
|
|
|
|
return true;
|
|
|
|
if (!m_action->group())
|
|
|
|
return true;
|
|
|
|
return m_action->group()->is_unchecking_allowed();
|
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
}
|