GButton.cpp 4.5 KB

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