Button.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include <AK/StringBuilder.h>
  27. #include <LibGUI/Action.h>
  28. #include <LibGUI/ActionGroup.h>
  29. #include <LibGUI/Button.h>
  30. #include <LibGUI/Painter.h>
  31. #include <LibGfx/Font.h>
  32. #include <LibGfx/Palette.h>
  33. #include <LibGfx/StylePainter.h>
  34. namespace GUI {
  35. Button::Button(String text)
  36. : AbstractButton(move(text))
  37. {
  38. set_min_width(32);
  39. set_fixed_height(22);
  40. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  41. REGISTER_ENUM_PROPERTY(
  42. "button_style", button_style, set_button_style, Gfx::ButtonStyle,
  43. { Gfx::ButtonStyle::Normal, "Normal" },
  44. { Gfx::ButtonStyle::CoolBar, "CoolBar" });
  45. }
  46. Button::~Button()
  47. {
  48. if (m_action)
  49. m_action->unregister_button({}, *this);
  50. }
  51. void Button::paint_event(PaintEvent& event)
  52. {
  53. Painter painter(*this);
  54. painter.add_clip_rect(event.rect());
  55. Gfx::StylePainter::paint_button(painter, rect(), palette(), m_button_style, is_being_pressed(), is_hovered(), is_checked(), is_enabled(), is_focused());
  56. if (text().is_empty() && !m_icon)
  57. return;
  58. auto content_rect = rect().shrunken(8, 2);
  59. auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Gfx::IntPoint();
  60. if (m_icon && !text().is_empty())
  61. icon_location.set_x(content_rect.x());
  62. if (is_being_pressed() || is_checked())
  63. painter.translate(1, 1);
  64. else if (m_icon && is_enabled() && is_hovered() && button_style() == Gfx::ButtonStyle::CoolBar) {
  65. auto shadow_color = palette().button().darkened(0.7f);
  66. painter.blit_filtered(icon_location.translated(1, 1), *m_icon, m_icon->rect(), [&shadow_color](auto) {
  67. return shadow_color;
  68. });
  69. icon_location.move_by(-1, -1);
  70. }
  71. if (m_icon) {
  72. if (is_enabled()) {
  73. if (is_hovered())
  74. painter.blit_brightened(icon_location, *m_icon, m_icon->rect());
  75. else
  76. painter.blit(icon_location, *m_icon, m_icon->rect());
  77. } else {
  78. painter.blit_disabled(icon_location, *m_icon, m_icon->rect(), palette());
  79. }
  80. }
  81. auto& font = is_checked() ? Gfx::Font::default_bold_font() : this->font();
  82. if (m_icon && !text().is_empty()) {
  83. content_rect.move_by(m_icon->width() + 4, 0);
  84. content_rect.set_width(content_rect.width() - m_icon->width() - 4);
  85. }
  86. Gfx::IntRect text_rect { 0, 0, font.width(text()), font.glyph_height() };
  87. if (text_rect.width() > content_rect.width())
  88. text_rect.set_width(content_rect.width());
  89. text_rect.align_within(content_rect, text_alignment());
  90. paint_text(painter, text_rect, font, text_alignment());
  91. if (is_focused()) {
  92. Gfx::IntRect focus_rect;
  93. if (m_icon && !text().is_empty())
  94. focus_rect = text_rect.inflated(6, 6);
  95. else
  96. focus_rect = rect().shrunken(8, 8);
  97. painter.draw_focus_rect(focus_rect, palette().focus_outline());
  98. }
  99. }
  100. void Button::click(unsigned modifiers)
  101. {
  102. if (!is_enabled())
  103. return;
  104. NonnullRefPtr protector = *this;
  105. if (is_checkable()) {
  106. if (is_checked() && !is_uncheckable())
  107. return;
  108. set_checked(!is_checked());
  109. }
  110. if (on_click)
  111. on_click(modifiers);
  112. if (m_action)
  113. m_action->activate(this);
  114. }
  115. void Button::context_menu_event(ContextMenuEvent& context_menu_event)
  116. {
  117. if (!is_enabled())
  118. return;
  119. if (on_context_menu_request)
  120. on_context_menu_request(context_menu_event);
  121. }
  122. void Button::set_action(Action& action)
  123. {
  124. m_action = action;
  125. action.register_button({}, *this);
  126. set_enabled(action.is_enabled());
  127. set_checkable(action.is_checkable());
  128. if (action.is_checkable())
  129. set_checked(action.is_checked());
  130. }
  131. void Button::set_icon(RefPtr<Gfx::Bitmap>&& icon)
  132. {
  133. if (m_icon == icon)
  134. return;
  135. m_icon = move(icon);
  136. update();
  137. }
  138. bool Button::is_uncheckable() const
  139. {
  140. if (!m_action)
  141. return true;
  142. if (!m_action->group())
  143. return true;
  144. return m_action->group()->is_unchecking_allowed();
  145. }
  146. }