GCheckBox.cpp 3.4 KB

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