forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompFunc.pas
550 lines (500 loc) · 16.1 KB
/
CompFunc.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
unit CompFunc;
{
Inno Setup
Copyright (C) 1997-2020 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Additional Compiler IDE functions
}
{$I VERSION.INC}
{$IFDEF IS_D6}
{$WARN SYMBOL_PLATFORM OFF}
{$ENDIF}
interface
uses
Windows,
Classes, Forms, Dialogs, Menus, StdCtrls,
ScintEdit, ModernColors;
const
MRUListMaxCount = 10;
type
TMRUItemCompareProc = function(const S1, S2: String): Integer;
TAddLinesPrefix = (alpNone, alpTimestamp, alpCountdown);
procedure InitFormFont(Form: TForm);
function GetDisplayFilename(const Filename: String): String;
function GetFileTitle(const Filename: String): String;
function GetLastWriteTimeOfFile(const Filename: String;
LastWriteTime: PFileTime): Boolean;
procedure AddFileToRecentDocs(const Filename: String);
function GenerateGuid: String;
function ISPPInstalled: Boolean;
function IsISPPBuiltins(const Filename: String): Boolean;
function ISCryptInstalled: Boolean;
function GetDefaultThemeType: TThemeType;
procedure OpenDonateSite;
procedure OpenMailingListSite;
procedure ReadMRUList(const MRUList: TStringList; const Section, Ident: String);
procedure ModifyMRUList(const MRUList: TStringList; const Section, Ident: String;
const AItem: String; const AddNewItem: Boolean; CompareProc: TMRUItemCompareProc);
procedure LoadKnownIncludedFiles(const AFilename: String; const IncludedFiles: TStringList);
procedure SaveKnownIncludedFiles(const AFilename: String; const IncludedFiles: TStringList);
procedure DeleteKnownIncludedFiles(const AFilename: String);
procedure SetFakeShortCutText(const MenuItem: TMenuItem; const S: String);
procedure SetFakeShortCut(const MenuItem: TMenuItem; const Key: Word;
const Shift: TShiftState);
procedure SaveTextToFile(const Filename: String;
const S: String; const ForceUTF8Encoding: Boolean);
procedure AddLines(const ListBox: TListBox; const S: String; const AObject: TObject; const LineBreaks: Boolean; const Prefix: TAddLinesPrefix; const PrefixParam: Cardinal);
procedure SetLowPriority(ALowPriority: Boolean; var SavePriorityClass: DWORD);
function GetHelpFile: String;
function FindOptionsToSearchOptions(const FindOptions: TFindOptions): TScintFindOptions;
procedure StartAddRemovePrograms;
function GetSourcePath(const AFilename: String): String;
function ReadScriptLines(const ALines: TStringList; const ReadFromFile: Boolean;
const ReadFromFileFilename: String; const NotReadFromFileMemo: TScintEdit): Integer;
implementation
uses
ActiveX, ShlObj, ShellApi, CommDlg, SysUtils,
Messages,
CmnFunc2, PathFunc, FileClass,
CompMsgs, CompTypes;
procedure InitFormFont(Form: TForm);
var
FontName: String;
Metrics: TNonClientMetrics;
begin
begin
Metrics.cbSize := SizeOf(Metrics);
if SystemParametersInfo(SPI_GETNONCLIENTMETRICS, SizeOf(Metrics),
@Metrics, 0) then
FontName := Metrics.lfMessageFont.lfFaceName;
{ Only allow fonts that we know will fit the text correctly }
if not SameText(FontName, 'Microsoft Sans Serif') and
not SameText(FontName, 'Segoe UI') then
FontName := 'Tahoma';
end;
Form.Font.Name := FontName;
Form.Font.Size := 8;
end;
function GetDisplayFilename(const Filename: String): String;
var
Buf: array[0..MAX_PATH-1] of Char;
begin
if CommDlg.GetFileTitle(PChar(Filename), Buf, SizeOf(Buf) div SizeOf(Buf[0])) = 0 then
Result := Buf
else
Result := Filename;
end;
function GetFileTitle(const Filename: String): String;
begin
if Filename = '' then
Result := 'Untitled'
else
Result := Filename;
end;
function GetLastWriteTimeOfFile(const Filename: String;
LastWriteTime: PFileTime): Boolean;
var
H: THandle;
begin
H := CreateFile(PChar(Filename), 0, FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, 0, 0);
if H <> INVALID_HANDLE_VALUE then begin
Result := GetFileTime(H, nil, nil, LastWriteTime);
CloseHandle(H);
end
else
Result := False;
end;
procedure AddFileToRecentDocs(const Filename: String);
{ Notifies the shell that a document has been opened. On Windows 7, this will
add the file to the Recent section of the app's Jump List.
It is only necessary to call this function when the shell is unaware that
a file is being opened. Files opened through Explorer or common dialogs get
added to the Jump List automatically. }
begin
SHAddToRecentDocs(SHARD_PATHW, PChar(Filename));
end;
function GenerateGuid: String;
var
Guid: TGUID;
P: PWideChar;
begin
if CoCreateGuid(Guid) <> S_OK then
raise Exception.Create('CoCreateGuid failed');
if StringFromCLSID(Guid, P) <> S_OK then
raise Exception.Create('StringFromCLSID failed');
try
Result := P;
finally
CoTaskMemFree(P);
end;
end;
function ISPPInstalled: Boolean;
begin
Result := NewFileExists(PathExtractPath(NewParamStr(0)) + 'ISPP.dll');
end;
function IsISPPBuiltins(const Filename: String): Boolean;
begin
Result := PathCompare(PathExtractName(Filename), 'ISPPBuiltins.iss') = 0;
end;
function ISCryptInstalled: Boolean;
begin
Result := NewFileExists(PathExtractPath(NewParamStr(0)) + 'iscrypt.dll');
end;
function GetDefaultThemeType: TThemeType;
var
K: HKEY;
Size, AppsUseLightTheme: DWORD;
begin
Result := ttModernLight;
if (Win32MajorVersion >= 10) and (RegOpenKeyExView(rvDefault, HKEY_CURRENT_USER, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize', 0, KEY_QUERY_VALUE, K) = ERROR_SUCCESS) then begin
Size := SizeOf(AppsUseLightTheme);
if (RegQueryValueEx(K, 'AppsUseLightTheme', nil, nil, @AppsUseLightTheme, @Size) = ERROR_SUCCESS) and (AppsUseLightTheme = 0) then
Result := ttModernDark;
RegCloseKey(K);
end;
end;
procedure OpenDonateSite;
begin
ShellExecute(Application.Handle, 'open', 'https://jrsoftware.org/isdonate.php', nil,
nil, SW_SHOW);
end;
procedure OpenMailingListSite;
begin
ShellExecute(Application.Handle, 'open', 'https://jrsoftware.org/ismail.php', nil,
nil, SW_SHOW);
end;
procedure ReadMRUList(const MRUList: TStringList; const Section, Ident: String);
{ Loads a list of MRU items from the registry }
var
Ini: TConfigIniFile;
I: Integer;
S: String;
begin
Ini := TConfigIniFile.Create;
try
MRUList.Clear;
for I := 0 to MRUListMaxCount-1 do begin
S := Ini.ReadString(Section, Ident + IntToStr(I), '');
if S <> '' then MRUList.Add(S);
end;
finally
Ini.Free;
end;
end;
procedure ModifyMRUList(const MRUList: TStringList; const Section, Ident: String;
const AItem: String; const AddNewItem: Boolean; CompareProc: TMRUItemCompareProc);
var
I: Integer;
Ini: TConfigIniFile;
S: String;
begin
I := 0;
while I < MRUList.Count do begin
if CompareProc(MRUList[I], AItem) = 0 then
MRUList.Delete(I)
else
Inc(I);
end;
if AddNewItem then
MRUList.Insert(0, AItem);
while MRUList.Count > MRUListMaxCount do
MRUList.Delete(MRUList.Count-1);
{ Save new MRU items }
Ini := TConfigIniFile.Create;
try
{ MRU list }
for I := 0 to MRUListMaxCount-1 do begin
if I < MRUList.Count then
S := MRUList[I]
else
S := '';
Ini.WriteString(Section, Ident + IntToStr(I), S);
end;
finally
Ini.Free;
end;
end;
procedure LoadKnownIncludedFiles(const AFilename: String; const IncludedFiles: TStringList);
var
Ini: TConfigIniFile;
OldDelimiter: Char;
begin
OldDelimiter := IncludedFiles.Delimiter;
Ini := TConfigIniFile.Create;
try
IncludedFiles.Delimiter := '*';
IncludedFiles.DelimitedText := Ini.ReadString('IncludedFilesHistory', AFilename, '');
finally
Ini.Free;
IncludedFiles.Delimiter := OldDelimiter;
end;
end;
procedure SaveKnownIncludedFiles(const AFilename: String; const IncludedFiles: TStringList);
var
Ini: TConfigIniFile;
OldDelimiter: Char;
begin
if IncludedFiles.Count = 0 then begin
DeleteKnownIncludedFiles(AFilename);
Exit;
end;
if AFilename = '' then
raise Exception.Create('AFilename must be set');
OldDelimiter := IncludedFiles.Delimiter;
Ini := TConfigIniFile.Create;
try
IncludedFiles.Delimiter := '*';
Ini.WriteString('IncludedFilesHistory', AFilename, IncludedFiles.DelimitedText);
finally
Ini.Free;
IncludedFiles.Delimiter := OldDelimiter;
end;
end;
procedure DeleteKnownIncludedFiles(const AFilename: String);
var
Ini: TConfigIniFile;
begin
if AFilename = '' then
raise Exception.Create('AFilename must be set');
Ini := TConfigIniFile.Create;
try
Ini.DeleteKey('IncludedFilesHistory', AFilename);
finally
Ini.Free;
end;
end;
procedure SetFakeShortCutText(const MenuItem: TMenuItem; const S: String);
begin
MenuItem.Caption := MenuItem.Caption + #9 + S;
end;
procedure SetFakeShortCut(const MenuItem: TMenuItem; const Key: Word;
const Shift: TShiftState);
begin
SetFakeShortCutText(MenuItem, ShortCutToText(ShortCut(Key, Shift)));
end;
procedure SaveTextToFile(const Filename: String;
const S: String; const ForceUTF8Encoding: Boolean);
var
AnsiMode: Boolean;
AnsiStr: AnsiString;
F: TTextFileWriter;
begin
AnsiMode := False;
if not ForceUTF8Encoding then begin
AnsiStr := AnsiString(S);
if S = String(AnsiStr) then
AnsiMode := True;
end;
F := TTextFileWriter.Create(Filename, fdCreateAlways, faWrite, fsNone);
try
if AnsiMode then
F.WriteAnsi(AnsiStr)
else
F.Write(S);
finally
F.Free;
end;
end;
procedure AddLines(const ListBox: TListBox; const S: String; const AObject: TObject; const LineBreaks: Boolean; const Prefix: TAddLinesPrefix; const PrefixParam: Cardinal);
var
ST: TSystemTime;
LineNumber: Cardinal;
procedure AddLine(S: String);
var
TimestampPrefixTab: Boolean;
DC: HDC;
Size: TSize;
begin
TimestampPrefixTab := False;
case Prefix of
alpTimestamp:
begin
if LineNumber = 0 then begin
{ Don't forget about ListBox's DrawItem if you change the format of the following timestamp. }
Insert(Format('[%.2u%s%.2u%s%.2u%s%.3u] ', [ST.wHour, {$IFDEF IS_DXE}FormatSettings.{$ENDIF}TimeSeparator,
ST.wMinute, {$IFDEF IS_DXE}FormatSettings.{$ENDIF}TimeSeparator, ST.wSecond, {$IFDEF IS_DXE}FormatSettings.{$ENDIF}DecimalSeparator,
ST.wMilliseconds]), S, 1);
end else begin
Insert(#9, S, 1); { Not actually painted - just for Ctrl+C }
TimestampPrefixTab := True;
end;
end;
alpCountdown:
begin
Insert(Format('[%.2d] ', [PrefixParam-LineNumber]), S, 1);
end;
end;
try
ListBox.TopIndex := ListBox.Items.AddObject(S, AObject);
except
on EOutOfResources do begin
ListBox.Clear;
SendMessage(ListBox.Handle, LB_SETHORIZONTALEXTENT, 0, 0);
ListBox.Items.Add(SCompilerStatusReset);
ListBox.TopIndex := ListBox.Items.Add(S);
end;
end;
DC := GetDC(0);
try
SelectObject(DC, ListBox.Font.Handle);
if TimestampPrefixTab then
GetTextExtentPoint(DC, PChar(S)+1, Length(S)-1, Size)
else
GetTextExtentPoint(DC, PChar(S), Length(S), Size);
finally
ReleaseDC(0, DC);
end;
Inc(Size.cx, 5);
if TimestampPrefixTab then
Inc(Size.cx, PrefixParam);
if Size.cx > SendMessage(ListBox.Handle, LB_GETHORIZONTALEXTENT, 0, 0) then
SendMessage(ListBox.Handle, LB_SETHORIZONTALEXTENT, Size.cx, 0);
Inc(LineNumber);
end;
var
LineStart, I: Integer;
LastWasCR: Boolean;
begin
GetLocalTime(ST);
if LineBreaks then begin
LineNumber := 0;
LineStart := 1;
LastWasCR := False;
{ Call AddLine for each line. CR, LF, and CRLF line breaks are supported. }
for I := 1 to Length(S) do begin
if S[I] = #13 then begin
AddLine(Copy(S, LineStart, I - LineStart));
LineStart := I + 1;
LastWasCR := True;
end
else begin
if S[I] = #10 then begin
if not LastWasCR then
AddLine(Copy(S, LineStart, I - LineStart));
LineStart := I + 1;
end;
LastWasCR := False;
end;
end;
AddLine(Copy(S, LineStart, Maxint));
end else
AddLine(S);
end;
procedure SetLowPriority(ALowPriority: Boolean; var SavePriorityClass: DWORD);
begin
if ALowPriority then begin
{ Save current priority and change to 'low' }
if SavePriorityClass = 0 then
SavePriorityClass := GetPriorityClass(GetCurrentProcess);
SetPriorityClass(GetCurrentProcess, IDLE_PRIORITY_CLASS);
end
else begin
{ Restore original priority }
if SavePriorityClass <> 0 then begin
SetPriorityClass(GetCurrentProcess, SavePriorityClass);
SavePriorityClass := 0;
end;
end;
end;
function GetHelpFile: String;
begin
Result := PathExtractPath(NewParamStr(0)) + 'isetup.chm';
end;
function FindOptionsToSearchOptions(const FindOptions: TFindOptions): TScintFindOptions;
begin
Result := [];
if frMatchCase in FindOptions then
Include(Result, sfoMatchCase);
if frWholeWord in FindOptions then
Include(Result, sfoWholeWord);
end;
procedure StartAddRemovePrograms;
var
Dir: String;
Wow64DisableWow64FsRedirectionFunc: function(var OldValue: Pointer): BOOL; stdcall;
Wow64RevertWow64FsRedirectionFunc: function(OldValue: Pointer): BOOL; stdcall;
RedirDisabled: Boolean;
RedirOldValue: Pointer;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
Dir := GetSystemDir
else
Dir := GetWinDir;
FillChar(StartupInfo, SizeOf(StartupInfo), 0);
StartupInfo.cb := SizeOf(StartupInfo);
{ Have to disable file system redirection because the 32-bit version of
appwiz.cpl is buggy on XP x64 RC2 -- it doesn't show any Change/Remove
buttons on 64-bit MSI entries, and it doesn't list non-MSI 64-bit apps
at all. }
Wow64DisableWow64FsRedirectionFunc := GetProcAddress(GetModuleHandle(kernel32),
'Wow64DisableWow64FsRedirection');
Wow64RevertWow64FsRedirectionFunc := GetProcAddress(GetModuleHandle(kernel32),
'Wow64RevertWow64FsRedirection');
RedirDisabled := Assigned(Wow64DisableWow64FsRedirectionFunc) and
Assigned(Wow64RevertWow64FsRedirectionFunc) and
Wow64DisableWow64FsRedirectionFunc(RedirOldValue);
try
Win32Check(CreateProcess(nil, PChar('"' + AddBackslash(Dir) + 'control.exe" appwiz.cpl'),
nil, nil, False, 0, nil, PChar(Dir), StartupInfo, ProcessInfo));
finally
if RedirDisabled then
Wow64RevertWow64FsRedirectionFunc(RedirOldValue);
end;
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
function GetSourcePath(const AFilename: String): String;
begin
if AFilename <> '' then
Result := PathExtractPath(AFilename)
else begin
{ If the script was not saved, default to My Documents }
Result := GetShellFolderPath(CSIDL_PERSONAL);
if Result = '' then
raise Exception.Create('GetShellFolderPath failed');
end;
end;
function ReadScriptLines(const ALines: TStringList; const ReadFromFile: Boolean;
const ReadFromFileFilename: String; const NotReadFromFileMemo: TScintEdit): Integer;
function ContainsNullChar(const S: String): Boolean;
var
I: Integer;
begin
Result := False;
for I := 1 to Length(S) do
if S[I] = #0 then begin
Result := True;
Break;
end;
end;
var
F: TTextFileReader;
I: Integer;
begin
if ReadFromFile then begin
F := TTextFileReader.Create(ReadFromFileFilename, fdOpenExisting, faRead, fsRead);
try
while not F.Eof do
ALines.Add(F.ReadLine);
finally
F.Free;
end;
end
else begin
ALines.Capacity := NotReadFromFileMemo.Lines.Count;
ALines.Assign(NotReadFromFileMemo.Lines);
end;
{ Check for null characters }
for I := 0 to ALines.Count-1 do begin
if ContainsNullChar(ALines[I]) then begin
Result := I;
Exit;
end;
end;
Result := -1;
end;
end.