RadioButton.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/Painter.h>
  8. #include <LibGUI/RadioButton.h>
  9. #include <LibGfx/Bitmap.h>
  10. #include <LibGfx/Font/Font.h>
  11. #include <LibGfx/Palette.h>
  12. #include <LibGfx/StylePainter.h>
  13. REGISTER_WIDGET(GUI, RadioButton)
  14. namespace GUI {
  15. RadioButton::RadioButton(String text)
  16. : AbstractButton(move(text))
  17. {
  18. set_exclusive(true);
  19. set_checkable(true);
  20. set_min_width(32);
  21. set_fixed_height(22);
  22. }
  23. Gfx::IntSize RadioButton::circle_size()
  24. {
  25. return { 12, 12 };
  26. }
  27. void RadioButton::paint_event(PaintEvent& event)
  28. {
  29. Painter painter(*this);
  30. painter.add_clip_rect(event.rect());
  31. if (fill_with_background_color())
  32. painter.fill_rect(rect(), palette().window());
  33. if (is_enabled() && is_hovered())
  34. painter.fill_rect(rect(), palette().hover_highlight());
  35. Gfx::IntRect circle_rect { { 2, 0 }, circle_size() };
  36. circle_rect.center_vertically_within(rect());
  37. Gfx::StylePainter::paint_radio_button(painter, circle_rect, palette(), is_checked(), is_being_pressed());
  38. Gfx::IntRect text_rect { circle_rect.right() + 7, 0, font().width(text()), font().glyph_height() };
  39. text_rect.center_vertically_within(rect());
  40. paint_text(painter, text_rect, font(), Gfx::TextAlignment::TopLeft);
  41. if (is_focused())
  42. painter.draw_focus_rect(text_rect.inflated(6, 6), palette().focus_outline());
  43. }
  44. void RadioButton::click(unsigned)
  45. {
  46. if (!is_enabled())
  47. return;
  48. set_checked(true);
  49. }
  50. }