MessageBox.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/Label.h>
  29. #include <LibGUI/MessageBox.h>
  30. #include <LibGfx/Font.h>
  31. #include <stdio.h>
  32. namespace GUI {
  33. int MessageBox::show(const StringView& text, const StringView& title, Type type, InputType input_type, Core::Object* parent)
  34. {
  35. auto box = MessageBox::construct(text, title, type, input_type);
  36. if (parent)
  37. parent->add_child(box);
  38. return box->exec();
  39. }
  40. MessageBox::MessageBox(const StringView& text, const StringView& title, Type type, InputType input_type, Core::Object* parent)
  41. : Dialog(parent)
  42. , m_text(text)
  43. , m_type(type)
  44. , m_input_type(input_type)
  45. {
  46. set_title(title);
  47. build();
  48. }
  49. MessageBox::~MessageBox()
  50. {
  51. }
  52. RefPtr<Gfx::Bitmap> MessageBox::icon() const
  53. {
  54. switch (m_type) {
  55. case Type::Information:
  56. return Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-information.png");
  57. case Type::Warning:
  58. return Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-warning.png");
  59. case Type::Error:
  60. return Gfx::Bitmap::load_from_file("/res/icons/32x32/msgbox-error.png");
  61. default:
  62. return nullptr;
  63. }
  64. }
  65. bool MessageBox::should_include_ok_button() const
  66. {
  67. return m_input_type == InputType::OK || m_input_type == InputType::OKCancel;
  68. }
  69. bool MessageBox::should_include_cancel_button() const
  70. {
  71. return m_input_type == InputType::OKCancel || m_input_type == InputType::YesNoCancel;
  72. }
  73. bool MessageBox::should_include_yes_button() const
  74. {
  75. return m_input_type == InputType::YesNo || m_input_type == InputType::YesNoCancel;
  76. }
  77. bool MessageBox::should_include_no_button() const
  78. {
  79. return should_include_yes_button();
  80. }
  81. void MessageBox::build()
  82. {
  83. auto& widget = set_main_widget<Widget>();
  84. int text_width = widget.font().width(m_text);
  85. int icon_width = 0;
  86. widget.set_layout<VerticalBoxLayout>();
  87. widget.set_fill_with_background_color(true);
  88. widget.layout()->set_margins({ 0, 15, 0, 15 });
  89. widget.layout()->set_spacing(15);
  90. RefPtr<Widget> message_container = widget;
  91. if (m_type != Type::None) {
  92. message_container = widget.add<Widget>();
  93. message_container->set_layout<HorizontalBoxLayout>();
  94. message_container->layout()->set_margins({ 8, 0, 8, 0 });
  95. message_container->layout()->set_spacing(8);
  96. auto icon_label = message_container->add<Label>();
  97. icon_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  98. icon_label->set_preferred_size(32, 32);
  99. icon_label->set_icon(icon());
  100. icon_width = icon_label->icon()->width();
  101. }
  102. auto label = message_container->add<Label>(m_text);
  103. label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  104. label->set_preferred_size(text_width, 16);
  105. auto button_container = widget.add<Widget>();
  106. button_container->set_layout<HorizontalBoxLayout>();
  107. button_container->layout()->set_spacing(5);
  108. button_container->layout()->set_margins({ 15, 0, 15, 0 });
  109. auto add_button = [&](String label, Dialog::ExecResult result) {
  110. auto button = button_container->add<Button>();
  111. button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  112. button->set_preferred_size(0, 20);
  113. button->set_text(label);
  114. button->on_click = [this, label, result] {
  115. dbg() << "GUI::MessageBox: '" << label << "' button clicked";
  116. done(result);
  117. };
  118. };
  119. if (should_include_ok_button())
  120. add_button("OK", Dialog::ExecOK);
  121. if (should_include_yes_button())
  122. add_button("Yes", Dialog::ExecYes);
  123. if (should_include_no_button())
  124. add_button("No", Dialog::ExecNo);
  125. if (should_include_cancel_button())
  126. add_button("Cancel", Dialog::ExecCancel);
  127. set_rect(x(), y(), text_width + icon_width + 80, 100);
  128. set_resizable(false);
  129. }
  130. }