Serenity Operating System
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/InputBox.h>
30#include <LibGUI/Label.h>
31#include <LibGUI/TextBox.h>
32#include <LibGfx/Font.h>
33#include <stdio.h>
34
35namespace GUI {
36
37InputBox::InputBox(const StringView& prompt, const StringView& title, GUI::Window* parent_window)
38 : Dialog(parent_window)
39 , m_prompt(prompt)
40{
41 set_title(title);
42 build();
43}
44
45InputBox::~InputBox()
46{
47}
48
49void InputBox::build()
50{
51 auto& widget = set_main_widget<Widget>();
52
53 int text_width = widget.font().width(m_prompt);
54 int title_width = widget.font().width(title()) + 24 /* icon, plus a little padding -- not perfect */;
55 int max_width = AK::max(text_width, title_width);
56
57 set_rect(x(), y(), max_width + 80, 80);
58
59 widget.set_layout<VerticalBoxLayout>();
60 widget.set_fill_with_background_color(true);
61
62 widget.layout()->set_margins({ 8, 8, 8, 8 });
63 widget.layout()->set_spacing(8);
64
65 auto& label = widget.add<Label>(m_prompt);
66 label.set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
67 label.set_preferred_size(text_width, 16);
68
69 m_text_editor = widget.add<TextBox>();
70 m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
71 m_text_editor->set_preferred_size(0, 19);
72
73 auto& button_container_outer = widget.add<Widget>();
74 button_container_outer.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
75 button_container_outer.set_preferred_size(0, 20);
76 button_container_outer.set_layout<VerticalBoxLayout>();
77
78 auto& button_container_inner = button_container_outer.add<Widget>();
79 button_container_inner.set_layout<HorizontalBoxLayout>();
80 button_container_inner.layout()->set_spacing(8);
81
82 m_cancel_button = button_container_inner.add<Button>();
83 m_cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
84 m_cancel_button->set_preferred_size(0, 20);
85 m_cancel_button->set_text("Cancel");
86 m_cancel_button->on_click = [this] {
87 dbgprintf("GUI::InputBox: Cancel button clicked\n");
88 done(ExecCancel);
89 };
90
91 m_ok_button = button_container_inner.add<Button>();
92 m_ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
93 m_ok_button->set_preferred_size(0, 20);
94 m_ok_button->set_text("OK");
95 m_ok_button->on_click = [this] {
96 dbgprintf("GUI::InputBox: OK button clicked\n");
97 m_text_value = m_text_editor->text();
98 done(ExecOK);
99 };
100
101 m_text_editor->on_return_pressed = [this] {
102 m_ok_button->click();
103 };
104 m_text_editor->on_escape_pressed = [this] {
105 m_cancel_button->click();
106 };
107 m_text_editor->set_focus(true);
108}
109
110}