GWindow.cpp 13 KB

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