MonitorWidget.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  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 "MonitorWidget.h"
  27. #include <LibGUI/Painter.h>
  28. #include <LibGfx/Bitmap.h>
  29. namespace DisplaySettings {
  30. MonitorWidget::MonitorWidget()
  31. {
  32. m_monitor_bitmap = Gfx::Bitmap::load_from_file("/res/graphics/monitor.png");
  33. m_monitor_rect = { 8, 9, 320, 180 };
  34. }
  35. bool MonitorWidget::set_wallpaper(String path)
  36. {
  37. auto bitmap_ptr = Gfx::Bitmap::load_from_file(path);
  38. if (!bitmap_ptr && !path.is_empty())
  39. return false;
  40. m_desktop_wallpaper_path = path;
  41. m_desktop_wallpaper_bitmap = bitmap_ptr;
  42. return true;
  43. }
  44. String MonitorWidget::wallpaper()
  45. {
  46. return m_desktop_wallpaper_path;
  47. }
  48. void MonitorWidget::set_wallpaper_mode(String mode)
  49. {
  50. m_desktop_wallpaper_mode = mode;
  51. }
  52. String MonitorWidget::wallpaper_mode()
  53. {
  54. return m_desktop_wallpaper_mode;
  55. }
  56. void MonitorWidget::set_desktop_resolution(Gfx::IntSize resolution)
  57. {
  58. m_desktop_resolution = resolution;
  59. }
  60. Gfx::IntSize MonitorWidget::desktop_resolution()
  61. {
  62. return m_desktop_resolution;
  63. }
  64. void MonitorWidget::set_background_color(Gfx::Color color)
  65. {
  66. m_desktop_color = color;
  67. }
  68. Gfx::Color MonitorWidget::background_color()
  69. {
  70. return m_desktop_color;
  71. }
  72. void MonitorWidget::paint_event(GUI::PaintEvent& event)
  73. {
  74. Gfx::IntRect screen_rect = { 0, 0, m_desktop_resolution.width(), m_desktop_resolution.height() };
  75. auto screen_bitmap = Gfx::Bitmap::create(m_monitor_bitmap->format(), m_desktop_resolution);
  76. GUI::Painter screen_painter(*screen_bitmap);
  77. screen_painter.fill_rect(screen_rect, m_desktop_color);
  78. if (!m_desktop_wallpaper_bitmap.is_null()) {
  79. if (m_desktop_wallpaper_mode == "simple") {
  80. screen_painter.blit({ 0, 0 }, *m_desktop_wallpaper_bitmap, m_desktop_wallpaper_bitmap->rect());
  81. } else if (m_desktop_wallpaper_mode == "center") {
  82. Gfx::IntPoint offset { screen_rect.width() / 2 - m_desktop_wallpaper_bitmap->size().width() / 2, screen_rect.height() / 2 - m_desktop_wallpaper_bitmap->size().height() / 2 };
  83. screen_painter.blit_offset(screen_rect.location(), *m_desktop_wallpaper_bitmap, screen_rect, offset);
  84. } else if (m_desktop_wallpaper_mode == "tile") {
  85. screen_painter.draw_tiled_bitmap(screen_bitmap->rect(), *m_desktop_wallpaper_bitmap);
  86. } else if (m_desktop_wallpaper_mode == "scaled") {
  87. screen_painter.draw_scaled_bitmap(screen_bitmap->rect(), *m_desktop_wallpaper_bitmap, m_desktop_wallpaper_bitmap->rect());
  88. } else {
  89. ASSERT_NOT_REACHED();
  90. }
  91. }
  92. GUI::Painter painter(*this);
  93. painter.add_clip_rect(event.rect());
  94. painter.blit({ 0, 0 }, *m_monitor_bitmap, m_monitor_bitmap->rect());
  95. painter.draw_scaled_bitmap(m_monitor_rect, *screen_bitmap, screen_bitmap->rect());
  96. if (!m_desktop_resolution.is_null()) {
  97. painter.draw_text(m_monitor_rect.translated(1, 1), m_desktop_resolution.to_string(), Gfx::TextAlignment::Center, Color::Black);
  98. painter.draw_text(m_monitor_rect, m_desktop_resolution.to_string(), Gfx::TextAlignment::Center, Color::White);
  99. }
  100. }
  101. }