home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l044 / 2.ddi / TVISION.ZIP / STDDLG.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-10-23  |  36.0 KB  |  1,435 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Turbo Pascal Version 6.0                        }
  5. {       Turbo Vision Unit                               }
  6. {                                                       }
  7. {       Copyright (c) 1990 Borland International        }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. unit StdDlg;
  12.  
  13. {$F+,O+,V-,X+,D-}
  14.  
  15. interface
  16.  
  17. uses Objects, Drivers, Views, Dialogs, Dos;
  18.  
  19. const
  20.  
  21. { Commands }
  22.  
  23.   cmFileOpen    = 800;   { Returned from TFileDialog when Open pressed }
  24.   cmFileReplace = 801;   { Returned from TFileDialog when Replace pressed }
  25.   cmFileClear   = 802;   { Returned from TFileDialog when Clear pressed }
  26.   cmFileInit    = 803;   { Used by TFileDialog internally }
  27.   cmChangeDir   = 804;   { Used by TChDirDialog internally }
  28.   cmRevert      = 805;   { Used by TChDirDialog internally }
  29.  
  30. { Messages }
  31.  
  32.   cmFileFocused = 806;    { A new file was focused in the TFileList }
  33.   cmFileDoubleClicked     { A file was selected in the TFileList }
  34.                 = 807;
  35.  
  36. type
  37.  
  38.   { TSearchRec }
  39.  
  40.   {  Record used to store directory information by TFileDialog }
  41.  
  42.   TSearchRec = record
  43.     Attr: Byte;
  44.     Time: Longint;
  45.     Size: Longint;
  46.     Name: string[12];
  47.   end;
  48.  
  49. type
  50.  
  51.   { TFileInputLine is a special input line that is used by      }
  52.   { TFileDialog that will update its contents in response to a  }
  53.   { cmFileFocused command from a TFileList.                     }
  54.  
  55.   PFileInputLine = ^TFileInputLine;
  56.   TFileInputLine = object(TInputLine)
  57.     constructor Init(var Bounds: TRect; AMaxLen: Integer);
  58.     procedure HandleEvent(var Event: TEvent); virtual;
  59.   end;
  60.  
  61.   { TFileCollection is a collection of TSearchRec's.            }
  62.  
  63.   PFileCollection = ^TFileCollection;
  64.   TFileCollection = object(TSortedCollection)
  65.     function Compare(Key1, Key2: Pointer): Integer; virtual;
  66.     procedure FreeItem(Item: Pointer); virtual;
  67.     function GetItem(var S: TStream): Pointer; virtual;
  68.     procedure PutItem(var S: TStream; Item: Pointer); virtual;
  69.   end;
  70.  
  71.   { TSortedListBox is a TListBox that assumes it has a          }
  72.   { TStoredCollection instead of just a TCollection.  It will   }
  73.   { perform an incremental search on the contents.              }
  74.  
  75.   PSortedListBox = ^TSortedListBox;
  76.   TSortedListBox = object(TListBox)
  77.     SearchPos: Word;
  78.     ShiftState: Byte;
  79.     constructor Init(var Bounds: TRect; ANumCols: Word;
  80.       AScrollBar: PScrollBar);
  81.     procedure HandleEvent(var Event: TEvent); virtual;
  82.     function GetKey(var S: String): Pointer; virtual;
  83.     procedure NewList(AList: PCollection); virtual;
  84.   end;
  85.  
  86.   { TFileList is a TSortedList box that assumes it contains     }
  87.   { a TFileCollection as its collection.  It also communicates  }
  88.   { through broadcast messages to TFileInput and TInfoPane      }
  89.   { what file is currently selected.                            }
  90.  
  91.   PFileList = ^TFileList;
  92.   TFileList = object(TSortedListBox)
  93.     constructor Init(var Bounds: TRect; AWildCard: PathStr;
  94.       AScrollBar: PScrollBar);
  95.     destructor Done; virtual;
  96.     function DataSize: Word; virtual;
  97.     procedure FocusItem(Item: Integer); virtual;
  98.     procedure GetData(var Rec); virtual;
  99.     function GetText(Item: Integer; MaxLen: Integer): String; virtual;
  100.     function GetKey(var S: String): Pointer; virtual;
  101.     procedure HandleEvent(var Event: TEvent); virtual;
  102.     procedure ReadDirectory(AWildCard: PathStr);
  103.     procedure SetData(var Rec); virtual;
  104.   end;
  105.  
  106.   { TFileInfoPane is a TView that displays the information      }
  107.   { about the currently selected file in the TFileList          }
  108.   { of a TFileDialog.                                           }
  109.  
  110.   PFileInfoPane = ^TFileInfoPane;
  111.   TFileInfoPane = object(TView)
  112.     S: TSearchRec;
  113.     constructor Init(var Bounds: TRect);
  114.     procedure Draw; virtual;
  115.     function GetPalette: PPalette; virtual;
  116.     procedure HandleEvent(var Event: TEvent); virtual;
  117.   end;
  118.  
  119.   { TFileDialog is a standard file name input dialog            }
  120.  
  121.   TWildStr = PathStr;
  122.  
  123. const
  124.   fdOkButton      = $0001;      { Put an OK button in the dialog }
  125.   fdOpenButton    = $0002;      { Put an Open button in the dialog }
  126.   fdReplaceButton = $0004;      { Put a Replace button in the dialog }
  127.   fdClearButton   = $0008;      { Put a Clear button in the dialog }
  128.   fdHelpButton    = $0010;      { Put a Help button in the dialog }
  129.   fdNoLoadDir     = $0100;      { Do not load the current directory }
  130.                                 { contents into the dialog at Init. }
  131.                                 { This means you intend to change the }
  132.                                 { WildCard by using SetData or store }
  133.                                 { the dialog on a stream. }
  134.  
  135. type
  136.  
  137.   PFileDialog = ^TFileDialog;
  138.   TFileDialog = object(TDialog)
  139.     FileName: PFileInputLine;
  140.     FileList: PFileList;
  141.     WildCard: TWildStr;
  142.     Directory: PString;
  143.     constructor Init(AWildCard: TWildStr; ATitle: String;
  144.       InputName: String; AOptions: Word; HistoryId: Byte);
  145.     constructor Load(var S: TStream);
  146.     destructor Done; virtual;
  147.     procedure GetData(var Rec); virtual;
  148.     procedure GetFileName(var S: PathStr);
  149.     procedure HandleEvent(var Event: TEvent); virtual;
  150.     procedure SetData(var Rec); virtual;
  151.     procedure Store(var S: TStream);
  152.     function Valid(Command: Word): Boolean; virtual;
  153.   private
  154.     procedure ReadDirectory;
  155.   end;
  156.  
  157.   { TDirEntry }
  158.  
  159.   PDirEntry = ^TDirEntry;
  160.   TDirEntry = record
  161.     DisplayText: PString;
  162.     Directory: PString;
  163.   end;
  164.  
  165.   { TDirCollection is a collection of TDirEntry's used by       }
  166.   { TDirListBox.                                                }
  167.  
  168.   PDirCollection = ^TDirCollection;
  169.   TDirCollection = object(TCollection)
  170.     function GetItem(var S: TStream): Pointer; virtual;
  171.     procedure FreeItem(Item: Pointer); virtual;
  172.     procedure PutItem(var S: TStream; Item: Pointer); virtual;
  173.   end;
  174.  
  175.   { TDirListBox displays a tree of directories for use in the }
  176.   { TChDirDialog.                                               }
  177.  
  178.   PDirListBox = ^TDirListBox;
  179.   TDirListBox = object(TListBox)
  180.     Dir: DirStr;
  181.     Cur: Word;
  182.     constructor Init(var Bounds: TRect; AScrollBar: PScrollBar);
  183.     destructor Done; virtual;
  184.     function GetText(Item: Integer; MaxLen: Integer): String; virtual;
  185.     procedure HandleEvent(var Event: TEvent); virtual;
  186.     function IsSelected(Item: Integer): Boolean; virtual;
  187.     procedure NewDirectory(var ADir: DirStr);
  188.     procedure SetState(AState: Word; Enable: Boolean); virtual;
  189.   end;
  190.  
  191.   { TChDirDialog is a standard change directory dialog.         }
  192.  
  193. const
  194.   cdNormal     = $0000; { Option to use dialog immediately }
  195.   cdNoLoadDir  = $0001; { Option to init the dialog to store on a stream }
  196.   cdHelpButton = $0002; { Put a help button in the dialog }
  197.  
  198. type
  199.  
  200.   PChDirDialog = ^TChDirDialog;
  201.   TChDirDialog = object(TDialog)
  202.     DirInput: PInputLine;
  203.     DirList: PDirListBox;
  204.     OkButton: PButton;
  205.     ChDirButton: PButton;
  206.     constructor Init(AOptions: Word; HistoryId: Word);
  207.     constructor Load(var S: TStream);
  208.     function DataSize: Word; virtual;
  209.     procedure GetData(var Rec); virtual;
  210.     procedure HandleEvent(var Event: TEvent); virtual;
  211.     procedure SetData(var Rec); virtual;
  212.     procedure Store(var S: TStream);
  213.     function Valid(Command: Word): Boolean; virtual;
  214.   private
  215.     procedure SetUpDialog;
  216.   end;
  217.  
  218. const
  219.  
  220.   CInfoPane = #30;
  221.  
  222.   { TStream registration records }
  223.  
  224.   RFileInputLine: TStreamRec = (
  225.      ObjType: 60;
  226.      VmtLink: Ofs(TypeOf(TFileInputLine)^);
  227.      Load:    @TFileInputLine.Load;
  228.      Store:   @TFileInputLine.Store
  229.   );
  230.   RFileCollection: TStreamRec = (
  231.      ObjType: 61;
  232.      VmtLink: Ofs(TypeOf(TFileCollection)^);
  233.      Load:    @TFileCollection.Load;
  234.      Store:   @TFileCollection.Store
  235.   );
  236.   RFileList: TStreamRec = (
  237.      ObjType: 62;
  238.      VmtLink: Ofs(TypeOf(TFileList)^);
  239.      Load:    @TFileList.Load;
  240.      Store:   @TFileList.Store
  241.   );
  242.   RFileInfoPane: TStreamRec = (
  243.      ObjType: 63;
  244.      VmtLink: Ofs(TypeOf(TFileInfoPane)^);
  245.      Load:    @TFileInfoPane.Load;
  246.      Store:   @TFileInfoPane.Store
  247.   );
  248.   RFileDialog: TStreamRec = (
  249.      ObjType: 64;
  250.      VmtLink: Ofs(TypeOf(TFileDialog)^);
  251.      Load:    @TFileDialog.Load;
  252.      Store:   @TFileDialog.Store
  253.   );
  254.   RDirCollection: TStreamRec = (
  255.      ObjType: 65;
  256.      VmtLink: Ofs(TypeOf(TDirCollection)^);
  257.      Load:    @TDirCollection.Load;
  258.      Store:   @TDirCollection.Store
  259.   );
  260.   RDirListBox: TStreamRec = (
  261.      ObjType: 66;
  262.      VmtLink: Ofs(TypeOf(TDirListBox)^);
  263.      Load:    @TDirListBox.Load;
  264.      Store:   @TDirListBox.Store
  265.   );
  266.   RChDirDialog: TStreamRec = (
  267.      ObjType: 67;
  268.      VmtLink: Ofs(TypeOf(TChDirDialog)^);
  269.      Load:    @TChDirDialog.Load;
  270.      Store:   @TChDirDialog.Store
  271.   );
  272.  
  273. procedure RegisterStdDlg;
  274.  
  275. implementation
  276.  
  277. uses App, Memory, HistList, MsgBox;
  278.  
  279. function DriveValid(Drive: Char): Boolean; assembler;
  280. asm
  281.     MOV    DL,Drive
  282.         MOV    AH,36H
  283.         SUB    DL,'A'-1
  284.         INT    21H
  285.         INC    AX
  286.         JE    @@2
  287. @@1:    MOV    AL,1
  288. @@2:
  289. end;
  290.  
  291. function PathValid(var Path: PathStr): Boolean;
  292. var
  293.   ExpPath: PathStr;
  294.   F: File;
  295.   SR: SearchRec;
  296. begin
  297.   ExpPath := FExpand(Path);
  298.   if Length(ExpPath) <= 3 then PathValid := DriveValid(ExpPath[1])
  299.   else
  300.   begin
  301.     if ExpPath[Length(ExpPath)] = '\' then Dec(ExpPath[0]);
  302.     FindFirst(ExpPath, Directory, SR);
  303.     PathValid := (DosError = 0) and (SR.Attr and Directory <> 0);
  304.   end;
  305. end;
  306.  
  307. function ValidFileName(var FileName: PathStr): Boolean;
  308. const
  309.   IllegalChars = ';,=+<>|"[] \';
  310. var
  311.   Dir: DirStr;
  312.   Name: NameStr;
  313.   Ext: ExtStr;
  314.  
  315. { Contains returns true if S1 contains any characters in S2 }
  316. function Contains(S1, S2: String): Boolean; near; assembler;
  317. asm
  318.     PUSH    DS
  319.         CLD
  320.         LDS    SI,S1
  321.         LES    DI,S2
  322.         MOV    DX,DI
  323.         XOR    AH,AH
  324.         LODSB
  325.         MOV    BX,AX
  326.         OR      BX,BX
  327.         JZ      @@2
  328.         MOV    AL,ES:[DI]
  329.         XCHG    AX,CX
  330. @@1:    PUSH    CX
  331.     MOV    DI,DX
  332.     LODSB
  333.         REPNE    SCASB
  334.         POP    CX
  335.         JE    @@3
  336.     DEC    BX
  337.         JNZ    @@1
  338. @@2:    XOR    AL,AL
  339.     JMP    @@4
  340. @@3:    MOV    AL,1
  341. @@4:    POP    DS
  342. end;
  343.  
  344. begin
  345.   ValidFileName := True;
  346.   FSplit(FileName, Dir, Name, Ext);
  347.   if not ((Dir = '') or PathValid(Dir)) or Contains(Name, IllegalChars) or
  348.     Contains(Dir, IllegalChars) then ValidFileName := False;
  349. end;
  350.  
  351. function GetCurDir: DirStr;
  352. var
  353.   CurDir: DirStr;
  354. begin
  355.   GetDir(0, CurDir);
  356.   if Length(CurDir) > 3 then
  357.   begin
  358.     Inc(CurDir[0]);
  359.     CurDir[Length(CurDir)] := '\';
  360.   end;
  361.   GetCurDir := CurDir;
  362. end;
  363.  
  364. type
  365.   PSearchRec = ^TSearchRec;
  366.  
  367. function IsWild(var S: String): Boolean;
  368. begin
  369.   IsWild := (Pos('?',S) > 0) or (Pos('*',S) > 0);
  370. end;
  371.  
  372. function IsDir(var S: String): Boolean;
  373. var
  374.   SR: SearchRec;
  375. begin
  376.   FindFirst(S, Directory, SR);
  377.   if DosError = 0 then
  378.     IsDir := SR.Attr and Directory <> 0
  379.   else IsDir := False;
  380. end;
  381.  
  382. { TFileInputLine }
  383.  
  384. constructor TFileInputLine.Init(var Bounds: TRect; AMaxLen: Integer);
  385. begin
  386.   TInputLine.Init(Bounds, AMaxLen);
  387.   EventMask := EventMask or evBroadcast;
  388. end;
  389.  
  390. procedure TFileInputLine.HandleEvent(var Event: TEvent);
  391. var
  392.   Dir: DirStr;
  393.   Name: NameStr;
  394.   Ext: ExtStr;
  395. begin
  396.   TInputLine.HandleEvent(Event);
  397.   if (Event.What = evBroadcast) and (Event.Command = cmFileFocused) and
  398.     (State and sfSelected = 0) then
  399.   begin
  400.      if PSearchRec(Event.InfoPtr)^.Attr and Directory <> 0 then
  401.         Data^ := PSearchRec(Event.InfoPtr)^.Name + '\'+
  402.           PFileDialog(Owner)^.WildCard
  403.      else Data^ := PSearchRec(Event.InfoPtr)^.Name;
  404.      DrawView;
  405.   end;
  406. end;
  407.  
  408. { TFileCollection }
  409.  
  410. function TFileCollection.Compare(Key1, Key2: Pointer): Integer;
  411. begin
  412.   if PSearchRec(Key1)^.Name = PSearchRec(Key2)^.Name then Compare := 0
  413.   else if PSearchRec(Key1)^.Name = '..' then Compare := 1
  414.   else if PSearchRec(Key2)^.Name = '..' then Compare := -1
  415.   else if (PSearchRec(Key1)^.Attr and Directory <> 0) and
  416.      (PSearchRec(Key2)^.Attr and Directory = 0) then Compare := 1
  417.   else if (PSearchRec(Key2)^.Attr and Directory <> 0) and
  418.      (PSearchRec(Key1)^.Attr and Directory = 0) then Compare := -1
  419.   else if PSearchRec(Key1)^.Name > PSearchRec(Key2)^.Name then
  420.     Compare := 1
  421.   else Compare := -1;
  422. end;
  423.  
  424. procedure TFileCollection.FreeItem(Item: Pointer);
  425. begin
  426.   Dispose(PSearchRec(Item));
  427. end;
  428.  
  429. function TFileCollection.GetItem(var S: TStream): Pointer;
  430. var
  431.   Item: PSearchRec;
  432. begin
  433.   New(Item);
  434.   S.Read(Item^, SizeOf(TSearchRec));
  435.   GetItem := Item;
  436. end;
  437.  
  438. procedure TFileCollection.PutItem(var S: TStream; Item: Pointer);
  439. begin
  440.   S.Write(Item^, SizeOf(TSearchRec));
  441. end;
  442.  
  443. { TSortedListBox }
  444.  
  445. constructor TSortedListBox.Init(var Bounds: TRect; ANumCols: Word;
  446.   AScrollBar: PScrollBar);
  447. begin
  448.   TListBox.Init(Bounds, ANumCols, AScrollBar);
  449.   SearchPos := 0;
  450.   ShowCursor;
  451.   SetCursor(1,0);
  452. end;
  453.  
  454. procedure TSortedListBox.HandleEvent(var Event: TEvent);
  455. var
  456.   ShiftKeys: Byte absolute $40:$17;
  457.   CurString, NewString: String;
  458.   K: Pointer;
  459.   Value, OldPos, OldValue: Integer;
  460.   T: Boolean;
  461.  
  462. function Equal(var S1: String; var S2: String; Count: Word): Boolean;
  463. var
  464.   I: Word;
  465. begin
  466.   Equal := False;
  467.   if (Length(S1) < Count) or (Length(S2) < Count) then Exit;
  468.   for I := 1 to Count do
  469.     if UpCase(S1[I]) <> UpCase(S2[I]) then Exit;
  470.   Equal := True;
  471. end;
  472.  
  473. begin
  474.   OldValue := Focused;
  475.   TListBox.HandleEvent(Event);
  476.   if OldValue <> Focused then SearchPos := 0;
  477.   if Event.What = evKeyDown then
  478.   begin
  479.     if Event.CharCode <> #0 then
  480.     begin
  481.       Value := Focused;
  482.       if Value < Range then CurString := GetText(Value, 255)
  483.       else CurString := '';
  484.       OldPos := SearchPos;
  485.       if Event.KeyCode = kbBack then
  486.       begin
  487.         if SearchPos = 0 then Exit;
  488.         Dec(SearchPos);
  489.         if SearchPos = 0 then ShiftState := ShiftKeys;
  490.         CurString[0] := Char(SearchPos);
  491.       end
  492.       else if (Event.CharCode = '.') then SearchPos := Pos('.',CurString)
  493.       else
  494.       begin
  495.         Inc(SearchPos);
  496.         if SearchPos = 1 then ShiftState := ShiftKeys;
  497.         CurString[0] := Char(SearchPos);
  498.         CurString[SearchPos] := Event.CharCode;
  499.       end;
  500.       K := GetKey(CurString);
  501.       T := PSortedCollection(List)^.Search(K, Value);
  502.       if Value < Range then
  503.       begin
  504.         if Value < Range then NewString := GetText(Value, 255)
  505.         else NewString := '';
  506.         if Equal(NewString, CurString, SearchPos) then
  507.         begin
  508.           if Value <> OldValue then
  509.           begin
  510.             FocusItem(Value);
  511.             { Assumes ListControl will set the cursor to the first character }
  512.             { of the sfFocused item }
  513.             SetCursor(Cursor.X+SearchPos, Cursor.Y);
  514.           end
  515.           else SetCursor(Cursor.X+(SearchPos-OldPos), Cursor.Y);
  516.         end
  517.         else SearchPos := OldPos;
  518.       end
  519.       else SearchPos := OldPos;
  520.       if (SearchPos <> OldPos) or (Event.CharCode in ['A'..'Z','a'..'z']) then
  521.         ClearEvent(Event);
  522.     end;
  523.   end;
  524. end;
  525.  
  526. function TSortedListBox.GetKey(var S: String): Pointer;
  527. begin
  528.   GetKey := @S;
  529. end;
  530.  
  531. procedure TSortedListBox.NewList(AList: PCollection);
  532. begin
  533.   TListBox.NewList(AList);
  534.   SearchPos := 0;
  535. end;
  536.  
  537. { TFileList }
  538.  
  539. constructor TFileList.Init(var Bounds: TRect; AWildCard: PathStr;
  540.   AScrollBar: PScrollBar);
  541. begin
  542.   TSortedListBox.Init(Bounds, 2, AScrollBar);
  543. end;
  544.  
  545. destructor TFileList.Done;
  546. begin
  547.   if List <> nil then Dispose(List, Done);
  548.   TListBox.Done;
  549. end;
  550.  
  551. function TFileList.DataSize: Word;
  552. begin
  553.   DataSize := 0;
  554. end;
  555.  
  556. procedure TFileList.FocusItem(Item: Integer);
  557. begin
  558.   TSortedListBox.FocusItem(Item);
  559.   Message(Owner, evBroadcast, cmFileFocused, List^.At(Item));
  560. end;
  561.  
  562. procedure TFileList.GetData(var Rec);
  563. begin
  564. end;
  565.  
  566. function TFileList.GetKey(var S: String): Pointer;
  567. const
  568.   SR: TSearchRec = ();
  569.  
  570. procedure UpStr(var S: String);
  571. var
  572.   I: Integer;
  573. begin
  574.   for I := 1 to Length(S) do S[I] := UpCase(S[I]);
  575. end;
  576.  
  577. begin
  578.   if (ShiftState and $03 <> 0) or ((S <> '') and (S[1]='.')) then
  579.     SR.Attr := Directory
  580.   else SR.Attr := 0;
  581.   SR.Name := S;
  582.   UpStr(SR.Name);
  583.   GetKey := @SR;
  584. end;
  585.  
  586. function TFileList.GetText(Item: Integer; MaxLen: Integer): String;
  587. var
  588.   S: String;
  589.   SR: PSearchRec;
  590. begin
  591.   SR := PSearchRec(List^.At(Item));
  592.   S := SR^.Name;
  593.   if SR^.Attr and Directory <> 0 then
  594.   begin
  595.     S[Length(S)+1] := '\';
  596.     Inc(S[0]);
  597.   end;
  598.   GetText := S;
  599. end;
  600.  
  601. procedure TFileList.HandleEvent(var Event: TEvent);
  602. begin
  603.   if (Event.What = evMouseDown) and (Event.Double) then
  604.   begin
  605.     Event.What := evCommand;
  606.     Event.Command := cmOK;
  607.     PutEvent(Event);
  608.     ClearEvent(Event);
  609.   end
  610.   else TSortedListBox.HandleEvent(Event);
  611. end;
  612.  
  613. procedure TFileList.ReadDirectory(AWildCard: PathStr);
  614. const
  615.   FindAttr = ReadOnly + Archive;
  616.   AllFiles = '*.*';
  617.   PrevDir  = '..';
  618. var
  619.   S: SearchRec;
  620.   P: PSearchRec;
  621.   FileList: PFileCollection;
  622.   NumFiles: Word;
  623.   CurPath: PathStr;
  624.   Dir: DirStr;
  625.   Name: NameStr;
  626.   Ext: ExtStr;
  627.   Event: TEvent;
  628.   Tmp: PathStr;
  629.   Flag: Integer;
  630. begin
  631.   NumFiles := 0;
  632.   AWildCard := FExpand(AWildCard);
  633.   FSplit(AWildCard, Dir, Name, Ext);
  634.   FileList := New(PFileCollection, Init(5, 5));
  635.   FindFirst(AWildCard, FindAttr, S);
  636.   P := @P;
  637.   while (P <> nil) and (DosError = 0) do
  638.   begin
  639.     if (S.Attr and Directory = 0) then
  640.     begin
  641.       P := MemAlloc(SizeOf(P^));
  642.       if P <> nil then
  643.       begin
  644.         Move(S.Attr, P^, SizeOf(P^));
  645.         FileList^.Insert(P);
  646.       end;
  647.     end;
  648.     FindNext(S);
  649.   end;
  650.   Tmp := Dir + AllFiles;
  651.   FindFirst(Tmp, Directory, S);
  652.   while (P <> nil) and (DosError = 0) do
  653.   begin
  654.     if (S.Attr and Directory <> 0) and (S.Name[1] <> '.') then
  655.     begin
  656.       P := MemAlloc(SizeOf(P^));
  657.       if P <> nil then
  658.       begin
  659.         Move(S.Attr, P^, SizeOf(P^));
  660.         FileList^.Insert(PObject(P));
  661.       end;
  662.     end;
  663.     FindNext(S);
  664.   end;
  665.   if Length(Dir) > 4 then
  666.   begin
  667.     P := MemAlloc(SizeOf(P^));
  668.     if P <> nil then
  669.     begin
  670.       FindFirst(Tmp, Directory, S);
  671.       FindNext(S);
  672.       if (DosError = 0) and (S.Name = PrevDir) then
  673.         Move(S.Attr, P^, SizeOf(P^))
  674.       else
  675.       begin
  676.         P^.Name := PrevDir;
  677.         P^.Size := 0;
  678.         P^.Time := $210000;
  679.         P^.Attr := Directory;
  680.       end;
  681.       FileList^.Insert(PObject(P));
  682.     end;
  683.   end;
  684.   if P = nil then MessageBox('Too many files.', nil, mfOkButton + mfWarning);
  685.   NewList(FileList);
  686.   if List^.Count > 0 then
  687.   begin
  688.     Event.What := evBroadcast;
  689.     Event.Command := cmFileFocused;
  690.     Event.InfoPtr := List^.At(0);
  691.     Owner^.HandleEvent(Event);
  692.   end;
  693. end;
  694.  
  695. procedure TFileList.SetData(var Rec);
  696. begin
  697.   with PFileDialog(Owner)^ do
  698.     Self.ReadDirectory(Directory^ + WildCard);
  699. end;
  700.  
  701. { TFileInfoPane }
  702.  
  703. constructor TFileInfoPane.Init(var Bounds: TRect);
  704. begin
  705.   TView.Init(Bounds);
  706.   EventMask := EventMask or evBroadcast;
  707. end;
  708.  
  709. procedure TFileInfoPane.Draw;
  710. var
  711.   B: TDrawBuffer;
  712.   D: String[9];
  713.   M: String[3];
  714.   PM: Boolean;
  715.   Color: Word;
  716.   Time: DateTime;
  717.   Path: PathStr;
  718.   FmtId: String;
  719.   Params: array[0..7] of LongInt;
  720.   Str: String[80];
  721. const
  722.   sDirectoryLine = ' %-12s %-9s %3s %2d, %4d  %2d:%02d%cm';
  723.   sFileLine      = ' %-12s %-9d %3s %2d, %4d  %2d:%02d%cm';
  724.   Month: array[1..12] of String[3] = 
  725.     ('Jan','Feb','Mar','Apr','May','Jun',
  726.      'Jul','Aug','Sep','Oct','Nov','Dec');
  727. begin
  728.   { Display path }
  729.   Path := FExpand(PFileDialog(Owner)^.Directory^+PFileDialog(Owner)^.WildCard);
  730.   Color := GetColor($01);
  731.   MoveChar(B, ' ', Color, Size.X);
  732.   MoveStr(B[1], Path, Color);
  733.   WriteLine(0, 0, Size.X, 1, B);
  734.  
  735.   { Display file }
  736.   Params[0] := LongInt(@S.Name);
  737.   MoveChar(B, ' ', Color, Size.X);
  738.   Params[0] := LongInt(@S.Name);
  739.   if S.Attr and Directory <> 0 then
  740.   begin
  741.     FmtId := sDirectoryLine;
  742.     D := 'Directory';
  743.     Params[1] := LongInt(@D);
  744.   end else
  745.   begin
  746.     FmtId := sFileLine;
  747.     Params[1] := S.Size;
  748.   end;
  749.   UnpackTime(S.Time, Time);
  750.   M := Month[Time.Month];
  751.   Params[2] := LongInt(@M);
  752.   Params[3] := Time.Day;
  753.   Params[4] := Time.Year;
  754.   PM := Time.Hour >= 12;
  755.   Time.Hour := Time.Hour mod 12;
  756.   if Time.Hour = 0 then Time.Hour := 12;
  757.   Params[5] := Time.Hour;
  758.   Params[6] := Time.Min;
  759.   if PM then Params[7] := Byte('p')
  760.   else Params[7] := Byte('a');
  761.   FormatStr(Str, FmtId, Params);
  762.   MoveStr(B, Str, Color);
  763.   WriteLine(0, 1, Size.X, 1, B);
  764.  
  765.   { Fill in rest of rectangle }
  766.   MoveChar(B, ' ', Color, Size.X);
  767.   WriteLine(0, 2, Size.X, Size.Y-2, B);
  768. end;
  769.  
  770. function TFileInfoPane.GetPalette: PPalette;
  771. const
  772.   P: String[Length(CInfoPane)] = CInfoPane;
  773. begin
  774.   GetPalette := @P;
  775. end;
  776.  
  777. procedure TFileInfoPane.HandleEvent(var Event: TEvent);
  778. begin
  779.   TView.HandleEvent(Event);
  780.   if (Event.What = evBroadcast) and (Event.Command = cmFileFocused) then
  781.   begin
  782.     S := PSearchRec(Event.InfoPtr)^;
  783.     DrawView;
  784.   end;
  785. end;
  786.  
  787. { TFileDialog }
  788.  
  789. constructor TFileDialog.Init(AWildCard: TWildStr; ATitle: String;
  790.   InputName: String; AOptions: Word; HistoryId: Byte);
  791. var
  792.   Control: PView;
  793.   R: TRect;
  794.   S: String;
  795.   Opt: Word;
  796.   ACurDir: PathStr;
  797. begin
  798.   R.Assign(15,1,64,20);
  799.   TDialog.Init(R, ATitle);
  800.   Options := Options or ofCentered;
  801.   WildCard := AWildCard;
  802.  
  803.   R.Assign(3,3,31,4);
  804.   FileName := New(PFileInputLine, Init(R, 79));
  805.   FileName^.Data^ := WildCard;
  806.   Insert(FileName);
  807.   R.Assign(2,2,3+CStrLen(InputName),3);
  808.   Control := New(PLabel, Init(R, InputName, FileName));
  809.   Insert(Control);
  810.   R.Assign(31,3,34,4);
  811.   Control := New(PHistory, Init(R, FileName, HistoryId));
  812.   Insert(Control);
  813.  
  814.   R.Assign(3,14,34,15);
  815.   Control := New(PScrollBar, Init(R));
  816.   Insert(Control);
  817.   R.Assign(3,6,34,14);
  818.   FileList := New(PFileList, Init(R, WildCard, PScrollBar(Control)));
  819.   Insert(FileList);
  820.   R.Assign(2,5,8,6);
  821.   Control := New(PLabel, Init(R, '~F~iles', FileList));
  822.   Insert(Control);
  823.  
  824.   R.Assign(35,3,46,5);
  825.   Opt := bfDefault;
  826.   if AOptions and fdOpenButton <> 0 then
  827.   begin
  828.     Insert(New(PButton, Init(R, '~O~pen', cmFileOpen, Opt)));
  829.     Opt := bfNormal;
  830.     Inc(R.A.Y,3); Inc(R.B.Y,3);
  831.   end;
  832.   if AOptions and fdOkButton <> 0 then
  833.   begin
  834.     Insert(New(PButton, Init(R, 'O~K~', cmFileOpen, Opt)));
  835.     Opt := bfNormal;
  836.     Inc(R.A.Y,3); Inc(R.B.Y,3);
  837.   end;
  838.   if AOptions and fdReplaceButton <> 0 then
  839.   begin
  840.     Insert(New(PButton, Init(R, '~R~eplace',cmFileReplace, Opt)));
  841.     Opt := bfNormal;
  842.     Inc(R.A.Y,3); Inc(R.B.Y,3);
  843.   end;
  844.   if AOptions and fdClearButton <> 0 then
  845.   begin
  846.     Insert(New(PButton, Init(R, '~C~lear',cmFileClear, Opt)));
  847.     Opt := bfNormal;
  848.     Inc(R.A.Y,3); Inc(R.B.Y,3);
  849.   end;
  850.   Insert(New(PButton, Init(R, 'Cancel', cmCancel, bfNormal)));
  851.   Inc(R.A.Y,3); Inc(R.B.Y,3);
  852.   if AOptions and fdHelpButton <> 0 then
  853.   begin
  854.     Insert(New(PButton, Init(R, 'Help',cmHelp, bfNormal)));
  855.     Inc(R.A.Y,3); Inc(R.B.Y,3);
  856.   end;
  857.  
  858.   R.Assign(1,16,48,18);
  859.   Control := New(PFileInfoPane, Init(R));
  860.   Insert(Control);
  861.  
  862.   SelectNext(False);
  863.  
  864.   if AOptions and fdNoLoadDir = 0 then ReadDirectory;
  865. end;
  866.  
  867. constructor TFileDialog.Load(var S: TStream);
  868. var
  869.   ACurDir: DirStr;
  870.   ViewId: Word;
  871. begin
  872.   TDialog.Load(S);
  873.   S.Read(WildCard, SizeOf(TWildStr));
  874.   GetSubViewPtr(S, FileName);
  875.   GetSubViewPtr(S, FileList);
  876.  
  877.   ReadDirectory;
  878. end;
  879.  
  880. destructor TFileDialog.Done;
  881. begin
  882.   DisposeStr(Directory);
  883.   TDialog.Done;
  884. end;
  885.  
  886. procedure TFileDialog.GetData(var Rec);
  887. begin
  888.   GetFilename(PathStr(Rec));
  889. end;
  890.  
  891. procedure TFileDialog.GetFileName(var S: PathStr);
  892. var
  893.   Path: PathStr;
  894.   Name: NameStr;
  895.   Ext: ExtStr;
  896.   TPath: PathStr;
  897.   TName: NameStr;
  898.   TExt: NameStr;
  899.  
  900. function LTrim(S: String): String;
  901. var
  902.   I: Integer;
  903. begin
  904.   I := 1;
  905.   while (I < Length(S)) and (S[I] = ' ') do Inc(I);
  906.   LTrim := Copy(S, I, 255);
  907. end;
  908.  
  909. function RTrim(S: String): String;
  910. var
  911.   I: Integer;
  912. begin
  913.   while S[Length(S)] = ' ' do Dec(S[0]);
  914.   RTrim := S;
  915. end;
  916.  
  917. function RelativePath(var S: PathStr): Boolean;
  918. var
  919.   I,J: Integer;
  920.   P: PathStr;
  921. begin
  922.   S := LTrim(RTrim(S));
  923.   if (S <> '') and ((S[1] = '\') or (S[2] = ':')) then RelativePath := False
  924.   else RelativePath := True;
  925. end;
  926.  
  927. function NoWildChars(S: String): String; assembler;
  928. asm
  929.     PUSH    DS
  930.     LDS    SI,S
  931.         XOR     AX,AX
  932.     LODSB
  933.     XCHG    AX,CX
  934.         LES     DI,@Result
  935.         INC     DI
  936. @@1:    LODSB
  937.     CMP    AL,'?'
  938.     JE    @@2
  939.     CMP    AL,'*'
  940.     JE    @@2
  941.     STOSB
  942. @@2:    LOOP    @@1
  943.     XCHG    AX,DI
  944.     MOV    DI,WORD PTR @Result
  945.     SUB    AX,DI
  946.         DEC     AX
  947.         STOSB
  948.     POP    DS
  949. end;
  950.  
  951. begin
  952.   S := FileName^.Data^;
  953.   if RelativePath(S) then S := FExpand(Directory^ + S)
  954.   else S := FExpand(S);
  955.   FSplit(S, Path, Name, Ext);
  956.   if ((Name = '') or (Ext = '')) and not IsDir(S) then
  957.   begin
  958.     FSplit(WildCard, TPath, TName, TExt);
  959.     if ((Name = '') and (Ext = '')) then S := Path + TName + TExt
  960.     else if Name = '' then S := Path + TName + Ext
  961.     else if Ext = '' then
  962.     begin
  963.       if IsWild(Name) then S := Path + Name + TExt
  964.       else S := Path + Name + NoWildChars(TExt);
  965.     end;
  966.   end;
  967. end;
  968.  
  969. procedure TFileDialog.HandleEvent(var Event: TEvent);
  970. begin
  971.   TDialog.HandleEvent(Event);
  972.   if Event.What = evCommand then
  973.     case Event.Command of
  974.       cmFileOpen, cmFileReplace, cmFileClear:
  975.         begin
  976.           EndModal(Event.Command);
  977.           ClearEvent(Event);
  978.         end;
  979.     end;
  980. end;
  981.  
  982. procedure TFileDialog.SetData(var Rec);
  983. begin
  984.   TDialog.SetData(Rec);
  985.   if (PathStr(Rec) <> '') and (IsWild(TWildStr(Rec))) then
  986.   begin
  987.     Valid(cmFileInit);
  988.     FileName^.Select;
  989.   end;
  990. end;
  991.  
  992. procedure TFileDialog.ReadDirectory;
  993. begin
  994.   FileList^.ReadDirectory(WildCard);
  995.   Directory := NewStr(GetCurDir);
  996. end;
  997.  
  998. procedure TFileDialog.Store(var S: TStream);
  999. begin
  1000.   TDialog.Store(S);
  1001.   S.Write(WildCard, SizeOf(TWildStr));
  1002.   PutSubViewPtr(S, FileName);
  1003.   PutSubViewPtr(S, FileList);
  1004. end;
  1005.  
  1006. function TFileDialog.Valid(Command: Word): Boolean;
  1007. var
  1008.   T: Boolean;
  1009.   FName: PathStr;
  1010.   Dir: DirStr;
  1011.   Name: NameStr;
  1012.   Ext: ExtStr;
  1013.  
  1014. function CheckDirectory(var S: PathStr): Boolean;
  1015. begin
  1016.   if not PathValid(S) then
  1017.   begin
  1018.     MessageBox('Invalid drive or directory.', nil, mfError + mfOkButton);
  1019.     FileName^.Select;
  1020.     CheckDirectory := False;
  1021.   end else CheckDirectory := True;
  1022. end;
  1023.  
  1024. begin
  1025.   if Command = 0 then
  1026.   begin
  1027.     Valid := True;
  1028.     Exit;
  1029.   end else Valid := False;
  1030.   if TDialog.Valid(Command) then
  1031.   begin
  1032.     GetFileName(FName);
  1033.     if (Command <> cmCancel) and (Command <> cmFileClear) then
  1034.     begin
  1035.       if IsWild(FName) then
  1036.       begin
  1037.         FSplit(FName, Dir, Name, Ext);
  1038.         if CheckDirectory(Dir) then
  1039.         begin
  1040.           DisposeStr(Directory);
  1041.           Directory := NewStr(Dir);
  1042.           WildCard := Name+Ext;
  1043.           if Command <> cmFileInit then FileList^.Select;
  1044.           FileList^.ReadDirectory(Directory^+WildCard);
  1045.         end
  1046.       end
  1047.       else if IsDir(FName) then
  1048.       begin
  1049.         if CheckDirectory(FName) then
  1050.         begin
  1051.           DisposeStr(Directory);
  1052.       Directory := NewSTr(FName+'\');
  1053.       if Command <> cmFileInit then FileList^.Select;
  1054.       FileList^.ReadDirectory(Directory^+WildCard);
  1055.         end
  1056.       end else if ValidFileName(FName) then Valid := True
  1057.       else
  1058.       begin
  1059.         MessageBox('Invalid file name.', nil, mfError + mfOkButton);
  1060.         Valid := False;
  1061.       end
  1062.     end
  1063.     else Valid := True;
  1064.   end;
  1065. end;
  1066.  
  1067. { TDirCollection }
  1068.  
  1069. function TDirCollection.GetItem(var S: TStream): Pointer;
  1070. var
  1071.   DirItem: PDirEntry;
  1072. begin
  1073.   New(DirItem);
  1074.   DirItem^.DisplayText := S.ReadStr;
  1075.   DirItem^.Directory := S.ReadStr;
  1076.   GetItem := DirItem;
  1077. end;
  1078.  
  1079. procedure TDirCollection.FreeItem(Item: Pointer);
  1080. var
  1081.   DirItem: PDirEntry absolute Item;
  1082. begin
  1083.   DisposeStr(DirItem^.DisplayText);
  1084.   DisposeStr(DirItem^.Directory);
  1085.   Dispose(DirItem);
  1086. end;
  1087.  
  1088. procedure TDirCollection.PutItem(var S: TStream; Item: Pointer);
  1089. var
  1090.   DirItem: PDirEntry absolute Item;
  1091. begin
  1092.   S.WriteStr(DirItem^.DisplayText);
  1093.   S.WriteStr(DirItem^.Directory);
  1094. end;
  1095.  
  1096. { TDirListBox }
  1097.  
  1098. const
  1099.   DrivesS: String[6] = 'Drives';
  1100.   Drives: PString = @DrivesS;
  1101.  
  1102. constructor TDirListBox.Init(var Bounds: TRect; AScrollBar:
  1103.   PScrollBar);
  1104. begin
  1105.   TListBox.Init(Bounds, 1, AScrollBar);
  1106.   Dir := '';
  1107. end;
  1108.  
  1109. destructor TDirListBox.Done;
  1110. begin
  1111.   if List <> nil then Dispose(List, Done);
  1112.   TListBox.Done;
  1113. end;
  1114.  
  1115. function TDirListBox.GetText(Item: Integer; MaxLen: Integer): String;
  1116. begin
  1117.   GetText := PDirEntry(List^.At(Item))^.DisplayText^;
  1118. end;
  1119.  
  1120. procedure TDirListBox.HandleEvent(var Event: TEvent);
  1121. begin
  1122.   if (Event.What = evMouseDown) and (Event.Double) then
  1123.   begin
  1124.     Event.What := evCommand;
  1125.     Event.Command := cmChangeDir;
  1126.     PutEvent(Event);
  1127.     ClearEvent(Event);
  1128.   end
  1129.   else TListBox.HandleEvent(Event);
  1130. end;
  1131.  
  1132. function TDirListBox.IsSelected(Item: Integer): Boolean;
  1133. begin
  1134.   IsSelected := Item = Cur;
  1135. end;
  1136.  
  1137. procedure TDirListBox.NewDirectory(var ADir: DirStr);
  1138. const
  1139.   PathDir            = '└─┬';
  1140.   FirstDir           =   '└┬─';
  1141.   MiddleDir          =   ' ├─';
  1142.   LastDir            =   ' └─';
  1143.   IndentSize         = '  ';
  1144. var
  1145.   AList: PCollection;
  1146.   NewDir, Dirct: DirStr;
  1147.   C, OldC: Char;
  1148.   S, Indent: String[80];
  1149.   P: PString;
  1150.   isFirst: Boolean;
  1151.   SR: SearchRec;
  1152.   I: Integer;
  1153.   DirEntry: PDirEntry;
  1154.  
  1155. function NewDirEntry(DisplayText, Directory: String): PDirEntry; near;
  1156. var
  1157.   DirEntry: PDirEntry;
  1158. begin
  1159.   New(DirEntry);
  1160.   DirEntry^.DisplayText := NewStr(DisplayText);
  1161.   DirEntry^.Directory := NewStr(Directory);
  1162.   NewDirEntry := DirEntry;
  1163. end;
  1164.  
  1165. function GetCurDrive: Char; assembler;
  1166. asm
  1167.     MOV    AH,19H
  1168.         INT    21H
  1169.         ADD    AL,'A'
  1170. end;
  1171.  
  1172. begin
  1173.   Dir := ADir;
  1174.   AList := New(PDirCollection, Init(5,5));
  1175.   AList^.Insert(NewDirEntry(Drives^,Drives^));
  1176.   if Dir = Drives^ then
  1177.   begin
  1178.     isFirst := True;
  1179.     OldC := ' ';
  1180.     for C := 'A' to 'Z' do
  1181.     begin
  1182.       if (C < 'C') or DriveValid(C) then
  1183.       begin
  1184.         if OldC <> ' ' then
  1185.     begin
  1186.           if isFirst then
  1187.       begin
  1188.         S := FirstDir + OldC;
  1189.             isFirst := False;
  1190.           end
  1191.           else S := MiddleDir + OldC;
  1192.       AList^.Insert(NewDirEntry(S, OldC + ':\'));
  1193.         end;
  1194.         if C = GetCurDrive then Cur := AList^.Count;
  1195.         OldC := C;
  1196.       end;
  1197.     end;
  1198.     if OldC <> ' ' then AList^.Insert(NewDirEntry(LastDir + OldC, OldC + ':\'));
  1199.   end
  1200.   else
  1201.   begin
  1202.     Indent := IndentSize;
  1203.     NewDir := Dir;
  1204.     Dirct := Copy(NewDir,1,3);
  1205.     AList^.Insert(NewDirEntry(PathDir + Dirct, Dirct));
  1206.     NewDir := Copy(NewDir,4,255);
  1207.     while NewDir <> '' do
  1208.     begin
  1209.       I := Pos('\',NewDir);
  1210.       if I <> 0 then
  1211.       begin
  1212.         S := Copy(NewDir,1,I-1);
  1213.         Dirct := Dirct + S;
  1214.         AList^.Insert(NewDirEntry(Indent + PathDir + S, Dirct));
  1215.         NewDir := Copy(NewDir,I+1,255);
  1216.       end
  1217.       else
  1218.       begin
  1219.         Dirct := Dirct + NewDir;
  1220.         AList^.Insert(NewDirEntry(Indent + PathDir + NewDir, Dirct));
  1221.         NewDir := '';
  1222.       end;
  1223.       Indent := Indent + IndentSize;
  1224.       Dirct := Dirct + '\';
  1225.     end;
  1226.     Cur := AList^.Count-1;
  1227.     isFirst := True;
  1228.     NewDir := Dirct + '*.*';
  1229.     FindFirst(NewDir, Directory, SR);
  1230.     while DosError = 0 do
  1231.     begin
  1232.       if (SR.Attr and Directory <> 0) and (SR.Name[1] <> '.') then
  1233.       begin
  1234.         if isFirst then
  1235.     begin
  1236.       S := FirstDir;
  1237.       isFirst := False;
  1238.     end else S := MiddleDir;
  1239.         AList^.Insert(NewDirEntry(Indent + S + SR.Name, Dirct + SR.Name));
  1240.       end;
  1241.       FindNext(SR);
  1242.     end;
  1243.     P := PDirEntry(AList^.At(AList^.Count-1))^.DisplayText;
  1244.     I := Pos('└',P^);
  1245.     if I = 0 then
  1246.     begin
  1247.       I := Pos('├',P^);
  1248.       if I <> 0 then P^[I] := '└';
  1249.     end else
  1250.     begin
  1251.       P^[I+1] := '─';
  1252.       P^[I+2] := '─';
  1253.     end;
  1254.   end;
  1255.   NewList(AList);
  1256.   FocusItem(Cur);
  1257. end;
  1258.  
  1259. procedure TDirListBox.SetState(AState: Word; Enable: Boolean);
  1260. begin
  1261.   TListBox.SetState(AState, Enable);
  1262.   if AState and sfFocused <> 0 then
  1263.     PChDirDialog(Owner)^.ChDirButton^.MakeDefault(Enable);
  1264. end;
  1265.  
  1266. { TChDirDialog }
  1267.  
  1268. constructor TChDirDialog.Init(AOptions: Word; HistoryId: Word);
  1269. var
  1270.   R: TRect;
  1271.   Control: PView;
  1272.   CurDir: DirStr;
  1273. begin
  1274.   R.Assign(16, 2, 64, 20);
  1275.   TDialog.Init(R, 'Change Directory');
  1276.  
  1277.   Options := Options or ofCentered;
  1278.  
  1279.   R.Assign(3, 3, 30, 4);
  1280.   DirInput := New(PInputLine, Init(R, 68));
  1281.   Insert(DirInput);
  1282.   R.Assign(2, 2, 17, 3);
  1283.   Control := New(PLabel, Init(R, 'Directory ~n~ame', DirInput));
  1284.   Insert(Control);
  1285.   R.Assign(30, 3, 33, 4);
  1286.   Control := New(PHistory, Init(R, DirInput, HistoryId));
  1287.   Insert(Control);
  1288.  
  1289.   R.Assign(32, 6, 33, 16);
  1290.   Control := New(PScrollBar, Init(R));
  1291.   Insert(Control);
  1292.   R.Assign(3, 6, 32, 16);
  1293.   DirList := New(PDirListBox, Init(R, PScrollBar(Control)));
  1294.   Insert(DirList);
  1295.   R.Assign(2, 5, 17, 6);
  1296.   Control := New(PLabel, Init(R, 'Directory ~t~ree', DirList));
  1297.   Insert(Control);
  1298.  
  1299.   R.Assign(35, 6, 45, 8);
  1300.   OkButton := New(PButton, Init(R, 'O~K~', cmOK, bfDefault));
  1301.   Insert(OkButton);
  1302.   Inc(R.A.Y,3); Inc(R.B.Y,3);
  1303.   ChDirButton := New(PButton, Init(R, '~C~hdir', cmChangeDir, bfNormal));
  1304.   Insert(ChDirButton);
  1305.   Inc(R.A.Y,3); Inc(R.B.Y,3);
  1306.   Insert(New(PButton, Init(R, '~R~evert', cmRevert, bfNormal)));
  1307.   if AOptions and cdHelpButton <> 0 then
  1308.   begin
  1309.     Inc(R.A.Y,3); Inc(R.B.Y,3);
  1310.     Insert(New(PButton, Init(R, 'Help', cmHelp, bfNormal)));
  1311.   end;
  1312.  
  1313.   if AOptions and cdNoLoadDir = 0 then SetUpDialog;
  1314.  
  1315.   SelectNext(False);
  1316. end;
  1317.  
  1318. constructor TChDirDialog.Load(var S: TStream);
  1319. var
  1320.   CurDir: DirStr;
  1321. begin
  1322.   TDialog.Load(S);
  1323.   GetSubViewPtr(S, DirList);
  1324.   GetSubViewPtr(S, DirInput);
  1325.   GetSubViewPtr(S, OkButton);
  1326.   GetSubViewPtr(S, ChDirbutton);
  1327.   SetUpDialog;
  1328. end;
  1329.  
  1330. function TChDirDialog.DataSize: Word;
  1331. begin
  1332.   DataSize := 0;
  1333. end;
  1334.  
  1335. procedure TChDirDialog.GetData(var Rec);
  1336. begin
  1337. end;
  1338.  
  1339. procedure TChDirDialog.HandleEvent(var Event: TEvent); 
  1340. var
  1341.   CurDir: DirStr;
  1342.   P: PDirEntry;
  1343. begin
  1344.   TDialog.HandleEvent(Event);
  1345.   case Event.What of
  1346.     evCommand:
  1347.       begin
  1348.         case Event.Command of
  1349.           cmRevert: GetDir(0,CurDir);
  1350.           cmChangeDir:
  1351.             begin
  1352.               P := DirList^.List^.At(DirList^.Focused);
  1353.               if (P^.Directory^ = Drives^) or DriveValid(P^.Directory^[1]) then
  1354.                 CurDir := P^.Directory^
  1355.               else Exit;
  1356.             end;
  1357.         else
  1358.           Exit;
  1359.         end;
  1360.         if (Length(CurDir) > 3) and (CurDir[Length(CurDir)] = '\') then
  1361.           CurDir := Copy(CurDir,1,Length(CurDir)-1);
  1362.         DirList^.NewDirectory(CurDir);
  1363.         DirInput^.Data^ := CurDir;
  1364.         DirInput^.DrawView;
  1365.         DirList^.Select;
  1366.         ClearEvent(Event);
  1367.       end;
  1368.   end;
  1369. end;
  1370.  
  1371. procedure TChDirDialog.SetData(var Rec);
  1372. begin
  1373. end;
  1374.  
  1375. procedure TChDirDialog.SetUpDialog;
  1376. var
  1377.   CurDir: DirStr;
  1378. begin
  1379.   if DirList <> nil then
  1380.   begin
  1381.     CurDir := GetCurDir;
  1382.     DirList^.NewDirectory(CurDir);
  1383.     if (Length(CurDir) > 3) and (CurDir[Length(CurDir)] = '\') then
  1384.       CurDir := Copy(CurDir,1,Length(CurDir)-1);
  1385.     if DirInput <> nil then
  1386.     begin
  1387.       DirInput^.Data^ := CurDir;
  1388.       DirInput^.DrawView;
  1389.     end;
  1390.   end;
  1391. end;
  1392.  
  1393. procedure TChDirDialog.Store(var S: TStream);
  1394. begin
  1395.   TDialog.Store(S);
  1396.   PutSubViewPtr(S, DirList);
  1397.   PutSubViewPtr(S, DirInput);
  1398.   PutSubViewPtr(S, OkButton);
  1399.   PutSubViewPtr(S, ChDirButton);
  1400. end;
  1401.  
  1402. function TChDirDialog.Valid(Command: Word): Boolean;
  1403. var
  1404.   P: PathStr;
  1405. begin
  1406.   Valid := True;
  1407.   if Command = cmOk then
  1408.   begin
  1409.     P := FExpand(DirInput^.Data^);
  1410.     if (Length(P) > 3) and (P[Length(P)] = '\') then Dec(P[0]);
  1411.     {$I-}
  1412.     ChDir(P);
  1413.     if IOResult <> 0 then
  1414.     begin
  1415.       MessageBox('Invalid directory.', nil, mfError + mfOkButton);
  1416.       Valid := False;
  1417.     end;
  1418.     {$I+}
  1419.   end;
  1420. end;
  1421.  
  1422. procedure RegisterStdDlg;
  1423. begin
  1424.   RegisterType(RFileInputLine);
  1425.   RegisterType(RFileCollection);
  1426.   RegisterType(RFileList);
  1427.   RegisterType(RFileInfoPane);
  1428.   RegisterType(RFileDialog);
  1429.   RegisterType(RDirCollection);
  1430.   RegisterType(RDirListBox);
  1431.   RegisterType(RChDirDialog);
  1432. end;
  1433.  
  1434. end.
  1435.