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.
|
|
|
|
*/
|
|
|
|
|
2019-06-07 09:46:02 +00:00
|
|
|
#include <AK/HashMap.h>
|
2019-08-18 18:39:46 +00:00
|
|
|
#include <AK/JsonObject.h>
|
2019-12-26 21:13:50 +00:00
|
|
|
#include <AK/NeverDestroyed.h>
|
2020-01-01 17:53:34 +00:00
|
|
|
#include <AK/SharedBuffer.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-07-18 08:15:00 +00:00
|
|
|
#include <LibDraw/GraphicsBitmap.h>
|
2020-02-02 00:57:57 +00:00
|
|
|
#include <LibGUI/GAction.h>
|
2019-07-23 16:16:25 +00:00
|
|
|
#include <LibGUI/GApplication.h>
|
|
|
|
#include <LibGUI/GEvent.h>
|
|
|
|
#include <LibGUI/GPainter.h>
|
|
|
|
#include <LibGUI/GWidget.h>
|
|
|
|
#include <LibGUI/GWindow.h>
|
2019-11-08 10:39:45 +00:00
|
|
|
#include <LibGUI/GWindowServerConnection.h>
|
2019-01-20 03:49:48 +00:00
|
|
|
|
2019-02-11 07:27:13 +00:00
|
|
|
//#define UPDATE_COALESCING_DEBUG
|
|
|
|
|
2019-12-26 21:13:50 +00:00
|
|
|
static NeverDestroyed<HashTable<GWindow*>> all_windows;
|
|
|
|
static NeverDestroyed<HashMap<int, GWindow*>> reified_windows;
|
2019-01-20 04:48:43 +00:00
|
|
|
|
|
|
|
GWindow* GWindow::from_window_id(int window_id)
|
|
|
|
{
|
2019-12-26 21:13:50 +00:00
|
|
|
auto it = reified_windows->find(window_id);
|
|
|
|
if (it != reified_windows->end())
|
2019-01-20 04:48:43 +00:00
|
|
|
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-12-26 21:13:50 +00:00
|
|
|
all_windows->set(this);
|
2019-01-30 19:03:52 +00:00
|
|
|
m_rect_when_windowless = { 100, 400, 140, 140 };
|
|
|
|
m_title_when_windowless = "GWindow";
|
|
|
|
}
|
|
|
|
|
|
|
|
GWindow::~GWindow()
|
|
|
|
{
|
2019-12-26 21:13:50 +00:00
|
|
|
all_windows->remove(this);
|
2019-01-30 19:03:52 +00:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::close()
|
|
|
|
{
|
2019-09-21 16:53:17 +00:00
|
|
|
hide();
|
2019-01-30 19:03:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 18:10:37 +00:00
|
|
|
void GWindow::move_to_front()
|
|
|
|
{
|
|
|
|
if (!m_window_id)
|
|
|
|
return;
|
|
|
|
|
2019-12-02 08:33:37 +00:00
|
|
|
GWindowServerConnection::the().send_sync<WindowServer::MoveWindowToFront>(m_window_id);
|
2019-06-01 18:10:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 19:03:52 +00:00
|
|
|
void GWindow::show()
|
|
|
|
{
|
|
|
|
if (m_window_id)
|
|
|
|
return;
|
2019-12-02 08:33:37 +00:00
|
|
|
auto response = GWindowServerConnection::the().send_sync<WindowServer::CreateWindow>(
|
|
|
|
m_rect_when_windowless,
|
|
|
|
m_has_alpha_channel,
|
|
|
|
m_modal,
|
2020-01-04 11:12:02 +00:00
|
|
|
m_minimizable,
|
2019-12-02 08:33:37 +00:00
|
|
|
m_resizable,
|
|
|
|
m_fullscreen,
|
|
|
|
m_show_titlebar,
|
|
|
|
m_opacity_when_windowless,
|
|
|
|
m_base_size,
|
|
|
|
m_size_increment,
|
|
|
|
(i32)m_window_type,
|
|
|
|
m_title_when_windowless);
|
|
|
|
m_window_id = response->window_id();
|
2019-01-20 04:48:43 +00:00
|
|
|
|
2019-08-02 19:02:56 +00:00
|
|
|
apply_icon();
|
|
|
|
|
2019-12-26 21:13:50 +00:00
|
|
|
reified_windows->set(m_window_id, this);
|
2019-10-25 05:23:35 +00:00
|
|
|
GApplication::the().did_create_window({});
|
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;
|
2019-12-26 21:13:50 +00:00
|
|
|
reified_windows->remove(m_window_id);
|
2019-12-02 08:33:37 +00:00
|
|
|
GWindowServerConnection::the().send_sync<WindowServer::DestroyWindow>(m_window_id);
|
2019-04-08 16:58:44 +00:00
|
|
|
m_window_id = 0;
|
|
|
|
m_pending_paint_event_rects.clear();
|
|
|
|
m_back_bitmap = nullptr;
|
|
|
|
m_front_bitmap = nullptr;
|
2019-09-21 16:53:17 +00:00
|
|
|
|
|
|
|
bool app_has_visible_windows = false;
|
2019-12-26 21:13:50 +00:00
|
|
|
for (auto& window : *all_windows) {
|
2019-09-21 16:53:17 +00:00
|
|
|
if (window->is_visible()) {
|
|
|
|
app_has_visible_windows = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!app_has_visible_windows)
|
|
|
|
GApplication::the().did_delete_last_window({});
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 12:58:02 +00:00
|
|
|
void GWindow::set_title(const StringView& 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-12-02 08:33:37 +00:00
|
|
|
GWindowServerConnection::the().send_sync<WindowServer::SetWindowTitle>(m_window_id, title);
|
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-12-02 08:33:37 +00:00
|
|
|
return GWindowServerConnection::the().send_sync<WindowServer::GetWindowTitle>(m_window_id)->title();
|
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-12-02 08:33:37 +00:00
|
|
|
return GWindowServerConnection::the().send_sync<WindowServer::GetWindowRect>(m_window_id)->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-12-02 08:33:37 +00:00
|
|
|
GWindowServerConnection::the().send_sync<WindowServer::SetWindowRect>(m_window_id, a_rect);
|
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;
|
2019-12-02 08:33:37 +00:00
|
|
|
GWindowServerConnection::the().send_sync<WindowServer::SetWindowOverrideCursor>(m_window_id, (u32)cursor);
|
2019-03-31 21:52:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 14:56:55 +00:00
|
|
|
void GWindow::event(CEvent& event)
|
2019-01-20 03:49:48 +00:00
|
|
|
{
|
2019-12-08 15:50:23 +00:00
|
|
|
if (event.type() == GEvent::Drop) {
|
|
|
|
auto& drop_event = static_cast<GDropEvent&>(event);
|
|
|
|
if (!m_main_widget)
|
|
|
|
return;
|
|
|
|
auto result = m_main_widget->hit_test(drop_event.position());
|
2019-12-19 19:09:31 +00:00
|
|
|
auto local_event = make<GDropEvent>(result.local_position, drop_event.text(), drop_event.data_type(), drop_event.data());
|
2019-12-08 15:50:23 +00:00
|
|
|
ASSERT(result.widget);
|
|
|
|
return result.widget->dispatch_event(*local_event, this);
|
|
|
|
}
|
|
|
|
|
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-09-20 18:37:31 +00:00
|
|
|
m_global_cursor_tracking_widget->dispatch_event(*local_event, this);
|
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-09-20 18:37:31 +00:00
|
|
|
m_automatic_cursor_tracking_widget->dispatch_event(*local_event, this);
|
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())
|
2019-09-20 18:37:31 +00:00
|
|
|
return result.widget->dispatch_event(*local_event, this);
|
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;
|
2019-12-09 19:08:44 +00:00
|
|
|
if (!m_back_bitmap) {
|
2019-03-17 03:23:54 +00:00
|
|
|
m_back_bitmap = create_backing_bitmap(paint_event.window_size());
|
2019-12-09 19:08:44 +00:00
|
|
|
} else if (m_double_buffering_enabled) {
|
|
|
|
bool still_has_pixels = m_back_bitmap->shared_buffer()->set_nonvolatile();
|
|
|
|
if (!still_has_pixels) {
|
|
|
|
m_back_bitmap = create_backing_bitmap(paint_event.window_size());
|
|
|
|
created_new_backing_store = true;
|
|
|
|
}
|
|
|
|
}
|
2019-03-17 03:23:54 +00:00
|
|
|
|
2019-04-20 15:37:28 +00:00
|
|
|
auto rect = rects.first();
|
|
|
|
if (rect.is_empty() || created_new_backing_store) {
|
|
|
|
rects.clear();
|
2019-06-07 09:46:02 +00:00
|
|
|
rects.append({ {}, paint_event.window_size() });
|
2019-04-20 15:37:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 19:05:10 +00:00
|
|
|
for (auto& rect : rects)
|
2019-09-20 18:37:31 +00:00
|
|
|
m_main_widget->dispatch_event(*make<GPaintEvent>(rect), this);
|
2019-03-17 03:23:54 +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-12-02 08:33:37 +00:00
|
|
|
Vector<Rect> rects_to_send;
|
|
|
|
for (auto& r : rects)
|
|
|
|
rects_to_send.append(r);
|
|
|
|
GWindowServerConnection::the().post_message(WindowServer::DidFinishPainting(m_window_id, rects_to_send));
|
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-12-26 22:29:18 +00:00
|
|
|
if (m_focused_widget)
|
|
|
|
return m_focused_widget->dispatch_event(event, this);
|
|
|
|
if (m_main_widget)
|
|
|
|
return m_main_widget->dispatch_event(event, this);
|
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)
|
2019-09-20 18:37:31 +00:00
|
|
|
m_main_widget->dispatch_event(event, this);
|
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) {
|
2019-08-27 18:35:37 +00:00
|
|
|
if (on_close_request) {
|
|
|
|
if (on_close_request() == GWindow::CloseRequestDecision::StayOpen)
|
|
|
|
return;
|
|
|
|
}
|
2019-02-05 09:31:37 +00:00
|
|
|
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();
|
2019-06-07 09:46:02 +00:00
|
|
|
m_pending_paint_event_rects.append({ {}, new_size });
|
2019-04-21 23:15:47 +00:00
|
|
|
}
|
2019-06-07 09:46:02 +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
|
|
|
}
|
|
|
|
|
|
|
|
bool GWindow::is_visible() const
|
|
|
|
{
|
2019-06-22 08:37:03 +00:00
|
|
|
return m_window_id != 0;
|
2019-01-20 03:49:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 10:34:40 +00:00
|
|
|
void GWindow::update()
|
|
|
|
{
|
|
|
|
update({ 0, 0, width(), height() });
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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()) {
|
2019-06-07 09:46:02 +00:00
|
|
|
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-12-02 08:33:37 +00:00
|
|
|
Vector<Rect> rects_to_send;
|
|
|
|
for (auto& r : rects)
|
|
|
|
rects_to_send.append(r);
|
|
|
|
GWindowServerConnection::the().post_message(WindowServer::InvalidateRect(m_window_id, rects_to_send));
|
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;
|
2019-08-18 10:17:09 +00:00
|
|
|
if (m_main_widget)
|
|
|
|
remove_child(*m_main_widget);
|
2019-01-20 04:48:43 +00:00
|
|
|
m_main_widget = widget;
|
2019-02-10 13:28:39 +00:00
|
|
|
if (m_main_widget) {
|
2019-08-18 10:17:09 +00:00
|
|
|
add_child(*widget);
|
2019-02-10 13:28:39 +00:00
|
|
|
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);
|
2019-06-07 09:46:02 +00:00
|
|
|
m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
|
2019-02-10 13:28:39 +00:00
|
|
|
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-09-22 18:50:39 +00:00
|
|
|
CEventLoop::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-09-22 18:50:39 +00:00
|
|
|
CEventLoop::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;
|
|
|
|
|
2019-12-02 08:33:37 +00:00
|
|
|
GWindowServerConnection::the().send_sync<WindowServer::SetWindowHasAlphaChannel>(m_window_id, value);
|
2019-05-03 19:07:16 +00:00
|
|
|
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;
|
2019-12-02 08:33:37 +00:00
|
|
|
GWindowServerConnection::the().send_sync<WindowServer::SetWindowOpacity>(m_window_id, opacity);
|
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-09-22 18:50:39 +00:00
|
|
|
CEventLoop::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-09-22 18:50:39 +00:00
|
|
|
CEventLoop::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)
|
|
|
|
{
|
2019-12-02 08:33:37 +00:00
|
|
|
GWindowServerConnection::the().send_sync<WindowServer::SetWindowBackingStore>(m_window_id, 32, bitmap.pitch(), bitmap.shared_buffer_id(), bitmap.has_alpha_channel(), bitmap.size(), flush_immediately);
|
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-12-09 20:33:39 +00:00
|
|
|
m_back_bitmap->shared_buffer()->set_volatile();
|
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-12-09 19:08:44 +00:00
|
|
|
|
|
|
|
m_back_bitmap->shared_buffer()->set_volatile();
|
2019-03-17 03:23:54 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 08:18:49 +00:00
|
|
|
NonnullRefPtr<GraphicsBitmap> GWindow::create_shared_bitmap(GraphicsBitmap::Format format, const Size& size)
|
2019-03-17 03:23:54 +00:00
|
|
|
{
|
2019-07-17 18:57:27 +00:00
|
|
|
ASSERT(GWindowServerConnection::the().server_pid());
|
2019-03-17 03:23:54 +00:00
|
|
|
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-07-18 07:52:22 +00:00
|
|
|
auto shared_buffer = SharedBuffer::create_with_size(size_in_bytes);
|
2019-03-17 03:23:54 +00:00
|
|
|
ASSERT(shared_buffer);
|
2019-07-18 07:52:22 +00:00
|
|
|
shared_buffer->share_with(GWindowServerConnection::the().server_pid());
|
2019-03-17 03:23:54 +00:00
|
|
|
return GraphicsBitmap::create_with_shared_buffer(format, *shared_buffer, size);
|
|
|
|
}
|
2019-03-18 23:01:02 +00:00
|
|
|
|
2019-07-28 08:18:49 +00:00
|
|
|
NonnullRefPtr<GraphicsBitmap> GWindow::create_backing_bitmap(const Size& size)
|
|
|
|
{
|
|
|
|
auto format = m_has_alpha_channel ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32;
|
|
|
|
return create_shared_bitmap(format, 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
|
|
|
|
2019-07-28 08:18:49 +00:00
|
|
|
void GWindow::set_icon(const GraphicsBitmap* icon)
|
|
|
|
{
|
|
|
|
if (m_icon == icon)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_icon = create_shared_bitmap(GraphicsBitmap::Format::RGBA32, icon->size());
|
|
|
|
{
|
|
|
|
GPainter painter(*m_icon);
|
|
|
|
painter.blit({ 0, 0 }, *icon, icon->rect());
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:02:56 +00:00
|
|
|
apply_icon();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWindow::apply_icon()
|
|
|
|
{
|
|
|
|
if (!m_icon)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!m_window_id)
|
|
|
|
return;
|
|
|
|
|
2019-07-29 05:26:01 +00:00
|
|
|
int rc = seal_shared_buffer(m_icon->shared_buffer_id());
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
|
|
|
|
rc = share_buffer_globally(m_icon->shared_buffer_id());
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
|
|
|
|
static bool has_set_process_icon;
|
|
|
|
if (!has_set_process_icon)
|
|
|
|
set_process_icon(m_icon->shared_buffer_id());
|
|
|
|
|
2019-12-02 08:33:37 +00:00
|
|
|
GWindowServerConnection::the().send_sync<WindowServer::SetWindowIconBitmap>(m_window_id, m_icon->shared_buffer_id(), m_icon->size());
|
2019-07-28 08:18:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 23:38:24 +00:00
|
|
|
void GWindow::start_wm_resize()
|
|
|
|
{
|
2019-12-02 08:33:37 +00:00
|
|
|
GWindowServerConnection::the().post_message(WindowServer::WM_StartWindowResize(GWindowServerConnection::the().my_client_id(), m_window_id));
|
2019-05-02 23:38:24 +00:00
|
|
|
}
|
2019-05-15 00:39:58 +00:00
|
|
|
|
|
|
|
Vector<GWidget*> GWindow::focusable_widgets() const
|
|
|
|
{
|
|
|
|
if (!m_main_widget)
|
2019-06-07 09:46:02 +00:00
|
|
|
return {};
|
2019-05-15 00:39:58 +00:00
|
|
|
|
|
|
|
Vector<GWidget*> collected_widgets;
|
|
|
|
|
2019-06-07 09:46:02 +00:00
|
|
|
Function<void(GWidget&)> collect_focusable_widgets = [&](GWidget& widget) {
|
2019-05-15 00:39:58 +00:00
|
|
|
if (widget.accepts_focus())
|
|
|
|
collected_widgets.append(&widget);
|
2019-06-07 09:46:02 +00:00
|
|
|
widget.for_each_child_widget([&](auto& child) {
|
2019-05-27 01:52:33 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-09-21 15:05:35 +00:00
|
|
|
collect_focusable_widgets(const_cast<GWidget&>(*m_main_widget));
|
2019-05-15 00:39:58 +00:00
|
|
|
return collected_widgets;
|
|
|
|
}
|
2019-08-18 18:39:46 +00:00
|
|
|
|
|
|
|
void GWindow::save_to(AK::JsonObject& json)
|
|
|
|
{
|
|
|
|
json.set("title", title());
|
|
|
|
json.set("visible", is_visible());
|
|
|
|
json.set("active", is_active());
|
2020-01-04 11:12:02 +00:00
|
|
|
json.set("minimizable", is_minimizable());
|
2019-08-18 18:39:46 +00:00
|
|
|
json.set("resizable", is_resizable());
|
|
|
|
json.set("fullscreen", is_fullscreen());
|
|
|
|
json.set("rect", rect().to_string());
|
|
|
|
json.set("base_size", base_size().to_string());
|
|
|
|
json.set("size_increment", size_increment().to_string());
|
|
|
|
CObject::save_to(json);
|
|
|
|
}
|
2019-09-16 16:38:42 +00:00
|
|
|
|
|
|
|
void GWindow::set_fullscreen(bool fullscreen)
|
|
|
|
{
|
|
|
|
if (m_fullscreen == fullscreen)
|
|
|
|
return;
|
|
|
|
m_fullscreen = fullscreen;
|
|
|
|
if (!m_window_id)
|
|
|
|
return;
|
2019-12-02 08:33:37 +00:00
|
|
|
GWindowServerConnection::the().send_sync<WindowServer::SetFullscreen>(m_window_id, fullscreen);
|
2019-09-16 16:38:42 +00:00
|
|
|
}
|
2019-10-26 10:27:01 +00:00
|
|
|
|
|
|
|
void GWindow::schedule_relayout()
|
|
|
|
{
|
|
|
|
if (m_layout_pending)
|
|
|
|
return;
|
|
|
|
m_layout_pending = true;
|
|
|
|
deferred_invoke([this](auto&) {
|
|
|
|
if (main_widget())
|
|
|
|
main_widget()->do_layout();
|
|
|
|
update();
|
|
|
|
m_layout_pending = false;
|
|
|
|
});
|
|
|
|
}
|
2019-12-23 19:24:26 +00:00
|
|
|
|
|
|
|
void GWindow::update_all_windows(Badge<GWindowServerConnection>)
|
|
|
|
{
|
2019-12-26 21:13:50 +00:00
|
|
|
for (auto* window : *all_windows) {
|
2019-12-23 19:24:26 +00:00
|
|
|
window->update();
|
|
|
|
}
|
|
|
|
}
|
2019-12-26 11:06:07 +00:00
|
|
|
|
2019-12-27 10:34:40 +00:00
|
|
|
void GWindow::notify_state_changed(Badge<GWindowServerConnection>, bool minimized, bool occluded)
|
2019-12-26 11:06:07 +00:00
|
|
|
{
|
2019-12-29 14:58:07 +00:00
|
|
|
m_visible_for_timer_purposes = !minimized && !occluded;
|
|
|
|
|
2019-12-27 10:34:40 +00:00
|
|
|
// When double buffering is enabled, minimization/occlusion means we can mark the front bitmap volatile (in addition to the back bitmap.)
|
2019-12-26 11:06:07 +00:00
|
|
|
// When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
|
|
|
|
RefPtr<GraphicsBitmap>& bitmap = m_double_buffering_enabled ? m_front_bitmap : m_back_bitmap;
|
|
|
|
if (!bitmap)
|
|
|
|
return;
|
2019-12-27 10:34:40 +00:00
|
|
|
if (minimized || occluded) {
|
2019-12-26 11:06:07 +00:00
|
|
|
bitmap->shared_buffer()->set_volatile();
|
|
|
|
} else {
|
2019-12-27 10:34:40 +00:00
|
|
|
if (!bitmap->shared_buffer()->set_nonvolatile()) {
|
2019-12-26 11:06:07 +00:00
|
|
|
bitmap = nullptr;
|
2019-12-27 10:34:40 +00:00
|
|
|
update();
|
|
|
|
}
|
2019-12-26 11:06:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 00:57:57 +00:00
|
|
|
|
|
|
|
GAction* GWindow::action_for_key_event(const GKeyEvent& event)
|
|
|
|
{
|
|
|
|
GShortcut shortcut(event.modifiers(), (KeyCode)event.key());
|
|
|
|
GAction* found_action = nullptr;
|
|
|
|
for_each_child_of_type<GAction>([&](auto& action) {
|
|
|
|
if (action.shortcut() == shortcut) {
|
|
|
|
found_action = &action;
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
return found_action;
|
|
|
|
}
|