GWindow.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. #include "GWindow.h"
  2. #include "GEvent.h"
  3. #include "GEventLoop.h"
  4. #include "GWidget.h"
  5. #include <AK/HashMap.h>
  6. #include <AK/StringBuilder.h>
  7. #include <LibC/stdio.h>
  8. #include <LibC/stdlib.h>
  9. #include <LibC/unistd.h>
  10. #include <LibGUI/GPainter.h>
  11. #include <SharedGraphics/GraphicsBitmap.h>
  12. //#define UPDATE_COALESCING_DEBUG
  13. static HashMap<int, GWindow*>* s_windows;
  14. static HashMap<int, GWindow*>& windows()
  15. {
  16. if (!s_windows)
  17. s_windows = new HashMap<int, GWindow*>;
  18. return *s_windows;
  19. }
  20. GWindow* GWindow::from_window_id(int window_id)
  21. {
  22. auto it = windows().find(window_id);
  23. if (it != windows().end())
  24. return (*it).value;
  25. return nullptr;
  26. }
  27. GWindow::GWindow(CObject* parent)
  28. : CObject(parent)
  29. {
  30. m_rect_when_windowless = { 100, 400, 140, 140 };
  31. m_title_when_windowless = "GWindow";
  32. }
  33. GWindow::~GWindow()
  34. {
  35. if (m_main_widget)
  36. delete m_main_widget;
  37. hide();
  38. }
  39. void GWindow::close()
  40. {
  41. if (should_exit_event_loop_on_close())
  42. GEventLoop::current().quit(0);
  43. delete_later();
  44. }
  45. void GWindow::move_to_front()
  46. {
  47. if (!m_window_id)
  48. return;
  49. WSAPI_ClientMessage request;
  50. request.type = WSAPI_ClientMessage::Type::MoveWindowToFront;
  51. request.window_id = m_window_id;
  52. GEventLoop::post_message_to_server(request);
  53. }
  54. void GWindow::show()
  55. {
  56. if (m_window_id)
  57. return;
  58. WSAPI_ClientMessage request;
  59. request.type = WSAPI_ClientMessage::Type::CreateWindow;
  60. request.window_id = m_window_id;
  61. request.window.rect = m_rect_when_windowless;
  62. request.window.has_alpha_channel = m_has_alpha_channel;
  63. request.window.modal = m_modal;
  64. request.window.resizable = m_resizable;
  65. request.window.fullscreen = m_fullscreen;
  66. request.window.show_titlebar = m_show_titlebar;
  67. request.window.opacity = m_opacity_when_windowless;
  68. request.window.background_color = m_background_color.value();
  69. request.window.size_increment = m_size_increment;
  70. request.window.base_size = m_base_size;
  71. request.window.type = (WSAPI_WindowType)m_window_type;
  72. ASSERT(m_title_when_windowless.length() < (ssize_t)sizeof(request.text));
  73. strcpy(request.text, m_title_when_windowless.characters());
  74. request.text_length = m_title_when_windowless.length();
  75. auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidCreateWindow);
  76. m_window_id = response.window_id;
  77. windows().set(m_window_id, this);
  78. update();
  79. }
  80. void GWindow::hide()
  81. {
  82. if (!m_window_id)
  83. return;
  84. windows().remove(m_window_id);
  85. WSAPI_ClientMessage request;
  86. request.type = WSAPI_ClientMessage::Type::DestroyWindow;
  87. request.window_id = m_window_id;
  88. GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidDestroyWindow);
  89. m_window_id = 0;
  90. m_pending_paint_event_rects.clear();
  91. m_back_bitmap = nullptr;
  92. m_front_bitmap = nullptr;
  93. }
  94. void GWindow::set_title(const StringView& title)
  95. {
  96. m_title_when_windowless = title;
  97. if (!m_window_id)
  98. return;
  99. WSAPI_ClientMessage request;
  100. request.type = WSAPI_ClientMessage::Type::SetWindowTitle;
  101. request.window_id = m_window_id;
  102. ASSERT(m_title_when_windowless.length() < (ssize_t)sizeof(request.text));
  103. strcpy(request.text, m_title_when_windowless.characters());
  104. request.text_length = m_title_when_windowless.length();
  105. GEventLoop::current().post_message_to_server(request);
  106. }
  107. String GWindow::title() const
  108. {
  109. if (!m_window_id)
  110. return m_title_when_windowless;
  111. WSAPI_ClientMessage request;
  112. request.type = WSAPI_ClientMessage::Type::GetWindowTitle;
  113. request.window_id = m_window_id;
  114. auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidGetWindowTitle);
  115. return String(response.text, response.text_length);
  116. }
  117. Rect GWindow::rect() const
  118. {
  119. if (!m_window_id)
  120. return m_rect_when_windowless;
  121. WSAPI_ClientMessage request;
  122. request.type = WSAPI_ClientMessage::Type::GetWindowRect;
  123. request.window_id = m_window_id;
  124. auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidGetWindowRect);
  125. ASSERT(response.window_id == m_window_id);
  126. return response.window.rect;
  127. }
  128. void GWindow::set_rect(const Rect& a_rect)
  129. {
  130. m_rect_when_windowless = a_rect;
  131. if (!m_window_id) {
  132. if (m_main_widget)
  133. m_main_widget->resize(m_rect_when_windowless.size());
  134. return;
  135. }
  136. WSAPI_ClientMessage request;
  137. request.type = WSAPI_ClientMessage::Type::SetWindowRect;
  138. request.window_id = m_window_id;
  139. request.window.rect = a_rect;
  140. GEventLoop::current().post_message_to_server(request);
  141. if (m_back_bitmap && m_back_bitmap->size() != a_rect.size())
  142. m_back_bitmap = nullptr;
  143. if (m_front_bitmap && m_front_bitmap->size() != a_rect.size())
  144. m_front_bitmap = nullptr;
  145. if (m_main_widget)
  146. m_main_widget->resize(a_rect.size());
  147. }
  148. void GWindow::set_window_type(GWindowType window_type)
  149. {
  150. m_window_type = window_type;
  151. }
  152. void GWindow::set_override_cursor(GStandardCursor cursor)
  153. {
  154. if (!m_window_id)
  155. return;
  156. WSAPI_ClientMessage request;
  157. request.type = WSAPI_ClientMessage::Type::SetWindowOverrideCursor;
  158. request.window_id = m_window_id;
  159. request.cursor.cursor = (WSAPI_StandardCursor)cursor;
  160. GEventLoop::current().post_message_to_server(request);
  161. }
  162. void GWindow::event(CEvent& event)
  163. {
  164. if (event.type() == GEvent::MouseUp || event.type() == GEvent::MouseDown || event.type() == GEvent::MouseDoubleClick || event.type() == GEvent::MouseMove || event.type() == GEvent::MouseWheel) {
  165. auto& mouse_event = static_cast<GMouseEvent&>(event);
  166. if (m_global_cursor_tracking_widget) {
  167. auto window_relative_rect = m_global_cursor_tracking_widget->window_relative_rect();
  168. Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  169. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  170. m_global_cursor_tracking_widget->event(*local_event);
  171. return;
  172. }
  173. if (m_automatic_cursor_tracking_widget) {
  174. auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect();
  175. Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  176. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  177. m_automatic_cursor_tracking_widget->event(*local_event);
  178. if (mouse_event.buttons() == 0)
  179. m_automatic_cursor_tracking_widget = nullptr;
  180. return;
  181. }
  182. if (!m_main_widget)
  183. return;
  184. auto result = m_main_widget->hit_test(mouse_event.position());
  185. 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());
  186. ASSERT(result.widget);
  187. set_hovered_widget(result.widget);
  188. if (mouse_event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
  189. m_automatic_cursor_tracking_widget = result.widget->make_weak_ptr();
  190. if (result.widget != m_global_cursor_tracking_widget.ptr())
  191. return result.widget->event(*local_event);
  192. return;
  193. }
  194. if (event.type() == GEvent::MultiPaint) {
  195. if (!m_window_id)
  196. return;
  197. if (!m_main_widget)
  198. return;
  199. auto& paint_event = static_cast<GMultiPaintEvent&>(event);
  200. auto rects = paint_event.rects();
  201. ASSERT(!rects.is_empty());
  202. if (m_back_bitmap && m_back_bitmap->size() != paint_event.window_size()) {
  203. // Eagerly discard the backing store if we learn from this paint event that it needs to be bigger.
  204. // Otherwise we would have to wait for a resize event to tell us. This way we don't waste the
  205. // effort on painting into an undersized bitmap that will be thrown away anyway.
  206. m_back_bitmap = nullptr;
  207. }
  208. bool created_new_backing_store = !m_back_bitmap;
  209. if (!m_back_bitmap)
  210. m_back_bitmap = create_backing_bitmap(paint_event.window_size());
  211. auto rect = rects.first();
  212. if (rect.is_empty() || created_new_backing_store) {
  213. rects.clear();
  214. rects.append({ {}, paint_event.window_size() });
  215. }
  216. for (auto& rect : rects)
  217. m_main_widget->event(*make<GPaintEvent>(rect));
  218. paint_keybinds();
  219. if (m_double_buffering_enabled)
  220. flip(rects);
  221. else if (created_new_backing_store)
  222. set_current_backing_bitmap(*m_back_bitmap, true);
  223. if (m_window_id) {
  224. WSAPI_ClientMessage message;
  225. message.type = WSAPI_ClientMessage::Type::DidFinishPainting;
  226. message.window_id = m_window_id;
  227. message.rect_count = rects.size();
  228. for (int i = 0; i < min(WSAPI_ClientMessage::max_inline_rect_count, rects.size()); ++i)
  229. message.rects[i] = rects[i];
  230. ByteBuffer extra_data;
  231. if (rects.size() > WSAPI_ClientMessage::max_inline_rect_count)
  232. extra_data = ByteBuffer::wrap(&rects[WSAPI_ClientMessage::max_inline_rect_count], (rects.size() - WSAPI_ClientMessage::max_inline_rect_count) * sizeof(WSAPI_Rect));
  233. GEventLoop::current().post_message_to_server(message, extra_data);
  234. }
  235. return;
  236. }
  237. if (event.type() == GEvent::KeyUp || event.type() == GEvent::KeyDown) {
  238. auto keyevent = static_cast<GKeyEvent&>(event);
  239. if (keyevent.logo() && event.type() == GEvent::KeyUp) {
  240. if (m_keybind_mode) {
  241. m_keybind_mode = false;
  242. } else {
  243. m_keybind_mode = true;
  244. find_keyboard_selectable();
  245. m_entered_keybind = "";
  246. }
  247. update();
  248. return;
  249. }
  250. if (m_keybind_mode) {
  251. if (event.type() == GEvent::KeyUp) {
  252. StringBuilder builder;
  253. builder.append(m_entered_keybind);
  254. builder.append(keyevent.text());
  255. m_entered_keybind = builder.to_string();
  256. auto found_widget = m_hashed_potential_keybind_widgets.find(m_entered_keybind);
  257. if (found_widget != m_hashed_potential_keybind_widgets.end()) {
  258. m_keybind_mode = false;
  259. auto event = make<GMouseEvent>(GEvent::MouseDown, Point(), 0, GMouseButton::Left, 0, 0);
  260. found_widget->value->event(*event);
  261. event = make<GMouseEvent>(GEvent::MouseUp, Point(), 0, GMouseButton::Left, 0, 0);
  262. found_widget->value->event(*event);
  263. } else if (m_entered_keybind.length() >= m_max_keybind_length) {
  264. m_keybind_mode = false;
  265. }
  266. update();
  267. }
  268. } else {
  269. if (m_focused_widget)
  270. return m_focused_widget->event(event);
  271. if (m_main_widget)
  272. return m_main_widget->event(event);
  273. }
  274. return;
  275. }
  276. if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) {
  277. if (event.type() == GEvent::WindowBecameInactive && m_keybind_mode) {
  278. m_keybind_mode = false;
  279. update();
  280. }
  281. m_is_active = event.type() == GEvent::WindowBecameActive;
  282. if (m_main_widget)
  283. m_main_widget->event(event);
  284. if (m_focused_widget)
  285. m_focused_widget->update();
  286. return;
  287. }
  288. if (event.type() == GEvent::WindowCloseRequest) {
  289. close();
  290. return;
  291. }
  292. if (event.type() == GEvent::WindowLeft) {
  293. set_hovered_widget(nullptr);
  294. return;
  295. }
  296. if (event.type() == GEvent::Resize) {
  297. auto new_size = static_cast<GResizeEvent&>(event).size();
  298. if (m_back_bitmap && m_back_bitmap->size() != new_size)
  299. m_back_bitmap = nullptr;
  300. if (!m_pending_paint_event_rects.is_empty()) {
  301. m_pending_paint_event_rects.clear_with_capacity();
  302. m_pending_paint_event_rects.append({ {}, new_size });
  303. }
  304. m_rect_when_windowless = { {}, new_size };
  305. m_main_widget->set_relative_rect({ {}, new_size });
  306. return;
  307. }
  308. if (event.type() > GEvent::__Begin_WM_Events && event.type() < GEvent::__End_WM_Events)
  309. return wm_event(static_cast<GWMEvent&>(event));
  310. CObject::event(event);
  311. }
  312. void GWindow::paint_keybinds()
  313. {
  314. if (!m_keybind_mode)
  315. return;
  316. GPainter painter(*m_main_widget);
  317. for (auto& keypair : m_hashed_potential_keybind_widgets) {
  318. auto widget = keypair.value;
  319. bool could_be_keybind = true;
  320. for (size_t i = 0; i < m_entered_keybind.length(); i++) {
  321. if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) {
  322. could_be_keybind = false;
  323. }
  324. }
  325. if (could_be_keybind) {
  326. auto rect = Rect(widget->x() - 5, widget->y() - 5, 4 + Font::default_font().width(keypair.key), 16);
  327. auto highlight_rect = Rect(widget->x() - 3, widget->y() - 5, 0, 16);
  328. painter.fill_rect(rect, Color::LightGray);
  329. painter.draw_rect(rect, Color::Black, false);
  330. painter.draw_text(rect, keypair.key.characters(), TextAlignment::Center, Color::Black);
  331. painter.draw_text(highlight_rect, m_entered_keybind.characters(), TextAlignment::CenterLeft, Color::MidGray);
  332. }
  333. }
  334. }
  335. void GWindow::find_keyboard_selectable()
  336. {
  337. Vector<GWidget*> potential_keybind_widgets;
  338. m_hashed_potential_keybind_widgets.clear();
  339. find_keyboard_selectable_children(m_main_widget, potential_keybind_widgets);
  340. m_max_keybind_length = ceil_div(potential_keybind_widgets.size(), ('z' - 'a'));
  341. size_t buffer_length = m_max_keybind_length + 1;
  342. char keybind_buffer[buffer_length];
  343. for (size_t i = 0; i < buffer_length - 1; i++) {
  344. keybind_buffer[i] = 'a';
  345. }
  346. keybind_buffer[buffer_length - 1] = '\0';
  347. for (auto& widget : potential_keybind_widgets) {
  348. m_hashed_potential_keybind_widgets.set(String(keybind_buffer), widget);
  349. for (size_t i = 0; i < buffer_length - 1; i++) {
  350. if (keybind_buffer[i] >= 'z') {
  351. keybind_buffer[i] = 'a';
  352. } else {
  353. keybind_buffer[i]++;
  354. break;
  355. }
  356. }
  357. }
  358. }
  359. void GWindow::find_keyboard_selectable_children(GWidget* widget, Vector<GWidget*>& potential_keybind_widgets)
  360. {
  361. widget->for_each_child_widget([&](auto& child) {
  362. if (child.accepts_keyboard_select()) {
  363. potential_keybind_widgets.append(&child);
  364. find_keyboard_selectable_children(&child, potential_keybind_widgets);
  365. }
  366. return IterationDecision::Continue;
  367. });
  368. }
  369. bool GWindow::is_visible() const
  370. {
  371. return false;
  372. }
  373. void GWindow::update(const Rect& a_rect)
  374. {
  375. if (!m_window_id)
  376. return;
  377. for (auto& pending_rect : m_pending_paint_event_rects) {
  378. if (pending_rect.contains(a_rect)) {
  379. #ifdef UPDATE_COALESCING_DEBUG
  380. dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
  381. #endif
  382. return;
  383. }
  384. }
  385. if (m_pending_paint_event_rects.is_empty()) {
  386. deferred_invoke([this](auto&) {
  387. auto rects = move(m_pending_paint_event_rects);
  388. if (rects.is_empty())
  389. return;
  390. WSAPI_ClientMessage request;
  391. request.type = WSAPI_ClientMessage::Type::InvalidateRect;
  392. request.window_id = m_window_id;
  393. for (int i = 0; i < min(WSAPI_ClientMessage::max_inline_rect_count, rects.size()); ++i)
  394. request.rects[i] = rects[i];
  395. ByteBuffer extra_data;
  396. if (rects.size() > WSAPI_ClientMessage::max_inline_rect_count)
  397. extra_data = ByteBuffer::wrap(&rects[WSAPI_ClientMessage::max_inline_rect_count], (rects.size() - WSAPI_ClientMessage::max_inline_rect_count) * sizeof(WSAPI_Rect));
  398. request.rect_count = rects.size();
  399. GEventLoop::current().post_message_to_server(request, extra_data);
  400. });
  401. }
  402. m_pending_paint_event_rects.append(a_rect);
  403. }
  404. void GWindow::set_main_widget(GWidget* widget)
  405. {
  406. if (m_main_widget == widget)
  407. return;
  408. m_main_widget = widget;
  409. if (m_main_widget) {
  410. auto new_window_rect = rect();
  411. if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
  412. new_window_rect.set_width(m_main_widget->preferred_size().width());
  413. if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
  414. new_window_rect.set_height(m_main_widget->preferred_size().height());
  415. set_rect(new_window_rect);
  416. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  417. m_main_widget->set_window(this);
  418. if (m_main_widget->accepts_focus())
  419. m_main_widget->set_focus(true);
  420. }
  421. update();
  422. }
  423. void GWindow::set_focused_widget(GWidget* widget)
  424. {
  425. if (m_focused_widget == widget)
  426. return;
  427. if (m_focused_widget) {
  428. GEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusOut));
  429. m_focused_widget->update();
  430. }
  431. m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
  432. if (m_focused_widget) {
  433. GEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusIn));
  434. m_focused_widget->update();
  435. }
  436. }
  437. void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
  438. {
  439. if (widget == m_global_cursor_tracking_widget)
  440. return;
  441. m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  442. }
  443. void GWindow::set_automatic_cursor_tracking_widget(GWidget* widget)
  444. {
  445. if (widget == m_automatic_cursor_tracking_widget)
  446. return;
  447. m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  448. }
  449. void GWindow::set_has_alpha_channel(bool value)
  450. {
  451. if (m_has_alpha_channel == value)
  452. return;
  453. m_has_alpha_channel = value;
  454. if (!m_window_id)
  455. return;
  456. m_pending_paint_event_rects.clear();
  457. m_back_bitmap = nullptr;
  458. m_front_bitmap = nullptr;
  459. WSAPI_ClientMessage message;
  460. message.type = WSAPI_ClientMessage::Type::SetWindowHasAlphaChannel;
  461. message.window_id = m_window_id;
  462. message.value = value;
  463. GEventLoop::current().sync_request(message, WSAPI_ServerMessage::DidSetWindowHasAlphaChannel);
  464. update();
  465. }
  466. void GWindow::set_double_buffering_enabled(bool value)
  467. {
  468. ASSERT(!m_window_id);
  469. m_double_buffering_enabled = value;
  470. }
  471. void GWindow::set_opacity(float opacity)
  472. {
  473. m_opacity_when_windowless = opacity;
  474. if (!m_window_id)
  475. return;
  476. WSAPI_ClientMessage request;
  477. request.type = WSAPI_ClientMessage::Type::SetWindowOpacity;
  478. request.window_id = m_window_id;
  479. request.window.opacity = opacity;
  480. m_opacity_when_windowless = opacity;
  481. GEventLoop::current().post_message_to_server(request);
  482. }
  483. void GWindow::set_hovered_widget(GWidget* widget)
  484. {
  485. if (widget == m_hovered_widget)
  486. return;
  487. if (m_hovered_widget)
  488. GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Leave));
  489. m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
  490. if (m_hovered_widget)
  491. GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Enter));
  492. }
  493. void GWindow::set_current_backing_bitmap(GraphicsBitmap& bitmap, bool flush_immediately)
  494. {
  495. WSAPI_ClientMessage message;
  496. message.type = WSAPI_ClientMessage::Type::SetWindowBackingStore;
  497. message.window_id = m_window_id;
  498. message.backing.bpp = 32;
  499. message.backing.pitch = bitmap.pitch();
  500. message.backing.shared_buffer_id = bitmap.shared_buffer_id();
  501. message.backing.has_alpha_channel = bitmap.has_alpha_channel();
  502. message.backing.size = bitmap.size();
  503. message.backing.flush_immediately = flush_immediately;
  504. GEventLoop::current().sync_request(message, WSAPI_ServerMessage::Type::DidSetWindowBackingStore);
  505. }
  506. void GWindow::flip(const Vector<Rect, 32>& dirty_rects)
  507. {
  508. swap(m_front_bitmap, m_back_bitmap);
  509. set_current_backing_bitmap(*m_front_bitmap);
  510. if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) {
  511. m_back_bitmap = create_backing_bitmap(m_front_bitmap->size());
  512. memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size_in_bytes());
  513. return;
  514. }
  515. // Copy whatever was painted from the front to the back.
  516. Painter painter(*m_back_bitmap);
  517. for (auto& dirty_rect : dirty_rects)
  518. painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect);
  519. }
  520. Retained<GraphicsBitmap> GWindow::create_backing_bitmap(const Size& size)
  521. {
  522. ASSERT(GEventLoop::server_pid());
  523. ASSERT(!size.is_empty());
  524. size_t pitch = round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16);
  525. size_t size_in_bytes = size.height() * pitch;
  526. auto shared_buffer = SharedBuffer::create(GEventLoop::server_pid(), size_in_bytes);
  527. ASSERT(shared_buffer);
  528. auto format = m_has_alpha_channel ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32;
  529. return GraphicsBitmap::create_with_shared_buffer(format, *shared_buffer, size);
  530. }
  531. void GWindow::set_modal(bool modal)
  532. {
  533. ASSERT(!m_window_id);
  534. m_modal = modal;
  535. }
  536. void GWindow::wm_event(GWMEvent&)
  537. {
  538. }
  539. void GWindow::set_icon_path(const StringView& path)
  540. {
  541. if (m_icon_path == path)
  542. return;
  543. m_icon_path = path;
  544. if (!m_window_id)
  545. return;
  546. WSAPI_ClientMessage message;
  547. message.type = WSAPI_ClientMessage::Type::SetWindowIcon;
  548. message.window_id = m_window_id;
  549. ASSERT(path.length() < sizeof(message.text));
  550. strcpy(message.text, path.characters());
  551. message.text_length = path.length();
  552. GEventLoop::post_message_to_server(message);
  553. }
  554. void GWindow::start_wm_resize()
  555. {
  556. WSAPI_ClientMessage message;
  557. message.type = WSAPI_ClientMessage::Type::WM_StartWindowResize;
  558. message.wm.client_id = GEventLoop::my_client_id();
  559. message.wm.window_id = m_window_id;
  560. GEventLoop::post_message_to_server(message);
  561. }
  562. Vector<GWidget*> GWindow::focusable_widgets() const
  563. {
  564. if (!m_main_widget)
  565. return {};
  566. Vector<GWidget*> collected_widgets;
  567. Function<void(GWidget&)> collect_focusable_widgets = [&](GWidget& widget) {
  568. if (widget.accepts_focus())
  569. collected_widgets.append(&widget);
  570. widget.for_each_child_widget([&](auto& child) {
  571. if (!child.is_visible())
  572. return IterationDecision::Continue;
  573. if (!child.is_enabled())
  574. return IterationDecision::Continue;
  575. collect_focusable_widgets(child);
  576. return IterationDecision::Continue;
  577. });
  578. };
  579. collect_focusable_widgets(*m_main_widget);
  580. return collected_widgets;
  581. }