GMessageBox.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <LibGUI/GMessageBox.h>
  2. #include <LibGUI/GBoxLayout.h>
  3. #include <LibGUI/GLabel.h>
  4. #include <LibGUI/GButton.h>
  5. GMessageBox::GMessageBox(const String& text, const String& title, CObject* parent)
  6. : GDialog(parent)
  7. , m_text(text)
  8. {
  9. set_title(title);
  10. build();
  11. }
  12. GMessageBox::~GMessageBox()
  13. {
  14. }
  15. void GMessageBox::build()
  16. {
  17. auto* widget = new GWidget;
  18. set_main_widget(widget);
  19. int text_width = widget->font().width(m_text);
  20. set_rect(x(), y(), text_width + 80, 80);
  21. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  22. widget->set_fill_with_background_color(true);
  23. widget->layout()->set_margins({ 0, 15, 0, 15 });
  24. widget->layout()->set_spacing(15);
  25. auto* label = new GLabel(m_text, widget);
  26. label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  27. label->set_preferred_size({ text_width, 16 });
  28. auto* button = new GButton(widget);
  29. button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  30. button->set_preferred_size({ 100, 20 });
  31. button->set_caption("OK");
  32. button->on_click = [this] (auto&) {
  33. dbgprintf("GMessageBox: OK button clicked\n");
  34. done(0);
  35. };
  36. }