RadioButton.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_checkable(true);
  19. set_min_width(32);
  20. set_fixed_height(22);
  21. }
  22. RadioButton::~RadioButton()
  23. {
  24. }
  25. Gfx::IntSize RadioButton::circle_size()
  26. {
  27. return { 12, 12 };
  28. }
  29. void RadioButton::paint_event(PaintEvent& event)
  30. {
  31. Painter painter(*this);
  32. painter.add_clip_rect(event.rect());
  33. if (fill_with_background_color())
  34. painter.fill_rect(rect(), palette().window());
  35. if (is_enabled() && is_hovered())
  36. painter.fill_rect(rect(), palette().hover_highlight());
  37. Gfx::IntRect circle_rect { { 2, 0 }, circle_size() };
  38. circle_rect.center_vertically_within(rect());
  39. Gfx::StylePainter::paint_radio_button(painter, circle_rect, palette(), is_checked(), is_being_pressed());
  40. Gfx::IntRect text_rect { circle_rect.right() + 7, 0, font().width(text()), font().glyph_height() };
  41. text_rect.center_vertically_within(rect());
  42. paint_text(painter, text_rect, font(), Gfx::TextAlignment::TopLeft);
  43. if (is_focused())
  44. painter.draw_focus_rect(text_rect.inflated(6, 6), palette().focus_outline());
  45. }
  46. void RadioButton::click(unsigned)
  47. {
  48. if (!is_enabled())
  49. return;
  50. set_checked(true);
  51. }
  52. }