WSWindowManager.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. if (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left) {
  335. dbgprintf("[WM] MouseDown on menu '%s'\n", menu.name().characters());
  336. if (m_current_menu == &menu)
  337. return;
  338. close_current_menu();
  339. auto& menu_window = menu.ensure_menu_window();
  340. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() });
  341. menu_window.set_visible(true);
  342. m_current_menu = &menu;
  343. return;
  344. }
  345. if (event.type() == WSMouseEvent::MouseUp && event.button() == MouseButton::Left) {
  346. close_current_menu();
  347. return;
  348. }
  349. }
  350. void WSWindowManager::close_current_menu()
  351. {
  352. if (m_current_menu && m_current_menu->menu_window())
  353. m_current_menu->menu_window()->set_visible(false);
  354. m_current_menu = nullptr;
  355. }
  356. void WSWindowManager::handle_menubar_mouse_event(WSMenuBar& menu, WSMouseEvent& event)
  357. {
  358. m_current_menubar->for_each_menu([&] (WSMenu& menu) {
  359. if (menu.rect_in_menubar().contains(event.position())) {
  360. handle_menu_mouse_event(menu, event);
  361. return false;
  362. }
  363. return true;
  364. });
  365. }
  366. void WSWindowManager::handle_titlebar_mouse_event(WSWindow& window, WSMouseEvent& event)
  367. {
  368. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  369. #ifdef DRAG_DEBUG
  370. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  371. #endif
  372. m_drag_window = window.make_weak_ptr();;
  373. m_drag_origin = event.position();
  374. m_drag_window_origin = window.position();
  375. m_drag_start_rect = outer_window_rect(window.rect());
  376. window.set_is_being_dragged(true);
  377. invalidate(window);
  378. return;
  379. }
  380. }
  381. void WSWindowManager::handle_close_button_mouse_event(WSWindow& window, WSMouseEvent& event)
  382. {
  383. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  384. WSMessage message(WSMessage::WindowCloseRequest);
  385. window.on_message(message);
  386. return;
  387. }
  388. }
  389. void WSWindowManager::process_mouse_event(WSMouseEvent& event)
  390. {
  391. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Left) {
  392. if (m_drag_window) {
  393. #ifdef DRAG_DEBUG
  394. printf("[WM] Finish dragging WSWindow{%p}\n", m_drag_window.ptr());
  395. #endif
  396. invalidate(*m_drag_window);
  397. m_drag_window->set_is_being_dragged(false);
  398. m_drag_end_rect = outer_window_rect(m_drag_window->rect());
  399. m_drag_window = nullptr;
  400. return;
  401. }
  402. }
  403. if (event.type() == WSMessage::MouseMove) {
  404. if (m_drag_window) {
  405. auto old_window_rect = m_drag_window->rect();
  406. Point pos = m_drag_window_origin;
  407. #ifdef DRAG_DEBUG
  408. dbgprintf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_drag_origin.x(), m_drag_origin.y(), event.x(), event.y());
  409. #endif
  410. pos.move_by(event.x() - m_drag_origin.x(), event.y() - m_drag_origin.y());
  411. m_drag_window->set_position_without_repaint(pos);
  412. invalidate(outer_window_rect(old_window_rect));
  413. invalidate(outer_window_rect(m_drag_window->rect()));
  414. return;
  415. }
  416. }
  417. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  418. if (!window->global_cursor_tracking())
  419. continue;
  420. ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
  421. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  422. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  423. window->on_message(*local_event);
  424. }
  425. if (m_current_menubar && menubar_rect().contains(event.position())) {
  426. handle_menubar_mouse_event(*m_current_menubar, event);
  427. return;
  428. }
  429. // FIXME: Figure out an automatic menu dismissal logic that feels right.
  430. #if 0
  431. if (m_current_menu && event.type() == WSMouseEvent::MouseUp && event.button() == MouseButton::Left)
  432. close_current_menu();
  433. #endif
  434. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  435. if (!window->is_visible())
  436. continue;
  437. if (title_bar_rect(window->rect()).contains(event.position())) {
  438. if (event.type() == WSMessage::MouseDown) {
  439. move_to_front(*window);
  440. set_active_window(window);
  441. }
  442. if (close_button_rect_for_window(window->rect()).contains(event.position())) {
  443. handle_close_button_mouse_event(*window, event);
  444. return;
  445. }
  446. handle_titlebar_mouse_event(*window, event);
  447. return;
  448. }
  449. if (window->rect().contains(event.position())) {
  450. if (event.type() == WSMessage::MouseDown) {
  451. move_to_front(*window);
  452. set_active_window(window);
  453. }
  454. // FIXME: Should we just alter the coordinates of the existing MouseEvent and pass it through?
  455. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  456. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  457. window->on_message(*local_event);
  458. return;
  459. }
  460. }
  461. }
  462. void WSWindowManager::compose()
  463. {
  464. LOCKER(m_lock);
  465. auto dirty_rects = move(m_dirty_rects);
  466. auto cursor_location = m_screen.cursor_location();
  467. dirty_rects.append(m_last_cursor_rect);
  468. dirty_rects.append({ cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() });
  469. #ifdef DEBUG_COUNTERS
  470. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.size());
  471. dbgprintf("kmalloc stats: alloc:%u free:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_sum_eternal);
  472. #endif
  473. auto any_window_contains_rect = [this] (const Rect& r) {
  474. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  475. if (!window->is_visible())
  476. continue;
  477. if (outer_window_rect(window->rect()).contains(r))
  478. return true;
  479. }
  480. return false;
  481. };
  482. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  483. auto window_rect = outer_window_rect(window.rect());
  484. for (auto& dirty_rect : dirty_rects) {
  485. if (dirty_rect.intersects(window_rect))
  486. return true;
  487. }
  488. return false;
  489. };
  490. for (auto& dirty_rect : dirty_rects) {
  491. if (any_window_contains_rect(dirty_rect)) {
  492. continue;
  493. }
  494. LOCKER(m_wallpaper_path.lock());
  495. if (!m_wallpaper)
  496. m_back_painter->fill_rect(dirty_rect, m_background_color);
  497. else
  498. m_back_painter->blit(dirty_rect.location(), *m_wallpaper, dirty_rect);
  499. }
  500. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  501. if (!window->is_visible())
  502. continue;
  503. WSWindowLocker locker(*window);
  504. RetainPtr<GraphicsBitmap> backing = window->backing();
  505. if (!backing)
  506. continue;
  507. if (!any_dirty_rect_intersects_window(*window))
  508. continue;
  509. for (auto& dirty_rect : dirty_rects) {
  510. m_back_painter->set_clip_rect(dirty_rect);
  511. paint_window_frame(*window);
  512. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window->rect());
  513. if (dirty_rect_in_window_coordinates.is_empty())
  514. continue;
  515. dirty_rect_in_window_coordinates.set_x(dirty_rect_in_window_coordinates.x() - window->x());
  516. dirty_rect_in_window_coordinates.set_y(dirty_rect_in_window_coordinates.y() - window->y());
  517. auto dst = window->position();
  518. dst.move_by(dirty_rect_in_window_coordinates.location());
  519. m_back_painter->blit(dst, *backing, dirty_rect_in_window_coordinates);
  520. m_back_painter->clear_clip_rect();
  521. }
  522. m_back_painter->clear_clip_rect();
  523. }
  524. draw_menubar();
  525. draw_cursor();
  526. if (m_flash_flush.lock_and_copy()) {
  527. for (auto& rect : dirty_rects)
  528. m_front_painter->fill_rect(rect, Color::Yellow);
  529. }
  530. flip_buffers();
  531. for (auto& r : dirty_rects)
  532. flush(r);
  533. }
  534. void WSWindowManager::invalidate_cursor()
  535. {
  536. LOCKER(m_lock);
  537. auto cursor_location = m_screen.cursor_location();
  538. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  539. invalidate(cursor_rect);
  540. }
  541. Rect WSWindowManager::menubar_rect() const
  542. {
  543. return { 0, 0, m_screen_rect.width(), 16 };
  544. }
  545. void WSWindowManager::draw_menubar()
  546. {
  547. if (!m_current_menubar)
  548. return;
  549. m_back_painter->fill_rect(menubar_rect(), Color::LightGray);
  550. m_back_painter->draw_line({ 0, menubar_rect().bottom() }, { menubar_rect().right(), menubar_rect().bottom() }, Color::White);
  551. m_current_menubar->for_each_menu([&] (WSMenu& menu) {
  552. Color text_color = Color::Black;
  553. if (&menu == m_current_menu) {
  554. m_back_painter->fill_rect(menu.rect_in_menubar(), Color(0, 0, 104));
  555. text_color = Color::White;
  556. }
  557. m_back_painter->draw_text(menu.text_rect_in_menubar(), menu.name(), TextAlignment::CenterLeft, text_color);
  558. return true;
  559. });
  560. }
  561. void WSWindowManager::draw_cursor()
  562. {
  563. ASSERT_INTERRUPTS_ENABLED();
  564. LOCKER(m_lock);
  565. auto cursor_location = m_screen.cursor_location();
  566. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  567. Color inner_color = Color::White;
  568. Color outer_color = Color::Black;
  569. if (m_screen.left_mouse_button_pressed())
  570. swap(inner_color, outer_color);
  571. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_inner, inner_color);
  572. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_outer, outer_color);
  573. m_last_cursor_rect = cursor_rect;
  574. }
  575. void WSWindowManager::on_message(WSMessage& message)
  576. {
  577. ASSERT_INTERRUPTS_ENABLED();
  578. LOCKER(m_lock);
  579. if (message.is_mouse_event())
  580. return process_mouse_event(static_cast<WSMouseEvent&>(message));
  581. if (message.is_key_event()) {
  582. // FIXME: This is a good place to hook key events globally. :)
  583. if (m_active_window)
  584. return m_active_window->on_message(message);
  585. return;
  586. }
  587. if (message.type() == WSMessage::WM_DeferredCompose) {
  588. m_pending_compose_event = false;
  589. compose();
  590. return;
  591. }
  592. }
  593. void WSWindowManager::set_active_window(WSWindow* window)
  594. {
  595. LOCKER(m_lock);
  596. if (window == m_active_window.ptr())
  597. return;
  598. if (auto* previously_active_window = m_active_window.ptr()) {
  599. WSMessageLoop::the().post_message(previously_active_window, make<WSMessage>(WSMessage::WindowDeactivated));
  600. invalidate(*previously_active_window);
  601. }
  602. m_active_window = window->make_weak_ptr();
  603. if (m_active_window) {
  604. WSMessageLoop::the().post_message(m_active_window.ptr(), make<WSMessage>(WSMessage::WindowActivated));
  605. invalidate(*m_active_window);
  606. }
  607. }
  608. void WSWindowManager::invalidate()
  609. {
  610. LOCKER(m_lock);
  611. m_dirty_rects.clear_with_capacity();
  612. m_dirty_rects.append(m_screen_rect);
  613. }
  614. void WSWindowManager::invalidate(const Rect& a_rect)
  615. {
  616. LOCKER(m_lock);
  617. auto rect = Rect::intersection(a_rect, m_screen_rect);
  618. if (rect.is_empty())
  619. return;
  620. for (auto& r : m_dirty_rects) {
  621. if (r.contains(rect))
  622. return;
  623. if (r.intersects(rect)) {
  624. // Unite with the existing dirty rect.
  625. // FIXME: It would be much nicer to compute the exact rects needing repaint.
  626. r = r.united(rect);
  627. return;
  628. }
  629. }
  630. m_dirty_rects.append(rect);
  631. if (!m_pending_compose_event) {
  632. ASSERT_INTERRUPTS_ENABLED();
  633. WSMessageLoop::the().post_message(this, make<WSMessage>(WSMessage::WM_DeferredCompose));
  634. m_pending_compose_event = true;
  635. }
  636. }
  637. void WSWindowManager::invalidate(const WSWindow& window)
  638. {
  639. ASSERT_INTERRUPTS_ENABLED();
  640. LOCKER(m_lock);
  641. invalidate(outer_window_rect(window.rect()));
  642. }
  643. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  644. {
  645. if (rect.is_empty()) {
  646. invalidate(window);
  647. return;
  648. }
  649. ASSERT_INTERRUPTS_ENABLED();
  650. LOCKER(m_lock);
  651. auto outer_rect = outer_window_rect(window.rect());
  652. auto inner_rect = rect;
  653. inner_rect.move_by(window.position());
  654. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  655. inner_rect.intersect(outer_rect);
  656. invalidate(inner_rect);
  657. }
  658. void WSWindowManager::flush(const Rect& a_rect)
  659. {
  660. auto rect = Rect::intersection(a_rect, m_screen_rect);
  661. #ifdef DEBUG_COUNTERS
  662. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  663. #endif
  664. RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  665. RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  666. size_t pitch = m_back_bitmap->pitch();
  667. for (int y = 0; y < rect.height(); ++y) {
  668. fast_dword_copy(back_ptr, front_ptr, rect.width());
  669. front_ptr = (RGBA32*)((byte*)front_ptr + pitch);
  670. back_ptr = (RGBA32*)((byte*)back_ptr + pitch);
  671. }
  672. }