ClassicStylePainter.cpp 25 KB

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