Tools
Tools
{ }
{ Turbo Vision File Manager Demo }
{ Copyright (c) 1992 by Borland International }
{ }
{************************************************}
unit Tools;
{$X+,V-}
interface
type
String2 = String[2];
String4 = String[4];
TConfigHeader = String[24];
const
StatusMsg : String = '';
implementation
const
StatusBox : PStatusBox = nil;
StatusPMsg : PString = @StatusMsg;
procedure ShowStatusBox;
var
R: TRect;
P: PView;
begin
if StatusBox <> nil then exit;
R.Assign(0,0,40,5);
StatusBox := New(PStatusBox, Init(R, 'Status'));
with StatusBox^ do
begin
Options := Options or ofCentered;
Options := Options and (not ofBuffered);
Flags := Flags and (not wfClose) and (not wfMove);
R.Assign(2,2,38,3);
P := New(PParamText, Init(R, ^C'%s', 1));
Insert(P);
end;
StatusMsg := '';
StatusPMsg := @StatusMsg;
StatusBox^.SetData(StatusPMsg);
Desktop^.Insert(StatusBox);
end;
procedure ShowCopyStatusBox(MaxSize: Longint);
var
R: TRect;
P: PView;
begin
if StatusBox <> nil then exit;
R.Assign(0,0,40,7);
StatusBox := New(PStatusBox, Init(R, 'Status'));
with StatusBox^ do
begin
Options := Options or ofCentered;
Options := Options and (not ofBuffered);
Flags := Flags and (not wfClose) and (not wfMove);
R.Assign(2,2,38,3);
P := New(PParamText, Init(R, ^C'%s', 1));
Insert(P);
R.Assign(5,4,34,5);
Insert(New(PBarGauge, Init(R, MaxSize)));
R.Assign(2,4,4,5);
Insert(New(PStaticText, Init(R, '0%')));
R.Assign(35,4,39,5);
Insert(New(PStaticText, Init(R, '100%')));
end;
StatusMsg := '';
StatusPMsg := @StatusMsg;
StatusBox^.SetData(StatusPMsg);
Desktop^.Insert(StatusBox);
end;
procedure KillStatusBox;
begin
if StatusBox <> nil then
begin
Dispose(StatusBox, Done);
StatusBox := nil;
end;
end;
Command := '';
Params := '';
{ Does an association exist for this file? }
if not IsExecutable(FileName) then
begin
Association := GetAssociatedCommand(E);
if Association <> nil then Command := Association^.Cmd^;
if Command = '' then
begin
L[0] := Longint(@FileName);
MessageBox(RezStrings^.Get(sNoAssociation), @L, mfError +
mfOKButton);
Exit;
end
else
begin
ParamPos := Pos(' ', Command);
if ParamPos > 0 then
begin
Params := Copy(Command, ParamPos + 1, $FF);
Delete(Command, ParamPos, $FF);
Params := Params + ' ' + FileName;
end
else
Params := FileName;
end;
end
else
begin
Command := FileName;
Params := '';
end;
R.Assign(0,0,50,8);
D:= New(PDialog, Init(R, 'Execute Program'));
with D^ do
begin
Options := Options or ofCentered;
R.Assign(2,2,15,3);
Insert(New(PStaticText, Init(R, ' Executing:')));
R.Assign(15,2,48,3);
Insert(New(PStaticText, Init(R, Command)));
R.Assign(15,3,48,4);
P := New(PInputLine, Init(R, 80));
Insert(P);
R.Assign(2,3,15,4);
Insert(New(PLabel, Init(R, '~P~arameters', P)));
R.Assign(12,5,24,7);
Insert(New(PButton, Init(R, '~O~K', cmOK, bfDefault)));
R.Move(14,0);
Insert(New(PButton, Init(R, '~C~ancel', cmCancel, bfNormal)));
SelectNext(False);
end;
if E = '.BAT' then
begin
Command := GetEnv('COMSPEC');
Params := '/c ' + FileName + Params;
end;
Exec(Command, Params);
SwapVectors;
PrintStr(RezStrings^.Get(sPressAnyKey));
Event.What := evNothing;
repeat
GetKeyEvent(Event);
until Event.What <> evNothing;
InitDosMem;
InitVideo;
InitEvents;
InitSysError;
Application^.Redraw;
FSplit(Viewer, D, N, E);
DoneSysError;
DoneEvents;
DoneVideo;
DoneDosMem;
SwapVectors;
if E = '.BAT' then
begin
Command := GetEnv('COMSPEC');
Params := '/c ' + Viewer + ' ' + FileName;
end
else
begin
Command := Viewer;
Params := FileName;
end;
Exec(Command, Params);
SwapVectors;
InitDosMem;
InitVideo;
InitEvents;
InitSysError;
Application^.Redraw;
end;
{$IFNDEF DPMI}
List := New(PDeviceCollection, Init(10,10));
for I := 0 to 99 do
begin
if GetRedirEntry(I, @LocalName, @NetworkName) then
begin
if (LocalName[0] in ['D'..'Z']) and (LocalName[1] = ':') then
begin
New(Device);
Device^.LocalName := LocalName[0];
P := @NetworkName[2];
Device^.NetworkName := NewStr( StrPas(P) );
List^.Insert(Device);
end;
end
else Break;
end;
if List^.Count = 0 then
begin
Dispose(List, Done);
List := nil;
end;
{$ENDIF}
RedirDeviceList := List;
end;
begin
DriveList := New(PStringCollection, Init(26,0));
DeviceList := RedirDeviceList;
for Drive := 'A' to 'Z' do
begin
if DriveValid(Drive) then
begin
S := Drive + ':';
if DeviceList <> nil then
begin
Device := DeviceList^.FirstThat(@DriveMatch);
if Device <> nil then S := S + ' ' + Device^.NetworkName^;
end;
DriveList^.Insert(NewStr(S));
end;
end;
if DriveList^.Count = 0 then
begin
Dispose(DriveList, Done);
DriveList := nil;
end;
ValidDriveList := DriveList;
if DeviceList <> nil then Dispose(DeviceList, Done);
end;
begin
GetDir(0, CurDir); { save this value }
SelectDrive := ' ';
DriveList := ValidDriveList;
P := DriveList^.FirstThat(@IsCurrentDirectory);
if P <> nil then
LB^.FocusItem(DriveList^.IndexOf(P));
begin
Dest := '';
Dlg := PDialog( RezFile.Get('CopyDialog') );
Application^.ExecuteDialog(Dlg, @Dest);
if Dest = '' then Exit;
Dest := FExpand(Dest);
if (Dest[Length(Dest)] <> '\') and (Dest[Length(Dest)] <> ':') then
Dest := Dest + '\';
C.Init(20);
TotalSize := 0;
if HasTaggedFiles(P) then P^.ForEach(@AddSizes)
else TotalSize := PFileRec(P^.At(Current))^.Size;
ShowCopyStatusBox(TotalSize);
C.Done;
KillStatusBox;
if ConfirmDelete then
begin
D := PDialog( RezFile.Get('ConfirmDelete') );
C := Application^.ExecuteDialog(D, @Params);
end;
if C = cmYes then
begin
{ if file was read-only, clear that attribute }
if (Attr and ReadOnly) <> 0 then
begin
SetFAttr(F, Attr and (not ReadOnly));
if DosError <> 0 then
begin
L := DosError;
Params[0] := @Msg;
Params[1] := Pointer(L);
MessageBox(RezStrings^.Get(sSetAttrErr), @Params, mfError+mfOKButton);
SafeDelete := DosError;
Exit;
end;
end;
begin
ConfirmDelete := False;
StatusMsg := '';
ShowStatusBox;
List^.ForEach(@DeleteIfTagged);
KillStatusBox;
ConfirmDelete := True;
end;
F := List^.At(Current);
Command := cmNo; { default to only deleting current file }
if HasTaggedFiles(List) then
begin
D := PDialog( RezFile.Get('DeleteWhich') );
Command := Application^.ExecuteDialog(D, nil);
end;
end;
procedure RenameFile(const Path: FNameStr; F: PFileRec);
var
D: PRenameDialog;
Dir: DirStr;
N: NameStr;
E: ExtStr;
begin
D := New(PRenameDialog, Init(Path + '\' + F^.Name + F^.Ext));
if D <> nil then
begin
if Application^.ExecuteDialog(D, nil) = cmOK then
begin
FSplit(D^.NewName, Dir, N, E);
F^.Name := N;
F^.Ext := E;
InvalidateDir(Path);
end;
end;
end;
procedure InstallViewer;
var
VPath: FNameStr;
Valid, Done: Boolean;
L: Longint;
begin
VPath := Viewer;
Valid := False;
Done := False;
while (not Valid) and (not Done) do
begin
if InputBox(RezStrings^.Get(sCustomViewer), RezStrings^.Get(sPathAndName),
VPath, SizeOf(FNameStr) - 1) = cmOK then
begin
UpperCase(VPath);
VPath := FSearch(VPath, GetEnv('PATH'));
if VPath = '' then
begin
MessageBox(RezStrings^.Get(sCantLocateOnPath), nil,
mfError + mfOKButton);
end
else if not IsExecutable(VPath) then
begin
L := Longint(@VPath);
MessageBox(RezStrings^.Get(sFileNotAnExe), @L, mfError+mfOKButton);
end
else Valid := True;
end
else Done := True;
end;
if Valid then Viewer := VPath;
end;
procedure SetDisplayPrefs;
var
D: PDialog;
SaveMask: string[12];
begin
D := PDialog( RezFile.Get('DisplayPref') );
SaveMask := ConfigRec.FileMask;
if Application^.ExecuteDialog(D, @ConfigRec) = cmOK then
begin
Uppercase(ConfigRec.FileMask);
procedure SaveConfig;
var
Result: Longint;
F: PDosStream;
Pal: PString;
begin
F := New(PDosStream, Init(GetExeBaseName + CFGExt, stCreate));
Result := F^.Status;
if Result <> 0 then
begin
MessageBox(RezStrings^.Get(sWriteCfgErr), @Result, mfError+mfOKButton);
Exit;
end;
F^.Write(ConfigHeader[1], SizeOf(TConfigHeader) - 1);
F^.Write(ConfigRec, SizeOf(TConfigRec));
F^.Write(Viewer, SizeOf(FNameStr));
Pal := @Application^.GetPalette^;
F^.WriteStr(Pal);
WriteAssociationList(F^);
Dispose(F, Done);
end;
procedure ReadConfig;
var
Result: Longint;
F: PDosStream;
Header: TConfigHeader;
Pal: PString;
begin
F := New(PDosStream, Init(GetExeBaseName + CFGExt, stOpenRead));
Result := F^.Status;
if Result <> 0 then Exit;
F^.Read(Header[1], SizeOf(TConfigHeader) - 1);
Header[0] := Char( SizeOf(TConfigHeader) -1 );
if Header <> ConfigHeader then
begin
MessageBox(RezStrings^.Get(sInvalidCfgErr), nil, mfError + mfOKButton);
Exit;
end;
F^.Read(ConfigRec, SizeOf(TConfigRec));
F^.Read(Viewer, SizeOf(FNameStr));
Pal := F^.ReadStr;
if Pal <> nil then
begin
Application^.GetPalette^ := Pal^;
DoneMemory;
Application^.ReDraw;
DisposeStr(Pal);
end;
ReadAssociationList(F^);
Dispose(F, Done);
end;
SwapVectors;
Exec(GetEnv('COMSPEC'), '/C' + Command);
SwapVectors;
PrintStr(RezStrings^.Get(sPressAnyKey));
repeat
GetKeyEvent(Event);
until Event.What <> evNothing;
InitDosMem;
InitVideo;
InitEvents;
InitSysError;
Application^.Redraw;
end;
end;
begin
if Mover^.Dir = Dest then
begin
MessageBox('Files cannot be copied to same directory.',nil,
mfError + mfOKButton);
Exit;
end;
C.Init(20);
TotalSize := 0;
Mover^.Items^.ForEach(@AddSizes);
ShowCopyStatusBox(TotalSize);
Mover^.Items^.ForEach(@CopyFiles);
C.Done;
KillStatusBox;
InvalidateDir(Dest);
end;
function WildCardMatch(const Name, Card: FNameStr): Boolean;
var
I, J: Integer;
begin
WildCardMatch := False;
J := 1;
I := 1;
while J <= Length(Card) do
case Card[J] of
'*':
begin
while (J <= Length(Card)) and (Card[J] <> '.') do Inc(J);
while (I <= Length(Name)) and (Name[I] <> '.') do Inc(I);
end;
'?':
begin
Inc(J);
Inc(I);
end;
'.':
begin
if I <= Length(Name) then
if Name[I] <> '.' then
Exit
else
Inc(I);
Inc(J);
end;
else
if (I > Length(Name)) or (Card[J] <> Name[I]) then Exit;
Inc(I);
Inc(J);
end;
WildCardMatch := (I > Length(Name)) and (J > Length(Card));
end;
{ TStatusBox }
procedure TStatusBox.HandleEvent(var Event:TEvent);
begin
inherited HandleEvent(Event);
if (Event.What=evBroadcast) and (Event.Command = cmStatusUpdate) then
DrawView;
end;
{ TCopier }
procedure TCopier.ReadMsg(const FName: FNameStr; Progress: Longint);
begin
StatusMsg := RezStrings^.Get(sReading) + FName;
Message(StatusBox, evBroadcast, cmStatusUpdate, nil);
Message(StatusBox, evBroadcast, cmAddGauge, Pointer(Progress div 2));
end;
R.Assign(0,0,55,7);
D := New(PDialog, Init(R, FName));
with D^ do
begin
Options := Options or ofCentered;
R.Assign(2,2,52,3);
Insert(New(PStaticText, Init(R, Msg)));
R.Assign(20,4,32,6);
Insert(New(PButton, Init(R, '~R~etry', cmOK, bfDefault)));
R.Move(14,0);
Insert(New(PButton, Init(R, '~A~bort', cmCancel, bfNormal)));
SelectNext(False);
end;
if Application^.ExecuteDialog(D, nil) = cmOK then IOError := erRetry
else IOError := erAbort;
end;
{ TOkListBox }
end.