GWindow.cpp 24 KB

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