ClassicStylePainter.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * Copyright (c) 2018-2023, 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, Cameron Youell <cameronyouell@gmail.com>
  6. * Copyright (c) 2022, the SerenityOS developers.
  7. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include <AK/StringView.h>
  11. #include <LibGfx/AntiAliasingPainter.h>
  12. #include <LibGfx/Bitmap.h>
  13. #include <LibGfx/CharacterBitmap.h>
  14. #include <LibGfx/ClassicStylePainter.h>
  15. #include <LibGfx/Painter.h>
  16. #include <LibGfx/Palette.h>
  17. namespace Gfx {
  18. void ClassicStylePainter::paint_tab_button(Painter& painter, IntRect const& rect, Palette const& palette, bool active, bool hovered, bool enabled, TabPosition position, bool in_active_window, bool accented)
  19. {
  20. Color base_color = palette.button();
  21. Color highlight_color2 = palette.threed_highlight();
  22. Color shadow_color1 = palette.threed_shadow1();
  23. Color shadow_color2 = palette.threed_shadow2();
  24. if (hovered && enabled && !active)
  25. base_color = palette.hover_highlight();
  26. PainterStateSaver saver(painter);
  27. painter.translate(rect.location());
  28. auto accent = palette.accent();
  29. if (!in_active_window)
  30. accent = accent.to_grayscale();
  31. switch (position) {
  32. case TabPosition::Top:
  33. // Base
  34. painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 1 }, base_color);
  35. // Top line
  36. if (active && accented) {
  37. painter.draw_line({ 3, 0 }, { rect.width() - 3, 0 }, accent.darkened());
  38. painter.fill_rect_with_gradient({ 1, 1, rect.width() - 2, 2 }, accent, accent.lightened(1.5f));
  39. painter.set_pixel({ 2, 0 }, highlight_color2);
  40. } else {
  41. painter.draw_line({ 2, 0 }, { rect.width() - 3, 0 }, highlight_color2);
  42. }
  43. // Left side
  44. painter.draw_line({ 0, 2 }, { 0, rect.height() - 1 }, highlight_color2);
  45. painter.set_pixel({ 1, 1 }, highlight_color2);
  46. // Right side
  47. painter.draw_line({ rect.width() - 1, 2 }, { rect.width() - 1, rect.height() - 1 }, shadow_color2);
  48. painter.draw_line({ rect.width() - 2, 2 }, { rect.width() - 2, rect.height() - 1 }, shadow_color1);
  49. painter.set_pixel(rect.width() - 2, 1, shadow_color2);
  50. break;
  51. case TabPosition::Bottom:
  52. // Base
  53. painter.fill_rect({ 0, 0, rect.width() - 1, rect.height() }, base_color);
  54. // Bottom line
  55. if (active && accented) {
  56. painter.fill_rect_with_gradient({ 1, rect.height() - 3, rect.width() - 2, 2 }, accent, accent.lightened(1.5f));
  57. painter.draw_line({ 2, rect.height() - 1 }, { rect.width() - 3, rect.height() - 1 }, accent.darkened());
  58. } else {
  59. painter.draw_line({ 2, rect.height() - 1 }, { rect.width() - 3, rect.height() - 1 }, shadow_color2);
  60. }
  61. // Left side
  62. painter.draw_line({ 0, 0 }, { 0, rect.height() - 3 }, highlight_color2);
  63. painter.set_pixel({ 1, rect.height() - 2 }, highlight_color2);
  64. // Right side
  65. painter.draw_line({ rect.width() - 1, 0 }, { rect.width() - 1, rect.height() - 3 }, shadow_color2);
  66. painter.draw_line({ rect.width() - 2, 0 }, { rect.width() - 2, rect.height() - 3 }, shadow_color1);
  67. painter.set_pixel({ rect.width() - 2, rect.height() - 2 }, shadow_color2);
  68. break;
  69. case TabPosition::Left:
  70. // Base tab
  71. painter.fill_rect({ 1, 1, rect.width(), rect.height() - 1 }, base_color);
  72. painter.draw_line({ 2, 0 }, { rect.width(), 0 }, highlight_color2);
  73. painter.draw_line({ 2, rect.height() - 1 }, { rect.width(), rect.height() - 1 }, shadow_color2);
  74. // If the tab is active, draw the accent line
  75. if (active && accented) {
  76. painter.fill_rect_with_gradient({ 1, 1, 2, rect.height() - 2 }, accent, accent.lightened(1.5f));
  77. painter.draw_line({ 0, 2 }, { 0, rect.height() - 3 }, accent.darkened());
  78. } else {
  79. painter.draw_line({ 0, 2 }, { 0, rect.height() - 3 }, highlight_color2);
  80. painter.draw_line({ rect.width(), 1 }, { rect.width(), rect.height() - 1 }, shadow_color1);
  81. }
  82. // Make appear as if the tab is rounded
  83. painter.set_pixel({ 1, 1 }, highlight_color2);
  84. painter.set_pixel({ 1, rect.height() - 2 }, shadow_color2);
  85. break;
  86. case TabPosition::Right:
  87. // Base tab
  88. painter.fill_rect({ 0, 1, rect.width() - 1, rect.height() - 1 }, base_color);
  89. painter.draw_line({ 0, 0 }, { rect.width() - 2, 0 }, highlight_color2);
  90. painter.draw_line({ 0, rect.height() - 1 }, { rect.width() - 2, rect.height() - 1 }, shadow_color2);
  91. // If the tab is active, draw the accent line
  92. if (active && accented) {
  93. painter.fill_rect_with_gradient({ rect.width() - 2, 1, 2, rect.height() - 2 }, accent.lightened(1.5f), accent);
  94. painter.draw_line({ rect.width(), 2 }, { rect.width(), rect.height() - 3 }, accent.darkened());
  95. } else {
  96. painter.draw_line({ rect.width(), 2 }, { rect.width(), rect.height() - 3 }, shadow_color2);
  97. painter.draw_line({ 0, 0 }, { 0, rect.height() - 1 }, shadow_color1);
  98. }
  99. // Make appear as if the tab is rounded
  100. painter.set_pixel({ rect.width() - 1, 1 }, shadow_color1);
  101. painter.set_pixel({ rect.width() - 1, rect.height() - 2 }, shadow_color2);
  102. break;
  103. }
  104. }
  105. 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)
  106. {
  107. Color button_color = palette.button();
  108. Color highlight_color = palette.threed_highlight();
  109. Color shadow_color1 = palette.threed_shadow1();
  110. Color shadow_color2 = palette.threed_shadow2();
  111. if (checked && enabled) {
  112. if (hovered)
  113. button_color = palette.hover_highlight();
  114. else
  115. button_color = palette.button();
  116. } else if (hovered && enabled)
  117. button_color = palette.hover_highlight();
  118. PainterStateSaver saver(painter);
  119. auto rect = a_rect;
  120. if (focused || default_button) {
  121. painter.draw_rect(a_rect, palette.threed_shadow2());
  122. rect.shrink(2, 2);
  123. }
  124. painter.translate(rect.location());
  125. if (pressed || checked) {
  126. // Base
  127. Gfx::IntRect base_rect { 1, 1, rect.width() - 2, rect.height() - 2 };
  128. if (checked && !pressed)
  129. painter.fill_rect_with_dither_pattern(base_rect, palette.button().lightened(1.3f), palette.button());
  130. else
  131. painter.fill_rect(base_rect, button_color);
  132. // Top shadow
  133. painter.draw_line({ 0, 0 }, { rect.width() - 2, 0 }, shadow_color2);
  134. painter.draw_line({ 0, 0 }, { 0, rect.height() - 2 }, shadow_color2);
  135. // Sunken shadow
  136. painter.draw_line({ 1, 1 }, { rect.width() - 3, 1 }, shadow_color1);
  137. painter.draw_line({ 1, 2 }, { 1, rect.height() - 3 }, shadow_color1);
  138. // Outer highlight
  139. painter.draw_line({ 0, rect.height() - 1 }, { rect.width() - 1, rect.height() - 1 }, highlight_color);
  140. painter.draw_line({ rect.width() - 1, 0 }, { rect.width() - 1, rect.height() - 2 }, highlight_color);
  141. // Inner highlight
  142. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, palette.button());
  143. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, palette.button());
  144. } else {
  145. // Base
  146. painter.fill_rect({ 0, 0, rect.width(), rect.height() }, button_color);
  147. // Top highlight
  148. if (style == ButtonStyle::Normal) {
  149. painter.draw_line({ 0, 0 }, { rect.width() - 2, 0 }, highlight_color);
  150. painter.draw_line({ 0, 0 }, { 0, rect.height() - 2 }, highlight_color);
  151. } else if (style == ButtonStyle::ThickCap) {
  152. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, highlight_color);
  153. painter.draw_line({ 1, 1 }, { 1, rect.height() - 2 }, highlight_color);
  154. }
  155. // Outer shadow
  156. painter.draw_line({ 0, rect.height() - 1 }, { rect.width() - 1, rect.height() - 1 }, shadow_color2);
  157. painter.draw_line({ rect.width() - 1, 0 }, { rect.width() - 1, rect.height() - 2 }, shadow_color2);
  158. // Inner shadow
  159. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color1);
  160. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color1);
  161. }
  162. }
  163. 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)
  164. {
  165. if (button_style == ButtonStyle::Normal || button_style == ButtonStyle::ThickCap)
  166. return paint_button_new(painter, rect, palette, button_style, pressed, checked, hovered, enabled, focused, default_button);
  167. if (button_style == ButtonStyle::Coolbar && !enabled)
  168. return;
  169. Color button_color = palette.button();
  170. Color highlight_color = palette.threed_highlight();
  171. Color shadow_color = button_style == ButtonStyle::Coolbar ? palette.threed_shadow1() : palette.threed_shadow2();
  172. PainterStateSaver saver(painter);
  173. painter.translate(rect.location());
  174. if (pressed || checked) {
  175. // Base
  176. IntRect base_rect { 1, 1, rect.width() - 2, rect.height() - 2 };
  177. if (button_style == ButtonStyle::Coolbar) {
  178. if (checked && !pressed) {
  179. painter.fill_rect_with_dither_pattern(base_rect, palette.button().lightened(1.3f), Color());
  180. } else {
  181. painter.fill_rect(base_rect, button_color);
  182. }
  183. }
  184. // Sunken shadow
  185. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color);
  186. painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, shadow_color);
  187. // Bottom highlight
  188. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, highlight_color);
  189. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, highlight_color);
  190. } else if (hovered) {
  191. if (button_style == ButtonStyle::Coolbar) {
  192. // Base
  193. painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
  194. }
  195. // Top highlight
  196. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, highlight_color);
  197. painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, highlight_color);
  198. // Bottom shadow
  199. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color);
  200. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color);
  201. }
  202. }
  203. void ClassicStylePainter::paint_frame(Painter& painter, IntRect const& rect, Palette const& palette, FrameStyle style, bool skip_vertical_lines)
  204. {
  205. if (style == Gfx::FrameStyle::NoFrame)
  206. return;
  207. if (style == FrameStyle::Window) {
  208. StylePainter::paint_window_frame(painter, rect, palette);
  209. return;
  210. }
  211. Color top_left_color;
  212. Color bottom_right_color;
  213. Color dark_shade = palette.threed_shadow1();
  214. Color light_shade = palette.threed_highlight();
  215. if (style == FrameStyle::RaisedContainer)
  216. dark_shade = palette.threed_shadow2();
  217. switch (style) {
  218. case FrameStyle::RaisedContainer:
  219. case FrameStyle::RaisedBox:
  220. case FrameStyle::RaisedPanel:
  221. top_left_color = light_shade;
  222. bottom_right_color = dark_shade;
  223. break;
  224. case FrameStyle::SunkenContainer:
  225. case FrameStyle::SunkenBox:
  226. case FrameStyle::SunkenPanel:
  227. top_left_color = dark_shade;
  228. bottom_right_color = light_shade;
  229. break;
  230. case FrameStyle::Plain:
  231. top_left_color = dark_shade;
  232. bottom_right_color = dark_shade;
  233. break;
  234. default:
  235. VERIFY_NOT_REACHED();
  236. }
  237. painter.draw_line(rect.top_left(), rect.top_right().moved_left(1), top_left_color);
  238. painter.draw_line(rect.bottom_left().moved_up(1), rect.bottom_right().translated(-1), bottom_right_color);
  239. if ((style != FrameStyle::SunkenPanel && style != FrameStyle::RaisedPanel) || !skip_vertical_lines) {
  240. painter.draw_line(rect.top_left().moved_down(1), rect.bottom_left().moved_up(2), top_left_color);
  241. painter.draw_line(rect.top_right().moved_left(1), rect.bottom_right().translated(-1, -2), bottom_right_color);
  242. }
  243. if (style == FrameStyle::RaisedContainer || style == FrameStyle::SunkenContainer) {
  244. Color top_left_color;
  245. Color bottom_right_color;
  246. Color dark_shade = palette.threed_shadow2();
  247. Color light_shade = palette.button();
  248. if (style == FrameStyle::RaisedContainer) {
  249. dark_shade = palette.threed_shadow1();
  250. top_left_color = light_shade;
  251. bottom_right_color = dark_shade;
  252. } else if (style == FrameStyle::SunkenContainer) {
  253. top_left_color = dark_shade;
  254. bottom_right_color = light_shade;
  255. }
  256. IntRect inner_container_frame_rect = rect.shrunken(2, 2);
  257. painter.draw_line(inner_container_frame_rect.top_left(), inner_container_frame_rect.top_right().moved_left(1), top_left_color);
  258. painter.draw_line(inner_container_frame_rect.bottom_left().moved_up(1), inner_container_frame_rect.bottom_right().translated(-1), bottom_right_color);
  259. painter.draw_line(inner_container_frame_rect.top_left().moved_down(1), inner_container_frame_rect.bottom_left().moved_up(2), top_left_color);
  260. painter.draw_line(inner_container_frame_rect.top_right().moved_left(1), inner_container_frame_rect.bottom_right().translated(-1, -2), bottom_right_color);
  261. }
  262. if (style == FrameStyle::RaisedBox || style == FrameStyle::SunkenBox) {
  263. swap(top_left_color, bottom_right_color);
  264. IntRect inner_rect = rect.shrunken(2, 2);
  265. painter.draw_line(inner_rect.top_left(), inner_rect.top_right().moved_left(1), top_left_color);
  266. painter.draw_line(inner_rect.bottom_left().moved_up(1), inner_rect.bottom_right().translated(-1), bottom_right_color);
  267. painter.draw_line(inner_rect.top_left().moved_down(1), inner_rect.bottom_left().moved_up(2), top_left_color);
  268. painter.draw_line(inner_rect.top_right().moved_left(1), inner_rect.bottom_right().translated(-1, -2), bottom_right_color);
  269. }
  270. }
  271. void ClassicStylePainter::paint_window_frame(Painter& painter, IntRect const& rect, Palette const& palette)
  272. {
  273. Color base_color = palette.button();
  274. Color dark_shade = palette.threed_shadow2();
  275. Color mid_shade = palette.threed_shadow1();
  276. Color light_shade = palette.threed_highlight();
  277. auto border_thickness = palette.window_border_thickness();
  278. auto border_radius = palette.window_border_radius();
  279. if (border_radius > 0) {
  280. // FIXME: This will draw "useless" pixels that'll get drawn over by the window contents.
  281. // preferably we should just remove the corner pixels from the completely drawn window
  282. // but I don't know how to do that yet. :^)
  283. AntiAliasingPainter aa_painter { painter };
  284. aa_painter.fill_rect_with_rounded_corners(rect, base_color, border_radius);
  285. return;
  286. }
  287. painter.draw_rect_with_thickness({ rect.x() + border_thickness / 2,
  288. rect.y() + border_thickness / 2,
  289. rect.width() - border_thickness,
  290. rect.height() - border_thickness },
  291. base_color, border_thickness);
  292. painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), base_color);
  293. painter.draw_line(rect.top_left().translated(1, 1), rect.top_right().translated(-2, 1), light_shade);
  294. painter.draw_line(rect.top_left().translated(1, 1), rect.bottom_left().translated(1, -2), light_shade);
  295. painter.draw_line(rect.top_left().translated(2, 2), rect.top_right().translated(-3, 2), base_color);
  296. painter.draw_line(rect.top_left().translated(2, 2), rect.bottom_left().translated(2, -3), base_color);
  297. painter.draw_line(rect.top_left().translated(3, 3), rect.top_right().translated(-4, 3), base_color);
  298. painter.draw_line(rect.top_left().translated(3, 3), rect.bottom_left().translated(3, -4), base_color);
  299. painter.draw_line(rect.top_right().translated(-1, 0), rect.bottom_right().translated(-1, -1), dark_shade);
  300. painter.draw_line(rect.top_right().translated(-2, 1), rect.bottom_right().translated(-2, -2), mid_shade);
  301. painter.draw_line(rect.top_right().translated(-3, 2), rect.bottom_right().translated(-3, -3), base_color);
  302. painter.draw_line(rect.top_right().translated(-4, 3), rect.bottom_right().translated(-4, -4), base_color);
  303. painter.draw_line(rect.bottom_left().translated(0, -1), rect.bottom_right().translated(-1, -1), dark_shade);
  304. painter.draw_line(rect.bottom_left().translated(1, -2), rect.bottom_right().translated(-2, -2), mid_shade);
  305. painter.draw_line(rect.bottom_left().translated(2, -3), rect.bottom_right().translated(-3, -3), base_color);
  306. painter.draw_line(rect.bottom_left().translated(3, -4), rect.bottom_right().translated(-4, -4), base_color);
  307. }
  308. void ClassicStylePainter::paint_progressbar(Painter& painter, IntRect const& rect, Palette const& palette, int min, int max, int value, StringView text, Orientation orientation)
  309. {
  310. // First we fill the entire widget with the gradient. This incurs a bit of
  311. // overdraw but ensures a consistent look throughout the progression.
  312. Color start_color = palette.active_window_border1();
  313. Color end_color = palette.active_window_border2();
  314. painter.fill_rect_with_gradient(orientation, rect, start_color, end_color);
  315. if (!text.is_null()) {
  316. painter.draw_text(rect.translated(1, 1), text, TextAlignment::Center, palette.base_text());
  317. painter.draw_text(rect, text, TextAlignment::Center, palette.base_text().inverted());
  318. }
  319. float range_size = max - min;
  320. float progress = (value - min) / range_size;
  321. // Then we carve out a hole in the remaining part of the widget.
  322. // We draw the text a third time, clipped and inverse, for sharp contrast.
  323. IntRect hole_rect;
  324. if (orientation == Orientation::Horizontal) {
  325. float progress_width = progress * rect.width();
  326. hole_rect = { (int)progress_width, 0, (int)(rect.width() - progress_width), rect.height() };
  327. } else {
  328. float progress_height = progress * rect.height();
  329. hole_rect = { 0, 0, rect.width(), (int)(rect.height() - progress_height) };
  330. }
  331. hole_rect.translate_by(rect.location());
  332. hole_rect.set_right_without_resize(rect.right());
  333. PainterStateSaver saver(painter);
  334. painter.fill_rect(hole_rect, palette.base());
  335. painter.add_clip_rect(hole_rect);
  336. if (!text.is_null())
  337. painter.draw_text(rect.translated(0, 0), text, TextAlignment::Center, palette.base_text());
  338. }
  339. void ClassicStylePainter::paint_radio_button(Painter& painter, IntRect const& a_rect, Palette const& palette, bool is_checked, bool is_being_pressed)
  340. {
  341. // Outer top left arc, starting at bottom left point.
  342. constexpr Gfx::IntPoint outer_top_left_arc[] = {
  343. { 1, 9 },
  344. { 1, 8 },
  345. { 0, 7 },
  346. { 0, 6 },
  347. { 0, 5 },
  348. { 0, 4 },
  349. { 1, 3 },
  350. { 1, 2 },
  351. { 2, 1 },
  352. { 3, 1 },
  353. { 4, 0 },
  354. { 5, 0 },
  355. { 6, 0 },
  356. { 7, 0 },
  357. { 8, 1 },
  358. { 9, 1 },
  359. };
  360. // Outer bottom right arc, starting at top right point.
  361. constexpr Gfx::IntPoint outer_bottom_right_arc[] = {
  362. { 10, 2 },
  363. { 10, 3 },
  364. { 11, 4 },
  365. { 11, 5 },
  366. { 11, 6 },
  367. { 11, 7 },
  368. { 10, 8 },
  369. { 10, 9 },
  370. { 9, 10 },
  371. { 8, 10 },
  372. { 7, 11 },
  373. { 6, 11 },
  374. { 5, 11 },
  375. { 4, 11 },
  376. { 3, 10 },
  377. { 2, 10 },
  378. };
  379. // Inner top left arc, starting at bottom left point.
  380. constexpr Gfx::IntPoint inner_top_left_arc[] = {
  381. { 2, 8 },
  382. { 1, 7 },
  383. { 1, 6 },
  384. { 1, 5 },
  385. { 1, 4 },
  386. { 2, 3 },
  387. { 2, 2 },
  388. { 3, 2 },
  389. { 3, 2 },
  390. { 3, 2 },
  391. { 3, 2 },
  392. { 4, 1 },
  393. { 5, 1 },
  394. { 6, 1 },
  395. { 7, 1 },
  396. { 8, 2 },
  397. { 9, 2 },
  398. };
  399. // Inner bottom right arc, starting at top right point.
  400. constexpr Gfx::IntPoint inner_bottom_right_arc[] = {
  401. { 9, 3 },
  402. { 10, 4 },
  403. { 10, 5 },
  404. { 10, 6 },
  405. { 10, 7 },
  406. { 9, 8 },
  407. { 9, 9 },
  408. { 8, 9 },
  409. { 7, 10 },
  410. { 6, 10 },
  411. { 5, 10 },
  412. { 4, 10 },
  413. { 3, 9 },
  414. { 2, 9 },
  415. };
  416. // Inner "being pressed" circle, starting at top left corner point.
  417. constexpr Gfx::IntPoint inner_being_pressed_circle[] = {
  418. { 3, 3 },
  419. { 4, 2 },
  420. { 5, 2 },
  421. { 6, 2 },
  422. { 7, 2 },
  423. { 8, 3 },
  424. { 9, 4 },
  425. { 9, 5 },
  426. { 9, 6 },
  427. { 9, 7 },
  428. { 8, 8 },
  429. { 7, 9 },
  430. { 6, 9 },
  431. { 5, 9 },
  432. { 4, 9 },
  433. { 3, 8 },
  434. { 2, 7 },
  435. { 2, 6 },
  436. { 2, 5 },
  437. { 2, 4 },
  438. };
  439. // Inner "checked" circle, starting at top left.
  440. constexpr Gfx::IntPoint checked_circle[] = {
  441. { 5, 4 },
  442. { 6, 4 },
  443. { 4, 5 },
  444. { 5, 5 },
  445. { 6, 5 },
  446. { 7, 5 },
  447. { 4, 6 },
  448. { 5, 6 },
  449. { 6, 6 },
  450. { 7, 6 },
  451. { 5, 7 },
  452. { 6, 7 },
  453. };
  454. // FIXME: Support radio buttons at any size.
  455. Gfx::IntRect rect { a_rect.x(), a_rect.y(), 12, 12 };
  456. auto set_pixels = [&](auto const& points, Gfx::Color color) {
  457. for (auto const& p : points) {
  458. painter.set_pixel(rect.location().translated(p), color);
  459. }
  460. };
  461. // Fill center with base color
  462. painter.fill_rect(rect.shrunken(4, 4), palette.base());
  463. set_pixels(outer_top_left_arc, palette.threed_shadow1());
  464. set_pixels(outer_bottom_right_arc, palette.threed_highlight());
  465. set_pixels(inner_top_left_arc, palette.threed_shadow2());
  466. set_pixels(inner_bottom_right_arc, palette.button());
  467. if (is_being_pressed) {
  468. set_pixels(inner_being_pressed_circle, palette.threed_shadow1());
  469. }
  470. if (is_checked) {
  471. set_pixels(checked_circle, palette.base_text());
  472. }
  473. }
  474. static constexpr Gfx::CharacterBitmap s_checked_bitmap {
  475. " "
  476. " # "
  477. " ## "
  478. " ### "
  479. " ## ### "
  480. " ##### "
  481. " ### "
  482. " # "
  483. " "sv,
  484. 9, 9
  485. };
  486. void ClassicStylePainter::paint_check_box(Painter& painter, IntRect const& rect, Palette const& palette, bool is_enabled, bool is_checked, bool is_being_pressed)
  487. {
  488. painter.fill_rect(rect, is_enabled ? palette.base() : palette.window());
  489. paint_frame(painter, rect, palette, Gfx::FrameStyle::SunkenContainer);
  490. if (is_being_pressed) {
  491. // FIXME: This color should not be hard-coded.
  492. painter.draw_rect(rect.shrunken(4, 4), Color::MidGray);
  493. }
  494. if (is_checked) {
  495. auto check_rect = Gfx::IntRect({}, s_checked_bitmap.size()).centered_within(rect);
  496. painter.draw_bitmap(check_rect.location(), s_checked_bitmap, is_enabled ? palette.base_text() : palette.threed_shadow1());
  497. }
  498. }
  499. void ClassicStylePainter::paint_transparency_grid(Painter& painter, IntRect const& rect, Palette const& palette)
  500. {
  501. painter.fill_rect_with_checkerboard(rect, { 8, 8 }, palette.base().darkened(0.9), palette.base());
  502. }
  503. void ClassicStylePainter::paint_simple_rect_shadow(Painter& painter, IntRect const& containing_rect, Bitmap const& shadow_bitmap, bool shadow_includes_frame, bool fill_content)
  504. {
  505. // The layout of the shadow_bitmap is defined like this:
  506. // +---------+----+---------+----+----+----+
  507. // | TL | T | TR | LT | L | LB |
  508. // +---------+----+---------+----+----+----+
  509. // | BL | B | BR | RT | R | RB |
  510. // +---------+----+---------+----+----+----+
  511. // Located strictly on the top or bottom of the rectangle, above or below of the content:
  512. // TL = top-left T = top TR = top-right
  513. // BL = bottom-left B = bottom BR = bottom-right
  514. // Located on the left or right of the rectangle, but not above or below of the content:
  515. // LT = left-top L = left LB = left-bottom
  516. // RT = right-top R = right RB = right-bottom
  517. // So, the bitmap has two rows and 6 column, two of which are twice as wide.
  518. // The height divided by two defines a cell size, and width of each
  519. // column must be the same as the height of the cell, except for the
  520. // first and third column, which are twice as wide.
  521. // If fill_content is true, it will use the RGBA color of right-bottom pixel of TL to fill the rectangle enclosed
  522. if (shadow_bitmap.height() % 2 != 0) {
  523. dbgln("Can't paint simple rect shadow, shadow bitmap height {} is not even", shadow_bitmap.height());
  524. return;
  525. }
  526. auto base_size = shadow_bitmap.height() / 2;
  527. if (shadow_bitmap.width() != base_size * (6 + 2)) {
  528. if (shadow_bitmap.width() % base_size != 0)
  529. dbgln("Can't paint simple rect shadow, shadow bitmap width {} is not a multiple of {}", shadow_bitmap.width(), base_size);
  530. else
  531. dbgln("Can't paint simple rect shadow, shadow bitmap width {} but expected {}", shadow_bitmap.width(), base_size * (6 + 2));
  532. return;
  533. }
  534. // The containing_rect should have been inflated appropriately
  535. VERIFY(containing_rect.size().contains(Gfx::IntSize { base_size, base_size }));
  536. auto sides_height = containing_rect.height() - 2 * base_size;
  537. auto half_height = sides_height / 2;
  538. auto containing_horizontal_rect = containing_rect;
  539. int horizontal_shift = 0;
  540. if (half_height < base_size && !shadow_includes_frame) {
  541. // If the height is too small we need to shift the left/right accordingly, unless the shadow includes portions of the frame
  542. horizontal_shift = base_size - half_height;
  543. containing_horizontal_rect.set_left(containing_horizontal_rect.left() + horizontal_shift);
  544. containing_horizontal_rect.set_right(containing_horizontal_rect.right() - 2 * horizontal_shift);
  545. }
  546. auto half_width = containing_horizontal_rect.width() / 2;
  547. int corner_piece_width = min(containing_horizontal_rect.width() / 2, base_size * 2);
  548. int left_corners_right = containing_horizontal_rect.left() + corner_piece_width;
  549. int right_corners_left = max(containing_horizontal_rect.right() - corner_piece_width, left_corners_right + 1);
  550. auto paint_horizontal = [&](int y, int src_row) {
  551. if (half_width <= 0)
  552. return;
  553. Gfx::PainterStateSaver save(painter);
  554. painter.add_clip_rect({ containing_horizontal_rect.left(), y, containing_horizontal_rect.width(), base_size });
  555. painter.blit({ containing_horizontal_rect.left(), y }, shadow_bitmap, { 0, src_row * base_size, corner_piece_width, base_size });
  556. painter.blit({ right_corners_left, y }, shadow_bitmap, { 5 * base_size - corner_piece_width, src_row * base_size, corner_piece_width, base_size });
  557. for (int x = left_corners_right; x < right_corners_left; x += base_size) {
  558. auto width = min(right_corners_left - x, base_size);
  559. painter.blit({ x, y }, shadow_bitmap, { corner_piece_width, src_row * base_size, width, base_size });
  560. }
  561. };
  562. paint_horizontal(containing_rect.top(), 0);
  563. paint_horizontal(containing_rect.bottom() - base_size, 1);
  564. int corner_piece_height = min(half_height, base_size);
  565. int top_corners_bottom = base_size + corner_piece_height;
  566. int bottom_corners_top = base_size + max(half_height, sides_height - corner_piece_height);
  567. auto paint_vertical = [&](int x, int src_row, int hshift, int hsrcshift) {
  568. Gfx::PainterStateSaver save(painter);
  569. painter.add_clip_rect({ x, containing_rect.y() + base_size, base_size, containing_rect.height() - 2 * base_size });
  570. 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 });
  571. 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 });
  572. for (int y = top_corners_bottom; y < bottom_corners_top; y += base_size) {
  573. auto height = min(bottom_corners_top - y, base_size);
  574. painter.blit({ x, containing_rect.top() + y }, shadow_bitmap, { base_size * 6, src_row * base_size, base_size, height });
  575. }
  576. };
  577. paint_vertical(containing_rect.left(), 0, horizontal_shift, 0);
  578. if (shadow_includes_frame)
  579. horizontal_shift = 0; // TODO: fix off-by-one on rectangles barely wide enough
  580. paint_vertical(containing_rect.right() - base_size, 1, 0, horizontal_shift);
  581. if (fill_content) {
  582. // Fill the enclosed rectangle with the RGBA color of the right-bottom pixel of the TL tile
  583. auto inner_rect = containing_rect.shrunken(2 * base_size, 2 * base_size);
  584. if (!inner_rect.is_empty())
  585. painter.fill_rect(inner_rect, shadow_bitmap.get_pixel(2 * base_size - 1, base_size - 1));
  586. }
  587. }
  588. }