Subversion Repositories Kolibri OS

Rev

Rev 9295 | Rev 9448 | 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
 
9193 akron1 22
IMPORT SYSTEM, K := 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
64
    K.sysfunc7(65, canvas.bitmap, canvas.width*65536 + canvas.height, x*65536 + y, 32, 0, 0);
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
178
    color := canvas.color;
9295 akron1 179
    ptr := canvas.bitmap + 4*(y*canvas.width + x1);
8728 leency 180
    IF k = -1 THEN
9295 akron1 181
        d := 4*(canvas.width + 1)
8728 leency 182
    ELSIF k = 1 THEN
9295 akron1 183
        d := 4*(1 - canvas.width)
8728 leency 184
    END;
185
    WHILE x1 <= x2 DO
186
        SYSTEM.PUT32(ptr, color);
187
        INC(ptr, d);
188
        INC(x1)
189
    END
190
END DLine;
191
 
192
 
9174 akron1 193
PROCEDURE Triangle* (canvas: tCanvas; x1, y1, x2, y2: INTEGER; orientation: BOOLEAN);
194
VAR
195
	i, a, b, d: INTEGER;
9208 akron1 196
	line: PROCEDURE (canvas: tCanvas; c, c1, c2: INTEGER);
9174 akron1 197
BEGIN
9208 akron1 198
	line := NIL;
9174 akron1 199
	d := ORD(orientation)*2 - 1;
200
	IF y1 = y2 THEN
201
		i := y1;
202
		a := MIN(x1, x2);
203
		b := MAX(x1, x2);
9208 akron1 204
		line := HLine
9174 akron1 205
	ELSIF x1 = x2 THEN
206
		i := x1;
207
		a := MIN(y1, y2);
208
		b := MAX(y1, y2);
9208 akron1 209
		line := VLine
210
	END;
211
	IF line # NIL THEN
9174 akron1 212
		WHILE a <= b DO
9208 akron1 213
			line(canvas, i, a, b);
9174 akron1 214
			INC(i, d);
215
			INC(a);
216
			DEC(b)
217
		END
218
	END
219
END Triangle;
220
 
221
 
8728 leency 222
PROCEDURE FillRect* (canvas: tCanvas; left, top, right, bottom: INTEGER);
223
VAR
224
    y: INTEGER;
225
BEGIN
226
    FOR y := top TO bottom DO
227
        HLine(canvas, y, left, right)
228
    END
229
END FillRect;
230
 
231
 
232
PROCEDURE Rect* (canvas: tCanvas; left, top, right, bottom: INTEGER);
233
BEGIN
234
    HLine(canvas, top, left, right);
235
    HLine(canvas, bottom, left, right);
236
    VLine(canvas, left, top, bottom);
237
    VLine(canvas, right, top, bottom)
238
END Rect;
239
 
240
 
241
PROCEDURE clear* (canvas: tCanvas);
242
VAR
243
    ptr, ptr2, w, i: INTEGER;
244
BEGIN
245
    HLine(canvas, 0, 0, canvas.width - 1);
246
    w := canvas.width*4;
247
    ptr := canvas.bitmap;
248
    ptr2 := ptr;
249
    i := canvas.height - 1;
250
    WHILE i > 0 DO
251
        INC(ptr2, w);
252
        SYSTEM.MOVE(ptr, ptr2, w);
253
        DEC(i)
254
    END
255
END clear;
256
 
257
 
9193 akron1 258
PROCEDURE TextOut* (canvas: tCanvas; x, y: INTEGER; text: INTEGER; n: INTEGER; delimColor: INTEGER);
8728 leency 259
CONST
260
    WCHAR_SIZE = 2;
261
VAR
9208 akron1 262
    color, i, font: INTEGER;
9174 akron1 263
    c: WCHAR;
8728 leency 264
BEGIN
265
    IF (0 <= y) & (y <= canvas.height - canvas.font.height - 1) THEN
266
        IF x < 0 THEN
267
            i := -(x DIV canvas.font.width);
268
            INC(x, i*canvas.font.width);
269
            DEC(n, i)
270
        ELSE
271
            i := 0
272
        END;
273
        IF n > 0 THEN
274
            n := MAX(MIN(n, (canvas.width - x) DIV canvas.font.width), 0);
275
            color := canvas.color;
276
            canvas.color := canvas.backColor;
277
            FillRect(canvas, x, y, x + n*canvas.font.width, y + canvas.font.height);
278
            canvas.color := color;
9208 akron1 279
            font := LSL(28H + canvas.font.size, 24);
9174 akron1 280
            WHILE n > 0 DO
281
                SYSTEM.GET(text + i*WCHAR_SIZE, c);
282
                IF ~Lines.isSpace(c) THEN
9193 akron1 283
                	IF Languages.isDelim(c) THEN
9208 akron1 284
                		color := delimColor
9193 akron1 285
                	ELSE
9208 akron1 286
                		color := canvas.textColor
9193 akron1 287
                	END;
9208 akron1 288
                    K.sysfunc6(4, x*65536 + y, font + color, SYSTEM.ADR(c), 1, canvas.bitmap - 8)
9174 akron1 289
                END;
8728 leency 290
                INC(x, canvas.font.width);
291
                INC(i);
292
                DEC(n)
9174 akron1 293
            END
8728 leency 294
        END
295
    END
296
END TextOut;
297
 
298
 
299
PROCEDURE TextOut2* (canvas: tCanvas; x, y: INTEGER; text: ARRAY OF WCHAR; n: INTEGER);
300
BEGIN
9193 akron1 301
    TextOut(canvas, x, y, SYSTEM.ADR(text[0]), n, canvas.textColor)
8728 leency 302
END TextOut2;
303
 
304
 
305
PROCEDURE CreateCanvas* (width, height: INTEGER): tCanvas;
306
VAR
307
    canvas: tCanvas;
308
BEGIN
309
    NEW(canvas);
310
    canvas.bitmap := K.malloc(width*height*4 + 8);
311
    ASSERT(canvas.bitmap # 0);
312
    SYSTEM.PUT32(canvas.bitmap, width);
313
    SYSTEM.PUT32(canvas.bitmap + 4, height);
314
    INC(canvas.bitmap, 8);
315
    canvas.width := width;
316
    canvas.height := height;
317
    canvas.mode := modeCOPY
318
    RETURN canvas
319
END CreateCanvas;
320
 
321
 
322
PROCEDURE destroy* (VAR canvas: tCanvas);
323
BEGIN
324
    IF canvas # NIL THEN
325
        canvas.bitmap := K.free(canvas.bitmap);
326
        DISPOSE(canvas)
327
    END
328
END destroy;
329
 
330
 
331
END Graph.