GWindow.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. const auto &point = Point();
  260. auto event = make<GMouseEvent>(GEvent::MouseDown, point, 0, GMouseButton::Left, 0, 0);
  261. found_widget->value->event(*event);
  262. event = make<GMouseEvent>(GEvent::MouseUp, point, 0, GMouseButton::Left, 0, 0);
  263. found_widget->value->event(*event);
  264. } else if (m_entered_keybind.length() >= m_max_keybind_length) {
  265. m_keybind_mode = false;
  266. }
  267. update();
  268. }
  269. } else {
  270. if (m_focused_widget)
  271. return m_focused_widget->event(event);
  272. if (m_main_widget)
  273. return m_main_widget->event(event);
  274. }
  275. return;
  276. }
  277. if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) {
  278. if (event.type() == GEvent::WindowBecameInactive && m_keybind_mode) {
  279. m_keybind_mode = false;
  280. update();
  281. }
  282. m_is_active = event.type() == GEvent::WindowBecameActive;
  283. if (m_main_widget)
  284. m_main_widget->event(event);
  285. if (m_focused_widget)
  286. m_focused_widget->update();
  287. return;
  288. }
  289. if (event.type() == GEvent::WindowCloseRequest) {
  290. close();
  291. return;
  292. }
  293. if (event.type() == GEvent::WindowLeft) {
  294. set_hovered_widget(nullptr);
  295. return;
  296. }
  297. if (event.type() == GEvent::Resize) {
  298. auto new_size = static_cast<GResizeEvent&>(event).size();
  299. if (m_back_bitmap && m_back_bitmap->size() != new_size)
  300. m_back_bitmap = nullptr;
  301. if (!m_pending_paint_event_rects.is_empty()) {
  302. m_pending_paint_event_rects.clear_with_capacity();
  303. m_pending_paint_event_rects.append({ {}, new_size });
  304. }
  305. m_rect_when_windowless = { {}, new_size };
  306. m_main_widget->set_relative_rect({ {}, new_size });
  307. return;
  308. }
  309. if (event.type() > GEvent::__Begin_WM_Events && event.type() < GEvent::__End_WM_Events)
  310. return wm_event(static_cast<GWMEvent&>(event));
  311. CObject::event(event);
  312. }
  313. void GWindow::paint_keybinds() {
  314. if (!m_keybind_mode) return;
  315. GPainter painter(*m_main_widget);
  316. for (auto& keypair: m_hashed_potential_keybind_widgets) {
  317. auto widget = keypair.value;
  318. bool could_be_keybind = true;
  319. for (size_t i = 0; i < m_entered_keybind.length(); i++) {
  320. if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) {
  321. could_be_keybind = false;
  322. }
  323. }
  324. if (could_be_keybind) {
  325. auto rect = Rect(widget->x()-5, widget->y()-5, 4+Font::default_font().width(keypair.key), 16);
  326. auto highlight_rect = Rect(widget->x()-3, widget->y()-5, 0, 16);
  327. painter.fill_rect(rect, Color::LightGray);
  328. painter.draw_rect(rect, Color::Black, false);
  329. painter.draw_text(rect, keypair.key.characters(), TextAlignment::Center, Color::Black);
  330. painter.draw_text(highlight_rect, m_entered_keybind.characters(), TextAlignment::CenterLeft, Color::MidGray);
  331. }
  332. }
  333. }
  334. void GWindow::find_keyboard_selectable() {
  335. Vector<GWidget*> potential_keybind_widgets;
  336. m_hashed_potential_keybind_widgets.clear();
  337. find_keyboard_selectable_children(m_main_widget, potential_keybind_widgets);
  338. m_max_keybind_length = ceil_div(potential_keybind_widgets.size(), ('z'-'a'));
  339. size_t buffer_length = m_max_keybind_length + 1;
  340. char keybind_buffer[buffer_length];
  341. for (size_t i = 0; i < buffer_length-1; i++) {
  342. keybind_buffer[i] = 'a';
  343. }
  344. keybind_buffer[buffer_length-1] = '\0';
  345. for (auto& widget: potential_keybind_widgets) {
  346. m_hashed_potential_keybind_widgets.set(String(keybind_buffer), widget);
  347. for (size_t i = 0; i < buffer_length-1; i++) {
  348. if (keybind_buffer[i] >= 'z') {
  349. keybind_buffer[i] = 'a';
  350. } else {
  351. keybind_buffer[i]++;
  352. break;
  353. }
  354. }
  355. }
  356. }
  357. void GWindow::find_keyboard_selectable_children(GWidget* widget, Vector<GWidget*> &potential_keybind_widgets) {
  358. widget -> for_each_child_widget([&] (auto& child) {
  359. if (child.accepts_keyboard_select()) {
  360. potential_keybind_widgets.append(&child);
  361. find_keyboard_selectable_children(&child, potential_keybind_widgets);
  362. }
  363. return IterationDecision::Continue;
  364. });
  365. }
  366. bool GWindow::is_visible() const
  367. {
  368. return false;
  369. }
  370. void GWindow::update(const Rect& a_rect)
  371. {
  372. if (!m_window_id)
  373. return;
  374. for (auto& pending_rect : m_pending_paint_event_rects) {
  375. if (pending_rect.contains(a_rect)) {
  376. #ifdef UPDATE_COALESCING_DEBUG
  377. dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
  378. #endif
  379. return;
  380. }
  381. }
  382. if (m_pending_paint_event_rects.is_empty()) {
  383. deferred_invoke([this](auto&) {
  384. auto rects = move(m_pending_paint_event_rects);
  385. if (rects.is_empty())
  386. return;
  387. WSAPI_ClientMessage request;
  388. request.type = WSAPI_ClientMessage::Type::InvalidateRect;
  389. request.window_id = m_window_id;
  390. for (int i = 0; i < min(WSAPI_ClientMessage::max_inline_rect_count, rects.size()); ++i)
  391. request.rects[i] = rects[i];
  392. ByteBuffer extra_data;
  393. if (rects.size() > WSAPI_ClientMessage::max_inline_rect_count)
  394. extra_data = ByteBuffer::wrap(&rects[WSAPI_ClientMessage::max_inline_rect_count], (rects.size() - WSAPI_ClientMessage::max_inline_rect_count) * sizeof(WSAPI_Rect));
  395. request.rect_count = rects.size();
  396. GEventLoop::current().post_message_to_server(request, extra_data);
  397. });
  398. }
  399. m_pending_paint_event_rects.append(a_rect);
  400. }
  401. void GWindow::set_main_widget(GWidget* widget)
  402. {
  403. if (m_main_widget == widget)
  404. return;
  405. m_main_widget = widget;
  406. if (m_main_widget) {
  407. auto new_window_rect = rect();
  408. if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
  409. new_window_rect.set_width(m_main_widget->preferred_size().width());
  410. if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
  411. new_window_rect.set_height(m_main_widget->preferred_size().height());
  412. set_rect(new_window_rect);
  413. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  414. m_main_widget->set_window(this);
  415. if (m_main_widget->accepts_focus())
  416. m_main_widget->set_focus(true);
  417. }
  418. update();
  419. }
  420. void GWindow::set_focused_widget(GWidget* widget)
  421. {
  422. if (m_focused_widget == widget)
  423. return;
  424. if (m_focused_widget) {
  425. GEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusOut));
  426. m_focused_widget->update();
  427. }
  428. m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
  429. if (m_focused_widget) {
  430. GEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusIn));
  431. m_focused_widget->update();
  432. }
  433. }
  434. void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
  435. {
  436. if (widget == m_global_cursor_tracking_widget)
  437. return;
  438. m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  439. }
  440. void GWindow::set_automatic_cursor_tracking_widget(GWidget* widget)
  441. {
  442. if (widget == m_automatic_cursor_tracking_widget)
  443. return;
  444. m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  445. }
  446. void GWindow::set_has_alpha_channel(bool value)
  447. {
  448. if (m_has_alpha_channel == value)
  449. return;
  450. m_has_alpha_channel = value;
  451. if (!m_window_id)
  452. return;
  453. m_pending_paint_event_rects.clear();
  454. m_back_bitmap = nullptr;
  455. m_front_bitmap = nullptr;
  456. WSAPI_ClientMessage message;
  457. message.type = WSAPI_ClientMessage::Type::SetWindowHasAlphaChannel;
  458. message.window_id = m_window_id;
  459. message.value = value;
  460. GEventLoop::current().sync_request(message, WSAPI_ServerMessage::DidSetWindowHasAlphaChannel);
  461. update();
  462. }
  463. void GWindow::set_double_buffering_enabled(bool value)
  464. {
  465. ASSERT(!m_window_id);
  466. m_double_buffering_enabled = value;
  467. }
  468. void GWindow::set_opacity(float opacity)
  469. {
  470. m_opacity_when_windowless = opacity;
  471. if (!m_window_id)
  472. return;
  473. WSAPI_ClientMessage request;
  474. request.type = WSAPI_ClientMessage::Type::SetWindowOpacity;
  475. request.window_id = m_window_id;
  476. request.window.opacity = opacity;
  477. m_opacity_when_windowless = opacity;
  478. GEventLoop::current().post_message_to_server(request);
  479. }
  480. void GWindow::set_hovered_widget(GWidget* widget)
  481. {
  482. if (widget == m_hovered_widget)
  483. return;
  484. if (m_hovered_widget)
  485. GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Leave));
  486. m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
  487. if (m_hovered_widget)
  488. GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Enter));
  489. }
  490. void GWindow::set_current_backing_bitmap(GraphicsBitmap& bitmap, bool flush_immediately)
  491. {
  492. WSAPI_ClientMessage message;
  493. message.type = WSAPI_ClientMessage::Type::SetWindowBackingStore;
  494. message.window_id = m_window_id;
  495. message.backing.bpp = 32;
  496. message.backing.pitch = bitmap.pitch();
  497. message.backing.shared_buffer_id = bitmap.shared_buffer_id();
  498. message.backing.has_alpha_channel = bitmap.has_alpha_channel();
  499. message.backing.size = bitmap.size();
  500. message.backing.flush_immediately = flush_immediately;
  501. GEventLoop::current().sync_request(message, WSAPI_ServerMessage::Type::DidSetWindowBackingStore);
  502. }
  503. void GWindow::flip(const Vector<Rect, 32>& dirty_rects)
  504. {
  505. swap(m_front_bitmap, m_back_bitmap);
  506. set_current_backing_bitmap(*m_front_bitmap);
  507. if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) {
  508. m_back_bitmap = create_backing_bitmap(m_front_bitmap->size());
  509. memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size_in_bytes());
  510. return;
  511. }
  512. // Copy whatever was painted from the front to the back.
  513. Painter painter(*m_back_bitmap);
  514. for (auto& dirty_rect : dirty_rects)
  515. painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect);
  516. }
  517. Retained<GraphicsBitmap> GWindow::create_backing_bitmap(const Size& size)
  518. {
  519. ASSERT(GEventLoop::server_pid());
  520. ASSERT(!size.is_empty());
  521. size_t pitch = round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16);
  522. size_t size_in_bytes = size.height() * pitch;
  523. auto shared_buffer = SharedBuffer::create(GEventLoop::server_pid(), size_in_bytes);
  524. ASSERT(shared_buffer);
  525. auto format = m_has_alpha_channel ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32;
  526. return GraphicsBitmap::create_with_shared_buffer(format, *shared_buffer, size);
  527. }
  528. void GWindow::set_modal(bool modal)
  529. {
  530. ASSERT(!m_window_id);
  531. m_modal = modal;
  532. }
  533. void GWindow::wm_event(GWMEvent&)
  534. {
  535. }
  536. void GWindow::set_icon_path(const StringView& path)
  537. {
  538. if (m_icon_path == path)
  539. return;
  540. m_icon_path = path;
  541. if (!m_window_id)
  542. return;
  543. WSAPI_ClientMessage message;
  544. message.type = WSAPI_ClientMessage::Type::SetWindowIcon;
  545. message.window_id = m_window_id;
  546. ASSERT(path.length() < sizeof(message.text));
  547. strcpy(message.text, path.characters());
  548. message.text_length = path.length();
  549. GEventLoop::post_message_to_server(message);
  550. }
  551. void GWindow::start_wm_resize()
  552. {
  553. WSAPI_ClientMessage message;
  554. message.type = WSAPI_ClientMessage::Type::WM_StartWindowResize;
  555. message.wm.client_id = GEventLoop::my_client_id();
  556. message.wm.window_id = m_window_id;
  557. GEventLoop::post_message_to_server(message);
  558. }
  559. Vector<GWidget*> GWindow::focusable_widgets() const
  560. {
  561. if (!m_main_widget)
  562. return {};
  563. Vector<GWidget*> collected_widgets;
  564. Function<void(GWidget&)> collect_focusable_widgets = [&](GWidget& widget) {
  565. if (widget.accepts_focus())
  566. collected_widgets.append(&widget);
  567. widget.for_each_child_widget([&](auto& child) {
  568. if (!child.is_visible())
  569. return IterationDecision::Continue;
  570. if (!child.is_enabled())
  571. return IterationDecision::Continue;
  572. collect_focusable_widgets(child);
  573. return IterationDecision::Continue;
  574. });
  575. };
  576. collect_focusable_widgets(*m_main_widget);
  577. return collected_widgets;
  578. }