Serenity Operating System
at portability 153 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, Core::Object* parent) 37{ 38 auto box = MessageBox::construct(text, title, type, input_type); 39 if (parent) 40 parent->add_child(box); 41 return box->exec(); 42} 43 44MessageBox::MessageBox(const StringView& text, const StringView& title, Type type, InputType input_type, Core::Object* parent) 45 : Dialog(parent) 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 = Widget::construct(); 95 set_main_widget(widget); 96 97 int text_width = widget->font().width(m_text); 98 int icon_width = 0; 99 100 widget->set_layout(make<VerticalBoxLayout>()); 101 widget->set_fill_with_background_color(true); 102 103 widget->layout()->set_margins({ 0, 15, 0, 15 }); 104 widget->layout()->set_spacing(15); 105 106 RefPtr<Widget> message_container = widget; 107 if (m_type != Type::None) { 108 message_container = widget->add<Widget>(); 109 message_container->set_layout(make<HorizontalBoxLayout>()); 110 message_container->layout()->set_margins({ 8, 0, 8, 0 }); 111 message_container->layout()->set_spacing(8); 112 113 auto icon_label = message_container->add<Label>(); 114 icon_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); 115 icon_label->set_preferred_size(32, 32); 116 icon_label->set_icon(icon()); 117 icon_width = icon_label->icon()->width(); 118 } 119 120 auto label = message_container->add<Label>(m_text); 121 label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 122 label->set_preferred_size(text_width, 16); 123 124 auto button_container = widget->add<Widget>(); 125 button_container->set_layout(make<HorizontalBoxLayout>()); 126 button_container->layout()->set_spacing(5); 127 button_container->layout()->set_margins({ 15, 0, 15, 0 }); 128 129 auto add_button = [&](String label, Dialog::ExecResult result) { 130 auto button = button_container->add<Button>(); 131 button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 132 button->set_preferred_size(0, 20); 133 button->set_text(label); 134 button->on_click = [this, label, result](auto&) { 135 dbg() << "GUI::MessageBox: '" << label << "' button clicked"; 136 done(result); 137 }; 138 }; 139 140 if (should_include_ok_button()) 141 add_button("OK", Dialog::ExecOK); 142 if (should_include_yes_button()) 143 add_button("Yes", Dialog::ExecYes); 144 if (should_include_no_button()) 145 add_button("No", Dialog::ExecNo); 146 if (should_include_cancel_button()) 147 add_button("Cancel", Dialog::ExecCancel); 148 149 set_rect(x(), y(), text_width + icon_width + 80, 100); 150 set_resizable(false); 151} 152 153}