GCheckBox.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "GCheckBox.h"
  2. #include <SharedGraphics/Painter.h>
  3. #include <SharedGraphics/CharacterBitmap.h>
  4. #include <Kernel/KeyCode.h>
  5. //#define GCHECKBOX_DEBUG
  6. static const char* s_checked_bitmap_data = {
  7. " "
  8. " ## "
  9. " ## "
  10. " ## "
  11. " ## "
  12. " ## ## "
  13. " #### "
  14. " ## "
  15. " "
  16. };
  17. static CharacterBitmap* s_checked_bitmap;
  18. static const int s_checked_bitmap_width = 9;
  19. static const int s_checked_bitmap_height = 9;
  20. static const int s_box_width = 11;
  21. static const int s_box_height = 11;
  22. GCheckBox::GCheckBox(GWidget* parent)
  23. : GWidget(parent)
  24. {
  25. if (!s_checked_bitmap)
  26. s_checked_bitmap = &CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
  27. }
  28. GCheckBox::~GCheckBox()
  29. {
  30. }
  31. void GCheckBox::set_caption(String&& caption)
  32. {
  33. if (caption == m_caption)
  34. return;
  35. m_caption = move(caption);
  36. update();
  37. }
  38. void GCheckBox::set_checked(bool b)
  39. {
  40. if (m_checked == b)
  41. return;
  42. m_checked = b;
  43. update();
  44. }
  45. void GCheckBox::paint_event(GPaintEvent&)
  46. {
  47. Painter painter(*this);
  48. auto text_rect = rect();
  49. text_rect.set_left(s_box_width + 4);
  50. text_rect.set_top(height() / 2 - font().glyph_height() / 2);
  51. if (fill_with_background_color())
  52. painter.fill_rect(rect(), background_color());
  53. Rect box_rect {
  54. 2, height() / 2 - s_box_height / 2 - 1,
  55. s_box_width, s_box_height
  56. };
  57. painter.fill_rect(box_rect, Color::White);
  58. painter.draw_rect(box_rect, Color::Black);
  59. if (m_being_modified)
  60. painter.draw_rect(box_rect.shrunken(2, 2), Color::MidGray);
  61. if (m_checked)
  62. painter.draw_bitmap(box_rect.shrunken(2, 2).location(), *s_checked_bitmap, foreground_color());
  63. if (!caption().is_empty())
  64. painter.draw_text(text_rect, caption(), TextAlignment::TopLeft, foreground_color());
  65. if (is_focused()) {
  66. // NOTE: Painter::draw_focus_rect() will shrink(2,2) the passed rect.
  67. auto focus_rect = box_rect;
  68. focus_rect.inflate(4, 4);
  69. painter.draw_focus_rect(focus_rect);
  70. }
  71. }
  72. void GCheckBox::mousemove_event(GMouseEvent& event)
  73. {
  74. if (m_tracking_cursor) {
  75. bool being_pressed = rect().contains(event.position());
  76. if (being_pressed != m_being_modified) {
  77. m_being_modified = being_pressed;
  78. update();
  79. }
  80. }
  81. GWidget::mousemove_event(event);
  82. }
  83. void GCheckBox::mousedown_event(GMouseEvent& event)
  84. {
  85. #ifdef GCHECKBOX_DEBUG
  86. dbgprintf("GCheckBox::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. m_being_modified = true;
  90. m_tracking_cursor = true;
  91. set_global_cursor_tracking(true);
  92. update();
  93. }
  94. GWidget::mousedown_event(event);
  95. }
  96. void GCheckBox::mouseup_event(GMouseEvent& event)
  97. {
  98. #ifdef GCHECKBOX_DEBUG
  99. dbgprintf("GCheckBox::mouseup_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  100. #endif
  101. if (event.button() == GMouseButton::Left) {
  102. bool was_being_pressed = m_being_modified;
  103. m_being_modified = false;
  104. m_tracking_cursor = false;
  105. set_global_cursor_tracking(false);
  106. if (was_being_pressed) {
  107. set_checked(!is_checked());
  108. }
  109. update();
  110. }
  111. GWidget::mouseup_event(event);
  112. }
  113. void GCheckBox::keydown_event(GKeyEvent& event)
  114. {
  115. if (!m_tracking_cursor && event.key() == KeyCode::Key_Space) {
  116. set_checked(!is_checked());
  117. update();
  118. }
  119. GWidget::keydown_event(event);
  120. }