GCheckBox.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <Kernel/KeyCode.h>
  2. #include <LibGUI/GCheckBox.h>
  3. #include <LibGUI/GPainter.h>
  4. #include <LibDraw/CharacterBitmap.h>
  5. #include <LibDraw/StylePainter.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. }
  26. GCheckBox::GCheckBox(const StringView& text, GWidget* parent)
  27. : GAbstractButton(text, parent)
  28. {
  29. }
  30. GCheckBox::~GCheckBox()
  31. {
  32. }
  33. void GCheckBox::paint_event(GPaintEvent& event)
  34. {
  35. GPainter painter(*this);
  36. painter.add_clip_rect(event.rect());
  37. auto text_rect = rect();
  38. text_rect.set_left(s_box_width + 4);
  39. text_rect.set_width(font().width(text()));
  40. text_rect.set_top(height() / 2 - font().glyph_height() / 2);
  41. text_rect.set_height(font().glyph_height());
  42. if (fill_with_background_color())
  43. painter.fill_rect(rect(), background_color());
  44. Rect box_rect {
  45. 0, height() / 2 - s_box_height / 2 - 1,
  46. s_box_width, s_box_height
  47. };
  48. painter.fill_rect(box_rect, Color::White);
  49. StylePainter::paint_frame(painter, box_rect, FrameShape::Container, FrameShadow::Sunken, 2);
  50. if (is_being_pressed())
  51. painter.draw_rect(box_rect.shrunken(4, 4), Color::MidGray);
  52. if (is_checked()) {
  53. if (!s_checked_bitmap)
  54. s_checked_bitmap = &CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
  55. painter.draw_bitmap(box_rect.shrunken(4, 4).location(), *s_checked_bitmap, foreground_color());
  56. }
  57. paint_text(painter, text_rect, font(), TextAlignment::TopLeft);
  58. }
  59. void GCheckBox::click()
  60. {
  61. if (!is_enabled())
  62. return;
  63. set_checked(!is_checked());
  64. }