WSWindowManager.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. #include "WSWindowManager.h"
  2. #include "WSWindow.h"
  3. #include "WSScreen.h"
  4. #include "WSMessageLoop.h"
  5. #include "Process.h"
  6. #include "MemoryManager.h"
  7. #include <Kernel/ProcFS.h>
  8. #include <SharedGraphics/Font.h>
  9. #include <SharedGraphics/Painter.h>
  10. #include <SharedGraphics/CharacterBitmap.h>
  11. #include <AK/StdLibExtras.h>
  12. #include <Kernel/BochsVGADevice.h>
  13. #include "WSMenu.h"
  14. #include "WSMenuBar.h"
  15. #include "WSMenuItem.h"
  16. //#define DEBUG_COUNTERS
  17. //#define DEBUG_WID_IN_TITLE_BAR
  18. static const int window_titlebar_height = 16;
  19. static inline Rect title_bar_rect(const Rect& window)
  20. {
  21. return {
  22. window.x() - 1,
  23. window.y() - window_titlebar_height,
  24. window.width() + 2,
  25. window_titlebar_height
  26. };
  27. }
  28. static inline Rect title_bar_text_rect(const Rect& window)
  29. {
  30. auto titlebar_rect = title_bar_rect(window);
  31. return {
  32. titlebar_rect.x() + 2,
  33. titlebar_rect.y(),
  34. titlebar_rect.width() - 4,
  35. titlebar_rect.height()
  36. };
  37. }
  38. static inline Rect close_button_rect_for_window(const Rect& window_rect)
  39. {
  40. auto titlebar_inner_rect = title_bar_text_rect(window_rect);
  41. int close_button_margin = 1;
  42. int close_button_size = titlebar_inner_rect.height() - close_button_margin * 2;
  43. return Rect {
  44. titlebar_inner_rect.right() - close_button_size + 1,
  45. titlebar_inner_rect.top() + close_button_margin,
  46. close_button_size,
  47. close_button_size - 1
  48. };
  49. }
  50. static inline Rect border_window_rect(const Rect& window)
  51. {
  52. auto titlebar_rect = title_bar_rect(window);
  53. return { titlebar_rect.x() - 1,
  54. titlebar_rect.y() - 1,
  55. titlebar_rect.width() + 2,
  56. window_titlebar_height + window.height() + 3
  57. };
  58. }
  59. static inline Rect outer_window_rect(const Rect& window)
  60. {
  61. auto rect = border_window_rect(window);
  62. rect.inflate(2, 2);
  63. return rect;
  64. }
  65. static WSWindowManager* s_the;
  66. WSWindowManager& WSWindowManager::the()
  67. {
  68. if (!s_the)
  69. s_the = new WSWindowManager;
  70. return *s_the;
  71. }
  72. static const char* cursor_bitmap_inner_ascii = {
  73. " # "
  74. " ## "
  75. " ### "
  76. " #### "
  77. " ##### "
  78. " ###### "
  79. " ####### "
  80. " ######## "
  81. " ######### "
  82. " ########## "
  83. " ###### "
  84. " ## ## "
  85. " # ## "
  86. " ## "
  87. " ## "
  88. " ## "
  89. " "
  90. };
  91. static const char* cursor_bitmap_outer_ascii = {
  92. "## "
  93. "# # "
  94. "# # "
  95. "# # "
  96. "# # "
  97. "# # "
  98. "# # "
  99. "# # "
  100. "# # "
  101. "# # "
  102. "# #### "
  103. "# ## # "
  104. "# # # # "
  105. "## # # "
  106. " # # "
  107. " # # "
  108. " ## "
  109. };
  110. void WSWindowManager::flip_buffers()
  111. {
  112. swap(m_front_bitmap, m_back_bitmap);
  113. swap(m_front_painter, m_back_painter);
  114. if (m_buffers_are_flipped)
  115. BochsVGADevice::the().set_y_offset(0);
  116. else
  117. BochsVGADevice::the().set_y_offset(m_screen_rect.height());
  118. m_buffers_are_flipped = !m_buffers_are_flipped;
  119. }
  120. WSWindowManager::WSWindowManager()
  121. : m_screen(WSScreen::the())
  122. , m_screen_rect(m_screen.rect())
  123. , m_lock("WSWindowManager")
  124. , m_flash_flush(false)
  125. {
  126. #ifndef DEBUG_COUNTERS
  127. (void)m_compose_count;
  128. (void)m_flush_count;
  129. #endif
  130. auto size = m_screen_rect.size();
  131. m_front_bitmap = GraphicsBitmap::create_wrapper(size, m_screen.scanline(0));
  132. m_back_bitmap = GraphicsBitmap::create_wrapper(size, m_screen.scanline(size.height()));
  133. m_front_painter = make<Painter>(*m_front_bitmap);
  134. m_back_painter = make<Painter>(*m_back_bitmap);
  135. m_font = Font::default_font();
  136. m_front_painter->set_font(font());
  137. m_back_painter->set_font(font());
  138. m_background_color = Color(50, 50, 50);
  139. m_active_window_border_color = Color(110, 34, 9);
  140. m_active_window_border_color2 = Color(244, 202, 158);
  141. m_active_window_title_color = Color::White;
  142. m_inactive_window_border_color = Color(128, 128, 128);
  143. m_inactive_window_border_color2 = Color(192, 192, 192);
  144. m_inactive_window_title_color = Color(213, 208, 199);
  145. m_dragging_window_border_color = Color(161, 50, 13);
  146. m_dragging_window_border_color2 = Color(250, 220, 187);
  147. m_dragging_window_title_color = Color::White;
  148. m_cursor_bitmap_inner = CharacterBitmap::create_from_ascii(cursor_bitmap_inner_ascii, 12, 17);
  149. m_cursor_bitmap_outer = CharacterBitmap::create_from_ascii(cursor_bitmap_outer_ascii, 12, 17);
  150. {
  151. LOCKER(m_wallpaper_path.lock());
  152. m_wallpaper_path.resource() = "/res/wallpapers/cool.rgb";
  153. m_wallpaper = GraphicsBitmap::load_from_file(m_wallpaper_path.resource(), m_screen_rect.size());
  154. }
  155. ProcFS::the().add_sys_bool("wm_flash_flush", m_flash_flush);
  156. ProcFS::the().add_sys_string("wm_wallpaper", m_wallpaper_path, [this] {
  157. LOCKER(m_wallpaper_path.lock());
  158. m_wallpaper = GraphicsBitmap::load_from_file(m_wallpaper_path.resource(), m_screen_rect.size());
  159. invalidate(m_screen_rect);
  160. });
  161. {
  162. auto menubar = make<WSMenuBar>();
  163. {
  164. auto menu = make<WSMenu>("Serenity");
  165. menu->add_item(make<WSMenuItem>(0, "Launch Terminal"));
  166. menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
  167. menu->add_item(make<WSMenuItem>(1, "Hello again"));
  168. menu->add_item(make<WSMenuItem>(2, "To all my friends"));
  169. menu->add_item(make<WSMenuItem>(3, "Together we can play some rock&roll"));
  170. menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
  171. menu->add_item(make<WSMenuItem>(4, "About..."));
  172. menu->on_item_activation = [] (WSMenuItem& item) {
  173. kprintf("WSMenu 1 item activated: '%s'\n", item.text().characters());
  174. };
  175. menubar->add_menu(move(menu));
  176. }
  177. {
  178. auto menu = make<WSMenu>("Application");
  179. menu->add_item(make<WSMenuItem>(5, "Foo."));
  180. menu->add_item(make<WSMenuItem>(6, "Bar?"));
  181. menu->add_item(make<WSMenuItem>(7, "Baz!"));
  182. menu->on_item_activation = [] (WSMenuItem& item) {
  183. kprintf("WSMenu 2 item activated: '%s'\n", item.text().characters());
  184. };
  185. menubar->add_menu(move(menu));
  186. }
  187. set_current_menubar(menubar.ptr());
  188. m_menubars.set(1, move(menubar));
  189. }
  190. invalidate();
  191. compose();
  192. }
  193. WSWindowManager::~WSWindowManager()
  194. {
  195. }
  196. void WSWindowManager::set_current_menubar(WSMenuBar* menubar)
  197. {
  198. if (m_current_menubar == menubar)
  199. return;
  200. m_current_menubar = menubar;
  201. if (!m_current_menubar)
  202. return;
  203. int menu_margin = 16;
  204. Point next_menu_location { menu_margin / 2, 3 };
  205. m_current_menubar->for_each_menu([&] (WSMenu& menu) {
  206. int text_width = font().width(menu.name());
  207. menu.set_rect_in_menubar({ next_menu_location.x() - menu_margin / 2, 0, text_width + menu_margin, menubar_rect().height() - 1 });
  208. menu.set_text_rect_in_menubar({ next_menu_location, { text_width, font().glyph_height() } });
  209. next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
  210. return true;
  211. });
  212. }
  213. static const char* s_close_button_bitmap_data = {
  214. "## ##"
  215. "### ###"
  216. " ###### "
  217. " #### "
  218. " ## "
  219. " #### "
  220. " ###### "
  221. "### ###"
  222. "## ##"
  223. };
  224. static CharacterBitmap* s_close_button_bitmap;
  225. static const int s_close_button_bitmap_width = 8;
  226. static const int s_close_button_bitmap_height = 9;
  227. void WSWindowManager::paint_window_frame(WSWindow& window)
  228. {
  229. LOCKER(m_lock);
  230. //printf("[WM] paint_window_frame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
  231. if (window.is_menu()) {
  232. m_back_painter->draw_rect(window.rect().inflated(2, 2), Color::LightGray);
  233. return;
  234. }
  235. auto titlebar_rect = title_bar_rect(window.rect());
  236. auto titlebar_inner_rect = title_bar_text_rect(window.rect());
  237. auto outer_rect = outer_window_rect(window.rect());
  238. auto border_rect = border_window_rect(window.rect());
  239. auto close_button_rect = close_button_rect_for_window(window.rect());
  240. auto titlebar_title_rect = titlebar_inner_rect;
  241. titlebar_title_rect.set_width(font().glyph_width() * window.title().length());
  242. Rect inner_border_rect {
  243. window.x() - 1,
  244. window.y() - 1,
  245. window.width() + 2,
  246. window.height() + 2
  247. };
  248. Color title_color;
  249. Color border_color;
  250. Color border_color2;
  251. Color middle_border_color;
  252. if (&window == m_drag_window.ptr()) {
  253. border_color = m_dragging_window_border_color;
  254. border_color2 = m_dragging_window_border_color2;
  255. title_color = m_dragging_window_title_color;
  256. middle_border_color = Color::White;
  257. } else if (&window == m_active_window.ptr()) {
  258. border_color = m_active_window_border_color;
  259. border_color2 = m_active_window_border_color2;
  260. title_color = m_active_window_title_color;
  261. middle_border_color = Color::MidGray;
  262. } else {
  263. border_color = m_inactive_window_border_color;
  264. border_color2 = m_inactive_window_border_color2;
  265. title_color = m_inactive_window_title_color;
  266. middle_border_color = Color::MidGray;
  267. }
  268. m_back_painter->fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
  269. for (int i = 2; i <= titlebar_inner_rect.height() - 4; i += 2) {
  270. m_back_painter->draw_line({ titlebar_title_rect.right() + 4, titlebar_inner_rect.y() + i }, { close_button_rect.left() - 3, titlebar_inner_rect.y() + i }, border_color);
  271. }
  272. m_back_painter->draw_rect(border_rect, middle_border_color);
  273. m_back_painter->draw_rect(outer_rect, border_color);
  274. m_back_painter->draw_rect(inner_border_rect, border_color);
  275. m_back_painter->draw_text(titlebar_title_rect, window.title(), TextAlignment::CenterLeft, title_color);
  276. if (!s_close_button_bitmap)
  277. s_close_button_bitmap = CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();
  278. m_back_painter->fill_rect_with_gradient(close_button_rect.shrunken(2, 2), Color::LightGray, Color::White);
  279. m_back_painter->draw_rect(close_button_rect, Color::DarkGray, true);
  280. auto x_location = close_button_rect.center();
  281. x_location.move_by(-(s_close_button_bitmap_width / 2), -(s_close_button_bitmap_height / 2));
  282. m_back_painter->draw_bitmap(x_location, *s_close_button_bitmap, Color::Black);
  283. #ifdef DEBUG_WID_IN_TITLE_BAR
  284. Color metadata_color(96, 96, 96);
  285. m_back_painter->draw_text(
  286. titlebar_inner_rect,
  287. String::format("%d:%d", window.pid(), window.window_id()),
  288. TextAlignment::CenterRight,
  289. metadata_color
  290. );
  291. #endif
  292. }
  293. void WSWindowManager::add_window(WSWindow& window)
  294. {
  295. LOCKER(m_lock);
  296. m_windows.set(&window);
  297. m_windows_in_order.append(&window);
  298. if (!active_window())
  299. set_active_window(&window);
  300. }
  301. void WSWindowManager::move_to_front(WSWindow& window)
  302. {
  303. LOCKER(m_lock);
  304. if (m_windows_in_order.tail() != &window)
  305. invalidate(window);
  306. m_windows_in_order.remove(&window);
  307. m_windows_in_order.append(&window);
  308. }
  309. void WSWindowManager::remove_window(WSWindow& window)
  310. {
  311. LOCKER(m_lock);
  312. if (!m_windows.contains(&window))
  313. return;
  314. invalidate(window);
  315. m_windows.remove(&window);
  316. m_windows_in_order.remove(&window);
  317. if (!active_window() && !m_windows.is_empty())
  318. set_active_window(*m_windows.begin());
  319. }
  320. void WSWindowManager::notify_title_changed(WSWindow& window)
  321. {
  322. printf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  323. invalidate(outer_window_rect(window.rect()));
  324. }
  325. void WSWindowManager::notify_rect_changed(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  326. {
  327. printf("[WM] WSWindow %p rect changed (%d,%d %dx%d) -> (%d,%d %dx%d)\n", &window, old_rect.x(), old_rect.y(), old_rect.width(), old_rect.height(), new_rect.x(), new_rect.y(), new_rect.width(), new_rect.height());
  328. ASSERT_INTERRUPTS_ENABLED();
  329. invalidate(outer_window_rect(old_rect));
  330. invalidate(outer_window_rect(new_rect));
  331. }
  332. void WSWindowManager::handle_menu_mouse_event(WSMenu& menu, WSMouseEvent& event)
  333. {
  334. bool should_open_menu = (event.type() == WSMouseEvent::MouseMove && event.buttons() & (unsigned)MouseButton::Left)
  335. || (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left);
  336. if (should_open_menu) {
  337. if (m_current_menu == &menu)
  338. return;
  339. close_current_menu();
  340. auto& menu_window = menu.ensure_menu_window();
  341. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() });
  342. menu_window.set_visible(true);
  343. m_current_menu = &menu;
  344. return;
  345. }
  346. if (event.type() == WSMouseEvent::MouseUp && event.button() == MouseButton::Left) {
  347. close_current_menu();
  348. return;
  349. }
  350. }
  351. void WSWindowManager::close_current_menu()
  352. {
  353. if (m_current_menu && m_current_menu->menu_window())
  354. m_current_menu->menu_window()->set_visible(false);
  355. m_current_menu = nullptr;
  356. }
  357. void WSWindowManager::handle_menubar_mouse_event(WSMenuBar& menu, WSMouseEvent& event)
  358. {
  359. m_current_menubar->for_each_menu([&] (WSMenu& menu) {
  360. if (menu.rect_in_menubar().contains(event.position())) {
  361. handle_menu_mouse_event(menu, event);
  362. return false;
  363. }
  364. return true;
  365. });
  366. }
  367. void WSWindowManager::handle_titlebar_mouse_event(WSWindow& window, WSMouseEvent& event)
  368. {
  369. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  370. #ifdef DRAG_DEBUG
  371. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  372. #endif
  373. m_drag_window = window.make_weak_ptr();;
  374. m_drag_origin = event.position();
  375. m_drag_window_origin = window.position();
  376. m_drag_start_rect = outer_window_rect(window.rect());
  377. window.set_is_being_dragged(true);
  378. invalidate(window);
  379. return;
  380. }
  381. }
  382. void WSWindowManager::handle_close_button_mouse_event(WSWindow& window, WSMouseEvent& event)
  383. {
  384. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  385. WSMessage message(WSMessage::WindowCloseRequest);
  386. window.on_message(message);
  387. return;
  388. }
  389. }
  390. void WSWindowManager::process_mouse_event(WSMouseEvent& event)
  391. {
  392. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Left) {
  393. if (m_drag_window) {
  394. #ifdef DRAG_DEBUG
  395. printf("[WM] Finish dragging WSWindow{%p}\n", m_drag_window.ptr());
  396. #endif
  397. invalidate(*m_drag_window);
  398. m_drag_window->set_is_being_dragged(false);
  399. m_drag_end_rect = outer_window_rect(m_drag_window->rect());
  400. m_drag_window = nullptr;
  401. return;
  402. }
  403. }
  404. if (event.type() == WSMessage::MouseMove) {
  405. if (m_drag_window) {
  406. auto old_window_rect = m_drag_window->rect();
  407. Point pos = m_drag_window_origin;
  408. #ifdef DRAG_DEBUG
  409. dbgprintf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_drag_origin.x(), m_drag_origin.y(), event.x(), event.y());
  410. #endif
  411. pos.move_by(event.x() - m_drag_origin.x(), event.y() - m_drag_origin.y());
  412. m_drag_window->set_position_without_repaint(pos);
  413. invalidate(outer_window_rect(old_window_rect));
  414. invalidate(outer_window_rect(m_drag_window->rect()));
  415. return;
  416. }
  417. }
  418. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  419. if (!window->global_cursor_tracking())
  420. continue;
  421. ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
  422. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  423. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  424. window->on_message(*local_event);
  425. }
  426. if (m_current_menubar && menubar_rect().contains(event.position())) {
  427. handle_menubar_mouse_event(*m_current_menubar, event);
  428. return;
  429. }
  430. // FIXME: Figure out an automatic menu dismissal logic that feels right.
  431. #if 0
  432. if (m_current_menu && event.type() == WSMouseEvent::MouseUp && event.button() == MouseButton::Left)
  433. close_current_menu();
  434. #endif
  435. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  436. if (!window->is_visible())
  437. continue;
  438. if (title_bar_rect(window->rect()).contains(event.position())) {
  439. if (event.type() == WSMessage::MouseDown) {
  440. move_to_front(*window);
  441. set_active_window(window);
  442. }
  443. if (close_button_rect_for_window(window->rect()).contains(event.position())) {
  444. handle_close_button_mouse_event(*window, event);
  445. return;
  446. }
  447. handle_titlebar_mouse_event(*window, event);
  448. return;
  449. }
  450. if (window->rect().contains(event.position())) {
  451. if (event.type() == WSMessage::MouseDown) {
  452. move_to_front(*window);
  453. set_active_window(window);
  454. }
  455. // FIXME: Should we just alter the coordinates of the existing MouseEvent and pass it through?
  456. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  457. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  458. window->on_message(*local_event);
  459. return;
  460. }
  461. }
  462. }
  463. void WSWindowManager::compose()
  464. {
  465. LOCKER(m_lock);
  466. auto dirty_rects = move(m_dirty_rects);
  467. auto cursor_location = m_screen.cursor_location();
  468. dirty_rects.append(m_last_cursor_rect);
  469. dirty_rects.append({ cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() });
  470. #ifdef DEBUG_COUNTERS
  471. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.size());
  472. dbgprintf("kmalloc stats: alloc:%u free:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_sum_eternal);
  473. #endif
  474. auto any_window_contains_rect = [this] (const Rect& r) {
  475. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  476. if (!window->is_visible())
  477. continue;
  478. if (outer_window_rect(window->rect()).contains(r))
  479. return true;
  480. }
  481. return false;
  482. };
  483. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  484. auto window_rect = outer_window_rect(window.rect());
  485. for (auto& dirty_rect : dirty_rects) {
  486. if (dirty_rect.intersects(window_rect))
  487. return true;
  488. }
  489. return false;
  490. };
  491. for (auto& dirty_rect : dirty_rects) {
  492. if (any_window_contains_rect(dirty_rect)) {
  493. continue;
  494. }
  495. LOCKER(m_wallpaper_path.lock());
  496. if (!m_wallpaper)
  497. m_back_painter->fill_rect(dirty_rect, m_background_color);
  498. else
  499. m_back_painter->blit(dirty_rect.location(), *m_wallpaper, dirty_rect);
  500. }
  501. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  502. if (!window->is_visible())
  503. continue;
  504. WSWindowLocker locker(*window);
  505. RetainPtr<GraphicsBitmap> backing = window->backing();
  506. if (!backing)
  507. continue;
  508. if (!any_dirty_rect_intersects_window(*window))
  509. continue;
  510. for (auto& dirty_rect : dirty_rects) {
  511. m_back_painter->set_clip_rect(dirty_rect);
  512. paint_window_frame(*window);
  513. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window->rect());
  514. if (dirty_rect_in_window_coordinates.is_empty())
  515. continue;
  516. dirty_rect_in_window_coordinates.set_x(dirty_rect_in_window_coordinates.x() - window->x());
  517. dirty_rect_in_window_coordinates.set_y(dirty_rect_in_window_coordinates.y() - window->y());
  518. auto dst = window->position();
  519. dst.move_by(dirty_rect_in_window_coordinates.location());
  520. m_back_painter->blit(dst, *backing, dirty_rect_in_window_coordinates);
  521. m_back_painter->clear_clip_rect();
  522. }
  523. m_back_painter->clear_clip_rect();
  524. }
  525. draw_menubar();
  526. draw_cursor();
  527. if (m_flash_flush.lock_and_copy()) {
  528. for (auto& rect : dirty_rects)
  529. m_front_painter->fill_rect(rect, Color::Yellow);
  530. }
  531. flip_buffers();
  532. for (auto& r : dirty_rects)
  533. flush(r);
  534. }
  535. void WSWindowManager::invalidate_cursor()
  536. {
  537. LOCKER(m_lock);
  538. auto cursor_location = m_screen.cursor_location();
  539. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  540. invalidate(cursor_rect);
  541. }
  542. Rect WSWindowManager::menubar_rect() const
  543. {
  544. return { 0, 0, m_screen_rect.width(), 16 };
  545. }
  546. void WSWindowManager::draw_menubar()
  547. {
  548. if (!m_current_menubar)
  549. return;
  550. m_back_painter->fill_rect(menubar_rect(), Color::LightGray);
  551. m_back_painter->draw_line({ 0, menubar_rect().bottom() }, { menubar_rect().right(), menubar_rect().bottom() }, Color::White);
  552. m_current_menubar->for_each_menu([&] (WSMenu& menu) {
  553. Color text_color = Color::Black;
  554. if (&menu == m_current_menu) {
  555. m_back_painter->fill_rect(menu.rect_in_menubar(), Color(0, 0, 104));
  556. text_color = Color::White;
  557. }
  558. m_back_painter->draw_text(menu.text_rect_in_menubar(), menu.name(), TextAlignment::CenterLeft, text_color);
  559. return true;
  560. });
  561. }
  562. void WSWindowManager::draw_cursor()
  563. {
  564. ASSERT_INTERRUPTS_ENABLED();
  565. LOCKER(m_lock);
  566. auto cursor_location = m_screen.cursor_location();
  567. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  568. Color inner_color = Color::White;
  569. Color outer_color = Color::Black;
  570. if (m_screen.left_mouse_button_pressed())
  571. swap(inner_color, outer_color);
  572. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_inner, inner_color);
  573. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_outer, outer_color);
  574. m_last_cursor_rect = cursor_rect;
  575. }
  576. void WSWindowManager::on_message(WSMessage& message)
  577. {
  578. ASSERT_INTERRUPTS_ENABLED();
  579. LOCKER(m_lock);
  580. if (message.is_mouse_event())
  581. return process_mouse_event(static_cast<WSMouseEvent&>(message));
  582. if (message.is_key_event()) {
  583. // FIXME: This is a good place to hook key events globally. :)
  584. if (m_active_window)
  585. return m_active_window->on_message(message);
  586. return;
  587. }
  588. if (message.type() == WSMessage::WM_DeferredCompose) {
  589. m_pending_compose_event = false;
  590. compose();
  591. return;
  592. }
  593. }
  594. void WSWindowManager::set_active_window(WSWindow* window)
  595. {
  596. LOCKER(m_lock);
  597. if (window == m_active_window.ptr())
  598. return;
  599. if (auto* previously_active_window = m_active_window.ptr()) {
  600. WSMessageLoop::the().post_message(previously_active_window, make<WSMessage>(WSMessage::WindowDeactivated));
  601. invalidate(*previously_active_window);
  602. }
  603. m_active_window = window->make_weak_ptr();
  604. if (m_active_window) {
  605. WSMessageLoop::the().post_message(m_active_window.ptr(), make<WSMessage>(WSMessage::WindowActivated));
  606. invalidate(*m_active_window);
  607. }
  608. }
  609. void WSWindowManager::invalidate()
  610. {
  611. LOCKER(m_lock);
  612. m_dirty_rects.clear_with_capacity();
  613. m_dirty_rects.append(m_screen_rect);
  614. }
  615. void WSWindowManager::invalidate(const Rect& a_rect)
  616. {
  617. LOCKER(m_lock);
  618. auto rect = Rect::intersection(a_rect, m_screen_rect);
  619. if (rect.is_empty())
  620. return;
  621. for (auto& r : m_dirty_rects) {
  622. if (r.contains(rect))
  623. return;
  624. if (r.intersects(rect)) {
  625. // Unite with the existing dirty rect.
  626. // FIXME: It would be much nicer to compute the exact rects needing repaint.
  627. r = r.united(rect);
  628. return;
  629. }
  630. }
  631. m_dirty_rects.append(rect);
  632. if (!m_pending_compose_event) {
  633. ASSERT_INTERRUPTS_ENABLED();
  634. WSMessageLoop::the().post_message(this, make<WSMessage>(WSMessage::WM_DeferredCompose));
  635. m_pending_compose_event = true;
  636. }
  637. }
  638. void WSWindowManager::invalidate(const WSWindow& window)
  639. {
  640. ASSERT_INTERRUPTS_ENABLED();
  641. LOCKER(m_lock);
  642. invalidate(outer_window_rect(window.rect()));
  643. }
  644. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  645. {
  646. if (rect.is_empty()) {
  647. invalidate(window);
  648. return;
  649. }
  650. ASSERT_INTERRUPTS_ENABLED();
  651. LOCKER(m_lock);
  652. auto outer_rect = outer_window_rect(window.rect());
  653. auto inner_rect = rect;
  654. inner_rect.move_by(window.position());
  655. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  656. inner_rect.intersect(outer_rect);
  657. invalidate(inner_rect);
  658. }
  659. void WSWindowManager::flush(const Rect& a_rect)
  660. {
  661. auto rect = Rect::intersection(a_rect, m_screen_rect);
  662. #ifdef DEBUG_COUNTERS
  663. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  664. #endif
  665. RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  666. RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  667. size_t pitch = m_back_bitmap->pitch();
  668. for (int y = 0; y < rect.height(); ++y) {
  669. fast_dword_copy(back_ptr, front_ptr, rect.width());
  670. front_ptr = (RGBA32*)((byte*)front_ptr + pitch);
  671. back_ptr = (RGBA32*)((byte*)back_ptr + pitch);
  672. }
  673. }