-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.pas
2992 lines (2650 loc) · 85.5 KB
/
main.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SynEdit, DosCommand, rkShellPath, rkEdit,
Vcl.StdCtrls, Vcl.ExtCtrls, System.ImageList, Vcl.ImgList,
Vcl.ComCtrls, Vcl.WinXCtrls, TlHelp32, ShellApi, ShDocVw, ActiveX, ShlObj, IniFiles, ComObj,
Vcl.Menus, DzDirSeek, rkSmartPath, rkVistaProBar, Vcl.VirtualImage,
uHostPreview, Winapi.Wincodec, StrUtils, ES.BaseControls, ES.Images, rkView,
JPEG, Math, CommCtrl {HIMAGELIST}, rkIntegerList, SynEditHighlighter,
SynHighlighterUNIXShellScript, UWP.Form, madExceptVcl, scStyledForm, libgit2,
rkPathViewer, IconFontsImageListBase, IconFontsImageList, Clipbrd,
SynHighlighterMulti, SynEditCodeFolding, SynHighlighterPas, Vcl.Buttons,
System.Actions, Vcl.ActnList, Vcl.ToolWin, MPCommonObjects,
EasyListview, VirtualExplorerEasyListview, kcontrols, khexeditor, keditcommon,
Process, UWP.Autorun;
const
KeyEvent = WM_USER + 1;
KeyEventAll = WM_USER + 2;
CM_UpdateView = WM_USER + 2;
CM_Progress = WM_USER + 3;
IID_IImageList: TGUID = '{46EB5926-582E-4017-9FDF-E8998DAA0950}';
type
EInvalidImageFormat = class(Exception);
type
PItemData = ^TItemData;
TItemData = record
Name: string;
ThumbWidth: Word;
ThumbHeight: Word;
Size: Integer;
Modified: TDateTime;
Dir: Boolean;
GotThumb: Boolean;
IWidth, IHeight: Word;
ImgIdx: Integer;
IsIcon: Boolean;
ImgState: Byte;
Image: TObject;
end;
ThumbThread = class(TThread)
private
{ Private Declarations }
ViewLink: TrkView;
ItemsLink: TList;
protected
procedure Execute; override;
public
constructor Create(View: TrkView; Items: TList);
end;
TFuzzyStringMatcher = class
private
FThreshold: Integer;
function DamerauLevenshteinDistance(const S1, S2: string): Integer;
public
constructor Create(Threshold: Integer);
function IsMatch(const Str, SubStr: string): Boolean;
end;
// Autocomplete https://stackoverflow.com/a/5465826
TEnumString = class(TInterfacedObject, IEnumString)
private
type
TPointerList = array[0..0] of Pointer;
var
FStrings: TStringList;
FCurrIndex: Integer;
public
// IEnumString
function Next(celt: Longint; out elt;
pceltFetched: PLongint): HResult; stdcall;
function Skip(celt: Longint): HResult; stdcall;
function Reset: HResult; stdcall;
function Clone(out enm: IEnumString): HResult; stdcall;
// VCL
constructor Create;
destructor Destroy; override;
end;
{ ACO_NONE = 0;
ACO_AUTOSUGGEST = $1;
ACO_AUTOAPPEND = $2;
ACO_SEARCH = $4;
ACO_FILTERPREFIXES = $8;
ACO_USETAB = $10;
ACO_UPDOWNKEYDROPSLIST = $20;
ACO_RTLREADING = $40;
ACO_WORD_FILTER = $80;
ACO_NOPREFIXFILTERING = $100;
}
TACOption = (acNone, acAutoSuggest, acAutoAppend, acSearch, acFilterPrefixes,
acUseTab, acUpDownKeyDropsList, acRTLReading, acWordFilter, acNoPrefixFiltering);
TACOptions = set of TACOption;
TACSource = (acsList, acsHistory, acsMRU, acsShell);
TButtonedEdit = class(Vcl.ExtCtrls.TButtonedEdit)
private
FACList: TEnumString;
FEnumString: IEnumString;
FAutoComplete: IAutoComplete;
FACEnabled: Boolean;
FACOptions: TACOptions;
FACSource: TACSource;
function GetACStrings : TStringList;
procedure SetACEnabled(const Value: Boolean);
procedure SetACOptions(const Value: TACOptions);
procedure SetACSource(const Value: TACSource);
procedure SetACStrings(const Value: TStringList);
protected
procedure CreateWnd; override;
procedure DestroyWnd; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property ACEnabled: Boolean read FACEnabled write SetACEnabled;
property ACOptions: TACOptions read FACOptions write SetACOptions;
property ACSource: TACSource read FACSource write SetACSource;
property ACStrings: TStringList read GetACStrings write SetACStrings;
end;
TCommandType = (ctNormal, ctEnvironment, ctOther);
TForm1 = class(TForm)
DosCommand1: TDosCommand;
ButtonedEdit1: TButtonedEdit;
ImageList1: TImageList;
BCEditor1: TSynEdit;
StatusBar1: TStatusBar;
SearchBox1: TSearchBox;
TrayIcon1: TTrayIcon;
PopupMenu1: TPopupMenu;
Exit1: TMenuItem;
Show1: TMenuItem;
N1: TMenuItem;
DzDirSeek1: TDzDirSeek;
pnlPreview: TPanel;
Splitter1: TSplitter;
EsImage1: TEsImage;
rkView1: TrkView;
Image1: TImage;
SynUNIXShellScriptSyn1: TSynUNIXShellScriptSyn;
ListBox1: TListBox;
ComboBox1: TComboBox;
scStyledForm1: TscStyledForm;
pnlTop: TPanel;
IconFontsImageList1: TIconFontsImageList;
rkSmartPath1: TrkSmartPath;
PopupMenu2: TPopupMenu;
OpenURL1: TMenuItem;
CopyPathtoClipboard1: TMenuItem;
SynPasSyn1: TSynPasSyn;
SynMultiSyn1: TSynMultiSyn;
SpeedButton1: TSpeedButton;
IconFontsImageList2: TIconFontsImageList;
ActionList1: TActionList;
actPreview: TAction;
actHide: TAction;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
Panel1: TPanel;
actUnPin: TAction;
actSigInt: TAction;
VirtualMultiPathExplorerEasyListview1: TVirtualMultiPathExplorerEasyListview;
KHexEditor1: TKHexEditor;
actPath2Clip: TAction;
tmrToast: TTimer;
AppAutoStart1: TAppAutoStart;
mnuAutoStart: TMenuItem;
pnlTitle: TPanel;
LinkLabel1: TLinkLabel;
procedure ButtonedEdit1Enter(Sender: TObject);
procedure ButtonedEdit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure DosCommand1ExecuteError(ASender: TObject; AE: Exception;
var AHandled: Boolean);
procedure DosCommand1NewLine(ASender: TObject; const ANewLine: string;
AOutputType: TOutputType);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure Show1Click(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure TrayIcon1DblClick(Sender: TObject);
procedure DosCommand1Terminated(Sender: TObject);
procedure DosCommand1TerminateProcess(ASender: TObject;
var ACanTerminate: Boolean);
procedure ListBox1DblClick(Sender: TObject);
procedure ListBox1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure OpenURL1Click(Sender: TObject);
procedure CopyPathtoClipboard1Click(Sender: TObject);
procedure ButtonedEdit1KeyPress(Sender: TObject; var Key: Char);
procedure BCEditor1DblClick(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure actPreviewExecute(Sender: TObject);
procedure actUnPinExecute(Sender: TObject);
procedure actSigIntExecute(Sender: TObject);
procedure actPath2ClipExecute(Sender: TObject);
procedure tmrToastTimer(Sender: TObject);
procedure mnuAutoStartClick(Sender: TObject);
procedure LinkLabel1LinkClick(Sender: TObject; const Link: string;
LinkType: TSysLinkType);
private
{ Private declarations }
FPinned: Boolean;
Items: TList;
ThumbSizeW, ThumbSizeH: Integer;
FhImageList48: Cardinal;
FIconSize: Integer;
FCommandOutput: TStringList;
lastExplorerHandle: HWND;
lastExplorerPath: String;
lstExplorerPath: TStringList;
lstExplorerWnd: TStringList;
lstExplorerItem: TStringList;
fPreview: THostPreviewHandler;
function ListExplorerInstances:Integer;
procedure KeyEventHandler(var Msg: TMessage); message KeyEvent;
procedure KeyEventHandlerAll(var Msg: TMessage); message KeyEventAll;
procedure OnFocusLost(Sender: TObject);
function GetExplorerAddressBarRect(AHandle: HWND): TRect;
function ShowPreview(const FileName: string): Boolean;
procedure SwitchToWindow(AWnd: HWND);
procedure ProcessDosCommand(Sender: TObject; ACommand: string; terminateCurrent: Boolean = False);
procedure CMFocusChanged(var Msg: TCMFocusChanged); message CM_FOCUSCHANGED;
procedure UpdateMainMenu(const ForeGroundWindow: HWND);
procedure FlushIcons;
procedure NoBorder(var Msg: TWMNCActivate); message WM_NCACTIVATE;
protected
procedure CreateParams(var Params: TCreateParams); override;
private
FCommandType: TCommandType;
FEnvExecutables: TStringList;
FEnvStrings: TStringList;
procedure UpdateStyle;
procedure RefreshEnvironmentVariables;
procedure WMSettingChange(var Msg: TMessage); message WM_SETTINGCHANGE;
function ConvertImageToJpeg(const InputFileName, OutputFileName: string): Boolean;
public
{ Public declarations }
Directory: string;
CurrentDir: string;
CurrentFile: string;
GitUrl: string;
procedure Toast(aText, aTitle: string; sType: string = 'S,I,E'; ParentBase: TWinControl = nil);
procedure populateCommands;
procedure populateEnvironmentStrings;
procedure populateMyFolders;
procedure populateEnvExecutables;
end;
var
Form1: TForm1;
args: TStringList;
function StartHook:BOOL; stdcall; external 'HotkeyHook.dll' name 'STARTHOOK';
procedure StopHook; stdcall; external 'HotkeyHook.dll' name 'STOPHOOK';
procedure SwitchToThisWindow(h1: hWnd; x: bool); stdcall;
external user32 Name 'SwitchToThisWindow';
implementation
{$R *.dfm}
uses
frmHover, UIAutomationClient, DarkModeApi.Vcl, Vcl.Themes,
DarkModeApi, Winapi.UxTheme, UWP.DarkMode, Ntapi.UserEnv, Ntapi.WinNt, Ntapi.ntrtl,
pngimage, GIFImg, Cod.Imaging.Heif, Cod.Imaging.WebP;
type
THostPreviewHandlerClass = class(THostPreviewHandler);
{ Global Functions}
function RtlGetVersion(var RTL_OSVERSIONINFOEXW): LONGINT; stdcall;
external 'ntdll.dll' Name 'RtlGetVersion';
function isWindows11:Boolean;
var
winver: RTL_OSVERSIONINFOEXW;
begin
Result := False;
if ((RtlGetVersion(winver) = 0) and (winver.dwMajorVersion>=10) and (winver.dwBuildNumber > 22000)) then
Result := True;
end;
procedure EnableNCShadow(Wnd: HWND);
const
DWMWCP_DEFAULT = 0; // Let the system decide whether or not to round window corners
DWMWCP_DONOTROUND = 1; // Never round window corners
DWMWCP_ROUND = 2; // Round the corners if appropriate
DWMWCP_ROUNDSMALL = 3; // Round the corners if appropriate, with a small radius
DWMWA_WINDOW_CORNER_PREFERENCE = 33; // [set] WINDOW_CORNER_PREFERENCE, Controls the policy that rounds top-level window corners
var
DWM_WINDOW_CORNER_PREFERENCE: Cardinal;
begin
if isWindows11 then
begin
DWM_WINDOW_CORNER_PREFERENCE := DWMWCP_ROUNDSMALL;
DwmSetWindowAttribute(Wnd, DWMWA_WINDOW_CORNER_PREFERENCE, @DWM_WINDOW_CORNER_PREFERENCE, sizeof(DWM_WINDOW_CORNER_PREFERENCE));
end;
end;
procedure UseImmersiveDarkMode(Handle: HWND; Enable: Boolean);
const
DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
var
DarkMode: DWORD;
Attribute: DWORD;
begin
//https://stackoverflow.com/a/62811758
DarkMode := DWORD(Enable);
if Win32MajorVersion = 10 then
begin
if Win32BuildNumber >= 17763 then
begin
Attribute := DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1;
if Win32BuildNumber >= 18985 then
Attribute := DWMWA_USE_IMMERSIVE_DARK_MODE;
DwmSetWindowAttribute(Handle, Attribute, @DarkMode, SizeOf(DWord));
SetWindowPos(Handle, 0, 0, 0, 0, 0, SWP_DRAWFRAME or SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER);
end;
end;
end;
function RunProcess(const Binary: string; const DirPath: string; args: TStrings): Boolean;
const
BufSize = 4096; //1024
var
Process: TProcess;
Buf: AnsiString;
Count: Integer;
i: Integer;
LineStart: Integer;
OutputLine: AnsiString;
begin
Process := TProcess.Create(nil);
try
Process.Executable := Binary;
Process.Options := [poUsePipes, poStderrToOutPut];
Process.ShowWindow := swoHIDE;
Process.Parameters.Assign(args);
Process.CurrentDirectory := DirPath;
Process.Execute;
OutputLine := '';
SetLength(Buf, BufSize);
repeat
if (Process.Output <> nil) then
begin
Count := Process.Output.Read(PChar(Buf)^, BufSize);
end
else
Count := 0;
LineStart := 1;
i := 1;
while i <= Count do
begin
if CharInSet(Buf[i], [#10, #13]) then
begin
OutputLine := OutputLine + Copy(Buf, LineStart, i - LineStart);
Form1.BCEditor1.Lines.Add(OutputLine);
OutputLine := '';
if (i < Count) and (CharInSet(Buf[i], [#10, #13])) and (Buf[i] <> Buf[i + 1]) then
Inc(i);
LineStart := i + 1;
end;
Inc(i);
end;
OutputLine := Copy(Buf, LineStart, Count - LineStart + 1);
until Count = 0;
if OutputLine <> '' then
Form1.BCEditor1.Lines.Add(OutputLine);
Process.WaitOnExit;
Result := Process.ExitStatus = 0;
if not Result then
Form1.BCEditor1.Lines.Add('Command ' + Process.Executable + ' failed with exit code: ' + IntToStr(Process.ExitStatus));
finally
FreeAndNil(Process);
end;
end;
function IsGitRepository(const Dir: string): Boolean;
var
repo: Pgit_repository;
dirPath: PAnsiChar;
error: Integer;
begin
dirPath := PAnsiChar(AnsiString(Dir));
error := git_repository_open(@repo, dirPath);
if error = 0 then
begin
git_repository_free(repo);
Result := True;
end
else
Result := False;
end;
function IsGit(const RepoDir): boolean;
var
repo: Pgit_repository;
remote: Pgit_remote;
dirPath, remoteNamePAnsi: PAnsiChar;
remoteURL: PAnsiChar;
error: Integer;
begin
Result := False;
dirPath := PAnsiChar(AnsiString(RepoDir));
// Open the repository
error := git_repository_open(@repo, dirPath);
if error <> 0 then
Exit;
Result := True;
// Free the repository resource
git_repository_free(repo);
end;
function GetRemoteURL(const RepoDir, RemoteName: string): string;
var
repo: Pgit_repository;
remote: Pgit_remote;
dirPath, remoteNamePAnsi: PAnsiChar;
remoteURL: PAnsiChar;
error: Integer;
begin
Result := '';
dirPath := PAnsiChar(AnsiString(RepoDir));
remoteNamePAnsi := PAnsiChar(AnsiString(RemoteName));
// Open the repository
error := git_repository_open(@repo, dirPath);
if error <> 0 then
Exit;
// Look up the remote by its name
error := git_remote_lookup(@remote, repo, remoteNamePAnsi);
if error = 0 then
begin
// Get the remote URL
remoteURL := git_remote_url(remote);
Result := string(remoteURL);
// Free the remote resource
git_remote_free(remote);
end;
// Free the repository resource
git_repository_free(repo);
end;
function ExtractThumbnail(Path: string; SizeX, SizeY: Integer; InitOle: Boolean = False): HBitmap;
var
ShellFolder, DesktopShellFolder: IShellFolder;
XtractImage: IExtractImage;
Eaten: DWord;
PIDL: PItemIDList;
RunnableTask: IRunnableTask;
Flags: DWord;
Buf: array [0 .. MAX_PATH] of Char;
BmpHandle: HBITMAP;
Atribute, Priority: DWord;
GetLocationRes: HResult;
ASize: TSize;
begin
Result := 0;
try
if InitOle then
CoInitializeEx(nil, COINIT_APARTMENTTHREADED or COINIT_DISABLE_OLE1DDE);
try
OleCheck(SHGetDesktopFolder(DesktopShellFolder));
OleCheck(DesktopShellFolder.ParseDisplayName(0, nil, StringToOleStr(ExtractFilePath(Path)),
Eaten, PIDL, Atribute));
OleCheck(DesktopShellFolder.BindToObject(PIDL, nil, IID_IShellFolder, Pointer(ShellFolder)));
CoTaskMemFree(PIDL);
OleCheck(ShellFolder.ParseDisplayName(0, nil, StringToOleStr(ExtractFileName(Path)), Eaten, PIDL, Atribute));
ShellFolder.GetUIObjectOf(0, 1, PIDL, IExtractImage, nil, XtractImage);
CoTaskMemFree(PIDL);
if Assigned(XtractImage) then // Try getting a thumbnail..
begin
RunnableTask := nil;
ASize.cx := SizeX;
ASize.cy := SizeY;
Priority := 0;
Flags:= IEIFLAG_ASPECT or IEIFLAG_OFFLINE or IEIFLAG_CACHE or IEIFLAG_QUALITY;
GetLocationRes := XtractImage.GetLocation(Buf, SizeOf(Buf), Priority, ASize, 32, Flags);
if (GetLocationRes = NOERROR) or (GetLocationRes = E_PENDING) then
begin
if GetLocationRes = E_PENDING then
if XtractImage.QueryInterface(IRunnableTask, RunnableTask) <> S_OK then
RunnableTask := nil;
try
//do not call OleCheck for debug
XtractImage.Extract(BmpHandle);
// This could consume a long time.
Result := BmpHandle;
except
on E: EOleSysError do
OutputDebugString(PChar(string(E.ClassName) + ': ' + E.message))
end; // try/except
end;
end;
finally
if InitOle then
CoUninitialize;
end;
except
Result := 0;
end;
end;
procedure HackAlpha(ABitmap: TBitmap; Color: TColor);
type
PRGB32 = ^TRGB32;
TRGB32 = record
B, G, R, A: Byte;
end;
PPixel32 = ^TPixel32;
TPixel32 = array[0..0] of TRGB32;
var
Row: PPixel32;
X, Y, slMain, slSize: Integer;
R, G, B: Byte;
c: Integer;
begin
ABitmap.PixelFormat := pf32bit;
c := ColorToRGB(Color);
R := Byte(c);
G := Byte(c shr 8);
B := Byte(c shr 16);
slMain := Integer(ABitmap.ScanLine[0]);
slSize := Integer(ABitmap.ScanLine[1]) - slMain;
for Y := 0 to ABitmap.Height - 1 do
begin
Row := PPixel32(slMain);
for X := 0 to ABitmap.Width - 1 do
begin
Row[X].R := Row[X].A * (Row[X].R - R) shr 8 + R;
Row[X].G := Row[X].A * (Row[X].G - G) shr 8 + G;
Row[X].B := Row[X].A * (Row[X].B - B) shr 8 + B;
end;
slMain := slMain + slSize;
end;
end;
function HackIconSize(ABitmap: TBitmap): TPoint;
type
PPixel32 = ^TPixel32;
TPixel32 = array [0..0] of Cardinal;
var
Row: PPixel32;
X, Y, i, j, slMain, slSize: Integer;
begin
ABitmap.PixelFormat := pf32bit;
Result.X := ABitmap.Width;
Result.Y := ABitmap.Height;
if (Result.X < 1) or (Result.Y < 1) then
Exit;
slMain := Integer(ABitmap.ScanLine[0]);
slSize := Integer(ABitmap.ScanLine[1]) - slMain;
Result.X := 0;
Result.Y := 0;
for Y := 0 to ABitmap.Height - 1 do
begin
Row := PPixel32(slMain);
for X := 0 to ABitmap.Width - 1 do
begin
if (Row[X] and $FF000000) <> 0 then
begin
if X > Result.X then
Result.X := X;
if Y > Result.Y then
Result.Y := Y;
end;
end;
slMain := slMain + slSize;
end;
I := Math.Max(Result.X, Result.Y);
j := 0;
while I > j do
j := j + 8;
if j > 256 then
j := 256;
Result.X := j;
Result.Y := Result.X;
end;
procedure GetIconFromFile(AFile: string; var AIcon: TIcon; SHIL_FLAG: Cardinal);
var
LImgList: HIMAGELIST;
SFI: TSHFileInfo;
LIndex: Integer;
begin
// Get the index of the imagelist
SHGetFileInfo(PChar(AFile), FILE_ATTRIBUTE_NORMAL, SFI, SizeOf(TSHFileInfo),
SHGFI_ICON {or SHGFI_LARGEICON} or SHGFI_SHELLICONSIZE or
SHGFI_SYSICONINDEX or SHGFI_TYPENAME or SHGFI_DISPLAYNAME);
if not Assigned(AIcon) then
AIcon := TIcon.Create;
// get image list
SHGetImageList(SHIL_FLAG, IID_IImageList, Pointer(LImgList));
// its index
LIndex := SFI.iIcon;
// seems that ILD_NORMAL returns bad result in Windows 7, so opt for ILD_IMAGE
AIcon.Handle := ImageList_GetIcon(LImgList, LIndex, ILD_IMAGE);
end;
procedure Graphic2Bitmap(const ASrc: TGraphic; const ADest: TBitmap;
const ATransparentColor: TColor);
var
LCrop: TPoint;
begin
if not Assigned(ASrc) or not Assigned(ADest) then
Exit;
if (ASrc.Width = 0) or (ASrc.Height = 0) then
Exit;
ADest.Width := ASrc.Width;
ADest.Height := ASrc.Height;
if ASrc.Transparent then
begin
ADest.Transparent := True;
if (ATransparentColor <> clNone) then
begin
ADest.TransparentColor := ATransparentColor;
ADest.TransparentMode := tmFixed;
ADest.Canvas.Brush.Color := ATransparentColor;
end
else
ADest.TransparentMode := tmAuto;
end;
ADest.Canvas.FillRect(Rect(0, 0, ADest.Width, ADest.Height));
ADest.Canvas.Draw(0, 0, ASrc);
LCrop := HackIconSize(ADest);
ADest.Width := LCrop.X;
ADest.Height := LCrop.Y;
end;
function Byte2Str(const i64Size: Int64): string;
const
i64GB = 1024 * 1024 * 1024;
i64MB = 1024 * 1024;
i64KB = 1024;
begin
if i64Size div i64GB > 0 then
Result := Format('%.1f GB', [i64Size / i64GB])
else if i64Size div i64MB > 0 then
Result := Format('%.2f MB', [i64Size / i64MB])
else if i64Size div i64KB > 0 then
Result := Format('%.0f KB', [i64Size / i64KB])
else
Result := IntToStr(i64Size) + ' bytes';
end;
function CalcTHumbSize(Width, Height, ThumbWidth, ThumbHeight: Cardinal): Cardinal;
begin
Result := 0;
if (Width = 0) or (Height = 0) then
Exit;
if (Width < ThumbWidth) and (Height < ThumbHeight) then
Result := (Width shl 16) + Height
else
begin
if Width > Height then
begin
if Width < ThumbWidth then
ThumbWidth := Width;
Result := (ThumbWidth shl 16) + Trunc(ThumbWidth * Height / Width);
if (Result and $FFFF) >ThumbHeight then
Result := (Trunc(ThumbHeight * Width / Height) shl 16) + ThumbHeight;
end
else
begin
if Height < ThumbHeight then
ThumbHeight := Height;
Result := (Trunc(ThumbHeight * Width / Height) shl 16) + ThumbHeight;
if ((Result shr 16) and $FFFF) > ThumbWidth then
Result := (ThumbWidth shl 16) + Trunc(ThumbWidth * Height / Width);
end;
end;
end;
function Blend(Color1, Color2: TColor; A: Byte): TColor;
var
C1, C2: LongInt;
R, G, B, v1, v2: Byte;
begin
A := Round(2.55 * A);
C1 := ColorToRGB(Color1);
C2 := COlorToRGB(COlor2);
v1 := Byte(C1);
v2 := Byte(C2);
R := A * (v1 - v2) shr 8 + v2;
v1 := Byte(C1 shr 8);
v2 := Byte(C2 shr 8);
G := A * (v1 - v2) shr 8 + v2;
v1 := Byte(C1 shr 16);
v2 := Byte(C2 shr 16);
B := A * (v1 - v2) shr 8 + v2;
Result := (B shl 16) + (G shl 8) + R;
end;
procedure WinGradient(DC: HDC; ARect: TRect; AColor1, AColor2: TColor);
var
Vertexs: array[0..1] of TTriVertex;
GRect: TGradientRect;
begin
Vertexs[0].x := ARect.Left;
Vertexs[0].y := ARect.Top;
Vertexs[0].Red := (AColor1 and $000000FF) shl 8;
Vertexs[0].Green := (AColor1 and $0000FF00);
Vertexs[0].Blue := (AColor1 and $00FF0000) shr 8;
Vertexs[0].Alpha := 0;
Vertexs[1].x := ARect.Right;
Vertexs[1].y := ARect.Bottom;
Vertexs[1].Red := (AColor2 and $000000FF) shl 8;
Vertexs[1].Green := (AColor2 and $0000FF00);
Vertexs[1].Blue := (AColor2 and $00FF0000) shr 8;
Vertexs[1].Alpha := 0;
GRect.UpperLeft := 0;
GRect.LowerRight := 1;
GradientFill(DC, @Vertexs, 2, @GRect, 1, GRADIENT_FILL_RECT_V);
end;
function CompareNatural(s1, s2: string): Integer;
function ExtractNr(n: Integer; var Txt: string): Int64;
begin
while (n <= Length(Txt)) and (Txt[n] >= '0') and (Txt[n] <= '9') do
n := n + 1;
Result := StrToInt64Def(Copy(Txt, 1, n - 1), 0);
Delete(Txt, 1, (n - 1));
end;
var
B: Boolean;
begin
Result := 0;
s1 := LowerCase(s1);
s2 := LowerCase(s2);
if (s1 <> s2) and (s1 <> '') and (s2 <> '') then
begin
B := False;
while (not B) do
begin
if ((s1[1] >= '0') and (s1[1] <= '9'))
and ((s2[1] >= '0') and (s2[1] <= '9'))
then
Result := Sign(ExtractNr(1, s1) - ExtractNr(1, s2))
else
Result := Sign(Integer(s1[1]) - Integer(s2[1]));
B := (Result <> 0) or (Min(Length(s1), Length(s2)) < 2);
if not B then
begin
Delete(s1, 1, 1);
Delete(s2, 1, 1);
end;
end;
end;
if Result = 0 then
begin
if (Length(s1) = 1) and (Length(s2) = 1) then
Result := Sign(Integer(s1[1]) - Integer(s2[1]))
else
Result := Sign(Length(s1) - Length(s2));
end;
end;
// a custom sort
function SortItem(List: rkIntegerList.TIntList; Index1, Index2: Integer): Integer;
var
Item1, Item2: PItemData;
begin
Item1 := Form1.Items[List[Index1]];
Item2 := Form1.Items[List[Index2]];
if Item1.Dir and Item2.Dir then
Result := CompareNatural(Item1.Name, Item2.Name)
else if Item1.Dir then
Result := -1
else if Item2.Dir then
Result := 1
else
Result := CompareNatural(Item1.Name, Item2.Name);
end;
{ Form1 }
procedure TForm1.actPath2ClipExecute(Sender: TObject);
begin
// Copy current path to clipboard
if not CurrentDir.IsEmpty and DirectoryExists(CurrentDir) then
begin
Clipboard.AsText := CurrentDir;
Toast('Path copied to clipboard!', 'Current Path', 'S');
end;
end;
procedure TForm1.actPreviewExecute(Sender: TObject);
begin
pnlPreview.Visible := not pnlPreview.Visible;
end;
procedure TForm1.actSigIntExecute(Sender: TObject);
begin
if DosCommand1.IsRunning then
DosCommand1.SigInt;
end;
procedure TForm1.actUnPinExecute(Sender: TObject);
begin
SpeedButton1Click(Sender);
end;
procedure TForm1.BCEditor1DblClick(Sender: TObject);
begin
UpdateMainMenu(lastExplorerHandle);
end;
procedure TForm1.ButtonedEdit1Enter(Sender: TObject);
begin
// ButtonedEdit1.RightButton.Visible := True;
end;
procedure TForm1.ButtonedEdit1KeyPress(Sender: TObject; var Key: Char);
begin
// avoid ding sound
if (Key = #13) or (Key = #27) then
Key := #0;
end;
procedure TForm1.ButtonedEdit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
I: Integer;
CLI: string;
begin
CLI := ButtonedEdit1.Text;
if key = 13 then
begin
// populateCommands;
if CLI = 'list' then
begin
ListExplorerInstances;
BCEditor1.Text := 'Current HWND: ' + IntToStr(lastExplorerHandle) + '';
for I := 0 to lstExplorerPath.Count - 1 do
begin
if lstExplorerWnd[I] = IntToStr(lastExplorerHandle) then
BCEditor1.Text := BCEditor1.Text + #13#10 + lstExplorerPath[I] + ' ' + lstExplorerWnd[i];
end;
end
else if CLI = 'items' then
begin
ListExplorerInstances;
BCEditor1.Text := 'Current HWND: ' + IntToStr(lastExplorerHandle) + '';
for I := 0 to lstExplorerItem.Count - 1 do
begin
BCEditor1.Text := BCEditor1.Text + #13#10 + lstExplorerItem[I];
end;
end
else if CLI = '%' then
begin
populateEnvironmentStrings;
end
else if CLI = 'preview' then
begin
var curFile := StatusBar1.Panels[0].Text;
if FileExists(curFile) then
BCEditor1.Lines.LoadFromFile(curFile);
end
else if CLI = 'tojpg' then
begin
var curFile := StatusBar1.Panels[0].Text;
if FileExists(curFile) then
begin
if ConvertImageToJpeg(curFile, curFile +'.jpg') then
begin
BCEditor1.Clear;
BCEditor1.Lines.Add('Image converted to JPG %90');
BCEditor1.Lines.Add(curFile + '.jpg');
end;
end;
end
else if CLI = 'center' then
begin
if IsZoomed(lastExplorerHandle) then Exit;
var _R: TRect;
var _M: TMonitor;
GetWindowRect(lastExplorerHandle, _R);
_M := Screen.MonitorFromRect(_R);
if (_R.Width > 0) and (_R.Height > 0) then
begin
var NewPos: TPoint;
NewPos.X := _M.Left + (_M.Width - _R.Width) div 2;
NewPos.Y := _M.Top + (_M.Height - _R.Height) div 2;
MoveWindow(lastExplorerHandle, NewPos.X, NewPos.Y, _R.Width, _R.Height, True);
end;
end
else if CLI = 'cmd' then
begin
if DirectoryExists(lastExplorerPath) then
// ShellExecute(0, PChar('OPEN'), PChar('cmd.exe'), PChar('/k refreshenv && cd /d ' + lastExplorerPath), PChar(lastExplorerPath), SW_SHOWNORMAL);
ShellExecute(0, PChar('OPEN'), PChar('cmd.exe'), PChar('/k cd /d ' + lastExplorerPath), PChar(lastExplorerPath), SW_SHOWNORMAL)
else
ShellExecute(0, PChar('OPEN'), PChar('cmd.exe'), PChar('/k cd %USERPROFILE%'), nil, SW_SHOWNORMAL)
end
else if CLI = 'env' then
begin
BCEditor1.Lines.Clear;
BCEditor1.Lines.Add('[Environment PATH]');
for var _env in FEnvStrings do
BCEditor1.Lines.Add(PChar(_env));
end
else if CLI = 'flushicons' then
begin
FlushIcons;
end
// show file explorer quick access directories
else if CLI = ':' then
begin
populateMyFolders;
end
else if CLI = '>' then
begin
populateEnvExecutables;
end
else if Pos('>', CLI) = 1 then
begin
if Cli.Length > 1 then
begin
var command := Copy(CLI,2, Length(CLI) - 1);
ShellExecute(0, PChar('OPEN'), PChar(command), nil, PChar(lastExplorerPath), SW_SHOWNORMAL);
end
end
else if Pos('find ', CLI) = 1 then
begin
if DirectoryExists(lastExplorerPath) then
begin
DzDirSeek1.Dir := lastExplorerPath;
DzDirSeek1.MaskKind := TDSMaskKind.mkInclusions;
DzDirSeek1.Masks.Clear;
DzDirSeek1.Masks.Add(Copy(CLI,6));
DzDirSeek1.ResultKind := TDSResultKind.rkRelative;
DzDirSeek1.Seek;
BCEditor1.Lines.Clear;
BCEditor1.Text := DzDirSeek1.List.GetText;
end;
end
else if CLI = 'listexplorers' then
begin
ListBox1.Items := lstExplorerPath;
ListBox1.Show;
if ListBox1.Visible then
ListBox1.SetFocus;
end
else if CLI = 'exit' then