GMessageBox.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, InputType input_type, CObject* parent)
  7. {
  8. GMessageBox box(text, title, type, input_type, parent);
  9. box.exec();
  10. }
  11. GMessageBox::GMessageBox(const StringView& text, const StringView& title, Type type, InputType input_type, CObject* parent)
  12. : GDialog(parent)
  13. , m_text(text)
  14. , m_type(type)
  15. , m_input_type(input_type)
  16. {
  17. set_title(title);
  18. build();
  19. }
  20. GMessageBox::~GMessageBox()
  21. {
  22. }
  23. RefPtr<GraphicsBitmap> GMessageBox::icon() const
  24. {
  25. switch (m_type) {
  26. case Type::Information:
  27. return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-information.png");
  28. case Type::Warning:
  29. return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-warning.png");
  30. case Type::Error:
  31. return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-error.png");
  32. default:
  33. return nullptr;
  34. }
  35. }
  36. bool GMessageBox::should_include_ok_button() const
  37. {
  38. return m_input_type == InputType::OK || m_input_type == InputType::OKCancel;
  39. }
  40. bool GMessageBox::should_include_cancel_button() const
  41. {
  42. return m_input_type == InputType::OKCancel;
  43. }
  44. void GMessageBox::build()
  45. {
  46. auto widget = GWidget::construct();
  47. set_main_widget(widget);
  48. int text_width = widget->font().width(m_text);
  49. int icon_width = 0;
  50. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  51. widget->set_fill_with_background_color(true);
  52. widget->layout()->set_margins({ 0, 15, 0, 15 });
  53. widget->layout()->set_spacing(15);
  54. ObjectPtr<GWidget> message_container = widget;
  55. if (m_type != Type::None) {
  56. message_container = GWidget::construct(widget);
  57. message_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  58. message_container->layout()->set_margins({ 8, 0, 8, 0 });
  59. message_container->layout()->set_spacing(8);
  60. auto icon_label = GLabel::construct(message_container);
  61. icon_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  62. icon_label->set_preferred_size(32, 32);
  63. icon_label->set_icon(icon());
  64. icon_width = icon_label->icon()->width();
  65. }
  66. auto label = GLabel::construct(m_text, message_container);
  67. label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  68. label->set_preferred_size(text_width, 16);
  69. auto button_container = GWidget::construct(widget);
  70. button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  71. button_container->layout()->set_spacing(5);
  72. button_container->layout()->set_margins({ 15, 0, 15, 0 });
  73. if (should_include_ok_button()) {
  74. auto* ok_button = new GButton(button_container);
  75. ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  76. ok_button->set_preferred_size(0, 20);
  77. ok_button->set_text("OK");
  78. ok_button->on_click = [this](auto&) {
  79. dbgprintf("GMessageBox: OK button clicked\n");
  80. done(GDialog::ExecOK);
  81. };
  82. }
  83. if (should_include_cancel_button()) {
  84. auto* cancel_button = new GButton(button_container);
  85. cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  86. cancel_button->set_preferred_size(0, 20);
  87. cancel_button->set_text("Cancel");
  88. cancel_button->on_click = [this](auto&) {
  89. dbgprintf("GMessageBox: Cancel button clicked\n");
  90. done(GDialog::ExecCancel);
  91. };
  92. }
  93. set_rect(x(), y(), text_width + icon_width + 80, 100);
  94. set_resizable(false);
  95. }