GWindow.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. 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. m_main_widget = widget;
  414. if (m_main_widget) {
  415. auto new_window_rect = rect();
  416. if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
  417. new_window_rect.set_width(m_main_widget->preferred_size().width());
  418. if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
  419. new_window_rect.set_height(m_main_widget->preferred_size().height());
  420. set_rect(new_window_rect);
  421. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  422. m_main_widget->set_window(this);
  423. if (m_main_widget->accepts_focus())
  424. m_main_widget->set_focus(true);
  425. }
  426. update();
  427. }
  428. void GWindow::set_focused_widget(GWidget* widget)
  429. {
  430. if (m_focused_widget == widget)
  431. return;
  432. if (m_focused_widget) {
  433. GEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusOut));
  434. m_focused_widget->update();
  435. }
  436. m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
  437. if (m_focused_widget) {
  438. GEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusIn));
  439. m_focused_widget->update();
  440. }
  441. }
  442. void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
  443. {
  444. if (widget == m_global_cursor_tracking_widget)
  445. return;
  446. m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  447. }
  448. void GWindow::set_automatic_cursor_tracking_widget(GWidget* widget)
  449. {
  450. if (widget == m_automatic_cursor_tracking_widget)
  451. return;
  452. m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  453. }
  454. void GWindow::set_has_alpha_channel(bool value)
  455. {
  456. if (m_has_alpha_channel == value)
  457. return;
  458. m_has_alpha_channel = value;
  459. if (!m_window_id)
  460. return;
  461. m_pending_paint_event_rects.clear();
  462. m_back_bitmap = nullptr;
  463. m_front_bitmap = nullptr;
  464. WSAPI_ClientMessage message;
  465. message.type = WSAPI_ClientMessage::Type::SetWindowHasAlphaChannel;
  466. message.window_id = m_window_id;
  467. message.value = value;
  468. GWindowServerConnection::the().sync_request(message, WSAPI_ServerMessage::DidSetWindowHasAlphaChannel);
  469. update();
  470. }
  471. void GWindow::set_double_buffering_enabled(bool value)
  472. {
  473. ASSERT(!m_window_id);
  474. m_double_buffering_enabled = value;
  475. }
  476. void GWindow::set_opacity(float opacity)
  477. {
  478. m_opacity_when_windowless = opacity;
  479. if (!m_window_id)
  480. return;
  481. WSAPI_ClientMessage request;
  482. request.type = WSAPI_ClientMessage::Type::SetWindowOpacity;
  483. request.window_id = m_window_id;
  484. request.window.opacity = opacity;
  485. m_opacity_when_windowless = opacity;
  486. GWindowServerConnection::the().post_message_to_server(request);
  487. }
  488. void GWindow::set_hovered_widget(GWidget* widget)
  489. {
  490. if (widget == m_hovered_widget)
  491. return;
  492. if (m_hovered_widget)
  493. GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Leave));
  494. m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
  495. if (m_hovered_widget)
  496. GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Enter));
  497. }
  498. void GWindow::set_current_backing_bitmap(GraphicsBitmap& bitmap, bool flush_immediately)
  499. {
  500. WSAPI_ClientMessage message;
  501. message.type = WSAPI_ClientMessage::Type::SetWindowBackingStore;
  502. message.window_id = m_window_id;
  503. message.backing.bpp = 32;
  504. message.backing.pitch = bitmap.pitch();
  505. message.backing.shared_buffer_id = bitmap.shared_buffer_id();
  506. message.backing.has_alpha_channel = bitmap.has_alpha_channel();
  507. message.backing.size = bitmap.size();
  508. message.backing.flush_immediately = flush_immediately;
  509. GWindowServerConnection::the().sync_request(message, WSAPI_ServerMessage::Type::DidSetWindowBackingStore);
  510. }
  511. void GWindow::flip(const Vector<Rect, 32>& dirty_rects)
  512. {
  513. swap(m_front_bitmap, m_back_bitmap);
  514. set_current_backing_bitmap(*m_front_bitmap);
  515. if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) {
  516. m_back_bitmap = create_backing_bitmap(m_front_bitmap->size());
  517. memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size_in_bytes());
  518. return;
  519. }
  520. // Copy whatever was painted from the front to the back.
  521. Painter painter(*m_back_bitmap);
  522. for (auto& dirty_rect : dirty_rects)
  523. painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect);
  524. }
  525. NonnullRefPtr<GraphicsBitmap> GWindow::create_shared_bitmap(GraphicsBitmap::Format format, const Size& size)
  526. {
  527. ASSERT(GWindowServerConnection::the().server_pid());
  528. ASSERT(!size.is_empty());
  529. size_t pitch = round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16);
  530. size_t size_in_bytes = size.height() * pitch;
  531. auto shared_buffer = SharedBuffer::create_with_size(size_in_bytes);
  532. ASSERT(shared_buffer);
  533. shared_buffer->share_with(GWindowServerConnection::the().server_pid());
  534. return GraphicsBitmap::create_with_shared_buffer(format, *shared_buffer, size);
  535. }
  536. NonnullRefPtr<GraphicsBitmap> GWindow::create_backing_bitmap(const Size& size)
  537. {
  538. auto format = m_has_alpha_channel ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32;
  539. return create_shared_bitmap(format, size);
  540. }
  541. void GWindow::set_modal(bool modal)
  542. {
  543. ASSERT(!m_window_id);
  544. m_modal = modal;
  545. }
  546. void GWindow::wm_event(GWMEvent&)
  547. {
  548. }
  549. void GWindow::set_icon(const GraphicsBitmap* icon)
  550. {
  551. if (m_icon == icon)
  552. return;
  553. if (!m_window_id)
  554. return;
  555. m_icon = create_shared_bitmap(GraphicsBitmap::Format::RGBA32, icon->size());
  556. {
  557. GPainter painter(*m_icon);
  558. painter.blit({ 0, 0 }, *icon, icon->rect());
  559. }
  560. int rc = seal_shared_buffer(m_icon->shared_buffer_id());
  561. ASSERT(rc == 0);
  562. rc = share_buffer_globally(m_icon->shared_buffer_id());
  563. ASSERT(rc == 0);
  564. static bool has_set_process_icon;
  565. if (!has_set_process_icon)
  566. set_process_icon(m_icon->shared_buffer_id());
  567. WSAPI_ClientMessage message;
  568. message.type = WSAPI_ClientMessage::Type::SetWindowIconBitmap;
  569. message.window_id = m_window_id;
  570. message.window.icon_buffer_id = m_icon->shared_buffer_id();
  571. message.window.icon_size = icon->size();
  572. GWindowServerConnection::the().post_message_to_server(message);
  573. }
  574. void GWindow::start_wm_resize()
  575. {
  576. WSAPI_ClientMessage message;
  577. message.type = WSAPI_ClientMessage::Type::WM_StartWindowResize;
  578. message.wm.client_id = GWindowServerConnection::the().my_client_id();
  579. message.wm.window_id = m_window_id;
  580. GWindowServerConnection::the().post_message_to_server(message);
  581. }
  582. Vector<GWidget*> GWindow::focusable_widgets() const
  583. {
  584. if (!m_main_widget)
  585. return {};
  586. Vector<GWidget*> collected_widgets;
  587. Function<void(GWidget&)> collect_focusable_widgets = [&](GWidget& widget) {
  588. if (widget.accepts_focus())
  589. collected_widgets.append(&widget);
  590. widget.for_each_child_widget([&](auto& child) {
  591. if (!child.is_visible())
  592. return IterationDecision::Continue;
  593. if (!child.is_enabled())
  594. return IterationDecision::Continue;
  595. collect_focusable_widgets(child);
  596. return IterationDecision::Continue;
  597. });
  598. };
  599. collect_focusable_widgets(*m_main_widget);
  600. return collected_widgets;
  601. }