2021-05-23 11:43:28 +02:00
|
|
|
(*
|
2022-01-02 19:57:13 +01:00
|
|
|
Copyright 2021, 2022 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 Clipboard;
|
|
|
|
|
2021-12-20 20:09:01 +01:00
|
|
|
IMPORT SYSTEM, KOSAPI, E := Encodings, Lines, K := KolibriOS;
|
2021-05-23 11:43:28 +02:00
|
|
|
|
|
|
|
CONST
|
|
|
|
TTEXT = 0;
|
|
|
|
lenEOL* = 2;
|
|
|
|
|
|
|
|
TYPE
|
|
|
|
tBuffer* = POINTER TO RECORD
|
|
|
|
dataPtr*: INTEGER;
|
|
|
|
pos: INTEGER
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE free (ptr: INTEGER);
|
|
|
|
BEGIN
|
2021-12-20 20:09:01 +01:00
|
|
|
ptr := KOSAPI.free(ptr)
|
2021-05-23 11:43:28 +02:00
|
|
|
END free;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE bufSize* (buffer: tBuffer): INTEGER;
|
|
|
|
RETURN buffer.pos - buffer.dataPtr
|
|
|
|
END bufSize;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE put* (buffer: tBuffer);
|
|
|
|
VAR
|
|
|
|
a, cnt, size: INTEGER;
|
|
|
|
ptr, pchar: INTEGER;
|
|
|
|
wch: WCHAR;
|
|
|
|
BEGIN
|
|
|
|
cnt := bufSize(buffer) DIV 2;
|
|
|
|
size := cnt + 12;
|
2021-12-20 20:09:01 +01:00
|
|
|
a := KOSAPI.malloc(size);
|
2021-05-23 11:43:28 +02:00
|
|
|
ASSERT(a # 0);
|
|
|
|
SYSTEM.PUT32(a + 4, TTEXT);
|
|
|
|
SYSTEM.PUT32(a + 8, 1);
|
|
|
|
pchar := a + 12;
|
|
|
|
ptr := buffer.dataPtr;
|
|
|
|
WHILE cnt > 0 DO
|
|
|
|
SYSTEM.GET(ptr, wch);
|
2022-01-02 19:57:13 +01:00
|
|
|
IF wch = Lines.TAB1 THEN
|
|
|
|
DEC(size)
|
|
|
|
ELSIF wch = Lines.NUL THEN
|
|
|
|
SYSTEM.PUT(pchar, 0X);
|
2021-09-03 01:15:33 +02:00
|
|
|
INC(pchar)
|
|
|
|
ELSE
|
2022-01-02 19:57:13 +01:00
|
|
|
SYSTEM.PUT(pchar, CHR(E.UNI[ORD(wch), E.CP866] MOD 256));
|
|
|
|
INC(pchar)
|
2021-09-03 01:15:33 +02:00
|
|
|
END;
|
2021-05-23 11:43:28 +02:00
|
|
|
INC(ptr, 2);
|
|
|
|
DEC(cnt)
|
|
|
|
END;
|
2021-09-03 01:15:33 +02:00
|
|
|
SYSTEM.PUT32(a, size);
|
2021-12-20 20:09:01 +01:00
|
|
|
K.CBDelSlot;
|
|
|
|
K.CBWrite(size, a)
|
2021-05-23 11:43:28 +02:00
|
|
|
END put;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE create* (bufSize: INTEGER): tBuffer;
|
|
|
|
VAR
|
|
|
|
res: tBuffer;
|
|
|
|
BEGIN
|
|
|
|
NEW(res);
|
2021-12-20 20:09:01 +01:00
|
|
|
res.dataPtr := KOSAPI.malloc(bufSize*SYSTEM.SIZE(WCHAR) + 4096);
|
2021-05-23 11:43:28 +02:00
|
|
|
ASSERT(res.dataPtr # 0);
|
|
|
|
res.pos := res.dataPtr
|
|
|
|
RETURN res
|
|
|
|
END create;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE destroy* (VAR buffer: tBuffer);
|
|
|
|
BEGIN
|
|
|
|
IF buffer # NIL THEN
|
|
|
|
IF buffer.dataPtr # 0 THEN
|
|
|
|
free(buffer.dataPtr)
|
|
|
|
END;
|
|
|
|
DISPOSE(buffer)
|
|
|
|
END
|
|
|
|
END destroy;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE append* (buffer: tBuffer; line: Lines.tLine; first, last: INTEGER);
|
|
|
|
VAR
|
|
|
|
strSize: INTEGER;
|
|
|
|
BEGIN
|
|
|
|
strSize := (last - first + 1)*SYSTEM.SIZE(WCHAR);
|
|
|
|
IF strSize > 0 THEN
|
|
|
|
SYSTEM.MOVE(Lines.getPChar(line, first), buffer.pos, strSize);
|
|
|
|
INC(buffer.pos, strSize)
|
|
|
|
END
|
|
|
|
END append;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE appends* (buffer: tBuffer; s: ARRAY OF WCHAR; first, last: INTEGER);
|
|
|
|
VAR
|
|
|
|
strSize: INTEGER;
|
|
|
|
BEGIN
|
|
|
|
strSize := (last - first + 1)*SYSTEM.SIZE(WCHAR);
|
|
|
|
IF strSize > 0 THEN
|
|
|
|
SYSTEM.MOVE(SYSTEM.ADR(s[first]), buffer.pos, strSize);
|
|
|
|
INC(buffer.pos, strSize)
|
|
|
|
END
|
|
|
|
END appends;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE eol* (buffer: tBuffer);
|
|
|
|
BEGIN
|
2021-07-21 18:13:29 +02:00
|
|
|
appends(buffer, 0DX + 0AX, 0, 1)
|
2021-05-23 11:43:28 +02:00
|
|
|
END eol;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE eot* (buffer: tBuffer);
|
|
|
|
END eot;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE available* (): BOOLEAN;
|
|
|
|
VAR
|
|
|
|
ptr: INTEGER;
|
|
|
|
n, size, typ, x: INTEGER;
|
|
|
|
res: BOOLEAN;
|
|
|
|
BEGIN
|
|
|
|
res := FALSE;
|
2021-12-20 20:09:01 +01:00
|
|
|
n := K.CBCount();
|
2021-05-23 11:43:28 +02:00
|
|
|
IF n > 0 THEN
|
2021-12-20 20:09:01 +01:00
|
|
|
ptr := K.CBRead(n - 1);
|
2021-05-23 11:43:28 +02:00
|
|
|
SYSTEM.GET32(ptr, size);
|
|
|
|
SYSTEM.GET32(ptr + 4, typ);
|
|
|
|
SYSTEM.GET(ptr + 8, x);
|
|
|
|
res := (typ = TTEXT) & (x = 1);
|
|
|
|
free(ptr)
|
|
|
|
END
|
|
|
|
RETURN res
|
|
|
|
END available;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE get* (VAR cnt: INTEGER): INTEGER;
|
|
|
|
VAR
|
|
|
|
ptr: INTEGER;
|
|
|
|
BEGIN
|
|
|
|
ptr := 0;
|
|
|
|
cnt := 0;
|
|
|
|
IF available() THEN
|
2021-12-20 20:09:01 +01:00
|
|
|
ptr := K.CBRead(K.CBCount() - 1);
|
2021-05-23 11:43:28 +02:00
|
|
|
SYSTEM.GET32(ptr, cnt);
|
|
|
|
DEC(cnt, 12);
|
|
|
|
INC(ptr, 12)
|
|
|
|
END
|
|
|
|
RETURN ptr
|
|
|
|
END get;
|
|
|
|
|
|
|
|
|
|
|
|
END Clipboard.
|