GCheckBox.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "GCheckBox.h"
  2. #include <LibGUI/GPainter.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. GPainter painter(*this);
  50. painter.add_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 (event.buttons() == GMouseButton::Left) {
  78. bool being_modified = rect().contains(event.position());
  79. if (being_modified != m_being_modified) {
  80. m_being_modified = being_modified;
  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. 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. if (was_being_pressed)
  106. set_checked(!is_checked());
  107. update();
  108. }
  109. GWidget::mouseup_event(event);
  110. }
  111. void GCheckBox::keydown_event(GKeyEvent& event)
  112. {
  113. if (event.key() == KeyCode::Key_Space) {
  114. set_checked(!is_checked());
  115. update();
  116. }
  117. GWidget::keydown_event(event);
  118. }