PreviewWidget.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 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 "PreviewWidget.h"
  27. #include <AK/StringView.h>
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/Button.h>
  30. #include <LibGUI/CheckBox.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGUI/RadioButton.h>
  33. #include <LibGUI/StatusBar.h>
  34. #include <LibGUI/TextEditor.h>
  35. #include <LibGfx/Bitmap.h>
  36. #include <LibGfx/WindowTheme.h>
  37. namespace ThemeEditor {
  38. class MiniWidgetGallery final : public GUI::Widget {
  39. C_OBJECT(MiniWidgetGallery);
  40. public:
  41. void set_preview_palette(const Gfx::Palette& palette)
  42. {
  43. set_palette(palette);
  44. Function<void(GUI::Widget&)> recurse = [&](GUI::Widget& parent_widget) {
  45. parent_widget.for_each_child_widget([&](auto& widget) {
  46. widget.set_palette(palette);
  47. recurse(widget);
  48. return IterationDecision::Continue;
  49. });
  50. };
  51. recurse(*this);
  52. }
  53. private:
  54. MiniWidgetGallery()
  55. {
  56. set_fill_with_background_color(true);
  57. m_button = add<GUI::Button>();
  58. m_button->set_text("Button");
  59. m_checkbox = add<GUI::CheckBox>();
  60. m_checkbox->set_text("Check box");
  61. m_radio = add<GUI::RadioButton>();
  62. m_radio->set_text("Radio button");
  63. m_statusbar = add<GUI::StatusBar>();
  64. m_statusbar->set_text("Status bar");
  65. m_editor = add<GUI::TextEditor>();
  66. m_editor->set_text("Text editor\nwith multiple\nlines.");
  67. }
  68. virtual void resize_event(GUI::ResizeEvent&) override
  69. {
  70. m_editor->set_relative_rect(10, 70, 200, 140);
  71. m_button->set_relative_rect(10, 10, 200, 20);
  72. m_checkbox->set_relative_rect(10, 30, 200, 20);
  73. m_radio->set_relative_rect(10, 50, 200, 20);
  74. m_statusbar->set_relative_rect(0, height() - 16, width(), 16);
  75. }
  76. RefPtr<GUI::TextEditor> m_editor;
  77. RefPtr<GUI::Button> m_button;
  78. RefPtr<GUI::CheckBox> m_checkbox;
  79. RefPtr<GUI::RadioButton> m_radio;
  80. RefPtr<GUI::StatusBar> m_statusbar;
  81. };
  82. PreviewWidget::PreviewWidget(const Gfx::Palette& preview_palette)
  83. : m_preview_palette(preview_palette)
  84. {
  85. m_active_window_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png");
  86. m_inactive_window_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png");
  87. m_close_bitmap = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-close.png");
  88. m_maximize_bitmap = Gfx::Bitmap::load_from_file("/res/icons/16x16/upward-triangle.png");
  89. m_minimize_bitmap = Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png");
  90. m_gallery = add<MiniWidgetGallery>();
  91. set_greedy_for_hits(true);
  92. }
  93. PreviewWidget::~PreviewWidget()
  94. {
  95. }
  96. void PreviewWidget::set_preview_palette(const Gfx::Palette& palette)
  97. {
  98. m_preview_palette = palette;
  99. m_gallery->set_preview_palette(palette);
  100. update();
  101. }
  102. void PreviewWidget::paint_event(GUI::PaintEvent& event)
  103. {
  104. GUI::Frame::paint_event(event);
  105. GUI::Painter painter(*this);
  106. painter.add_clip_rect(event.rect());
  107. painter.add_clip_rect(frame_inner_rect());
  108. painter.fill_rect(frame_inner_rect(), m_preview_palette.desktop_background());
  109. struct Button {
  110. Gfx::IntRect rect;
  111. RefPtr<Gfx::Bitmap> bitmap;
  112. };
  113. auto paint_window = [&](auto& title, const Gfx::IntRect& rect, auto state, const Gfx::Bitmap& icon) {
  114. int window_button_width = m_preview_palette.window_title_button_width();
  115. int window_button_height = m_preview_palette.window_title_button_height();
  116. auto title_bar_text_rect = Gfx::WindowTheme::current().title_bar_text_rect(Gfx::WindowTheme::WindowType::Normal, rect, m_preview_palette);
  117. int pos = title_bar_text_rect.right() + 1;
  118. Vector<Button> buttons;
  119. buttons.append(Button { {}, m_close_bitmap });
  120. buttons.append(Button { {}, m_maximize_bitmap });
  121. buttons.append(Button { {}, m_minimize_bitmap });
  122. for (auto& button : buttons) {
  123. pos -= window_button_width;
  124. Gfx::IntRect rect { pos, 0, window_button_width, window_button_height };
  125. rect.center_vertically_within(title_bar_text_rect);
  126. button.rect = rect;
  127. }
  128. auto frame_rect = Gfx::WindowTheme::current().frame_rect_for_window(Gfx::WindowTheme::WindowType::Normal, rect, m_preview_palette, 0);
  129. Gfx::PainterStateSaver saver(painter);
  130. painter.translate(frame_rect.location());
  131. Gfx::WindowTheme::current().paint_normal_frame(painter, state, rect, title, icon, m_preview_palette, buttons.last().rect, 0);
  132. for (auto& button : buttons) {
  133. Gfx::StylePainter::paint_button(painter, button.rect, m_preview_palette, Gfx::ButtonStyle::Normal, false);
  134. auto bitmap_rect = button.bitmap->rect();
  135. bitmap_rect.center_within(button.rect);
  136. painter.blit(bitmap_rect.location(), *button.bitmap, button.bitmap->rect());
  137. }
  138. };
  139. Gfx::IntRect active_rect { 0, 0, 320, 240 };
  140. active_rect.center_within(frame_inner_rect());
  141. Gfx::IntRect inactive_rect = active_rect.translated(-20, -20);
  142. paint_window("Inactive window", inactive_rect, Gfx::WindowTheme::WindowState::Inactive, *m_active_window_icon);
  143. paint_window("Active window", active_rect, Gfx::WindowTheme::WindowState::Active, *m_inactive_window_icon);
  144. }
  145. void PreviewWidget::resize_event(GUI::ResizeEvent&)
  146. {
  147. Gfx::IntRect gallery_rect { 0, 0, 320, 240 };
  148. gallery_rect.center_within(rect());
  149. m_gallery->set_relative_rect(gallery_rect);
  150. }
  151. }