GWindow.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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.opacity = m_opacity_when_windowless;
  56. request.window.background_color = m_background_color.value();
  57. request.window.size_increment = m_size_increment;
  58. request.window.base_size = m_base_size;
  59. request.window.type = (WSAPI_WindowType)m_window_type;
  60. ASSERT(m_title_when_windowless.length() < (ssize_t)sizeof(request.text));
  61. strcpy(request.text, m_title_when_windowless.characters());
  62. request.text_length = m_title_when_windowless.length();
  63. auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidCreateWindow);
  64. m_window_id = response.window_id;
  65. windows().set(m_window_id, this);
  66. update();
  67. }
  68. void GWindow::hide()
  69. {
  70. if (!m_window_id)
  71. return;
  72. windows().remove(m_window_id);
  73. WSAPI_ClientMessage request;
  74. request.type = WSAPI_ClientMessage::Type::DestroyWindow;
  75. request.window_id = m_window_id;
  76. GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidDestroyWindow);
  77. m_window_id = 0;
  78. m_pending_paint_event_rects.clear();
  79. m_back_bitmap = nullptr;
  80. m_front_bitmap = nullptr;
  81. }
  82. void GWindow::set_title(const String& title)
  83. {
  84. m_title_when_windowless = title;
  85. if (!m_window_id)
  86. return;
  87. WSAPI_ClientMessage request;
  88. request.type = WSAPI_ClientMessage::Type::SetWindowTitle;
  89. request.window_id = m_window_id;
  90. ASSERT(m_title_when_windowless.length() < (ssize_t)sizeof(request.text));
  91. strcpy(request.text, m_title_when_windowless.characters());
  92. request.text_length = m_title_when_windowless.length();
  93. GEventLoop::current().post_message_to_server(request);
  94. }
  95. String GWindow::title() const
  96. {
  97. if (!m_window_id)
  98. return m_title_when_windowless;
  99. WSAPI_ClientMessage request;
  100. request.type = WSAPI_ClientMessage::Type::GetWindowTitle;
  101. request.window_id = m_window_id;
  102. auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidGetWindowTitle);
  103. return String(response.text, response.text_length);
  104. }
  105. Rect GWindow::rect() const
  106. {
  107. if (!m_window_id)
  108. return m_rect_when_windowless;
  109. WSAPI_ClientMessage request;
  110. request.type = WSAPI_ClientMessage::Type::GetWindowRect;
  111. request.window_id = m_window_id;
  112. auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidGetWindowRect);
  113. ASSERT(response.window_id == m_window_id);
  114. return response.window.rect;
  115. }
  116. void GWindow::set_rect(const Rect& a_rect)
  117. {
  118. m_rect_when_windowless = a_rect;
  119. if (!m_window_id) {
  120. if (m_main_widget)
  121. m_main_widget->resize(m_rect_when_windowless.size());
  122. return;
  123. }
  124. WSAPI_ClientMessage request;
  125. request.type = WSAPI_ClientMessage::Type::SetWindowRect;
  126. request.window_id = m_window_id;
  127. request.window.rect = a_rect;
  128. GEventLoop::current().post_message_to_server(request);
  129. if (m_back_bitmap->size() != a_rect.size())
  130. m_back_bitmap = nullptr;
  131. if (m_front_bitmap->size() != a_rect.size())
  132. m_front_bitmap = nullptr;
  133. if (m_main_widget)
  134. m_main_widget->resize(a_rect.size());
  135. }
  136. void GWindow::set_window_type(GWindowType window_type)
  137. {
  138. m_window_type = window_type;
  139. }
  140. void GWindow::set_override_cursor(GStandardCursor cursor)
  141. {
  142. if (!m_window_id)
  143. return;
  144. WSAPI_ClientMessage request;
  145. request.type = WSAPI_ClientMessage::Type::SetWindowOverrideCursor;
  146. request.window_id = m_window_id;
  147. request.cursor.cursor = (WSAPI_StandardCursor)cursor;
  148. GEventLoop::current().post_message_to_server(request);
  149. }
  150. void GWindow::event(CEvent& event)
  151. {
  152. if (event.type() == GEvent::MouseUp || event.type() == GEvent::MouseDown || event.type() == GEvent::MouseMove) {
  153. auto& mouse_event = static_cast<GMouseEvent&>(event);
  154. if (m_global_cursor_tracking_widget) {
  155. auto window_relative_rect = m_global_cursor_tracking_widget->window_relative_rect();
  156. Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  157. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers());
  158. m_global_cursor_tracking_widget->event(*local_event);
  159. return;
  160. }
  161. if (m_automatic_cursor_tracking_widget) {
  162. auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect();
  163. Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  164. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers());
  165. m_automatic_cursor_tracking_widget->event(*local_event);
  166. if (mouse_event.buttons() == 0)
  167. m_automatic_cursor_tracking_widget = nullptr;
  168. return;
  169. }
  170. if (!m_main_widget)
  171. return;
  172. if (m_main_widget) {
  173. auto result = m_main_widget->hit_test(mouse_event.position());
  174. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), result.local_position, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers());
  175. ASSERT(result.widget);
  176. set_hovered_widget(result.widget);
  177. if (mouse_event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
  178. m_automatic_cursor_tracking_widget = result.widget->make_weak_ptr();
  179. if (result.widget != m_global_cursor_tracking_widget.ptr())
  180. return result.widget->event(*local_event);
  181. }
  182. return;
  183. }
  184. if (event.type() == GEvent::Paint) {
  185. if (!m_window_id)
  186. return;
  187. m_pending_paint_event_rects.clear();
  188. if (!m_main_widget)
  189. return;
  190. auto& paint_event = static_cast<GPaintEvent&>(event);
  191. auto rect = paint_event.rect();
  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. if (rect.is_empty() || created_new_backing_store)
  202. rect = { { }, paint_event.window_size() };
  203. m_main_widget->event(*make<GPaintEvent>(rect));
  204. if (m_double_buffering_enabled)
  205. flip(rect);
  206. else if (created_new_backing_store)
  207. set_current_backing_bitmap(*m_back_bitmap, true);
  208. if (m_window_id) {
  209. WSAPI_ClientMessage message;
  210. message.type = WSAPI_ClientMessage::Type::DidFinishPainting;
  211. message.window_id = m_window_id;
  212. message.window.rect = rect;
  213. GEventLoop::current().post_message_to_server(message);
  214. }
  215. return;
  216. }
  217. if (event.type() == GEvent::KeyUp || event.type() == GEvent::KeyDown) {
  218. if (m_focused_widget)
  219. return m_focused_widget->event(event);
  220. if (m_main_widget)
  221. return m_main_widget->event(event);
  222. return;
  223. }
  224. if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) {
  225. m_is_active = event.type() == GEvent::WindowBecameActive;
  226. if (m_main_widget)
  227. m_main_widget->event(event);
  228. if (m_focused_widget)
  229. m_focused_widget->update();
  230. return;
  231. }
  232. if (event.type() == GEvent::WindowCloseRequest) {
  233. close();
  234. return;
  235. }
  236. if (event.type() == GEvent::WindowLeft) {
  237. set_hovered_widget(nullptr);
  238. return;
  239. }
  240. if (event.type() == GEvent::Resize) {
  241. auto new_size = static_cast<GResizeEvent&>(event).size();
  242. if (m_back_bitmap && m_back_bitmap->size() != new_size)
  243. m_back_bitmap = nullptr;
  244. m_pending_paint_event_rects.clear();
  245. m_rect_when_windowless = { { }, new_size };
  246. m_main_widget->set_relative_rect({ { }, new_size });
  247. return;
  248. }
  249. if (event.type() > GEvent::__Begin_WM_Events && event.type() < GEvent::__End_WM_Events)
  250. return wm_event(static_cast<GWMEvent&>(event));
  251. CObject::event(event);
  252. }
  253. bool GWindow::is_visible() const
  254. {
  255. return false;
  256. }
  257. void GWindow::update(const Rect& a_rect)
  258. {
  259. if (!m_window_id)
  260. return;
  261. for (auto& pending_rect : m_pending_paint_event_rects) {
  262. if (pending_rect.contains(a_rect)) {
  263. #ifdef UPDATE_COALESCING_DEBUG
  264. dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
  265. #endif
  266. return;
  267. }
  268. }
  269. m_pending_paint_event_rects.append(a_rect);
  270. WSAPI_ClientMessage request;
  271. request.type = WSAPI_ClientMessage::Type::InvalidateRect;
  272. request.window_id = m_window_id;
  273. request.window.rect = a_rect;
  274. GEventLoop::current().post_message_to_server(request);
  275. }
  276. void GWindow::set_main_widget(GWidget* widget)
  277. {
  278. if (m_main_widget == widget)
  279. return;
  280. m_main_widget = widget;
  281. if (m_main_widget) {
  282. auto new_window_rect = rect();
  283. if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
  284. new_window_rect.set_width(m_main_widget->preferred_size().width());
  285. if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
  286. new_window_rect.set_height(m_main_widget->preferred_size().height());
  287. set_rect(new_window_rect);
  288. m_main_widget->set_relative_rect({ { }, new_window_rect.size() });
  289. m_main_widget->set_window(this);
  290. if (m_main_widget->accepts_focus())
  291. m_main_widget->set_focus(true);
  292. }
  293. update();
  294. }
  295. void GWindow::set_focused_widget(GWidget* widget)
  296. {
  297. if (m_focused_widget == widget)
  298. return;
  299. if (m_focused_widget) {
  300. GEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusOut));
  301. m_focused_widget->update();
  302. }
  303. m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
  304. if (m_focused_widget) {
  305. GEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusIn));
  306. m_focused_widget->update();
  307. }
  308. }
  309. void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
  310. {
  311. if (widget == m_global_cursor_tracking_widget)
  312. return;
  313. m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  314. }
  315. void GWindow::set_automatic_cursor_tracking_widget(GWidget* widget)
  316. {
  317. if (widget == m_automatic_cursor_tracking_widget)
  318. return;
  319. m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  320. }
  321. void GWindow::set_has_alpha_channel(bool value)
  322. {
  323. ASSERT(!m_window_id);
  324. m_has_alpha_channel = value;
  325. }
  326. void GWindow::set_double_buffering_enabled(bool value)
  327. {
  328. ASSERT(!m_window_id);
  329. m_double_buffering_enabled = value;
  330. }
  331. void GWindow::set_opacity(float opacity)
  332. {
  333. m_opacity_when_windowless = opacity;
  334. if (!m_window_id)
  335. return;
  336. WSAPI_ClientMessage request;
  337. request.type = WSAPI_ClientMessage::Type::SetWindowOpacity;
  338. request.window_id = m_window_id;
  339. request.window.opacity = opacity;
  340. m_opacity_when_windowless = opacity;
  341. GEventLoop::current().post_message_to_server(request);
  342. }
  343. void GWindow::set_hovered_widget(GWidget* widget)
  344. {
  345. if (widget == m_hovered_widget)
  346. return;
  347. if (m_hovered_widget)
  348. GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Leave));
  349. m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
  350. if (m_hovered_widget)
  351. GEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Enter));
  352. }
  353. void GWindow::set_current_backing_bitmap(GraphicsBitmap& bitmap, bool flush_immediately)
  354. {
  355. WSAPI_ClientMessage message;
  356. message.type = WSAPI_ClientMessage::Type::SetWindowBackingStore;
  357. message.window_id = m_window_id;
  358. message.backing.bpp = 32;
  359. message.backing.pitch = bitmap.pitch();
  360. message.backing.shared_buffer_id = bitmap.shared_buffer_id();
  361. message.backing.has_alpha_channel = bitmap.has_alpha_channel();
  362. message.backing.size = bitmap.size();
  363. message.backing.flush_immediately = flush_immediately;
  364. GEventLoop::current().sync_request(message, WSAPI_ServerMessage::Type::DidSetWindowBackingStore);
  365. }
  366. void GWindow::flip(const Rect& dirty_rect)
  367. {
  368. swap(m_front_bitmap, m_back_bitmap);
  369. set_current_backing_bitmap(*m_front_bitmap);
  370. if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) {
  371. m_back_bitmap = create_backing_bitmap(m_front_bitmap->size());
  372. memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size().area() * sizeof(RGBA32));
  373. return;
  374. }
  375. // Copy whatever was painted from the front to the back.
  376. Painter painter(*m_back_bitmap);
  377. painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect);
  378. }
  379. Retained<GraphicsBitmap> GWindow::create_backing_bitmap(const Size& size)
  380. {
  381. ASSERT(GEventLoop::server_pid());
  382. ASSERT(!size.is_empty());
  383. size_t size_in_bytes = size.area() * sizeof(RGBA32);
  384. auto shared_buffer = SharedBuffer::create(GEventLoop::server_pid(), size_in_bytes);
  385. ASSERT(shared_buffer);
  386. auto format = m_has_alpha_channel ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32;
  387. return GraphicsBitmap::create_with_shared_buffer(format, *shared_buffer, size);
  388. }
  389. void GWindow::set_modal(bool modal)
  390. {
  391. ASSERT(!m_window_id);
  392. m_modal = modal;
  393. }
  394. void GWindow::wm_event(GWMEvent&)
  395. {
  396. }
  397. void GWindow::set_icon_path(const String& path)
  398. {
  399. if (m_icon_path == path)
  400. return;
  401. m_icon_path = path;
  402. if (!m_window_id)
  403. return;
  404. WSAPI_ClientMessage message;
  405. message.type = WSAPI_ClientMessage::Type::SetWindowIcon;
  406. message.window_id = m_window_id;
  407. ASSERT(path.length() < sizeof(message.text));
  408. strcpy(message.text, path.characters());
  409. message.text_length = path.length();
  410. GEventLoop::post_message_to_server(message);
  411. }