forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCmnFunc.pas
390 lines (342 loc) · 12.1 KB
/
CmnFunc.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
unit CmnFunc;
{
Inno Setup
Copyright (C) 1997-2024 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Common VCL functions
}
{$B-}
interface
uses
Windows, Messages, SysUtils, Forms, Graphics, Controls, StdCtrls, Classes;
type
TWindowDisabler = class
private
FFallbackWnd, FOwnerWnd: HWND;
FPreviousActiveWnd, FPreviousFocusWnd: HWND;
FWindowList: Pointer;
public
constructor Create;
destructor Destroy; override;
end;
{ Note: This type is also present in ScriptFunc_C.pas }
TMsgBoxType = (mbInformation, mbConfirmation, mbError, mbCriticalError);
TMsgBoxCallbackFunc = procedure(const Flags: LongInt; const After: Boolean;
const Param: LongInt);
{ Useful constant }
const
EnableColor: array[Boolean] of TColor = (clBtnFace, clWindow);
procedure UpdateHorizontalExtent(const ListBox: TCustomListBox);
function MinimizePathName(const Filename: String; const Font: TFont;
MaxLen: Integer): String;
function AppMessageBox(const Text, Caption: PChar; Flags: Longint): Integer;
function MsgBoxP(const Text, Caption: PChar; const Typ: TMsgBoxType;
const Buttons: Cardinal): Integer;
function MsgBox(const Text, Caption: String; const Typ: TMsgBoxType;
const Buttons: Cardinal): Integer;
function MsgBoxFmt(const Text: String; const Args: array of const;
const Caption: String; const Typ: TMsgBoxType; const Buttons: Cardinal): Integer;
procedure ReactivateTopWindow;
procedure SetMessageBoxCaption(const Typ: TMsgBoxType; const NewCaption: PChar);
function GetMessageBoxCaption(const Caption: PChar; const Typ: TMsgBoxType): PChar;
procedure SetMessageBoxRightToLeft(const ARightToLeft: Boolean);
function GetMessageBoxRightToLeft: Boolean;
procedure SetMessageBoxCallbackFunc(const AFunc: TMsgBoxCallbackFunc; const AParam: LongInt);
procedure TriggerMessageBoxCallbackFunc(const Flags: LongInt; const After: Boolean);
implementation
uses
Consts, PathFunc, CmnFunc2;
var
MessageBoxCaptions: array[TMsgBoxType] of PChar;
MessageBoxRightToLeft: Boolean;
MessageBoxCallbackFunc: TMsgBoxCallbackFunc;
MessageBoxCallbackParam: LongInt;
MessageBoxCallbackActive: Boolean;
type
TListBoxAccess = class(TCustomListBox);
procedure UpdateHorizontalExtent(const ListBox: TCustomListBox);
var
I: Integer;
Extent, MaxExtent: Longint;
DC: HDC;
Size: TSize;
TextMetrics: TTextMetric;
begin
DC := GetDC(0);
try
SelectObject(DC, TListBoxAccess(ListBox).Font.Handle);
//Q66370 says tmAveCharWidth should be added to extent
GetTextMetrics(DC, TextMetrics);
MaxExtent := 0;
for I := 0 to ListBox.Items.Count-1 do begin
GetTextExtentPoint32(DC, PChar(ListBox.Items[I]), Length(ListBox.Items[I]), Size);
Extent := Size.cx + TextMetrics.tmAveCharWidth;
if Extent > MaxExtent then
MaxExtent := Extent;
end;
finally
ReleaseDC(0, DC);
end;
if MaxExtent > SendMessage(ListBox.Handle, LB_GETHORIZONTALEXTENT, 0, 0) then
SendMessage(ListBox.Handle, LB_SETHORIZONTALEXTENT, MaxExtent, 0);
end;
function MinimizePathName(const Filename: String; const Font: TFont;
MaxLen: Integer): String;
procedure CutFirstDirectory(var S: String);
var
P: Integer;
begin
if Copy(S, 1, 4) = '...\' then
Delete(S, 1, 4);
P := PathPos('\', S);
if P <> 0 then
begin
Delete(S, 1, P);
S := '...\' + S;
end
else
S := '';
end;
var
DC: HDC;
Drive, Dir, Name: String;
DriveLen: Integer;
begin
DC := GetDC(0);
try
SelectObject(DC, Font.Handle);
Result := FileName;
Dir := PathExtractPath(Result);
Name := PathExtractName(Result);
DriveLen := PathDrivePartLength(Dir);
{ Include any slash following drive part, or a leading slash if DriveLen=0 }
if (DriveLen < Length(Dir)) and PathCharIsSlash(Dir[DriveLen+1]) then
Inc(DriveLen);
Drive := Copy(Dir, 1, DriveLen);
Delete(Dir, 1, DriveLen);
while ((Dir <> '') or (Drive <> '')) and (GetTextWidth(DC, Result, False) > MaxLen) do
begin
if Dir <> '' then
CutFirstDirectory(Dir);
{ If there's no directory left, minimize the drive part.
'C:\...\filename' -> '...\filename' }
if (Dir = '') and (Drive <> '') then
begin
Drive := '';
Dir := '...\';
end;
Result := Drive + Dir + Name;
end;
finally
ReleaseDC(0, DC);
end;
end;
procedure SetMessageBoxCaption(const Typ: TMsgBoxType; const NewCaption: PChar);
begin
StrDispose(MessageBoxCaptions[Typ]);
MessageBoxCaptions[Typ] := nil;
if Assigned(NewCaption) then
MessageBoxCaptions[Typ] := StrNew(NewCaption);
end;
function GetMessageBoxCaption(const Caption: PChar; const Typ: TMsgBoxType): PChar;
const
DefaultCaptions: array[TMsgBoxType] of PChar =
('Information', 'Confirm', 'Error', 'Error');
begin
Result := Caption;
if (Result = nil) or (Result[0] = #0) then begin
Result := MessageBoxCaptions[Typ];
if Result = nil then
Result := DefaultCaptions[Typ];
end;
end;
procedure SetMessageBoxRightToLeft(const ARightToLeft: Boolean);
begin
MessageBoxRightToLeft := ARightToLeft;
end;
function GetMessageBoxRightToLeft: Boolean;
begin
Result := MessageBoxRightToLeft;
end;
procedure SetMessageBoxCallbackFunc(const AFunc: TMsgBoxCallbackFunc; const AParam: LongInt);
begin
MessageBoxCallbackFunc := AFunc;
MessageBoxCallbackParam := AParam;
end;
procedure TriggerMessageBoxCallbackFunc(const Flags: LongInt; const After: Boolean);
begin
if Assigned(MessageBoxCallbackFunc) and not MessageBoxCallbackActive then begin
MessageBoxCallbackActive := True;
try
MessageBoxCallbackFunc(Flags, After, MessageBoxCallbackParam);
finally
MessageBoxCallbackActive := False;
end;
end;
end;
function AppMessageBox(const Text, Caption: PChar; Flags: Longint): Integer;
var
ActiveWindow: HWND;
WindowList: Pointer;
begin
if MessageBoxRightToLeft then
Flags := Flags or (MB_RTLREADING or MB_RIGHT);
TriggerMessageBoxCallbackFunc(Flags, False);
try
{ If the application window isn't currently visible, show the message box
with no owner window so it'll get a taskbar button }
if IsIconic(Application.Handle) or
(GetWindowLong(Application.Handle, GWL_STYLE) and WS_VISIBLE = 0) or
(GetWindowLong(Application.Handle, GWL_EXSTYLE) and WS_EX_TOOLWINDOW <> 0) then begin
ActiveWindow := GetActiveWindow;
WindowList := DisableTaskWindows(0);
try
{ Note: DisableTaskWindows doesn't disable invisible windows.
MB_TASKMODAL will ensure that Application.Handle gets disabled too. }
Result := MessageBox(0, Text, Caption, Flags or MB_TASKMODAL);
finally
EnableTaskWindows(WindowList);
SetActiveWindow(ActiveWindow);
end;
Exit;
end;
Result := Application.MessageBox(Text, Caption, Flags);
finally
TriggerMessageBoxCallbackFunc(Flags, True);
end;
end;
function MsgBoxP(const Text, Caption: PChar; const Typ: TMsgBoxType;
const Buttons: Cardinal): Integer;
const
IconFlags: array[TMsgBoxType] of Cardinal =
(MB_ICONINFORMATION, MB_ICONQUESTION, MB_ICONEXCLAMATION, MB_ICONSTOP);
begin
Result := AppMessageBox(Text, GetMessageBoxCaption(Caption, Typ), Buttons or IconFlags[Typ]);
end;
function MsgBox(const Text, Caption: String; const Typ: TMsgBoxType;
const Buttons: Cardinal): Integer;
begin
Result := MsgBoxP(PChar(Text), PChar(Caption), Typ, Buttons);
end;
function MsgBoxFmt(const Text: String; const Args: array of const;
const Caption: String; const Typ: TMsgBoxType; const Buttons: Cardinal): Integer;
begin
Result := MsgBox(Format(Text, Args), Caption, Typ, Buttons);
end;
function ReactivateTopWindowEnumProc(Wnd: HWND; LParam: LPARAM): BOOL; stdcall;
begin
{ Stop if we encounter the application window; don't consider it or any
windows below it }
if Wnd = Application.Handle then
Result := False
else
if IsWindowVisible(Wnd) and IsWindowEnabled(Wnd) and
(GetWindowLong(Wnd, GWL_EXSTYLE) and (WS_EX_TOPMOST or WS_EX_TOOLWINDOW) = 0) then begin
SetActiveWindow(Wnd);
Result := False;
end
else
Result := True;
end;
procedure ReactivateTopWindow;
{ If the application window is active, reactivates the top window owned by the
current thread. Tool windows and windows that are invisible, disabled, or
topmost are not considered. }
begin
if GetActiveWindow = Application.Handle then
EnumThreadWindows(GetCurrentThreadId, @ReactivateTopWindowEnumProc, 0);
end;
procedure FreeCaptions; far;
var
T: TMsgBoxType;
begin
for T := Low(T) to High(T) do begin
StrDispose(MessageBoxCaptions[T]);
MessageBoxCaptions[T] := nil;
end;
end;
{ TWindowDisabler }
const
WindowDisablerWndClassName = 'TWindowDisabler-Window';
var
WindowDisablerWndClassAtom: TAtom;
function WindowDisablerWndProc(Wnd: HWND; Msg: UINT; WParam: WPARAM;
LParam: LPARAM): LRESULT; stdcall;
begin
if Msg = WM_CLOSE then
{ If the fallback window becomes focused (e.g. by Alt+Tabbing onto it) and
Alt+F4 is pressed, we must not pass the message to DefWindowProc because
it would destroy the window }
Result := 0
else
Result := DefWindowProc(Wnd, Msg, WParam, LParam);
end;
constructor TWindowDisabler.Create;
const
WndClass: TWndClass = (
style: 0;
lpfnWndProc: @WindowDisablerWndProc;
cbClsExtra: 0;
cbWndExtra: 0;
hInstance: 0;
hIcon: 0;
hCursor: 0;
hbrBackground: COLOR_WINDOW + 1;
lpszMenuName: nil;
lpszClassName: WindowDisablerWndClassName);
begin
inherited Create;
FPreviousActiveWnd := GetActiveWindow;
FPreviousFocusWnd := GetFocus;
FWindowList := DisableTaskWindows(0);
{ Create the "fallback" window.
When a child process hides its last window, Windows will try to activate
the top-most enabled window on the desktop. If all of our windows were
disabled, it would end up bringing some other application to the
foreground. This gives Windows an enabled window to re-activate, which
is invisible to the user. }
if WindowDisablerWndClassAtom = 0 then
WindowDisablerWndClassAtom := Windows.RegisterClass(WndClass);
if WindowDisablerWndClassAtom <> 0 then begin
{ Create an invisible owner window for the fallback window so that it
doesn't display a taskbar button. (We can't just give it the
WS_EX_TOOLWINDOW style because Windows skips tool windows when searching
for a new window to activate.) }
FOwnerWnd := CreateWindowEx(0, WindowDisablerWndClassName, '',
WS_POPUP or WS_DISABLED, 0, 0, 0, 0, HWND_DESKTOP, 0, HInstance, nil);
if FOwnerWnd <> 0 then begin
FFallbackWnd := CreateWindowEx(0, WindowDisablerWndClassName,
PChar(Application.Title), WS_POPUP, 0, 0, 0, 0, FOwnerWnd, 0,
HInstance, nil);
if FFallbackWnd <> 0 then
ShowWindow(FFallbackWnd, SW_SHOWNA);
end;
end;
{ Take the focus away from whatever has it. While you can't click controls
inside a disabled window, keystrokes will still reach the focused control
(e.g. you can press Space to re-click a focused button). }
SetFocus(0);
end;
destructor TWindowDisabler.Destroy;
begin
EnableTaskWindows(FWindowList);
{ Re-activate the previous window. But don't do this if GetActiveWindow
returns zero, because that means another application is in the foreground
(possibly a child process spawned by us that is still running). }
if GetActiveWindow <> 0 then begin
if FPreviousActiveWnd <> 0 then
SetActiveWindow(FPreviousActiveWnd);
{ If the active window never changed, then the above SetActiveWindow call
won't have an effect. Explicitly restore the focus. }
if FPreviousFocusWnd <> 0 then
SetFocus(FPreviousFocusWnd);
end;
if FOwnerWnd <> 0 then
DestroyWindow(FOwnerWnd); { will destroy FFallbackWnd too }
inherited;
end;
initialization
finalization
FreeCaptions;
end.