ClassicStylePainter.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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, bool default_button)
  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 || default_button) {
  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, bool default_button)
  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, default_button);
  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. if (shape == Gfx::FrameShape::NoFrame)
  179. return;
  180. if (shape == FrameShape::Window) {
  181. StylePainter::paint_window_frame(painter, rect, palette);
  182. return;
  183. }
  184. Color top_left_color;
  185. Color bottom_right_color;
  186. Color dark_shade = palette.threed_shadow1();
  187. Color light_shade = palette.threed_highlight();
  188. if (shape == FrameShape::Container && thickness >= 2) {
  189. if (shadow == FrameShadow::Raised) {
  190. dark_shade = palette.threed_shadow2();
  191. }
  192. }
  193. if (shadow == FrameShadow::Raised) {
  194. top_left_color = light_shade;
  195. bottom_right_color = dark_shade;
  196. } else if (shadow == FrameShadow::Sunken) {
  197. top_left_color = dark_shade;
  198. bottom_right_color = light_shade;
  199. } else if (shadow == FrameShadow::Plain) {
  200. top_left_color = dark_shade;
  201. bottom_right_color = dark_shade;
  202. }
  203. if (thickness >= 1) {
  204. painter.draw_line(rect.top_left(), rect.top_right(), top_left_color);
  205. painter.draw_line(rect.bottom_left(), rect.bottom_right(), bottom_right_color);
  206. if (shape != FrameShape::Panel || !skip_vertical_lines) {
  207. painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), top_left_color);
  208. painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), bottom_right_color);
  209. }
  210. }
  211. if (shape == FrameShape::Container && thickness >= 2) {
  212. Color top_left_color;
  213. Color bottom_right_color;
  214. Color dark_shade = palette.threed_shadow2();
  215. Color light_shade = palette.button();
  216. if (shadow == FrameShadow::Raised) {
  217. dark_shade = palette.threed_shadow1();
  218. top_left_color = light_shade;
  219. bottom_right_color = dark_shade;
  220. } else if (shadow == FrameShadow::Sunken) {
  221. top_left_color = dark_shade;
  222. bottom_right_color = light_shade;
  223. } else if (shadow == FrameShadow::Plain) {
  224. top_left_color = dark_shade;
  225. bottom_right_color = dark_shade;
  226. }
  227. IntRect inner_container_frame_rect = rect.shrunken(2, 2);
  228. painter.draw_line(inner_container_frame_rect.top_left(), inner_container_frame_rect.top_right(), top_left_color);
  229. painter.draw_line(inner_container_frame_rect.bottom_left(), inner_container_frame_rect.bottom_right(), bottom_right_color);
  230. painter.draw_line(inner_container_frame_rect.top_left().translated(0, 1), inner_container_frame_rect.bottom_left().translated(0, -1), top_left_color);
  231. painter.draw_line(inner_container_frame_rect.top_right(), inner_container_frame_rect.bottom_right().translated(0, -1), bottom_right_color);
  232. }
  233. if (shape == FrameShape::Box && thickness >= 2) {
  234. swap(top_left_color, bottom_right_color);
  235. IntRect inner_rect = rect.shrunken(2, 2);
  236. painter.draw_line(inner_rect.top_left(), inner_rect.top_right(), top_left_color);
  237. painter.draw_line(inner_rect.bottom_left(), inner_rect.bottom_right(), bottom_right_color);
  238. painter.draw_line(inner_rect.top_left().translated(0, 1), inner_rect.bottom_left().translated(0, -1), top_left_color);
  239. painter.draw_line(inner_rect.top_right(), inner_rect.bottom_right().translated(0, -1), bottom_right_color);
  240. }
  241. }
  242. void ClassicStylePainter::paint_window_frame(Painter& painter, IntRect const& rect, Palette const& palette)
  243. {
  244. Color base_color = palette.button();
  245. Color dark_shade = palette.threed_shadow2();
  246. Color mid_shade = palette.threed_shadow1();
  247. Color light_shade = palette.threed_highlight();
  248. auto border_thickness = palette.window_border_thickness();
  249. auto border_radius = palette.window_border_radius();
  250. if (border_radius > 0) {
  251. // FIXME: This will draw "useless" pixels that'll get drawn over by the window contents.
  252. // preferrably we should just remove the corner pixels from the completely drawn window
  253. // but I don't know how to do that yet. :^)
  254. painter.fill_rect_with_rounded_corners(rect, base_color, border_radius);
  255. return;
  256. }
  257. painter.draw_rect_with_thickness({ rect.x() + border_thickness / 2,
  258. rect.y() + border_thickness / 2,
  259. rect.width() - border_thickness,
  260. rect.height() - border_thickness },
  261. base_color, border_thickness);
  262. painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left(), base_color);
  263. painter.draw_line(rect.top_left().translated(1, 1), rect.top_right().translated(-1, 1), light_shade);
  264. painter.draw_line(rect.top_left().translated(1, 1), rect.bottom_left().translated(1, -1), light_shade);
  265. painter.draw_line(rect.top_left().translated(2, 2), rect.top_right().translated(-2, 2), base_color);
  266. painter.draw_line(rect.top_left().translated(2, 2), rect.bottom_left().translated(2, -2), base_color);
  267. painter.draw_line(rect.top_left().translated(3, 3), rect.top_right().translated(-3, 3), base_color);
  268. painter.draw_line(rect.top_left().translated(3, 3), rect.bottom_left().translated(3, -3), base_color);
  269. painter.draw_line(rect.top_right(), rect.bottom_right(), dark_shade);
  270. painter.draw_line(rect.top_right().translated(-1, 1), rect.bottom_right().translated(-1, -1), mid_shade);
  271. painter.draw_line(rect.top_right().translated(-2, 2), rect.bottom_right().translated(-2, -2), base_color);
  272. painter.draw_line(rect.top_right().translated(-3, 3), rect.bottom_right().translated(-3, -3), base_color);
  273. painter.draw_line(rect.bottom_left(), rect.bottom_right(), dark_shade);
  274. painter.draw_line(rect.bottom_left().translated(1, -1), rect.bottom_right().translated(-1, -1), mid_shade);
  275. painter.draw_line(rect.bottom_left().translated(2, -2), rect.bottom_right().translated(-2, -2), base_color);
  276. painter.draw_line(rect.bottom_left().translated(3, -3), rect.bottom_right().translated(-3, -3), base_color);
  277. }
  278. void ClassicStylePainter::paint_progressbar(Painter& painter, IntRect const& rect, Palette const& palette, int min, int max, int value, StringView text, Orientation orientation)
  279. {
  280. // First we fill the entire widget with the gradient. This incurs a bit of
  281. // overdraw but ensures a consistent look throughout the progression.
  282. Color start_color = palette.active_window_border1();
  283. Color end_color = palette.active_window_border2();
  284. painter.fill_rect_with_gradient(orientation, rect, start_color, end_color);
  285. if (!text.is_null()) {
  286. painter.draw_text(rect.translated(1, 1), text, TextAlignment::Center, palette.base_text());
  287. painter.draw_text(rect, text, TextAlignment::Center, palette.base_text().inverted());
  288. }
  289. float range_size = max - min;
  290. float progress = (value - min) / range_size;
  291. // Then we carve out a hole in the remaining part of the widget.
  292. // We draw the text a third time, clipped and inverse, for sharp contrast.
  293. IntRect hole_rect;
  294. if (orientation == Orientation::Horizontal) {
  295. float progress_width = progress * rect.width();
  296. hole_rect = { (int)progress_width, 0, (int)(rect.width() - progress_width), rect.height() };
  297. } else {
  298. float progress_height = progress * rect.height();
  299. hole_rect = { 0, 0, rect.width(), (int)(rect.height() - progress_height) };
  300. }
  301. hole_rect.translate_by(rect.location());
  302. hole_rect.set_right_without_resize(rect.right());
  303. PainterStateSaver saver(painter);
  304. painter.fill_rect(hole_rect, palette.base());
  305. painter.add_clip_rect(hole_rect);
  306. if (!text.is_null())
  307. painter.draw_text(rect.translated(0, 0), text, TextAlignment::Center, palette.base_text());
  308. }
  309. static RefPtr<Gfx::Bitmap> s_unfilled_circle_bitmap;
  310. static RefPtr<Gfx::Bitmap> s_filled_circle_bitmap;
  311. static RefPtr<Gfx::Bitmap> s_changing_filled_circle_bitmap;
  312. static RefPtr<Gfx::Bitmap> s_changing_unfilled_circle_bitmap;
  313. static Gfx::Bitmap const& circle_bitmap(bool checked, bool changing)
  314. {
  315. if (changing)
  316. return checked ? *s_changing_filled_circle_bitmap : *s_changing_unfilled_circle_bitmap;
  317. return checked ? *s_filled_circle_bitmap : *s_unfilled_circle_bitmap;
  318. }
  319. void ClassicStylePainter::paint_radio_button(Painter& painter, IntRect const& rect, Palette const&, bool is_checked, bool is_being_pressed)
  320. {
  321. if (!s_unfilled_circle_bitmap) {
  322. s_unfilled_circle_bitmap = Bitmap::try_load_from_file("/res/icons/serenity/unfilled-radio-circle.png").release_value_but_fixme_should_propagate_errors();
  323. s_filled_circle_bitmap = Bitmap::try_load_from_file("/res/icons/serenity/filled-radio-circle.png").release_value_but_fixme_should_propagate_errors();
  324. 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();
  325. 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();
  326. }
  327. auto& bitmap = circle_bitmap(is_checked, is_being_pressed);
  328. painter.blit(rect.location(), bitmap, bitmap.rect());
  329. }
  330. static char const* s_checked_bitmap_data = {
  331. " "
  332. " # "
  333. " ## "
  334. " ### "
  335. " ## ### "
  336. " ##### "
  337. " ### "
  338. " # "
  339. " "
  340. };
  341. static Gfx::CharacterBitmap* s_checked_bitmap;
  342. static int const s_checked_bitmap_width = 9;
  343. static int const s_checked_bitmap_height = 9;
  344. void ClassicStylePainter::paint_check_box(Painter& painter, IntRect const& rect, Palette const& palette, bool is_enabled, bool is_checked, bool is_being_pressed)
  345. {
  346. painter.fill_rect(rect, is_enabled ? palette.base() : palette.window());
  347. paint_frame(painter, rect, palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  348. if (is_being_pressed) {
  349. // FIXME: This color should not be hard-coded.
  350. painter.draw_rect(rect.shrunken(4, 4), Color::MidGray);
  351. }
  352. if (is_checked) {
  353. if (!s_checked_bitmap)
  354. s_checked_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
  355. painter.draw_bitmap(rect.shrunken(4, 4).location(), *s_checked_bitmap, is_enabled ? palette.base_text() : palette.threed_shadow1());
  356. }
  357. }
  358. void ClassicStylePainter::paint_transparency_grid(Painter& painter, IntRect const& rect, Palette const& palette)
  359. {
  360. painter.fill_rect_with_checkerboard(rect, { 8, 8 }, palette.base().darkened(0.9), palette.base());
  361. }
  362. void ClassicStylePainter::paint_simple_rect_shadow(Painter& painter, IntRect const& containing_rect, Bitmap const& shadow_bitmap, bool shadow_includes_frame, bool fill_content)
  363. {
  364. // The layout of the shadow_bitmap is defined like this:
  365. // +---------+----+---------+----+----+----+
  366. // | TL | T | TR | LT | L | LB |
  367. // +---------+----+---------+----+----+----+
  368. // | BL | B | BR | RT | R | RB |
  369. // +---------+----+---------+----+----+----+
  370. // Located strictly on the top or bottom of the rectangle, above or below of the content:
  371. // TL = top-left T = top TR = top-right
  372. // BL = bottom-left B = bottom BR = bottom-right
  373. // Located on the left or right of the rectangle, but not above or below of the content:
  374. // LT = left-top L = left LB = left-bottom
  375. // RT = right-top R = right RB = right-bottom
  376. // So, the bitmap has two rows and 6 column, two of which are twice as wide.
  377. // The height divided by two defines a cell size, and width of each
  378. // column must be the same as the height of the cell, except for the
  379. // first and third column, which are twice as wide.
  380. // If fill_content is true, it will use the RGBA color of right-bottom pixel of TL to fill the rectangle enclosed
  381. if (shadow_bitmap.height() % 2 != 0) {
  382. dbgln("Can't paint simple rect shadow, shadow bitmap height {} is not even", shadow_bitmap.height());
  383. return;
  384. }
  385. auto base_size = shadow_bitmap.height() / 2;
  386. if (shadow_bitmap.width() != base_size * (6 + 2)) {
  387. if (shadow_bitmap.width() % base_size != 0)
  388. dbgln("Can't paint simple rect shadow, shadow bitmap width {} is not a multiple of {}", shadow_bitmap.width(), base_size);
  389. else
  390. dbgln("Can't paint simple rect shadow, shadow bitmap width {} but expected {}", shadow_bitmap.width(), base_size * (6 + 2));
  391. return;
  392. }
  393. // The containing_rect should have been inflated appropriately
  394. VERIFY(containing_rect.size().contains(Gfx::IntSize { base_size, base_size }));
  395. auto sides_height = containing_rect.height() - 2 * base_size;
  396. auto half_height = sides_height / 2;
  397. auto containing_horizontal_rect = containing_rect;
  398. int horizontal_shift = 0;
  399. if (half_height < base_size && !shadow_includes_frame) {
  400. // If the height is too small we need to shift the left/right accordingly, unless the shadow includes portions of the frame
  401. horizontal_shift = base_size - half_height;
  402. containing_horizontal_rect.set_left(containing_horizontal_rect.left() + horizontal_shift);
  403. containing_horizontal_rect.set_right(containing_horizontal_rect.right() - 2 * horizontal_shift);
  404. }
  405. auto half_width = containing_horizontal_rect.width() / 2;
  406. int corner_piece_width = min(containing_horizontal_rect.width() / 2, base_size * 2);
  407. int left_corners_right = containing_horizontal_rect.left() + corner_piece_width;
  408. int right_corners_left = max(containing_horizontal_rect.right() - corner_piece_width + 1, left_corners_right + 1);
  409. auto paint_horizontal = [&](int y, int src_row) {
  410. if (half_width <= 0)
  411. return;
  412. Gfx::PainterStateSaver save(painter);
  413. painter.add_clip_rect({ containing_horizontal_rect.left(), y, containing_horizontal_rect.width(), base_size });
  414. painter.blit({ containing_horizontal_rect.left(), y }, shadow_bitmap, { 0, src_row * base_size, corner_piece_width, base_size });
  415. painter.blit({ right_corners_left, y }, shadow_bitmap, { 5 * base_size - corner_piece_width, src_row * base_size, corner_piece_width, base_size });
  416. for (int x = left_corners_right; x < right_corners_left; x += base_size) {
  417. auto width = min(right_corners_left - x, base_size);
  418. painter.blit({ x, y }, shadow_bitmap, { corner_piece_width, src_row * base_size, width, base_size });
  419. }
  420. };
  421. paint_horizontal(containing_rect.top(), 0);
  422. paint_horizontal(containing_rect.bottom() - base_size + 1, 1);
  423. int corner_piece_height = min(half_height, base_size);
  424. int top_corners_bottom = base_size + corner_piece_height;
  425. int bottom_corners_top = base_size + max(half_height, sides_height - corner_piece_height);
  426. auto paint_vertical = [&](int x, int src_row, int hshift, int hsrcshift) {
  427. Gfx::PainterStateSaver save(painter);
  428. painter.add_clip_rect({ x, containing_rect.y() + base_size, base_size, containing_rect.height() - 2 * base_size });
  429. 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 });
  430. 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 });
  431. for (int y = top_corners_bottom; y < bottom_corners_top; y += base_size) {
  432. auto height = min(bottom_corners_top - y, base_size);
  433. painter.blit({ x, containing_rect.top() + y }, shadow_bitmap, { base_size * 6, src_row * base_size, base_size, height });
  434. }
  435. };
  436. paint_vertical(containing_rect.left(), 0, horizontal_shift, 0);
  437. if (shadow_includes_frame)
  438. horizontal_shift = 0; // TODO: fix off-by-one on rectangles barely wide enough
  439. paint_vertical(containing_rect.right() - base_size + 1, 1, 0, horizontal_shift);
  440. if (fill_content) {
  441. // Fill the enclosed rectangle with the RGBA color of the right-bottom pixel of the TL tile
  442. auto inner_rect = containing_rect.shrunken(2 * base_size, 2 * base_size);
  443. if (!inner_rect.is_empty())
  444. painter.fill_rect(inner_rect, shadow_bitmap.get_pixel(2 * base_size - 1, base_size - 1));
  445. }
  446. }
  447. }