GWindow.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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::move_to_front()
  45. {
  46. if (!m_window_id)
  47. return;
  48. WSAPI_ClientMessage request;
  49. request.type = WSAPI_ClientMessage::Type::MoveWindowToFront;
  50. request.window_id = m_window_id;
  51. GEventLoop::post_message_to_server(request);
  52. }
  53. void GWindow::show()
  54. {
  55. if (m_window_id)
  56. return;
  57. WSAPI_ClientMessage request;
  58. request.type = WSAPI_ClientMessage::Type::CreateWindow;
  59. request.window_id = m_window_id;
  60. request.window.rect = m_rect_when_windowless;
  61. request.window.has_alpha_channel = m_has_alpha_channel;
  62. request.window.modal = m_modal;
  63. request.window.resizable = m_resizable;
  64. request.window.fullscreen = m_fullscreen;
  65. request.window.show_titlebar = m_show_titlebar;
  66. request.window.opacity = m_opacity_when_windowless;
  67. request.window.background_color = m_background_color.value();
  68. request.window.size_increment = m_size_increment;
  69. request.window.base_size = m_base_size;
  70. request.window.type = (WSAPI_WindowType)m_window_type;
  71. ASSERT(m_title_when_windowless.length() < (ssize_t)sizeof(request.text));
  72. strcpy(request.text, m_title_when_windowless.characters());
  73. request.text_length = m_title_when_windowless.length();
  74. auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidCreateWindow);
  75. m_window_id = response.window_id;
  76. windows().set(m_window_id, this);
  77. update();
  78. }
  79. void GWindow::hide()
  80. {
  81. if (!m_window_id)
  82. return;
  83. windows().remove(m_window_id);
  84. WSAPI_ClientMessage request;
  85. request.type = WSAPI_ClientMessage::Type::DestroyWindow;
  86. request.window_id = m_window_id;
  87. GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidDestroyWindow);
  88. m_window_id = 0;
  89. m_pending_paint_event_rects.clear();
  90. m_back_bitmap = nullptr;
  91. m_front_bitmap = nullptr;
  92. }
  93. void GWindow::set_title(const String& title)
  94. {
  95. m_title_when_windowless = title;
  96. if (!m_window_id)
  97. return;
  98. WSAPI_ClientMessage request;
  99. request.type = WSAPI_ClientMessage::Type::SetWindowTitle;
  100. request.window_id = m_window_id;
  101. ASSERT(m_title_when_windowless.length() < (ssize_t)sizeof(request.text));
  102. strcpy(request.text, m_title_when_windowless.characters());
  103. request.text_length = m_title_when_windowless.length();
  104. GEventLoop::current().post_message_to_server(request);
  105. }
  106. String GWindow::title() const
  107. {
  108. if (!m_window_id)
  109. return m_title_when_windowless;
  110. WSAPI_ClientMessage request;
  111. request.type = WSAPI_ClientMessage::Type::GetWindowTitle;
  112. request.window_id = m_window_id;
  113. auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidGetWindowTitle);
  114. return String(response.text, response.text_length);
  115. }
  116. Rect GWindow::rect() const
  117. {
  118. if (!m_window_id)
  119. return m_rect_when_windowless;
  120. WSAPI_ClientMessage request;
  121. request.type = WSAPI_ClientMessage::Type::GetWindowRect;
  122. request.window_id = m_window_id;
  123. auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidGetWindowRect);
  124. ASSERT(response.window_id == m_window_id);
  125. return response.window.rect;
  126. }
  127. void GWindow::set_rect(const Rect& a_rect)
  128. {
  129. m_rect_when_windowless = a_rect;
  130. if (!m_window_id) {
  131. if (m_main_widget)
  132. m_main_widget->resize(m_rect_when_windowless.size());
  133. return;
  134. }
  135. WSAPI_ClientMessage request;
  136. request.type = WSAPI_ClientMessage::Type::SetWindowRect;
  137. request.window_id = m_window_id;
  138. request.window.rect = a_rect;
  139. GEventLoop::current().post_message_to_server(request);
  140. if (m_back_bitmap && m_back_bitmap->size() != a_rect.size())
  141. m_back_bitmap = nullptr;
  142. if (m_front_bitmap && m_front_bitmap->size() != a_rect.size())
  143. m_front_bitmap = nullptr;
  144. if (m_main_widget)
  145. m_main_widget->resize(a_rect.size());
  146. }
  147. void GWindow::set_window_type(GWindowType window_type)
  148. {
  149. m_window_type = window_type;
  150. }
  151. void GWindow::set_override_cursor(GStandardCursor cursor)
  152. {
  153. if (!m_window_id)
  154. return;
  155. WSAPI_ClientMessage request;
  156. request.type = WSAPI_ClientMessage::Type::SetWindowOverrideCursor;
  157. request.window_id = m_window_id;
  158. request.cursor.cursor = (WSAPI_StandardCursor)cursor;
  159. GEventLoop::current().post_message_to_server(request);
  160. }
  161. void GWindow::event(CEvent& event)
  162. {
  163. if (event.type() == GEvent::MouseUp || event.type() == GEvent::MouseDown || event.type() == GEvent::MouseDoubleClick || event.type() == GEvent::MouseMove || event.type() == GEvent::MouseWheel) {
  164. auto& mouse_event = static_cast<GMouseEvent&>(event);
  165. if (m_global_cursor_tracking_widget) {
  166. auto window_relative_rect = m_global_cursor_tracking_widget->window_relative_rect();
  167. Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  168. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  169. m_global_cursor_tracking_widget->event(*local_event);
  170. return;
  171. }
  172. if (m_automatic_cursor_tracking_widget) {
  173. auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect();
  174. Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  175. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  176. m_automatic_cursor_tracking_widget->event(*local_event);
  177. if (mouse_event.buttons() == 0)
  178. m_automatic_cursor_tracking_widget = nullptr;
  179. return;
  180. }
  181. if (!m_main_widget)
  182. return;
  183. auto result = m_main_widget->hit_test(mouse_event.position());
  184. 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());
  185. ASSERT(result.widget);
  186. set_hovered_widget(result.widget);
  187. if (mouse_event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
  188. m_automatic_cursor_tracking_widget = result.widget->make_weak_ptr();
  189. if (result.widget != m_global_cursor_tracking_widget.ptr())
  190. return result.widget->event(*local_event);
  191. return;
  192. }
  193. if (event.type() == GEvent::MultiPaint) {
  194. if (!m_window_id)
  195. return;
  196. if (!m_main_widget)
  197. return;
  198. auto& paint_event = static_cast<GMultiPaintEvent&>(event);
  199. auto rects = paint_event.rects();
  200. ASSERT(!rects.is_empty());
  201. if (m_back_bitmap && m_back_bitmap->size() != paint_event.window_size()) {
  202. // Eagerly discard the backing store if we learn from this paint event that it needs to be bigger.
  203. // Otherwise we would have to wait for a resize event to tell us. This way we don't waste the
  204. // effort on painting into an undersized bitmap that will be thrown away anyway.
  205. m_back_bitmap = nullptr;
  206. }
  207. bool created_new_backing_store = !m_back_bitmap;
  208. if (!m_back_bitmap)
  209. m_back_bitmap = create_backing_bitmap(paint_event.window_size());
  210. auto rect = rects.first();
  211. if (rect.is_empty() || created_new_backing_store) {
  212. rects.clear();
  213. rects.append({ { }, paint_event.window_size() });
  214. }
  215. for (auto& rect : rects)
  216. m_main_widget->event(*make<GPaintEvent>(rect));
  217. if (m_double_buffering_enabled)
  218. flip(rects);
  219. else if (created_new_backing_store)
  220. set_current_backing_bitmap(*m_back_bitmap, true);
  221. if (m_window_id) {
  222. WSAPI_ClientMessage message;
  223. message.type = WSAPI_ClientMessage::Type::DidFinishPainting;
  224. message.window_id = m_window_id;
  225. message.rect_count = rects.size();
  226. for (int i = 0; i < min(WSAPI_ClientMessage::max_inline_rect_count, rects.size()); ++i)
  227. message.rects[i] = rects[i];
  228. ByteBuffer extra_data;
  229. if (rects.size() > WSAPI_ClientMessage::max_inline_rect_count)
  230. extra_data = ByteBuffer::wrap(&rects[WSAPI_ClientMessage::max_inline_rect_count], (rects.size() - WSAPI_ClientMessage::max_inline_rect_count) * sizeof(WSAPI_Rect));
  231. GEventLoop::current().post_message_to_server(message, extra_data);
  232. }
  233. return;
  234. }
  235. if (event.type() == GEvent::KeyUp || event.type() == GEvent::KeyDown) {
  236. if (m_focused_widget)
  237. return m_focused_widget->event(event);
  238. if (m_main_widget)
  239. return m_main_widget->event(event);
  240. return;
  241. }
  242. if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) {
  243. m_is_active = event.type() == GEvent::WindowBecameActive;
  244. if (m_main_widget)
  245. m_main_widget->event(event);
  246. if (m_focused_widget)
  247. m_focused_widget->update();
  248. return;
  249. }
  250. if (event.type() == GEvent::WindowCloseRequest) {
  251. close();
  252. return;
  253. }
  254. if (event.type() == GEvent::WindowLeft) {
  255. set_hovered_widget(nullptr);
  256. return;
  257. }
  258. if (event.type() == GEvent::Resize) {
  259. auto new_size = static_cast<GResizeEvent&>(event).size();
  260. if (m_back_bitmap && m_back_bitmap->size() != new_size)
  261. m_back_bitmap = nullptr;
  262. if (!m_pending_paint_event_rects.is_empty()) {
  263. m_pending_paint_event_rects.clear_with_capacity();
  264. m_pending_paint_event_rects.append({ { }, new_size });
  265. }
  266. m_rect_when_windowless = { { }, new_size };
  267. m_main_widget->set_relative_rect({ { }, new_size });
  268. return;
  269. }
  270. if (event.type() > GEvent::__Begin_WM_Events && event.type() < GEvent::__End_WM_Events)
  271. return wm_event(static_cast<GWMEvent&>(event));
  272. CObject::event(event);
  273. }
  274. bool GWindow::is_visible() const
  275. {
  276. return false;
  277. }
  278. void GWindow::update(const Rect& a_rect)
  279. {
  280. if (!m_window_id)
  281. return;
  282. for (auto& pending_rect : m_pending_paint_event_rects) {
  283. if (pending_rect.contains(a_rect)) {
  284. #ifdef UPDATE_COALESCING_DEBUG
  285. dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
  286. #endif
  287. return;
  288. }
  289. }
  290. if (m_pending_paint_event_rects.is_empty()) {
  291. deferred_invoke([this] (auto&) {
  292. auto rects = move(m_pending_paint_event_rects);
  293. if (rects.is_empty())
  294. return;
  295. WSAPI_ClientMessage request;
  296. request.type = WSAPI_ClientMessage::Type::InvalidateRect;
  297. request.window_id = m_window_id;
  298. for (int i = 0; i < min(WSAPI_ClientMessage::max_inline_rect_count, rects.size()); ++i)
  299. request.rects[i] = rects[i];
  300. ByteBuffer extra_data;
  301. if (rects.size() > WSAPI_ClientMessage::max_inline_rect_count)
  302. extra_data = ByteBuffer::wrap(&rects[WSAPI_ClientMessage::max_inline_rect_count], (rects.size() - WSAPI_ClientMessage::max_inline_rect_count) * sizeof(WSAPI_Rect));
  303. request.rect_count = rects.size();
  304. GEventLoop::current().post_message_to_server(request, extra_data);
  305. });
  306. }
  307. m_pending_paint_event_rects.append(a_rect);
  308. }
  309. void GWindow::set_main_widget(GWidget* widget)
  310. {
  311. if (m_main_widget == widget)
  312. return;
  313. m_main_widget = widget;
  314. if (m_main_widget) {
  315. auto new_window_rect = rect();
  316. if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
  317. new_window_rect.set_width(m_main_widget->preferred_size().width());
  318. if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
  319. new_window_rect.set_height(m_main_widget->preferred_size().height());
  320. set_rect(new_window_rect);
  321. m_main_widget->set_relative_rect({ { }, new_window_rect.size() });
  322. m_main_widget->set_window(this);
  323. if (m_main_widget->accepts_focus())
  324. m_main_widget->set_focus(true);
  325. }
  326. update();
  327. }
  328. void GWindow::set_focused_widget(GWidget* widget)
  329. {
  330. if (m_focused_widget == widget)
  331. return;
  332. if (m_focused_widget) {
  333. GEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusOut));
  334. m_focused_widget->update();
  335. }
  336. m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
  337. if (m_focused_widget) {
  338. GEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusIn));
  339. m_focused_widget->update();
  340. }
  341. }
  342. void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
  343. {
  344. if (widget == m_global_cursor_tracking_widget)
  345. return;
  346. m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  347. }
  348. void GWindow::set_automatic_cursor_tracking_widget(GWidget* widget)
  349. {
  350. if (widget == m_automatic_cursor_tracking_widget)
  351. return;
  352. m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  353. }
  354. void GWindow::set_has_alpha_channel(bool value)
  355. {
  356. if (m_has_alpha_channel == value)
  357. return;
  358. m_has_alpha_channel = value;
  359. if (!m_window_id)
  360. return;
  361. m_pending_paint_event_rects.clear();
  362. m_back_bitmap = nullptr;
  363. m_front_bitmap = nullptr;
  364. WSAPI_ClientMessage message;
  365. message.type = WSAPI_ClientMessage::Type::SetWindowHasAlphaChannel;
  366. message.window_id = m_window_id;
  367. message.value = value;
  368. GEventLoop::current().sync_request(message, WSAPI_ServerMessage::DidSetWindowHasAlphaChannel);
  369. update();
  370. }
  371. void GWindow::set_double_buffering_enabled(bool value)
  372. {
  373. ASSERT(!m_window_id);
  374. m_double_buffering_enabled = value;
  375. }
  376. void GWindow::set_opacity(float opacity)
  377. {
  378. m_opacity_when_windowless = opacity;
  379. if (!m_window_id)
  380. return;
  381. WSAPI_ClientMessage request;
  382. request.type = WSAPI_ClientMessage::Type::SetWindowOpacity;
  383. request.window_id = m_window_id;
  384. request.window.opacity = opacity;
  385. m_opacity_when_windowless = opacity;
  386. GEventLoop::current().post_message_to_server(request);
  387. }
  388. void GWindow::set_hovered_widget(GWidget* widget)
  389. {
  390. if (widget == m_hovered_widget)
  391. return;
  392. if (m_hovered_widget)
  393. GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Leave));
  394. m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
  395. if (m_hovered_widget)
  396. GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Enter));
  397. }
  398. void GWindow::set_current_backing_bitmap(GraphicsBitmap& bitmap, bool flush_immediately)
  399. {
  400. WSAPI_ClientMessage message;
  401. message.type = WSAPI_ClientMessage::Type::SetWindowBackingStore;
  402. message.window_id = m_window_id;
  403. message.backing.bpp = 32;
  404. message.backing.pitch = bitmap.pitch();
  405. message.backing.shared_buffer_id = bitmap.shared_buffer_id();
  406. message.backing.has_alpha_channel = bitmap.has_alpha_channel();
  407. message.backing.size = bitmap.size();
  408. message.backing.flush_immediately = flush_immediately;
  409. GEventLoop::current().sync_request(message, WSAPI_ServerMessage::Type::DidSetWindowBackingStore);
  410. }
  411. void GWindow::flip(const Vector<Rect, 32>& dirty_rects)
  412. {
  413. swap(m_front_bitmap, m_back_bitmap);
  414. set_current_backing_bitmap(*m_front_bitmap);
  415. if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) {
  416. m_back_bitmap = create_backing_bitmap(m_front_bitmap->size());
  417. memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size_in_bytes());
  418. return;
  419. }
  420. // Copy whatever was painted from the front to the back.
  421. Painter painter(*m_back_bitmap);
  422. for (auto& dirty_rect : dirty_rects)
  423. painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect);
  424. }
  425. Retained<GraphicsBitmap> GWindow::create_backing_bitmap(const Size& size)
  426. {
  427. ASSERT(GEventLoop::server_pid());
  428. ASSERT(!size.is_empty());
  429. size_t pitch = round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16);
  430. size_t size_in_bytes = size.height() * pitch;
  431. auto shared_buffer = SharedBuffer::create(GEventLoop::server_pid(), size_in_bytes);
  432. ASSERT(shared_buffer);
  433. auto format = m_has_alpha_channel ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32;
  434. return GraphicsBitmap::create_with_shared_buffer(format, *shared_buffer, size);
  435. }
  436. void GWindow::set_modal(bool modal)
  437. {
  438. ASSERT(!m_window_id);
  439. m_modal = modal;
  440. }
  441. void GWindow::wm_event(GWMEvent&)
  442. {
  443. }
  444. void GWindow::set_icon_path(const String& path)
  445. {
  446. if (m_icon_path == path)
  447. return;
  448. m_icon_path = path;
  449. if (!m_window_id)
  450. return;
  451. WSAPI_ClientMessage message;
  452. message.type = WSAPI_ClientMessage::Type::SetWindowIcon;
  453. message.window_id = m_window_id;
  454. ASSERT(path.length() < sizeof(message.text));
  455. strcpy(message.text, path.characters());
  456. message.text_length = path.length();
  457. GEventLoop::post_message_to_server(message);
  458. }
  459. void GWindow::start_wm_resize()
  460. {
  461. WSAPI_ClientMessage message;
  462. message.type = WSAPI_ClientMessage::Type::WM_StartWindowResize;
  463. message.wm.client_id = GEventLoop::my_client_id();
  464. message.wm.window_id = m_window_id;
  465. GEventLoop::post_message_to_server(message);
  466. }
  467. Vector<GWidget*> GWindow::focusable_widgets() const
  468. {
  469. if (!m_main_widget)
  470. return { };
  471. Vector<GWidget*> collected_widgets;
  472. Function<void(GWidget&)> collect_focusable_widgets = [&] (GWidget& widget) {
  473. if (widget.accepts_focus())
  474. collected_widgets.append(&widget);
  475. widget.for_each_child_widget([&] (auto& child) {
  476. if (!child.is_visible())
  477. return IterationDecision::Continue;
  478. if (!child.is_enabled())
  479. return IterationDecision::Continue;
  480. collect_focusable_widgets(child);
  481. return IterationDecision::Continue;
  482. });
  483. };
  484. collect_focusable_widgets(*m_main_widget);
  485. return collected_widgets;
  486. }