-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrichmemoutils.pas
368 lines (324 loc) · 10.7 KB
/
richmemoutils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
{
richmemoutils.pas
Author: Dmitry 'skalogryz' Boyarintsev
*****************************************************************************
* *
* This file is part of the Lazarus Component Library (LCL) *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program 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. *
* *
*****************************************************************************
}
unit RichMemoUtils;
interface
{$mode objfpc}{$h+}
uses
Types, SysUtils, Classes, Graphics, LazFileUtils, LazUTF8, RichMemo;
const
NoResize : TSize = ( cx: 0; cy : 0 );
var
{ Disclaimer: the function would insert an image file into RichMemo
(if implemented by the widgetset) But in a very inefficient way.
The image would be read again and the memory would be re-allocated for
the image every time. So please, don't use it for smileys in
your chat instant messaging. A better API (with data caching) is considered.
(That's why this method is not part of TCustomRichMemo class)
APos - position in the text
AImgSize - size to be inserted (in POINTS, not pixels!).
if both width and height are 0, the image would not be resized at all.
}
InsertImageFromFile : function (const ARichMemo: TCustomRichMemo; APos: Integer;
const FileNameUTF8: string;
const AImgSize: TSize
): Boolean = nil;
function InsertImageFromFileNoResize (const ARichMemo: TCustomRichMemo; APos: Integer;
const FileNameUTF8: string): Boolean;
procedure LoadRTFFile(const ARichMemo: TCustomRichMemo; const FileNameUTF8: string);
procedure SaveRTFFile(const ARichMemo: TCustomRichMemo; const FileNameUTF8: string);
procedure InsertStyledText(const ARichMemo: TCustomRichMemo; const TextUTF8: String; AStyle: TFontStyles;
InsPos : Integer = -1 );
procedure InsertColorStyledText(const ARichMemo: TCustomRichMemo; const TextUTF8: String; AColor: TColor; AStyle: TFontStyles;
InsPos : Integer = -1 );
procedure InsertFontText(const ARichMemo: TCustomRichMemo; const TextUTF8: String; const prms: TFontParams;
InsPos : Integer = -1 );
type
TCopyOptions = set of (
coCopyBuf // copy via buffer. Always used by default, if RichMemo tries to copy in itself,
// the flag is only helpful if rich memos are different
);
// copies text from one rich memo to another
// if ASrc and ADst are the same rich memo, calls to CopyRichTextViaBuf()
// returns the number of characters copied
function CopyRichText(const ASrc: TCustomRichMemo;
SrcStart, SrcLen: Integer;
const ADst: TCustomRichMemo;
DstStart, DstLen: integer;
const CopyOpts: TCopyOptions = []
): Integer;
procedure AddFontStyle(rm: TCustomRichMemo; fs: TFontStyles);
procedure AddFontStyle(rm: TCustomRichMemo; fs: TFontStyles; ofs, len: integer);
procedure RemoveFontStyle(rm: TCustomRichMemo; fs: TFontStyles);
procedure RemoveFontStyle(rm: TCustomRichMemo; fs: TFontStyles; ofs, len: integer);
implementation
procedure AddFontStyle(rm: TCustomRichMemo; fs: TFontStyles);
begin
if (rm = nil) then Exit;
AddFontStyle(rm, fs, rm.SelStart, rm.SelLength);
end;
procedure AddFontStyle(rm: TCustomRichMemo; fs: TFontStyles; ofs, len: integer);
var
p : TFontParams;
begin
if (rm = nil) then Exit;
InitFontParams(p);
rm.SetRangeParams(ofs, len, [tmm_Styles], p, fs, []);
end;
procedure RemoveFontStyle(rm: TCustomRichMemo; fs: TFontStyles);
begin
if (rm = nil) then Exit;
RemoveFontStyle(rm, fs, rm.SelStart, rm.SelLength);
end;
procedure RemoveFontStyle(rm: TCustomRichMemo; fs: TFontStyles; ofs, len: integer);
var
p : TFontParams;
begin
if (rm = nil) then Exit;
InitFontParams(p);
rm.SetRangeParams(ofs, len, [tmm_Styles], p, [], fs);
end;
procedure InsertFontText(const ARichMemo: TCustomRichMemo; const TextUTF8: String; const prms: TFontParams;
InsPos : Integer = -1 );
var
len : Integer;
begin
if InsPos<0 then InsPos:=UTF8Length(ARichMemo.Text);
len:=ARichMemo.InDelText(TextUTF8, InsPos, 0);
ARichMemo.SetTextAttributes(InsPos, len, prms);
end;
procedure InsertColorStyledText(const ARichMemo: TCustomRichMemo; const TextUTF8: String; AColor: TColor; AStyle: TFontStyles;
InsPos : Integer = -1 );
var
sel: Integer;
ft : TFontParams;
begin
if InsPos<0 then InsPos:=UTF8Length(ARichMemo.Text);
ARichMemo.GetTextAttributes(InsPos, ft);
ft.Style:=AStyle;
ft.Color:=AColor;
InsertFontText(ARichMemo, TextUTF8, ft, InsPos);
end;
procedure InsertStyledText(const ARichMemo: TCustomRichMemo; const TextUTF8: String; AStyle: TFontStyles;
InsPos : Integer );
var
sel: Integer;
ft : TFontParams;
begin
if InsPos<0 then InsPos:=UTF8Length(ARichMemo.Text);
ARichMemo.GetTextAttributes(InsPos, ft);
ft.Style:=AStyle;
InsertFontText(ARichMemo, TextUTF8, ft, InsPos);
end;
function InsertImageFileDummy(const ARichMemo: TCustomRichMemo; APos: Integer;
const FileNameUTF8: string;
const AImgSize: TSize): Boolean;
begin
Result:=false;
end;
function InsertImageFromFileNoResize (const ARichMemo: TCustomRichMemo; APos: Integer;
const FileNameUTF8: string): Boolean;
begin
Result:=InsertImageFromFile(ARichMemo, APos, FileNameUTF8, NoResize);
end;
{$IFDEF USELCLUtf8}
procedure LoadRTFFile(const ARichMemo: TCustomRichMemo; const FileNameUTF8: string);
var
fs : THandleStream;
h : THandle;
begin
if not Assigned(ARichMemo) then Exit;
h:= FileOpenUTF8(FileNameUTF8, fmShareDenyNone or fmOpenRead);
fs := THandleStream.Create( h );
try
ARichMemo.LoadRichText(fs);
finally
fs.Free;
end;
FileClose(h);
end;
{$ENDIF}
procedure LoadRTFFile(const ARichMemo: TCustomRichMemo; const FileNameUTF8: string);
var
fs : TFileStream;
begin
if not Assigned(ARichMemo) then Exit;
fs:= TFileStream.Create( UTF8Decode(FileNameUTF8), fmShareDenyNone or fmOpenRead);
try
ARichMemo.LoadRichText(fs);
finally
fs.Free;
end;
end;
procedure SaveRTFFile(const ARichMemo: TCustomRichMemo; const FileNameUTF8: string);
var
fs : TFileStream;
begin
if not Assigned(ARichMemo) then Exit;
fs:= TFileStream.Create( UTF8Decode(FileNameUTF8), fmCreate);
try
ARichMemo.SaveRichText(fs);
finally
fs.Free;
end;
end;
type
TBuffer = class(TObject)
text : string;
len : Integer;
font : TFontParams;
para : TParaMetric;
palign : TParaAlignment;
next : TBuffer;
end;
type
TCopyResult = record
TotalLen : integer;
end;
procedure _CopyBuf(const ASrc: TCustomRichMemo;
SrcStart, SrcLen: Integer;
const ADst: TCustomRichMemo;
DstStart, DstLen: integer;
const CopyOpts: TCopyOptions;
out Result: TcopyResult);
var
bf : TBuffer;
hd : TBuffer;
lt : TBuffer;
i,iend : integer;
ofs,ln : integer;
l: integer;
begin
lt := nil;
hd := nil;
Result.TotalLen := 0;
try
i:=SrcStart;
iend := SrcStart + SrcLen;
while i < iend do begin
bf := TBuffer.Create;
ASrc.GetStyleRange(i, ofs, ln);
if ofs + ln > iend then ln := iend - ofs;
l := ln+ofs - i;
bf.text := ASrc.GetText(i, l);
ASrc.GetTextAttributes(ofs, bf.font);
bf.len := l;
if hd = nil then hd := bf;
if lt <> nil then lt.Next := bf;
lt := bf;
inc(i, ln);
end;
while Assigned(hd) do begin
ADst.InDelText(hd.text, DstStart, DstLen);
ADst.SetTextAttributes(DstStart, hd.len, hd.font);
inc(DstStart, hd.len);
inc(Result.TotalLen, hd.len);
if DstLen > 0 then DstLen := 0;
lt := hd;
hd := hd.Next;
lt.Free;
end;
finally
while Assigned(hd) do begin // an exception occurred?
lt := hd;
hd := hd.Next;
lt.Free;
end;
end;
end;
// consume less memory, BUT can be slower. (depending on WS implementation)
procedure _CopyDirect(const ASrc: TCustomRichMemo;
SrcStart, SrcLen: Integer;
const ADst: TCustomRichMemo;
DstStart, DstLen: integer;
const CopyOpts: TCopyOptions;
out Result: TcopyResult);
var
i : integer;
l : integer;
iend: Integer;
isFirst: boolean;
txt: string;
ofs: integer;
ln: integer;
fnt: TFontParams;
copyParaParams: Boolean;
begin
i:=SrcStart;
iend := SrcStart + SrcLen;
copyParaParams := false;
Result.TotalLen := 0;
InitFontParams(fnt);
while i < iend do begin
ASrc.GetStyleRange(i, ofs, ln);
if ofs + ln > iend then ln := iend - ofs;
l := ln+ofs - i;
txt := ASrc.GetText(i, l);
ASrc.GetTextAttributes(ofs, fnt);
ADst.InDelText(txt, DstStart, DstLen);
ADst.SetTextAttributes(DstStart, l, fnt);
inc(DstStart, l);
inc(i, l);
inc(Result.TotalLen, l);
if isFirst then begin
DstLen := 0;
isFirst := false;
end;
end;
end;
function CopyRichText(const ASrc: TCustomRichMemo;
SrcStart, SrcLen: Integer;
const ADst: TCustomRichMemo;
DstStart, DstLen: integer;
const CopyOpts: TCopyOptions): Integer;
var
isSame : Boolean;
copyBuf : boolean;
res : TCopyResult;
_initDst : integer;
_initLen : integer;
begin
if (ASrc = nil) or (ADst = nil) then begin
Result := 0;
Exit;
end;
isSame := (ASrc = ADst);
if isSame then copyBuf := true
else copyBuf := coCopyBuf in CopyOpts;
ASrc.Lines.BeginUpdate;
if not isSame then ADst.Lines.BeginUpdate;
try
if SrcStart < 0 then SrcStart := 0;
if SrcLen < 0 then SrcLen := ASrc.GetTextLen;
if SrcLen = 0 then begin
Result := 0; // nothing to be copied over
Exit;
end;
if DstStart < 0 then DstStart := 0;
if DstLen < 0 then DstLen := ADst.GetTextLen;
if copyBuf
then _CopyBuf (ASrc, SrcStart, SrcLen, ADst, DstStart, DstLen, CopyOpts, res)
else _CopyDirect(ASrc, SrcStart, SrcLen, ADst, DstStart, DstLen, CopyOpts, res);
Result := res.TotalLen;
finally
if not isSame then ADst.Lines.EndUpdate;
ASrc.Lines.EndUpdate;
end;
end;
initialization
if not Assigned(InsertImageFromFile) then
InsertImageFromFile := @InsertImageFileDummy;
end.