MessageBox.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/BoxLayout.h>
  7. #include <LibGUI/Button.h>
  8. #include <LibGUI/ImageWidget.h>
  9. #include <LibGUI/Label.h>
  10. #include <LibGUI/MessageBox.h>
  11. #include <LibGfx/Font.h>
  12. #include <stdio.h>
  13. namespace GUI {
  14. int MessageBox::show(Window* parent_window, const StringView& text, const StringView& title, Type type, InputType input_type)
  15. {
  16. auto box = MessageBox::construct(parent_window, text, title, type, input_type);
  17. if (parent_window)
  18. box->set_icon(parent_window->icon());
  19. return box->exec();
  20. }
  21. int MessageBox::show_error(Window* parent_window, const StringView& text)
  22. {
  23. return show(parent_window, text, "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
  24. }
  25. MessageBox::MessageBox(Window* parent_window, const StringView& text, const StringView& title, Type type, InputType input_type)
  26. : Dialog(parent_window)
  27. , m_text(text)
  28. , m_type(type)
  29. , m_input_type(input_type)
  30. {
  31. set_title(title);
  32. build();
  33. }
  34. MessageBox::~MessageBox()
  35. {
  36. }
  37. RefPtr<Gfx::Bitmap> MessageBox::icon() const
  38. {
  39. switch (m_type) {
  40. case Type::Information:
  41. return Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-information.png");
  42. case Type::Warning:
  43. return Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-warning.png");
  44. case Type::Error:
  45. return Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-error.png");
  46. case Type::Question:
  47. return Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-question.png");
  48. default:
  49. return nullptr;
  50. }
  51. }
  52. bool MessageBox::should_include_ok_button() const
  53. {
  54. return m_input_type == InputType::OK || m_input_type == InputType::OKCancel;
  55. }
  56. bool MessageBox::should_include_cancel_button() const
  57. {
  58. return m_input_type == InputType::OKCancel || m_input_type == InputType::YesNoCancel;
  59. }
  60. bool MessageBox::should_include_yes_button() const
  61. {
  62. return m_input_type == InputType::YesNo || m_input_type == InputType::YesNoCancel;
  63. }
  64. bool MessageBox::should_include_no_button() const
  65. {
  66. return should_include_yes_button();
  67. }
  68. void MessageBox::build()
  69. {
  70. auto& widget = set_main_widget<Widget>();
  71. int text_width = widget.font().width(m_text);
  72. int icon_width = 0;
  73. widget.set_layout<VerticalBoxLayout>();
  74. widget.set_fill_with_background_color(true);
  75. widget.layout()->set_margins({ 8, 8, 8, 8 });
  76. widget.layout()->set_spacing(6);
  77. auto& message_container = widget.add<Widget>();
  78. message_container.set_layout<HorizontalBoxLayout>();
  79. message_container.layout()->set_spacing(8);
  80. if (m_type != Type::None) {
  81. auto& icon_image = message_container.add<ImageWidget>();
  82. icon_image.set_bitmap(icon());
  83. if (icon()) {
  84. icon_width = icon()->width();
  85. if (icon_width > 0)
  86. message_container.layout()->set_margins({ 8, 0, 0, 0 });
  87. }
  88. }
  89. auto& label = message_container.add<Label>(m_text);
  90. label.set_fixed_height(16);
  91. if (m_type != Type::None)
  92. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  93. auto& button_container = widget.add<Widget>();
  94. button_container.set_layout<HorizontalBoxLayout>();
  95. button_container.set_fixed_height(24);
  96. button_container.layout()->set_spacing(8);
  97. constexpr int button_width = 80;
  98. int button_count = 0;
  99. auto add_button = [&](String label, Dialog::ExecResult result) {
  100. auto& button = button_container.add<Button>();
  101. button.set_fixed_width(button_width);
  102. button.set_text(label);
  103. button.on_click = [this, label, result](auto) {
  104. done(result);
  105. };
  106. ++button_count;
  107. };
  108. button_container.layout()->add_spacer();
  109. if (should_include_ok_button())
  110. add_button("OK", Dialog::ExecOK);
  111. if (should_include_yes_button())
  112. add_button("Yes", Dialog::ExecYes);
  113. if (should_include_no_button())
  114. add_button("No", Dialog::ExecNo);
  115. if (should_include_cancel_button())
  116. add_button("Cancel", Dialog::ExecCancel);
  117. button_container.layout()->add_spacer();
  118. int width = (button_count * button_width) + ((button_count - 1) * button_container.layout()->spacing()) + 32;
  119. width = max(width, text_width + icon_width + 56);
  120. set_rect(x(), y(), width, 96);
  121. set_resizable(false);
  122. }
  123. }