Button.cpp 6.0 KB

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