source: trunk/Components/DirectoryEdit.PAS@ 39

Last change on this file since 39 was 15, checked in by RBRi, 19 years ago

+ components stuff

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1Unit DirectoryEdit;
2// An edit box that will act as a directory
3// changer, with tab completion and enter
4// selecting a dir.
5Interface
6
7Uses
8 Classes, Forms, StdCtrls, SysUtils;
9
10Type
11 TNotifyDirChange = procedure( NewDir: string ) of object;
12
13 TDirectoryEdit=Class(TEdit)
14 Protected
15 FDirectory: string;
16 FCompletionBasePath: string;
17 FCompletionIndex: longint;
18 FCompletionStart: string;
19 FOnChangeDir: TNotifyDirChange;
20 FCompletions: TStringList;
21 Procedure SetupComponent; Override;
22 Procedure ScanEvent( Var KeyCode: TKeyCode;
23 RepeatCount: Byte ); override;
24 Procedure CharEvent( Var Key: Char;
25 RepeatCount: Byte ); override;
26 procedure ReadCompletions;
27 Public
28 Destructor Destroy; Override;
29 Published
30 property OnChangeDirectory: TNotifyDirChange read FOnChangeDir write FOnChangeDir;
31 procedure SetDirectory( const Directory: string );
32 End;
33
34Exports
35 TDirectoryEdit,'User','diredit.bmp';
36
37Implementation
38
39Uses
40 PMWIN, BseDos, ACLFileUtility, ACLFindFunctions;
41
42Procedure TDirectoryEdit.SetupComponent;
43Begin
44 Inherited SetupComponent;
45 Name:= 'DirectoryEdit';
46 FCompletionIndex:= 0;
47 FCompletions:= TStringList.Create;
48End;
49
50Destructor TDirectoryEdit.Destroy;
51Begin
52 Inherited Destroy;
53End;
54
55procedure TDirectoryEdit.SetDirectory( const Directory: string );
56begin
57 FDirectory := Directory;
58 Text := Directory;
59
60 // move cursor to end
61 SelStart := Length( Directory );
62end;
63
64procedure TDirectoryEdit.ReadCompletions;
65Var
66 SearchResults: TSearchData;
67 rc:integer;
68 Drive: string;
69Begin
70 FCompletions.Clear;
71 Drive := ExtractFileDrive( FCompletionBasePath );
72 if Length( Drive ) < 2 then
73 // must have a drive spec
74 exit;
75
76 rc:= MyFindFirst( FCompletionBasePath
77 + FCompletionStart
78 + '*',
79 SearchResults );
80 while rc = 0 do
81 begin
82 if ( SearchResults.Attr and faDirectory ) > 0 then
83 if ( SearchResults.Name <> '.' )
84 and ( SearchResults.Name <> '..' ) then
85 FCompletions.Add( SearchResults.Name );
86 rc:= MyFindNext( SearchResults );
87 end;
88 MyFindClose( SearchResults );
89 FCompletions.Sort;
90End;
91
92Procedure TDirectoryEdit.ScanEvent( Var KeyCode: TKeyCode;
93 RepeatCount: Byte );
94Var
95 Entry: string;
96 rc: longint;
97 Dir: string;
98 NewDirectory: string;
99Begin
100 if KeyCode = kbTab then
101 begin
102 KeyCode:= kbNull;
103 // want to use tab for completion
104 if FCompletions.Count = 0 then
105 begin
106 // First time, read possible completions
107 FCompletionStart := ExtractFileName( Text );
108 FCompletionBasePath:= ExpandPath( FDirectory,
109 ExtractFilePath( Text ) );
110 ReadCompletions;
111 end;
112
113 if FCompletionIndex < FCompletions.Count then
114 begin
115 // Display next completion
116 Text:= FCompletionBasePath + FCompletions[ FCompletionIndex ];
117 // Place cursor at end
118 SelStart := Length( Text );
119 inc( FCompletionIndex );
120 end
121 else
122 begin
123 // nothing more
124 Beep( 1000, 50 );
125 end;
126 exit;
127 end;
128
129 // not a tab
130 FCompletionIndex:= 0;
131 FCompletions.Clear;
132
133 if KeyCode = kb_VK + VK_NEWLINE then
134 begin
135 KeyCode:= kbNull;
136 // enter key pressed - change dir
137 NewDirectory := ExpandPath( FDirectory,
138 Text );
139 if DirectoryExists( NewDirectory ) then
140 begin
141 SetDirectory( NewDirectory );
142 if Assigned( FOnChangeDir ) then
143 FOnChangeDir( NewDirectory )
144 end
145 else
146 begin
147 Beep( 1000, 50 );
148 end;
149 exit;
150 end;
151
152 if KeyCode = kbCtrlTab then
153 begin
154 // fake a normal focus shift
155 KeyCode:= kbTab;
156 Parent.ScanEvent( KeyCode, 1 );
157 KeyCode:= kbNull;
158 end;
159
160End;
161
162Procedure TDirectoryEdit.CharEvent( Var Key: Char;
163 RepeatCount: Byte );
164Begin
165 FCompletions.Clear;
166 FCompletionIndex:= 0;
167End;
168
169Initialization
170 {Register classes}
171 RegisterClasses([TDirectoryEdit]);
172End.
173
Note: See TracBrowser for help on using the repository browser.