2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-09-12 16:06:24 +02:00
|
|
|
* Copyright (c) 2021, networkException <networkexception@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
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>
|
2022-04-09 09:28:38 +02:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2022-12-30 21:13:54 +01:00
|
|
|
InputBox::InputBox(Window* parent_window, DeprecatedString text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder)
|
2020-03-04 20:53:51 +01:00
|
|
|
: Dialog(parent_window)
|
2022-11-16 07:35:02 -05:00
|
|
|
, m_text_value(move(text_value))
|
2019-03-19 01:41:00 +01:00
|
|
|
, m_prompt(prompt)
|
2022-12-31 17:52:09 +01:00
|
|
|
, m_input_type(input_type)
|
2021-07-06 16:47:44 +02:00
|
|
|
, m_placeholder(placeholder)
|
2019-03-19 01:41:00 +01:00
|
|
|
{
|
|
|
|
|
set_title(title);
|
2022-12-31 17:52:09 +01:00
|
|
|
build();
|
2019-03-19 01:41:00 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-30 21:13:54 +01:00
|
|
|
Dialog::ExecResult InputBox::show(Window* parent_window, DeprecatedString& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder)
|
2020-07-16 07:54:42 -06:00
|
|
|
{
|
2022-12-30 21:13:54 +01:00
|
|
|
auto box = InputBox::construct(parent_window, text_value, prompt, title, input_type, placeholder);
|
2020-07-17 10:21:44 -04:00
|
|
|
box->set_resizable(false);
|
|
|
|
|
if (parent_window)
|
|
|
|
|
box->set_icon(parent_window->icon());
|
2020-07-16 07:54:42 -06:00
|
|
|
auto result = box->exec();
|
|
|
|
|
text_value = box->text_value();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void InputBox::set_text_value(DeprecatedString text_value)
|
2022-11-16 07:35:02 -05:00
|
|
|
{
|
|
|
|
|
m_text_editor->set_text(move(text_value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InputBox::on_done(ExecResult result)
|
|
|
|
|
{
|
2022-12-31 17:52:09 +01:00
|
|
|
if (result == ExecResult::OK) {
|
2022-11-16 07:35:02 -05:00
|
|
|
m_text_value = m_text_editor->text();
|
2022-12-31 17:52:09 +01:00
|
|
|
|
|
|
|
|
switch (m_input_type) {
|
|
|
|
|
case InputType::Text:
|
|
|
|
|
case InputType::Password:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case InputType::NonemptyText:
|
|
|
|
|
VERIFY(!m_text_value.is_empty());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-16 07:35:02 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-31 17:52:09 +01:00
|
|
|
void InputBox::build()
|
2019-03-19 01:41:00 +01:00
|
|
|
{
|
2023-01-06 16:48:37 +00:00
|
|
|
auto widget = set_main_widget<Widget>().release_value_but_fixme_should_propagate_errors();
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2023-01-06 16:48:37 +00:00
|
|
|
int text_width = widget->font().width(m_prompt);
|
|
|
|
|
int title_width = widget->font().width(title()) + 24 /* icon, plus a little padding -- not perfect */;
|
2021-02-25 21:10:47 +01:00
|
|
|
int max_width = max(text_width, title_width);
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2023-01-06 16:48:37 +00:00
|
|
|
widget->set_layout<VerticalBoxLayout>();
|
|
|
|
|
widget->set_fill_with_background_color(true);
|
|
|
|
|
widget->set_preferred_height(SpecialDimension::Fit);
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2023-01-06 16:48:37 +00:00
|
|
|
widget->layout()->set_margins(6);
|
|
|
|
|
widget->layout()->set_spacing(6);
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2023-01-06 16:48:37 +00:00
|
|
|
auto& label_editor_container = widget->add<Widget>();
|
2020-07-17 10:21:44 -04:00
|
|
|
label_editor_container.set_layout<HorizontalBoxLayout>();
|
2022-06-28 17:12:52 +02:00
|
|
|
label_editor_container.set_preferred_height(SpecialDimension::Fit);
|
2020-07-17 10:21:44 -04:00
|
|
|
|
|
|
|
|
auto& label = label_editor_container.add<Label>(m_prompt);
|
2022-06-28 17:12:52 +02:00
|
|
|
label.set_preferred_width(text_width);
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2022-12-31 17:52:09 +01:00
|
|
|
switch (m_input_type) {
|
2021-07-24 22:59:39 -06:00
|
|
|
case InputType::Text:
|
2023-01-14 15:14:47 -07:00
|
|
|
case InputType::NonemptyText:
|
2021-07-24 22:59:39 -06:00
|
|
|
m_text_editor = label_editor_container.add<TextBox>();
|
|
|
|
|
break;
|
|
|
|
|
case InputType::Password:
|
|
|
|
|
m_text_editor = label_editor_container.add<PasswordBox>();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-20 12:04:15 +01:00
|
|
|
m_text_editor->set_text(m_text_value);
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2021-07-06 16:47:44 +02:00
|
|
|
if (!m_placeholder.is_null())
|
|
|
|
|
m_text_editor->set_placeholder(m_placeholder);
|
|
|
|
|
|
2023-01-06 16:48:37 +00:00
|
|
|
auto& button_container_outer = widget->add<Widget>();
|
2022-06-28 17:12:52 +02:00
|
|
|
button_container_outer.set_preferred_height(SpecialDimension::Fit);
|
2020-03-04 19:07:55 +01:00
|
|
|
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>();
|
2022-06-28 17:12:52 +02:00
|
|
|
button_container_inner.set_preferred_height(SpecialDimension::Fit);
|
2020-07-17 10:21:44 -04:00
|
|
|
button_container_inner.layout()->set_spacing(6);
|
|
|
|
|
button_container_inner.layout()->add_spacer();
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2022-06-10 23:03:11 +02:00
|
|
|
m_ok_button = button_container_inner.add<DialogButton>();
|
2023-02-11 20:51:04 +01:00
|
|
|
m_ok_button->set_text(String::from_utf8_short_string("OK"sv));
|
2020-05-12 20:30:33 +02:00
|
|
|
m_ok_button->on_click = [this](auto) {
|
2021-01-10 10:02:20 +01:00
|
|
|
dbgln("GUI::InputBox: OK button clicked");
|
2022-05-13 13:10:27 +01:00
|
|
|
done(ExecResult::OK);
|
2019-03-19 01:41:00 +01:00
|
|
|
};
|
2022-01-24 14:29:21 -05:00
|
|
|
m_ok_button->set_default(true);
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2022-06-10 23:03:11 +02:00
|
|
|
m_cancel_button = button_container_inner.add<DialogButton>();
|
2023-02-11 20:51:04 +01:00
|
|
|
m_cancel_button->set_text(String::from_utf8_short_string("Cancel"sv));
|
2020-05-12 20:30:33 +02:00
|
|
|
m_cancel_button->on_click = [this](auto) {
|
2021-01-10 10:02:20 +01:00
|
|
|
dbgln("GUI::InputBox: Cancel button clicked");
|
2022-05-13 13:10:27 +01:00
|
|
|
done(ExecResult::Cancel);
|
2020-04-29 19:31:15 +02: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);
|
2022-06-28 17:12:52 +02:00
|
|
|
|
2022-12-31 17:52:09 +01:00
|
|
|
if (m_input_type == InputType::NonemptyText) {
|
2023-01-14 15:14:47 -07:00
|
|
|
m_text_editor->on_change = [this] {
|
|
|
|
|
m_ok_button->set_enabled(!m_text_editor->text().is_empty());
|
|
|
|
|
};
|
|
|
|
|
m_text_editor->on_change();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 16:48:37 +00:00
|
|
|
set_rect(x(), y(), max_width + 140, widget->effective_preferred_size().height().as_int());
|
2019-03-19 01:41:00 +01:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|