-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDIALOGS.PAS
1122 lines (1029 loc) · 28.2 KB
/
DIALOGS.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
{$I COMPILER.INC}
unit Dialogs;
interface
uses
AplObj,
AplConst,
AplTypes,
AplMath,
Actions,
Lists,
Drawing,
Files,
Graphics,
MouseDrv,
KeyDrv,
VeriType,
VeriCons,
ListView,
Controls,
Editors,
Views;
const
acDialogYes = acVeridianDialog + 1;
acDialogOk = acVeridianDialog + 2;
acDialogAll = acVeridianDialog + 3;
acDialogYesToAll = acVeridianDialog + 4;
acDialogNo = acVeridianDialog + 5;
acDialogNoToAll = acVeridianDialog + 6;
acDialogContinue = acVeridianDialog + 7;
acDialogRetry = acVeridianDialog + 8;
acDialogAbort = acVeridianDialog + 9;
acDialogIgnore = acVeridianDialog + 10;
acDialogHelp = acVeridianDialog + 11;
acDialogCancel = acVeridianDialog + 12;
type
PDialog = ^TDialog;
PDialogButton = ^TDialogButton;
PFileDialog = ^TFileDialog;
PFileNameEntry = ^TFileNameEntry;
PFileListView = ^TFileListView;
PShowMessageDialog = ^TShowMessageDialog;
TDialogButtonType =
(
mbNone,
mbYes,
mbOk,
mbAll,
mbYesToAll,
mbNo,
mbNoToAll,
mbContinue,
mbRetry,
mbAbort,
mbIgnore,
mbHelp,
mbCancel
);
TDialogButtons = set of TDialogButtonType;
TDialog = object(TWindow)
private
FDialogButtons: PButtonList;
FOldMenuEnabled: boolean;
FOldActionList: PActionList;
function CreateAction(AButton: TDialogButtonType; AText: string;
AShortCut: word): PAction;
function CreateButton(AButton: TDialogButtonType): PDialogButton;
procedure CreateButtons;
procedure Prepare;
procedure CleanUp;
function IsDialogButton: boolean; virtual;
public
Buttons: TDialogButtons;
ButtonAlign: THorzAlign;
DefaultButton: TDialogButtonType;
CancelButton: TDialogButtonType;
ModalResult: TModalResult;
ActionList: PActionList;
constructor Create(const AId, ATitle: string; AButtons: TDialogButtons);
destructor Free; virtual;
function ShowDialog: TModalResult;
function DialogButtons: PButtonList;
function CanExecute(AActionId: word): boolean; virtual;
procedure SetButtonTabOrders(AStartValue: integer); virtual;
procedure ActionExecute(ASender: PActionControl; AAction: PAction); virtual;
procedure Init; virtual;
procedure BeforeShow; virtual;
procedure AfterShow; virtual;
procedure Layout; virtual;
procedure CenterScreenX;
procedure CenterScreenY;
procedure Close(ASender: PControl); virtual;
procedure KeyPress(var AEvent: TKeyEvent); virtual;
procedure SetDefaultButton(AButton: TDialogButtonType);
procedure SetCancelButton(AButton: TDialogButtonType);
procedure GetButtonArea(var ARect: TRect);
end;
TDialogButton = object(TButton)
private
public
Owner: PDialog;
Default: boolean;
Cancel: boolean;
ModalResult: TModalResult;
ButtonType: TDialogButtonType;
procedure KeyPress(var AEvent: TKeyEvent); virtual;
procedure Init; virtual;
end;
TFileDialogOption =
(
foFileMustExist,
foOverwritePrompt
);
TFileDialogOptions = set of TFileDialogOption;
TFileNameEntry = object(TEdit)
private
public
procedure KeyPress(var AEvent: TKeyEvent); virtual;
end;
TFileListView = object(TListView)
private
public
procedure KeyPress(var AEvent: TKeyEvent); virtual;
procedure MouseDoubleClick(var AEvent: TMouseEvent); virtual;
procedure SelectedIndexChanged(var AEvent: TIndexChangedEvent); virtual;
end;
TFileDialog = object(TDialog)
private
procedure PopulateFileView(const APath: string);
procedure ReadPath(const APath: string);
procedure Cancel;
procedure CloseDialog(AModalResult: TModalResult; AFilename: string);
public
FileView: PFileListView;
FilenameEntry: PFileNameEntry;
DirectoryLabel: PLabel;
InitialPath: PChar;
DefaultExt: PChar;
Filename: PChar;
FilterList: PFileFilterList;
DefaultFilter: PFileFilter;
FileOptions: TFileDialogOptions;
CurrentDir: PDirectoryContents;
LastDir: PChar;
constructor Create(const AId, ATitle: string);
procedure SelectFile(AFile: PFile); virtual;
procedure SelectText(AFilename: string); virtual;
procedure ActionExecute(ASender: PActionControl; AAction: PAction); virtual;
procedure Layout; virtual;
procedure BeforeShow; virtual;
procedure AfterShow; virtual;
procedure Init; virtual;
procedure SetInitialPath(const APath: string);
procedure SetDefaultExt(const AExt: string);
procedure SetFilename(const AFilename: string);
function AddFilter(const ADescription, AFilter: string): PFileFilter;
function GetInitialPath: string;
function GetDefaultExt: string;
function GetFilename: string;
function MinHeight: integer; virtual;
destructor Free; virtual;
end;
TShowMessageDialog = object(TDialog)
private
public
Message: string;
MessageLabel: PLabel;
constructor Create(const ATitle, AMessage: string; AButtons: TDialogButtons);
procedure Init; virtual;
procedure Layout; virtual;
end;
procedure ShowMessage(const ATitle, AMessage: string);
function MessageBox(const ATitle, AMessage: string; AButtons: TDialogButtons;
ADefault: TDialogButtonType; ACancel: TDialogButtonType): TModalResult;
const
DialogButtonIds: array[TDialogButtonType] of string = (
'None',
'Yes',
'Ok',
'All',
'YesToAll',
'No',
'NoToAll',
'Continue',
'Retry',
'Abort',
'Ignore',
'Help',
'Cancel'
);
DialogButtonCaptions: array[TDialogButtonType] of string = (
'',
'&Yes',
'&Ok',
'A&ll',
'Y&es to all',
'&No',
'No &to all',
'C&ontinue',
'&Retry',
'&Abort',
'&Ignore',
'&Help',
'&Cancel'
);
implementation
uses
Strings,
Dos,
AplStr,
AplUtils,
Veridian;
procedure ShowMessage(const ATitle, AMessage: string);
var
dialog: PShowMessageDialog;
begin
dialog := New(PShowMessageDialog, Create(ATitle, AMessage, [mbOk]));
dialog^.ShowDialog;
FreeAndNil(dialog);
end;
function MessageBox(const ATitle, AMessage: string; AButtons: TDialogButtons;
ADefault: TDialogButtonType; ACancel: TDialogButtonType): TModalResult;
var
dialog: PShowMessageDialog;
begin
dialog := New(PShowMessageDialog, Create(ATitle, AMessage, AButtons));
dialog^.SetDefaultButton(ADefault);
dialog^.SetCancelButton(ACancel);
MessageBox := dialog^.ShowDialog;
FreeAndNil(dialog);
end;
constructor TDialog.Create(const AId, ATitle: string; AButtons: TDialogButtons);
begin
VeridianApp^.PushState;
VeridianApp^.State.DrawEnabled := false;
inherited Create(AId, ATitle, Desktop);
VeridianApp^.PopState;
Buttons := AButtons;
CreateButtons;
end;
function TDialog.CreateAction(AButton: TDialogButtonType; AText: string;
AShortCut: word): PAction;
var
newAction: PAction;
newId: word;
begin
newId := acVeridianDialog + word(AButton);
newAction := New(PAction, Create(newId, AText, AShortCut));
newAction^.ModalResult := TModalResult(AButton);
ActionList^.Add(newAction);
CreateAction := newAction;
end;
function TDialog.CreateButton(AButton: TDialogButtonType): PDialogButton;
var
button: PDialogButton;
newId: string;
newAction: PAction;
begin
newId := DialogButtonIds[AButton];
button := New(PDialogButton, CreateParent(newId + 'Button', @self));
button^.Owner := @self;
button^.ShowShortCut := false;
button^.Position := rpParentRelative;
if CancelButton = AButton then
button^.Cancel := true;
if DefaultButton = AButton then
button^.Default := true;
button^.Action := CreateAction(AButton, DialogButtonCaptions[AButton], 0);
button^.Action^.ShortCut := button^.GetAltHotKey;
button^.ButtonType := AButton;
FDialogButtons^.Insert(0, button);
CreateButton := button;
end;
procedure TDialog.CreateButtons;
var
index: TDialogButtonType;
tabOrderValue, tabOrderIndex: integer;
button: PButton;
begin
if (CancelButton = mbNone) and (mbCancel in Buttons) then
CancelButton := mbCancel;
tabOrderValue := 0;
for index := High(TDialogButtonType) downto Succ(mbNone) do begin
if index in Buttons then
button := CreateButton(index);
end;
tabOrderValue := 0;
for tabOrderIndex := 0 to FDialogButtons^.Count - 1 do begin
button := FDialogButtons^.GetItem(tabOrderIndex);
button^.TabOrder := tabOrderValue;
Inc(tabOrderValue);
end;
end;
procedure TDialog.SetDefaultButton(AButton: TDialogButtonType);
var
index: integer;
button: PDialogButton;
begin
for index := 0 to FDialogButtons^.Count - 1 do begin
button := PDialogButton(FDialogButtons^.GetItem(index));
button^.Default := false;
end;
for index := 0 to FDialogButtons^.Count - 1 do begin
button := PDialogButton(FDialogButtons^.GetItem(index));
if button^.ButtonType = AButton then
button^.Default := true;
end;
end;
procedure TDialog.SetCancelButton(AButton: TDialogButtonType);
var
index: integer;
button: PDialogButton;
begin
for index := 0 to FDialogButtons^.Count - 1 do begin
button := PDialogButton(FDialogButtons^.GetItem(index));
button^.Cancel := false;
end;
for index := 0 to FDialogButtons^.Count - 1 do begin
button := PDialogButton(FDialogButtons^.GetItem(index));
if button^.ButtonType = AButton then
button^.Cancel := true;
end;
end;
procedure TDialog.Init;
begin
VeridianApp^.PushState;
VeridianApp^.State.DrawEnabled := false;
inherited Init;
FDialogButtons := New(PButtonList, Create);
FDialogButtons^.DisposeObjects := false;
FOldMenuEnabled := VeridianApp^.MenuBar^.IsEnabled;
Buttons := [];
ButtonAlign := haRight;
DefaultButton := mbNone;
CancelButton := mbNone;
ModalResult := mrNone;
WindowOptions := [
woTitleBar,
woCloseButton,
woMoveable
];
ScrollType := scNone;
Grip^.Visible := false;
TitleBar^.MinimizeButton^.Visible := false;
TitleBar^.MaximizeButton^.Visible := false;
ActionList := New(PActionList, Create);
ActionList^.DisposeObjects := true;
VeridianApp^.PopState;
Padding.CreateValue(2);
Visible := false;
end;
procedure TDialog.CenterScreenX;
begin
X := (Graph^.Mode^.Width - Width) div 2;
end;
procedure TDialog.CenterScreenY;
begin
Y := (Graph^.Mode^.Height - Height) div 2;
end;
procedure TDialog.BeforeShow;
begin
end;
procedure TDialog.AfterShow;
begin
end;
procedure TDialog.Layout;
var
min, bx, by, buttonWidth, buttonHeight: integer;
index: integer;
control: PControl;
button: PDialogButton;
lowRes: boolean;
fontHeight: integer;
totalWidth: integer;
rect: TRect;
begin
inherited Layout;
rect.CreateDims(0, 0, Width, Height);
if FDialogButtons^.Count = 0 then
exit;
lowRes := Graph^.Mode^.Height < 512;
if lowRes then
fontHeight := Font^.Height;
button := PDialogButton(FDialogButtons^.GetItem(0));
fontHeight := button^.Font^.Height;
buttonHeight := fontHeight + button^.Padding.Height + 2 * button^.OuterWidth;
by := Height - Padding.Bottom - buttonHeight - OuterWidth;
min := 65;
if Graph^.Mode^.Width < 512 then
min := 35;
bx := 0;
totalWidth := 0;
for index := FDialogButtons^.Count - 1 downto 0 do begin
button := PDialogButton(FDialogButtons^.GetItem(index));
buttonWidth := Max(min, button^.TrueTextWidth(button^.GetCaption) + button^.Padding.Width);
Inc(totalWidth, buttonWidth + 3);
end;
case ButtonAlign of
haLeft: bx := rect.X;
haRight: bx := rect.Right - outerWidth - Padding.Right;
haCenter: bx := rect.Right - (rect.Width - totalWidth) div 2;
end;
for index := FDialogButtons^.Count - 1 downto 0 do begin
button := PDialogButton(FDialogButtons^.GetItem(index));
buttonWidth := Max(min, button^.TrueTextWidth(button^.GetCaption) + button^.Padding.Width);
Dec(bx, buttonWidth);
button^.SetBounds(bx, by, buttonWidth, buttonHeight);
Dec(bx, 3);
end;
end;
procedure TDialog.SetButtonTabOrders(AStartValue: integer);
var
button: PButton;
index: integer;
begin
for index := 0 to FDialogButtons^.Count - 1 do begin
button := DialogButtons^.GetItem(index);
button^.TabOrder := AStartValue;
Inc(AStartValue);
end;
end;
procedure TDialog.GetButtonArea(var ARect: TRect);
var
button: PButton;
areaHeight: integer;
begin
areaHeight := 0;
if FDialogButtons^.Count > 0 then begin
button := PDialogButton(FDialogButtons^.GetItem(0));
areaHeight := button^.Height + Padding.Bottom + OuterWidth;
end;
ARect.CreateDims(0, ClientHeight - areaHeight ,ClientWidth, areaHeight);
end;
procedure TDialog.KeyPress(var AEvent: TKeyEvent);
var
hotKey: word;
event: TActionEvent;
index: integer;
button: PDialogButton;
begin
inherited KeyPress(AEvent);
for index := 0 to FDialogButtons^.Count - 1 do begin
if AEvent.Handled then
exit;
button := PDialogButton(FDialogButtons^.GetItem(index));
event.Create;
event.Action := button^.Action;
if not Assigned(button^.Action) then
continue;
if not button^.IsVisibleAndEnabled then
continue;
hotKey := button^.GetAltHotKey;
if AEvent.Key = hotKey then begin
AEvent.Handled := true;
ActionExecute(button, button^.Action);
end;
case AEvent.Key of
kyEsc: begin
AEvent.Handled := true;
ModalResult := mrCancel;
Close(nil);
end;
kyEnter: begin
if button^.Focused then begin
AEvent.Handled := true;
ActionExecute(button, button^.Action);
end;
end;
end;
end;
end;
procedure TDialogButton.KeyPress(var AEvent: TKeyEvent);
begin
inherited KeyPress(AEvent);
if AEvent.Handled then
exit;
if not Focused then
exit;
if AEvent.Key = kyLeft then
AEvent.Key := kyShiftTab;
if AEvent.Key = kyRight then
AEvent.Key := kyTab;
Owner^.KeyPress(AEvent);
end;
procedure TDialog.ActionExecute(ASender: PActionControl; AAction: PAction);
begin
if not CanExecute(AAction^.ActionId) then
exit;
ModalResult := AAction^.ModalResult;
Close(ASender);
end;
function TDialog.CanExecute(AActionId: word): boolean;
begin
CanExecute := true;
end;
function TDialog.IsDialogButton: boolean;
begin
IsDialogButton := true;
end;
procedure TDialog.Close(ASender: PControl);
begin
inherited Close(ASender);
end;
{
All this preparation is a bit complex. In order to run the dialog within a
function while still processing the event loop, the dialog basically needs to
take over from TVeridianApp.Run.
To Prepare:
- All the states of the special controls are stored, the desktop is disabled,
and the active control and window are unfocused and deactivated.
- The Desktop's ActionList is reassigned to the dialog's ActionList.
The event loop is then run in the dialog's ShowDialog function rather than
TVeridianApp.Run loop, until the dialog is closed.
To clean up after:
- Special controls are popped back to their original states.
- The dialog is hidden, the desktop is reenabled, and the previous active
control and window are reactivated.
- The ActionList is restored to the Desktop's ActionList.
}
procedure TDialog.Prepare;
var
index: integer;
button: PDialogButton;
found: boolean;
rect: TRect;
begin
VeridianApp^.StoreSpecialControlState;
if Assigned(VeridianApp^.ActiveControl) then
VeridianApp^.ActiveControl^.Unfocus;
if Assigned(VeridianApp^.ActiveWindow) then
VeridianApp^.ActiveWindow^.Deactivate;
FOldMenuEnabled := VeridianApp^.MenuBar^.IsEnabled;
Enabled := true;
VeridianApp^.PushState;
VeridianApp^.State.DrawEnabled := false;
Desktop^.SetEnabled(false);
FOldActionList := Desktop^.ActionList;
VeridianApp^.ClearSpecialControlState;
found := false;
for index := 0 to FDialogButtons^.Count - 1 do begin
button := PDialogButton(FDialogButtons^.GetItem(index));
if button^.Default then begin
VeridianApp^.ActiveControl := FDialogButtons^.GetItem(index);
found := true;
break;
end;
end;
if (not found) and (FDialogButtons^.Count > 0) then
VeridianApp^.ActiveControl := FDialogButtons^.GetItem(0);
VeridianApp^.PopState;
Visible := false;
BringToFront;
VeridianApp^.ActiveWindow := @self;
VeridianApp^.ActiveDialog := @self;
VeridianApp^.PushState;
Desktop^.ActionList := ActionList;
VeridianApp^.MenuBar^.SetEnabled(false);
VeridianApp^.MenuBar^.Draw;
end;
procedure TDialog.CleanUp;
var
activeControl: PFocusControl;
activeWindow: PWindow;
begin
Desktop^.ActionList := FOldActionList;
VeridianApp^.RestoreSpecialControlState;
VeridianApp^.PopState;
if VeridianApp^.ActiveWindow = @self then
VeridianApp^.ActiveWindow := nil;
Deactivate;
VeridianApp^.PushState;
VeridianApp^.State.DrawEnabled := false;
VeridianApp^.MenuBar^.SetEnabled(FOldMenuEnabled);
Desktop^.Controls^.RemoveItem(@self);
Desktop^.SetEnabled(true);
VeridianApp^.PopState;
VeridianApp^.MenuBar^.Draw;
if Assigned(VeridianApp^.ActiveWindow) then begin
activeWindow := VeridianApp^.ActiveWindow;
VeridianApp^.ActiveWindow := nil;
activeWindow^.Activate;
end;
if Assigned(VeridianApp^.ActiveControl) then begin
activeControl := VeridianApp^.ActiveControl;
VeridianApp^.ActiveControl := nil;
activeControl^.Focus;
end;
end;
function TDialog.ShowDialog: TModalResult;
begin
Prepare;
BeforeShow;
Show;
AfterShow;
repeat
VeridianApp^.ProcessEvents;
if Closed or VeridianApp^.Closed then
break;
until Closed;
Hide;
CleanUp;
ShowDialog := ModalResult;
end;
function TDialog.DialogButtons: PButtonList;
begin
DialogButtons := FDialogButtons;
end;
destructor TDialog.Free;
begin
FreeAndNil(FDialogButtons);
FreeAndNil(ActionList);
if Assigned(Parent) then
PControl(Parent)^.Controls^.RemoveItem(@self);
inherited Free;
end;
procedure TDialogButton.Init;
begin
inherited Init;
Default := false;
Cancel := false;
ModalResult := mrNone;
Padding.CreateAll(4, 1, 3, 1);
end;
constructor TFileDialog.Create(const AId, ATitle: string);
begin
inherited Create(AId, ATitle, [mbOk, mbCancel]);
end;
function TFileDialog.AddFilter(const ADescription, AFilter: string): PFileFilter;
var
result: PFileFilter;
begin
result := New(PFileFilter, CreateFilter(ADescription, AFilter));
FilterList^.Add(result);
AddFilter := result;
end;
procedure TFileDialog.CloseDialog(AModalResult: TModalResult; AFilename: string);
var
result: TModalResult;
path: PathStr;
name: NameStr;
ext: NameStr;
exists: boolean;
begin
result := AModalResult;
exists := true;
if not FileExists(AFilename) then begin
exists := false;
FSplit(CurrentDir^.Filter, path, name, ext);
if not HasWildCard(ext) then begin
AFilename := UpperCase(AFilename + ext);
if FileExists(AFilename) then
exists := true;
end;
end;
if not exists and (foFileMustExist in FileOptions) then begin
ShowMessage('Error', 'File ' + ExtractFullName(AFilename) + ' does not exist.');
exit;
end
else if exists and (foOverwritePrompt in FileOptions) then begin
result := MessageBox(
'Warning',
'File ' + ExtractFullName(AFilename) + ' already exists. Overwrite?',
[mbYes, mbNo, mbCancel], mbYes, mbCancel);
end;
case result of
mrYes, mrOk: begin
ModalResult := mrOk;
SetFilename(AFilename);
Close(nil);
end;
mrNo: begin
ModalResult := mrNo;
Close(nil);
end;
end;
end;
procedure TFileDialog.ActionExecute(ASender: PActionControl; AAction: PAction);
var
aFile: PFile;
begin
case AAction^.ModalResult of
mrCancel: Close(ASender);
mrOk: begin
SelectText(FileNameEntry^.GetText);
end;
end;
end;
function TFileDialog.GetInitialPath: string;
begin
GetInitialPath := TString.GetString(InitialPath);
end;
function TFileDialog.GetDefaultExt: string;
begin
GetDefaultExt := TString.GetString(DefaultExt);
end;
function TFileDialog.GetFilename: string;
begin
GetFilename := TString.GetString(Filename);
end;
procedure TFileDialog.SetInitialPath(const APath: string);
begin
TString.AssignString(InitialPath, APath);
end;
procedure TFileDialog.SetDefaultExt(const AExt: string);
begin
TString.AssignString(DefaultExt, AExt);
end;
procedure TFileDialog.SetFilename(const AFilename: string);
begin
TString.AssignString(Filename, AFilename);
end;
procedure TFileDialog.Cancel;
begin
Close(@self);
end;
procedure TFileDialog.SelectText(AFilename: string);
var
isDir: boolean;
name: string;
current: string;
ext: string;
begin
AFilename := Trim(AFilename);
current := GetCurrentDirectory;
if Length(AFilename) = 0 then
exit;
AFilename := FExpand(AFilename);
if (ExtractExtension(AFilename) = '') and
FileExists(AFilename + GetDefaultExt) then
AFilename := UpperCase(AFilename + GetDefaultExt);
if DirectoryExists(AFilename) then begin
AFilename := FExpand(AFilename);
if not ChangeDirectory(AFilename) then
exit;
ReadPath(IncludeBackSlash(AFilename) + CurrentDir^.Filter);
FilenameEntry^.SetText('');
exit;
end;
if HasWildcard(AFilename) then begin
ReadPath(AFilename);
FilenameEntry^.SetText('');
exit;
end;
CloseDialog(mrOk, AFilename);
end;
procedure TFileDialog.SelectFile(AFile: PFile);
var
txt: string;
attributes: word;
error: integer;
tempFile: File;
dir: string;
begin
dir := TString.GetString(CurrentDir^.Name);
if TString.GetString(AFile^.Name) = '..' then
txt := '..'
else
txt := dir + TString.GetString(AFile^.Name);
System.Assign(tempFile, txt);
GetFAttr(tempFile, attributes);
error := DosError;
if error = 0 then begin
if faDirectory in TFileAttributes(byte(attributes)) then begin
if not ChangeDirectory(txt) then
exit;
txt := GetCurrentDirectory;
ReadPath(IncludeBackslash(txt) + CurrentDir^.Filter);
FilenameEntry^.SetText('');
FilenameEntry^.Draw;
end else begin
CloseDialog(mrOk, txt);
end;
end;
end;
procedure TFileDialog.PopulateFileView(const APath: string);
var
index: integer;
newFile: PFile;
item: PListItem;
name: string;
path: string;
begin
FreeAndNil(CurrentDir);
FileView^.Clear;
path := FExpand(Trim(APath));
CurrentDir := TDirectory.GetDirectory(path);
CurrentDir^.Sort(dsFilename, dsAsc, true);
for index := 0 to CurrentDir^.Files^.Count - 1 do begin
newFile := CurrentDir^.Files^.Getitem(index);
name := FirstUpper(TString.GetString(newFile^.Name));
if faDirectory in newFile^.Attr then
name := '[ ' + name + ' ]';
FileView^.AddItemValue(name, newFile);
end;
end;
procedure TFileDialog.BeforeShow;
begin
TString.AssignString(InitialPath, FExpand(GetInitialPath));
VeridianApp^.PushState;
VeridianApp^.State.DrawEnabled := false;
ReadPath(GetInitialPath);
VeridianApp^.PopState;
end;
procedure TFileDialog.AfterShow;
begin
FilenameEntry^.SetText(GetFilename);
FileView^.Focus;
end;
procedure TFileDialog.ReadPath(const APath: string);
var
path: string;
begin
path := FExpand(APath);
PopulateFileView(APath);
FileView^.SelectedIndex := 0;
FileView^.Draw;
DirectoryLabel^.Clear;
DirectoryLabel^.SetCaption(APath);
end;
procedure TFileDialog.Init;
var
lowRes: boolean;
rect: TRect;
button: PButton;
begin
inherited Init;
LastDir := nil;
TString.AssignString(LastDir, GetCurrentDirectory);
FileOptions := [];
Buttons := [mbOk, mbCancel];
Include(WindowOptions, woResizeable);
ScrollType := scNone;
ButtonAlign := haRight;
CurrentDir := nil;
FilterList := New(PFileFilterList, Create);
DefaultFilter := New(PFileFilter, CreateFilter('All Files', '*.*'));
FilterList^.Add(DefaultFilter);
Filename := nil;
DefaultExt := nil;
InitialPath := nil;
FilenameEntry := New(PFileNameEntry, CreateParent('FilenameEntry', @self));
FilenameEntry^.ValidChars := ValidDosChars + ['\', ':', #9];
FilenameEntry^.MaxLength := 255;
FileView := New(PFileListView, CreateParent('FileListView', @self));
FileView^.AutoHeight := false;
DirectoryLabel := New(PLabel, CreateCaption('DirLabel', GetInitialPath, @self));
DirectoryLabel^.AutoSize := false;
DirectoryLabel^.TransparentBack := false;
DirectoryLabel^.Font := VeridianApp^.GetFont('editor');
Width := 300;
Height := 350;
if Graph^.Mode^.Width < 512 then begin
Width := 150;
Height := 150;
end;
CenterScreen;
Padding.CreateValue(2);
end;
function TFileDialog.MinHeight: integer;
begin
MinHeight := 142;
end;
procedure TFileDialog.Layout;
var
items: integer;
lowRes: boolean;
dirHeight: integer;
entryHeight, entryWidth: integer;
xPos: integer;
index: integer;
buttonRect, rect: TRect;
begin
inherited Layout;
FileView^.TabOrder := 0;
FileNameEntry^.TabOrder := 1;
for index := FDialogButtons^.Count - 1 downto 0 do begin
PButtonList(FDialogButtons)^.GetItem(index)^.TabOrder := index + 2;
end;
Grip^.Visible := false;
dirHeight := DirectoryLabel^.Font^.Height + 2;
lowRes := Graph^.Mode^.Width < 512;
if lowRes then begin
items := 12;
entryHeight := Font^.Height + 4;
end
else begin
items := 17;
entryHeight := FilenameEntry^.Font^.Height + 6;
end;
xPos := 0;
entryWidth := ContentWidth;
DirectoryLabel^.SetBounds(
xPos,
0,
entryWidth,
dirHeight);
DirectoryLabel^.VertAlign := vaCenter;
GetButtonArea(buttonRect);
FileView^.SetBounds(
xPos,
DirectoryLabel^.Height,
entryWidth,
Height - TitleBar^.Height - dirHeight - entryHeight - 3 * Padding.Height
- buttonRect.Height
);
FilenameEntry^.SetBounds(
xPos,
FileView^.Y + FileView^.Height + 4,
entryWidth,
entryHeight
);
end;
destructor TFileDialog.Free;
begin
ChangeDirectory(TString.GetString(LastDir));
FreeAndNil(CurrentDir);
FreeAndNil(FilterList);
TString.Free(LastDir);