-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathformMain.pas
1049 lines (887 loc) · 32.8 KB
/
formMain.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
{
******************************************************
USB Disk Ejector
Copyright (c) 2006 - 2017 Bennyboy
Http://quickandeasysoftware.net
******************************************************
}
{
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
{
DO BEFORE RELEASE:
DISABLE ReportMemoryLeaksOnShutdown in project's .dpr
Change build configuration from debug to release
Update readme
Update version string in uDiskEjectConst
Update version info in project
Jedi API - change compiler def to Win2000 and up
JwaWindows - check which version its pointing to - release/debug static/dynamic
}
{
TODO:
OTHER:
For grouped drives show mountpoint/drive letters somehow
Cant tab to the move label on main form
Optional/Possibilities/Non-critical:
See mindmap for full list
Timer for ejection? - eg if running from pstart menu - run command to eject stick in 5 secs - gives time to close the menu
Intercept shutdown/restart message and warn that usb stick is still in the drive. Dont forget it.
REMOVEALL switch - eject every usb drive it finds?
Hide certain drives when specified by user?
Use new Delph 2010 hints for all controls in options
If restarts in mobile mode and eject fails - load main app back up again somehow?
Customise what is displayed for each drive? Icon, colour, label etc
Hotkeys - need to know when restoring from ini - rebuildhotkeys - if hotkey active or not + to show this in options
Localisation
Card readers possible alternate detection? Use same method as windows rather than polling - is this available on all versions of windows though?
}
unit formMain;
interface
uses
Forms, Sysutils, Controls, Classes, ExtCtrls, ImgList,
graphics, JwaWindows, types, UiTypes, System.Contnrs,
JvExControls, JvLabel, JvAppInst,
JclSysInfo, JclShell, JCLStrings,
Menus, VirtualTrees, Generics.Collections,
uDiskEjectConst, uDiskEjectUtils, uDiskEjectOptions,
uCustomHotKeyManager, uCardReaderManager, uDriveEjector, uCommunicationManager,
System.ImageList;
type
TMainfrm = class(TForm)
Tree: TVirtualStringTree;
TrayIcon1: TTrayIcon;
pnlBottom: TPanel;
popupmenuTray: TPopupMenu;
popupExit: TMenuItem;
popupOptions: TMenuItem;
popupAbout: TMenuItem;
lblMore: TJvLabel;
popupEjectMenu: TMenuItem;
JvAppInstances1: TJvAppInstances;
ImageList1: TImageList;
ImageList2: TImageList;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure TreeGetNodeDataSize(Sender: TBaseVirtualTree;
var NodeDataSize: Integer);
procedure TreeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
procedure TreeDblClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure TrayIcon1Click(Sender: TObject);
procedure TreeKeyPress(Sender: TObject; var Key: Char);
procedure popupExitClick(Sender: TObject);
procedure popupOptionsClick(Sender: TObject);
procedure popupmenuTrayPopup(Sender: TObject);
procedure popupAboutClick(Sender: TObject);
procedure lblMoreMouseEnter(Sender: TObject);
procedure lblMoreMouseLeave(Sender: TObject);
procedure lblMoreClick(Sender: TObject);
procedure TreeMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure JvAppInstances1Rejected(Sender: TObject);
procedure TreeInitNode(Sender: TBaseVirtualTree; ParentNode,
Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
procedure TreeDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
Node: PVirtualNode; Column: TColumnIndex; const Text: string;
const CellRect: TRect; var DefaultDraw: Boolean);
procedure TreeGetHint(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; var LineBreakStyle: TVTTooltipLineBreakStyle;
var HintText: string);
procedure FormShow(Sender: TObject);
procedure TreeGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode;
Kind: TVTImageKind; Column: TColumnIndex; var Ghosted: Boolean;
var ImageIndex: TImageIndex);
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
DrivePopups: array of TMenuItem;
Procedure MinimizeClick(Sender:TObject);
Procedure MinimizeToTray;
procedure FillDriveList;
procedure CloseProgram; //Use this rather than form.close because this prevents the CloseToTray option stopping it closing
procedure HotKeyPressed(HotKey: Cardinal; Index: Word);
procedure ResizeTree;
procedure OnCardMediaChanged (Sender: TObject);
procedure OnDrivesChanged (Sender: TObject);
procedure DrivePopupMenuHandler(Sender: TObject);
procedure AddDrivePopups;
procedure ChangeDriveVisibility;
procedure UpdateFormStrings;
procedure GUIRemoveDrive(MountPoint: String; RemoveCard: boolean);
public
end;
var
Mainfrm: TMainfrm;
Ejector: TDriveEjector;
Communicator: TCommunicationManager;
HotKeys: TCustomHotKeyManager;
CardReaders: TCardReaderManager;
ForceClose: boolean = false; //Used by CloseProgram() to prevent the CloseToTray option stopping the app closing
implementation
uses formOptions, formAbout;
{$R *.dfm}
procedure TMainfrm.FormCreate(Sender: TObject);
begin
{Set font on Vista/Windows7}
if IsWindowsVistaOrLater and (Screen.Fonts.IndexOf('Segoe UI') > 0)then
begin
Application.DefaultFont.Name := 'Segoe UI';
Application.DefaultFont.Size := 9; //Segoe UI default is size 9
Tree.ParentFont:=false;
Tree.Font.Size:=10;
Tree.Header.ParentFont:=true;
Tree.Header.Font.Size:=9;
end
else
begin
//Resize tree fonts
Tree.ParentFont:=false;
Tree.Font.Size:=10;
Tree.Header.ParentFont:=true;
Tree.Header.Font.Size:=8;
end;
//For detecting escape key press to minimize
KeyPreview := true;
//Load strings
UpdateFormStrings;
if Options.MaxWidth >0 then
Mainfrm.Constraints.MaxWidth := options.MaxWidth;
if options.PreserveWindowSize then
begin
mainfrm.Height:=options.WindowHeight;
mainfrm.Width:=options.WindowWidth;
end;
if options.PreserveWindowLocation then
begin
mainfrm.top:=options.WindowTopPos;
mainfrm.left:=options.WindowLeftPos;
end;
Communicator := TCommunicationManager.Create(TrayIcon1);
Ejector:=TDriveEjector.Create;
Application.OnMinimize := MinimizeClick;
HotKeys:=TCustomHotKeyManager.Create;
HotKeys.OnHotKeyPressed:=HotKeyPressed;
CardReaders := TCardReaderManager.Create;
Ejector.OnCardMediaChanged:=OnCardMediaChanged;
Ejector.OnDrivesChanged:=OnDrivesChanged;
Ejector.CardPolling:=Options.CardPolling;
Ejector.CardPollingInterval := Options.CardPollingInterval;
Options.CardReaders := CardReaders;
Options.RebuildCardReaders;
FillDriveList;
end;
procedure TMainfrm.FormDestroy(Sender: TObject);
var
i: integer;
begin
if DrivePopups <> nil then
begin
for i:=low(DrivePopups) to high(DrivePopups) do
DrivePopups[i].Free;
DrivePopups:=nil;
end;
HotKeys.Free;
CardReaders.Free;
Ejector.Free;
Communicator.Free;
end;
procedure TMainfrm.FormKeyPress(Sender: TObject; var Key: Char);
begin
if Key = #27 then
if Options.MinimizeToTray then
Self.MinimizeClick(self)
else
Application.Minimize;
end;
procedure TMainfrm.FormShow(Sender: TObject);
begin
//Delay the hints
Application.HintPause := 500;
end;
procedure TMainfrm.HotKeyPressed(HotKey: Cardinal; Index: Word);
var
EjectCard: boolean;
TempMountPoint, TempParam: string;
begin
//Stop hotkeys if options form or about form are showing
if Optionsfrm.Showing then exit;
if Aboutfrm.Showing then exit;
EjectCard:=false;
case TCustomHotKey(HotKeys.HotKeys[Index]).HotKeyType of
RestoreMinimizeAppToggle: begin
if (IsIconic(Application.Handle)=false) and (Self.Visible= true) then //Already showing...so minimize
begin
if Options.MinimizeToTray then
Self.MinimizeClick(self)
else
Application.Minimize;
end
else //Hidden - so show and bring to front
TrayIcon1Click(Mainfrm);
end;
EjectByDriveLetter: begin
TempMountPoint := ConvertDriveLetterToMountPoint(TCustomHotKey(HotKeys.HotKeys[Index]).HotKeyParam);
GetIfCardReader_FromMountPoint(TempMountPoint, Ejector, EjectCard);
GUIRemoveDrive(TempMountPoint, EjectCard);
end;
EjectByMountPoint: begin
GetIfCardReader_FromMountPoint(TCustomHotKey( HotKeys.HotKeys[Index]).HotKeyParam, Ejector, EjectCard);
GUIRemoveDrive(TCustomHotKey(
HotKeys.HotKeys[Index]).HotKeyParam, EjectCard);
end;
EjectByDriveName: begin
TempParam:=TCustomHotKey(HotKeys.HotKeys[Index]).HotKeyParam;
TempMountPoint:=MatchNameToMountPoint(TempParam, Ejector);
GetIfCardReader_FromName(TempParam, Ejector, EjectCard);
if TempMountPoint = '' then
begin
if options.UseWindowsNotifications = false then
Communicator.DoMessage('"' + TempParam + '" ' + str_REMOVE_ERROR_NAME_NOT_FOUND, bfError);
end
else
GUIRemoveDrive(TempMountPoint, EjectCard);
end;
EjectByDriveLabel: begin
TempParam:=TCustomHotKey(HotKeys.HotKeys[Index]).HotKeyParam;
TempMountPoint:=MatchLabelToMountPoint(TempParam, Ejector);
GetIfCardReader_FromMountPoint(TempMountPoint, Ejector, EjectCard);
if TempMountPoint = '' then
begin
if options.UseWindowsNotifications = false then
Communicator.DoMessage('"' + TempParam + '" ' + str_REMOVE_ERROR_LABEL_NOT_FOUND, bfError);
end
else
GUIRemoveDrive(TempMountPoint, EjectCard);
end;
end;
end;
procedure TMainfrm.JvAppInstances1Rejected(Sender: TObject);
begin
trayicon1.Visible:=false;
end;
procedure TMainfrm.CloseProgram;
begin
ForceClose:=true;
Mainfrm.Close;
end;
procedure TMainfrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ForceClose then //Stops CloseToTray option preventing closure if we really need to close the app
begin
ForceClose:=false
end
else
if options.CloseToTray then
begin
Action:=caNone;
MinimizeToTray;
end;
if options.PreserveWindowSize then
begin
options.WindowHeight:=mainfrm.Height;
options.WindowWidth:=mainfrm.Width;
options.SaveConfig;
end;
if options.PreserveWindowLocation then
begin
options.WindowTopPos:=mainfrm.top;
options.WindowLeftPos:=mainfrm.left;
options.SaveConfig;
end;
if options.InMobileMode=false then exit;
CreateCleanupBatFileAndRun;
end;
procedure TMainfrm.lblMoreClick(Sender: TObject);
var
pt: TPoint;
begin
pt:=lblMore.ClientToScreen( Point( 0, lblMore.Height+1 ));
if assigned( lblMore.popupmenu ) then
begin
popupEjectMenu.Visible:=false; //Hide ejection popups when clicking label
lblMore.popupmenu.popup( pt.x, pt.y );
popupEjectMenu.Visible:=true;
end;
//popupmenutray.Popup(mainfrm.Left + pnlBottom.Left, mainfrm.Top + pnlBottom.Top + pnlBottom.Height);
end;
procedure TMainfrm.lblMoreMouseEnter(Sender: TObject);
begin
lblMore.Font.style:=[fsunderline];
end;
procedure TMainfrm.lblMoreMouseLeave(Sender: TObject);
begin
lblMore.Font.style:=[];
end;
Procedure TMainfrm.MinimizeClick(Sender:TObject);
begin
if Options.MinimizeToTray = false then exit;
MinimizeToTray;
end;
procedure TMainfrm.MinimizeToTray;
begin
Hide;
if IsWindowVisible(Application.Handle) then //hide the taskbar button
ShowWindow(Application.Handle, SW_HIDE);
end;
procedure TMainfrm.OnCardMediaChanged(Sender: TObject);
var
i: integer;
begin
//beep;
ChangeDriveVisibility;
//CHECK if this causes problems when form is minimized
tree.FullExpand;
//Change icons if necessary
for i:=low(DrivePopups) to high(DrivePopups) do
begin
if Ejector.RemovableDrives[i].IsCardReader then
begin
if Ejector.RemovableDrives[i].CardMediaPresent then
popupEjectMenu.Items[i].ImageIndex:=6
else
popupEjectMenu.Items[i].ImageIndex:=5
end;
end;
ResizeTree;
end;
procedure TMainfrm.popupAboutClick(Sender: TObject);
begin
Aboutfrm.ShowModal;
end;
procedure TMainfrm.popupExitClick(Sender: TObject);
begin
CloseProgram;
end;
procedure TMainfrm.popupmenuTrayPopup(Sender: TObject);
var
i: integer;
begin
if (fsModal in Aboutfrm.FormState) or (fsModal in Optionsfrm.FormState) then
begin
for I := 0 to popupmenuTray.Items.Count - 1 do
popupmenuTray.Items[i].Enabled:=false;
popupExit.Enabled:=true;
end
else
begin
for I := 0 to popupmenuTray.Items.Count - 1 do
popupmenuTray.Items[i].Enabled:=true;
end;
//If there are no drive popups then disable the submenu
if Length(DrivePopups) > 0 then
popupEjectMenu.Enabled:=true
else
popupEjectMenu.Enabled:=false;
end;
procedure TMainfrm.popupOptionsClick(Sender: TObject);
begin
Optionsfrm.showmodal;
Communicator.RefreshOptions;
Ejector.CardPollingInterval := Options.CardPollingInterval;
Ejector.CardPolling:=Options.CardPolling;
if Options.MaxWidth >0 then
Mainfrm.Constraints.MaxWidth := options.MaxWidth
else
Mainfrm.Constraints.MaxWidth := null;
FillDriveList; //Options probably changed - rather than lots of checking what option has changed just rescan no matter what
end;
procedure TMainfrm.GUIRemoveDrive(MountPoint: String; RemoveCard: boolean);
var
EjectError: integer;
strCardEjectSwitch: string;
begin
//Check if trying to eject drive that its running from
if IsAppRunningFromThisLocation( MountPoint ) then
begin
if RemoveCard then
strCardEjectSwitch := ' /EJECTCARD' //Space before as it should always be last param
else
strCardEjectSwitch := '';
StartInMobileMode('/NOSAVE ' + '/REMOVEMOUNTPOINT ' + StrQuote(MountPoint, '"') + strCardEjectSwitch);
CloseProgram;
Exit;
end;
//Otherwise just eject
Tree.Enabled:=false;
if Ejector.RemoveDrive(MountPoint, EjectError, options.UseWindowsNotifications, RemoveCard, options.CloseRunningApps_Ask, options.CloseRunningApps_Force) = false then
begin;
Tree.Enabled:=true;
if options.UseWindowsNotifications = false then //If true then windows shows its own error messagebox
begin
case EjectError of
REMOVE_ERROR_DRIVE_NOT_FOUND: Communicator.DoMessage( '(' + MountPoint + ':) ' + str_REMOVE_ERROR_DRIVE_NOT_FOUND, bfError);
REMOVE_ERROR_DISK_IN_USE: Communicator.DoMessage( '(' + MountPoint + ':) ' + str_REMOVE_ERROR_DISK_IN_USE, bfError);
REMOVE_ERROR_NO_CARD_MEDIA: Communicator.DoMessage( '(' + MountPoint + ':) ' + str_REMOVE_ERROR_NO_CARD_MEDIA, bfError);
REMOVE_ERROR_WINAPI_ERROR: Communicator.DoMessage( '(' + MountPoint + ':) ' + str_REMOVE_ERROR_WINAPI_ERROR, bfError);
REMOVE_ERROR_NONE: Communicator.DoMessage( '(' + MountPoint + ':) ' + str_Remove_Error_None + inttostr(GetLastError), bfError);
REMOVE_ERROR_UNKNOWN_ERROR: //No point showing if windows error code is just 0. 0= no problems
begin
if GetLastError > 0 then Communicator.DoMessage( '(' + MountPoint + ':) ' + str_REMOVE_ERROR_UNKNOWN_ERROR_REPORT_CODE + inttostr(GetLastError), bfError)
else
Communicator.DoMessage( '(' + MountPoint + ':) ' + str_Remove_Error_Unknown_Error, bfError);
end
else
Communicator.DoMessage( '(' + MountPoint + ':) ' + str_REMOVE_ERROR_UNKNOWN_ERROR_REPORT_CODE + inttostr(GetLastError), bfError);
end;
end;
exit;
end
else //Eject successful
begin
if options.UseWindowsNotifications = false then //If true then windows shows its own message
Communicator.DoMessage( '(' + MountPoint + ':) ' + str_REMOVE_SUCCESSFUL, bfInfo );
end;
if RemoveCard then //No WMDeviceChange fired for cards
begin
ChangeDriveVisibility;
ResizeTree;
tree.Enabled:=true;
end;
if options.AfterEject= int_AFTER_EJECT_DO_CLOSE then CloseProgram;
if options.AfterEject= int_AFTER_EJECT_DO_MINIMIZE then Application.Minimize;
end;
procedure TMainfrm.ResizeTree;
var
i, NodeWidth, NodesHeight: integer;
TempNode, PrevNode: pVirtualNode;
statictext: string;
const
VertFormPadding: integer = 100;
HorizFormPadding: integer = 140; //This could be wrong 140
begin
if Tree.RootNodeCount = 0 then exit;
if Options.AutoResize = false then exit;
if tree.Enabled = false then exit;
Tree.BeginUpdate;
NodesHeight:=Tree.NodeHeight[tree.GetFirst] * Tree.VisibleCount; {RootNodeCount;} //All nodes same height
NodeWidth:=0;
TempNode:=tree.GetFirst;
for I := 0 to Tree.RootNodeCount - 1 do
begin
if Tree.IsVisible[TempNode] = false then continue; //ignore hidden drives
if Ejector.DrivesCount > 0 then
statictext:=Ejector.RemovableDrives[TempNode.Index].VolumeLabel
else
statictext:='';
//Account for Multiline - first line and second line
if mainfrm.Canvas.TextWidth( StrBefore(#13#10, tree.Text[TempNode, 0]) ) > NodeWidth then
NodeWidth := mainfrm.Canvas.TextWidth( StrBefore(#13#10, tree.Text[TempNode, 0] ) );
if mainfrm.Canvas.TextWidth( StrAfter(#13#10, tree.Text[TempNode, 0]) ) > NodeWidth then
NodeWidth := mainfrm.Canvas.TextWidth( StrAfter(#13#10, tree.Text[TempNode, 0] ) );
prevNode:=TempNode;
TempNode:=Tree.GetNext(PrevNode);
end;
mainfrm.Width:=NodeWidth + HorizFormPadding;
mainfrm.Height:=NodesHeight + VertFormPadding;
{--------------------------------Docking---------------------------------------}
case Options.SnapTo of
//Snapping disabled
0:
begin
//Assume position is screen center or last saved position
//Correct width and check if already docked at right
if mainfrm.Left = screen.Width - mainfrm.Width then
begin
mainfrm.Width:=NodeWidth + HorizFormPadding;
mainfrm.Left:= screen.Width - mainfrm.Width
end
else
mainfrm.Width:=NodeWidth + HorizFormPadding;
//Correct height and check if already docked at bottom
if mainfrm.Top = screen.Height - mainfrm.Height - GetTaskBarHeight then
begin
mainfrm.Height:=NodesHeight + VertFormPadding;
mainfrm.top:= screen.Height - mainfrm.Height - GetTaskBarHeight;
end
else
mainfrm.Height:=NodesHeight + VertFormPadding;
//Check and correct position if resolution different
if mainfrm.Left < 0 then
mainfrm.Left:=0;
if mainfrm.Left + mainfrm.Width > screen.Width then
mainfrm.Left:=screen.Width - mainfrm.Width;
if mainfrm.Top < 0 then
mainfrm.Top:=0;
//Stop form resizing so its below taskbar if its docked at the bottom
//Also corrects for different resolution - if it was previously docked at bottom
if (mainfrm.Top + mainfrm.Height) > (screen.Height - GetTaskBarHeight) then
mainfrm.Top := screen.Height - mainfrm.Height - GetTaskBarHeight;
end;
1:
//Bottom right
begin
if GetTaskBarPos = _RIGHT then
mainfrm.Left := (screen.Width - mainfrm.Width) - GetTaskBarWidth
else
mainfrm.Left:=screen.Width - mainfrm.Width;
if GetTaskBarPos = _BOTTOM then
mainfrm.top:= screen.Height - mainfrm.Height - GetTaskBarHeight
else
mainfrm.top:= screen.Height - mainfrm.Height;
end;
//Top right
2:
begin
if GetTaskBarPos = _RIGHT then
mainfrm.Left := (screen.Width - mainfrm.Width) - GetTaskBarWidth
else
mainfrm.Left:=screen.Width - mainfrm.Width;
if GetTaskBarPos = _TOP then
mainfrm.Top := GetTaskBarHeight
else
mainfrm.top:= 0;
end;
//Top left
3:
begin
if GetTaskBarPos = _LEFT then
mainfrm.Left := GetTaskBarWidth
else
mainfrm.Left:= 0;
if GetTaskBarPos = _TOP then
mainfrm.Top := GetTaskBarHeight
else
mainfrm.top:= 0;
end;
//Bottom left
4:
begin
if GetTaskBarPos = _LEFT then
mainfrm.Left := GetTaskBarWidth
else
mainfrm.Left:= 0;
if GetTaskBarPos = _BOTTOM then
mainfrm.Top := screen.Height - mainfrm.Height - GetTaskBarHeight
else
mainfrm.top:= screen.Height - mainfrm.Height;
end;
end;
{------------------------------------------------------------------------------}
//Stops the occasional statictext glitches where they arent fully repainted
tree.ReinitChildren(tree.RootNode, true);
Tree.EndUpdate;
end;
procedure TMainfrm.TrayIcon1Click(Sender: TObject);
begin
//SendMessage(handle, WM_SYSCOMMAND, SC_RESTORE, 0);
Application.Restore; {restore the application}
if WindowState = wsMinimized then WindowState := wsNormal; {Reset minimized state}
Visible:=true;
ResizeTree;
SetForegroundWindow(Application.Handle); {Force form to the foreground }
end;
procedure TMainfrm.TreeDblClick(Sender: TObject);
var
RemoveCard: boolean;
MountPoint: String;
begin
if Ejector.DrivesCount = 0 then exit;
if Tree.SelectedCount = 0 then exit;
if Tree.VisibleCount = 0 then exit;
RemoveCard:=Ejector.RemovableDrives[Tree.focusednode.Index].IsCardReader;
MountPoint:=Ejector.RemovableDrives[Tree.focusednode.Index].DriveMountPoint;
GUIRemoveDrive(MountPoint, RemoveCard);
end;
procedure TMainfrm.TreeDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
Node: PVirtualNode; Column: TColumnIndex; const Text: string;
const CellRect: TRect; var DefaultDraw: Boolean);
var
DrawRect: TRect;
DrawFlags: Cardinal;
DrawParams: TDrawTextParams;
TextEnds: integer;
TempText: string;
begin
defaultdraw:=false;
DrawRect := CellRect;
DrawFlags := DT_END_ELLIPSIS or DT_NOPREFIX {or DT_WORDBREAK} or DT_EDITCONTROL;
DrawText(TargetCanvas.Handle, PChar(Text), -1, DrawRect, DrawFlags or DT_CALCRECT);
DrawRect.Right := CellRect.Right;
if DrawRect.Bottom < CellRect.Bottom then
OffsetRect(DrawRect, 0, (CellRect.Bottom - DrawRect.Bottom) div 2)
else
DrawRect.Bottom := CellRect.Bottom;
ZeroMemory(@DrawParams, SizeOf(DrawParams));
DrawParams.cbSize := SizeOf(DrawParams);
//See if there's a newline character
TextEnds := AnsiPos(#13#10, Text);
if TextEnds > 0 then
begin
//Draw the top line
TempText:=Copy(Text, 1, TextEnds);
DrawTextEx(TargetCanvas.Handle, PChar(TempText), -1, DrawRect, DrawFlags, @DrawParams);
//Draw the bottom line in bold
TempText:=Copy(Text, TextEnds, length(text));
TargetCanvas.Font.Style :=[fsBold];
DrawTextEx(TargetCanvas.Handle, PChar(TempText), -1, DrawRect, DrawFlags, @DrawParams);
end
else //draw normally
begin
DrawTextEx(TargetCanvas.Handle, PChar(Text), -1, DrawRect, DrawFlags, @DrawParams);
end;
end;
procedure TMainfrm.DrivePopupMenuHandler(Sender: TObject);
var
MountPoint: string;
EjectCard: boolean;
begin
with Sender as TMenuItem do
begin
MountPoint:=Ejector.RemovableDrives[tag].DriveMountPoint;
EjectCard:=Ejector.RemovableDrives[tag].IsCardReader;
GUIRemoveDrive(MountPoint, EjectCard);
end;
end;
procedure TMainfrm.FillDriveList;
begin
Tree.BeginUpdate;
if Ejector.DrivesCount = 0 then
begin
Tree.RootNodeCount:=1
end
else
begin
Tree.RootNodeCount:=Ejector.DrivesCount;
Tree.FocusedNode:=Tree.GetFirst;
Tree.Selected[Tree.GetFirst]:=true;
end;
AddCustomCardReaders(CardReaders, Ejector);
ChangeDriveVisibility;
AddDrivePopups;
Tree.EndUpdate;
//if tree is resized when form is minimised then form partially restores itself
if WindowState <> wsMinimized then
ResizeTree;
end;
procedure TMainfrm.ChangeDriveVisibility;
var
i: integer;
List: TList<Integer>;
TempNode, PrevNode: pVirtualNode;
begin
if Ejector.DrivesCount = 0 then //Make that first node visible again
begin
Tree.IsVisible[Tree.GetFirst] := true;
exit;
end;
//Make all drives visible again to start with
TempNode:=tree.GetFirst;
for I := 0 to Tree.RootNodeCount - 1 do
begin
Tree.IsVisible[TempNode] := true;
prevNode:=TempNode;
TempNode:=Tree.GetNext(PrevNode);
end;
List := TList<Integer>.Create();
try
TempNode:=tree.GetFirst;
for I := 0 to Tree.RootNodeCount - 1 do
begin //First check if all card readers should be hidden
if Options.ShowCardReaders = false then
begin
if Ejector.RemovableDrives[i].IsCardReader then
Tree.IsVisible[TempNode] := false;
end
else //If not check if card readers with no media in should be hidden
if Options.HideCardReadersWithNoMedia then
begin
if Ejector.RemovableDrives[i].IsCardReader then
if Ejector.RemovableDrives[i].CardMediaPresent = false then
Tree.IsVisible[TempNode] := false;
end;
if Options.ShowPartitionsAsOne then
begin
if Ejector.RemovableDrives[i].IsCardReader = false then //Dont group card readers
begin
if Ejector.RemovableDrives[i].HasSiblings then
if List.IndexOf(Ejector.RemovableDrives[i].ParentDevInst) <> -1 then //drive with same is already found and visible
Tree.IsVisible[TempNode] := false
else
List.Add(Ejector.RemovableDrives[i].ParentDevInst);
end;
end;
prevNode:=TempNode;
TempNode:=Tree.GetNext(PrevNode);
end;
finally
List.Free;
end;
end;
procedure TMainfrm.AddDrivePopups;
var
i: integer;
TempNode, PrevNode: pVirtualNode;
begin
if DrivePopups <> nil then
for i:=low(DrivePopups) to high(DrivePopups) do
begin
DrivePopups[i].Free;
end;
if Ejector.DrivesCount = 0 then
begin
DrivePopups:=nil;
exit;
end;
TempNode:=tree.GetFirst;
SetLength(DrivePopups, Ejector.DrivesCount);
for i:=low(DrivePopups) to high(DrivePopups) do
begin
DrivePopups[i]:=TMenuItem.Create(Self);
DrivePopups[i].Caption:=Ejector.RemovableDrives[i].DriveMountPoint + ': ' +
Ejector.RemovableDrives[i].VendorId + ' ' +
Ejector.RemovableDrives[i].ProductID;
if Ejector.RemovableDrives[i].VolumeLabel > '' then
DrivePopups[i].Caption:=DrivePopups[i].Caption + ' (' +
Ejector.RemovableDrives[i].VolumeLabel + ')';
DrivePopups[i].Tag:=i; //Store drive index for eject later
begin //Image index
if pos('IPOD', Uppercase(Ejector.RemovableDrives[i].ProductID )) > 0 then
DrivePopups[i].ImageIndex:=4
else
if Ejector.RemovableDrives[i].BusType = 4 then //firewire
DrivePopups[i].ImageIndex:=7
else
if Ejector.RemovableDrives[i].IsCardReader then
begin
if Ejector.RemovableDrives[i].CardMediaPresent then
DrivePopups[i].ImageIndex:=6
else
DrivePopups[i].ImageIndex:=5
end
else
if Options.ShowPartitionsAsOne and Ejector.RemovableDrives[i].HasSiblings then
DrivePopups[i].ImageIndex:=8
else
DrivePopups[i].ImageIndex:=3;
end;
popupMenuTray.Items[2].add(DrivePopups[i]); //Add to the eject submenu
DrivePopups[i].OnClick:=DrivePopupMenuHandler;
//Show/hide popups - match to whether node is visible in the tree
if Tree.IsVisible[TempNode] then
DrivePopups[i].Visible := true
else
DrivePopups[i].Visible := false;
prevNode:=TempNode;
TempNode:=Tree.GetNext(PrevNode);
end;
end;
procedure TMainfrm.TreeGetHint(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; var LineBreakStyle: TVTTooltipLineBreakStyle;
var HintText: string);
var
i: integer;
begin
if Ejector = nil then exit;
if Ejector.DrivesCount <= 0 then exit;
if Ejector.Busy then exit;
if Ejector.RemovableDrives[Node.Index].HasSiblings then
begin
HintText := Ejector.RemovableDrives[Node.Index].DriveMountPoint;
for I := low(Ejector.RemovableDrives[Node.Index].SiblingIndexes) to high(Ejector.RemovableDrives[Node.Index].SiblingIndexes) do
HintText := HintText + #13#10 + Ejector.RemovableDrives[Ejector.RemovableDrives[Node.Index].SiblingIndexes[i]].DriveMountPoint
end;
end;
procedure TMainfrm.TreeGetImageIndex(Sender: TBaseVirtualTree;
Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
var Ghosted: Boolean; var ImageIndex: TImageIndex);
begin
if Ejector.Busy then exit;
if Kind <> ikOverlay then //fix for vt bug
begin
if Ejector.DrivesCount = 0 then
ImageIndex:=1
else
if pos('IPOD', Uppercase(Ejector.RemovableDrives[node.index].ProductID )) > 0 then
ImageIndex:=2
else
if Ejector.RemovableDrives[node.index].BusType = 4 then //firewire
ImageIndex:=5
else
if Ejector.RemovableDrives[node.index].IsCardReader then
begin
if Ejector.RemovableDrives[node.index].CardMediaPresent then
ImageIndex:=4
else
ImageIndex:=3
end
else
if Options.ShowPartitionsAsOne and Ejector.RemovableDrives[node.Index].HasSiblings then
ImageIndex:=6
else
ImageIndex:=0;
end;
end;
procedure TMainfrm.TreeGetNodeDataSize(Sender: TBaseVirtualTree;
var NodeDataSize: Integer);
begin
NodeDataSize := 0;
end;
procedure TMainfrm.TreeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
begin
if Ejector.Busy then exit;
if Ejector.DrivesCount = 0 then
case texttype of
ttNormal: CellText:= str_No_Drive;
ttStatic: CellText:= '';
end
else
if TextType = ttNormal then
begin
if Options.ShowPartitionsAsOne and Ejector.RemovableDrives[Node.Index].HasSiblings then //Just show name not mountpoint
CellText:=Ejector.RemovableDrives[Node.Index].VendorId + ' ' + Ejector.RemovableDrives[Node.Index].ProductID
else
begin //Build a string - trying to avoid orphaned newline characters - tre bug means they show up as rectangles in xp
if Ejector.RemovableDrives[Node.Index].VolumeLabel > '' then //Trim because VendorId is empty sometimes - so there ends up being a space then productid
CellText:='(' + Ejector.RemovableDrives[Node.Index].DriveMountPoint + ') ' + Trim( Ejector.RemovableDrives[Node.Index].VendorId + ' ' + Ejector.RemovableDrives[Node.Index].ProductID) + #13#10 + Ejector.RemovableDrives[Node.Index].VolumeLabel