GWindow.cpp 24 KB

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