MessageBox.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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::try_load_from_file("/res/icons/32x32/msgbox-information.png");
  42. case Type::Warning:
  43. return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-warning.png");
  44. case Type::Error:
  45. return Gfx::Bitmap::try_load_from_file("/res/icons/32x32/msgbox-error.png");
  46. case Type::Question:
  47. return Gfx::Bitmap::try_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. auto number_of_lines = m_text.split('\n').size();
  73. int padded_text_height = widget.font().glyph_height() * 1.6;
  74. int total_text_height = number_of_lines * padded_text_height;
  75. int icon_width = 0;
  76. widget.set_layout<VerticalBoxLayout>();
  77. widget.set_fill_with_background_color(true);
  78. widget.layout()->set_margins({ 8, 8, 8, 8 });
  79. widget.layout()->set_spacing(6);
  80. auto& message_container = widget.add<Widget>();
  81. message_container.set_layout<HorizontalBoxLayout>();
  82. message_container.layout()->set_spacing(8);
  83. if (m_type != Type::None) {
  84. auto& icon_image = message_container.add<ImageWidget>();
  85. icon_image.set_bitmap(icon());
  86. if (icon()) {
  87. icon_width = icon()->width();
  88. if (icon_width > 0)
  89. message_container.layout()->set_margins({ 8, 0, 0, 0 });
  90. }
  91. }
  92. auto& label = message_container.add<Label>(m_text);
  93. label.set_fixed_height(total_text_height);
  94. if (m_type != Type::None)
  95. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  96. auto& button_container = widget.add<Widget>();
  97. button_container.set_layout<HorizontalBoxLayout>();
  98. button_container.set_fixed_height(24);
  99. button_container.layout()->set_spacing(8);
  100. constexpr int button_width = 80;
  101. int button_count = 0;
  102. auto add_button = [&](String label, Dialog::ExecResult result) {
  103. auto& button = button_container.add<Button>();
  104. button.set_fixed_width(button_width);
  105. button.set_text(label);
  106. button.on_click = [this, label, result](auto) {
  107. done(result);
  108. };
  109. ++button_count;
  110. };
  111. button_container.layout()->add_spacer();
  112. if (should_include_ok_button())
  113. add_button("OK", Dialog::ExecOK);
  114. if (should_include_yes_button())
  115. add_button("Yes", Dialog::ExecYes);
  116. if (should_include_no_button())
  117. add_button("No", Dialog::ExecNo);
  118. if (should_include_cancel_button())
  119. add_button("Cancel", Dialog::ExecCancel);
  120. button_container.layout()->add_spacer();
  121. int width = (button_count * button_width) + ((button_count - 1) * button_container.layout()->spacing()) + 32;
  122. width = max(width, text_width + icon_width + 56);
  123. set_rect(x(), y(), width, 80 + label.max_height());
  124. set_resizable(false);
  125. }
  126. }