2019-01-20 03:49:48 +00:00
|
|
|
#include "GWindow.h"
|
|
|
|
#include "GEvent.h"
|
|
|
|
#include "GEventLoop.h"
|
2019-01-20 06:03:38 +00:00
|
|
|
#include "GWidget.h"
|
2019-01-20 03:49:48 +00:00
|
|
|
#include <SharedGraphics/GraphicsBitmap.h>
|
2019-01-20 04:48:43 +00:00
|
|
|
#include <LibC/gui.h>
|
|
|
|
#include <LibC/stdio.h>
|
|
|
|
#include <LibC/stdlib.h>
|
2019-01-22 15:34:24 +00:00
|
|
|
#include <LibC/unistd.h>
|
2019-01-20 04:48:43 +00:00
|
|
|
#include <AK/HashMap.h>
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-01-20 04:48:43 +00:00
|
|
|
static HashMap<int, GWindow*>* s_windows;
|
|
|
|
|
|
|
|
static HashMap<int, GWindow*>& windows()
|
|
|
|
{
|
|
|
|
if (!s_windows)
|
|
|
|
s_windows = new HashMap<int, GWindow*>;
|
|
|
|
return *s_windows;
|
|
|
|
}
|
|
|
|
|
|
|
|
GWindow* GWindow::from_window_id(int window_id)
|
|
|
|
{
|
|
|
|
auto it = windows().find(window_id);
|
|
|
|
if (it != windows().end())
|
|
|
|
return (*it).value;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GWindow::GWindow(GObject* parent)
|
|
|
|
: GObject(parent)
|
2019-01-20 03:49:48 +00:00
|
|
|
{
|
2019-01-30 19:03:52 +00:00
|
|
|
m_rect_when_windowless = { 100, 400, 140, 140 };
|
|
|
|
m_title_when_windowless = "GWindow";
|
|
|
|
}
|
|
|
|
|
|
|
|
GWindow::~GWindow()
|
|
|
|
{
|
2019-02-05 09:31:37 +00:00
|
|
|
if (m_main_widget)
|
|
|
|
delete m_main_widget;
|
2019-01-30 19:03:52 +00:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::close()
|
|
|
|
{
|
2019-02-05 09:31:37 +00:00
|
|
|
// FIXME: If we exit the event loop, we're never gonna deal with the delete_later request!
|
|
|
|
// This will become relevant once we support nested event loops.
|
|
|
|
if (should_exit_app_on_close())
|
|
|
|
GEventLoop::main().exit(0);
|
2019-01-30 19:03:52 +00:00
|
|
|
delete_later();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::show()
|
|
|
|
{
|
|
|
|
if (m_window_id)
|
|
|
|
return;
|
|
|
|
|
2019-01-20 05:02:09 +00:00
|
|
|
GUI_WindowParameters wparams;
|
2019-01-30 19:03:52 +00:00
|
|
|
wparams.rect = m_rect_when_windowless;
|
2019-01-20 04:48:43 +00:00
|
|
|
wparams.background_color = 0xffc0c0;
|
2019-01-30 19:03:52 +00:00
|
|
|
strcpy(wparams.title, m_title_when_windowless.characters());
|
2019-01-20 04:48:43 +00:00
|
|
|
m_window_id = gui_create_window(&wparams);
|
|
|
|
if (m_window_id < 0) {
|
|
|
|
perror("gui_create_window");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
windows().set(m_window_id, this);
|
2019-01-30 19:03:52 +00:00
|
|
|
update();
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 19:03:52 +00:00
|
|
|
void GWindow::hide()
|
2019-01-20 03:49:48 +00:00
|
|
|
{
|
2019-01-30 19:03:52 +00:00
|
|
|
if (!m_window_id)
|
|
|
|
return;
|
|
|
|
windows().remove(m_window_id);
|
|
|
|
int rc = gui_destroy_window(m_window_id);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("gui_destroy_window");
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::set_title(String&& title)
|
|
|
|
{
|
2019-01-30 19:03:52 +00:00
|
|
|
m_title_when_windowless = title;
|
|
|
|
if (m_window_id) {
|
|
|
|
int rc = gui_set_window_title(m_window_id, title.characters(), title.length());
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("gui_set_window_title");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
2019-01-20 05:02:09 +00:00
|
|
|
|
2019-01-26 04:20:32 +00:00
|
|
|
String GWindow::title() const
|
|
|
|
{
|
2019-01-30 19:03:52 +00:00
|
|
|
if (m_window_id) {
|
|
|
|
char buffer[256];
|
|
|
|
int rc = gui_get_window_title(m_window_id, buffer, sizeof(buffer));
|
|
|
|
ASSERT(rc >= 0);
|
|
|
|
return String(buffer, rc);
|
|
|
|
}
|
|
|
|
return m_title_when_windowless;
|
2019-01-26 04:20:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 23:13:35 +00:00
|
|
|
Rect GWindow::rect() const
|
|
|
|
{
|
|
|
|
if (m_window_id) {
|
|
|
|
GUI_Rect buffer;
|
|
|
|
int rc = gui_get_window_rect(m_window_id, &buffer);
|
|
|
|
ASSERT(rc >= 0);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
return m_rect_when_windowless;
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:40:12 +00:00
|
|
|
void GWindow::set_rect(const Rect& a_rect)
|
2019-01-20 03:49:48 +00:00
|
|
|
{
|
2019-01-30 19:03:52 +00:00
|
|
|
m_rect_when_windowless = a_rect;
|
|
|
|
if (m_window_id) {
|
|
|
|
GUI_Rect rect = a_rect;
|
|
|
|
int rc = gui_set_window_rect(m_window_id, &rect);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::event(GEvent& event)
|
|
|
|
{
|
2019-01-20 06:03:38 +00:00
|
|
|
if (event.is_mouse_event()) {
|
2019-01-27 07:48:34 +00:00
|
|
|
if (m_global_cursor_tracking_widget) {
|
|
|
|
// FIXME: This won't work for widgets-within-widgets.
|
|
|
|
auto& mouse_event = static_cast<GMouseEvent&>(event);
|
|
|
|
Point local_point { mouse_event.x() - m_global_cursor_tracking_widget->relative_rect().x(), mouse_event.y() - m_global_cursor_tracking_widget->relative_rect().y() };
|
|
|
|
auto local_event = make<GMouseEvent>(event.type(), local_point, mouse_event.buttons(), mouse_event.button());
|
|
|
|
m_global_cursor_tracking_widget->event(*local_event);
|
|
|
|
}
|
2019-01-20 06:03:38 +00:00
|
|
|
if (!m_main_widget)
|
|
|
|
return;
|
|
|
|
auto& mouse_event = static_cast<GMouseEvent&>(event);
|
|
|
|
if (m_main_widget) {
|
2019-01-20 23:46:08 +00:00
|
|
|
auto result = m_main_widget->hit_test(mouse_event.x(), mouse_event.y());
|
2019-01-20 06:03:38 +00:00
|
|
|
auto local_event = make<GMouseEvent>(event.type(), Point { result.localX, result.localY }, mouse_event.buttons(), mouse_event.button());
|
|
|
|
ASSERT(result.widget);
|
|
|
|
return result.widget->event(*local_event);
|
|
|
|
}
|
2019-01-26 20:58:43 +00:00
|
|
|
return;
|
2019-01-20 06:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event.is_paint_event()) {
|
2019-02-10 13:46:43 +00:00
|
|
|
m_pending_paint_event_rects.clear();
|
2019-01-20 06:03:38 +00:00
|
|
|
if (!m_main_widget)
|
|
|
|
return;
|
|
|
|
auto& paint_event = static_cast<GPaintEvent&>(event);
|
2019-01-20 06:56:48 +00:00
|
|
|
auto rect = paint_event.rect();
|
|
|
|
if (rect.is_empty())
|
|
|
|
rect = m_main_widget->rect();
|
|
|
|
m_main_widget->event(*make<GPaintEvent>(rect));
|
2019-01-30 19:03:52 +00:00
|
|
|
if (m_window_id) {
|
|
|
|
GUI_Rect gui_rect = rect;
|
|
|
|
int rc = gui_notify_paint_finished(m_window_id, &gui_rect);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
2019-01-26 20:58:43 +00:00
|
|
|
return;
|
2019-01-20 06:03:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 05:39:13 +00:00
|
|
|
if (event.is_key_event()) {
|
2019-02-10 13:28:39 +00:00
|
|
|
if (m_focused_widget)
|
|
|
|
return m_focused_widget->event(event);
|
|
|
|
if (m_main_widget)
|
|
|
|
return m_main_widget->event(event);
|
|
|
|
return;
|
2019-01-26 05:39:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 20:58:43 +00:00
|
|
|
if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) {
|
|
|
|
m_is_active = event.type() == GEvent::WindowBecameActive;
|
2019-02-10 13:28:39 +00:00
|
|
|
if (m_main_widget)
|
|
|
|
m_main_widget->event(event);
|
2019-01-26 20:58:43 +00:00
|
|
|
if (m_focused_widget)
|
|
|
|
m_focused_widget->update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-05 09:31:37 +00:00
|
|
|
if (event.type() == GEvent::WindowCloseRequest) {
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-26 20:58:43 +00:00
|
|
|
GObject::event(event);
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GWindow::is_visible() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-26 04:20:32 +00:00
|
|
|
void GWindow::update(const Rect& a_rect)
|
2019-01-20 04:48:43 +00:00
|
|
|
{
|
2019-01-30 19:03:52 +00:00
|
|
|
if (!m_window_id)
|
|
|
|
return;
|
2019-02-10 13:46:43 +00:00
|
|
|
for (auto& pending_rect : m_pending_paint_event_rects) {
|
|
|
|
if (pending_rect.contains(a_rect)) {
|
|
|
|
dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_pending_paint_event_rects.append(a_rect);
|
|
|
|
|
2019-01-26 04:20:32 +00:00
|
|
|
GUI_Rect rect = a_rect;
|
|
|
|
int rc = gui_invalidate_window(m_window_id, a_rect.is_null() ? nullptr : &rect);
|
|
|
|
ASSERT(rc == 0);
|
2019-01-20 04:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::set_main_widget(GWidget* widget)
|
|
|
|
{
|
|
|
|
if (m_main_widget == widget)
|
|
|
|
return;
|
|
|
|
m_main_widget = widget;
|
2019-02-10 13:28:39 +00:00
|
|
|
if (m_main_widget) {
|
|
|
|
auto new_window_rect = rect();
|
|
|
|
if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
|
|
|
|
new_window_rect.set_width(m_main_widget->preferred_size().width());
|
|
|
|
if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
|
|
|
|
new_window_rect.set_height(m_main_widget->preferred_size().height());
|
|
|
|
set_rect(new_window_rect);
|
|
|
|
m_main_widget->set_relative_rect({ { }, new_window_rect.size() });
|
|
|
|
m_main_widget->set_window(this);
|
|
|
|
if (m_main_widget->accepts_focus())
|
|
|
|
m_main_widget->set_focus(true);
|
|
|
|
}
|
2019-01-20 04:48:43 +00:00
|
|
|
update();
|
|
|
|
}
|
2019-01-26 05:39:13 +00:00
|
|
|
|
|
|
|
void GWindow::set_focused_widget(GWidget* widget)
|
|
|
|
{
|
|
|
|
if (m_focused_widget == widget)
|
|
|
|
return;
|
2019-01-26 10:24:16 +00:00
|
|
|
if (m_focused_widget) {
|
|
|
|
GEventLoop::main().post_event(m_focused_widget, make<GEvent>(GEvent::FocusOut));
|
2019-01-26 05:39:13 +00:00
|
|
|
m_focused_widget->update();
|
2019-01-26 10:24:16 +00:00
|
|
|
}
|
2019-01-26 05:39:13 +00:00
|
|
|
m_focused_widget = widget;
|
2019-01-26 10:24:16 +00:00
|
|
|
if (m_focused_widget) {
|
|
|
|
GEventLoop::main().post_event(m_focused_widget, make<GEvent>(GEvent::FocusIn));
|
|
|
|
m_focused_widget->update();
|
|
|
|
}
|
2019-01-26 05:39:13 +00:00
|
|
|
}
|
2019-01-27 07:48:34 +00:00
|
|
|
|
|
|
|
void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
|
|
|
|
{
|
2019-01-30 19:03:52 +00:00
|
|
|
ASSERT(m_window_id);
|
2019-01-27 07:48:34 +00:00
|
|
|
if (widget == m_global_cursor_tracking_widget.ptr())
|
|
|
|
return;
|
2019-01-31 16:31:23 +00:00
|
|
|
m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
|
2019-01-27 07:48:34 +00:00
|
|
|
gui_set_global_cursor_tracking_enabled(m_window_id, widget != nullptr);
|
|
|
|
}
|