GMessageBox.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/GBoxLayout.h>
  27. #include <LibGUI/GButton.h>
  28. #include <LibGUI/GLabel.h>
  29. #include <LibGUI/GMessageBox.h>
  30. #include <stdio.h>
  31. int GMessageBox::show(const StringView& text, const StringView& title, Type type, InputType input_type, CObject* parent)
  32. {
  33. auto box = GMessageBox::construct(text, title, type, input_type, parent);
  34. return box->exec();
  35. }
  36. GMessageBox::GMessageBox(const StringView& text, const StringView& title, Type type, InputType input_type, CObject* parent)
  37. : GDialog(parent)
  38. , m_text(text)
  39. , m_type(type)
  40. , m_input_type(input_type)
  41. {
  42. set_title(title);
  43. build();
  44. }
  45. GMessageBox::~GMessageBox()
  46. {
  47. }
  48. RefPtr<GraphicsBitmap> GMessageBox::icon() const
  49. {
  50. switch (m_type) {
  51. case Type::Information:
  52. return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-information.png");
  53. case Type::Warning:
  54. return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-warning.png");
  55. case Type::Error:
  56. return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-error.png");
  57. default:
  58. return nullptr;
  59. }
  60. }
  61. bool GMessageBox::should_include_ok_button() const
  62. {
  63. return m_input_type == InputType::OK || m_input_type == InputType::OKCancel;
  64. }
  65. bool GMessageBox::should_include_cancel_button() const
  66. {
  67. return m_input_type == InputType::OKCancel;
  68. }
  69. void GMessageBox::build()
  70. {
  71. auto widget = GWidget::construct();
  72. set_main_widget(widget);
  73. int text_width = widget->font().width(m_text);
  74. int icon_width = 0;
  75. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  76. widget->set_fill_with_background_color(true);
  77. widget->layout()->set_margins({ 0, 15, 0, 15 });
  78. widget->layout()->set_spacing(15);
  79. RefPtr<GWidget> message_container = widget;
  80. if (m_type != Type::None) {
  81. message_container = GWidget::construct(widget.ptr());
  82. message_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  83. message_container->layout()->set_margins({ 8, 0, 8, 0 });
  84. message_container->layout()->set_spacing(8);
  85. auto icon_label = GLabel::construct(message_container);
  86. icon_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  87. icon_label->set_preferred_size(32, 32);
  88. icon_label->set_icon(icon());
  89. icon_width = icon_label->icon()->width();
  90. }
  91. auto label = GLabel::construct(m_text, message_container);
  92. label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  93. label->set_preferred_size(text_width, 16);
  94. auto button_container = GWidget::construct(widget.ptr());
  95. button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  96. button_container->layout()->set_spacing(5);
  97. button_container->layout()->set_margins({ 15, 0, 15, 0 });
  98. if (should_include_ok_button()) {
  99. auto ok_button = GButton::construct(button_container);
  100. ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  101. ok_button->set_preferred_size(0, 20);
  102. ok_button->set_text("OK");
  103. ok_button->on_click = [this](auto&) {
  104. dbgprintf("GMessageBox: OK button clicked\n");
  105. done(GDialog::ExecOK);
  106. };
  107. }
  108. if (should_include_cancel_button()) {
  109. auto cancel_button = GButton::construct(button_container);
  110. cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  111. cancel_button->set_preferred_size(0, 20);
  112. cancel_button->set_text("Cancel");
  113. cancel_button->on_click = [this](auto&) {
  114. dbgprintf("GMessageBox: Cancel button clicked\n");
  115. done(GDialog::ExecCancel);
  116. };
  117. }
  118. set_rect(x(), y(), text_width + icon_width + 80, 100);
  119. set_resizable(false);
  120. }