mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-15 14:16:07 -06:00
CtrlLib: sizeof(ScrollBar) optimised with VirtualButtons
This commit is contained in:
parent
8b0e370f8a
commit
1c0a22cca8
6 changed files with 254 additions and 75 deletions
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ file
|
|||
PushCtrl.h,
|
||||
Button.cpp,
|
||||
Switch.cpp,
|
||||
VirtualButtons.cpp,
|
||||
EditCtrl.h,
|
||||
EditField.cpp,
|
||||
TextEdit.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);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
class ScrollBar : public FrameCtrl<Ctrl> {
|
||||
class ScrollBar : public FrameCtrl<Ctrl>, 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<Style> {
|
||||
Color bgcolor;
|
||||
|
|
@ -30,19 +40,20 @@ public:
|
|||
private:
|
||||
int thumbpos;
|
||||
int thumbsize;
|
||||
bool horz:1;
|
||||
bool jump:1;
|
||||
bool track:1;
|
||||
int delta;
|
||||
int8 push;
|
||||
int8 light;
|
||||
|
||||
Button prev, prev2, next, next2;
|
||||
enum { PREV, PREV2, NEXT, NEXT2, BUTTONCOUNT };
|
||||
// Button prev, prev2, next, next2;
|
||||
int pagepos;
|
||||
int pagesize;
|
||||
int totalsize;
|
||||
int linesize;
|
||||
int minthumb;
|
||||
int8 push;
|
||||
int8 light;
|
||||
bool horz:1;
|
||||
bool jump:1;
|
||||
bool track:1;
|
||||
bool autohide:1;
|
||||
bool autodisable:1;
|
||||
bool is_active:1;
|
||||
|
|
|
|||
99
uppsrc/CtrlLib/VirtualButtons.cpp
Normal file
99
uppsrc/CtrlLib/VirtualButtons.cpp
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
#include "CtrlLib.h"
|
||||
|
||||
namespace Upp {
|
||||
|
||||
int VirtualButtons::ButtonCount() const { return 0; }
|
||||
Rect VirtualButtons::ButtonRect(int i) const { return Null; }
|
||||
const Button::Style& VirtualButtons::ButtonStyle(int i) const { return Button::StyleEdge(); }
|
||||
Image VirtualButtons::ButtonImage(int i) const { return Null; }
|
||||
bool VirtualButtons::ButtonEnabled(int i) const { return true; }
|
||||
bool VirtualButtons::ButtonMono(int i) const { return false; }
|
||||
|
||||
void VirtualButtons::ButtonPush(int i) {}
|
||||
void VirtualButtons::ButtonRepeat(int i) {}
|
||||
void VirtualButtons::ButtonAction(int i) {}
|
||||
|
||||
int VirtualButtons::FindButton(Point p) const
|
||||
{
|
||||
for(int i = 0; i < ButtonCount(); i++)
|
||||
if(ButtonRect(i).Contains(p))
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int VirtualButtons::ButtonVisualState(Ctrl *ctrl, int i)
|
||||
{
|
||||
if(ButtonEnabled(i)) {
|
||||
if(i == pushi)
|
||||
return CTRL_PRESSED;
|
||||
if(ctrl->HasMouseIn(ButtonRect(i)))
|
||||
return CTRL_HOT;
|
||||
return CTRL_NORMAL;
|
||||
}
|
||||
return CTRL_DISABLED;
|
||||
}
|
||||
|
||||
void VirtualButtons::PaintButtons(Draw& w, Ctrl *ctrl)
|
||||
{
|
||||
for(int i = 0; i < ButtonCount(); i++)
|
||||
Button::PaintButton(w, ButtonRect(i), ButtonStyle(i), ButtonVisualState(ctrl, i), false,
|
||||
String(), StdFont(), ButtonImage(i),
|
||||
ButtonMono(i), 0, false, !ButtonEnabled(i));
|
||||
}
|
||||
|
||||
void VirtualButtons::EndPush(Ctrl *ctrl)
|
||||
{
|
||||
int i = pushi;
|
||||
pushi = -1;
|
||||
RefreshButton(ctrl, i);
|
||||
}
|
||||
|
||||
void VirtualButtons::RefreshButton(Ctrl *ctrl, int i)
|
||||
{
|
||||
if(i >= 0)
|
||||
ctrl->Refresh(ButtonRect(i));
|
||||
}
|
||||
|
||||
bool VirtualButtons::ButtonsMouseEvent(Ctrl *ctrl, int event, Point p)
|
||||
{
|
||||
if(event == Ctrl::CURSORIMAGE)
|
||||
return false;
|
||||
int i = event == Ctrl::MOUSELEAVE ? -1 : FindButton(p);
|
||||
if(i != mi) {
|
||||
RefreshButton(ctrl, mi);
|
||||
RefreshButton(ctrl, mi = i);
|
||||
}
|
||||
switch(event) {
|
||||
case Ctrl::LEFTDOWN:
|
||||
pushi = i;
|
||||
if(pushi >= 0) {
|
||||
ButtonPush(i);
|
||||
ctrl->SetCapture();
|
||||
}
|
||||
else
|
||||
EndPush(ctrl);
|
||||
RefreshButton(ctrl, pushi);
|
||||
break;
|
||||
case Ctrl::MOUSEMOVE:
|
||||
if(ctrl->HasCapture() && i != pushi) {
|
||||
RefreshButton(ctrl, pushi);
|
||||
pushi = i;
|
||||
}
|
||||
break;
|
||||
case Ctrl::LEFTREPEAT:
|
||||
if(i >= 0)
|
||||
ButtonRepeat(i);
|
||||
break;
|
||||
case Ctrl::LEFTUP:
|
||||
int ii = pushi;
|
||||
if(ii >= 0) {
|
||||
EndPush(ctrl);
|
||||
ReleaseCapture();
|
||||
ButtonAction(ii);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return i >= 0;
|
||||
}
|
||||
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue