Button.cpp 4.9 KB

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