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