2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Dialog.h>
|
|
|
|
|
#include <LibGUI/Event.h>
|
2019-03-19 00:01:02 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2020-03-04 20:53:51 +01:00
|
|
|
Dialog::Dialog(Window* parent_window)
|
|
|
|
|
: Window(parent_window)
|
2019-03-19 00:01:02 +01:00
|
|
|
{
|
|
|
|
|
set_modal(true);
|
2020-11-28 10:22:41 +01:00
|
|
|
set_minimizable(false);
|
2019-03-19 00:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Dialog::~Dialog()
|
2019-03-19 00:01:02 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
int Dialog::exec()
|
2019-03-19 00:01:02 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_event_loop);
|
2020-02-02 12:34:39 +01:00
|
|
|
m_event_loop = make<Core::EventLoop>();
|
2021-01-01 15:58:07 +01:00
|
|
|
if (parent() && is<Window>(parent())) {
|
2020-02-02 15:07:41 +01:00
|
|
|
auto& parent_window = *static_cast<Window*>(parent());
|
2020-08-16 13:05:42 +02:00
|
|
|
if (parent_window.is_visible()) {
|
2020-12-30 13:44:58 +01:00
|
|
|
center_within(parent_window);
|
2020-08-16 13:05:42 +02:00
|
|
|
} else {
|
|
|
|
|
center_on_screen();
|
|
|
|
|
}
|
2019-05-10 22:22:03 +02:00
|
|
|
} else {
|
2020-08-16 13:05:42 +02:00
|
|
|
center_on_screen();
|
2019-03-19 02:20:00 +01:00
|
|
|
}
|
2019-03-19 00:01:02 +01:00
|
|
|
show();
|
2019-03-19 02:20:00 +01:00
|
|
|
auto result = m_event_loop->exec();
|
|
|
|
|
m_event_loop = nullptr;
|
2021-01-11 13:32:26 +01:00
|
|
|
dbgln("{}: Event loop returned with result {}", *this, result);
|
2019-09-22 00:46:29 +02:00
|
|
|
remove_from_parent();
|
2019-03-19 02:20:00 +01:00
|
|
|
return result;
|
2019-03-19 00:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Dialog::done(int result)
|
2019-03-19 00:01:02 +01:00
|
|
|
{
|
2019-03-20 22:31:21 +01:00
|
|
|
if (!m_event_loop)
|
|
|
|
|
return;
|
2019-03-19 00:01:02 +01:00
|
|
|
m_result = result;
|
2021-01-11 13:32:26 +01:00
|
|
|
dbgln("{}: Quit event loop with result {}", *this, result);
|
2019-03-19 02:20:00 +01:00
|
|
|
m_event_loop->quit(result);
|
2019-03-19 00:01:02 +01:00
|
|
|
}
|
2019-07-26 16:13:59 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Dialog::event(Core::Event& event)
|
2020-01-01 11:58:37 +11:00
|
|
|
{
|
2021-07-12 16:04:41 +02:00
|
|
|
if (event.type() == Event::KeyUp || event.type() == Event::KeyDown) {
|
2020-02-02 15:07:41 +01:00
|
|
|
auto& key_event = static_cast<KeyEvent&>(event);
|
2020-01-01 11:58:37 +11:00
|
|
|
if (key_event.key() == KeyCode::Key_Escape) {
|
|
|
|
|
done(ExecCancel);
|
2020-01-21 21:37:22 +01:00
|
|
|
event.accept();
|
2020-01-01 11:58:37 +11:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Window::event(event);
|
2020-01-01 11:58:37 +11:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Dialog::close()
|
2019-07-26 16:13:59 +02:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
Window::close();
|
2019-07-26 16:13:59 +02:00
|
|
|
m_event_loop->quit(ExecCancel);
|
|
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|