-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonguiutils.pas
233 lines (203 loc) · 6.99 KB
/
commonguiutils.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
unit CommonGUIUtils;
{ Common routines to work with GUI.
Copyright (C) 2019 Mohammadreza Bahrami m.audio91@gmail.com
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
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. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, LCLType, Graphics, Controls, ExtCtrls,
IntfGraphics, LazCanvas, extinterpolation, FPImage, Math,
Menus, CommonDrawUtils;
procedure CheckDisplayInScreen(aForm: TForm);
procedure ShowError(const AMsg, ATitle: String);
function ShowWarnYN(const AMsg, ATitle: String): boolean;
procedure CopyImageList(AImgList: TImageList; ASourceImageList: TImageList;
ADPI, ASourceDPI: Word; AMaskColor: TColor);
procedure SetMenuValues(AMenu: TMainMenu; Values: array of String;
AClickEvent: TNotifyEvent); overload;
procedure SetMenuValues(AMenu: TPopupMenu; Values: array of String;
AClickEvent: TNotifyEvent); overload;
function HasDarkBackgroundColor(AControl: TWinControl): Boolean;
function DefaultMacOSMenu(AForm: TForm): TMainMenu;
const
MenuSep='-';
implementation
procedure CheckDisplayInScreen(aForm: TForm);
var
t,l,w,h: Integer;
begin
if not Assigned(aForm) then Exit;
with aForm do
begin
t:= Top;
l:= Left;
w:= Width;
h:= Height;
if t<0 then
t := 0;
if l<0 then
l := 0;
if w>Screen.Width then
begin
Constraints.MinWidth := Min(Constraints.MinWidth,Screen.Width);
w := Screen.Width;
end;
if l+w>=Screen.Width then
l := Screen.Width-w;
if h>Screen.Height then
begin
Constraints.MinHeight := Min(Constraints.MinHeight,Screen.Height);
h := Screen.Height;
end;
if t+h>Screen.Height then
t := Screen.Height-h;
SetBounds(l,t,w,h);
end;
end;
procedure ShowError(const AMsg, ATitle: String);
begin
with Application do
MessageBox(PChar(AMsg), PChar(ATitle), MB_ICONERROR);
end;
function ShowWarnYN(const AMsg, ATitle: String): boolean;
begin
with Application do
Result := MessageBox(PChar(AMsg), PChar(ATitle), MB_YESNO + MB_ICONWARNING) = IDYES;
end;
procedure CopyImageList(AImgList: TImageList; ASourceImageList: TImageList;
ADPI, ASourceDPI: Word; AMaskColor: TColor);
procedure ClearBitmap(ABitmap: TBitmap; AW, AH: Integer);
begin
ABitmap.Width := 0;
ABitmap.Height := 0;
ABitmap.Width := AW;
ABitmap.Height := AH;
end;
var
IntfImg, ResizedIntfImg: TLazIntfImage;
Canv: TLazCanvas;
Bmp, ResizedBmp, TempBmp: TBitmap;
w,h,i: Integer;
begin
if not Assigned(AImgList) then Exit;
if not Assigned(ASourceImageList) then Exit;
if ASourceImageList.Count < 1 then Exit;
if ADPI = ASourceDPI then
begin
AImgList.Clear;
AImgList.Width := ASourceImageList.Width;
AImgList.Height := ASourceImageList.Height;
AImgList.AddImages(ASourceImageList);
Exit;
end;
w := Round((ADPI / ASourceDPI) * ASourceImageList.Width);
h := Round((ADPI / ASourceDPI) * ASourceImageList.Height);
AImgList.Clear;
AImgList.Width := w;
AImgList.Height := h;
Bmp := TBitmap.Create;
TempBmp := TBitmap.Create;
TempBmp.Canvas.Brush.Color := AMaskColor;
ResizedBmp := TBitmap.Create;
IntfImg := TLazIntfImage.Create(0, 0);
ResizedIntfImg := TLazIntfImage.Create(0, 0);
Canv := TLazCanvas.Create(ResizedIntfImg);
Canv.Interpolation := TLanczosInterpolation.Create;
Bmp.PixelFormat := {$IFDEF WINDOWS}pf32Bit{$ELSE}pf24Bit{$ENDIF};
ResizedBmp.PixelFormat := {$IFDEF WINDOWS}pf32Bit{$ELSE}pf24Bit{$ENDIF};
TempBmp.PixelFormat := {$IFDEF WINDOWS}pf32Bit{$ELSE}pf24Bit{$ENDIF};
for i := 0 to ASourceImageList.Count-1 do
begin
ResizedBmp.Transparent := False;
ClearBitmap(ResizedBmp, w, h);
ClearBitmap(Bmp, ASourceImageList.Width, ASourceImageList.Height);
ASourceImageList.GetBitmap(i, Bmp);
ResizedIntfImg.LoadFromBitmap(ResizedBmp.Handle, 0);
{$IFDEF WINDOWS}
IntfImg.LoadFromBitmap(Bmp.Handle, 0);
Canv.StretchDraw(0, 0, w, h, IntfImg);
ResizedBmp.LoadFromIntfImage(ResizedIntfImg);
{$ELSE}
ClearBitmap(TempBmp, ASourceImageList.Width, ASourceImageList.Height);
TempBmp.Canvas.FillRect(Rect(0,0,TempBmp.Width,TempBmp.Height));
TempBmp.Canvas.Draw(0,0,Bmp);
IntfImg.LoadFromBitmap(TempBmp.Handle, 0);
Canv.StretchDraw(0, 0, w, h, IntfImg);
ResizedBmp.LoadFromIntfImage(ResizedIntfImg);
ResizedBmp.TransparentColor := AMaskColor;
ResizedBmp.Transparent := True;
{$ENDIF}
AImgList.Add(ResizedBmp, nil);
end;
Bmp.Free;
TempBmp.Free;
ResizedBmp.Free;
Canv.Interpolation.Free;
Canv.Free;
IntfImg.Free;
ResizedIntfImg.Free;
end;
procedure SetMenuValues(AMenu: TMainMenu; Values: array of String;
AClickEvent: TNotifyEvent);
var
i: Integer;
begin
i := Length(Values);
AMenu.Items.Clear;
for i := Low(Values) to High(Values) do
AMenu.Items.Add(NewItem(Values[i], 0, False, True, AClickEvent, 0,
AMenu.Name+'MI'+i.ToString));
end;
procedure SetMenuValues(AMenu: TPopupMenu; Values: array of String;
AClickEvent: TNotifyEvent);
var
i: Integer;
begin
i := Length(Values);
AMenu.Items.Clear;
for i := Low(Values) to High(Values) do
AMenu.Items.Add(NewItem(Values[i], 0, False, True, AClickEvent, 0,
AMenu.Name+'MI'+i.ToString));
end;
function HasDarkBackgroundColor(AControl: TWinControl): Boolean;
begin
Result := False;
if LuminanceLevel(AControl.GetRGBColorResolvingParent)<50 then
Result := True;
end;
function DefaultMacOSMenu(AForm: TForm): TMainMenu;
{$ifdef darwin}
var
AppMenu: TMenuItem;
{$endif}
begin
{$ifdef darwin}
Result := TMainMenu.Create(AForm);
Result.Parent := AForm;
AppMenu:=TMenuItem.Create(AForm);
AppMenu.Caption:=#$EF#$A3#$BF;
Result.Items.Insert(0, AppMenu);
{$endif}
end;
end.