summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenny Baumann <BenBE@geshi.org>2020-11-21 17:03:17 +0100
committerBenBE <BenBE@geshi.org>2020-12-03 22:41:31 +0100
commit5fe2a88c08fad97095f75d3ae4c298d05459663f (patch)
treea0d3445da07bdab07119187231fe98ced55e76c7
parenta7955c49668dfee4f413747a2fafa6850c6c502a (diff)
Use common handling for scrolling
-rw-r--r--Panel.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/Panel.c b/Panel.c
index 7ef1948d..9c962e06 100644
--- a/Panel.c
+++ b/Panel.c
@@ -327,6 +327,10 @@ bool Panel_onKey(Panel* this, int key) {
assert (this != NULL);
int size = Vector_size(this->items);
+
+ #define CLAMP_INDEX(var, delta, min, max) \
+ CLAMP((var) + (delta), (min), MAXIMUM(0, (max)))
+
switch (key) {
case KEY_DOWN:
case KEY_CTRL('N'):
@@ -358,27 +362,23 @@ bool Panel_onKey(Panel* this, int key) {
break;
case KEY_PPAGE:
this->selected -= (this->h - 1);
- this->scrollV = MAXIMUM(0, this->scrollV - this->h + 1);
+ this->scrollV = CLAMP_INDEX(this->scrollV, -(this->h - 1), 0, size - this->h);
this->needsRedraw = true;
break;
case KEY_NPAGE:
this->selected += (this->h - 1);
- this->scrollV = MAXIMUM(0, MINIMUM(Vector_size(this->items) - this->h,
- this->scrollV + this->h - 1));
+ this->scrollV = CLAMP_INDEX(this->scrollV, +(this->h - 1), 0, size - this->h);
this->needsRedraw = true;
break;
case KEY_WHEELUP:
this->selected -= CRT_scrollWheelVAmount;
- this->scrollV -= CRT_scrollWheelVAmount;
+ this->scrollV = CLAMP_INDEX(this->scrollV, -CRT_scrollWheelVAmount, 0, size - this->h);
this->needsRedraw = true;
break;
case KEY_WHEELDOWN:
{
this->selected += CRT_scrollWheelVAmount;
- this->scrollV += CRT_scrollWheelVAmount;
- if (this->scrollV > Vector_size(this->items) - this->h) {
- this->scrollV = Vector_size(this->items) - this->h;
- }
+ this->scrollV = CLAMP_INDEX(this->scrollV, +CRT_scrollWheelVAmount, 0, size - this->h);
this->needsRedraw = true;
break;
}
@@ -402,6 +402,8 @@ bool Panel_onKey(Panel* this, int key) {
return false;
}
+ #undef CLAMP_INDEX
+
// ensure selection within bounds
if (this->selected < 0 || size == 0) {
this->selected = 0;

© 2014-2024 Faster IT GmbH | imprint | privacy policy