GButton.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <AK/StringBuilder.h>
  2. #include <Kernel/KeyCode.h>
  3. #include <LibGUI/GAction.h>
  4. #include <LibGUI/GActionGroup.h>
  5. #include <LibGUI/GButton.h>
  6. #include <LibGUI/GPainter.h>
  7. #include <LibDraw/StylePainter.h>
  8. GButton::GButton(GWidget* parent)
  9. : GAbstractButton(parent)
  10. {
  11. }
  12. GButton::GButton(const StringView& text, GWidget* parent)
  13. : GAbstractButton(text, parent)
  14. {
  15. }
  16. GButton::~GButton()
  17. {
  18. if (m_action)
  19. m_action->unregister_button({}, *this);
  20. }
  21. void GButton::paint_event(GPaintEvent& event)
  22. {
  23. GPainter painter(*this);
  24. painter.add_clip_rect(event.rect());
  25. StylePainter::paint_button(painter, rect(), m_button_style, is_being_pressed(), is_hovered(), is_checked(), is_enabled());
  26. if (text().is_empty() && !m_icon)
  27. return;
  28. auto content_rect = rect().shrunken(10, 2);
  29. auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Point();
  30. if (m_icon && !text().is_empty())
  31. icon_location.set_x(content_rect.x());
  32. if (is_being_pressed())
  33. painter.translate(1, 1);
  34. if (m_icon) {
  35. if (is_enabled())
  36. painter.blit(icon_location, *m_icon, m_icon->rect());
  37. else
  38. painter.blit_dimmed(icon_location, *m_icon, m_icon->rect());
  39. }
  40. auto& font = is_checked() ? Font::default_bold_font() : this->font();
  41. if (m_icon && !text().is_empty()) {
  42. content_rect.move_by(m_icon->width() + 4, 0);
  43. content_rect.set_width(content_rect.width() - m_icon->width() - 4);
  44. }
  45. Rect text_rect { 0, 0, font.width(text()), font.glyph_height() };
  46. if (text_rect.width() > content_rect.width())
  47. text_rect.set_width(content_rect.width());
  48. text_rect.align_within(content_rect, text_alignment());
  49. paint_text(painter, text_rect, font, TextAlignment::Center);
  50. }
  51. void GButton::click()
  52. {
  53. if (!is_enabled())
  54. return;
  55. if (is_checkable()) {
  56. if (is_checked() && !is_uncheckable())
  57. return;
  58. set_checked(!is_checked());
  59. }
  60. if (on_click)
  61. on_click(*this);
  62. if (m_action)
  63. m_action->activate();
  64. }
  65. bool GButton::supports_keyboard_activation() const
  66. {
  67. return is_enabled();
  68. }
  69. void GButton::set_action(GAction& action)
  70. {
  71. m_action = action.make_weak_ptr();
  72. action.register_button({}, *this);
  73. set_enabled(action.is_enabled());
  74. set_checkable(action.is_checkable());
  75. if (action.is_checkable())
  76. set_checked(action.is_checked());
  77. }
  78. void GButton::set_icon(RefPtr<GraphicsBitmap>&& icon)
  79. {
  80. if (m_icon == icon)
  81. return;
  82. m_icon = move(icon);
  83. update();
  84. }
  85. bool GButton::is_uncheckable() const
  86. {
  87. if (!m_action)
  88. return true;
  89. if (!m_action->group())
  90. return true;
  91. return m_action->group()->is_unchecking_allowed();
  92. }