GCheckBox.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. 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 = 13;
  21. static const int s_box_height = 13;
  22. GCheckBox::GCheckBox(GWidget* parent)
  23. : GAbstractButton(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::paint_event(GPaintEvent& event)
  32. {
  33. GPainter painter(*this);
  34. painter.add_clip_rect(event.rect());
  35. auto text_rect = rect();
  36. text_rect.set_left(s_box_width + 4);
  37. text_rect.set_width(font().width(text()));
  38. text_rect.set_top(height() / 2 - font().glyph_height() / 2);
  39. text_rect.set_height(font().glyph_height());
  40. if (fill_with_background_color())
  41. painter.fill_rect(rect(), background_color());
  42. Rect box_rect {
  43. 0, height() / 2 - s_box_height / 2 - 1,
  44. s_box_width, s_box_height
  45. };
  46. painter.fill_rect(box_rect, Color::White);
  47. StylePainter::paint_frame(painter, box_rect, FrameShape::Container, FrameShadow::Sunken, 2);
  48. if (is_being_pressed())
  49. painter.draw_rect(box_rect.shrunken(4, 4), Color::MidGray);
  50. if (is_checked())
  51. painter.draw_bitmap(box_rect.shrunken(4, 4).location(), *s_checked_bitmap, foreground_color());
  52. if (!text().is_empty()) {
  53. painter.draw_text(text_rect, text(), TextAlignment::TopLeft, foreground_color());
  54. if (is_focused()) {
  55. Rect focus_rect = text_rect;
  56. focus_rect.inflate(6, 4);
  57. painter.draw_rect(focus_rect, Color(140, 140, 140));
  58. }
  59. }
  60. }
  61. void GCheckBox::click()
  62. {
  63. if (!is_enabled())
  64. return;
  65. set_checked(!is_checked());
  66. }