WSWindowManager.cpp 29 KB

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