1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, networkException <networkexception@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/ImageWidget.h>
#include <LibGUI/InputBox.h>
#include <LibGUI/Label.h>
#include <LibGUI/SpinBox.h>
#include <LibGUI/TextBox.h>
namespace GUI {
ErrorOr<NonnullRefPtr<InputBox>> InputBox::create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type, RefPtr<Gfx::Bitmap const> icon)
{
VERIFY(input_type != InputType::Numeric);
auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) InputBox(parent_window, text_value, TRY(String::from_utf8(title)), TRY(String::from_utf8(prompt)), input_type, move(icon))));
TRY(box->build());
return box;
}
ErrorOr<NonnullRefPtr<InputBox>> InputBox::create_numeric(Window* parent_window, int value, StringView title, StringView prompt, RefPtr<Gfx::Bitmap const> icon)
{
auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) InputBox(parent_window, value, TRY(String::from_utf8(title)), TRY(String::from_utf8(prompt)), move(icon))));
TRY(box->build());
return box;
}
InputBox::InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type, RefPtr<Gfx::Bitmap const> icon)
: Dialog(parent_window)
, m_text_value(move(text_value))
, m_prompt(move(prompt))
, m_input_type(input_type)
, m_icon(move(icon))
{
set_title(move(title).to_deprecated_string());
set_resizable(false);
set_auto_shrink(true);
}
InputBox::InputBox(Window* parent_window, int value, String title, String prompt, RefPtr<Gfx::Bitmap const> icon)
: Dialog(parent_window)
, m_numeric_value(value)
, m_prompt(move(prompt))
, m_input_type(InputType::Numeric)
, m_icon(move(icon))
{
set_title(move(title).to_deprecated_string());
set_resizable(false);
set_auto_shrink(true);
}
Dialog::ExecResult InputBox::show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder, RefPtr<Gfx::Bitmap const> icon)
{
return MUST(try_show(parent_window, text_value, prompt, title, input_type, placeholder, move(icon)));
}
ErrorOr<Dialog::ExecResult> InputBox::try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder, RefPtr<Gfx::Bitmap const> icon)
{
VERIFY(input_type != InputType::Numeric);
auto box = TRY(InputBox::create(parent_window, text_value, prompt, title, input_type, move(icon)));
if (parent_window)
box->set_icon(parent_window->icon());
box->set_placeholder(placeholder);
auto result = box->exec();
text_value = box->text_value();
return result;
}
ErrorOr<Dialog::ExecResult> InputBox::show_numeric(Window* parent_window, int& value, int min, int max, StringView title, StringView prompt, RefPtr<Gfx::Bitmap const> icon)
{
auto box = TRY(InputBox::create_numeric(parent_window, value, title, prompt, move(icon)));
if (parent_window)
box->set_icon(parent_window->icon());
box->set_range(min, max);
auto result = box->exec();
value = box->numeric_value();
return result;
}
void InputBox::set_placeholder(StringView view)
{
m_text_editor->set_placeholder(view);
}
void InputBox::set_range(int min, int max)
{
m_spinbox->set_range(min, max);
}
void InputBox::set_text_value(String value)
{
if (m_text_value == value)
return;
m_text_value = move(value);
m_text_editor->set_text(m_text_value);
}
void InputBox::set_numeric_value(int value)
{
if (m_numeric_value == value)
return;
m_numeric_value = value;
m_spinbox->set_value(value);
}
void InputBox::on_done(ExecResult result)
{
if (result != ExecResult::OK)
return;
if (m_text_editor) {
auto value = String::from_deprecated_string(m_text_editor->text());
if (!value.is_error())
m_text_value = value.release_value();
} else if (m_spinbox)
m_numeric_value = m_spinbox->value();
if (m_input_type == InputType::NonemptyText)
VERIFY(!m_text_value.is_empty());
}
ErrorOr<void> InputBox::build()
{
auto main_widget = TRY(set_main_widget<Widget>());
TRY(main_widget->try_set_layout<VerticalBoxLayout>(4, 6));
main_widget->set_fill_with_background_color(true);
auto top_container = TRY(main_widget->try_add<Widget>());
TRY(top_container->try_set_layout<HorizontalBoxLayout>(0, 8));
if (m_icon) {
auto image_widget = TRY(top_container->try_add<ImageWidget>());
image_widget->set_bitmap(m_icon);
}
auto input_container = TRY(top_container->try_add<Widget>());
auto orientation = m_icon ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal;
TRY(input_container->try_set_layout<BoxLayout>(orientation));
TRY(input_container->add_spacer());
if (!m_prompt.is_empty()) {
m_label_container = TRY(input_container->try_add<Widget>());
TRY(m_label_container->try_set_layout<HorizontalBoxLayout>());
m_prompt_label = TRY(m_label_container->try_add<Label>());
m_prompt_label->set_autosize(true);
m_prompt_label->set_text_wrapping(Gfx::TextWrapping::DontWrap);
m_prompt_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
m_prompt_label->set_text(move(m_prompt));
}
switch (m_input_type) {
case InputType::Text:
case InputType::NonemptyText:
m_text_editor = TRY(input_container->try_add<TextBox>());
break;
case InputType::Password:
m_text_editor = TRY(input_container->try_add<PasswordBox>());
break;
case InputType::Numeric:
m_spinbox = TRY(input_container->try_add<SpinBox>());
break;
}
TRY(input_container->add_spacer());
auto button_container = TRY(main_widget->try_add<Widget>());
TRY(button_container->try_set_layout<HorizontalBoxLayout>(0, 6));
TRY(button_container->add_spacer());
m_ok_button = TRY(button_container->try_add<DialogButton>("OK"_short_string));
m_ok_button->on_click = [this](auto) {
if (m_spinbox)
m_spinbox->set_value_from_current_text();
done(ExecResult::OK);
};
m_ok_button->set_default(true);
m_cancel_button = TRY(button_container->try_add<DialogButton>("Cancel"_short_string));
m_cancel_button->on_click = [this](auto) { done(ExecResult::Cancel); };
auto resize_editor = [this, button_container] {
auto width = button_container->effective_min_size().width().as_int();
if (m_text_editor)
m_text_editor->set_min_width(width);
if (m_spinbox)
m_spinbox->set_min_width(width);
if (!m_icon && m_label_container)
m_label_container->set_fixed_width(m_prompt_label->max_width());
};
resize_editor();
on_font_change = [resize_editor] { resize_editor(); };
if (m_text_editor) {
m_text_editor->set_text(m_text_value);
if (m_input_type == InputType::NonemptyText) {
m_text_editor->on_change = [this] {
m_ok_button->set_enabled(!m_text_editor->text().is_empty());
};
m_text_editor->on_change();
}
}
if (m_spinbox)
m_spinbox->set_value(m_numeric_value);
auto size = main_widget->effective_min_size();
resize(TRY(size.width().shrink_value()), TRY(size.height().shrink_value()));
return {};
}
}
|