GCheckBox.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. if (on_change)
  44. on_change(*this, b);
  45. update();
  46. }
  47. void GCheckBox::paint_event(GPaintEvent& event)
  48. {
  49. Painter painter(*this);
  50. painter.set_clip_rect(event.rect());
  51. auto text_rect = rect();
  52. text_rect.set_left(s_box_width + 4);
  53. text_rect.set_top(height() / 2 - font().glyph_height() / 2);
  54. if (fill_with_background_color())
  55. painter.fill_rect(rect(), background_color());
  56. Rect box_rect {
  57. 2, height() / 2 - s_box_height / 2 - 1,
  58. s_box_width, s_box_height
  59. };
  60. painter.fill_rect(box_rect, Color::White);
  61. painter.draw_rect(box_rect, Color::Black);
  62. if (m_being_modified)
  63. painter.draw_rect(box_rect.shrunken(2, 2), Color::MidGray);
  64. if (m_checked)
  65. painter.draw_bitmap(box_rect.shrunken(2, 2).location(), *s_checked_bitmap, foreground_color());
  66. if (!caption().is_empty())
  67. painter.draw_text(text_rect, caption(), TextAlignment::TopLeft, foreground_color());
  68. if (is_focused()) {
  69. // NOTE: Painter::draw_focus_rect() will shrink(2,2) the passed rect.
  70. auto focus_rect = box_rect;
  71. focus_rect.inflate(4, 4);
  72. painter.draw_focus_rect(focus_rect);
  73. }
  74. }
  75. void GCheckBox::mousemove_event(GMouseEvent& event)
  76. {
  77. if (m_tracking_cursor) {
  78. bool being_pressed = rect().contains(event.position());
  79. if (being_pressed != m_being_modified) {
  80. m_being_modified = being_pressed;
  81. update();
  82. }
  83. }
  84. GWidget::mousemove_event(event);
  85. }
  86. void GCheckBox::mousedown_event(GMouseEvent& event)
  87. {
  88. #ifdef GCHECKBOX_DEBUG
  89. dbgprintf("GCheckBox::mouse_down_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  90. #endif
  91. if (event.button() == GMouseButton::Left) {
  92. m_being_modified = true;
  93. m_tracking_cursor = true;
  94. set_global_cursor_tracking(true);
  95. update();
  96. }
  97. GWidget::mousedown_event(event);
  98. }
  99. void GCheckBox::mouseup_event(GMouseEvent& event)
  100. {
  101. #ifdef GCHECKBOX_DEBUG
  102. dbgprintf("GCheckBox::mouseup_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  103. #endif
  104. if (event.button() == GMouseButton::Left) {
  105. bool was_being_pressed = m_being_modified;
  106. m_being_modified = false;
  107. m_tracking_cursor = false;
  108. set_global_cursor_tracking(false);
  109. if (was_being_pressed) {
  110. set_checked(!is_checked());
  111. }
  112. update();
  113. }
  114. GWidget::mouseup_event(event);
  115. }
  116. void GCheckBox::keydown_event(GKeyEvent& event)
  117. {
  118. if (!m_tracking_cursor && event.key() == KeyCode::Key_Space) {
  119. set_checked(!is_checked());
  120. update();
  121. }
  122. GWidget::keydown_event(event);
  123. }