MessageBox.cpp 4.5 KB

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