ClassicStylePainter.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Sarah Taube <metalflakecobaltpaint@gmail.com>
  4. * Copyright (c) 2021, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/StringView.h>
  9. #include <LibGfx/Bitmap.h>
  10. #include <LibGfx/CharacterBitmap.h>
  11. #include <LibGfx/ClassicStylePainter.h>
  12. #include <LibGfx/Painter.h>
  13. #include <LibGfx/Palette.h>
  14. namespace Gfx {
  15. void ClassicStylePainter::paint_tab_button(Painter& painter, IntRect const& rect, Palette const& palette, bool active, bool hovered, bool enabled, bool top, bool in_active_window)
  16. {
  17. Color base_color = palette.button();
  18. Color highlight_color2 = palette.threed_highlight();
  19. Color shadow_color1 = palette.threed_shadow1();
  20. Color shadow_color2 = palette.threed_shadow2();
  21. if (hovered && enabled && !active)
  22. base_color = palette.hover_highlight();
  23. PainterStateSaver saver(painter);
  24. painter.translate(rect.location());
  25. if (top) {
  26. // Base
  27. painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 1 }, base_color);
  28. // Top line
  29. if (active) {
  30. auto accent = palette.accent();
  31. if (!in_active_window)
  32. accent = accent.to_grayscale();
  33. painter.draw_line({ 3, 0 }, { rect.width() - 3, 0 }, accent.darkened());
  34. Gfx::IntRect accent_rect { 1, 1, rect.width() - 2, 2 };
  35. painter.fill_rect_with_gradient(accent_rect, accent, accent.lightened(1.5f));
  36. painter.set_pixel({ 2, 0 }, highlight_color2);
  37. } else {
  38. painter.draw_line({ 2, 0 }, { rect.width() - 3, 0 }, highlight_color2);
  39. }
  40. // Left side
  41. painter.draw_line({ 0, 2 }, { 0, rect.height() - 1 }, highlight_color2);
  42. painter.set_pixel({ 1, 1 }, highlight_color2);
  43. // Right side
  44. IntPoint top_right_outer { rect.width() - 1, 2 };
  45. IntPoint bottom_right_outer { rect.width() - 1, rect.height() - 1 };
  46. painter.draw_line(top_right_outer, bottom_right_outer, shadow_color2);
  47. IntPoint top_right_inner { rect.width() - 2, 2 };
  48. IntPoint bottom_right_inner { rect.width() - 2, rect.height() - 1 };
  49. painter.draw_line(top_right_inner, bottom_right_inner, shadow_color1);
  50. painter.set_pixel(rect.width() - 2, 1, shadow_color2);
  51. } else {
  52. // Base
  53. painter.fill_rect({ 0, 0, rect.width() - 1, rect.height() }, base_color);
  54. // Bottom line
  55. if (active) {
  56. auto accent = palette.accent();
  57. if (!in_active_window)
  58. accent = accent.to_grayscale();
  59. Gfx::IntRect accent_rect { 1, rect.height() - 3, rect.width() - 2, 2 };
  60. painter.fill_rect_with_gradient(accent_rect, accent, accent.lightened(1.5f));
  61. painter.draw_line({ 2, rect.height() - 1 }, { rect.width() - 3, rect.height() - 1 }, accent.darkened());
  62. } else {
  63. painter.draw_line({ 2, rect.height() - 1 }, { rect.width() - 3, rect.height() - 1 }, shadow_color2);
  64. }
  65. // Left side
  66. painter.draw_line({ 0, 0 }, { 0, rect.height() - 3 }, highlight_color2);
  67. painter.set_pixel({ 1, rect.height() - 2 }, highlight_color2);
  68. // Right side
  69. IntPoint top_right_outer { rect.width() - 1, 0 };
  70. IntPoint bottom_right_outer { rect.width() - 1, rect.height() - 3 };
  71. painter.draw_line(top_right_outer, bottom_right_outer, shadow_color2);
  72. IntPoint top_right_inner { rect.width() - 2, 0 };
  73. IntPoint bottom_right_inner { rect.width() - 2, rect.height() - 3 };
  74. painter.draw_line(top_right_inner, bottom_right_inner, shadow_color1);
  75. painter.set_pixel(rect.width() - 2, rect.height() - 2, shadow_color2);
  76. }
  77. }
  78. static void paint_button_new(Painter& painter, IntRect const& a_rect, Palette const& palette, ButtonStyle style, bool pressed, bool checked, bool hovered, bool enabled, bool focused)
  79. {
  80. Color button_color = palette.button();
  81. Color highlight_color = palette.threed_highlight();
  82. Color shadow_color1 = palette.threed_shadow1();
  83. Color shadow_color2 = palette.threed_shadow2();
  84. if (checked && enabled) {
  85. if (hovered)
  86. button_color = palette.hover_highlight();
  87. else
  88. button_color = palette.button();
  89. } else if (hovered && enabled)
  90. button_color = palette.hover_highlight();
  91. PainterStateSaver saver(painter);
  92. auto rect = a_rect;
  93. if (focused) {
  94. painter.draw_rect(a_rect, palette.threed_shadow2());
  95. rect.shrink(2, 2);
  96. }
  97. painter.translate(rect.location());
  98. if (pressed || checked) {
  99. // Base
  100. Gfx::IntRect base_rect { 1, 1, rect.width() - 2, rect.height() - 2 };
  101. if (checked && !pressed)
  102. painter.fill_rect_with_dither_pattern(base_rect, palette.button().lightened(1.3f), palette.button());
  103. else
  104. painter.fill_rect(base_rect, button_color);
  105. // Top shadow
  106. painter.draw_line({ 0, 0 }, { rect.width() - 2, 0 }, shadow_color2);
  107. painter.draw_line({ 0, 0 }, { 0, rect.height() - 2 }, shadow_color2);
  108. // Sunken shadow
  109. painter.draw_line({ 1, 1 }, { rect.width() - 3, 1 }, shadow_color1);
  110. painter.draw_line({ 1, 2 }, { 1, rect.height() - 3 }, shadow_color1);
  111. // Outer highlight
  112. painter.draw_line({ 0, rect.height() - 1 }, { rect.width() - 1, rect.height() - 1 }, highlight_color);
  113. painter.draw_line({ rect.width() - 1, 0 }, { rect.width() - 1, rect.height() - 2 }, highlight_color);
  114. // Inner highlight
  115. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, palette.button());
  116. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, palette.button());
  117. } else {
  118. // Base
  119. painter.fill_rect({ 0, 0, rect.width(), rect.height() }, button_color);
  120. // Top highlight
  121. if (style == ButtonStyle::Normal) {
  122. painter.draw_line({ 0, 0 }, { rect.width() - 2, 0 }, highlight_color);
  123. painter.draw_line({ 0, 0 }, { 0, rect.height() - 2 }, highlight_color);
  124. } else if (style == ButtonStyle::ThickCap) {
  125. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, highlight_color);
  126. painter.draw_line({ 1, 1 }, { 1, rect.height() - 2 }, highlight_color);
  127. }
  128. // Outer shadow
  129. painter.draw_line({ 0, rect.height() - 1 }, { rect.width() - 1, rect.height() - 1 }, shadow_color2);
  130. painter.draw_line({ rect.width() - 1, 0 }, { rect.width() - 1, rect.height() - 2 }, shadow_color2);
  131. // Inner shadow
  132. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color1);
  133. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color1);
  134. }
  135. }
  136. void ClassicStylePainter::paint_button(Painter& painter, IntRect const& rect, Palette const& palette, ButtonStyle button_style, bool pressed, bool hovered, bool checked, bool enabled, bool focused)
  137. {
  138. if (button_style == ButtonStyle::Normal || button_style == ButtonStyle::ThickCap)
  139. return paint_button_new(painter, rect, palette, button_style, pressed, checked, hovered, enabled, focused);
  140. if (button_style == ButtonStyle::Coolbar && !enabled)
  141. return;
  142. Color button_color = palette.button();
  143. Color highlight_color = palette.threed_highlight();
  144. Color shadow_color = button_style == ButtonStyle::Coolbar ? palette.threed_shadow1() : palette.threed_shadow2();
  145. PainterStateSaver saver(painter);
  146. painter.translate(rect.location());
  147. if (pressed || checked) {
  148. // Base
  149. IntRect base_rect { 1, 1, rect.width() - 2, rect.height() - 2 };
  150. if (button_style == ButtonStyle::Coolbar) {
  151. if (checked && !pressed) {
  152. painter.fill_rect_with_dither_pattern(base_rect, palette.button().lightened(1.3f), Color());
  153. } else {
  154. painter.fill_rect(base_rect, button_color);
  155. }
  156. }
  157. // Sunken shadow
  158. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color);
  159. painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, shadow_color);
  160. // Bottom highlight
  161. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, highlight_color);
  162. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, highlight_color);
  163. } else if (hovered) {
  164. if (button_style == ButtonStyle::Coolbar) {
  165. // Base
  166. painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
  167. }
  168. // Top highlight
  169. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, highlight_color);
  170. painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, highlight_color);
  171. // Bottom shadow
  172. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color);
  173. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color);
  174. }
  175. }
  176. void ClassicStylePainter::paint_frame(Painter& painter, IntRect const& rect, Palette const& palette, FrameShape shape, FrameShadow shadow, int thickness, bool skip_vertical_lines)
  177. {
  178. Color top_left_color;
  179. Color bottom_right_color;
  180. Color dark_shade = palette.threed_shadow1();
  181. Color light_shade = palette.threed_highlight();
  182. if (shape == FrameShape::Container && thickness >= 2) {
  183. if (shadow == FrameShadow::Raised) {
  184. dark_shade = palette.threed_shadow2();
  185. }
  186. }
  187. if (shadow == FrameShadow::Raised) {
  188. top_left_color = light_shade;
  189. bottom_right_color = dark_shade;
  190. } else if (shadow == FrameShadow::Sunken) {
  191. top_left_color = dark_shade;
  192. bottom_right_color = light_shade;
  193. } else if (shadow == FrameShadow::Plain) {
  194. top_left_color = dark_shade;
  195. bottom_right_color = dark_shade;
  196. }
  197. if (thickness >= 1) {
  198. painter.draw_line(rect.top_left(), rect.top_right(), top_left_color);
  199. painter.draw_line(rect.bottom_left(), rect.bottom_right(), bottom_right_color);
  200. if (shape != FrameShape::Panel || !skip_vertical_lines) {
  201. painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), top_left_color);
  202. painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), bottom_right_color);
  203. }
  204. }
  205. if (shape == FrameShape::Container && thickness >= 2) {
  206. Color top_left_color;
  207. Color bottom_right_color;
  208. Color dark_shade = palette.threed_shadow2();
  209. Color light_shade = palette.button();
  210. if (shadow == FrameShadow::Raised) {
  211. dark_shade = palette.threed_shadow1();
  212. top_left_color = light_shade;
  213. bottom_right_color = dark_shade;
  214. } else if (shadow == FrameShadow::Sunken) {
  215. top_left_color = dark_shade;
  216. bottom_right_color = light_shade;
  217. } else if (shadow == FrameShadow::Plain) {
  218. top_left_color = dark_shade;
  219. bottom_right_color = dark_shade;
  220. }
  221. IntRect inner_container_frame_rect = rect.shrunken(2, 2);
  222. painter.draw_line(inner_container_frame_rect.top_left(), inner_container_frame_rect.top_right(), top_left_color);
  223. painter.draw_line(inner_container_frame_rect.bottom_left(), inner_container_frame_rect.bottom_right(), bottom_right_color);
  224. painter.draw_line(inner_container_frame_rect.top_left().translated(0, 1), inner_container_frame_rect.bottom_left().translated(0, -1), top_left_color);
  225. painter.draw_line(inner_container_frame_rect.top_right(), inner_container_frame_rect.bottom_right().translated(0, -1), bottom_right_color);
  226. }
  227. if (shape == FrameShape::Box && thickness >= 2) {
  228. swap(top_left_color, bottom_right_color);
  229. IntRect inner_rect = rect.shrunken(2, 2);
  230. painter.draw_line(inner_rect.top_left(), inner_rect.top_right(), top_left_color);
  231. painter.draw_line(inner_rect.bottom_left(), inner_rect.bottom_right(), bottom_right_color);
  232. painter.draw_line(inner_rect.top_left().translated(0, 1), inner_rect.bottom_left().translated(0, -1), top_left_color);
  233. painter.draw_line(inner_rect.top_right(), inner_rect.bottom_right().translated(0, -1), bottom_right_color);
  234. }
  235. }
  236. void ClassicStylePainter::paint_window_frame(Painter& painter, IntRect const& rect, Palette const& palette)
  237. {
  238. Color base_color = palette.button();
  239. Color dark_shade = palette.threed_shadow2();
  240. Color mid_shade = palette.threed_shadow1();
  241. Color light_shade = palette.threed_highlight();
  242. auto border_thickness = palette.window_border_thickness();
  243. auto border_radius = palette.window_border_radius();
  244. if (border_radius > 0) {
  245. // FIXME: This will draw "useless" pixels that'll get drawn over by the window contents.
  246. // preferrably we should just remove the corner pixels from the completely drawn window
  247. // but I don't know how to do that yet. :^)
  248. painter.fill_rect_with_rounded_corners({ rect.x() - border_radius / 2,
  249. rect.y() - border_radius / 2,
  250. rect.width() + border_radius,
  251. rect.height() + border_radius },
  252. base_color, border_radius);
  253. return;
  254. }
  255. painter.draw_rect_with_thickness({ rect.x() + border_thickness / 2,
  256. rect.y() + border_thickness / 2,
  257. rect.width() - border_thickness,
  258. rect.height() - border_thickness },
  259. base_color, border_thickness);
  260. painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left(), base_color);
  261. painter.draw_line(rect.top_left().translated(1, 1), rect.top_right().translated(-1, 1), light_shade);
  262. painter.draw_line(rect.top_left().translated(1, 1), rect.bottom_left().translated(1, -1), light_shade);
  263. painter.draw_line(rect.top_left().translated(2, 2), rect.top_right().translated(-2, 2), base_color);
  264. painter.draw_line(rect.top_left().translated(2, 2), rect.bottom_left().translated(2, -2), base_color);
  265. painter.draw_line(rect.top_left().translated(3, 3), rect.top_right().translated(-3, 3), base_color);
  266. painter.draw_line(rect.top_left().translated(3, 3), rect.bottom_left().translated(3, -3), base_color);
  267. painter.draw_line(rect.top_right(), rect.bottom_right(), dark_shade);
  268. painter.draw_line(rect.top_right().translated(-1, 1), rect.bottom_right().translated(-1, -1), mid_shade);
  269. painter.draw_line(rect.top_right().translated(-2, 2), rect.bottom_right().translated(-2, -2), base_color);
  270. painter.draw_line(rect.top_right().translated(-3, 3), rect.bottom_right().translated(-3, -3), base_color);
  271. painter.draw_line(rect.bottom_left(), rect.bottom_right(), dark_shade);
  272. painter.draw_line(rect.bottom_left().translated(1, -1), rect.bottom_right().translated(-1, -1), mid_shade);
  273. painter.draw_line(rect.bottom_left().translated(2, -2), rect.bottom_right().translated(-2, -2), base_color);
  274. painter.draw_line(rect.bottom_left().translated(3, -3), rect.bottom_right().translated(-3, -3), base_color);
  275. }
  276. void ClassicStylePainter::paint_progressbar(Painter& painter, IntRect const& rect, Palette const& palette, int min, int max, int value, StringView text, Orientation orientation)
  277. {
  278. // First we fill the entire widget with the gradient. This incurs a bit of
  279. // overdraw but ensures a consistent look throughout the progression.
  280. Color start_color = palette.active_window_border1();
  281. Color end_color = palette.active_window_border2();
  282. painter.fill_rect_with_gradient(orientation, rect, start_color, end_color);
  283. if (!text.is_null()) {
  284. painter.draw_text(rect.translated(1, 1), text, TextAlignment::Center, palette.base_text());
  285. painter.draw_text(rect, text, TextAlignment::Center, palette.base_text().inverted());
  286. }
  287. float range_size = max - min;
  288. float progress = (value - min) / range_size;
  289. // Then we carve out a hole in the remaining part of the widget.
  290. // We draw the text a third time, clipped and inverse, for sharp contrast.
  291. IntRect hole_rect;
  292. if (orientation == Orientation::Horizontal) {
  293. float progress_width = progress * rect.width();
  294. hole_rect = { (int)progress_width, 0, (int)(rect.width() - progress_width), rect.height() };
  295. } else {
  296. float progress_height = progress * rect.height();
  297. hole_rect = { 0, 0, rect.width(), (int)(rect.height() - progress_height) };
  298. }
  299. hole_rect.translate_by(rect.location());
  300. hole_rect.set_right_without_resize(rect.right());
  301. PainterStateSaver saver(painter);
  302. painter.fill_rect(hole_rect, palette.base());
  303. painter.add_clip_rect(hole_rect);
  304. if (!text.is_null())
  305. painter.draw_text(rect.translated(0, 0), text, TextAlignment::Center, palette.base_text());
  306. }
  307. static RefPtr<Gfx::Bitmap> s_unfilled_circle_bitmap;
  308. static RefPtr<Gfx::Bitmap> s_filled_circle_bitmap;
  309. static RefPtr<Gfx::Bitmap> s_changing_filled_circle_bitmap;
  310. static RefPtr<Gfx::Bitmap> s_changing_unfilled_circle_bitmap;
  311. static Gfx::Bitmap const& circle_bitmap(bool checked, bool changing)
  312. {
  313. if (changing)
  314. return checked ? *s_changing_filled_circle_bitmap : *s_changing_unfilled_circle_bitmap;
  315. return checked ? *s_filled_circle_bitmap : *s_unfilled_circle_bitmap;
  316. }
  317. void ClassicStylePainter::paint_radio_button(Painter& painter, IntRect const& rect, Palette const&, bool is_checked, bool is_being_pressed)
  318. {
  319. if (!s_unfilled_circle_bitmap) {
  320. s_unfilled_circle_bitmap = Bitmap::try_load_from_file("/res/icons/serenity/unfilled-radio-circle.png").release_value_but_fixme_should_propagate_errors();
  321. s_filled_circle_bitmap = Bitmap::try_load_from_file("/res/icons/serenity/filled-radio-circle.png").release_value_but_fixme_should_propagate_errors();
  322. s_changing_filled_circle_bitmap = Bitmap::try_load_from_file("/res/icons/serenity/changing-filled-radio-circle.png").release_value_but_fixme_should_propagate_errors();
  323. s_changing_unfilled_circle_bitmap = Bitmap::try_load_from_file("/res/icons/serenity/changing-unfilled-radio-circle.png").release_value_but_fixme_should_propagate_errors();
  324. }
  325. auto& bitmap = circle_bitmap(is_checked, is_being_pressed);
  326. painter.blit(rect.location(), bitmap, bitmap.rect());
  327. }
  328. static char const* s_checked_bitmap_data = {
  329. " "
  330. " # "
  331. " ## "
  332. " ### "
  333. " ## ### "
  334. " ##### "
  335. " ### "
  336. " # "
  337. " "
  338. };
  339. static Gfx::CharacterBitmap* s_checked_bitmap;
  340. static int const s_checked_bitmap_width = 9;
  341. static int const s_checked_bitmap_height = 9;
  342. void ClassicStylePainter::paint_check_box(Painter& painter, IntRect const& rect, Palette const& palette, bool is_enabled, bool is_checked, bool is_being_pressed)
  343. {
  344. painter.fill_rect(rect, is_enabled ? palette.base() : palette.window());
  345. paint_frame(painter, rect, palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  346. if (is_being_pressed) {
  347. // FIXME: This color should not be hard-coded.
  348. painter.draw_rect(rect.shrunken(4, 4), Color::MidGray);
  349. }
  350. if (is_checked) {
  351. if (!s_checked_bitmap)
  352. s_checked_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
  353. painter.draw_bitmap(rect.shrunken(4, 4).location(), *s_checked_bitmap, is_enabled ? palette.base_text() : palette.threed_shadow1());
  354. }
  355. }
  356. void ClassicStylePainter::paint_transparency_grid(Painter& painter, IntRect const& rect, Palette const& palette)
  357. {
  358. painter.fill_rect_with_checkerboard(rect, { 8, 8 }, palette.base().darkened(0.9), palette.base());
  359. }
  360. void ClassicStylePainter::paint_simple_rect_shadow(Painter& painter, IntRect const& containing_rect, Bitmap const& shadow_bitmap, bool shadow_includes_frame, bool fill_content)
  361. {
  362. // The layout of the shadow_bitmap is defined like this:
  363. // +---------+----+---------+----+----+----+
  364. // | TL | T | TR | LT | L | LB |
  365. // +---------+----+---------+----+----+----+
  366. // | BL | B | BR | RT | R | RB |
  367. // +---------+----+---------+----+----+----+
  368. // Located strictly on the top or bottom of the rectangle, above or below of the content:
  369. // TL = top-left T = top TR = top-right
  370. // BL = bottom-left B = bottom BR = bottom-right
  371. // Located on the left or right of the rectangle, but not above or below of the content:
  372. // LT = left-top L = left LB = left-bottom
  373. // RT = right-top R = right RB = right-bottom
  374. // So, the bitmap has two rows and 6 column, two of which are twice as wide.
  375. // The height divided by two defines a cell size, and width of each
  376. // column must be the same as the height of the cell, except for the
  377. // first and third column, which are twice as wide.
  378. // If fill_content is true, it will use the RGBA color of right-bottom pixel of TL to fill the rectangle enclosed
  379. if (shadow_bitmap.height() % 2 != 0) {
  380. dbgln("Can't paint simple rect shadow, shadow bitmap height {} is not even", shadow_bitmap.height());
  381. return;
  382. }
  383. auto base_size = shadow_bitmap.height() / 2;
  384. if (shadow_bitmap.width() != base_size * (6 + 2)) {
  385. if (shadow_bitmap.width() % base_size != 0)
  386. dbgln("Can't paint simple rect shadow, shadow bitmap width {} is not a multiple of {}", shadow_bitmap.width(), base_size);
  387. else
  388. dbgln("Can't paint simple rect shadow, shadow bitmap width {} but expected {}", shadow_bitmap.width(), base_size * (6 + 2));
  389. return;
  390. }
  391. // The containing_rect should have been inflated appropriately
  392. VERIFY(containing_rect.size().contains(Gfx::IntSize { base_size, base_size }));
  393. auto sides_height = containing_rect.height() - 2 * base_size;
  394. auto half_height = sides_height / 2;
  395. auto containing_horizontal_rect = containing_rect;
  396. int horizontal_shift = 0;
  397. if (half_height < base_size && !shadow_includes_frame) {
  398. // If the height is too small we need to shift the left/right accordingly, unless the shadow includes portions of the frame
  399. horizontal_shift = base_size - half_height;
  400. containing_horizontal_rect.set_left(containing_horizontal_rect.left() + horizontal_shift);
  401. containing_horizontal_rect.set_right(containing_horizontal_rect.right() - 2 * horizontal_shift);
  402. }
  403. auto half_width = containing_horizontal_rect.width() / 2;
  404. int corner_piece_width = min(containing_horizontal_rect.width() / 2, base_size * 2);
  405. int left_corners_right = containing_horizontal_rect.left() + corner_piece_width;
  406. int right_corners_left = max(containing_horizontal_rect.right() - corner_piece_width + 1, left_corners_right + 1);
  407. auto paint_horizontal = [&](int y, int src_row) {
  408. if (half_width <= 0)
  409. return;
  410. Gfx::PainterStateSaver save(painter);
  411. painter.add_clip_rect({ containing_horizontal_rect.left(), y, containing_horizontal_rect.width(), base_size });
  412. painter.blit({ containing_horizontal_rect.left(), y }, shadow_bitmap, { 0, src_row * base_size, corner_piece_width, base_size });
  413. painter.blit({ right_corners_left, y }, shadow_bitmap, { 5 * base_size - corner_piece_width, src_row * base_size, corner_piece_width, base_size });
  414. for (int x = left_corners_right; x < right_corners_left; x += base_size) {
  415. auto width = min(right_corners_left - x, base_size);
  416. painter.blit({ x, y }, shadow_bitmap, { corner_piece_width, src_row * base_size, width, base_size });
  417. }
  418. };
  419. paint_horizontal(containing_rect.top(), 0);
  420. paint_horizontal(containing_rect.bottom() - base_size + 1, 1);
  421. int corner_piece_height = min(half_height, base_size);
  422. int top_corners_bottom = base_size + corner_piece_height;
  423. int bottom_corners_top = base_size + max(half_height, sides_height - corner_piece_height);
  424. auto paint_vertical = [&](int x, int src_row, int hshift, int hsrcshift) {
  425. Gfx::PainterStateSaver save(painter);
  426. painter.add_clip_rect({ x, containing_rect.y() + base_size, base_size, containing_rect.height() - 2 * base_size });
  427. painter.blit({ x + hshift, containing_rect.top() + top_corners_bottom - corner_piece_height }, shadow_bitmap, { base_size * 5 + hsrcshift, src_row * base_size, base_size - hsrcshift, corner_piece_height });
  428. painter.blit({ x + hshift, containing_rect.top() + bottom_corners_top }, shadow_bitmap, { base_size * 7 + hsrcshift, src_row * base_size + base_size - corner_piece_height, base_size - hsrcshift, corner_piece_height });
  429. for (int y = top_corners_bottom; y < bottom_corners_top; y += base_size) {
  430. auto height = min(bottom_corners_top - y, base_size);
  431. painter.blit({ x, containing_rect.top() + y }, shadow_bitmap, { base_size * 6, src_row * base_size, base_size, height });
  432. }
  433. };
  434. paint_vertical(containing_rect.left(), 0, horizontal_shift, 0);
  435. if (shadow_includes_frame)
  436. horizontal_shift = 0; // TODO: fix off-by-one on rectangles barely wide enough
  437. paint_vertical(containing_rect.right() - base_size + 1, 1, 0, horizontal_shift);
  438. if (fill_content) {
  439. // Fill the enclosed rectangle with the RGBA color of the right-bottom pixel of the TL tile
  440. auto inner_rect = containing_rect.shrunken(2 * base_size, 2 * base_size);
  441. if (!inner_rect.is_empty())
  442. painter.fill_rect(inner_rect, shadow_bitmap.get_pixel(2 * base_size - 1, base_size - 1));
  443. }
  444. }
  445. }