CheckBox.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/CheckBox.h>
  8. #include <LibGUI/Painter.h>
  9. #include <LibGfx/CharacterBitmap.h>
  10. #include <LibGfx/Font/Font.h>
  11. #include <LibGfx/Palette.h>
  12. #include <LibGfx/StylePainter.h>
  13. REGISTER_WIDGET(GUI, CheckBox)
  14. namespace GUI {
  15. static constexpr int s_box_width = 13;
  16. static constexpr int s_box_height = 13;
  17. static constexpr int s_horizontal_padding = 6;
  18. CheckBox::CheckBox(DeprecatedString deprecated_text)
  19. : CheckBox(String::from_deprecated_string(deprecated_text).release_value_but_fixme_should_propagate_errors())
  20. {
  21. }
  22. CheckBox::CheckBox(String text)
  23. : AbstractButton(move(text))
  24. {
  25. REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
  26. REGISTER_ENUM_PROPERTY(
  27. "checkbox_position", checkbox_position, set_checkbox_position, CheckBoxPosition,
  28. { CheckBoxPosition::Left, "Left" },
  29. { CheckBoxPosition::Right, "Right" });
  30. set_min_size({ 22, 22 });
  31. set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 });
  32. }
  33. void CheckBox::paint_event(PaintEvent& event)
  34. {
  35. Painter painter(*this);
  36. painter.add_clip_rect(event.rect());
  37. auto text_rect = rect();
  38. if (m_checkbox_position == CheckBoxPosition::Left)
  39. text_rect.set_left(s_box_width + s_horizontal_padding);
  40. text_rect.set_width(font().width(text_deprecated()));
  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. if (is_enabled() && is_hovered())
  46. painter.fill_rect(rect(), palette().hover_highlight());
  47. Gfx::IntRect box_rect {
  48. 0, height() / 2 - s_box_height / 2 - 1,
  49. s_box_width, s_box_height
  50. };
  51. if (m_checkbox_position == CheckBoxPosition::Right)
  52. box_rect.set_right_without_resize(rect().right());
  53. Gfx::StylePainter::paint_check_box(painter, box_rect, palette(), is_enabled(), is_checked(), is_being_pressed());
  54. paint_text(painter, text_rect, font(), Gfx::TextAlignment::TopLeft);
  55. if (is_focused())
  56. painter.draw_focus_rect(text_rect.inflated(6, 6), palette().focus_outline());
  57. }
  58. void CheckBox::click(unsigned)
  59. {
  60. if (!is_enabled())
  61. return;
  62. set_checked(!is_checked());
  63. }
  64. void CheckBox::set_autosize(bool autosize)
  65. {
  66. if (m_autosize == autosize)
  67. return;
  68. m_autosize = autosize;
  69. if (m_autosize)
  70. size_to_fit();
  71. }
  72. void CheckBox::size_to_fit()
  73. {
  74. set_fixed_width(s_box_width + font().width(text_deprecated()) + s_horizontal_padding * 2);
  75. }
  76. }