/
commonfunctionslcl.pas
443 lines (372 loc) · 10.3 KB
/
commonfunctionslcl.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
unit CommonFunctionsLCL;
// Copyright 2022-2024 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{$mode ObjFPC}{$H+}
{$i zxinc.inc}
interface
uses
Classes, SysUtils, Types, UnitCommon, Forms, LCLIntf, Graphics, Controls,
StdCtrls, Grids;
type
{ TCommonFunctionsLCL }
TCommonFunctionsLCL = class sealed (TObject)
strict private
class var
Canv: TCanvas;
MonoFont: TFont;
private
class procedure Init; static;
class procedure Final; static;
strict private
type
TLinkLabel = class(TCustomLabel)
protected
procedure MouseEnter; override;
procedure MouseLeave; override;
public
constructor Create(AOwner: TComponent; const ACaption: String); virtual;
constructor Create(AOwner: TComponent); override;
property ShowAccelChar default False;
property Cursor default crHandPoint;
end;
public
class function CreateLinkLabel(AOwner: TComponent; const ACaption: String = ''): TCustomLabel;
class procedure FormToScreenCentre(Form: TCustomForm); static;
class procedure GrowFormHeight(F: TCustomForm); static;
class procedure AdjustFormPos(Form: TCustomForm; Horizontal: Boolean = False); static;
class procedure CalculateTextSize(const F: TFont;
const S: String; out ATextSize: TSize); static;
class function GridMouseToCellRegular(AGrid: TCustomGrid; const MousePos: TPoint;
out GridCoordinates: TPoint): Boolean; static;
class procedure RowInView(AGrid: TCustomGrid; ARow: Integer); static;
class function GridMouseButtonAllowed(AGrid: TCustomGrid; Button: TMouseButton): Boolean;
class function GetBestContrastColorForFont(R, G, B: Integer): TColor;
class function MakeExtensionsFilter(const ArSt: Array of String): String;
class function GetMonoFont(): TFont;
end;
IFormAddCloseQuery = interface
['{5D69DCCC-F77B-44B0-B25E-EC4A6D350982}']
procedure AddCloseQuery(C: TCloseQueryEvent);
end;
implementation
{ TCommonFunctionsLCL.TLinkLabel }
procedure TCommonFunctionsLCL.TLinkLabel.MouseEnter;
begin
inherited MouseEnter;
Font.Style := Font.Style + [fsBold];
end;
procedure TCommonFunctionsLCL.TLinkLabel.MouseLeave;
begin
inherited MouseLeave;
Font.Style := Font.Style - [fsBold];
end;
constructor TCommonFunctionsLCL.TLinkLabel.Create(AOwner: TComponent;
const ACaption: String);
begin
inherited Create(AOwner);
Name := TCommonFunctions.GlobalObjectNameGenerator(Self);
ShowAccelChar := False;
if ACaption = '' then
Caption := ' '
else
Caption := ACaption;
AutoSize := True;
ShowHint := True;
Font.Color := clBlue;
Font.Style := Font.Style + [fsUnderline];
Cursor := crHandPoint;
Transparent := True;
end;
constructor TCommonFunctionsLCL.TLinkLabel.Create(AOwner: TComponent);
begin
Create(AOwner, ' ');
end;
class procedure TCommonFunctionsLCL.CalculateTextSize(const F: TFont;
const S: String; out ATextSize: TSize);
begin
if Canv = nil then begin
Canv := TCanvas.Create;
Canv.Handle := GetDC(0);
end;
Canv.Font := F;
ATextSize := Canv.TextExtent(S);
end;
type
TGridAccessProtected = class(TCustomGrid);
class function TCommonFunctionsLCL.GridMouseToCellRegular(AGrid: TCustomGrid;
const MousePos: TPoint; out GridCoordinates: TPoint): Boolean;
var
G: TGridAccessProtected;
Rest: Integer;
begin
G := TGridAccessProtected(AGrid);
Result :=
G.OffsetToColRow(
False, // IsCol
True, // Physical, means take scroll offset into consideration.
MousePos.Y,
GridCoordinates.Y,
Rest
)
and G.OffsetToColRow(
True, // IsCol
True, // Physical
MousePos.X,
GridCoordinates.X,
Rest
);
if not Result then
GridCoordinates := Point(-1, -1);
end;
class procedure TCommonFunctionsLCL.RowInView(AGrid: TCustomGrid; ARow: Integer
);
var
N: Integer;
G: TGridAccessProtected;
begin
G := TGridAccessProtected(AGrid);
G.BeginUpdate;
try
N := ARow - 2;
while ((N < ARow) and (not G.MoveExtend(False, G.Col, N, False))) do begin
Inc(N);
end;
N := ARow + 2;
while ((N > ARow) and (not G.MoveExtend(False, G.Col, N, False))) do begin
Dec(N);
end;
G.MoveExtend(False, G.Col, ARow, True);
finally
G.EndUpdate;
end;
end;
class function TCommonFunctionsLCL.GridMouseButtonAllowed(AGrid: TCustomGrid;
Button: TMouseButton): Boolean;
var
GC: TPoint;
begin
Result := (Button in [TMouseButton.mbLeft, TMouseButton.mbRight, TMouseButton.mbMiddle])
and GridMouseToCellRegular(AGrid, AGrid.ScreenToClient(Mouse.CursorPos), GC);
end;
class function TCommonFunctionsLCL.GetBestContrastColorForFont(R, G, B: Integer
): TColor;
begin
// double luma = ((0.299 * iColor.R) + (0.587 * iColor.G) + (0.114 * iColor.B)) / 255;
// from here: https://stackoverflow.com/questions/1855884/determine-font-color-based-on-background-color
// black or white:
if R * 299 + G * 587 + B * 114 <= 127500 then
Result := clWhite // $FFFFFF
else
Result := clBlack; // 0
end;
class function TCommonFunctionsLCL.MakeExtensionsFilter(
const ArSt: array of String): String;
var
I, L: Integer;
S1, Sa, Sb: String;
AI: String;
{$ifdef LCLGtk2}
J: Integer;
AIU: String;
{$endif}
begin
Result := '';
S1 := '';
Sa := '';
Sb := '';
L := 0;
for I := Low(ArSt) to High(ArSt) do begin
AI := LowerCase(Trim(ArSt[I]));
if AI <> '' then begin
{$ifdef LCLGtk2}
AIU := UpperCase(AI);
S1 := '';
for J := 1 to Length(AI) do
if AIU[J] <> AI[J] then
S1 := S1 + '[' + AIU[J] + AI[J] + ']'
else
S1 := S1 + AI[J];
{$else}
S1 := AI;
{$endif}
S1 := '*' + ExtensionSeparator + S1;
Result := Result + AI + '|' + S1 + '|';
Inc(L);
if L > 1 then begin
Sa := Sa + ', ';
Sb := Sb + ';';
end;
Sa := Sa + AI;
Sb := Sb + S1;
end;
end;
Result := Result + 'all files|*';
if L > 1 then
Result := Sa + '|' + Sb + '|' + Result;
end;
class function TCommonFunctionsLCL.GetMonoFont(): TFont;
const
FontsToTry: array of String = (
'Monospace',
'Consolas', 'Liberation Mono', 'Lucida Console', 'Courier New',
'DejaVu Sans Mono', 'FreeMono', 'Courier',
'Mono'
);
var
I: Integer;
begin
if MonoFont = nil then begin
MonoFont := TFont.Create;
for I := Low(FontsToTry) to High(FontsToTry) do begin
if Screen.Fonts.IndexOf(FontsToTry[I]) >= 0 then begin
MonoFont.Name := FontsToTry[I];
Break;
end;
end;
end;
Result := MonoFont;
end;
class procedure TCommonFunctionsLCL.Init;
begin
Canv := nil;
MonoFont := nil;
end;
class procedure TCommonFunctionsLCL.Final;
begin
if Assigned(Canv) then begin
if Canv.HandleAllocated then
ReleaseDC(0, Canv.Handle);
Canv.Free;
end;
MonoFont.Free;
end;
class function TCommonFunctionsLCL.CreateLinkLabel(AOwner: TComponent;
const ACaption: String): TCustomLabel;
begin
Result := TLinkLabel.Create(AOwner, ACaption);
end;
class procedure TCommonFunctionsLCL.FormToScreenCentre(Form: TCustomForm);
var
M: TMonitor;
R, FormRect: TRect;
L, T: Integer;
begin
Form.HandleNeeded;
if Form.HandleAllocated then begin
M := Screen.MonitorFromWindow(Form.Handle);
R := M.WorkareaRect;
if (R.Right <= R.Left) or (R.Bottom <= R.Top) then
R := M.BoundsRect;
T := R.Bottom - R.Top;
L := R.Right - R.Left;
if GetWindowRect(Form.Handle, FormRect{%H-}) <> 0 then begin
L := L - FormRect.Width;
T := T - FormRect.Height;
end else begin
T := T - Form.Height;
L := L - Form.Width;
end;
T := T div 2;
if T < R.Top then
T := R.Top;
L := L div 2;
if L < R.Left then
L := R.Left;
Form.SetBounds(L, T, Form.Width, Form.Height);
end;
end;
class procedure TCommonFunctionsLCL.GrowFormHeight(F: TCustomForm);
var
M: TMonitor;
H: Integer;
R: TRect;
begin
F.HandleNeeded;
M := Screen.MonitorFromWindow(F.Handle);
R := M.WorkareaRect;
if R.Height <= 0 then
R := M.BoundsRect;
H := (R.Height * 21) div 23;
if F.BorderStyle <> TFormBorderStyle.bsNone then
H := H - 26;
if F.Height < H then
F.Height := H;
end;
class procedure TCommonFunctionsLCL.AdjustFormPos(Form: TCustomForm;
Horizontal: Boolean);
var
M: TMonitor;
R: TRect;
P: TPoint;
N, K, Nn: Integer;
W, H: Integer;
SpectrumForm: TCustomForm;
begin
SpectrumForm := Application.MainForm;
if Assigned(SpectrumForm) and SpectrumForm.HandleAllocated and SpectrumForm.IsVisible then begin
M := Screen.MonitorFromWindow(SpectrumForm.Handle);
if Assigned(M) then begin
R := M.WorkareaRect;
if (R.Left >= R.Right) or (R.Top >= R.Bottom) then
R := M.BoundsRect;
W := Form.Width;
H := Form.Height;
if W > R.Width then
W := R.Width;
if H > R.Height then
H := R.Height;
N := 6;
Nn := N;
if not Horizontal then begin
P.X := (R.Width - SpectrumForm.Width) div 2;
if SpectrumForm.Left < P.X then begin
P.X := SpectrumForm.BoundsRect.Right + N;
end else begin
P.X := SpectrumForm.Left - W - N;
if Form.BorderStyle <> bsNone then
P.X := P.X - N;
end;
K := R.Width - W;
if Form.BorderStyle <> bsNone then begin
R.Bottom := R.Bottom - 27 - N;
K := K - N - 3;
end;
if P.X > K then
P.X := K;
P.Y := SpectrumForm.BoundsRect.Top;
if P.Y + H > R.Bottom then begin
P.Y := R.Bottom - H; // - N;
end;
end else begin
K := R.Height - H;
if Form.BorderStyle <> bsNone then begin
Nn := N + 27;
R.Right := R.Right - N;
K := K - 27;
end;
P.Y := (R.Height - SpectrumForm.Height) div 2;
if SpectrumForm.Top < P.Y then begin
P.Y := SpectrumForm.BoundsRect.Bottom + Nn + N;
end else begin
P.Y := SpectrumForm.Top - H - Nn;
end;
if P.Y > K then
P.Y := K;
P.X := SpectrumForm.BoundsRect.Left;
if P.X + W > R.Right then begin
P.X := R.Right - W; // - N;
end;
end;
if P.X < 0 then
P.X := 0;
if P.Y < 0 then
P.Y := 0;
Form.SetBounds(P.X, P.Y, W, H);
end;
end;
end;
initialization
TCommonFunctionsLCL.Init;
finalization
TCommonFunctionsLCL.Final;
end.