GWindow.cpp 22 KB

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