GStyle.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <LibGUI/GStyle.h>
  2. #include <SharedGraphics/Painter.h>
  3. static GStyle* s_the;
  4. GStyle& GStyle::the()
  5. {
  6. if (!s_the)
  7. s_the = new GStyle;
  8. return *s_the;
  9. }
  10. GStyle::GStyle()
  11. {
  12. }
  13. static void paint_button_new(Painter& painter, const Rect& rect, bool pressed)
  14. {
  15. Color button_color = Color::from_rgb(0xc0c0c0);
  16. Color highlight_color1 = Color::from_rgb(0xffffff);
  17. Color highlight_color2 = Color::from_rgb(0xdfdfdf);
  18. Color shadow_color1 = Color::from_rgb(0x808080);
  19. Color shadow_color2 = Color::from_rgb(0x404040);
  20. PainterStateSaver saver(painter);
  21. painter.translate(rect.location());
  22. if (pressed) {
  23. // Base
  24. painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
  25. painter.draw_rect(rect, shadow_color2);
  26. // Sunken shadow
  27. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color1);
  28. painter.draw_line({ 1, 2 }, {1, rect.height() - 2 }, shadow_color1);
  29. } else {
  30. // Base
  31. painter.fill_rect({ 2, 2, rect.width() - 4, rect.height() - 4 }, button_color);
  32. // Outer highlight
  33. painter.draw_line({ 0, 0 }, { rect.width() - 2, 0 }, highlight_color2);
  34. painter.draw_line({ 0, 1 }, { 0, rect.height() - 2 }, highlight_color2);
  35. // Inner highlight
  36. painter.draw_line({ 1, 1 }, { rect.width() - 3, 1 }, highlight_color1);
  37. painter.draw_line({ 1, 2 }, { 1, rect.height() - 3 }, highlight_color1);
  38. // Outer shadow
  39. painter.draw_line({ 0, rect.height() - 1 }, { rect.width() - 1, rect.height() - 1 }, shadow_color2);
  40. painter.draw_line({ rect.width() - 1, 0 }, { rect.width() - 1, rect.height() - 2 }, shadow_color2);
  41. // Inner shadow
  42. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color1);
  43. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color1);
  44. }
  45. }
  46. void GStyle::paint_button(Painter& painter, const Rect& rect, GButtonStyle button_style, bool pressed, bool hovered)
  47. {
  48. if (button_style == GButtonStyle::Normal)
  49. return paint_button_new(painter, rect, pressed);
  50. Color button_color = Color::LightGray;
  51. Color highlight_color = Color::White;
  52. Color shadow_color = Color(96, 96, 96);
  53. if (button_style == GButtonStyle::OldNormal)
  54. painter.draw_rect(rect, Color::Black);
  55. PainterStateSaver saver(painter);
  56. painter.translate(rect.location());
  57. if (pressed) {
  58. // Base
  59. painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
  60. // Sunken shadow
  61. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color);
  62. painter.draw_line({ 1, 2 }, {1, rect.height() - 2 }, shadow_color);
  63. // Bottom highlight
  64. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, highlight_color);
  65. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, highlight_color);
  66. } else if (button_style == GButtonStyle::OldNormal || (button_style == GButtonStyle::CoolBar && hovered)) {
  67. // Base
  68. painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
  69. // White highlight
  70. painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, highlight_color);
  71. painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, highlight_color);
  72. // Gray shadow
  73. painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color);
  74. painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color);
  75. }
  76. }
  77. void GStyle::paint_surface(Painter& painter, const Rect& rect)
  78. {
  79. painter.fill_rect({ rect.x(), rect.y() + 1, rect.width(), rect.height() - 2 }, Color::LightGray);
  80. painter.draw_line(rect.top_left(), rect.top_right(), Color::White);
  81. painter.draw_line(rect.bottom_left(), rect.bottom_right(), Color::MidGray);
  82. painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), Color::White);
  83. painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), Color::MidGray);
  84. }