RadioButton.cpp 1.5 KB

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