GWindow.cpp 14 KB

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