GButton.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "GButton.h"
  2. #include <SharedGraphics/Painter.h>
  3. //#define GBUTTON_DEBUG
  4. GButton::GButton(GWidget* parent)
  5. : GWidget(parent)
  6. {
  7. set_fill_with_background_color(false);
  8. }
  9. GButton::~GButton()
  10. {
  11. }
  12. void GButton::set_caption(String&& caption)
  13. {
  14. if (caption == m_caption)
  15. return;
  16. m_caption = move(caption);
  17. update();
  18. }
  19. void GButton::paint_event(GPaintEvent&)
  20. {
  21. Color button_color = Color::LightGray;
  22. Color highlight_color = Color::White;
  23. Color shadow_color = Color(96, 96, 96);
  24. Painter painter(*this);
  25. painter.draw_line({ 1, 0 }, { width() - 2, 0 }, Color::Black);
  26. painter.draw_line({ 1, height() - 1 }, { width() - 2, height() - 1}, Color::Black);
  27. painter.draw_line({ 0, 1 }, { 0, height() - 2 }, Color::Black);
  28. painter.draw_line({ width() - 1, 1 }, { width() - 1, height() - 2 }, Color::Black);
  29. if (m_being_pressed) {
  30. // Base
  31. painter.fill_rect({ 1, 1, width() - 2, height() - 2 }, button_color);
  32. // Sunken shadow
  33. painter.draw_line({ 1, 1 }, { width() - 2, 1 }, shadow_color);
  34. painter.draw_line({ 1, 2 }, {1, height() - 2 }, shadow_color);
  35. } else {
  36. // Base
  37. painter.fill_rect({ 3, 3, width() - 5, height() - 5 }, button_color);
  38. painter.fill_rect_with_gradient({ 3, 3, width() - 5, height() - 5 }, button_color, Color::White);
  39. // White highlight
  40. painter.draw_line({ 1, 1 }, { width() - 2, 1 }, highlight_color);
  41. painter.draw_line({ 1, 2 }, { width() - 3, 2 }, highlight_color);
  42. painter.draw_line({ 1, 3 }, { 1, height() - 2 }, highlight_color);
  43. painter.draw_line({ 2, 3 }, { 2, height() - 3 }, highlight_color);
  44. // Gray shadow
  45. painter.draw_line({ width() - 2, 1 }, { width() - 2, height() - 4 }, shadow_color);
  46. painter.draw_line({ width() - 3, 2 }, { width() - 3, height() - 4 }, shadow_color);
  47. painter.draw_line({ 1, height() - 2 }, { width() - 2, height() - 2 }, shadow_color);
  48. painter.draw_line({ 2, height() - 3 }, { width() - 2, height() - 3 }, shadow_color);
  49. }
  50. if (!caption().is_empty()) {
  51. auto text_rect = rect();
  52. if (m_being_pressed)
  53. text_rect.move_by(1, 1);
  54. painter.draw_text(text_rect, caption(), Painter::TextAlignment::Center, Color::Black);
  55. }
  56. }
  57. void GButton::mousemove_event(GMouseEvent& event)
  58. {
  59. if (m_tracking_cursor) {
  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. GWidget::mousemove_event(event);
  67. }
  68. void GButton::mousedown_event(GMouseEvent& event)
  69. {
  70. #ifdef GBUTTON_DEBUG
  71. dbgprintf("GButton::mouse_down_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  72. #endif
  73. if (event.button() == GMouseButton::Left) {
  74. m_being_pressed = true;
  75. m_tracking_cursor = true;
  76. set_global_cursor_tracking(true);
  77. update();
  78. }
  79. GWidget::mousedown_event(event);
  80. }
  81. void GButton::mouseup_event(GMouseEvent& event)
  82. {
  83. #ifdef GBUTTON_DEBUG
  84. dbgprintf("GButton::mouse_up_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  85. #endif
  86. if (event.button() == GMouseButton::Left) {
  87. bool was_being_pressed = m_being_pressed;
  88. m_being_pressed = false;
  89. m_tracking_cursor = false;
  90. set_global_cursor_tracking(false);
  91. update();
  92. if (was_being_pressed) {
  93. if (on_click)
  94. on_click(*this);
  95. }
  96. }
  97. GWidget::mouseup_event(event);
  98. }