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, Core::Object* parent)
38 : Dialog(parent)
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 = Widget::construct();
52 set_main_widget(widget);
53
54 int text_width = widget->font().width(m_prompt);
55 int title_width = widget->font().width(title()) + 24 /* icon, plus a little padding -- not perfect */;
56 int max_width = AK::max(text_width, title_width);
57
58 set_rect(x(), y(), max_width + 80, 80);
59
60 widget->set_layout(make<VerticalBoxLayout>());
61 widget->set_fill_with_background_color(true);
62
63 widget->layout()->set_margins({ 8, 8, 8, 8 });
64 widget->layout()->set_spacing(8);
65
66 auto label = widget->add<Label>(m_prompt);
67 label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
68 label->set_preferred_size(text_width, 16);
69
70 m_text_editor = widget->add<TextBox>();
71 m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
72 m_text_editor->set_preferred_size(0, 19);
73
74 auto button_container_outer = widget->add<Widget>();
75 button_container_outer->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
76 button_container_outer->set_preferred_size(0, 20);
77 button_container_outer->set_layout(make<VerticalBoxLayout>());
78
79 auto button_container_inner = button_container_outer->add<Widget>();
80 button_container_inner->set_layout(make<HorizontalBoxLayout>());
81 button_container_inner->layout()->set_spacing(8);
82
83 m_cancel_button = button_container_inner->add<Button>();
84 m_cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
85 m_cancel_button->set_preferred_size(0, 20);
86 m_cancel_button->set_text("Cancel");
87 m_cancel_button->on_click = [this](auto&) {
88 dbgprintf("GInputBox: Cancel button clicked\n");
89 done(ExecCancel);
90 };
91
92 m_ok_button = button_container_inner->add<Button>();
93 m_ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
94 m_ok_button->set_preferred_size(0, 20);
95 m_ok_button->set_text("OK");
96 m_ok_button->on_click = [this](auto&) {
97 dbgprintf("GInputBox: OK button clicked\n");
98 m_text_value = m_text_editor->text();
99 done(ExecOK);
100 };
101
102 m_text_editor->on_return_pressed = [this] {
103 m_ok_button->click();
104 };
105 m_text_editor->on_escape_pressed = [this] {
106 m_cancel_button->click();
107 };
108 m_text_editor->set_focus(true);
109}
110
111}