MessageBox.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/BoxLayout.h>
  27. #include <LibGUI/Button.h>
  28. #include <LibGUI/ImageWidget.h>
  29. #include <LibGUI/Label.h>
  30. #include <LibGUI/MessageBox.h>
  31. #include <LibGfx/Font.h>
  32. #include <stdio.h>
  33. namespace GUI {
  34. int MessageBox::show(Window* parent_window, const StringView& text, const StringView& title, Type type, InputType input_type)
  35. {
  36. auto box = MessageBox::construct(parent_window, text, title, type, input_type);
  37. if (parent_window)
  38. box->set_icon(parent_window->icon());
  39. return box->exec();
  40. }
  41. int MessageBox::show_error(Window* parent_window, const StringView& text)
  42. {
  43. return show(parent_window, text, "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
  44. }
  45. MessageBox::MessageBox(Window* parent_window, const StringView& text, const StringView& title, Type type, InputType input_type)
  46. : Dialog(parent_window)
  47. , m_text(text)
  48. , m_type(type)
  49. , m_input_type(input_type)
  50. {
  51. set_title(title);
  52. build();
  53. }
  54. MessageBox::~MessageBox()
  55. {
  56. }
  57. RefPtr<Gfx::Bitmap> MessageBox::icon() const
  58. {
  59. switch (m_type) {
  60. case Type::Information:
  61. return Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-information.png");
  62. case Type::Warning:
  63. return Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-warning.png");
  64. case Type::Error:
  65. return Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-error.png");
  66. case Type::Question:
  67. return Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-question.png");
  68. default:
  69. return nullptr;
  70. }
  71. }
  72. bool MessageBox::should_include_ok_button() const
  73. {
  74. return m_input_type == InputType::OK || m_input_type == InputType::OKCancel;
  75. }
  76. bool MessageBox::should_include_cancel_button() const
  77. {
  78. return m_input_type == InputType::OKCancel || m_input_type == InputType::YesNoCancel;
  79. }
  80. bool MessageBox::should_include_yes_button() const
  81. {
  82. return m_input_type == InputType::YesNo || m_input_type == InputType::YesNoCancel;
  83. }
  84. bool MessageBox::should_include_no_button() const
  85. {
  86. return should_include_yes_button();
  87. }
  88. void MessageBox::build()
  89. {
  90. auto& widget = set_main_widget<Widget>();
  91. int text_width = widget.font().width(m_text);
  92. int icon_width = 0;
  93. widget.set_layout<VerticalBoxLayout>();
  94. widget.set_fill_with_background_color(true);
  95. widget.layout()->set_margins({ 8, 8, 8, 8 });
  96. widget.layout()->set_spacing(8);
  97. auto& message_container = widget.add<Widget>();
  98. message_container.set_layout<HorizontalBoxLayout>();
  99. message_container.layout()->set_margins({ 8, 0, 0, 0 });
  100. message_container.layout()->set_spacing(8);
  101. if (m_type != Type::None) {
  102. auto& icon_image = message_container.add<ImageWidget>();
  103. icon_image.set_bitmap(icon());
  104. if (icon())
  105. icon_width = icon()->width();
  106. }
  107. auto& label = message_container.add<Label>(m_text);
  108. label.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  109. label.set_preferred_size(text_width, 16);
  110. if (m_type != Type::None)
  111. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  112. auto& button_container = widget.add<Widget>();
  113. button_container.set_layout<HorizontalBoxLayout>();
  114. button_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  115. button_container.set_preferred_size(0, 24);
  116. button_container.layout()->set_spacing(8);
  117. auto add_button = [&](String label, Dialog::ExecResult result) {
  118. auto& button = button_container.add<Button>();
  119. button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  120. button.set_preferred_size(96, 0);
  121. button.set_text(label);
  122. button.on_click = [this, label, result](auto) {
  123. done(result);
  124. };
  125. };
  126. button_container.layout()->add_spacer();
  127. if (should_include_ok_button())
  128. add_button("OK", Dialog::ExecOK);
  129. if (should_include_yes_button())
  130. add_button("Yes", Dialog::ExecYes);
  131. if (should_include_no_button())
  132. add_button("No", Dialog::ExecNo);
  133. if (should_include_cancel_button())
  134. add_button("Cancel", Dialog::ExecCancel);
  135. button_container.layout()->add_spacer();
  136. int width = button_container.child_widgets().size() * 96 + 32;
  137. if (width < text_width + icon_width + 56)
  138. width = text_width + icon_width + 56;
  139. set_rect(x(), y(), width, 96);
  140. set_resizable(false);
  141. }
  142. }