From 1c0a22cca8b641d3cbaa8198e276020a9bb0bbdd Mon Sep 17 00:00:00 2001 From: Mirek Fidler Date: Fri, 22 Apr 2022 16:21:33 +0200 Subject: [PATCH] CtrlLib: sizeof(ScrollBar) optimised with VirtualButtons --- benchmarks/sizeof_gui/main.cpp | 1 + uppsrc/CtrlLib/CtrlLib.upp | 1 + uppsrc/CtrlLib/PushCtrl.h | 27 +++++ uppsrc/CtrlLib/ScrollBar.cpp | 176 ++++++++++++++++++------------ uppsrc/CtrlLib/ScrollBar.h | 25 +++-- uppsrc/CtrlLib/VirtualButtons.cpp | 99 +++++++++++++++++ 6 files changed, 254 insertions(+), 75 deletions(-) create mode 100644 uppsrc/CtrlLib/VirtualButtons.cpp diff --git a/benchmarks/sizeof_gui/main.cpp b/benchmarks/sizeof_gui/main.cpp index 56e8344b8..85f0d0b68 100644 --- a/benchmarks/sizeof_gui/main.cpp +++ b/benchmarks/sizeof_gui/main.cpp @@ -10,6 +10,7 @@ GUI_APP_MAIN RDUMP(sizeof(Image)); RDUMP(sizeof(RichText)); RDUMP(sizeof(Ctrl::LogPos)); + RDUMP(sizeof(LabelBase)); RLOG("============="); RDUMP(sizeof(Ctrl)); RDUMP(sizeof(ScrollBar)); diff --git a/uppsrc/CtrlLib/CtrlLib.upp b/uppsrc/CtrlLib/CtrlLib.upp index ec77564e0..5334db87e 100644 --- a/uppsrc/CtrlLib/CtrlLib.upp +++ b/uppsrc/CtrlLib/CtrlLib.upp @@ -18,6 +18,7 @@ file PushCtrl.h, Button.cpp, Switch.cpp, + VirtualButtons.cpp, EditCtrl.h, EditField.cpp, TextEdit.h, diff --git a/uppsrc/CtrlLib/PushCtrl.h b/uppsrc/CtrlLib/PushCtrl.h index c7763668c..5d2a054fe 100644 --- a/uppsrc/CtrlLib/PushCtrl.h +++ b/uppsrc/CtrlLib/PushCtrl.h @@ -410,3 +410,30 @@ public: DataPusher(const Convert& convert, const Display& display = StdDisplay()); // deprecated DataPusher(const Display& display); // deprecated }; + +struct VirtualButtons { + virtual int ButtonCount() const; + virtual Rect ButtonRect(int i) const; + virtual const Button::Style& ButtonStyle(int i) const; + virtual Image ButtonImage(int i) const; + virtual bool ButtonMono(int i) const; + virtual bool ButtonEnabled(int i) const; + + virtual void ButtonPush(int i); + virtual void ButtonRepeat(int i); + virtual void ButtonAction(int i); + + int16 pushi = -1; + int16 mi = -1; + + int FindButton(Point p) const; + + void EndPush(Ctrl *ctrl); + + bool ButtonsCancelMode(Ctrl *ctrl); + bool ButtonsMouseEvent(Ctrl *ctrl, int event, Point p); + void PaintButtons(Draw& w, Ctrl *ctrl); + + int ButtonVisualState(Ctrl *ctrl, int i); + void RefreshButton(Ctrl *ctrl, int i); +}; diff --git a/uppsrc/CtrlLib/ScrollBar.cpp b/uppsrc/CtrlLib/ScrollBar.cpp index f61d47618..06c8445e6 100644 --- a/uppsrc/CtrlLib/ScrollBar.cpp +++ b/uppsrc/CtrlLib/ScrollBar.cpp @@ -45,26 +45,11 @@ ScrollBar::ScrollBar() { jump = false; track = true; horz = false; + push = light = -1; + thumbsize = 8; thumbpos = 0; - push = light = -1; - Add(prev); - Add(prev2); - Add(next); - Add(next2); NoWantFocus(); - prev.ScrollStyle().NoWantFocus().Transparent(); - prev.WhenPush = prev.WhenRepeat = callback(this, &ScrollBar::PrevLine); - prev.WhenPush << Proxy(WhenLeftClick); - prev2.ScrollStyle().NoWantFocus().Transparent(); - prev2.WhenRepeat = prev.WhenRepeat; - prev2.WhenPush = prev.WhenPush; - next.ScrollStyle().NoWantFocus().Transparent(); - next.WhenPush = next.WhenRepeat = callback(this, &ScrollBar::NextLine); - next.WhenPush << Proxy(WhenLeftClick); - next2.ScrollStyle().NoWantFocus().Transparent(); - next2.WhenRepeat = next.WhenRepeat; - next2.WhenPush = next.WhenPush; style = NULL; SetStyle(StyleDefault()); BackPaint(); @@ -73,6 +58,93 @@ ScrollBar::ScrollBar() { ScrollBar::~ScrollBar() {} +int ScrollBar::ButtonCount() const +{ + return BUTTONCOUNT; +} + +void ScrollBar::Layout() { + Size sz = GetSize(); + Set(pagepos); + Refresh(); +} + +Rect ScrollBar::ButtonRect(int i) const +{ + Size sz = GetSize(); + if(IsHorz()) { + int cc = sz.cx > (3 + style->isleft2 + style->isright2) * style->arrowsize ? style->arrowsize : 0; + if(i == PREV) + return RectC(0, 0, cc, sz.cy); + if(i == NEXT) + return RectC(sz.cx - cc, 0, cc, sz.cy); + if(i == PREV2 && style->isleft2) + return RectC(sz.cx - 2 * cc, 0, cc, sz.cy); + if(i == NEXT2 && style->isright2) + return RectC(cc, 0, cc, sz.cy); + } + else { + int cc = sz.cy > (3 + style->isup2 + style->isdown2) * style->arrowsize ? style->arrowsize : 0; + if(i == PREV) + return RectC(0, 0, sz.cx, cc); + if(i == NEXT) + return RectC(0, sz.cy - cc, sz.cx, cc); + if(i == PREV2 && style->isup2) + return RectC(0, sz.cy - 2 * cc, sz.cx, cc); + if(i == NEXT2 && style->isdown2) + return RectC(0, cc, sz.cx, cc); + } + return Null; +} + +bool ScrollBar::ButtonEnabled(int i) const +{ + return is_active || !autodisable; +} + +void ScrollBar::ButtonPush(int i) +{ + WhenLeftClick(); + ButtonRepeat(i); +} + +void ScrollBar::ButtonRepeat(int i) +{ + if(i == PREV) + PrevLine(); + if(i == NEXT) + NextLine(); + if(i == PREV2) + PrevPage(); + if(i == NEXT2) + NextPage(); +} + +const Button::Style& ScrollBar::ButtonStyle(int i) const +{ + if(IsHorz()) { + if(i == PREV) + return style->left; + if(i == NEXT) + return style->right; + if(i == PREV2) + return style->left2; + if(i == NEXT2) + return style->right2; + } + else { + if(i == PREV) + return style->up; + if(i == NEXT) + return style->down; + if(i == PREV2) + return style->up2; + if(i == NEXT2) + return style->down2; + } + return Button::StyleNormal(); +} + Rect ScrollBar::Slider(int& cc) const { Size sz = GetSize(); @@ -135,7 +207,8 @@ Rect ScrollBar::GetPartRect(int p) const { return h; } -void ScrollBar::Paint(Draw& w) { +void ScrollBar::Paint(Draw& w) +{ w.DrawRect(GetSize(), style->bgcolor); int cc; Size sz = style->through ? GetSize() : Slider(cc).GetSize(); @@ -148,7 +221,7 @@ void ScrollBar::Paint(Draw& w) { const Value **l = IsHorz() ? hl : vl; - if(prev.IsShowEnabled()) { + if(is_active || !autodisable) { for(int i = 0; i < 3; i++) { Rect pr = GetPartRect(i); if(i != 2) { @@ -169,6 +242,8 @@ void ScrollBar::Paint(Draw& w) { ChPaint(w, cc, 0, sz.cx, sz.cy, l[0][CTRL_DISABLED]); else ChPaint(w, 0, cc, sz.cx, sz.cy, l[0][CTRL_DISABLED]); + + PaintButtons(w, this); } @@ -216,6 +291,13 @@ void ScrollBar::Drag(Point p) { Position(); } +Image ScrollBar::MouseEvent(int event, Point p, int zdelta, dword keyflags) +{ + if(ButtonsMouseEvent(this, event, p)) + return Null; + return Ctrl::MouseEvent(event, p, zdelta, keyflags); +} + void ScrollBar::LeftDown(Point p, dword) { push = GetMousePart(); LLOG("ScrollBar::LeftDown(" << p << ")"); @@ -330,19 +412,16 @@ bool ScrollBar::Set(int apagepos) { void ScrollBar::Set(int _pagepos, int _pagesize, int _totalsize) { pagesize = _pagesize; totalsize = _totalsize; - is_active = totalsize > pagesize && pagesize > 0; + + bool active = totalsize > pagesize && pagesize > 0; + if(active != is_active) { + Refresh(); + is_active = active; + } if(autohide && is_active != IsShown()) { Show(is_active); WhenVisibility(); } - if(autodisable) { - if(prev.IsEnabled() != is_active) - Refresh(); - prev.Enable(is_active); - next.Enable(is_active); - prev2.Enable(is_active); - next2.Enable(is_active); - } Set(_pagepos); } @@ -472,38 +551,6 @@ bool ScrollBar::HorzKey(dword key) { return true; } -void ScrollBar::Layout() { - Size sz = GetSize(); - if(IsHorz()) { - prev.SetStyle(style->left); - next.SetStyle(style->right); - prev2.SetStyle(style->left2); - next2.SetStyle(style->right2); - int cc = sz.cx > (3 + style->isleft2 + style->isright2) * style->arrowsize ? style->arrowsize : 0; - prev.SetRect(0, 0, cc, sz.cy); - next.SetRect(sz.cx - cc, 0, cc, sz.cy); - prev2.Show(style->isleft2); - prev2.SetRect(sz.cx - 2 * cc, 0, cc, sz.cy); - next2.Show(style->isright2); - next2.SetRect(cc, 0, cc, sz.cy); - } - else { - prev.SetStyle(style->up); - next.SetStyle(style->down); - prev2.SetStyle(style->up2); - next2.SetStyle(style->down2); - int cc = sz.cy > (3 + style->isup2 + style->isdown2) * style->arrowsize ? style->arrowsize : 0; - prev.SetRect(0, 0, sz.cx, cc); - next.SetRect(0, sz.cy - cc, sz.cx, cc); - prev2.Show(style->isup2); - prev2.SetRect(0, sz.cy - 2 * cc, sz.cx, cc); - next2.Show(style->isdown2); - next2.SetRect(0, cc, sz.cx, cc); - } - Set(pagepos); - Refresh(); -} - bool ScrollBar::ScrollInto(int pos, int _linesize) { int new_pos = pagepos; if(pos > new_pos + pagesize - _linesize) @@ -560,14 +607,7 @@ ScrollBar& ScrollBar::AutoHide(bool b) { ScrollBar& ScrollBar::AutoDisable(bool b) { autodisable = b; - if(!b) { - if(!prev.IsEnabled()) - Refresh(); - prev.Enable(); - prev2.Enable(); - next.Enable(); - next2.Enable(); - } + Refresh(); return *this; } diff --git a/uppsrc/CtrlLib/ScrollBar.h b/uppsrc/CtrlLib/ScrollBar.h index 54cd0fd03..400449bf8 100644 --- a/uppsrc/CtrlLib/ScrollBar.h +++ b/uppsrc/CtrlLib/ScrollBar.h @@ -1,4 +1,4 @@ -class ScrollBar : public FrameCtrl { +class ScrollBar : public FrameCtrl, private VirtualButtons { public: virtual void Layout(); virtual Size GetStdSize() const; @@ -15,6 +15,16 @@ public: virtual void FrameLayout(Rect& r); virtual void FrameAddSize(Size& sz); + virtual Image MouseEvent(int event, Point p, int zdelta, dword keyflags); + + virtual int ButtonCount() const; + virtual Rect ButtonRect(int i) const; + virtual const Button::Style& ButtonStyle(int i) const; + virtual bool ButtonEnabled(int i) const; + + virtual void ButtonPush(int i); + virtual void ButtonRepeat(int i); + public: struct Style : ChStyle