GroupBox.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/GroupBox.h>
  8. #include <LibGUI/Painter.h>
  9. #include <LibGfx/Font/Font.h>
  10. #include <LibGfx/Palette.h>
  11. #include <LibGfx/StylePainter.h>
  12. REGISTER_WIDGET(GUI, GroupBox)
  13. namespace GUI {
  14. GroupBox::GroupBox(StringView title)
  15. : m_title(title)
  16. {
  17. REGISTER_STRING_PROPERTY("title", title, set_title);
  18. }
  19. Margins GroupBox::content_margins() const
  20. {
  21. return {
  22. (!m_title.is_empty() ? font().glyph_height() + 1 /*room for the focus rect*/ : 2),
  23. 2,
  24. 2,
  25. 2
  26. };
  27. }
  28. void GroupBox::paint_event(PaintEvent& event)
  29. {
  30. Painter painter(*this);
  31. painter.add_clip_rect(event.rect());
  32. Gfx::IntRect frame_rect {
  33. 0, (!m_title.is_empty() ? font().glyph_height() / 2 : 0),
  34. width(), height() - (!m_title.is_empty() ? font().glyph_height() / 2 : 0)
  35. };
  36. Gfx::StylePainter::paint_frame(painter, frame_rect, palette(), Gfx::FrameShape::Box, Gfx::FrameShadow::Sunken, 2);
  37. if (!m_title.is_empty()) {
  38. Gfx::IntRect text_rect { 6, 1, font().width(m_title) + 6, font().glyph_height() };
  39. painter.fill_rect(text_rect, palette().button());
  40. painter.draw_text(text_rect, m_title, Gfx::TextAlignment::Center, palette().button_text());
  41. }
  42. }
  43. void GroupBox::fonts_change_event(FontsChangeEvent& event)
  44. {
  45. Widget::fonts_change_event(event);
  46. invalidate_layout();
  47. }
  48. void GroupBox::set_title(StringView title)
  49. {
  50. if (m_title == title)
  51. return;
  52. m_title = title;
  53. update();
  54. }
  55. }