ClassicStylePainter.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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, GUI::TabWidget::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 GUI::TabWidget::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 GUI::TabWidget::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 GUI::TabWidget::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 GUI::TabWidget::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, FrameShape shape, FrameShadow shadow, int thickness, bool skip_vertical_lines)
  204. {
  205. if (shape == Gfx::FrameShape::NoFrame)
  206. return;
  207. if (shape == FrameShape::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 (shape == FrameShape::Container && thickness >= 2) {
  216. if (shadow == FrameShadow::Raised) {
  217. dark_shade = palette.threed_shadow2();
  218. }
  219. }
  220. if (shadow == FrameShadow::Raised) {
  221. top_left_color = light_shade;
  222. bottom_right_color = dark_shade;
  223. } else if (shadow == FrameShadow::Sunken) {
  224. top_left_color = dark_shade;
  225. bottom_right_color = light_shade;
  226. } else if (shadow == FrameShadow::Plain) {
  227. top_left_color = dark_shade;
  228. bottom_right_color = dark_shade;
  229. }
  230. if (thickness >= 1) {
  231. painter.draw_line(rect.top_left(), rect.top_right(), top_left_color);
  232. painter.draw_line(rect.bottom_left(), rect.bottom_right(), bottom_right_color);
  233. if (shape != FrameShape::Panel || !skip_vertical_lines) {
  234. painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), top_left_color);
  235. painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), bottom_right_color);
  236. }
  237. }
  238. if (shape == FrameShape::Container && thickness >= 2) {
  239. Color top_left_color;
  240. Color bottom_right_color;
  241. Color dark_shade = palette.threed_shadow2();
  242. Color light_shade = palette.button();
  243. if (shadow == FrameShadow::Raised) {
  244. dark_shade = palette.threed_shadow1();
  245. top_left_color = light_shade;
  246. bottom_right_color = dark_shade;
  247. } else if (shadow == FrameShadow::Sunken) {
  248. top_left_color = dark_shade;
  249. bottom_right_color = light_shade;
  250. } else if (shadow == FrameShadow::Plain) {
  251. top_left_color = dark_shade;
  252. bottom_right_color = dark_shade;
  253. }
  254. IntRect inner_container_frame_rect = rect.shrunken(2, 2);
  255. painter.draw_line(inner_container_frame_rect.top_left(), inner_container_frame_rect.top_right(), top_left_color);
  256. painter.draw_line(inner_container_frame_rect.bottom_left(), inner_container_frame_rect.bottom_right(), bottom_right_color);
  257. painter.draw_line(inner_container_frame_rect.top_left().translated(0, 1), inner_container_frame_rect.bottom_left().translated(0, -1), top_left_color);
  258. painter.draw_line(inner_container_frame_rect.top_right(), inner_container_frame_rect.bottom_right().translated(0, -1), bottom_right_color);
  259. }
  260. if (shape == FrameShape::Box && thickness >= 2) {
  261. swap(top_left_color, bottom_right_color);
  262. IntRect inner_rect = rect.shrunken(2, 2);
  263. painter.draw_line(inner_rect.top_left(), inner_rect.top_right(), top_left_color);
  264. painter.draw_line(inner_rect.bottom_left(), inner_rect.bottom_right(), bottom_right_color);
  265. painter.draw_line(inner_rect.top_left().translated(0, 1), inner_rect.bottom_left().translated(0, -1), top_left_color);
  266. painter.draw_line(inner_rect.top_right(), inner_rect.bottom_right().translated(0, -1), bottom_right_color);
  267. }
  268. }
  269. void ClassicStylePainter::paint_window_frame(Painter& painter, IntRect const& rect, Palette const& palette)
  270. {
  271. Color base_color = palette.button();
  272. Color dark_shade = palette.threed_shadow2();
  273. Color mid_shade = palette.threed_shadow1();
  274. Color light_shade = palette.threed_highlight();
  275. auto border_thickness = palette.window_border_thickness();
  276. auto border_radius = palette.window_border_radius();
  277. if (border_radius > 0) {
  278. // FIXME: This will draw "useless" pixels that'll get drawn over by the window contents.
  279. // preferably we should just remove the corner pixels from the completely drawn window
  280. // but I don't know how to do that yet. :^)
  281. AntiAliasingPainter aa_painter { painter };
  282. aa_painter.fill_rect_with_rounded_corners(rect, base_color, border_radius);
  283. return;
  284. }
  285. painter.draw_rect_with_thickness({ rect.x() + border_thickness / 2,
  286. rect.y() + border_thickness / 2,
  287. rect.width() - border_thickness,
  288. rect.height() - border_thickness },
  289. base_color, border_thickness);
  290. painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left(), base_color);
  291. painter.draw_line(rect.top_left().translated(1, 1), rect.top_right().translated(-1, 1), light_shade);
  292. painter.draw_line(rect.top_left().translated(1, 1), rect.bottom_left().translated(1, -1), light_shade);
  293. painter.draw_line(rect.top_left().translated(2, 2), rect.top_right().translated(-2, 2), base_color);
  294. painter.draw_line(rect.top_left().translated(2, 2), rect.bottom_left().translated(2, -2), base_color);
  295. painter.draw_line(rect.top_left().translated(3, 3), rect.top_right().translated(-3, 3), base_color);
  296. painter.draw_line(rect.top_left().translated(3, 3), rect.bottom_left().translated(3, -3), base_color);
  297. painter.draw_line(rect.top_right(), rect.bottom_right(), dark_shade);
  298. painter.draw_line(rect.top_right().translated(-1, 1), rect.bottom_right().translated(-1, -1), mid_shade);
  299. painter.draw_line(rect.top_right().translated(-2, 2), rect.bottom_right().translated(-2, -2), base_color);
  300. painter.draw_line(rect.top_right().translated(-3, 3), rect.bottom_right().translated(-3, -3), base_color);
  301. painter.draw_line(rect.bottom_left(), rect.bottom_right(), dark_shade);
  302. painter.draw_line(rect.bottom_left().translated(1, -1), rect.bottom_right().translated(-1, -1), mid_shade);
  303. painter.draw_line(rect.bottom_left().translated(2, -2), rect.bottom_right().translated(-2, -2), base_color);
  304. painter.draw_line(rect.bottom_left().translated(3, -3), rect.bottom_right().translated(-3, -3), base_color);
  305. }
  306. void ClassicStylePainter::paint_progressbar(Painter& painter, IntRect const& rect, Palette const& palette, int min, int max, int value, StringView text, Orientation orientation)
  307. {
  308. // First we fill the entire widget with the gradient. This incurs a bit of
  309. // overdraw but ensures a consistent look throughout the progression.
  310. Color start_color = palette.active_window_border1();
  311. Color end_color = palette.active_window_border2();
  312. painter.fill_rect_with_gradient(orientation, rect, start_color, end_color);
  313. if (!text.is_null()) {
  314. painter.draw_text(rect.translated(1, 1), text, TextAlignment::Center, palette.base_text());
  315. painter.draw_text(rect, text, TextAlignment::Center, palette.base_text().inverted());
  316. }
  317. float range_size = max - min;
  318. float progress = (value - min) / range_size;
  319. // Then we carve out a hole in the remaining part of the widget.
  320. // We draw the text a third time, clipped and inverse, for sharp contrast.
  321. IntRect hole_rect;
  322. if (orientation == Orientation::Horizontal) {
  323. float progress_width = progress * rect.width();
  324. hole_rect = { (int)progress_width, 0, (int)(rect.width() - progress_width), rect.height() };
  325. } else {
  326. float progress_height = progress * rect.height();
  327. hole_rect = { 0, 0, rect.width(), (int)(rect.height() - progress_height) };
  328. }
  329. hole_rect.translate_by(rect.location());
  330. hole_rect.set_right_without_resize(rect.right());
  331. PainterStateSaver saver(painter);
  332. painter.fill_rect(hole_rect, palette.base());
  333. painter.add_clip_rect(hole_rect);
  334. if (!text.is_null())
  335. painter.draw_text(rect.translated(0, 0), text, TextAlignment::Center, palette.base_text());
  336. }
  337. void ClassicStylePainter::paint_radio_button(Painter& painter, IntRect const& a_rect, Palette const& palette, bool is_checked, bool is_being_pressed)
  338. {
  339. // Outer top left arc, starting at bottom left point.
  340. constexpr Gfx::IntPoint outer_top_left_arc[] = {
  341. { 1, 9 },
  342. { 1, 8 },
  343. { 0, 7 },
  344. { 0, 6 },
  345. { 0, 5 },
  346. { 0, 4 },
  347. { 1, 3 },
  348. { 1, 2 },
  349. { 2, 1 },
  350. { 3, 1 },
  351. { 4, 0 },
  352. { 5, 0 },
  353. { 6, 0 },
  354. { 7, 0 },
  355. { 8, 1 },
  356. { 9, 1 },
  357. };
  358. // Outer bottom right arc, starting at top right point.
  359. constexpr Gfx::IntPoint outer_bottom_right_arc[] = {
  360. { 10, 2 },
  361. { 10, 3 },
  362. { 11, 4 },
  363. { 11, 5 },
  364. { 11, 6 },
  365. { 11, 7 },
  366. { 10, 8 },
  367. { 10, 9 },
  368. { 9, 10 },
  369. { 8, 10 },
  370. { 7, 11 },
  371. { 6, 11 },
  372. { 5, 11 },
  373. { 4, 11 },
  374. { 3, 10 },
  375. { 2, 10 },
  376. };
  377. // Inner top left arc, starting at bottom left point.
  378. constexpr Gfx::IntPoint inner_top_left_arc[] = {
  379. { 2, 8 },
  380. { 1, 7 },
  381. { 1, 6 },
  382. { 1, 5 },
  383. { 1, 4 },
  384. { 2, 3 },
  385. { 2, 2 },
  386. { 3, 2 },
  387. { 3, 2 },
  388. { 3, 2 },
  389. { 3, 2 },
  390. { 4, 1 },
  391. { 5, 1 },
  392. { 6, 1 },
  393. { 7, 1 },
  394. { 8, 2 },
  395. { 9, 2 },
  396. };
  397. // Inner bottom right arc, starting at top right point.
  398. constexpr Gfx::IntPoint inner_bottom_right_arc[] = {
  399. { 9, 3 },
  400. { 10, 4 },
  401. { 10, 5 },
  402. { 10, 6 },
  403. { 10, 7 },
  404. { 9, 8 },
  405. { 9, 9 },
  406. { 8, 9 },
  407. { 7, 10 },
  408. { 6, 10 },
  409. { 5, 10 },
  410. { 4, 10 },
  411. { 3, 9 },
  412. { 2, 9 },
  413. };
  414. // Inner "being pressed" circle, starting at top left corner point.
  415. constexpr Gfx::IntPoint inner_being_pressed_circle[] = {
  416. { 3, 3 },
  417. { 4, 2 },
  418. { 5, 2 },
  419. { 6, 2 },
  420. { 7, 2 },
  421. { 8, 3 },
  422. { 9, 4 },
  423. { 9, 5 },
  424. { 9, 6 },
  425. { 9, 7 },
  426. { 8, 8 },
  427. { 7, 9 },
  428. { 6, 9 },
  429. { 5, 9 },
  430. { 4, 9 },
  431. { 3, 8 },
  432. { 2, 7 },
  433. { 2, 6 },
  434. { 2, 5 },
  435. { 2, 4 },
  436. };
  437. // Inner "checked" circle, starting at top left.
  438. constexpr Gfx::IntPoint checked_circle[] = {
  439. { 5, 4 },
  440. { 6, 4 },
  441. { 4, 5 },
  442. { 5, 5 },
  443. { 6, 5 },
  444. { 7, 5 },
  445. { 4, 6 },
  446. { 5, 6 },
  447. { 6, 6 },
  448. { 7, 6 },
  449. { 5, 7 },
  450. { 6, 7 },
  451. };
  452. // FIXME: Support radio buttons at any size.
  453. Gfx::IntRect rect { a_rect.x(), a_rect.y(), 12, 12 };
  454. auto set_pixels = [&](auto const& points, Gfx::Color color) {
  455. for (auto const& p : points) {
  456. painter.set_pixel(rect.location().translated(p), color);
  457. }
  458. };
  459. // Fill center with base color
  460. painter.fill_rect(rect.shrunken(4, 4), palette.base());
  461. set_pixels(outer_top_left_arc, palette.threed_shadow1());
  462. set_pixels(outer_bottom_right_arc, palette.threed_highlight());
  463. set_pixels(inner_top_left_arc, palette.threed_shadow2());
  464. set_pixels(inner_bottom_right_arc, palette.button());
  465. if (is_being_pressed) {
  466. set_pixels(inner_being_pressed_circle, palette.threed_shadow1());
  467. }
  468. if (is_checked) {
  469. set_pixels(checked_circle, palette.base_text());
  470. }
  471. }
  472. static constexpr Gfx::CharacterBitmap s_checked_bitmap {
  473. " "
  474. " # "
  475. " ## "
  476. " ### "
  477. " ## ### "
  478. " ##### "
  479. " ### "
  480. " # "
  481. " "sv,
  482. 9, 9
  483. };
  484. void ClassicStylePainter::paint_check_box(Painter& painter, IntRect const& rect, Palette const& palette, bool is_enabled, bool is_checked, bool is_being_pressed)
  485. {
  486. painter.fill_rect(rect, is_enabled ? palette.base() : palette.window());
  487. paint_frame(painter, rect, palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  488. if (is_being_pressed) {
  489. // FIXME: This color should not be hard-coded.
  490. painter.draw_rect(rect.shrunken(4, 4), Color::MidGray);
  491. }
  492. if (is_checked) {
  493. auto check_rect = Gfx::IntRect({}, s_checked_bitmap.size()).centered_within(rect);
  494. painter.draw_bitmap(check_rect.location(), s_checked_bitmap, is_enabled ? palette.base_text() : palette.threed_shadow1());
  495. }
  496. }
  497. void ClassicStylePainter::paint_transparency_grid(Painter& painter, IntRect const& rect, Palette const& palette)
  498. {
  499. painter.fill_rect_with_checkerboard(rect, { 8, 8 }, palette.base().darkened(0.9), palette.base());
  500. }
  501. void ClassicStylePainter::paint_simple_rect_shadow(Painter& painter, IntRect const& containing_rect, Bitmap const& shadow_bitmap, bool shadow_includes_frame, bool fill_content)
  502. {
  503. // The layout of the shadow_bitmap is defined like this:
  504. // +---------+----+---------+----+----+----+
  505. // | TL | T | TR | LT | L | LB |
  506. // +---------+----+---------+----+----+----+
  507. // | BL | B | BR | RT | R | RB |
  508. // +---------+----+---------+----+----+----+
  509. // Located strictly on the top or bottom of the rectangle, above or below of the content:
  510. // TL = top-left T = top TR = top-right
  511. // BL = bottom-left B = bottom BR = bottom-right
  512. // Located on the left or right of the rectangle, but not above or below of the content:
  513. // LT = left-top L = left LB = left-bottom
  514. // RT = right-top R = right RB = right-bottom
  515. // So, the bitmap has two rows and 6 column, two of which are twice as wide.
  516. // The height divided by two defines a cell size, and width of each
  517. // column must be the same as the height of the cell, except for the
  518. // first and third column, which are twice as wide.
  519. // If fill_content is true, it will use the RGBA color of right-bottom pixel of TL to fill the rectangle enclosed
  520. if (shadow_bitmap.height() % 2 != 0) {
  521. dbgln("Can't paint simple rect shadow, shadow bitmap height {} is not even", shadow_bitmap.height());
  522. return;
  523. }
  524. auto base_size = shadow_bitmap.height() / 2;
  525. if (shadow_bitmap.width() != base_size * (6 + 2)) {
  526. if (shadow_bitmap.width() % base_size != 0)
  527. dbgln("Can't paint simple rect shadow, shadow bitmap width {} is not a multiple of {}", shadow_bitmap.width(), base_size);
  528. else
  529. dbgln("Can't paint simple rect shadow, shadow bitmap width {} but expected {}", shadow_bitmap.width(), base_size * (6 + 2));
  530. return;
  531. }
  532. // The containing_rect should have been inflated appropriately
  533. VERIFY(containing_rect.size().contains(Gfx::IntSize { base_size, base_size }));
  534. auto sides_height = containing_rect.height() - 2 * base_size;
  535. auto half_height = sides_height / 2;
  536. auto containing_horizontal_rect = containing_rect;
  537. int horizontal_shift = 0;
  538. if (half_height < base_size && !shadow_includes_frame) {
  539. // If the height is too small we need to shift the left/right accordingly, unless the shadow includes portions of the frame
  540. horizontal_shift = base_size - half_height;
  541. containing_horizontal_rect.set_left(containing_horizontal_rect.left() + horizontal_shift);
  542. containing_horizontal_rect.set_right(containing_horizontal_rect.right() - 2 * horizontal_shift);
  543. }
  544. auto half_width = containing_horizontal_rect.width() / 2;
  545. int corner_piece_width = min(containing_horizontal_rect.width() / 2, base_size * 2);
  546. int left_corners_right = containing_horizontal_rect.left() + corner_piece_width;
  547. int right_corners_left = max(containing_horizontal_rect.right() - corner_piece_width + 1, left_corners_right + 1);
  548. auto paint_horizontal = [&](int y, int src_row) {
  549. if (half_width <= 0)
  550. return;
  551. Gfx::PainterStateSaver save(painter);
  552. painter.add_clip_rect({ containing_horizontal_rect.left(), y, containing_horizontal_rect.width(), base_size });
  553. painter.blit({ containing_horizontal_rect.left(), y }, shadow_bitmap, { 0, src_row * base_size, corner_piece_width, base_size });
  554. painter.blit({ right_corners_left, y }, shadow_bitmap, { 5 * base_size - corner_piece_width, src_row * base_size, corner_piece_width, base_size });
  555. for (int x = left_corners_right; x < right_corners_left; x += base_size) {
  556. auto width = min(right_corners_left - x, base_size);
  557. painter.blit({ x, y }, shadow_bitmap, { corner_piece_width, src_row * base_size, width, base_size });
  558. }
  559. };
  560. paint_horizontal(containing_rect.top(), 0);
  561. paint_horizontal(containing_rect.bottom() - base_size + 1, 1);
  562. int corner_piece_height = min(half_height, base_size);
  563. int top_corners_bottom = base_size + corner_piece_height;
  564. int bottom_corners_top = base_size + max(half_height, sides_height - corner_piece_height);
  565. auto paint_vertical = [&](int x, int src_row, int hshift, int hsrcshift) {
  566. Gfx::PainterStateSaver save(painter);
  567. painter.add_clip_rect({ x, containing_rect.y() + base_size, base_size, containing_rect.height() - 2 * base_size });
  568. 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 });
  569. 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 });
  570. for (int y = top_corners_bottom; y < bottom_corners_top; y += base_size) {
  571. auto height = min(bottom_corners_top - y, base_size);
  572. painter.blit({ x, containing_rect.top() + y }, shadow_bitmap, { base_size * 6, src_row * base_size, base_size, height });
  573. }
  574. };
  575. paint_vertical(containing_rect.left(), 0, horizontal_shift, 0);
  576. if (shadow_includes_frame)
  577. horizontal_shift = 0; // TODO: fix off-by-one on rectangles barely wide enough
  578. paint_vertical(containing_rect.right() - base_size + 1, 1, 0, horizontal_shift);
  579. if (fill_content) {
  580. // Fill the enclosed rectangle with the RGBA color of the right-bottom pixel of the TL tile
  581. auto inner_rect = containing_rect.shrunken(2 * base_size, 2 * base_size);
  582. if (!inner_rect.is_empty())
  583. painter.fill_rect(inner_rect, shadow_bitmap.get_pixel(2 * base_size - 1, base_size - 1));
  584. }
  585. }
  586. }