| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/BoxLayout.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Button.h>
 | 
					
						
							|  |  |  | #include <LibGUI/InputBox.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Label.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-23 10:31:26 +01:00
										 |  |  | #include <LibGUI/TextBox.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 00:24:14 +01:00
										 |  |  | #include <LibGfx/Font.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | namespace GUI { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 20:53:51 +01:00
										 |  |  | InputBox::InputBox(const StringView& prompt, const StringView& title, GUI::Window* parent_window) | 
					
						
							|  |  |  |     : Dialog(parent_window) | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  |     , m_prompt(prompt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set_title(title); | 
					
						
							|  |  |  |     build(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | InputBox::~InputBox() | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void InputBox::build() | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-04 09:46:23 +01:00
										 |  |  |     auto& widget = set_main_widget<Widget>(); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 09:46:23 +01:00
										 |  |  |     int text_width = widget.font().width(m_prompt); | 
					
						
							|  |  |  |     int title_width = widget.font().width(title()) + 24 /* icon, plus a little padding -- not perfect */; | 
					
						
							| 
									
										
										
										
											2019-05-17 15:49:06 +02:00
										 |  |  |     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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 09:46:23 +01:00
										 |  |  |     widget.set_layout<VerticalBoxLayout>(); | 
					
						
							|  |  |  |     widget.set_fill_with_background_color(true); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 09:46:23 +01:00
										 |  |  |     widget.layout()->set_margins({ 8, 8, 8, 8 }); | 
					
						
							|  |  |  |     widget.layout()->set_spacing(8); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:07:55 +01:00
										 |  |  |     auto& label = widget.add<Label>(m_prompt); | 
					
						
							|  |  |  |     label.set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); | 
					
						
							|  |  |  |     label.set_preferred_size(text_width, 16); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 09:46:23 +01:00
										 |  |  |     m_text_editor = widget.add<TextBox>(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:07:55 +01:00
										 |  |  |     auto& button_container_outer = widget.add<Widget>(); | 
					
						
							|  |  |  |     button_container_outer.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							|  |  |  |     button_container_outer.set_preferred_size(0, 20); | 
					
						
							|  |  |  |     button_container_outer.set_layout<VerticalBoxLayout>(); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:07:55 +01:00
										 |  |  |     auto& button_container_inner = button_container_outer.add<Widget>(); | 
					
						
							|  |  |  |     button_container_inner.set_layout<HorizontalBoxLayout>(); | 
					
						
							|  |  |  |     button_container_inner.layout()->set_spacing(8); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:07:55 +01:00
										 |  |  |     m_cancel_button = button_container_inner.add<Button>(); | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2020-03-03 17:02:38 +01:00
										 |  |  |     m_cancel_button->on_click = [this] { | 
					
						
							| 
									
										
										
										
											2019-03-19 02:20:00 +01:00
										 |  |  |         dbgprintf("GInputBox: Cancel button clicked\n"); | 
					
						
							|  |  |  |         done(ExecCancel); | 
					
						
							| 
									
										
										
										
											2019-03-19 01:41:00 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:07:55 +01:00
										 |  |  |     m_ok_button = button_container_inner.add<Button>(); | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2020-03-03 17:02:38 +01:00
										 |  |  |     m_ok_button->on_click = [this] { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |