GButton.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. if (!m_caption.is_empty()) {
  57. painter.draw_text(content_rect, m_caption, font, text_alignment(), foreground_color(), TextElision::Right);
  58. if (is_focused()) {
  59. Rect focus_rect = { 0, 0, font.width(m_caption), font.glyph_height() };
  60. focus_rect.inflate(6, 4);
  61. focus_rect.center_within(content_rect);
  62. painter.draw_rect(focus_rect, Color(140, 140, 140));
  63. }
  64. }
  65. } else {
  66. painter.draw_text(content_rect.translated(1, 1), m_caption, font, text_alignment(), Color::White, TextElision::Right);
  67. painter.draw_text(content_rect, m_caption, font, text_alignment(), Color::from_rgb(0x808080), TextElision::Right);
  68. }
  69. }
  70. void GButton::mousemove_event(GMouseEvent& event)
  71. {
  72. bool is_over = rect().contains(event.position());
  73. m_hovered = is_over;
  74. if (event.buttons() & GMouseButton::Left) {
  75. if (is_enabled()) {
  76. bool being_pressed = is_over;
  77. if (being_pressed != m_being_pressed) {
  78. m_being_pressed = being_pressed;
  79. update();
  80. }
  81. }
  82. }
  83. GWidget::mousemove_event(event);
  84. }
  85. void GButton::mousedown_event(GMouseEvent& event)
  86. {
  87. #ifdef GBUTTON_DEBUG
  88. dbgprintf("GButton::mouse_down_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  89. #endif
  90. if (event.button() == GMouseButton::Left) {
  91. if (is_enabled()) {
  92. m_being_pressed = true;
  93. update();
  94. }
  95. }
  96. GWidget::mousedown_event(event);
  97. }
  98. void GButton::click()
  99. {
  100. if (!is_enabled())
  101. return;
  102. if (on_click)
  103. on_click(*this);
  104. }
  105. void GButton::mouseup_event(GMouseEvent& event)
  106. {
  107. #ifdef GBUTTON_DEBUG
  108. dbgprintf("GButton::mouse_up_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  109. #endif
  110. if (event.button() == GMouseButton::Left) {
  111. if (is_enabled()) {
  112. bool was_being_pressed = m_being_pressed;
  113. m_being_pressed = false;
  114. update();
  115. if (was_being_pressed)
  116. click();
  117. }
  118. }
  119. GWidget::mouseup_event(event);
  120. }
  121. void GButton::enter_event(CEvent&)
  122. {
  123. m_hovered = true;
  124. update();
  125. }
  126. void GButton::leave_event(CEvent&)
  127. {
  128. m_hovered = false;
  129. update();
  130. }
  131. void GButton::set_action(GAction& action)
  132. {
  133. m_action = action.make_weak_ptr();
  134. action.register_button({ }, *this);
  135. set_enabled(action.is_enabled());
  136. set_checkable(action.is_checkable());
  137. if (action.is_checkable())
  138. set_checked(action.is_checked());
  139. }
  140. void GButton::set_icon(RetainPtr<GraphicsBitmap>&& icon)
  141. {
  142. if (m_icon == icon)
  143. return;
  144. m_icon = move(icon);
  145. update();
  146. }
  147. void GButton::keydown_event(GKeyEvent& event)
  148. {
  149. if (event.key() == KeyCode::Key_Return)
  150. click();
  151. GWidget::keydown_event(event);
  152. }