2021-05-23 11:43:28 +02:00
|
|
|
(*
|
2023-03-05 22:40:21 +01:00
|
|
|
Copyright 2021-2023 Anton Krotov
|
2021-05-23 11:43:28 +02:00
|
|
|
|
|
|
|
This file is part of CEdit.
|
|
|
|
|
|
|
|
CEdit is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
CEdit is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with CEdit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE Scroll;
|
|
|
|
|
2021-09-11 22:57:43 +02:00
|
|
|
IMPORT G := Graph, K := KolibriOS, U := Utils;
|
2021-05-23 11:43:28 +02:00
|
|
|
|
2021-09-03 01:15:33 +02:00
|
|
|
CONST
|
|
|
|
|
2021-09-28 18:15:37 +02:00
|
|
|
DELAY = 40;
|
2021-09-03 01:15:33 +02:00
|
|
|
|
2021-05-23 11:43:28 +02:00
|
|
|
TYPE
|
|
|
|
|
2022-01-18 17:23:19 +01:00
|
|
|
tProcedure = PROCEDURE;
|
|
|
|
|
2021-09-03 01:15:33 +02:00
|
|
|
tScroll* = RECORD
|
2022-01-18 17:23:19 +01:00
|
|
|
vertical, mouse: BOOLEAN;
|
2021-09-03 01:15:33 +02:00
|
|
|
top*, left*,
|
|
|
|
width*, height*: INTEGER; (* read only *)
|
|
|
|
btnSize, sliderSize: INTEGER;
|
|
|
|
pos, Slider, pos0, maxVal*, value*: INTEGER;
|
2022-01-18 17:23:19 +01:00
|
|
|
canvas: G.tCanvas;
|
|
|
|
change: tProcedure;
|
|
|
|
delay: INTEGER;
|
|
|
|
btn*: INTEGER
|
2021-09-03 01:15:33 +02:00
|
|
|
END;
|
2021-05-23 11:43:28 +02:00
|
|
|
|
2021-09-28 18:15:37 +02:00
|
|
|
|
|
|
|
PROCEDURE MouseUp (VAR scroll: tScroll);
|
|
|
|
BEGIN
|
|
|
|
scroll.Slider := -1;
|
2022-01-18 17:23:19 +01:00
|
|
|
scroll.btn := 0;
|
2021-09-28 18:15:37 +02:00
|
|
|
scroll.mouse := FALSE;
|
2022-01-18 17:23:19 +01:00
|
|
|
scroll.delay := DELAY
|
2021-09-28 18:15:37 +02:00
|
|
|
END MouseUp;
|
|
|
|
|
2021-05-23 11:43:28 +02:00
|
|
|
|
2022-01-18 17:23:19 +01:00
|
|
|
PROCEDURE create* (vertical: BOOLEAN; width, height: INTEGER; btnSize, sliderSize: INTEGER; change: tProcedure; VAR scroll: tScroll);
|
2021-09-11 22:57:43 +02:00
|
|
|
VAR
|
|
|
|
res: tScroll;
|
2021-09-03 01:15:33 +02:00
|
|
|
BEGIN
|
2021-09-28 18:15:37 +02:00
|
|
|
MouseUp(res);
|
2021-09-11 22:57:43 +02:00
|
|
|
res.vertical := vertical;
|
|
|
|
res.left := 0;
|
|
|
|
res.top := 0;
|
|
|
|
res.width := width;
|
|
|
|
res.height := height;
|
|
|
|
res.btnSize := btnSize;
|
|
|
|
res.sliderSize := sliderSize;
|
|
|
|
res.pos := 0;
|
|
|
|
res.maxVal := 0;
|
|
|
|
res.canvas := G.CreateCanvas(width, height);
|
2022-01-18 17:23:19 +01:00
|
|
|
res.change := change;
|
2021-09-11 22:57:43 +02:00
|
|
|
scroll := res
|
2021-09-03 01:15:33 +02:00
|
|
|
END create;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Rect (canvas: G.tCanvas; left, top, right, bottom: INTEGER);
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2021-09-03 01:15:33 +02:00
|
|
|
G.FillRect(canvas, left, top, right, bottom);
|
2022-01-12 22:15:22 +01:00
|
|
|
G.SetColor(canvas, K.colors.line);
|
2021-09-03 01:15:33 +02:00
|
|
|
G.Rect(canvas, left, top, right, bottom);
|
|
|
|
END Rect;
|
|
|
|
|
|
|
|
|
2022-01-13 16:13:47 +01:00
|
|
|
PROCEDURE _draw (scroll: tScroll);
|
2021-05-23 11:43:28 +02:00
|
|
|
VAR
|
2021-09-03 01:15:33 +02:00
|
|
|
canvas: G.tCanvas;
|
|
|
|
x, y, d, x1, x2, y1, y2,
|
|
|
|
width, height, btn: INTEGER;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE SetColor (canvas: G.tCanvas; c: BOOLEAN);
|
|
|
|
VAR
|
|
|
|
color: INTEGER;
|
|
|
|
BEGIN
|
|
|
|
IF c THEN
|
2022-01-12 22:15:22 +01:00
|
|
|
color := K.colors.button
|
2021-09-03 01:15:33 +02:00
|
|
|
ELSE
|
2022-01-12 22:15:22 +01:00
|
|
|
color := K.colors.button_text
|
2021-09-03 01:15:33 +02:00
|
|
|
END;
|
|
|
|
G.SetColor(canvas, color)
|
|
|
|
END SetColor;
|
|
|
|
|
|
|
|
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2021-09-03 01:15:33 +02:00
|
|
|
btn := scroll.btnSize;
|
|
|
|
width := scroll.width;
|
|
|
|
height := scroll.height;
|
|
|
|
canvas := scroll.canvas;
|
2022-01-12 22:15:22 +01:00
|
|
|
G.SetColor(canvas, K.colors.light);
|
2021-09-06 23:06:31 +02:00
|
|
|
G.clear(canvas);
|
2022-01-12 22:15:22 +01:00
|
|
|
G.SetColor(canvas, K.colors.line);
|
2021-09-03 01:15:33 +02:00
|
|
|
G.Rect(canvas, 0, 0, width - 1, height - 1);
|
2023-03-05 22:40:21 +01:00
|
|
|
SetColor(canvas, scroll.btn # -1);
|
2021-09-03 01:15:33 +02:00
|
|
|
IF scroll.vertical THEN
|
|
|
|
Rect(canvas, 0, 0, width - 1, btn - 1);
|
2022-01-18 17:23:19 +01:00
|
|
|
SetColor(canvas, scroll.btn # 1);
|
2021-09-03 01:15:33 +02:00
|
|
|
Rect(canvas, 0, height - btn, width - 1, height - 1);
|
2022-01-12 22:15:22 +01:00
|
|
|
G.SetColor(canvas, K.colors.button);
|
2021-09-03 01:15:33 +02:00
|
|
|
Rect(canvas, 0, btn + scroll.pos - 1, width - 1, btn + scroll.pos + scroll.sliderSize - 1);
|
2021-05-23 11:43:28 +02:00
|
|
|
|
2022-01-12 22:15:22 +01:00
|
|
|
G.SetColor(canvas, K.colors.button_text);
|
2021-05-23 11:43:28 +02:00
|
|
|
|
2021-09-03 01:15:33 +02:00
|
|
|
y := btn + scroll.pos + scroll.sliderSize DIV 2 - 1;
|
|
|
|
G.HLine(canvas, y, width DIV 4, 3*width DIV 4);
|
|
|
|
G.HLine(canvas, y - 3, width DIV 3, 2*width DIV 3);
|
|
|
|
G.HLine(canvas, y + 3, width DIV 3, 2*width DIV 3);
|
|
|
|
|
|
|
|
d := 4*width DIV 10;
|
|
|
|
x1 := (width - d) DIV 2;
|
|
|
|
x2 := x1 + d;
|
|
|
|
|
2022-01-18 17:23:19 +01:00
|
|
|
SetColor(canvas, scroll.btn = -1);
|
2021-09-03 01:15:33 +02:00
|
|
|
y := (btn - d DIV 2) DIV 2 + d DIV 2 - 1;
|
|
|
|
G.Triangle(canvas, x1 - 1, y, x2, y, G.triUp);
|
|
|
|
|
2022-01-18 17:23:19 +01:00
|
|
|
SetColor(canvas, scroll.btn = 1);
|
2021-09-03 01:15:33 +02:00
|
|
|
y := y + height - btn - d DIV 2 + 1;
|
|
|
|
G.Triangle(canvas, x1 - 1, y, x2, y, G.triDown);
|
|
|
|
ELSE
|
|
|
|
Rect(canvas, 0, 0, btn - 1, height - 1);
|
2022-01-18 17:23:19 +01:00
|
|
|
SetColor(canvas, scroll.btn # 1);
|
2021-09-03 01:15:33 +02:00
|
|
|
Rect(canvas, width - btn, 0, width - 1, height - 1);
|
2022-01-12 22:15:22 +01:00
|
|
|
G.SetColor(canvas, K.colors.button);
|
2021-09-03 01:15:33 +02:00
|
|
|
Rect(canvas, btn + scroll.pos - 1, 0, btn + scroll.pos + scroll.sliderSize - 1, height - 1);
|
|
|
|
|
2022-01-12 22:15:22 +01:00
|
|
|
G.SetColor(canvas, K.colors.button_text);
|
2021-09-03 01:15:33 +02:00
|
|
|
|
|
|
|
x := btn + scroll.pos + scroll.sliderSize DIV 2 - 1;
|
|
|
|
G.VLine(canvas, x, height DIV 4, 3*height DIV 4);
|
|
|
|
G.VLine(canvas, x - 3, height DIV 3, 2*height DIV 3);
|
|
|
|
G.VLine(canvas, x + 3, height DIV 3, 2*height DIV 3);
|
|
|
|
|
|
|
|
d := 4*height DIV 10;
|
|
|
|
y1 := (height - d) DIV 2;
|
|
|
|
y2 := y1 + d;
|
|
|
|
|
2022-01-18 17:23:19 +01:00
|
|
|
SetColor(canvas, scroll.btn = -1);
|
2021-09-03 01:15:33 +02:00
|
|
|
x := (btn - d DIV 2) DIV 2 + d DIV 2 - 1;
|
|
|
|
G.Triangle(canvas, x, y1 - 1, x, y2, G.triLeft);
|
|
|
|
|
2022-01-18 17:23:19 +01:00
|
|
|
SetColor(canvas, scroll.btn = 1);
|
2021-09-03 01:15:33 +02:00
|
|
|
x := x + width - btn - d DIV 2 + 1;
|
|
|
|
G.Triangle(canvas, x, y1 - 1, x, y2, G.triRight);
|
|
|
|
END;
|
|
|
|
G.DrawCanvas(scroll.canvas, scroll.left, scroll.top)
|
2022-01-13 16:13:47 +01:00
|
|
|
END _draw;
|
2021-09-03 01:15:33 +02:00
|
|
|
|
|
|
|
|
2022-01-13 16:13:47 +01:00
|
|
|
PROCEDURE draw* (scroll: tScroll);
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2021-09-03 01:15:33 +02:00
|
|
|
IF scroll.canvas # NIL THEN
|
2022-01-13 16:13:47 +01:00
|
|
|
_draw(scroll)
|
2021-09-03 01:15:33 +02:00
|
|
|
END
|
2022-01-13 16:13:47 +01:00
|
|
|
END draw;
|
2021-09-03 01:15:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE resize* (VAR scroll: tScroll; width, height: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
G.destroy(scroll.canvas);
|
|
|
|
scroll.canvas := G.CreateCanvas(width, height);
|
|
|
|
scroll.width := width;
|
|
|
|
scroll.height := height;
|
2022-01-13 16:13:47 +01:00
|
|
|
draw(scroll)
|
2021-05-23 11:43:28 +02:00
|
|
|
END resize;
|
|
|
|
|
|
|
|
|
2021-09-03 01:15:33 +02:00
|
|
|
PROCEDURE setValue* (VAR scroll: tScroll; value: INTEGER);
|
|
|
|
VAR
|
2022-01-20 18:15:39 +01:00
|
|
|
pos, maxPos, maxVal, n, m: INTEGER;
|
2021-09-03 01:15:33 +02:00
|
|
|
BEGIN
|
2022-01-20 18:15:39 +01:00
|
|
|
maxVal := scroll.maxVal;
|
2021-09-03 01:15:33 +02:00
|
|
|
IF scroll.vertical THEN
|
|
|
|
maxPos := scroll.height
|
|
|
|
ELSE
|
|
|
|
maxPos := scroll.width
|
|
|
|
END;
|
|
|
|
maxPos := maxPos - scroll.btnSize*2 - scroll.sliderSize + 1;
|
2022-01-20 18:15:39 +01:00
|
|
|
IF (value < 0) OR (maxVal <= 0) THEN
|
2021-09-03 01:15:33 +02:00
|
|
|
value := 0;
|
|
|
|
pos := 0
|
2022-01-20 18:15:39 +01:00
|
|
|
ELSIF value > maxVal THEN
|
|
|
|
value := maxVal;
|
2021-09-03 01:15:33 +02:00
|
|
|
pos := maxPos
|
|
|
|
ELSE
|
2022-01-20 18:15:39 +01:00
|
|
|
IF (maxPos + 1) >= maxVal THEN
|
|
|
|
n := (maxPos + 1) DIV maxVal;
|
|
|
|
m := (maxPos + 1) MOD maxVal;
|
2021-09-03 01:15:33 +02:00
|
|
|
pos := value*n + MIN(value, m)
|
|
|
|
ELSE
|
2022-01-20 18:15:39 +01:00
|
|
|
pos := FLOOR(FLT(value)*FLT(maxPos + 1)/FLT(maxVal))
|
2021-09-03 01:15:33 +02:00
|
|
|
END;
|
|
|
|
IF pos > maxPos THEN
|
|
|
|
pos := maxPos;
|
2022-01-20 18:15:39 +01:00
|
|
|
value := maxVal
|
2021-09-03 01:15:33 +02:00
|
|
|
END
|
|
|
|
END;
|
|
|
|
scroll.pos := pos;
|
|
|
|
scroll.value := value
|
|
|
|
END setValue;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE ceil (p, q: INTEGER): INTEGER;
|
|
|
|
RETURN p DIV q + ORD(p MOD q # 0)
|
|
|
|
END ceil;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE setPos (VAR scroll: tScroll; pos: INTEGER);
|
|
|
|
VAR
|
2022-01-20 18:15:39 +01:00
|
|
|
maxPos, value, maxVal, n, m, x, x0, q: INTEGER;
|
2021-09-03 01:15:33 +02:00
|
|
|
BEGIN
|
2022-01-20 18:15:39 +01:00
|
|
|
maxVal := scroll.maxVal;
|
|
|
|
IF maxVal > 0 THEN
|
2021-09-03 01:15:33 +02:00
|
|
|
IF scroll.vertical THEN
|
|
|
|
maxPos := scroll.height
|
|
|
|
ELSE
|
|
|
|
maxPos := scroll.width
|
|
|
|
END;
|
|
|
|
maxPos := maxPos - scroll.btnSize*2 - scroll.sliderSize + 1;
|
|
|
|
IF pos <= 0 THEN
|
|
|
|
pos := 0;
|
|
|
|
value := 0
|
|
|
|
ELSIF pos >= maxPos THEN
|
|
|
|
pos := maxPos;
|
2022-01-20 18:15:39 +01:00
|
|
|
value := maxVal
|
2021-09-03 01:15:33 +02:00
|
|
|
ELSE
|
2022-01-20 18:15:39 +01:00
|
|
|
IF maxVal <= maxPos + 1 THEN
|
|
|
|
n := (maxPos + 1) DIV maxVal;
|
|
|
|
m := (maxPos + 1) MOD maxVal;
|
2021-09-03 01:15:33 +02:00
|
|
|
|
|
|
|
q := m*(n + 1);
|
|
|
|
IF q < pos THEN
|
|
|
|
value := ceil(pos - m, n)
|
|
|
|
ELSIF q > pos THEN
|
|
|
|
value := ceil(pos, n + 1)
|
|
|
|
ELSE
|
|
|
|
value := m
|
|
|
|
END;
|
|
|
|
|
|
|
|
x := value*n + MIN(value, m);
|
|
|
|
x0 := (value - 1)*n + MIN(value - 1, m);
|
|
|
|
|
|
|
|
IF x - pos > pos - x0 THEN
|
|
|
|
pos := x0;
|
|
|
|
DEC(value)
|
|
|
|
ELSE
|
|
|
|
pos := x;
|
|
|
|
IF pos > maxPos THEN
|
|
|
|
pos := maxPos;
|
2022-01-20 18:15:39 +01:00
|
|
|
value := maxVal
|
2021-09-03 01:15:33 +02:00
|
|
|
END
|
|
|
|
END
|
|
|
|
ELSE
|
2022-01-20 18:15:39 +01:00
|
|
|
value := FLOOR(FLT(maxVal)*FLT(pos)/FLT(maxPos + 1))
|
2021-09-03 01:15:33 +02:00
|
|
|
END
|
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
pos := 0;
|
2022-01-20 18:15:39 +01:00
|
|
|
value := 0
|
2021-09-03 01:15:33 +02:00
|
|
|
END;
|
|
|
|
scroll.pos := pos;
|
|
|
|
scroll.value := value
|
|
|
|
END setPos;
|
|
|
|
|
|
|
|
|
2021-09-28 18:15:37 +02:00
|
|
|
PROCEDURE MouseMove (VAR scroll: tScroll; x, y: INTEGER);
|
2021-09-03 01:15:33 +02:00
|
|
|
VAR
|
|
|
|
c: INTEGER;
|
|
|
|
BEGIN
|
2021-10-03 19:44:42 +02:00
|
|
|
IF scroll.vertical THEN
|
|
|
|
c := y - scroll.top
|
|
|
|
ELSE
|
|
|
|
c := x - scroll.left
|
|
|
|
END;
|
|
|
|
setPos(scroll, scroll.pos0 + c - scroll.Slider);
|
2022-01-13 16:13:47 +01:00
|
|
|
draw(scroll)
|
2021-09-03 01:15:33 +02:00
|
|
|
END MouseMove;
|
|
|
|
|
|
|
|
|
2022-01-18 17:23:19 +01:00
|
|
|
PROCEDURE button (VAR scroll: tScroll);
|
2021-10-03 19:44:42 +02:00
|
|
|
VAR
|
2022-01-18 17:23:19 +01:00
|
|
|
btn: INTEGER;
|
2021-09-03 01:15:33 +02:00
|
|
|
BEGIN
|
2022-01-18 17:23:19 +01:00
|
|
|
WHILE scroll.btn # 0 DO
|
|
|
|
btn := scroll.btn;
|
|
|
|
setValue(scroll, scroll.value + btn);
|
|
|
|
draw(scroll);
|
|
|
|
IF scroll.change # NIL THEN
|
|
|
|
scroll.change
|
2021-09-28 18:15:37 +02:00
|
|
|
END;
|
2022-01-18 17:23:19 +01:00
|
|
|
scroll.btn := 0;
|
|
|
|
IF 0 IN K.MouseState() THEN
|
|
|
|
WHILE (0 IN K.MouseState()) & (scroll.delay > 0) DO
|
|
|
|
K.Pause(1);
|
|
|
|
DEC(scroll.delay)
|
|
|
|
END;
|
|
|
|
IF scroll.delay = 0 THEN
|
|
|
|
scroll.btn := btn;
|
|
|
|
scroll.delay := 3
|
|
|
|
ELSE
|
|
|
|
scroll.delay := DELAY
|
|
|
|
END
|
2021-09-28 18:15:37 +02:00
|
|
|
ELSE
|
2022-01-18 17:23:19 +01:00
|
|
|
scroll.delay := DELAY
|
2021-09-28 18:15:37 +02:00
|
|
|
END
|
|
|
|
END
|
2022-01-18 17:23:19 +01:00
|
|
|
END button;
|
2021-09-28 18:15:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE MouseDown (VAR scroll: tScroll; x, y: INTEGER);
|
2021-05-23 11:43:28 +02:00
|
|
|
VAR
|
2021-09-03 01:15:33 +02:00
|
|
|
c, size: INTEGER;
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2021-10-03 19:44:42 +02:00
|
|
|
DEC(x, scroll.left);
|
|
|
|
DEC(y, scroll.top);
|
|
|
|
scroll.mouse := TRUE;
|
|
|
|
IF U.between(1, x, scroll.width - 2) & U.between(1, y, scroll.height - 2) THEN
|
|
|
|
IF scroll.vertical THEN
|
|
|
|
c := y;
|
|
|
|
size := scroll.height
|
|
|
|
ELSE
|
|
|
|
c := x;
|
|
|
|
size := scroll.width
|
|
|
|
END;
|
|
|
|
IF U.between(scroll.btnSize + scroll.pos - 1, c, scroll.btnSize + scroll.pos + scroll.sliderSize - 1) THEN
|
|
|
|
scroll.pos0 := scroll.pos;
|
|
|
|
scroll.Slider := c
|
|
|
|
ELSIF U.between(0, c, scroll.btnSize - 1) THEN
|
2022-01-18 17:23:19 +01:00
|
|
|
scroll.btn := -1
|
2021-10-03 19:44:42 +02:00
|
|
|
ELSIF U.between(size - scroll.btnSize, c, size - 1) THEN
|
2022-01-18 17:23:19 +01:00
|
|
|
scroll.btn := 1
|
2021-10-03 19:44:42 +02:00
|
|
|
ELSE
|
|
|
|
setPos(scroll, c - scroll.btnSize - scroll.sliderSize DIV 2);
|
|
|
|
scroll.pos0 := scroll.pos;
|
|
|
|
scroll.Slider := c;
|
2022-01-13 16:13:47 +01:00
|
|
|
draw(scroll)
|
2021-09-03 01:15:33 +02:00
|
|
|
END
|
|
|
|
END
|
|
|
|
END MouseDown;
|
|
|
|
|
|
|
|
|
2021-09-28 18:15:37 +02:00
|
|
|
PROCEDURE mouse* (VAR scroll: tScroll);
|
|
|
|
VAR
|
|
|
|
msState: SET;
|
|
|
|
x, y: INTEGER;
|
2021-05-23 11:43:28 +02:00
|
|
|
BEGIN
|
2021-09-28 18:15:37 +02:00
|
|
|
K.mouse(msState, x, y);
|
|
|
|
IF 0 IN msState THEN
|
2021-10-03 19:44:42 +02:00
|
|
|
IF ~scroll.mouse THEN
|
|
|
|
MouseDown(scroll, x, y)
|
|
|
|
ELSIF scroll.Slider # -1 THEN
|
|
|
|
MouseMove(scroll, x, y)
|
|
|
|
END
|
2021-09-28 18:15:37 +02:00
|
|
|
ELSIF scroll.mouse THEN
|
|
|
|
MouseUp(scroll);
|
2022-01-13 16:13:47 +01:00
|
|
|
draw(scroll)
|
2022-01-18 17:23:19 +01:00
|
|
|
END;
|
|
|
|
button(scroll)
|
2021-09-28 18:15:37 +02:00
|
|
|
END mouse;
|
|
|
|
|
|
|
|
|
2021-05-23 11:43:28 +02:00
|
|
|
END Scroll.
|