| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | #include <LibGUI/GBoxLayout.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GButton.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  | #include <LibGUI/GInputBox.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GLabel.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | #include <LibGUI/GTextEditor.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-02 14:58:02 +02:00
										 |  |  | GInputBox::GInputBox(const StringView& prompt, const StringView& title, CObject* parent) | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  |     : GDialog(parent) | 
					
						
							|  |  |  |     , m_prompt(prompt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set_title(title); | 
					
						
							|  |  |  |     build(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GInputBox::~GInputBox() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GInputBox::build() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-21 17:05:35 +02:00
										 |  |  |     auto widget = GWidget::construct(); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  |     set_main_widget(widget); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int text_width = widget->font().width(m_prompt); | 
					
						
							| 
									
										
										
										
											2019-05-17 15:49:06 +02:00
										 |  |  |     int title_width = widget->font().width(title()) + 24 /* icon, plus a little padding -- not perfect */; | 
					
						
							|  |  |  |     int max_width = AK::max(text_width, title_width); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-17 15:49:06 +02:00
										 |  |  |     set_rect(x(), y(), max_width + 80, 80); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     widget->set_fill_with_background_color(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     widget->layout()->set_margins({ 8, 8, 8, 8 }); | 
					
						
							|  |  |  |     widget->layout()->set_spacing(8); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 14:19:05 +02:00
										 |  |  |     auto label = GLabel::construct(m_prompt, widget); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  |     label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     label->set_preferred_size(text_width, 16); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 15:43:52 +02:00
										 |  |  |     m_text_editor = GTextEditor::construct(GTextEditor::SingleLine, widget); | 
					
						
							| 
									
										
										
										
											2019-03-19 02:20:00 +01:00
										 |  |  |     m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     m_text_editor->set_preferred_size(0, 19); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 17:05:35 +02:00
										 |  |  |     auto button_container_outer = GWidget::construct(widget); | 
					
						
							| 
									
										
										
										
											2019-03-19 03:00:42 +01:00
										 |  |  |     button_container_outer->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     button_container_outer->set_preferred_size(0, 20); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  |     button_container_outer->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 17:05:35 +02:00
										 |  |  |     auto button_container_inner = GWidget::construct(button_container_outer); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  |     button_container_inner->set_layout(make<GBoxLayout>(Orientation::Horizontal)); | 
					
						
							|  |  |  |     button_container_inner->layout()->set_spacing(8); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 19:28:28 +02:00
										 |  |  |     m_cancel_button = GButton::construct(button_container_inner); | 
					
						
							| 
									
										
										
										
											2019-03-19 02:20:00 +01:00
										 |  |  |     m_cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     m_cancel_button->set_preferred_size(0, 20); | 
					
						
							| 
									
										
										
										
											2019-05-24 16:32:20 +02:00
										 |  |  |     m_cancel_button->set_text("Cancel"); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     m_cancel_button->on_click = [this](auto&) { | 
					
						
							| 
									
										
										
										
											2019-03-19 02:20:00 +01:00
										 |  |  |         dbgprintf("GInputBox: Cancel button clicked\n"); | 
					
						
							|  |  |  |         done(ExecCancel); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 19:28:28 +02:00
										 |  |  |     m_ok_button = GButton::construct(button_container_inner); | 
					
						
							| 
									
										
										
										
											2019-03-19 02:20:00 +01:00
										 |  |  |     m_ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     m_ok_button->set_preferred_size(0, 20); | 
					
						
							| 
									
										
										
										
											2019-05-24 16:32:20 +02:00
										 |  |  |     m_ok_button->set_text("OK"); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     m_ok_button->on_click = [this](auto&) { | 
					
						
							| 
									
										
										
										
											2019-03-19 02:20:00 +01:00
										 |  |  |         dbgprintf("GInputBox: OK button clicked\n"); | 
					
						
							|  |  |  |         m_text_value = m_text_editor->text(); | 
					
						
							|  |  |  |         done(ExecOK); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 03:08:29 +02:00
										 |  |  |     m_text_editor->on_return_pressed = [this] { | 
					
						
							| 
									
										
										
										
											2019-03-19 02:20:00 +01:00
										 |  |  |         m_ok_button->click(); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-04-10 03:08:29 +02:00
										 |  |  |     m_text_editor->on_escape_pressed = [this] { | 
					
						
							| 
									
										
										
										
											2019-03-19 02:20:00 +01:00
										 |  |  |         m_cancel_button->click(); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-03-19 02:20:00 +01:00
										 |  |  |     m_text_editor->set_focus(true); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | } |