2020-01-18 08:38:21 +00: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 19:33:02 +00:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
#include <LibGUI/InputBox.h>
|
|
|
|
#include <LibGUI/Label.h>
|
2020-02-23 09:31:26 +00:00
|
|
|
#include <LibGUI/TextBox.h>
|
2020-02-14 23:24:14 +00:00
|
|
|
#include <LibGfx/Font.h>
|
2019-03-19 00:41:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
namespace GUI {
|
|
|
|
|
2020-07-16 13:54:42 +00:00
|
|
|
InputBox::InputBox(Window* parent_window, const StringView& prompt, const StringView& title)
|
2020-03-04 19:53:51 +00:00
|
|
|
: Dialog(parent_window)
|
2019-03-19 00:41:00 +00:00
|
|
|
, m_prompt(prompt)
|
|
|
|
{
|
|
|
|
set_title(title);
|
|
|
|
build();
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
InputBox::~InputBox()
|
2019-03-19 00:41:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-16 13:54:42 +00:00
|
|
|
int InputBox::show(String& text_value, Window* parent_window, const StringView& prompt, const StringView& title)
|
|
|
|
{
|
|
|
|
auto box = InputBox::construct(parent_window, prompt, title);
|
2020-07-17 14:21:44 +00:00
|
|
|
box->set_resizable(false);
|
|
|
|
if (parent_window)
|
|
|
|
box->set_icon(parent_window->icon());
|
2020-07-16 13:54:42 +00:00
|
|
|
auto result = box->exec();
|
|
|
|
text_value = box->text_value();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
void InputBox::build()
|
2019-03-19 00:41:00 +00:00
|
|
|
{
|
2020-03-04 08:46:23 +00:00
|
|
|
auto& widget = set_main_widget<Widget>();
|
2019-03-19 00:41:00 +00:00
|
|
|
|
2020-03-04 08:46:23 +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 */;
|
2019-05-17 13:49:06 +00:00
|
|
|
int max_width = AK::max(text_width, title_width);
|
2019-03-19 00:41:00 +00:00
|
|
|
|
2020-07-17 14:21:44 +00:00
|
|
|
set_rect(x(), y(), max_width + 140, 62);
|
2019-03-19 00:41:00 +00:00
|
|
|
|
2020-03-04 08:46:23 +00:00
|
|
|
widget.set_layout<VerticalBoxLayout>();
|
|
|
|
widget.set_fill_with_background_color(true);
|
2019-03-19 00:41:00 +00:00
|
|
|
|
2020-07-17 14:21:44 +00:00
|
|
|
widget.layout()->set_margins({ 6, 6, 6, 6 });
|
|
|
|
widget.layout()->set_spacing(6);
|
2019-03-19 00:41:00 +00:00
|
|
|
|
2020-07-17 14:21:44 +00:00
|
|
|
auto& label_editor_container = widget.add<Widget>();
|
|
|
|
label_editor_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
|
|
|
|
label_editor_container.set_layout<HorizontalBoxLayout>();
|
|
|
|
|
|
|
|
auto& label = label_editor_container.add<Label>(m_prompt);
|
2020-03-04 18:07:55 +00:00
|
|
|
label.set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
|
|
|
|
label.set_preferred_size(text_width, 16);
|
2019-03-19 00:41:00 +00:00
|
|
|
|
2020-07-17 14:21:44 +00:00
|
|
|
m_text_editor = label_editor_container.add<TextBox>();
|
2019-03-19 01:20:00 +00:00
|
|
|
m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
2019-07-20 20:39:24 +00:00
|
|
|
m_text_editor->set_preferred_size(0, 19);
|
2019-03-19 00:41:00 +00:00
|
|
|
|
2020-03-04 18:07:55 +00: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 00:41:00 +00:00
|
|
|
|
2020-03-04 18:07:55 +00:00
|
|
|
auto& button_container_inner = button_container_outer.add<Widget>();
|
|
|
|
button_container_inner.set_layout<HorizontalBoxLayout>();
|
2020-07-17 14:21:44 +00:00
|
|
|
button_container_inner.layout()->set_spacing(6);
|
|
|
|
button_container_inner.layout()->set_margins({ 4, 4, 0, 4 });
|
|
|
|
button_container_inner.layout()->add_spacer();
|
2019-03-19 00:41:00 +00:00
|
|
|
|
2020-03-04 18:07:55 +00:00
|
|
|
m_ok_button = button_container_inner.add<Button>();
|
2019-03-19 01:20:00 +00:00
|
|
|
m_ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
2019-07-20 20:39:24 +00:00
|
|
|
m_ok_button->set_preferred_size(0, 20);
|
2019-05-24 14:32:20 +00:00
|
|
|
m_ok_button->set_text("OK");
|
2020-05-12 18:30:33 +00:00
|
|
|
m_ok_button->on_click = [this](auto) {
|
2020-03-19 21:29:01 +00:00
|
|
|
dbgprintf("GUI::InputBox: OK button clicked\n");
|
2019-03-19 01:20:00 +00:00
|
|
|
m_text_value = m_text_editor->text();
|
|
|
|
done(ExecOK);
|
2019-03-19 00:41:00 +00:00
|
|
|
};
|
|
|
|
|
2020-04-29 17:31:15 +00:00
|
|
|
m_cancel_button = button_container_inner.add<Button>();
|
|
|
|
m_cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
|
|
|
m_cancel_button->set_preferred_size(0, 20);
|
|
|
|
m_cancel_button->set_text("Cancel");
|
2020-05-12 18:30:33 +00:00
|
|
|
m_cancel_button->on_click = [this](auto) {
|
2020-04-29 17:31:15 +00:00
|
|
|
dbgprintf("GUI::InputBox: Cancel button clicked\n");
|
|
|
|
done(ExecCancel);
|
|
|
|
};
|
|
|
|
|
2019-04-10 01:08:29 +00:00
|
|
|
m_text_editor->on_return_pressed = [this] {
|
2019-03-19 01:20:00 +00:00
|
|
|
m_ok_button->click();
|
2019-03-19 00:41:00 +00:00
|
|
|
};
|
2019-04-10 01:08:29 +00:00
|
|
|
m_text_editor->on_escape_pressed = [this] {
|
2019-03-19 01:20:00 +00:00
|
|
|
m_cancel_button->click();
|
2019-03-19 00:41:00 +00:00
|
|
|
};
|
2019-03-19 01:20:00 +00:00
|
|
|
m_text_editor->set_focus(true);
|
2019-03-19 00:41:00 +00:00
|
|
|
}
|
2020-02-02 14:07:41 +00:00
|
|
|
|
|
|
|
}
|