Subversion Repositories Kolibri OS

Rev

Rev 9448 | Rev 9560 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8728 leency 1
(*
2
    Copyright 2021 Anton Krotov
3
 
4
    This file is part of CEdit.
5
 
6
    CEdit is free software: you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation, either version 3 of the License, or
9
    (at your option) any later version.
10
 
11
    CEdit is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public License
17
    along with CEdit. If not, see .
18
*)
19
 
20
MODULE Graph;
21
 
9448 akron1 22
IMPORT SYSTEM, KOSAPI, Lines, Languages;
8728 leency 23
 
24
CONST
25
 
26
    modeCOPY = 0;
27
    modeNOT  = 1;
28
    modeXOR  = 2;
29
 
9174 akron1 30
    triUp* = FALSE;
31
    triDown* = TRUE;
32
    triLeft* = FALSE;
33
    triRight* = TRUE;
34
 
8728 leency 35
TYPE
36
 
37
    tFont* = POINTER TO RECORD
38
        handle*: INTEGER;
39
        height*: INTEGER;
40
        width*:  INTEGER;
41
        size:    INTEGER;
42
        name*:   ARRAY 256 OF WCHAR
43
    END;
44
 
45
    tCanvas* = POINTER TO RECORD
46
        bitmap: INTEGER;
47
        width*, height*: INTEGER;
48
        color, backColor, textColor: INTEGER;
49
        font*: tFont;
50
        mode: INTEGER
51
    END;
52
 
53
 
9431 akron1 54
PROCEDURE getRGB* (color: INTEGER; VAR r, g, b: BYTE);
55
BEGIN
56
	b := color MOD 256;
57
	g := color DIV 256 MOD 256;
58
	r := color DIV 65536 MOD 256
59
END getRGB;
60
 
61
 
8728 leency 62
PROCEDURE DrawCanvas* (canvas: tCanvas; x, y: INTEGER);
63
BEGIN
9448 akron1 64
    KOSAPI.sysfunc7(65, canvas.bitmap, canvas.width*65536 + canvas.height, x*65536 + y, 32, 0, 0)
8728 leency 65
END DrawCanvas;
66
 
67
 
68
PROCEDURE SetColor* (canvas: tCanvas; color: INTEGER);
69
BEGIN
70
    canvas.color := color
71
END SetColor;
72
 
73
 
74
PROCEDURE SetTextColor* (canvas: tCanvas; color: INTEGER);
75
BEGIN
76
    canvas.textColor := color
77
END SetTextColor;
78
 
79
 
80
PROCEDURE SetBkColor* (canvas: tCanvas; color: INTEGER);
81
BEGIN
82
    canvas.backColor := color
83
END SetBkColor;
84
 
85
 
86
PROCEDURE CreateFont* (height: INTEGER; name: ARRAY OF WCHAR; attr: SET): tFont;
87
VAR
88
    font: tFont;
89
BEGIN
90
    NEW(font);
91
    font.size := MAX(MIN(height, 8), 1);
92
    font.width := font.size*8;
93
    font.height := font.size*16;
94
    DEC(font.size);
95
    font.name := name
96
    RETURN font
97
END CreateFont;
98
 
99
 
100
PROCEDURE SetFont* (canvas: tCanvas; font: tFont);
101
BEGIN
102
    canvas.font := font
103
END SetFont;
104
 
105
 
106
PROCEDURE HLine* (canvas: tCanvas; y, x1, x2: INTEGER);
107
VAR
108
    X1, X2, i: INTEGER;
109
    ptr: INTEGER;
110
    color: INTEGER;
111
BEGIN
112
    X1 := MAX(MIN(x1, x2), 0);
113
    X2 := MIN(MAX(x1, x2), canvas.width - 1);
114
    IF (0 <= y) & (y < canvas.height) THEN
115
        color := canvas.color;
9295 akron1 116
        ptr := canvas.bitmap + 4*(y*canvas.width + X1);
8728 leency 117
        FOR i := X1 TO X2 DO
118
            SYSTEM.PUT32(ptr, color);
119
            INC(ptr, 4)
120
        END
121
    END
122
END HLine;
123
 
124
 
125
PROCEDURE VLine* (canvas: tCanvas; x, y1, y2: INTEGER);
126
VAR
127
    Y1, Y2, i: INTEGER;
128
    ptr: INTEGER;
129
    color: INTEGER;
130
BEGIN
131
    Y1 := MAX(MIN(y1, y2), 0);
132
    Y2 := MIN(MAX(y1, y2), canvas.height - 1);
133
    IF (0 <= x) & (x < canvas.width) THEN
134
        color := canvas.color;
9295 akron1 135
        ptr := canvas.bitmap + 4*(Y1*canvas.width + x);
8728 leency 136
        FOR i := Y1 TO Y2 DO
137
            IF canvas.mode = modeNOT THEN
138
                SYSTEM.GET32(ptr, color);
139
                color := ORD(-BITS(color)*{0..23})
140
            ELSIF canvas.mode = modeXOR THEN
141
                SYSTEM.GET32(ptr, color);
142
                color := ORD((BITS(color)/BITS(canvas.color))*{0..23})
143
            END;
144
            SYSTEM.PUT32(ptr, color);
145
            INC(ptr, canvas.width*4)
146
        END
147
    END
148
END VLine;
149
 
150
 
151
PROCEDURE notVLine* (canvas: tCanvas; x, y1, y2: INTEGER);
152
BEGIN
153
    IF (0 <= y1) & (y1 < canvas.height) & (0 <= y2) & (y2 < canvas.height) THEN
154
        canvas.mode := modeNOT;
155
        VLine(canvas, x, y1, y2);
156
        canvas.mode := modeCOPY
157
    END
158
END notVLine;
159
 
160
 
161
PROCEDURE xorVLine* (canvas: tCanvas; x, y1, y2: INTEGER);
162
BEGIN
163
    IF (0 <= y1) & (y1 < canvas.height) & (0 <= y2) & (y2 < canvas.height) THEN
164
        canvas.mode := modeXOR;
165
        SetColor(canvas, 0FF0000H);
166
        VLine(canvas, x, y1, y2);
167
        canvas.mode := modeCOPY
168
    END
169
END xorVLine;
170
 
171
 
172
PROCEDURE DLine* (canvas: tCanvas; x1, x2, y: INTEGER; k: INTEGER);
173
VAR
174
    ptr: INTEGER;
175
    color: INTEGER;
176
    d: INTEGER;
177
BEGIN
9522 akron1 178
	ASSERT(ABS(k) = 1);
8728 leency 179
    color := canvas.color;
9295 akron1 180
    ptr := canvas.bitmap + 4*(y*canvas.width + x1);
9522 akron1 181
    d := 4*(1 - canvas.width*k);
8728 leency 182
    WHILE x1 <= x2 DO
183
        SYSTEM.PUT32(ptr, color);
184
        INC(ptr, d);
185
        INC(x1)
186
    END
187
END DLine;
188
 
189
 
9174 akron1 190
PROCEDURE Triangle* (canvas: tCanvas; x1, y1, x2, y2: INTEGER; orientation: BOOLEAN);
191
VAR
192
	i, a, b, d: INTEGER;
9208 akron1 193
	line: PROCEDURE (canvas: tCanvas; c, c1, c2: INTEGER);
9174 akron1 194
BEGIN
9208 akron1 195
	line := NIL;
9174 akron1 196
	d := ORD(orientation)*2 - 1;
197
	IF y1 = y2 THEN
198
		i := y1;
199
		a := MIN(x1, x2);
200
		b := MAX(x1, x2);
9208 akron1 201
		line := HLine
9174 akron1 202
	ELSIF x1 = x2 THEN
203
		i := x1;
204
		a := MIN(y1, y2);
205
		b := MAX(y1, y2);
9208 akron1 206
		line := VLine
207
	END;
208
	IF line # NIL THEN
9174 akron1 209
		WHILE a <= b DO
9208 akron1 210
			line(canvas, i, a, b);
9174 akron1 211
			INC(i, d);
212
			INC(a);
213
			DEC(b)
214
		END
215
	END
216
END Triangle;
217
 
218
 
8728 leency 219
PROCEDURE FillRect* (canvas: tCanvas; left, top, right, bottom: INTEGER);
220
VAR
221
    y: INTEGER;
222
BEGIN
223
    FOR y := top TO bottom DO
224
        HLine(canvas, y, left, right)
225
    END
226
END FillRect;
227
 
228
 
229
PROCEDURE Rect* (canvas: tCanvas; left, top, right, bottom: INTEGER);
230
BEGIN
231
    HLine(canvas, top, left, right);
232
    HLine(canvas, bottom, left, right);
233
    VLine(canvas, left, top, bottom);
234
    VLine(canvas, right, top, bottom)
235
END Rect;
236
 
237
 
238
PROCEDURE clear* (canvas: tCanvas);
239
VAR
240
    ptr, ptr2, w, i: INTEGER;
241
BEGIN
242
    HLine(canvas, 0, 0, canvas.width - 1);
243
    w := canvas.width*4;
244
    ptr := canvas.bitmap;
245
    ptr2 := ptr;
246
    i := canvas.height - 1;
247
    WHILE i > 0 DO
248
        INC(ptr2, w);
249
        SYSTEM.MOVE(ptr, ptr2, w);
250
        DEC(i)
251
    END
252
END clear;
253
 
254
 
9193 akron1 255
PROCEDURE TextOut* (canvas: tCanvas; x, y: INTEGER; text: INTEGER; n: INTEGER; delimColor: INTEGER);
8728 leency 256
CONST
257
    WCHAR_SIZE = 2;
258
VAR
9208 akron1 259
    color, i, font: INTEGER;
9174 akron1 260
    c: WCHAR;
8728 leency 261
BEGIN
262
    IF (0 <= y) & (y <= canvas.height - canvas.font.height - 1) THEN
263
        IF x < 0 THEN
264
            i := -(x DIV canvas.font.width);
265
            INC(x, i*canvas.font.width);
266
            DEC(n, i)
267
        ELSE
268
            i := 0
269
        END;
270
        IF n > 0 THEN
271
            n := MAX(MIN(n, (canvas.width - x) DIV canvas.font.width), 0);
272
            color := canvas.color;
273
            canvas.color := canvas.backColor;
274
            FillRect(canvas, x, y, x + n*canvas.font.width, y + canvas.font.height);
275
            canvas.color := color;
9208 akron1 276
            font := LSL(28H + canvas.font.size, 24);
9174 akron1 277
            WHILE n > 0 DO
278
                SYSTEM.GET(text + i*WCHAR_SIZE, c);
279
                IF ~Lines.isSpace(c) THEN
9193 akron1 280
                	IF Languages.isDelim(c) THEN
9208 akron1 281
                		color := delimColor
9193 akron1 282
                	ELSE
9208 akron1 283
                		color := canvas.textColor
9193 akron1 284
                	END;
9448 akron1 285
                    KOSAPI.sysfunc6(4, x*65536 + y, font + color, SYSTEM.ADR(c), 1, canvas.bitmap - 8)
9174 akron1 286
                END;
8728 leency 287
                INC(x, canvas.font.width);
288
                INC(i);
289
                DEC(n)
9174 akron1 290
            END
8728 leency 291
        END
292
    END
293
END TextOut;
294
 
295
 
296
PROCEDURE TextOut2* (canvas: tCanvas; x, y: INTEGER; text: ARRAY OF WCHAR; n: INTEGER);
297
BEGIN
9193 akron1 298
    TextOut(canvas, x, y, SYSTEM.ADR(text[0]), n, canvas.textColor)
8728 leency 299
END TextOut2;
300
 
301
 
302
PROCEDURE CreateCanvas* (width, height: INTEGER): tCanvas;
303
VAR
304
    canvas: tCanvas;
305
BEGIN
306
    NEW(canvas);
9448 akron1 307
    canvas.bitmap := KOSAPI.malloc(width*height*4 + 8);
8728 leency 308
    ASSERT(canvas.bitmap # 0);
309
    SYSTEM.PUT32(canvas.bitmap, width);
310
    SYSTEM.PUT32(canvas.bitmap + 4, height);
311
    INC(canvas.bitmap, 8);
312
    canvas.width := width;
313
    canvas.height := height;
314
    canvas.mode := modeCOPY
315
    RETURN canvas
316
END CreateCanvas;
317
 
318
 
319
PROCEDURE destroy* (VAR canvas: tCanvas);
320
BEGIN
321
    IF canvas # NIL THEN
9448 akron1 322
        canvas.bitmap := KOSAPI.free(canvas.bitmap);
8728 leency 323
        DISPOSE(canvas)
324
    END
325
END destroy;
326
 
327
 
328
END Graph.