CheckBox.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2018-2023, 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. CheckBox::CheckBox(String text)
  16. : AbstractButton(move(text))
  17. {
  18. REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
  19. REGISTER_ENUM_PROPERTY(
  20. "checkbox_position", checkbox_position, set_checkbox_position, CheckBoxPosition,
  21. { CheckBoxPosition::Left, "Left" },
  22. { CheckBoxPosition::Right, "Right" });
  23. set_min_size(SpecialDimension::Shrink, SpecialDimension::Shrink);
  24. set_preferred_size(SpecialDimension::OpportunisticGrow, SpecialDimension::Shrink);
  25. }
  26. int CheckBox::gap_between_box_and_rect() const
  27. {
  28. return 6;
  29. }
  30. int CheckBox::horizontal_padding() const
  31. {
  32. return 2;
  33. }
  34. Gfx::IntRect CheckBox::box_rect() const
  35. {
  36. int box_size = max(13, height() - 10);
  37. Gfx::IntRect box_rect {
  38. 0,
  39. height() / 2 - box_size / 2 - 1,
  40. box_size,
  41. box_size,
  42. };
  43. if (m_checkbox_position == CheckBoxPosition::Right)
  44. box_rect.set_right_without_resize(rect().right());
  45. return box_rect;
  46. }
  47. void CheckBox::paint_event(PaintEvent& event)
  48. {
  49. Painter painter(*this);
  50. painter.add_clip_rect(event.rect());
  51. auto box_rect = this->box_rect();
  52. auto text_rect = rect();
  53. if (m_checkbox_position == CheckBoxPosition::Left)
  54. text_rect.set_left(box_rect.right() + gap_between_box_and_rect());
  55. text_rect.set_width(font().width_rounded_up(text()));
  56. text_rect.set_top(height() / 2 - font().pixel_size_rounded_up() / 2);
  57. text_rect.set_height(font().pixel_size_rounded_up());
  58. if (fill_with_background_color())
  59. painter.fill_rect(rect(), palette().window());
  60. if (is_enabled() && is_hovered())
  61. painter.fill_rect(rect(), palette().hover_highlight());
  62. Gfx::StylePainter::paint_check_box(painter, box_rect, palette(), is_enabled(), is_checked(), is_being_pressed());
  63. paint_text(painter, text_rect, font(), Gfx::TextAlignment::TopLeft);
  64. if (is_focused())
  65. painter.draw_focus_rect(text_rect.inflated(6, 6), palette().focus_outline());
  66. }
  67. void CheckBox::click(unsigned)
  68. {
  69. if (!is_enabled())
  70. return;
  71. set_checked(!is_checked());
  72. }
  73. void CheckBox::set_autosize(bool autosize)
  74. {
  75. if (m_autosize == autosize)
  76. return;
  77. m_autosize = autosize;
  78. if (m_autosize)
  79. size_to_fit();
  80. }
  81. void CheckBox::size_to_fit()
  82. {
  83. set_fixed_width(box_rect().width() + gap_between_box_and_rect() + font().width_rounded_up(text()) + horizontal_padding() * 2);
  84. }
  85. Optional<UISize> CheckBox::calculated_min_size() const
  86. {
  87. auto const& font = this->font();
  88. int width = box_rect().width();
  89. int height = max(22, max(static_cast<int>(ceilf(font.pixel_size())) + 8, box_rect().height()));
  90. return UISize(width, height);
  91. }
  92. }