diff --git a/LibGUI/GButton.cpp b/LibGUI/GButton.cpp index 2309ee0be0..775a187af7 100644 --- a/LibGUI/GButton.cpp +++ b/LibGUI/GButton.cpp @@ -1,5 +1,6 @@ #include "GButton.h" #include +#include //#define GBUTTON_DEBUG @@ -23,37 +24,9 @@ void GButton::set_caption(String&& caption) void GButton::paint_event(GPaintEvent&) { - Color button_color = Color::LightGray; - Color highlight_color = Color::White; - Color shadow_color = Color(96, 96, 96); - Painter painter(*this); - painter.draw_rect(rect(), Color::Black, true); - if (m_being_pressed) { - // Base - painter.fill_rect(rect().shrunken(2, 2), button_color); - - // Sunken shadow - painter.draw_line({ 1, 1 }, { width() - 2, 1 }, shadow_color); - painter.draw_line({ 1, 2 }, {1, height() - 2 }, shadow_color); - } else { - // Base - painter.fill_rect({ 3, 3, width() - 5, height() - 5 }, button_color); - painter.fill_rect_with_gradient({ 3, 3, width() - 5, height() - 5 }, button_color, Color::White); - - // White highlight - painter.draw_line({ 1, 1 }, { width() - 2, 1 }, highlight_color); - painter.draw_line({ 1, 2 }, { width() - 3, 2 }, highlight_color); - painter.draw_line({ 1, 3 }, { 1, height() - 2 }, highlight_color); - painter.draw_line({ 2, 3 }, { 2, height() - 3 }, highlight_color); - - // Gray shadow - painter.draw_line({ width() - 2, 1 }, { width() - 2, height() - 4 }, shadow_color); - painter.draw_line({ width() - 3, 2 }, { width() - 3, height() - 4 }, shadow_color); - painter.draw_line({ 1, height() - 2 }, { width() - 2, height() - 2 }, shadow_color); - painter.draw_line({ 2, height() - 3 }, { width() - 2, height() - 3 }, shadow_color); - } + GStyle::the().paint_button(painter, rect(), m_being_pressed); if (!caption().is_empty() || m_icon) { auto content_rect = rect(); diff --git a/LibGUI/GScrollBar.cpp b/LibGUI/GScrollBar.cpp index bc5f77abde..76691f0df9 100644 --- a/LibGUI/GScrollBar.cpp +++ b/LibGUI/GScrollBar.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -71,6 +72,11 @@ int GScrollBar::scrubbable_range_in_pixels() const return height() - button_size() * 3; } +bool GScrollBar::has_scrubber() const +{ + return m_max != m_min; +} + Rect GScrollBar::scrubber_rect() const { int range_size = m_max - m_min; @@ -122,16 +128,14 @@ void GScrollBar::paint_event(GPaintEvent&) painter.fill_rect(rect(), Color(164, 164, 164)); - painter.draw_rect(up_button_rect(), Color::DarkGray, true); - painter.fill_rect_with_gradient(up_button_rect().shrunken(2, 2), Color::LightGray, Color::White); + GStyle::the().paint_button(painter, up_button_rect(), false); painter.draw_bitmap(up_button_rect().location().translated(3, 3), *s_up_arrow_bitmap, Color::Black); - painter.draw_rect(down_button_rect(), Color::DarkGray, true); - painter.fill_rect_with_gradient(down_button_rect().shrunken(2, 2), Color::LightGray, Color::White); + GStyle::the().paint_button(painter, down_button_rect(), false); painter.draw_bitmap(down_button_rect().location().translated(3, 3), *s_down_arrow_bitmap, Color::Black); - painter.draw_rect(scrubber_rect(), Color::White, true); - painter.fill_rect_with_gradient(scrubber_rect().shrunken(2, 2), Color::LightGray, Color::White); + if (has_scrubber()) + GStyle::the().paint_button(painter, scrubber_rect(), m_scrubbing); } void GScrollBar::mousedown_event(GMouseEvent& event) @@ -154,11 +158,12 @@ void GScrollBar::mousedown_event(GMouseEvent& event) set_value(value() + m_big_step); return; } - if (scrubber_rect().contains(event.position())) { + if (has_scrubber() && scrubber_rect().contains(event.position())) { m_scrubbing = true; m_scrub_start_value = value(); m_scrub_origin = event.position(); set_global_cursor_tracking(true); + update(); return; } } @@ -171,6 +176,7 @@ void GScrollBar::mouseup_event(GMouseEvent& event) return; m_scrubbing = false; set_global_cursor_tracking(false); + update(); } void GScrollBar::mousemove_event(GMouseEvent& event) diff --git a/LibGUI/GScrollBar.h b/LibGUI/GScrollBar.h index 4fd458afd3..98abc1a1cf 100644 --- a/LibGUI/GScrollBar.h +++ b/LibGUI/GScrollBar.h @@ -18,6 +18,7 @@ public: void set_value(int value); void set_step(int step) { m_step = step; } void set_big_step(int big_step) { m_big_step = big_step; } + bool has_scrubber() const; Function on_change; diff --git a/LibGUI/GStyle.cpp b/LibGUI/GStyle.cpp new file mode 100644 index 0000000000..ea20e708c7 --- /dev/null +++ b/LibGUI/GStyle.cpp @@ -0,0 +1,52 @@ +#include +#include + +GStyle* s_the; + +GStyle& GStyle::the() +{ + if (!s_the) + s_the = new GStyle; + return *s_the; +} + +GStyle::GStyle() +{ +} + +void GStyle::paint_button(Painter& painter, const Rect& rect, bool pressed) +{ + Color button_color = Color::LightGray; + Color highlight_color = Color::White; + Color shadow_color = Color(96, 96, 96); + painter.draw_rect(rect, Color::Black, true); + + painter.translate(rect.location()); + + if (pressed) { + // Base + painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color); + + // Sunken shadow + painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color); + painter.draw_line({ 1, 2 }, {1, rect.height() - 2 }, shadow_color); + } else { + // Base + painter.fill_rect({ 3, 3, rect.width() - 5, rect.height() - 5 }, button_color); + painter.fill_rect_with_gradient({ 3, 3, rect.width() - 5, rect.height() - 5 }, button_color, Color::White); + + // White highlight + painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, highlight_color); + painter.draw_line({ 1, 2 }, { rect.width() - 3, 2 }, highlight_color); + painter.draw_line({ 1, 3 }, { 1, rect.height() - 2 }, highlight_color); + painter.draw_line({ 2, 3 }, { 2, rect.height() - 3 }, highlight_color); + + // Gray shadow + painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 4 }, shadow_color); + painter.draw_line({ rect.width() - 3, 2 }, { rect.width() - 3, rect.height() - 4 }, shadow_color); + painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color); + painter.draw_line({ 2, rect.height() - 3 }, { rect.width() - 2, rect.height() - 3 }, shadow_color); + } + + painter.translate(-rect.location().x(), -rect.location().y()); +} diff --git a/LibGUI/GStyle.h b/LibGUI/GStyle.h new file mode 100644 index 0000000000..8eae749a78 --- /dev/null +++ b/LibGUI/GStyle.h @@ -0,0 +1,14 @@ +#pragma once + +class Painter; +class Rect; + +class GStyle { +public: + static GStyle& the(); + + void paint_button(Painter& painter, const Rect& rect, bool pressed); + +private: + GStyle(); +}; diff --git a/LibGUI/Makefile b/LibGUI/Makefile index 046e667e87..9bc857475d 100644 --- a/LibGUI/Makefile +++ b/LibGUI/Makefile @@ -16,6 +16,7 @@ LIBGUI_OBJS = \ GTextBox.o \ GScrollBar.o \ GWidget.o \ + GStyle.o \ GWindow.o OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS) diff --git a/SharedGraphics/Painter.h b/SharedGraphics/Painter.h index eef0675c49..ee997cb0ca 100644 --- a/SharedGraphics/Painter.h +++ b/SharedGraphics/Painter.h @@ -50,6 +50,7 @@ public: Rect clip_rect() const { return m_clip_rect; } void translate(int dx, int dy) { m_translation.move_by(dx, dy); } + void translate(const Point& delta) { m_translation.move_by(delta); } private: void set_pixel_with_draw_op(dword& pixel, const Color&); diff --git a/SharedGraphics/Point.h b/SharedGraphics/Point.h index e9da12218a..fd1cc28c1e 100644 --- a/SharedGraphics/Point.h +++ b/SharedGraphics/Point.h @@ -28,7 +28,7 @@ public: move_by(delta.x(), delta.y()); } - Point translated(int dx, int dy) + Point translated(int dx, int dy) const { Point point = *this; point.move_by(dx, dy); diff --git a/SharedGraphics/Rect.h b/SharedGraphics/Rect.h index 0c0b1fd410..56279d2e62 100644 --- a/SharedGraphics/Rect.h +++ b/SharedGraphics/Rect.h @@ -68,21 +68,21 @@ public: set_height(height() - h); } - Rect shrunken(int w, int h) + Rect shrunken(int w, int h) const { Rect rect = *this; rect.shrink(w, h); return rect; } - Rect inflated(int w, int h) + Rect inflated(int w, int h) const { Rect rect = *this; rect.inflate(w, h); return rect; } - Rect translated(int dx, int dy) + Rect translated(int dx, int dy) const { Rect rect = *this; rect.move_by(dx, dy);