GroupBox.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/GroupBox.h>
  7. #include <LibGUI/Painter.h>
  8. #include <LibGfx/Font.h>
  9. #include <LibGfx/Palette.h>
  10. #include <LibGfx/StylePainter.h>
  11. REGISTER_WIDGET(GUI, GroupBox)
  12. namespace GUI {
  13. GroupBox::GroupBox(const StringView& title)
  14. : m_title(title)
  15. {
  16. REGISTER_STRING_PROPERTY("title", title, set_title);
  17. }
  18. GroupBox::~GroupBox()
  19. {
  20. }
  21. void GroupBox::paint_event(PaintEvent& event)
  22. {
  23. Painter painter(*this);
  24. painter.add_clip_rect(event.rect());
  25. Gfx::IntRect frame_rect {
  26. 0, (!m_title.is_empty() ? font().glyph_height() / 2 : 0),
  27. width(), height() - (!m_title.is_empty() ? font().glyph_height() / 2 : 0)
  28. };
  29. Gfx::StylePainter::paint_frame(painter, frame_rect, palette(), Gfx::FrameShape::Box, Gfx::FrameShadow::Sunken, 2);
  30. if (!m_title.is_empty()) {
  31. Gfx::IntRect text_rect { 6, 1, font().width(m_title) + 6, font().glyph_height() };
  32. painter.fill_rect(text_rect, palette().button());
  33. painter.draw_text(text_rect, m_title, Gfx::TextAlignment::Center, palette().button_text());
  34. }
  35. }
  36. void GroupBox::set_title(const StringView& title)
  37. {
  38. if (m_title == title)
  39. return;
  40. m_title = title;
  41. update();
  42. }
  43. }