GCheckBox.cpp 2.0 KB

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