Serenity Operating System
at hosted 131 lines 4.3 kB view raw
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 if (is_hovered()) 69 painter.blit_brightened(icon_location, *m_icon, m_icon->rect()); 70 else 71 painter.blit(icon_location, *m_icon, m_icon->rect()); 72 } else { 73 painter.blit_dimmed(icon_location, *m_icon, m_icon->rect()); 74 } 75 } 76 auto& font = is_checked() ? Gfx::Font::default_bold_font() : this->font(); 77 if (m_icon && !text().is_empty()) { 78 content_rect.move_by(m_icon->width() + 4, 0); 79 content_rect.set_width(content_rect.width() - m_icon->width() - 4); 80 } 81 82 Gfx::Rect text_rect { 0, 0, font.width(text()), font.glyph_height() }; 83 if (text_rect.width() > content_rect.width()) 84 text_rect.set_width(content_rect.width()); 85 text_rect.align_within(content_rect, text_alignment()); 86 paint_text(painter, text_rect, font, text_alignment()); 87} 88 89void Button::click() 90{ 91 if (!is_enabled()) 92 return; 93 if (is_checkable()) { 94 if (is_checked() && !is_uncheckable()) 95 return; 96 set_checked(!is_checked()); 97 } 98 if (on_click) 99 on_click(); 100 if (m_action) 101 m_action->activate(this); 102} 103 104void Button::set_action(Action& action) 105{ 106 m_action = action.make_weak_ptr(); 107 action.register_button({}, *this); 108 set_enabled(action.is_enabled()); 109 set_checkable(action.is_checkable()); 110 if (action.is_checkable()) 111 set_checked(action.is_checked()); 112} 113 114void Button::set_icon(RefPtr<Gfx::Bitmap>&& icon) 115{ 116 if (m_icon == icon) 117 return; 118 m_icon = move(icon); 119 update(); 120} 121 122bool Button::is_uncheckable() const 123{ 124 if (!m_action) 125 return true; 126 if (!m_action->group()) 127 return true; 128 return m_action->group()->is_unchecking_allowed(); 129} 130 131}