GButton.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "GButton.h"
  2. #include <SharedGraphics/Painter.h>
  3. GButton::GButton(GWidget* parent)
  4. : GWidget(parent)
  5. {
  6. set_fill_with_background_color(false);
  7. }
  8. GButton::~GButton()
  9. {
  10. }
  11. void GButton::set_caption(String&& caption)
  12. {
  13. if (caption == m_caption)
  14. return;
  15. m_caption = move(caption);
  16. update();
  17. }
  18. void GButton::paint_event(GPaintEvent&)
  19. {
  20. Color button_color = Color::LightGray;
  21. Color highlight_color = Color::White;
  22. Color shadow_color = Color(96, 96, 96);
  23. Painter painter(*this);
  24. painter.draw_line({ 1, 0 }, { width() - 2, 0 }, Color::Black);
  25. painter.draw_line({ 1, height() - 1 }, { width() - 2, height() - 1}, Color::Black);
  26. painter.draw_line({ 0, 1 }, { 0, height() - 2 }, Color::Black);
  27. painter.draw_line({ width() - 1, 1 }, { width() - 1, height() - 2 }, Color::Black);
  28. if (m_being_pressed) {
  29. // Base
  30. painter.fill_rect({ 1, 1, width() - 2, height() - 2 }, button_color);
  31. // Sunken shadow
  32. painter.draw_line({ 1, 1 }, { width() - 2, 1 }, shadow_color);
  33. painter.draw_line({ 1, 2 }, {1, height() - 2 }, shadow_color);
  34. } else {
  35. // Base
  36. painter.fill_rect({ 3, 3, width() - 5, height() - 5 }, button_color);
  37. // White highlight
  38. painter.draw_line({ 1, 1 }, { width() - 2, 1 }, highlight_color);
  39. painter.draw_line({ 1, 2 }, { width() - 3, 2 }, highlight_color);
  40. painter.draw_line({ 1, 3 }, { 1, height() - 2 }, highlight_color);
  41. painter.draw_line({ 2, 3 }, { 2, height() - 3 }, highlight_color);
  42. // Gray shadow
  43. painter.draw_line({ width() - 2, 1 }, { width() - 2, height() - 4 }, shadow_color);
  44. painter.draw_line({ width() - 3, 2 }, { width() - 3, height() - 4 }, shadow_color);
  45. painter.draw_line({ 1, height() - 2 }, { width() - 2, height() - 2 }, shadow_color);
  46. painter.draw_line({ 2, height() - 3 }, { width() - 2, height() - 3 }, shadow_color);
  47. }
  48. if (!caption().is_empty()) {
  49. auto textRect = rect();
  50. if (m_being_pressed)
  51. textRect.move_by(1, 1);
  52. painter.draw_text(textRect, caption(), Painter::TextAlignment::Center, Color::Black);
  53. }
  54. }
  55. void GButton::mousedown_event(GMouseEvent& event)
  56. {
  57. dbgprintf("Button::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  58. m_being_pressed = true;
  59. update();
  60. GWidget::mousedown_event(event);
  61. }
  62. void GButton::mouseup_event(GMouseEvent& event)
  63. {
  64. dbgprintf("Button::mouseUpEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  65. m_being_pressed = false;
  66. update();
  67. GWidget::mouseup_event(event);
  68. if (on_click)
  69. on_click(*this);
  70. }