GButton.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "GButton.h"
  2. #include <LibGUI/GPainter.h>
  3. #include <SharedGraphics/StylePainter.h>
  4. #include <AK/StringBuilder.h>
  5. #include <LibGUI/GAction.h>
  6. //#define GBUTTON_DEBUG
  7. GButton::GButton(GWidget* parent)
  8. : GWidget(parent)
  9. {
  10. }
  11. GButton::~GButton()
  12. {
  13. if (m_action)
  14. m_action->unregister_button({ }, *this);
  15. }
  16. void GButton::set_caption(const String& caption)
  17. {
  18. if (caption == m_caption)
  19. return;
  20. m_caption = caption;
  21. update();
  22. }
  23. void GButton::set_checked(bool checked)
  24. {
  25. if (m_checked == checked)
  26. return;
  27. m_checked = checked;
  28. update();
  29. }
  30. void GButton::paint_event(GPaintEvent& event)
  31. {
  32. GPainter painter(*this);
  33. painter.add_clip_rect(event.rect());
  34. StylePainter::paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered, m_checkable && m_checked, is_enabled());
  35. if (m_caption.is_empty() && !m_icon)
  36. return;
  37. auto content_rect = rect().shrunken(10, 2);
  38. auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Point();
  39. if (m_icon && !m_caption.is_empty())
  40. icon_location.set_x(content_rect.x());
  41. if (m_being_pressed)
  42. painter.translate(1, 1);
  43. if (m_icon) {
  44. if (is_enabled())
  45. painter.blit(icon_location, *m_icon, m_icon->rect());
  46. else
  47. painter.blit_dimmed(icon_location, *m_icon, m_icon->rect());
  48. }
  49. auto& font = (m_checkable && m_checked) ? Font::default_bold_font() : this->font();
  50. if (m_icon && !m_caption.is_empty()) {
  51. content_rect.move_by(m_icon->width() + 4, 0);
  52. content_rect.set_width(content_rect.width() - m_icon->width() - 4);
  53. }
  54. painter.draw_text(content_rect, m_caption, font, text_alignment(), foreground_color(), TextElision::Right);
  55. }
  56. void GButton::mousemove_event(GMouseEvent& event)
  57. {
  58. if (event.buttons() & GMouseButton::Left) {
  59. if (is_enabled()) {
  60. bool being_pressed = rect().contains(event.position());
  61. if (being_pressed != m_being_pressed) {
  62. m_being_pressed = being_pressed;
  63. update();
  64. }
  65. }
  66. }
  67. GWidget::mousemove_event(event);
  68. }
  69. void GButton::mousedown_event(GMouseEvent& event)
  70. {
  71. #ifdef GBUTTON_DEBUG
  72. dbgprintf("GButton::mouse_down_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  73. #endif
  74. if (event.button() == GMouseButton::Left) {
  75. if (is_enabled()) {
  76. m_being_pressed = true;
  77. update();
  78. }
  79. }
  80. GWidget::mousedown_event(event);
  81. }
  82. void GButton::click()
  83. {
  84. if (!is_enabled())
  85. return;
  86. if (on_click)
  87. on_click(*this);
  88. }
  89. void GButton::mouseup_event(GMouseEvent& event)
  90. {
  91. #ifdef GBUTTON_DEBUG
  92. dbgprintf("GButton::mouse_up_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  93. #endif
  94. if (event.button() == GMouseButton::Left) {
  95. if (is_enabled()) {
  96. bool was_being_pressed = m_being_pressed;
  97. m_being_pressed = false;
  98. update();
  99. if (was_being_pressed)
  100. click();
  101. }
  102. }
  103. GWidget::mouseup_event(event);
  104. }
  105. void GButton::enter_event(CEvent&)
  106. {
  107. m_hovered = true;
  108. update();
  109. }
  110. void GButton::leave_event(CEvent&)
  111. {
  112. m_hovered = false;
  113. update();
  114. }
  115. void GButton::set_action(GAction& action)
  116. {
  117. m_action = action.make_weak_ptr();
  118. action.register_button({ }, *this);
  119. set_enabled(action.is_enabled());
  120. set_checkable(action.is_checkable());
  121. if (action.is_checkable())
  122. set_checked(action.is_checked());
  123. }
  124. void GButton::set_icon(RetainPtr<GraphicsBitmap>&& icon)
  125. {
  126. if (m_icon == icon)
  127. return;
  128. m_icon = move(icon);
  129. update();
  130. }