Debian Codegraph @

components/KASToolBar/kaspathedit.pas

raw
1 {
2 Double Commander Components
3 -------------------------------------------------------------------------
4 Path edit class with auto complete feature
5
6 Copyright (C) 2012-2022 Alexander Koblov ([email protected])
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 }
21
22 unit KASPathEdit;
23
24 {$mode delphi}
25 {$IF DEFINED(LCLCOCOA)}
26 {$modeswitch objectivec1}
27 {$ENDIF}
28
29 interface
30
31 uses
32 Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
33 ShellCtrls, LCLType, LCLVersion
34 {$IF DEFINED(LCLCOCOA)}
35 , CocoaAll, CocoaWindows
36 {$ENDIF}
37 ;
38
39 type
40
41 { TKASPathEdit }
42
43 TKASPathEdit = class(TEdit)
44 private
45 FKeyDown: Word;
46 FBasePath: String;
47 FListBox: TListBox;
48 FPanel: THintWindow;
49 FAutoComplete: Boolean;
50 FStringList: TStringList;
51 FObjectTypes: TObjectTypes;
52 FFileSortType: TFileSortType;
53 private
54 procedure setTextAndSelect( newText:String );
55 procedure handleSpecialKeys( var Key: Word );
56 procedure handleUpKey;
57 procedure handleDownKey;
58 procedure AutoComplete(const Path: String);
59 procedure SetObjectTypes(const AValue: TObjectTypes);
60 procedure FormChangeBoundsEvent(Sender: TObject);
61 procedure ListBoxClick(Sender: TObject);
62 procedure ListBoxMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
63 private
64 function isShowingListBox(): Boolean; inline;
65 procedure ShowListBox;
66 procedure HideListBox;
67 protected
68 {$IF DEFINED(LCLWIN32)}
69 procedure CreateWnd; override;
70 {$ENDIF}
71 {$IF DEFINED(LCLCOCOA)}
72 procedure TextChanged; override;
73 {$ENDIF}
74 procedure DoExit; override;
75 procedure VisibleChanged; override;
76 procedure KeyDown(var Key: Word; Shift: TShiftState); override;
77 procedure KeyUpAfterInterface(var Key: Word; Shift: TShiftState); override;
78 public
79 onKeyESCAPE: TNotifyEvent;
80 onKeyRETURN: TNotifyEvent;
81 constructor Create(AOwner: TComponent); override;
82 destructor Destroy; override;
83 published
84 property ObjectTypes: TObjectTypes read FObjectTypes write SetObjectTypes;
85 property FileSortType: TFileSortType read FFileSortType write FFileSortType;
86 end;
87
88 procedure Register;
89
90 implementation
91
92 uses
93 LazUTF8, Math, LazFileUtils, Masks
94 {$IF DEFINED(LCLWIN32)}
95 , ComObj
96 {$ENDIF}
97 {$IF DEFINED(MSWINDOWS)}
98 , Windows
99 {$ENDIF}
100 ;
101
102 {$IF DEFINED(LCLWIN32)}
103
104 const
105 SHACF_AUTOAPPEND_FORCE_ON = $40000000;
106 SHACF_AUTOSUGGEST_FORCE_ON = $10000000;
107 SHACF_FILESYS_ONLY = $00000010;
108 SHACF_FILESYS_DIRS = $00000020;
109
110 function SHAutoComplete(hwndEdit: HWND; dwFlags: DWORD): HRESULT; stdcall; external 'shlwapi.dll';
111
112 function SHAutoCompleteX(hwndEdit: HWND; ObjectTypes: TObjectTypes): Boolean;
113 var
114 dwFlags: DWORD;
115 begin
116 if (ObjectTypes = []) then Exit(False);
117 dwFlags := SHACF_AUTOAPPEND_FORCE_ON or SHACF_AUTOSUGGEST_FORCE_ON;
118 if (otNonFolders in ObjectTypes) then
119 dwFlags := dwFlags or SHACF_FILESYS_ONLY
120 else if (otFolders in ObjectTypes) then
121 dwFlags := dwFlags or SHACF_FILESYS_DIRS;
122 Result:= (SHAutoComplete(hwndEdit, dwFlags) = 0);
123 end;
124
125 {$ENDIF}
126
127 procedure Register;
128 begin
129 RegisterComponents('KASComponents', [TKASPathEdit]);
130 end;
131
132 function FilesSortAlphabet(List: TStringList; Index1, Index2: Integer): Integer;
133 begin
134 Result:= CompareFilenames(List[Index1], List[Index2]);
135 end;
136
137 function FilesSortFoldersFirst(List: TStringList; Index1, Index2: Integer): Integer;
138 var
139 Attr1, Attr2: IntPtr;
140 begin
141 Attr1:= IntPtr(List.Objects[Index1]);
142 Attr2:= IntPtr(List.Objects[Index2]);
143 if (Attr1 and faDirectory <> 0) and (Attr2 and faDirectory <> 0) then
144 Result:= CompareFilenames(List[Index1], List[Index2])
145 else begin
146 if (Attr1 and faDirectory <> 0) then
147 Result:= -1
148 else begin
149 Result:= 1;
150 end;
151 end;
152 end;
153
154 procedure GetFilesInDir(const ABaseDir: String; AMask: String; AObjectTypes: TObjectTypes;
155 AResult: TStringList; AFileSortType: TFileSortType);
156 var
157 ExcludeAttr: Integer;
158 SearchRec: TSearchRec;
159 {$IF DEFINED(MSWINDOWS)}
160 ErrMode : LongWord;
161 {$ENDIF}
162 begin
163 {$IF DEFINED(MSWINDOWS)}
164 ErrMode:= SetErrorMode(SEM_FAILCRITICALERRORS or SEM_NOALIGNMENTFAULTEXCEPT or SEM_NOGPFAULTERRORBOX or SEM_NOOPENFILEERRORBOX);
165 try
166 {$ENDIF}
167 if FindFirst(ABaseDir + AMask, faAnyFile, SearchRec) = 0 then
168 begin
169 ExcludeAttr:= 0;
170
171 if not (otHidden in AObjectTypes) then
172 ExcludeAttr:= ExcludeAttr or faHidden;
173 if not (otFolders in AObjectTypes) then
174 ExcludeAttr:= ExcludeAttr or faDirectory;
175
176 repeat
177 if (SearchRec.Attr and ExcludeAttr <> 0) then
178 Continue;
179 if (SearchRec.Name = '.') or (SearchRec.Name = '..')then
180 Continue;
181 if (SearchRec.Attr and faDirectory = 0) and not (otNonFolders in AObjectTypes) then
182 Continue;
183
184 AResult.AddObject(SearchRec.Name, TObject(IntPtr(SearchRec.Attr)));
185 until FindNext(SearchRec) <> 0;
186
187 if AResult.Count > 0 then
188 begin
189 case AFileSortType of
190 fstAlphabet: AResult.CustomSort(@FilesSortAlphabet);
191 fstFoldersFirst: AResult.CustomSort(@FilesSortFoldersFirst);
192 end;
193 end;
194 end;
195 SysUtils.FindClose(SearchRec);
196 {$IF DEFINED(MSWINDOWS)}
197 finally
198 SetErrorMode(ErrMode);
199 end;
200 {$ENDIF}
201 end;
202
203 { TKASPathEdit }
204
205 function TKASPathEdit.isShowingListBox(): Boolean;
206 begin
207 Result:= FPanel<>nil;
208 end;
209
210 procedure TKASPathEdit.AutoComplete(const Path: String);
211 {$IF LCL_FULLVERSION < 4990000}
212 const
213 AFlags: array[Boolean] of TMaskOptions = (
214 [moDisableSets], [moDisableSets, moCaseSensitive]
215 );
216 {$ENDIF}
217 var
218 I: Integer;
219 AMask: TMask;
220 BasePath: String;
221 begin
222 FListBox.Clear;
223 if Pos(PathDelim, Path) = 0 then
224 HideListBox
225 else begin
226 BasePath:= ExtractFilePath(Path);
227 if CompareFilenames(FBasePath, BasePath) <> 0 then
228 begin
229 FStringList.Clear;
230 FBasePath:= BasePath;
231 GetFilesInDir(BasePath, AllFilesMask, FObjectTypes, FStringList, FFileSortType);
232 end;
233 if (FStringList.Count > 0) then
234 begin
235 FListBox.Items.BeginUpdate;
236 try
237 // Check mask and make absolute file name
238 AMask:= TMask.Create(ExtractFileName(Path) + '*',
239 {$IF LCL_FULLVERSION < 4990000}
240 AFlags[FileNameCaseSensitive]
241 {$ELSE}
242 FileNameCaseSensitive
243 {$ENDIF}
244 );
245 for I:= 0 to FStringList.Count - 1 do
246 begin
247 if AMask.Matches(FStringList[I]) then
248 FListBox.Items.Add(BasePath + FStringList[I]);
249 end;
250 AMask.Free;
251 finally
252 FListBox.Items.EndUpdate;
253 end;
254 if FListBox.Items.Count = 0 then HideListBox;
255 if FListBox.Items.Count > 0 then
256 begin
257 ShowListBox;
258 // Calculate ListBox height
259 with FListBox.ItemRect(0) do
260 I:= Bottom - Top; // TListBox.ItemHeight sometimes don't work under GTK2
261 with FListBox do
262 begin
263 {$IF NOT DEFINED(LCLCOCOA)}
264 if Items.Count = 1 then
265 FPanel.ClientHeight:= Self.Height
266 else
267 FPanel.ClientHeight:= I * IfThen(Items.Count > 10, 11, Items.Count + 1);
268 {$ELSE}
269 FPanel.ClientHeight:= I * IfThen(Items.Count > 10, 11, Items.Count + 1) + trunc(i/2);
270 {$ENDIF}
271 end;
272 end;
273 end;
274 end;
275 end;
276
277 procedure TKASPathEdit.SetObjectTypes(const AValue: TObjectTypes);
278 begin
279 if FObjectTypes = AValue then Exit;
280 FObjectTypes:= AValue;
281 {$IF DEFINED(LCLWIN32)}
282 if HandleAllocated then RecreateWnd(Self);
283 if FAutoComplete then
284 {$ENDIF}
285 FAutoComplete:= (FObjectTypes <> []);
286 end;
287
288 procedure TKASPathEdit.FormChangeBoundsEvent(Sender: TObject);
289 begin
290 HideListBox;
291 end;
292
293 procedure TKASPathEdit.ListBoxClick(Sender: TObject);
294 begin
295 if FListBox.ItemIndex >= 0 then
296 begin
297 setTextAndSelect( FListBox.Items[FListBox.ItemIndex] );
298 HideListBox;
299 SetFocus;
300 end;
301 end;
302
303 procedure TKASPathEdit.ListBoxMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
304 begin
305 FListBox.ItemIndex:= FListBox.ItemAtPos(Classes.Point(X, Y), True);
306 end;
307
308 {$IF DEFINED(LCLCOCOA)}
309 procedure cocoaNeedMouseEvent( hintWindow: THintWindow );
310 var
311 cnt: TCocoaWindowContent;
312 begin
313 cnt:= TCocoaWindowContent( hintWindow.Handle );
314 cnt.window.setIgnoresMouseEvents( false );
315 end;
316 {$ENDIF}
317
318 procedure TKASPathEdit.ShowListBox;
319 begin
320 if not isShowingListBox() then
321 begin
322 FPanel:= THintWindow.Create(Self);
323 {$IF DEFINED(LCLCOCOA)}
324 cocoaNeedMouseEvent(FPanel);
325 {$ENDIF}
326 FPanel.Color:= clDefault;
327 FListBox.Parent:= FPanel;
328
329 with Parent.ClientToScreen(CLasses.Point(Left, Top)) do
330 begin
331 FPanel.Left:= X;
332 FPanel.Top:= Y + Height;
333 end;
334
335 FPanel.Width:= Width;
336 FPanel.Visible:= True;
337
338 Application.AddOnDeactivateHandler(FormChangeBoundsEvent, True);
339 GetParentForm(Self).AddHandlerOnChangeBounds(FormChangeBoundsEvent, True);
340 end;
341 end;
342
343 procedure TKASPathEdit.HideListBox;
344 begin
345 if isShowingListBox() then
346 begin
347 FPanel.Visible:= False;
348 FListBox.Parent:= nil;
349 FreeAndNil(FPanel);
350 Application.RemoveOnDeactivateHandler(FormChangeBoundsEvent);
351 GetParentForm(Self).RemoveHandlerOnChangeBounds(FormChangeBoundsEvent);
352 end;
353 end;
354
355 {$IF DEFINED(LCLWIN32)}
356
357 procedure TKASPathEdit.CreateWnd;
358 begin
359 inherited CreateWnd;
360 FAutoComplete:= not SHAutoCompleteX(Handle, FObjectTypes);
361 end;
362
363 {$ENDIF}
364
365
366 {$IF DEFINED(LCLCOCOA)}
367 procedure TKASPathEdit.TextChanged;
368 begin
369 Inherited;
370 if not Modified then
371 Exit;
372 if FAutoComplete then
373 AutoComplete(Text);
374 end;
375 {$ENDIF}
376
377
378 procedure TKASPathEdit.setTextAndSelect( newText:String );
379 var
380 start: Integer;
381 begin
382 if Pos(Text,newText) > 0 then
383 start:= UTF8Length(Text)
384 else
385 start:= UTF8Length(ExtractFilePath(Text));
386 Text:= newText;
387 SelStart:= start;
388 SelLength:= UTF8Length(Text)-SelStart;
389 end;
390
391 procedure TKASPathEdit.DoExit;
392 begin
393 HideListBox;
394 inherited DoExit;
395 end;
396
397 procedure TKASPathEdit.VisibleChanged;
398 begin
399 FBasePath:= EmptyStr;
400 inherited VisibleChanged;
401 end;
402
403 procedure TKASPathEdit.handleSpecialKeys( var Key: Word );
404 begin
405 if isShowingListBox() then begin
406 HideListBox;
407 Key:= 0;
408 end else begin
409 if Key=VK_ESCAPE then begin
410 if Assigned(onKeyESCAPE) then begin
411 onKeyESCAPE( self );
412 Key:= 0;
413 end;
414 end else begin
415 if Assigned(onKeyRETURN) then begin
416 onKeyRETURN( self );
417 Key:= 0;
418 end;
419 end;
420 end;
421 end;
422
423 procedure TKASPathEdit.handleUpKey;
424 begin
425 if FListBox.ItemIndex = -1 then
426 FListBox.ItemIndex:= FListBox.Items.Count - 1
427 else if FListBox.ItemIndex - 1 < 0 then
428 FListBox.ItemIndex:= - 1
429 else
430 FListBox.ItemIndex:= FListBox.ItemIndex - 1;
431
432 if FListBox.ItemIndex >= 0 then
433 setTextAndSelect( FListBox.Items[FListBox.ItemIndex] )
434 else
435 setTextAndSelect( ExtractFilePath(Text) );
436 end;
437
438 procedure TKASPathEdit.handleDownKey;
439 begin
440 if FListBox.ItemIndex + 1 >= FListBox.Items.Count then
441 FListBox.ItemIndex:= -1
442 else if FListBox.ItemIndex = -1 then
443 FListBox.ItemIndex:= IfThen(FListBox.Items.Count > 0, 0, -1)
444 else
445 FListBox.ItemIndex:= FListBox.ItemIndex + 1;
446
447 if FListBox.ItemIndex >= 0 then
448 setTextAndSelect( FListBox.Items[FListBox.ItemIndex] )
449 else
450 setTextAndSelect( ExtractFilePath(Text) );
451 end;
452
453 procedure TKASPathEdit.KeyDown(var Key: Word; Shift: TShiftState);
454 begin
455 FKeyDown:= Key;
456 case Key of
457 VK_ESCAPE,
458 VK_RETURN,
459 VK_SELECT:
460 handleSpecialKeys( Key );
461 VK_UP:
462 if isShowingListBox() then
463 begin
464 Key:= 0;
465 handleUpKey();
466 end;
467 VK_DOWN:
468 if isShowingListBox() then
469 begin
470 Key:= 0;
471 handleDownKey();
472 end;
473 end;
474
475 inherited KeyDown(Key, Shift);
476 {$IFDEF LCLGTK2}
477 // Workaround for GTK2 - up and down arrows moving through controls.
478 if Key in [VK_UP, VK_DOWN] then Key:= 0;
479 {$ENDIF}
480 end;
481
482 procedure TKASPathEdit.KeyUpAfterInterface(var Key: Word; Shift: TShiftState);
483 begin
484 {$IF not DEFINED(LCLCOCOA)}
485 if (FKeyDown = Key) and FAutoComplete and not (Key in [VK_ESCAPE, VK_RETURN, VK_SELECT, VK_UP, VK_DOWN]) then
486 begin
487 if Modified then
488 begin
489 Modified:= False;
490 AutoComplete(Text);
491 end;
492 end;
493 {$ENDIF}
494 inherited KeyUpAfterInterface(Key, Shift);
495 {$IF DEFINED(LCLWIN32)}
496 // Windows auto-completer eats the TAB so LCL doesn't get it and doesn't move to next control.
497 if not FAutoComplete and (Key = VK_TAB) then
498 GetParentForm(Self).SelectNext(Self, True, True);
499 {$ENDIF}
500 end;
501
502 constructor TKASPathEdit.Create(AOwner: TComponent);
503 begin
504 inherited Create(AOwner);
505
506 FStringList:= TStringList.Create;
507
508 FListBox:= TListBox.Create(Self);
509 FListBox.TabStop:= False;
510 FListBox.Align:= alClient;
511 FListBox.ParentFont:= False;
512 FListBox.ClickOnSelChange:= False;
513 FListBox.OnClick:= ListBoxClick;
514 FListBox.OnMouseMove:= ListBoxMouseMove;
515
516 FAutoComplete:= True;
517 FFileSortType:= fstFoldersFirst;
518 FObjectTypes:= [otNonFolders, otFolders];
519 end;
520
521 destructor TKASPathEdit.Destroy;
522 begin
523 inherited Destroy;
524 FStringList.Free;
525 end;
526
527 end.