| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  | #include <LibGUI/GBoxLayout.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GButton.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  | #include <LibGUI/GLabel.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GMessageBox.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-08 20:13:39 +02:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 21:32:10 +02:00
										 |  |  | void GMessageBox::show(const StringView& text, const StringView& title, Type type, InputType input_type, CObject* parent) | 
					
						
							| 
									
										
										
										
											2019-05-08 20:13:39 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-16 21:32:10 +02:00
										 |  |  |     GMessageBox box(text, title, type, input_type, parent); | 
					
						
							| 
									
										
										
										
											2019-05-08 20:13:39 +02:00
										 |  |  |     box.exec(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 21:32:10 +02:00
										 |  |  | GMessageBox::GMessageBox(const StringView& text, const StringView& title, Type type, InputType input_type, CObject* parent) | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  |     : GDialog(parent) | 
					
						
							|  |  |  |     , m_text(text) | 
					
						
							| 
									
										
										
										
											2019-05-08 20:13:39 +02:00
										 |  |  |     , m_type(type) | 
					
						
							| 
									
										
										
										
											2019-07-16 21:32:10 +02:00
										 |  |  |     , m_input_type(input_type) | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     set_title(title); | 
					
						
							|  |  |  |     build(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GMessageBox::~GMessageBox() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-21 18:37:47 +02:00
										 |  |  | RefPtr<GraphicsBitmap> GMessageBox::icon() const | 
					
						
							| 
									
										
										
										
											2019-05-08 20:13:39 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							|  |  |  |     case Type::Information: | 
					
						
							|  |  |  |         return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-information.png"); | 
					
						
							|  |  |  |     case Type::Warning: | 
					
						
							|  |  |  |         return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-warning.png"); | 
					
						
							|  |  |  |     case Type::Error: | 
					
						
							|  |  |  |         return GraphicsBitmap::load_from_file("/res/icons/32x32/msgbox-error.png"); | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 21:32:10 +02:00
										 |  |  | bool GMessageBox::should_include_ok_button() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_input_type == InputType::OK || m_input_type == InputType::OKCancel; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool GMessageBox::should_include_cancel_button() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_input_type == InputType::OKCancel; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  | void GMessageBox::build() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* widget = new GWidget; | 
					
						
							|  |  |  |     set_main_widget(widget); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int text_width = widget->font().width(m_text); | 
					
						
							| 
									
										
										
										
											2019-05-08 20:13:39 +02:00
										 |  |  |     int icon_width = 0; | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     widget->set_fill_with_background_color(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     widget->layout()->set_margins({ 0, 15, 0, 15 }); | 
					
						
							|  |  |  |     widget->layout()->set_spacing(15); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-08 20:13:39 +02:00
										 |  |  |     GWidget* message_container = widget; | 
					
						
							|  |  |  |     if (m_type != Type::None) { | 
					
						
							|  |  |  |         message_container = new GWidget(widget); | 
					
						
							|  |  |  |         message_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); | 
					
						
							|  |  |  |         message_container->layout()->set_margins({ 8, 0, 8, 0 }); | 
					
						
							|  |  |  |         message_container->layout()->set_spacing(8); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto* icon_label = new GLabel(message_container); | 
					
						
							|  |  |  |         icon_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |         icon_label->set_preferred_size(32, 32); | 
					
						
							| 
									
										
										
										
											2019-05-08 20:13:39 +02:00
										 |  |  |         icon_label->set_icon(icon()); | 
					
						
							|  |  |  |         icon_width = icon_label->icon()->width(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto* label = new GLabel(m_text, message_container); | 
					
						
							|  |  |  |     label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     label->set_preferred_size(text_width, 16); | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 21:32:10 +02:00
										 |  |  |     auto* button_container = new GWidget(widget); | 
					
						
							|  |  |  |     button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); | 
					
						
							|  |  |  |     button_container->layout()->set_spacing(5); | 
					
						
							|  |  |  |     button_container->layout()->set_margins({ 15, 0, 15, 0 }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (should_include_ok_button()) { | 
					
						
							|  |  |  |         auto* ok_button = new GButton(button_container); | 
					
						
							|  |  |  |         ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |         ok_button->set_preferred_size(0, 20); | 
					
						
							| 
									
										
										
										
											2019-07-16 21:32:10 +02:00
										 |  |  |         ok_button->set_text("OK"); | 
					
						
							|  |  |  |         ok_button->on_click = [this](auto&) { | 
					
						
							|  |  |  |             dbgprintf("GMessageBox: OK button clicked\n"); | 
					
						
							|  |  |  |             done(GDialog::ExecOK); | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (should_include_cancel_button()) { | 
					
						
							|  |  |  |         auto* cancel_button = new GButton(button_container); | 
					
						
							|  |  |  |         cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |         cancel_button->set_preferred_size(0, 20); | 
					
						
							| 
									
										
										
										
											2019-07-16 21:32:10 +02:00
										 |  |  |         cancel_button->set_text("Cancel"); | 
					
						
							|  |  |  |         cancel_button->on_click = [this](auto&) { | 
					
						
							|  |  |  |             dbgprintf("GMessageBox: Cancel button clicked\n"); | 
					
						
							|  |  |  |             done(GDialog::ExecCancel); | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-08 20:13:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     set_rect(x(), y(), text_width + icon_width + 80, 100); | 
					
						
							|  |  |  |     set_resizable(false); | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  | } |