MessageBox.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <LibGUI/BoxLayout.h>
  8. #include <LibGUI/Button.h>
  9. #include <LibGUI/ImageWidget.h>
  10. #include <LibGUI/Label.h>
  11. #include <LibGUI/MessageBox.h>
  12. #include <LibGfx/Font.h>
  13. namespace GUI {
  14. int MessageBox::show(Window* parent_window, StringView text, 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, StringView text)
  22. {
  23. return show(parent_window, text, "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
  24. }
  25. int MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<Time> last_unmodified_timestamp)
  26. {
  27. StringBuilder builder;
  28. builder.append("Save changes to ");
  29. if (path.is_empty())
  30. builder.append("untitled document");
  31. else
  32. builder.appendff("\"{}\"", LexicalPath::basename(path));
  33. builder.append(" before closing?");
  34. if (!path.is_empty() && last_unmodified_timestamp.has_value()) {
  35. auto age = (Time::now_monotonic() - *last_unmodified_timestamp).to_seconds();
  36. builder.appendff("\nLast saved {} second{} ago.", age, age == 1 ? "" : "s");
  37. }
  38. auto box = MessageBox::construct(parent_window, builder.string_view(), "Unsaved changes", Type::Warning, InputType::YesNoCancel);
  39. if (parent_window)
  40. box->set_icon(parent_window->icon());
  41. box->m_yes_button->set_text(path.is_empty() ? "Save As..." : "Save");
  42. box->m_no_button->set_text("Close");
  43. box->m_cancel_button->set_text("Cancel");
  44. return box->exec();
  45. }
  46. MessageBox::MessageBox(Window* parent_window, StringView text, StringView title, Type type, InputType input_type)
  47. : Dialog(parent_window)
  48. , m_text(text)
  49. , m_type(type)
  50. , m_input_type(input_type)
  51. {
  52. set_title(title);
  53. build();
  54. }
  55. MessageBox::~MessageBox()
  56. {
  57. }
  58. RefPtr<Gfx::Bitmap> MessageBox::icon() const
  59. {
  60. switch (m_type) {
  61. case Type::Information:
  62. return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-information.png").release_value_but_fixme_should_propagate_errors();
  63. case Type::Warning:
  64. return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-warning.png").release_value_but_fixme_should_propagate_errors();
  65. case Type::Error:
  66. return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-error.png").release_value_but_fixme_should_propagate_errors();
  67. case Type::Question:
  68. return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-question.png").release_value_but_fixme_should_propagate_errors();
  69. default:
  70. return nullptr;
  71. }
  72. }
  73. bool MessageBox::should_include_ok_button() const
  74. {
  75. return m_input_type == InputType::OK || m_input_type == InputType::OKCancel;
  76. }
  77. bool MessageBox::should_include_cancel_button() const
  78. {
  79. return m_input_type == InputType::OKCancel || m_input_type == InputType::YesNoCancel;
  80. }
  81. bool MessageBox::should_include_yes_button() const
  82. {
  83. return m_input_type == InputType::YesNo || m_input_type == InputType::YesNoCancel;
  84. }
  85. bool MessageBox::should_include_no_button() const
  86. {
  87. return should_include_yes_button();
  88. }
  89. void MessageBox::build()
  90. {
  91. auto& widget = set_main_widget<Widget>();
  92. int text_width = widget.font().width(m_text);
  93. auto number_of_lines = m_text.split('\n').size();
  94. int padded_text_height = widget.font().glyph_height() * 1.6;
  95. int total_text_height = number_of_lines * padded_text_height;
  96. int icon_width = 0;
  97. widget.set_layout<VerticalBoxLayout>();
  98. widget.set_fill_with_background_color(true);
  99. widget.layout()->set_margins(8);
  100. widget.layout()->set_spacing(6);
  101. auto& message_container = widget.add<Widget>();
  102. message_container.set_layout<HorizontalBoxLayout>();
  103. message_container.layout()->set_spacing(8);
  104. if (m_type != Type::None) {
  105. auto& icon_image = message_container.add<ImageWidget>();
  106. icon_image.set_bitmap(icon());
  107. if (icon()) {
  108. icon_width = icon()->width();
  109. if (icon_width > 0)
  110. message_container.layout()->set_margins({ 0, 0, 0, 8 });
  111. }
  112. }
  113. auto& label = message_container.add<Label>(m_text);
  114. label.set_fixed_height(total_text_height);
  115. if (m_type != Type::None)
  116. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  117. auto& button_container = widget.add<Widget>();
  118. button_container.set_layout<HorizontalBoxLayout>();
  119. button_container.set_fixed_height(24);
  120. button_container.layout()->set_spacing(8);
  121. constexpr int button_width = 80;
  122. int button_count = 0;
  123. auto add_button = [&](String label, Dialog::ExecResult result) -> GUI::Button& {
  124. auto& button = button_container.add<Button>();
  125. button.set_fixed_width(button_width);
  126. button.set_text(label);
  127. button.on_click = [this, label, result](auto) {
  128. done(result);
  129. };
  130. ++button_count;
  131. return button;
  132. };
  133. button_container.layout()->add_spacer();
  134. if (should_include_ok_button())
  135. m_ok_button = add_button("OK", Dialog::ExecOK);
  136. if (should_include_yes_button())
  137. m_yes_button = add_button("Yes", Dialog::ExecYes);
  138. if (should_include_no_button())
  139. m_no_button = add_button("No", Dialog::ExecNo);
  140. if (should_include_cancel_button())
  141. m_cancel_button = add_button("Cancel", Dialog::ExecCancel);
  142. button_container.layout()->add_spacer();
  143. int width = (button_count * button_width) + ((button_count - 1) * button_container.layout()->spacing()) + 32;
  144. width = max(width, text_width + icon_width + 56);
  145. set_rect(x(), y(), width, 80 + label.max_height());
  146. set_resizable(false);
  147. }
  148. }