StylePainter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGfx/Bitmap.h>
  27. #include <LibGfx/Painter.h>
  28. #include <LibGfx/Palette.h>
  29. #include <LibGfx/StylePainter.h>
  30. namespace Gfx {
  31. void StylePainter::paint_tab_button(Painter& painter, const Rect& rect, const Palette& palette, bool active, bool hovered, bool enabled)
  32. {
  33. Color base_color = palette.button();
  34. Color highlight_color2 = palette.threed_highlight();
  35. Color shadow_color1 = palette.threed_shadow1();
  36. Color shadow_color2 = palette.threed_shadow2();
  37. if (hovered && enabled && !active)
  38. base_color = palette.hover_highlight();
  39. PainterStateSaver saver(painter);
  40. painter.translate(rect.location());
  41. // Base
  42. painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 1 }, base_color);
  43. // Top line
  44. painter.draw_line({ 2, 0 }, { rect.width() - 3, 0 }, highlight_color2);
  45. // Left side
  46. painter.draw_line({ 0, 2 }, { 0, rect.height() - 1 }, highlight_color2);
  47. painter.set_pixel({ 1, 1 }, highlight_color2);
  48. // Right side
  49. painter.draw_line({
  50. rect.width() - 1,
  51. 2,
  52. },
  53. { rect.width() - 1, rect.height() - 1 }, shadow_color2);
  54. painter.draw_line({
  55. rect.width() - 2,
  56. 2,
  57. },
  58. { rect.width() - 2, rect.height() - 1 }, shadow_color1);
  59. painter.set_pixel({
  60. rect.width() - 2,
  61. 1,
  62. },
  63. shadow_color2);
  64. }
  65. static void paint_button_new(Painter& painter, const Rect& rect, const Palette& palette, bool pressed, bool checked, bool hovered, bool enabled)
  66. {
  67. Color button_color = palette.button();
  68. Color highlight_color2 = palette.threed_highlight();
  69. Color shadow_color1 = palette.threed_shadow1();
  70. Color shadow_color2 = palette.threed_shadow2();
  71. if (checked && enabled) {
  72. if (hovered)
  73. button_color = palette.hover_highlight();
  74. else
  75. button_color = palette.button();
  76. } else if (hovered && enabled)
  77. button_color = palette.hover_highlight();
  78. PainterStateSaver saver(painter);
  79. painter.translate(rect.location());
  80. if (pressed || checked) {
  81. // Base
  82. painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
  83. painter.draw_rect({ {}, rect.size() }, shadow_color2);
  84. // Sunken shadow
  85. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color1);
  86. painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, shadow_color1);
  87. } else {
  88. // Base
  89. painter.fill_rect({ 1, 1, rect.width() - 3, rect.height() - 3 }, button_color);
  90. // Outer highlight
  91. painter.draw_line({ 0, 0 }, { rect.width() - 2, 0 }, highlight_color2);
  92. painter.draw_line({ 0, 1 }, { 0, rect.height() - 2 }, highlight_color2);
  93. // Outer shadow
  94. painter.draw_line({ 0, rect.height() - 1 }, { rect.width() - 1, rect.height() - 1 }, shadow_color2);
  95. painter.draw_line({ rect.width() - 1, 0 }, { rect.width() - 1, rect.height() - 2 }, shadow_color2);
  96. // Inner shadow
  97. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color1);
  98. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color1);
  99. }
  100. }
  101. void StylePainter::paint_button(Painter& painter, const Rect& rect, const Palette& palette, ButtonStyle button_style, bool pressed, bool hovered, bool checked, bool enabled)
  102. {
  103. if (button_style == ButtonStyle::Normal)
  104. return paint_button_new(painter, rect, palette, pressed, checked, hovered, enabled);
  105. Color button_color = palette.button();
  106. Color highlight_color = palette.threed_highlight();
  107. Color shadow_color = palette.threed_shadow1();
  108. if (button_style == ButtonStyle::CoolBar && !enabled)
  109. return;
  110. PainterStateSaver saver(painter);
  111. painter.translate(rect.location());
  112. if (pressed || checked) {
  113. // Base
  114. painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
  115. // Sunken shadow
  116. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color);
  117. painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, shadow_color);
  118. // Bottom highlight
  119. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, highlight_color);
  120. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, highlight_color);
  121. } else if (button_style == ButtonStyle::CoolBar && hovered) {
  122. // Base
  123. painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
  124. // White highlight
  125. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, highlight_color);
  126. painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, highlight_color);
  127. // Gray shadow
  128. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color);
  129. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color);
  130. }
  131. }
  132. void StylePainter::paint_surface(Painter& painter, const Rect& rect, const Palette& palette, bool paint_vertical_lines, bool paint_top_line)
  133. {
  134. painter.fill_rect({ rect.x(), rect.y() + 1, rect.width(), rect.height() - 2 }, palette.button());
  135. painter.draw_line(rect.top_left(), rect.top_right(), paint_top_line ? palette.threed_highlight() : palette.button());
  136. painter.draw_line(rect.bottom_left(), rect.bottom_right(), palette.threed_shadow1());
  137. if (paint_vertical_lines) {
  138. painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), palette.threed_highlight());
  139. painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), palette.threed_shadow1());
  140. }
  141. }
  142. void StylePainter::paint_frame(Painter& painter, const Rect& rect, const Palette& palette, FrameShape shape, FrameShadow shadow, int thickness, bool skip_vertical_lines)
  143. {
  144. Color top_left_color;
  145. Color bottom_right_color;
  146. Color dark_shade = palette.threed_shadow1();
  147. Color light_shade = palette.threed_highlight();
  148. if (shape == FrameShape::Container && thickness >= 2) {
  149. if (shadow == FrameShadow::Raised) {
  150. dark_shade = palette.threed_shadow2();
  151. }
  152. }
  153. if (shadow == FrameShadow::Raised) {
  154. top_left_color = light_shade;
  155. bottom_right_color = dark_shade;
  156. } else if (shadow == FrameShadow::Sunken) {
  157. top_left_color = dark_shade;
  158. bottom_right_color = light_shade;
  159. } else if (shadow == FrameShadow::Plain) {
  160. top_left_color = dark_shade;
  161. bottom_right_color = dark_shade;
  162. }
  163. if (thickness >= 1) {
  164. painter.draw_line(rect.top_left(), rect.top_right(), top_left_color);
  165. painter.draw_line(rect.bottom_left(), rect.bottom_right(), bottom_right_color);
  166. if (shape != FrameShape::Panel || !skip_vertical_lines) {
  167. painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), top_left_color);
  168. painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), bottom_right_color);
  169. }
  170. }
  171. if (shape == FrameShape::Container && thickness >= 2) {
  172. Color top_left_color;
  173. Color bottom_right_color;
  174. Color dark_shade = palette.threed_shadow2();
  175. Color light_shade = palette.button();
  176. if (shadow == FrameShadow::Raised) {
  177. dark_shade = palette.threed_shadow1();
  178. top_left_color = light_shade;
  179. bottom_right_color = dark_shade;
  180. } else if (shadow == FrameShadow::Sunken) {
  181. top_left_color = dark_shade;
  182. bottom_right_color = light_shade;
  183. } else if (shadow == FrameShadow::Plain) {
  184. top_left_color = dark_shade;
  185. bottom_right_color = dark_shade;
  186. }
  187. Rect inner_container_frame_rect = rect.shrunken(2, 2);
  188. painter.draw_line(inner_container_frame_rect.top_left(), inner_container_frame_rect.top_right(), top_left_color);
  189. painter.draw_line(inner_container_frame_rect.bottom_left(), inner_container_frame_rect.bottom_right(), bottom_right_color);
  190. painter.draw_line(inner_container_frame_rect.top_left().translated(0, 1), inner_container_frame_rect.bottom_left().translated(0, -1), top_left_color);
  191. painter.draw_line(inner_container_frame_rect.top_right(), inner_container_frame_rect.bottom_right().translated(0, -1), bottom_right_color);
  192. }
  193. if (shape == FrameShape::Box && thickness >= 2) {
  194. swap(top_left_color, bottom_right_color);
  195. Rect inner_rect = rect.shrunken(2, 2);
  196. painter.draw_line(inner_rect.top_left(), inner_rect.top_right(), top_left_color);
  197. painter.draw_line(inner_rect.bottom_left(), inner_rect.bottom_right(), bottom_right_color);
  198. painter.draw_line(inner_rect.top_left().translated(0, 1), inner_rect.bottom_left().translated(0, -1), top_left_color);
  199. painter.draw_line(inner_rect.top_right(), inner_rect.bottom_right().translated(0, -1), bottom_right_color);
  200. }
  201. }
  202. void StylePainter::paint_window_frame(Painter& painter, const Rect& rect, const Palette& palette)
  203. {
  204. Color base_color = palette.button();
  205. Color dark_shade = palette.threed_shadow2();
  206. Color mid_shade = palette.threed_shadow1();
  207. Color light_shade = palette.threed_highlight();
  208. painter.draw_line(rect.top_left(), rect.top_right(), base_color);
  209. painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left(), base_color);
  210. painter.draw_line(rect.top_left().translated(1, 1), rect.top_right().translated(-1, 1), light_shade);
  211. painter.draw_line(rect.top_left().translated(1, 1), rect.bottom_left().translated(1, -1), light_shade);
  212. painter.draw_line(rect.top_left().translated(2, 2), rect.top_right().translated(-2, 2), base_color);
  213. painter.draw_line(rect.top_left().translated(2, 2), rect.bottom_left().translated(2, -2), base_color);
  214. painter.draw_line(rect.top_right(), rect.bottom_right(), dark_shade);
  215. painter.draw_line(rect.top_right().translated(-1, 1), rect.bottom_right().translated(-1, -1), mid_shade);
  216. painter.draw_line(rect.top_right().translated(-2, 2), rect.bottom_right().translated(-2, -2), base_color);
  217. painter.draw_line(rect.bottom_left(), rect.bottom_right(), dark_shade);
  218. painter.draw_line(rect.bottom_left().translated(1, -1), rect.bottom_right().translated(-1, -1), mid_shade);
  219. painter.draw_line(rect.bottom_left().translated(2, -2), rect.bottom_right().translated(-2, -2), base_color);
  220. }
  221. void StylePainter::paint_progress_bar(Painter& painter, const Rect& rect, const Palette& palette, int min, int max, int value, const StringView& text)
  222. {
  223. // First we fill the entire widget with the gradient. This incurs a bit of
  224. // overdraw but ensures a consistent look throughout the progression.
  225. Color start_color = palette.active_window_border1();
  226. Color end_color = palette.active_window_border2();
  227. painter.fill_rect_with_gradient(rect, start_color, end_color);
  228. if (!text.is_null()) {
  229. painter.draw_text(rect.translated(1, 1), text, TextAlignment::Center, palette.base_text());
  230. painter.draw_text(rect, text, TextAlignment::Center, palette.base_text().inverted());
  231. }
  232. float range_size = max - min;
  233. float progress = (value - min) / range_size;
  234. // Then we carve out a hole in the remaining part of the widget.
  235. // We draw the text a third time, clipped and inverse, for sharp contrast.
  236. float progress_width = progress * rect.width();
  237. Rect hole_rect { (int)progress_width, 0, (int)(rect.width() - progress_width), rect.height() };
  238. hole_rect.move_by(rect.location());
  239. hole_rect.set_right_without_resize(rect.right());
  240. PainterStateSaver saver(painter);
  241. painter.fill_rect(hole_rect, palette.base());
  242. painter.add_clip_rect(hole_rect);
  243. if (!text.is_null())
  244. painter.draw_text(rect.translated(0, 0), text, TextAlignment::Center, palette.base_text());
  245. }
  246. static RefPtr<Gfx::Bitmap> s_unfilled_circle_bitmap;
  247. static RefPtr<Gfx::Bitmap> s_filled_circle_bitmap;
  248. static RefPtr<Gfx::Bitmap> s_changing_filled_circle_bitmap;
  249. static RefPtr<Gfx::Bitmap> s_changing_unfilled_circle_bitmap;
  250. static const Gfx::Bitmap& circle_bitmap(bool checked, bool changing)
  251. {
  252. if (changing)
  253. return checked ? *s_changing_filled_circle_bitmap : *s_changing_unfilled_circle_bitmap;
  254. return checked ? *s_filled_circle_bitmap : *s_unfilled_circle_bitmap;
  255. }
  256. void StylePainter::paint_radio_button(Painter& painter, const Rect& rect, const Palette&, bool is_checked, bool is_being_pressed)
  257. {
  258. if (!s_unfilled_circle_bitmap) {
  259. s_unfilled_circle_bitmap = Bitmap::load_from_file("/res/icons/unfilled-radio-circle.png");
  260. s_filled_circle_bitmap = Bitmap::load_from_file("/res/icons/filled-radio-circle.png");
  261. s_changing_filled_circle_bitmap = Bitmap::load_from_file("/res/icons/changing-filled-radio-circle.png");
  262. s_changing_unfilled_circle_bitmap = Bitmap::load_from_file("/res/icons/changing-unfilled-radio-circle.png");
  263. }
  264. auto& bitmap = circle_bitmap(is_checked, is_being_pressed);
  265. painter.blit(rect.location(), bitmap, bitmap.rect());
  266. }
  267. }