2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-02-16 11:17:49 +03:00
|
|
|
#include <LibCore/EventLoop.h>
|
2021-07-14 13:01:31 +03:00
|
|
|
#include <LibGUI/Desktop.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Dialog.h>
|
|
|
|
#include <LibGUI/Event.h>
|
2019-03-19 02:01:02 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
2021-07-14 13:01:31 +03:00
|
|
|
Dialog::Dialog(Window* parent_window, ScreenPosition screen_position)
|
2020-03-04 22:53:51 +03:00
|
|
|
: Window(parent_window)
|
2021-07-14 13:01:31 +03:00
|
|
|
, m_screen_position(screen_position)
|
2019-03-19 02:01:02 +03:00
|
|
|
{
|
|
|
|
set_modal(true);
|
2020-11-28 12:22:41 +03:00
|
|
|
set_minimizable(false);
|
2019-03-19 02:01:02 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Dialog::~Dialog()
|
2019-03-19 02:01:02 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
int Dialog::exec()
|
2019-03-19 02:01:02 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_event_loop);
|
2020-02-02 14:34:39 +03:00
|
|
|
m_event_loop = make<Core::EventLoop>();
|
2021-07-14 13:01:31 +03:00
|
|
|
|
|
|
|
auto desktop_rect = Desktop::the().rect();
|
|
|
|
auto window_rect = rect();
|
|
|
|
|
|
|
|
auto top_align = [](Gfx::Rect<int>& rect) { rect.set_y(32); };
|
|
|
|
auto bottom_align = [this, desktop_rect](Gfx::Rect<int>& rect) { rect.set_y(desktop_rect.height() - Desktop::the().taskbar_height() - height() - 12); };
|
|
|
|
|
|
|
|
auto left_align = [](Gfx::Rect<int>& rect) { rect.set_x(12); };
|
|
|
|
auto right_align = [this, desktop_rect](Gfx::Rect<int>& rect) { rect.set_x(desktop_rect.width() - width() - 12); };
|
|
|
|
|
|
|
|
switch (m_screen_position) {
|
|
|
|
case CenterWithinParent:
|
|
|
|
if (parent() && is<Window>(parent())) {
|
|
|
|
auto& parent_window = *static_cast<Window*>(parent());
|
|
|
|
if (parent_window.is_visible()) {
|
|
|
|
window_rect.center_within(parent_window.rect());
|
|
|
|
break;
|
|
|
|
}
|
2020-08-16 14:05:42 +03:00
|
|
|
}
|
2021-07-14 13:01:31 +03:00
|
|
|
[[fallthrough]]; // Fall back to `Center` if parent window is invalid or not visible
|
|
|
|
case Center:
|
|
|
|
window_rect.center_within(desktop_rect);
|
|
|
|
break;
|
|
|
|
case CenterLeft:
|
|
|
|
left_align(window_rect);
|
|
|
|
window_rect.center_vertically_within(desktop_rect);
|
|
|
|
break;
|
|
|
|
case CenterRight:
|
|
|
|
right_align(window_rect);
|
|
|
|
window_rect.center_vertically_within(desktop_rect);
|
|
|
|
break;
|
|
|
|
case TopLeft:
|
|
|
|
left_align(window_rect);
|
|
|
|
top_align(window_rect);
|
|
|
|
break;
|
|
|
|
case TopCenter:
|
|
|
|
window_rect.center_horizontally_within(desktop_rect);
|
|
|
|
top_align(window_rect);
|
|
|
|
break;
|
|
|
|
case TopRight:
|
|
|
|
right_align(window_rect);
|
|
|
|
top_align(window_rect);
|
|
|
|
break;
|
|
|
|
case BottomLeft:
|
|
|
|
left_align(window_rect);
|
|
|
|
bottom_align(window_rect);
|
|
|
|
break;
|
|
|
|
case BottomCenter:
|
|
|
|
window_rect.center_horizontally_within(desktop_rect);
|
|
|
|
bottom_align(window_rect);
|
|
|
|
break;
|
|
|
|
case BottomRight:
|
|
|
|
right_align(window_rect);
|
|
|
|
bottom_align(window_rect);
|
|
|
|
break;
|
2019-03-19 04:20:00 +03:00
|
|
|
}
|
2021-07-14 13:01:31 +03:00
|
|
|
|
|
|
|
set_rect(window_rect);
|
2019-03-19 02:01:02 +03:00
|
|
|
show();
|
2019-03-19 04:20:00 +03:00
|
|
|
auto result = m_event_loop->exec();
|
|
|
|
m_event_loop = nullptr;
|
2021-01-11 15:32:26 +03:00
|
|
|
dbgln("{}: Event loop returned with result {}", *this, result);
|
2019-09-22 01:46:29 +03:00
|
|
|
remove_from_parent();
|
2019-03-19 04:20:00 +03:00
|
|
|
return result;
|
2019-03-19 02:01:02 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Dialog::done(int result)
|
2019-03-19 02:01:02 +03:00
|
|
|
{
|
2019-03-21 00:31:21 +03:00
|
|
|
if (!m_event_loop)
|
|
|
|
return;
|
2019-03-19 02:01:02 +03:00
|
|
|
m_result = result;
|
2021-01-11 15:32:26 +03:00
|
|
|
dbgln("{}: Quit event loop with result {}", *this, result);
|
2019-03-19 04:20:00 +03:00
|
|
|
m_event_loop->quit(result);
|
2019-03-19 02:01:02 +03:00
|
|
|
}
|
2019-07-26 17:13:59 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Dialog::event(Core::Event& event)
|
2020-01-01 03:58:37 +03:00
|
|
|
{
|
2021-07-12 17:04:41 +03:00
|
|
|
if (event.type() == Event::KeyUp || event.type() == Event::KeyDown) {
|
2020-02-02 17:07:41 +03:00
|
|
|
auto& key_event = static_cast<KeyEvent&>(event);
|
2020-01-01 03:58:37 +03:00
|
|
|
if (key_event.key() == KeyCode::Key_Escape) {
|
|
|
|
done(ExecCancel);
|
2020-01-21 23:37:22 +03:00
|
|
|
event.accept();
|
2020-01-01 03:58:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Window::event(event);
|
2020-01-01 03:58:37 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Dialog::close()
|
2019-07-26 17:13:59 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
Window::close();
|
2019-07-26 17:13:59 +03:00
|
|
|
m_event_loop->quit(ExecCancel);
|
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
|
|
|
|
}
|