GMessageBox.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <LibGUI/GBoxLayout.h>
  2. #include <LibGUI/GButton.h>
  3. #include <LibGUI/GLabel.h>
  4. #include <LibGUI/GMessageBox.h>
  5. #include <stdio.h>
  6. void GMessageBox::show(const StringView& text, const StringView& title, Type type, CObject* parent)
  7. {
  8. GMessageBox box(text, title, type, parent);
  9. box.exec();
  10. }
  11. GMessageBox::GMessageBox(const StringView& text, const StringView& title, Type type, CObject* parent)
  12. : GDialog(parent)
  13. , m_text(text)
  14. , m_type(type)
  15. {
  16. set_title(title);
  17. build();
  18. }
  19. GMessageBox::~GMessageBox()
  20. {
  21. }
  22. RefPtr<GraphicsBitmap> GMessageBox::icon() const
  23. {
  24. switch (m_type) {
  25. case Type::Information:
  26. return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-information.png");
  27. case Type::Warning:
  28. return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-warning.png");
  29. case Type::Error:
  30. return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-error.png");
  31. default:
  32. return nullptr;
  33. }
  34. }
  35. void GMessageBox::build()
  36. {
  37. auto* widget = new GWidget;
  38. set_main_widget(widget);
  39. int text_width = widget->font().width(m_text);
  40. int icon_width = 0;
  41. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  42. widget->set_fill_with_background_color(true);
  43. widget->layout()->set_margins({ 0, 15, 0, 15 });
  44. widget->layout()->set_spacing(15);
  45. GWidget* message_container = widget;
  46. if (m_type != Type::None) {
  47. message_container = new GWidget(widget);
  48. message_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  49. message_container->layout()->set_margins({ 8, 0, 8, 0 });
  50. message_container->layout()->set_spacing(8);
  51. auto* icon_label = new GLabel(message_container);
  52. icon_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  53. icon_label->set_preferred_size({ 32, 32 });
  54. icon_label->set_icon(icon());
  55. icon_width = icon_label->icon()->width();
  56. }
  57. auto* label = new GLabel(m_text, message_container);
  58. label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  59. label->set_preferred_size({ text_width, 16 });
  60. auto* button = new GButton(widget);
  61. button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  62. button->set_preferred_size({ 100, 20 });
  63. button->set_text("OK");
  64. button->on_click = [this](auto&) {
  65. dbgprintf("GMessageBox: OK button clicked\n");
  66. done(0);
  67. };
  68. set_rect(x(), y(), text_width + icon_width + 80, 100);
  69. set_resizable(false);
  70. }