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-03-28 16:19:56 +00:00
|
|
|
#include <LibGUI/GPainter.h>
|
2019-01-20 04:48:43 +00:00
|
|
|
#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-02-11 07:27:13 +00:00
|
|
|
//#define UPDATE_COALESCING_DEBUG
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
GWindow::GWindow(CObject* parent)
|
|
|
|
: CObject(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-03-18 23:01:02 +00:00
|
|
|
if (should_exit_event_loop_on_close())
|
|
|
|
GEventLoop::current().quit(0);
|
2019-01-30 19:03:52 +00:00
|
|
|
delete_later();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::show()
|
|
|
|
{
|
|
|
|
if (m_window_id)
|
|
|
|
return;
|
|
|
|
|
2019-02-15 08:17:18 +00:00
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::CreateWindow;
|
2019-02-14 00:21:32 +00:00
|
|
|
request.window_id = m_window_id;
|
|
|
|
request.window.rect = m_rect_when_windowless;
|
2019-02-19 00:42:53 +00:00
|
|
|
request.window.has_alpha_channel = m_has_alpha_channel;
|
2019-03-18 23:52:39 +00:00
|
|
|
request.window.modal = m_modal;
|
|
|
|
request.window.resizable = m_resizable;
|
2019-05-17 19:33:44 +00:00
|
|
|
request.window.fullscreen = m_fullscreen;
|
2019-05-24 21:37:23 +00:00
|
|
|
request.window.show_titlebar = m_show_titlebar;
|
2019-02-19 00:42:53 +00:00
|
|
|
request.window.opacity = m_opacity_when_windowless;
|
2019-04-10 12:29:47 +00:00
|
|
|
request.window.background_color = m_background_color.value();
|
2019-02-20 23:21:23 +00:00
|
|
|
request.window.size_increment = m_size_increment;
|
|
|
|
request.window.base_size = m_base_size;
|
2019-04-03 17:38:44 +00:00
|
|
|
request.window.type = (WSAPI_WindowType)m_window_type;
|
2019-02-25 21:06:55 +00:00
|
|
|
ASSERT(m_title_when_windowless.length() < (ssize_t)sizeof(request.text));
|
2019-02-14 00:21:32 +00:00
|
|
|
strcpy(request.text, m_title_when_windowless.characters());
|
|
|
|
request.text_length = m_title_when_windowless.length();
|
2019-03-18 23:01:02 +00:00
|
|
|
auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidCreateWindow);
|
2019-02-14 00:21:32 +00:00
|
|
|
m_window_id = response.window_id;
|
2019-01-20 04:48:43 +00:00
|
|
|
|
|
|
|
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);
|
2019-02-15 08:17:18 +00:00
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::DestroyWindow;
|
2019-02-14 00:21:32 +00:00
|
|
|
request.window_id = m_window_id;
|
2019-04-08 16:58:44 +00:00
|
|
|
GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidDestroyWindow);
|
|
|
|
m_window_id = 0;
|
|
|
|
m_pending_paint_event_rects.clear();
|
|
|
|
m_back_bitmap = nullptr;
|
|
|
|
m_front_bitmap = nullptr;
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 23:01:02 +00:00
|
|
|
void GWindow::set_title(const String& title)
|
2019-01-20 03:49:48 +00:00
|
|
|
{
|
2019-03-18 23:01:02 +00:00
|
|
|
m_title_when_windowless = title;
|
2019-02-14 00:21:32 +00:00
|
|
|
if (!m_window_id)
|
|
|
|
return;
|
|
|
|
|
2019-02-15 08:17:18 +00:00
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::SetWindowTitle;
|
2019-02-14 00:21:32 +00:00
|
|
|
request.window_id = m_window_id;
|
2019-02-25 21:06:55 +00:00
|
|
|
ASSERT(m_title_when_windowless.length() < (ssize_t)sizeof(request.text));
|
2019-02-14 00:21:32 +00:00
|
|
|
strcpy(request.text, m_title_when_windowless.characters());
|
|
|
|
request.text_length = m_title_when_windowless.length();
|
2019-03-18 23:01:02 +00:00
|
|
|
GEventLoop::current().post_message_to_server(request);
|
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-02-14 00:21:32 +00:00
|
|
|
if (!m_window_id)
|
|
|
|
return m_title_when_windowless;
|
|
|
|
|
2019-02-15 08:17:18 +00:00
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::GetWindowTitle;
|
2019-02-14 00:21:32 +00:00
|
|
|
request.window_id = m_window_id;
|
2019-03-18 23:01:02 +00:00
|
|
|
auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidGetWindowTitle);
|
2019-02-14 00:21:32 +00:00
|
|
|
return String(response.text, response.text_length);
|
2019-01-26 04:20:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 23:13:35 +00:00
|
|
|
Rect GWindow::rect() const
|
|
|
|
{
|
2019-02-14 00:21:32 +00:00
|
|
|
if (!m_window_id)
|
|
|
|
return m_rect_when_windowless;
|
|
|
|
|
2019-02-15 08:17:18 +00:00
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::GetWindowRect;
|
2019-02-14 00:21:32 +00:00
|
|
|
request.window_id = m_window_id;
|
2019-03-18 23:01:02 +00:00
|
|
|
auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidGetWindowRect);
|
2019-02-14 00:21:32 +00:00
|
|
|
ASSERT(response.window_id == m_window_id);
|
|
|
|
return response.window.rect;
|
2019-02-07 23:13:35 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2019-03-18 23:01:02 +00:00
|
|
|
if (!m_window_id) {
|
|
|
|
if (m_main_widget)
|
|
|
|
m_main_widget->resize(m_rect_when_windowless.size());
|
2019-02-14 00:21:32 +00:00
|
|
|
return;
|
2019-03-18 23:01:02 +00:00
|
|
|
}
|
2019-02-15 08:17:18 +00:00
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::SetWindowRect;
|
2019-02-14 00:21:32 +00:00
|
|
|
request.window_id = m_window_id;
|
|
|
|
request.window.rect = a_rect;
|
2019-03-18 23:01:02 +00:00
|
|
|
GEventLoop::current().post_message_to_server(request);
|
2019-04-26 18:08:30 +00:00
|
|
|
if (m_back_bitmap && m_back_bitmap->size() != a_rect.size())
|
2019-04-10 12:35:13 +00:00
|
|
|
m_back_bitmap = nullptr;
|
2019-04-26 18:08:30 +00:00
|
|
|
if (m_front_bitmap && m_front_bitmap->size() != a_rect.size())
|
2019-04-10 12:35:13 +00:00
|
|
|
m_front_bitmap = nullptr;
|
2019-04-08 16:58:44 +00:00
|
|
|
if (m_main_widget)
|
|
|
|
m_main_widget->resize(a_rect.size());
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 17:38:44 +00:00
|
|
|
void GWindow::set_window_type(GWindowType window_type)
|
|
|
|
{
|
|
|
|
m_window_type = window_type;
|
|
|
|
}
|
|
|
|
|
2019-03-31 21:52:02 +00:00
|
|
|
void GWindow::set_override_cursor(GStandardCursor cursor)
|
|
|
|
{
|
|
|
|
if (!m_window_id)
|
|
|
|
return;
|
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::SetWindowOverrideCursor;
|
|
|
|
request.window_id = m_window_id;
|
|
|
|
request.cursor.cursor = (WSAPI_StandardCursor)cursor;
|
|
|
|
GEventLoop::current().post_message_to_server(request);
|
|
|
|
}
|
|
|
|
|
2019-04-10 14:56:55 +00:00
|
|
|
void GWindow::event(CEvent& event)
|
2019-01-20 03:49:48 +00:00
|
|
|
{
|
2019-05-15 20:17:09 +00:00
|
|
|
if (event.type() == GEvent::MouseUp || event.type() == GEvent::MouseDown || event.type() == GEvent::MouseDoubleClick || event.type() == GEvent::MouseMove || event.type() == GEvent::MouseWheel) {
|
2019-03-24 14:01:56 +00:00
|
|
|
auto& mouse_event = static_cast<GMouseEvent&>(event);
|
2019-01-27 07:48:34 +00:00
|
|
|
if (m_global_cursor_tracking_widget) {
|
2019-02-26 09:34:05 +00:00
|
|
|
auto window_relative_rect = m_global_cursor_tracking_widget->window_relative_rect();
|
|
|
|
Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
|
2019-05-13 17:52:57 +00:00
|
|
|
auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
|
2019-01-27 07:48:34 +00:00
|
|
|
m_global_cursor_tracking_widget->event(*local_event);
|
2019-03-24 14:01:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_automatic_cursor_tracking_widget) {
|
|
|
|
auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect();
|
|
|
|
Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
|
2019-05-13 17:52:57 +00:00
|
|
|
auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
|
2019-03-24 14:01:56 +00:00
|
|
|
m_automatic_cursor_tracking_widget->event(*local_event);
|
2019-03-25 00:42:15 +00:00
|
|
|
if (mouse_event.buttons() == 0)
|
2019-03-24 14:01:56 +00:00
|
|
|
m_automatic_cursor_tracking_widget = nullptr;
|
|
|
|
return;
|
2019-01-27 07:48:34 +00:00
|
|
|
}
|
2019-01-20 06:03:38 +00:00
|
|
|
if (!m_main_widget)
|
|
|
|
return;
|
2019-05-13 17:52:57 +00:00
|
|
|
auto result = m_main_widget->hit_test(mouse_event.position());
|
|
|
|
auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), result.local_position, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
|
|
|
|
ASSERT(result.widget);
|
|
|
|
set_hovered_widget(result.widget);
|
|
|
|
if (mouse_event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
|
|
|
|
m_automatic_cursor_tracking_widget = result.widget->make_weak_ptr();
|
|
|
|
if (result.widget != m_global_cursor_tracking_widget.ptr())
|
|
|
|
return result.widget->event(*local_event);
|
2019-01-26 20:58:43 +00:00
|
|
|
return;
|
2019-01-20 06:03:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 15:37:28 +00:00
|
|
|
if (event.type() == GEvent::MultiPaint) {
|
2019-04-08 16:58:44 +00:00
|
|
|
if (!m_window_id)
|
|
|
|
return;
|
2019-01-20 06:03:38 +00:00
|
|
|
if (!m_main_widget)
|
|
|
|
return;
|
2019-04-20 15:37:28 +00:00
|
|
|
auto& paint_event = static_cast<GMultiPaintEvent&>(event);
|
|
|
|
auto rects = paint_event.rects();
|
|
|
|
ASSERT(!rects.is_empty());
|
2019-04-10 13:39:28 +00:00
|
|
|
if (m_back_bitmap && m_back_bitmap->size() != paint_event.window_size()) {
|
|
|
|
// Eagerly discard the backing store if we learn from this paint event that it needs to be bigger.
|
|
|
|
// Otherwise we would have to wait for a resize event to tell us. This way we don't waste the
|
|
|
|
// effort on painting into an undersized bitmap that will be thrown away anyway.
|
|
|
|
m_back_bitmap = nullptr;
|
|
|
|
}
|
2019-03-17 03:23:54 +00:00
|
|
|
bool created_new_backing_store = !m_back_bitmap;
|
|
|
|
if (!m_back_bitmap)
|
|
|
|
m_back_bitmap = create_backing_bitmap(paint_event.window_size());
|
|
|
|
|
2019-04-20 15:37:28 +00:00
|
|
|
auto rect = rects.first();
|
|
|
|
if (rect.is_empty() || created_new_backing_store) {
|
|
|
|
rects.clear();
|
|
|
|
rects.append({ { }, paint_event.window_size() });
|
|
|
|
}
|
|
|
|
|
2019-05-21 19:05:10 +00:00
|
|
|
for (auto& rect : rects)
|
2019-04-20 15:37:28 +00:00
|
|
|
m_main_widget->event(*make<GPaintEvent>(rect));
|
2019-03-17 03:23:54 +00:00
|
|
|
|
2019-06-08 14:13:16 +00:00
|
|
|
paint_keybinds();
|
2019-06-03 15:02:49 +00:00
|
|
|
|
2019-05-21 19:05:10 +00:00
|
|
|
if (m_double_buffering_enabled)
|
|
|
|
flip(rects);
|
|
|
|
else if (created_new_backing_store)
|
2019-03-17 03:23:54 +00:00
|
|
|
set_current_backing_bitmap(*m_back_bitmap, true);
|
|
|
|
|
2019-01-30 19:03:52 +00:00
|
|
|
if (m_window_id) {
|
2019-02-15 08:17:18 +00:00
|
|
|
WSAPI_ClientMessage message;
|
|
|
|
message.type = WSAPI_ClientMessage::Type::DidFinishPainting;
|
2019-02-14 00:21:32 +00:00
|
|
|
message.window_id = m_window_id;
|
2019-04-21 23:15:47 +00:00
|
|
|
message.rect_count = rects.size();
|
|
|
|
for (int i = 0; i < min(WSAPI_ClientMessage::max_inline_rect_count, rects.size()); ++i)
|
|
|
|
message.rects[i] = rects[i];
|
|
|
|
ByteBuffer extra_data;
|
|
|
|
if (rects.size() > WSAPI_ClientMessage::max_inline_rect_count)
|
2019-04-23 11:00:53 +00:00
|
|
|
extra_data = ByteBuffer::wrap(&rects[WSAPI_ClientMessage::max_inline_rect_count], (rects.size() - WSAPI_ClientMessage::max_inline_rect_count) * sizeof(WSAPI_Rect));
|
2019-04-21 23:15:47 +00:00
|
|
|
GEventLoop::current().post_message_to_server(message, extra_data);
|
2019-01-30 19:03:52 +00:00
|
|
|
}
|
2019-01-26 20:58:43 +00:00
|
|
|
return;
|
2019-01-20 06:03:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 14:56:55 +00:00
|
|
|
if (event.type() == GEvent::KeyUp || event.type() == GEvent::KeyDown) {
|
2019-06-03 15:02:49 +00:00
|
|
|
auto keyevent = static_cast<GKeyEvent&>(event);
|
|
|
|
if (keyevent.logo() && event.type() == GEvent::KeyUp) {
|
|
|
|
if (m_keybind_mode) {
|
|
|
|
m_keybind_mode = false;
|
|
|
|
} else {
|
|
|
|
m_keybind_mode = true;
|
|
|
|
find_keyboard_selectable();
|
|
|
|
m_entered_keybind = "";
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_keybind_mode) {
|
2019-06-03 17:13:41 +00:00
|
|
|
if (event.type() == GEvent::KeyUp) {
|
2019-06-08 14:13:16 +00:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(m_entered_keybind);
|
|
|
|
builder.append(keyevent.text());
|
|
|
|
m_entered_keybind = builder.to_string();
|
|
|
|
|
|
|
|
auto found_widget = m_hashed_potential_keybind_widgets.find(m_entered_keybind);
|
|
|
|
if (found_widget != m_hashed_potential_keybind_widgets.end()) {
|
|
|
|
m_keybind_mode = false;
|
|
|
|
const auto &point = Point();
|
|
|
|
auto event = make<GMouseEvent>(GEvent::MouseDown, point, 0, GMouseButton::Left, 0, 0);
|
|
|
|
found_widget->value->event(*event);
|
|
|
|
event = make<GMouseEvent>(GEvent::MouseUp, point, 0, GMouseButton::Left, 0, 0);
|
|
|
|
found_widget->value->event(*event);
|
|
|
|
} else if (m_entered_keybind.length() >= m_max_keybind_length) {
|
|
|
|
m_keybind_mode = false;
|
|
|
|
}
|
|
|
|
update();
|
2019-06-03 15:02:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (m_focused_widget)
|
2019-06-08 14:13:16 +00:00
|
|
|
return m_focused_widget->event(event);
|
2019-06-03 15:02:49 +00:00
|
|
|
if (m_main_widget)
|
2019-06-08 14:13:16 +00:00
|
|
|
return m_main_widget->event(event);
|
2019-06-03 15:02:49 +00:00
|
|
|
}
|
2019-02-10 13:28:39 +00:00
|
|
|
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-02-20 09:12:19 +00:00
|
|
|
if (event.type() == GEvent::WindowLeft) {
|
|
|
|
set_hovered_widget(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:34:55 +00:00
|
|
|
if (event.type() == GEvent::Resize) {
|
2019-02-27 17:52:12 +00:00
|
|
|
auto new_size = static_cast<GResizeEvent&>(event).size();
|
2019-03-17 03:23:54 +00:00
|
|
|
if (m_back_bitmap && m_back_bitmap->size() != new_size)
|
|
|
|
m_back_bitmap = nullptr;
|
2019-04-21 23:15:47 +00:00
|
|
|
if (!m_pending_paint_event_rects.is_empty()) {
|
|
|
|
m_pending_paint_event_rects.clear_with_capacity();
|
|
|
|
m_pending_paint_event_rects.append({ { }, new_size });
|
|
|
|
}
|
2019-02-27 17:52:12 +00:00
|
|
|
m_rect_when_windowless = { { }, new_size };
|
|
|
|
m_main_widget->set_relative_rect({ { }, new_size });
|
2019-02-20 14:34:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-20 12:40:38 +00:00
|
|
|
if (event.type() > GEvent::__Begin_WM_Events && event.type() < GEvent::__End_WM_Events)
|
2019-04-03 19:03:12 +00:00
|
|
|
return wm_event(static_cast<GWMEvent&>(event));
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
CObject::event(event);
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:13:16 +00:00
|
|
|
void GWindow::paint_keybinds() {
|
|
|
|
if (m_keybind_mode) {
|
|
|
|
GPainter painter(*m_main_widget);
|
|
|
|
|
|
|
|
for (auto& keypair: m_hashed_potential_keybind_widgets) {
|
|
|
|
auto widget = keypair.value;
|
|
|
|
auto rect = Rect(widget->x()-5, widget->y()-5, 12, 12);
|
|
|
|
bool could_be_keybind = true;
|
|
|
|
for (size_t i = 0; i < m_entered_keybind.length(); i++) {
|
|
|
|
if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) {
|
|
|
|
could_be_keybind = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (could_be_keybind) {
|
|
|
|
painter.draw_text(rect, keypair.key.characters(), TextAlignment::TopLeft, Color::Black);
|
|
|
|
painter.draw_text(rect, m_entered_keybind.characters(), TextAlignment::TopLeft, Color::Green);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 15:02:49 +00:00
|
|
|
void GWindow::find_keyboard_selectable() {
|
2019-06-08 14:13:16 +00:00
|
|
|
Vector<GWidget*> potential_keybind_widgets;
|
2019-06-03 15:02:49 +00:00
|
|
|
m_hashed_potential_keybind_widgets.clear();
|
2019-06-08 14:13:16 +00:00
|
|
|
find_keyboard_selectable_children(m_main_widget, potential_keybind_widgets);
|
2019-06-03 15:02:49 +00:00
|
|
|
|
2019-06-08 14:13:16 +00:00
|
|
|
m_max_keybind_length = ceil_div(potential_keybind_widgets.size(), ('z'-'a'));
|
2019-06-03 17:13:41 +00:00
|
|
|
size_t buffer_length = m_max_keybind_length + 1;
|
2019-06-03 15:02:49 +00:00
|
|
|
char keybind_buffer[buffer_length];
|
|
|
|
for (size_t i = 0; i < buffer_length-1; i++) {
|
2019-06-08 14:13:16 +00:00
|
|
|
keybind_buffer[i] = 'a';
|
2019-06-03 15:02:49 +00:00
|
|
|
}
|
|
|
|
keybind_buffer[buffer_length-1] = '\0';
|
|
|
|
|
2019-06-08 14:13:16 +00:00
|
|
|
for (auto& widget: potential_keybind_widgets) {
|
|
|
|
m_hashed_potential_keybind_widgets.set(String(keybind_buffer), widget);
|
2019-06-03 15:02:49 +00:00
|
|
|
|
2019-06-08 14:13:16 +00:00
|
|
|
for (size_t i = 0; i < buffer_length-1; i++) {
|
|
|
|
if (keybind_buffer[i] >= 'z') {
|
|
|
|
keybind_buffer[i] = 'a';
|
|
|
|
} else {
|
|
|
|
keybind_buffer[i]++;
|
|
|
|
break;
|
|
|
|
}
|
2019-06-03 15:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:13:16 +00:00
|
|
|
void GWindow::find_keyboard_selectable_children(GWidget* widget, Vector<GWidget*> &potential_keybind_widgets) {
|
2019-06-03 15:02:49 +00:00
|
|
|
widget -> for_each_child_widget([&] (auto& child) {
|
|
|
|
if (child.accepts_keyboard_select()) {
|
2019-06-08 14:13:16 +00:00
|
|
|
potential_keybind_widgets.append(&child);
|
|
|
|
find_keyboard_selectable_children(&child, potential_keybind_widgets);
|
2019-06-03 15:02:49 +00:00
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-06-03 15:02:49 +00:00
|
|
|
|
|
|
|
//We probably shouldn't clear the buffer on updates
|
|
|
|
//find_keyboard_selectable();
|
|
|
|
|
2019-02-10 13:46:43 +00:00
|
|
|
for (auto& pending_rect : m_pending_paint_event_rects) {
|
|
|
|
if (pending_rect.contains(a_rect)) {
|
2019-02-11 07:27:13 +00:00
|
|
|
#ifdef UPDATE_COALESCING_DEBUG
|
2019-02-10 13:46:43 +00:00
|
|
|
dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
|
2019-02-11 07:27:13 +00:00
|
|
|
#endif
|
2019-02-28 12:58:04 +00:00
|
|
|
return;
|
2019-02-10 13:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 13:19:02 +00:00
|
|
|
if (m_pending_paint_event_rects.is_empty()) {
|
|
|
|
deferred_invoke([this] (auto&) {
|
2019-04-23 18:43:51 +00:00
|
|
|
auto rects = move(m_pending_paint_event_rects);
|
|
|
|
if (rects.is_empty())
|
|
|
|
return;
|
2019-04-20 15:19:56 +00:00
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::InvalidateRect;
|
|
|
|
request.window_id = m_window_id;
|
2019-04-23 18:43:51 +00:00
|
|
|
for (int i = 0; i < min(WSAPI_ClientMessage::max_inline_rect_count, rects.size()); ++i)
|
|
|
|
request.rects[i] = rects[i];
|
2019-04-21 23:15:47 +00:00
|
|
|
ByteBuffer extra_data;
|
2019-04-23 18:43:51 +00:00
|
|
|
if (rects.size() > WSAPI_ClientMessage::max_inline_rect_count)
|
|
|
|
extra_data = ByteBuffer::wrap(&rects[WSAPI_ClientMessage::max_inline_rect_count], (rects.size() - WSAPI_ClientMessage::max_inline_rect_count) * sizeof(WSAPI_Rect));
|
|
|
|
request.rect_count = rects.size();
|
2019-04-21 23:15:47 +00:00
|
|
|
GEventLoop::current().post_message_to_server(request, extra_data);
|
2019-04-20 13:19:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
m_pending_paint_event_rects.append(a_rect);
|
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) {
|
2019-03-18 23:01:02 +00:00
|
|
|
GEventLoop::current().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-04-18 21:16:57 +00:00
|
|
|
m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
|
2019-01-26 10:24:16 +00:00
|
|
|
if (m_focused_widget) {
|
2019-03-18 23:01:02 +00:00
|
|
|
GEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusIn));
|
2019-01-26 10:24:16 +00:00
|
|
|
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-04-14 00:36:06 +00:00
|
|
|
if (widget == m_global_cursor_tracking_widget)
|
2019-01-27 07:48:34 +00:00
|
|
|
return;
|
2019-01-31 16:31:23 +00:00
|
|
|
m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
|
2019-03-24 14:01:56 +00:00
|
|
|
}
|
2019-02-14 00:21:32 +00:00
|
|
|
|
2019-03-24 14:01:56 +00:00
|
|
|
void GWindow::set_automatic_cursor_tracking_widget(GWidget* widget)
|
|
|
|
{
|
2019-04-14 00:36:06 +00:00
|
|
|
if (widget == m_automatic_cursor_tracking_widget)
|
2019-03-24 14:01:56 +00:00
|
|
|
return;
|
|
|
|
m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
|
2019-01-27 07:48:34 +00:00
|
|
|
}
|
2019-02-19 00:42:53 +00:00
|
|
|
|
|
|
|
void GWindow::set_has_alpha_channel(bool value)
|
|
|
|
{
|
2019-05-03 19:07:16 +00:00
|
|
|
if (m_has_alpha_channel == value)
|
|
|
|
return;
|
2019-02-19 00:42:53 +00:00
|
|
|
m_has_alpha_channel = value;
|
2019-05-03 19:07:16 +00:00
|
|
|
if (!m_window_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_pending_paint_event_rects.clear();
|
|
|
|
m_back_bitmap = nullptr;
|
|
|
|
m_front_bitmap = nullptr;
|
|
|
|
|
|
|
|
WSAPI_ClientMessage message;
|
|
|
|
message.type = WSAPI_ClientMessage::Type::SetWindowHasAlphaChannel;
|
|
|
|
message.window_id = m_window_id;
|
|
|
|
message.value = value;
|
|
|
|
GEventLoop::current().sync_request(message, WSAPI_ServerMessage::DidSetWindowHasAlphaChannel);
|
|
|
|
|
|
|
|
update();
|
2019-02-19 00:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 03:23:54 +00:00
|
|
|
void GWindow::set_double_buffering_enabled(bool value)
|
|
|
|
{
|
|
|
|
ASSERT(!m_window_id);
|
|
|
|
m_double_buffering_enabled = value;
|
|
|
|
}
|
|
|
|
|
2019-02-19 00:42:53 +00:00
|
|
|
void GWindow::set_opacity(float opacity)
|
|
|
|
{
|
|
|
|
m_opacity_when_windowless = opacity;
|
|
|
|
if (!m_window_id)
|
|
|
|
return;
|
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::SetWindowOpacity;
|
|
|
|
request.window_id = m_window_id;
|
|
|
|
request.window.opacity = opacity;
|
|
|
|
m_opacity_when_windowless = opacity;
|
2019-03-18 23:01:02 +00:00
|
|
|
GEventLoop::current().post_message_to_server(request);
|
2019-02-19 00:42:53 +00:00
|
|
|
}
|
2019-02-20 09:12:19 +00:00
|
|
|
|
|
|
|
void GWindow::set_hovered_widget(GWidget* widget)
|
|
|
|
{
|
2019-04-14 00:36:06 +00:00
|
|
|
if (widget == m_hovered_widget)
|
2019-02-20 09:12:19 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_hovered_widget)
|
2019-03-18 23:01:02 +00:00
|
|
|
GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Leave));
|
2019-02-20 09:12:19 +00:00
|
|
|
|
|
|
|
m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
|
|
|
|
|
|
|
|
if (m_hovered_widget)
|
2019-03-18 23:01:02 +00:00
|
|
|
GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Enter));
|
2019-02-20 09:12:19 +00:00
|
|
|
}
|
2019-03-17 03:23:54 +00:00
|
|
|
|
|
|
|
void GWindow::set_current_backing_bitmap(GraphicsBitmap& bitmap, bool flush_immediately)
|
|
|
|
{
|
|
|
|
WSAPI_ClientMessage message;
|
|
|
|
message.type = WSAPI_ClientMessage::Type::SetWindowBackingStore;
|
|
|
|
message.window_id = m_window_id;
|
|
|
|
message.backing.bpp = 32;
|
|
|
|
message.backing.pitch = bitmap.pitch();
|
|
|
|
message.backing.shared_buffer_id = bitmap.shared_buffer_id();
|
|
|
|
message.backing.has_alpha_channel = bitmap.has_alpha_channel();
|
|
|
|
message.backing.size = bitmap.size();
|
|
|
|
message.backing.flush_immediately = flush_immediately;
|
2019-03-18 23:01:02 +00:00
|
|
|
GEventLoop::current().sync_request(message, WSAPI_ServerMessage::Type::DidSetWindowBackingStore);
|
2019-03-17 03:23:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 19:05:10 +00:00
|
|
|
void GWindow::flip(const Vector<Rect, 32>& dirty_rects)
|
2019-03-17 03:23:54 +00:00
|
|
|
{
|
|
|
|
swap(m_front_bitmap, m_back_bitmap);
|
|
|
|
|
|
|
|
set_current_backing_bitmap(*m_front_bitmap);
|
|
|
|
|
|
|
|
if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) {
|
|
|
|
m_back_bitmap = create_backing_bitmap(m_front_bitmap->size());
|
2019-05-08 01:33:44 +00:00
|
|
|
memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size_in_bytes());
|
2019-03-17 03:23:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy whatever was painted from the front to the back.
|
|
|
|
Painter painter(*m_back_bitmap);
|
2019-05-21 19:05:10 +00:00
|
|
|
for (auto& dirty_rect : dirty_rects)
|
|
|
|
painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect);
|
2019-03-17 03:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Retained<GraphicsBitmap> GWindow::create_backing_bitmap(const Size& size)
|
|
|
|
{
|
|
|
|
ASSERT(GEventLoop::server_pid());
|
|
|
|
ASSERT(!size.is_empty());
|
2019-05-06 12:04:54 +00:00
|
|
|
size_t pitch = round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16);
|
|
|
|
size_t size_in_bytes = size.height() * pitch;
|
2019-03-17 03:23:54 +00:00
|
|
|
auto shared_buffer = SharedBuffer::create(GEventLoop::server_pid(), size_in_bytes);
|
|
|
|
ASSERT(shared_buffer);
|
|
|
|
auto format = m_has_alpha_channel ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32;
|
|
|
|
return GraphicsBitmap::create_with_shared_buffer(format, *shared_buffer, size);
|
|
|
|
}
|
2019-03-18 23:01:02 +00:00
|
|
|
|
|
|
|
void GWindow::set_modal(bool modal)
|
|
|
|
{
|
|
|
|
ASSERT(!m_window_id);
|
|
|
|
m_modal = modal;
|
|
|
|
}
|
2019-04-03 19:03:12 +00:00
|
|
|
|
|
|
|
void GWindow::wm_event(GWMEvent&)
|
|
|
|
{
|
|
|
|
}
|
2019-04-13 14:59:55 +00:00
|
|
|
|
|
|
|
void GWindow::set_icon_path(const String& path)
|
|
|
|
{
|
|
|
|
if (m_icon_path == path)
|
|
|
|
return;
|
|
|
|
m_icon_path = path;
|
|
|
|
if (!m_window_id)
|
|
|
|
return;
|
|
|
|
WSAPI_ClientMessage message;
|
|
|
|
message.type = WSAPI_ClientMessage::Type::SetWindowIcon;
|
|
|
|
message.window_id = m_window_id;
|
|
|
|
ASSERT(path.length() < sizeof(message.text));
|
|
|
|
strcpy(message.text, path.characters());
|
|
|
|
message.text_length = path.length();
|
|
|
|
GEventLoop::post_message_to_server(message);
|
|
|
|
}
|
2019-05-02 23:38:24 +00:00
|
|
|
|
|
|
|
void GWindow::start_wm_resize()
|
|
|
|
{
|
|
|
|
WSAPI_ClientMessage message;
|
|
|
|
message.type = WSAPI_ClientMessage::Type::WM_StartWindowResize;
|
|
|
|
message.wm.client_id = GEventLoop::my_client_id();
|
|
|
|
message.wm.window_id = m_window_id;
|
|
|
|
GEventLoop::post_message_to_server(message);
|
|
|
|
}
|
2019-05-15 00:39:58 +00:00
|
|
|
|
|
|
|
Vector<GWidget*> GWindow::focusable_widgets() const
|
|
|
|
{
|
|
|
|
if (!m_main_widget)
|
|
|
|
return { };
|
|
|
|
|
|
|
|
Vector<GWidget*> collected_widgets;
|
|
|
|
|
|
|
|
Function<void(GWidget&)> collect_focusable_widgets = [&] (GWidget& widget) {
|
|
|
|
if (widget.accepts_focus())
|
|
|
|
collected_widgets.append(&widget);
|
2019-05-27 01:52:33 +00:00
|
|
|
widget.for_each_child_widget([&] (auto& child) {
|
|
|
|
if (!child.is_visible())
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
if (!child.is_enabled())
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
collect_focusable_widgets(child);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2019-05-15 00:39:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
collect_focusable_widgets(*m_main_widget);
|
|
|
|
return collected_widgets;
|
|
|
|
}
|