diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-10 03:43:46 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-10 03:43:46 +0200 |
commit | 4ab0cd5d4c6f87fa927b0b97916cbd127a2a1999 (patch) | |
tree | 1d5b2491db4b65bb68ffd7ed81458af376418461 | |
parent | e61dd994df7958fcb4cc764c245b1ec28802ad0d (diff) | |
download | serenity-4ab0cd5d4c6f87fa927b0b97916cbd127a2a1999.zip |
LibGUI: Move frame painting from GFrame to StylePainter.
This way it can be used by others who might not have a GFrame object.
-rw-r--r-- | Applications/FileManager/main.cpp | 4 | ||||
-rw-r--r-- | Applications/FontEditor/GlyphEditorWidget.cpp | 4 | ||||
-rw-r--r-- | Applications/FontEditor/GlyphMapWidget.cpp | 4 | ||||
-rw-r--r-- | Applications/Taskbar/TaskbarWindow.cpp | 4 | ||||
-rw-r--r-- | LibGUI/GApplication.cpp | 4 | ||||
-rw-r--r-- | LibGUI/GFrame.cpp | 51 | ||||
-rw-r--r-- | LibGUI/GFrame.h | 16 | ||||
-rw-r--r-- | LibGUI/GItemView.cpp | 4 | ||||
-rw-r--r-- | LibGUI/GProgressBar.cpp | 4 | ||||
-rw-r--r-- | LibGUI/GStatusBar.cpp | 4 | ||||
-rw-r--r-- | LibGUI/GTableView.cpp | 4 | ||||
-rw-r--r-- | LibGUI/GTextEditor.cpp | 4 | ||||
-rw-r--r-- | LibGUI/GTreeView.cpp | 4 | ||||
-rw-r--r-- | SharedGraphics/StylePainter.cpp | 51 | ||||
-rw-r--r-- | SharedGraphics/StylePainter.h | 3 | ||||
-rw-r--r-- | Userland/guitest2.cpp | 16 |
16 files changed, 93 insertions, 88 deletions
diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index c893375dff..2aef0cdf01 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -65,8 +65,8 @@ int main(int argc, char** argv) progressbar->set_caption("Generating thumbnails: "); progressbar->set_format(GProgressBar::Format::ValueSlashMax); progressbar->set_visible(false); - progressbar->set_frame_shape(GFrame::Shape::Panel); - progressbar->set_frame_shadow(GFrame::Shadow::Sunken); + progressbar->set_frame_shape(FrameShape::Panel); + progressbar->set_frame_shadow(FrameShadow::Sunken); progressbar->set_frame_thickness(1); location_textbox->on_return_pressed = [directory_view, location_textbox] { diff --git a/Applications/FontEditor/GlyphEditorWidget.cpp b/Applications/FontEditor/GlyphEditorWidget.cpp index 3e741795b9..c8e491dff1 100644 --- a/Applications/FontEditor/GlyphEditorWidget.cpp +++ b/Applications/FontEditor/GlyphEditorWidget.cpp @@ -6,8 +6,8 @@ GlyphEditorWidget::GlyphEditorWidget(Font& mutable_font, GWidget* parent) , m_font(mutable_font) { set_frame_thickness(2); - set_frame_shadow(GFrame::Shadow::Sunken); - set_frame_shape(GFrame::Shape::Container); + set_frame_shadow(FrameShadow::Sunken); + set_frame_shape(FrameShape::Container); set_relative_rect({ 0, 0, preferred_width(), preferred_height() }); } diff --git a/Applications/FontEditor/GlyphMapWidget.cpp b/Applications/FontEditor/GlyphMapWidget.cpp index e923576582..2be8c37e63 100644 --- a/Applications/FontEditor/GlyphMapWidget.cpp +++ b/Applications/FontEditor/GlyphMapWidget.cpp @@ -6,8 +6,8 @@ GlyphMapWidget::GlyphMapWidget(Font& mutable_font, GWidget* parent) , m_font(mutable_font) { set_frame_thickness(2); - set_frame_shape(GFrame::Shape::Container); - set_frame_shadow(GFrame::Shadow::Sunken); + set_frame_shape(FrameShape::Container); + set_frame_shadow(FrameShadow::Sunken); set_relative_rect({ 0, 0, preferred_width(), preferred_height() }); } diff --git a/Applications/Taskbar/TaskbarWindow.cpp b/Applications/Taskbar/TaskbarWindow.cpp index 866fdb230f..0cfa8a7d2e 100644 --- a/Applications/Taskbar/TaskbarWindow.cpp +++ b/Applications/Taskbar/TaskbarWindow.cpp @@ -24,8 +24,8 @@ TaskbarWindow::TaskbarWindow() widget->layout()->set_margins({ 3, 2, 3, 2 }); widget->layout()->set_spacing(3); widget->set_frame_thickness(1); - widget->set_frame_shape(GFrame::Shape::Panel); - widget->set_frame_shadow(GFrame::Shadow::Raised); + widget->set_frame_shape(FrameShape::Panel); + widget->set_frame_shadow(FrameShadow::Raised); set_main_widget(widget); m_window_list.aid_create_button = [this] { diff --git a/LibGUI/GApplication.cpp b/LibGUI/GApplication.cpp index aace66ffad..6c9f759998 100644 --- a/LibGUI/GApplication.cpp +++ b/LibGUI/GApplication.cpp @@ -80,8 +80,8 @@ public: m_label->set_background_color(Color::from_rgb(0xdac7b5)); m_label->set_fill_with_background_color(true); m_label->set_frame_thickness(1); - m_label->set_frame_shape(GFrame::Shape::Container); - m_label->set_frame_shadow(GFrame::Shadow::Plain); + m_label->set_frame_shape(FrameShape::Container); + m_label->set_frame_shadow(FrameShadow::Plain); set_main_widget(m_label); } diff --git a/LibGUI/GFrame.cpp b/LibGUI/GFrame.cpp index dcd92485b4..8048ce7791 100644 --- a/LibGUI/GFrame.cpp +++ b/LibGUI/GFrame.cpp @@ -13,57 +13,10 @@ GFrame::~GFrame() void GFrame::paint_event(GPaintEvent& event) { - if (m_shape == Shape::NoFrame) + if (m_shape == FrameShape::NoFrame) return; GPainter painter(*this); painter.add_clip_rect(event.rect()); - - Color top_left_color; - Color bottom_right_color; - Color dark_shade = Color::from_rgb(0x808080); - Color light_shade = Color::from_rgb(0xffffff); - - if (m_shadow == Shadow::Raised) { - top_left_color = light_shade; - bottom_right_color = dark_shade; - } else if (m_shadow == Shadow::Sunken) { - top_left_color = dark_shade; - bottom_right_color = light_shade; - } else if (m_shadow == Shadow::Plain) { - top_left_color = dark_shade; - bottom_right_color = dark_shade; - } - - if (m_thickness >= 1) { - painter.draw_line(rect().top_left(), rect().top_right(), top_left_color); - painter.draw_line(rect().bottom_left(), rect().bottom_right(), bottom_right_color); - - if (m_shape != Shape::Panel || !spans_entire_window_horizontally()) { - painter.draw_line(rect().top_left().translated(0, 1), rect().bottom_left().translated(0, -1), top_left_color); - painter.draw_line(rect().top_right(), rect().bottom_right().translated(0, -1), bottom_right_color); - } - } - - if (m_shape == Shape::Container && m_thickness >= 2) { - Color top_left_color; - Color bottom_right_color; - Color dark_shade = Color::from_rgb(0x404040); - Color light_shade = Color::from_rgb(0xc0c0c0); - if (m_shadow == Shadow::Raised) { - top_left_color = light_shade; - bottom_right_color = dark_shade; - } else if (m_shadow == Shadow::Sunken) { - top_left_color = dark_shade; - bottom_right_color = light_shade; - } else if (m_shadow == Shadow::Plain) { - top_left_color = dark_shade; - bottom_right_color = dark_shade; - } - Rect inner_container_frame_rect = rect().shrunken(2, 2); - painter.draw_line(inner_container_frame_rect.top_left(), inner_container_frame_rect.top_right(), top_left_color); - painter.draw_line(inner_container_frame_rect.bottom_left(), inner_container_frame_rect.bottom_right(), bottom_right_color); - painter.draw_line(inner_container_frame_rect.top_left().translated(0, 1), inner_container_frame_rect.bottom_left().translated(0, -1), top_left_color); - painter.draw_line(inner_container_frame_rect.top_right(), inner_container_frame_rect.bottom_right().translated(0, -1), bottom_right_color); - } + StylePainter::paint_frame(painter, rect(), m_shape, m_shadow, m_thickness, spans_entire_window_horizontally()); } diff --git a/LibGUI/GFrame.h b/LibGUI/GFrame.h index be10bf59fb..b6757e4d8d 100644 --- a/LibGUI/GFrame.h +++ b/LibGUI/GFrame.h @@ -1,23 +1,21 @@ #pragma once #include <LibGUI/GWidget.h> +#include <SharedGraphics/StylePainter.h> class GFrame : public GWidget { public: explicit GFrame(GWidget* parent = nullptr); virtual ~GFrame() override; - enum Shadow { Plain, Raised, Sunken }; - enum Shape { NoFrame, Container, Panel, VerticalLine, HorizontalLine }; - int frame_thickness() const { return m_thickness; } void set_frame_thickness(int thickness) { m_thickness = thickness; } - Shadow frame_shadow() const { return m_shadow; } - void set_frame_shadow(Shadow shadow) { m_shadow = shadow; } + FrameShadow frame_shadow() const { return m_shadow; } + void set_frame_shadow(FrameShadow shadow) { m_shadow = shadow; } - Shape frame_shape() const { return m_shape; } - void set_frame_shape(Shape shape) { m_shape = shape; } + FrameShape frame_shape() const { return m_shape; } + void set_frame_shape(FrameShape shape) { m_shape = shape; } Rect frame_inner_rect_for_size(const Size& size) const { return { m_thickness, m_thickness, size.width() - m_thickness * 2, size.height() - m_thickness * 2 }; } Rect frame_inner_rect() const { return frame_inner_rect_for_size(size()); } @@ -29,6 +27,6 @@ protected: private: int m_thickness { 0 }; - Shadow m_shadow { Plain }; - Shape m_shape { NoFrame }; + FrameShadow m_shadow { FrameShadow::Plain }; + FrameShape m_shape { FrameShape::NoFrame }; }; diff --git a/LibGUI/GItemView.cpp b/LibGUI/GItemView.cpp index c9f76c873e..f73d14555b 100644 --- a/LibGUI/GItemView.cpp +++ b/LibGUI/GItemView.cpp @@ -7,8 +7,8 @@ GItemView::GItemView(GWidget* parent) : GAbstractView(parent) { - set_frame_shape(GFrame::Shape::Container); - set_frame_shadow(GFrame::Shadow::Sunken); + set_frame_shape(FrameShape::Container); + set_frame_shadow(FrameShadow::Sunken); set_frame_thickness(2); horizontal_scrollbar().set_visible(false); } diff --git a/LibGUI/GProgressBar.cpp b/LibGUI/GProgressBar.cpp index 341bbd7f48..89abea3580 100644 --- a/LibGUI/GProgressBar.cpp +++ b/LibGUI/GProgressBar.cpp @@ -5,8 +5,8 @@ GProgressBar::GProgressBar(GWidget* parent) : GFrame(parent) { - set_frame_shape(GFrame::Shape::Container); - set_frame_shadow(GFrame::Shadow::Sunken); + set_frame_shape(FrameShape::Container); + set_frame_shadow(FrameShadow::Sunken); set_frame_thickness(2); } diff --git a/LibGUI/GStatusBar.cpp b/LibGUI/GStatusBar.cpp index ccf8ffc0bb..65d0b2ade5 100644 --- a/LibGUI/GStatusBar.cpp +++ b/LibGUI/GStatusBar.cpp @@ -13,8 +13,8 @@ GStatusBar::GStatusBar(GWidget* parent) layout()->set_margins({ 2, 2, 2, 2 }); layout()->set_spacing(2); m_label = new GLabel(this); - m_label->set_frame_shadow(GFrame::Shadow::Sunken); - m_label->set_frame_shape(GFrame::Shape::Panel); + m_label->set_frame_shadow(FrameShadow::Sunken); + m_label->set_frame_shape(FrameShape::Panel); m_label->set_frame_thickness(1); m_label->set_text_alignment(TextAlignment::CenterLeft); } diff --git a/LibGUI/GTableView.cpp b/LibGUI/GTableView.cpp index 83aae0d87a..4a29e269a4 100644 --- a/LibGUI/GTableView.cpp +++ b/LibGUI/GTableView.cpp @@ -7,8 +7,8 @@ GTableView::GTableView(GWidget* parent) : GAbstractView(parent) { - set_frame_shape(GFrame::Shape::Container); - set_frame_shadow(GFrame::Shadow::Sunken); + set_frame_shape(FrameShape::Container); + set_frame_shadow(FrameShadow::Sunken); set_frame_thickness(2); } diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp index 25b58eac93..69dacada43 100644 --- a/LibGUI/GTextEditor.cpp +++ b/LibGUI/GTextEditor.cpp @@ -14,8 +14,8 @@ GTextEditor::GTextEditor(Type type, GWidget* parent) : GScrollableWidget(parent) , m_type(type) { - set_frame_shape(GFrame::Shape::Container); - set_frame_shadow(GFrame::Shadow::Sunken); + set_frame_shape(FrameShape::Container); + set_frame_shadow(FrameShadow::Sunken); set_frame_thickness(2); set_scrollbars_enabled(is_multi_line()); m_ruler_visible = is_multi_line(); diff --git a/LibGUI/GTreeView.cpp b/LibGUI/GTreeView.cpp index 9a1a56fc61..c857c92e91 100644 --- a/LibGUI/GTreeView.cpp +++ b/LibGUI/GTreeView.cpp @@ -23,8 +23,8 @@ GTreeView::MetadataForIndex& GTreeView::ensure_metadata_for_index(const GModelIn GTreeView::GTreeView(GWidget* parent) : GAbstractView(parent) { - set_frame_shape(GFrame::Shape::Container); - set_frame_shadow(GFrame::Shadow::Sunken); + set_frame_shape(FrameShape::Container); + set_frame_shadow(FrameShadow::Sunken); set_frame_thickness(2); m_expand_bitmap = GraphicsBitmap::load_from_file("/res/icons/treeview-expand.png"); diff --git a/SharedGraphics/StylePainter.cpp b/SharedGraphics/StylePainter.cpp index d695b07dd8..a7970d1d32 100644 --- a/SharedGraphics/StylePainter.cpp +++ b/SharedGraphics/StylePainter.cpp @@ -103,3 +103,54 @@ void StylePainter::paint_surface(Painter& painter, const Rect& rect, bool paint_ painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), Color::MidGray); } } + +void StylePainter::paint_frame(Painter& painter, const Rect& rect, FrameShape shape, FrameShadow shadow, int thickness, bool skip_vertical_lines) +{ + Color top_left_color; + Color bottom_right_color; + Color dark_shade = Color::from_rgb(0x808080); + Color light_shade = Color::from_rgb(0xffffff); + + if (shadow == FrameShadow::Raised) { + top_left_color = light_shade; + bottom_right_color = dark_shade; + } else if (shadow == FrameShadow::Sunken) { + top_left_color = dark_shade; + bottom_right_color = light_shade; + } else if (shadow == FrameShadow::Plain) { + top_left_color = dark_shade; + bottom_right_color = dark_shade; + } + + if (thickness >= 1) { + painter.draw_line(rect.top_left(), rect.top_right(), top_left_color); + painter.draw_line(rect.bottom_left(), rect.bottom_right(), bottom_right_color); + + if (shape != FrameShape::Panel || !skip_vertical_lines) { + painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), top_left_color); + painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), bottom_right_color); + } + } + + if (shape == FrameShape::Container && thickness >= 2) { + Color top_left_color; + Color bottom_right_color; + Color dark_shade = Color::from_rgb(0x404040); + Color light_shade = Color::from_rgb(0xc0c0c0); + if (shadow == FrameShadow::Raised) { + top_left_color = light_shade; + bottom_right_color = dark_shade; + } else if (shadow == FrameShadow::Sunken) { + top_left_color = dark_shade; + bottom_right_color = light_shade; + } else if (shadow == FrameShadow::Plain) { + top_left_color = dark_shade; + bottom_right_color = dark_shade; + } + Rect inner_container_frame_rect = rect.shrunken(2, 2); + painter.draw_line(inner_container_frame_rect.top_left(), inner_container_frame_rect.top_right(), top_left_color); + painter.draw_line(inner_container_frame_rect.bottom_left(), inner_container_frame_rect.bottom_right(), bottom_right_color); + painter.draw_line(inner_container_frame_rect.top_left().translated(0, 1), inner_container_frame_rect.bottom_left().translated(0, -1), top_left_color); + painter.draw_line(inner_container_frame_rect.top_right(), inner_container_frame_rect.bottom_right().translated(0, -1), bottom_right_color); + } +} diff --git a/SharedGraphics/StylePainter.h b/SharedGraphics/StylePainter.h index ed9bd7e80a..264b7ba60c 100644 --- a/SharedGraphics/StylePainter.h +++ b/SharedGraphics/StylePainter.h @@ -4,9 +4,12 @@ class Painter; class Rect; enum class ButtonStyle { Normal, CoolBar, OldNormal }; +enum class FrameShadow { Plain, Raised, Sunken }; +enum class FrameShape { NoFrame, Container, Panel, VerticalLine, HorizontalLine }; class StylePainter { public: static void paint_button(Painter&, const Rect&, ButtonStyle, bool pressed, bool hovered = false, bool checked = false); static void paint_surface(Painter&, const Rect&, bool paint_vertical_lines = true); + static void paint_frame(Painter&, const Rect&, FrameShape, FrameShadow, int thickness, bool skip_vertical_lines = false); }; diff --git a/Userland/guitest2.cpp b/Userland/guitest2.cpp index 00bd8a34fd..4bb8a84e71 100644 --- a/Userland/guitest2.cpp +++ b/Userland/guitest2.cpp @@ -169,25 +169,25 @@ static GWindow* make_frames_window() widget->layout()->set_margins({ 8, 8, 8, 8 }); widget->layout()->set_spacing(8); - auto add_label = [widget] (const String& text, GFrame::Shape shape, GFrame::Shadow shadow) { + auto add_label = [widget] (const String& text, FrameShape shape, FrameShadow shadow) { auto* label = new GLabel(text, widget); label->set_size_policy(SizePolicy::Fill, SizePolicy::Fill); label->set_frame_thickness(1); label->set_frame_shape(shape); label->set_frame_shadow(shadow); - if (shape == GFrame::Shape::Container) { + if (shape == FrameShape::Container) { label->set_frame_thickness(2); label->set_fill_with_background_color(true); label->set_background_color(Color::White); } }; - add_label("Panel + Raised", GFrame::Shape::Panel, GFrame::Shadow::Raised); - add_label("Panel + Sunken", GFrame::Shape::Panel, GFrame::Shadow::Sunken); - add_label("Panel + Plain", GFrame::Shape::Panel, GFrame::Shadow::Plain); - add_label("Container + Raised", GFrame::Shape::Container, GFrame::Shadow::Raised); - add_label("Container + Sunken", GFrame::Shape::Container, GFrame::Shadow::Sunken); - add_label("Container + Plain", GFrame::Shape::Container, GFrame::Shadow::Plain); + add_label("Panel + Raised", FrameShape::Panel, FrameShadow::Raised); + add_label("Panel + Sunken", FrameShape::Panel, FrameShadow::Sunken); + add_label("Panel + Plain", FrameShape::Panel, FrameShadow::Plain); + add_label("Container + Raised", FrameShape::Container, FrameShadow::Raised); + add_label("Container + Sunken", FrameShape::Container, FrameShadow::Sunken); + add_label("Container + Plain", FrameShape::Container, FrameShadow::Plain); return window; } |