GCheckBox.cpp 3.6 KB

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