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
|
{
Double Commander Components
-------------------------------------------------------------------------
Path edit class with auto complete feature
Copyright (C) 2012-2014 Alexander Koblov (alexx2000@mail.ru)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
in a file called COPYING along with this program; if not, write to
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
02139, USA.
}
unit KASPathEdit;
{$mode delphi}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
ShellCtrls, LCLType;
type
{ TKASPathEdit }
TKASPathEdit = class(TEdit)
private
FPanel: THintWindow;
FListBox: TListBox;
FKeyDown: Word;
FAutoComplete: Boolean;
FObjectTypes: TObjectTypes;
FFileSortType: TFileSortType;
private
procedure AutoComplete(const Path: String);
procedure SetObjectTypes(const AValue: TObjectTypes);
procedure FormChangeBoundsEvent(Sender: TObject);
procedure ListBoxClick(Sender: TObject);
procedure ListBoxMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
private
procedure ShowListBox;
procedure HideListBox;
protected
{$IF DEFINED(LCLWIN32)}
procedure CreateWnd; override;
{$ENDIF}
procedure DoExit; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyUpAfterInterface(var Key: Word; Shift: TShiftState); override;
public
constructor Create(AOwner: TComponent); override;
published
property ObjectTypes: TObjectTypes read FObjectTypes write SetObjectTypes;
property FileSortType: TFileSortType read FFileSortType write FFileSortType;
end;
procedure Register;
implementation
uses
LazUTF8, Math
{$IF DEFINED(LCLWIN32)}
, ComObj
{$ENDIF}
;
{$IF DEFINED(LCLWIN32)}
const
SHACF_AUTOAPPEND_FORCE_ON = $40000000;
SHACF_AUTOSUGGEST_FORCE_ON = $10000000;
SHACF_FILESYS_ONLY = $00000010;
SHACF_FILESYS_DIRS = $00000020;
function SHAutoComplete(hwndEdit: HWND; dwFlags: DWORD): HRESULT; stdcall; external 'shlwapi.dll';
function SHAutoCompleteX(hwndEdit: HWND; ObjectTypes: TObjectTypes): Boolean;
var
dwFlags: DWORD;
begin
if (ObjectTypes = []) then Exit(False);
dwFlags := SHACF_AUTOAPPEND_FORCE_ON or SHACF_AUTOSUGGEST_FORCE_ON;
if (otNonFolders in ObjectTypes) then
dwFlags := dwFlags or SHACF_FILESYS_ONLY
else if (otFolders in ObjectTypes) then
dwFlags := dwFlags or SHACF_FILESYS_DIRS;
Result:= (SHAutoComplete(hwndEdit, dwFlags) = 0);
end;
{$ENDIF}
procedure Register;
begin
RegisterComponents('KASComponents', [TKASPathEdit]);
end;
{ TKASPathEdit }
procedure TKASPathEdit.AutoComplete(const Path: String);
var
I: LongWord;
BasePath: String;
begin
FListBox.Clear;
if Pos(PathDelim, Path) > 0 then
begin
BasePath:= ExtractFilePath(Path);
TCustomShellTreeView.GetFilesInDir(
BasePath,
ExtractFileName(Path) + '*',
FObjectTypes,
FListBox.Items,
FFileSortType
);
if (FListBox.Items.Count > 0) then
begin
ShowListBox;
// Make absolute file name
for I:= 0 to FListBox.Items.Count - 1 do
FListBox.Items[I]:= BasePath + FListBox.Items[I];
// Calculate ListBox height
with FListBox.ItemRect(0) do
I:= Bottom - Top; // TListBox.ItemHeight sometimes don't work under GTK2
with FListBox do
begin
if Items.Count = 1 then
FPanel.ClientHeight:= Self.Height
else
FPanel.ClientHeight:= I * IfThen(Items.Count > 10, 11, Items.Count + 1);
end;
end;
end;
if (FListBox.Items.Count = 0) then HideListBox;
end;
procedure TKASPathEdit.SetObjectTypes(const AValue: TObjectTypes);
begin
if FObjectTypes = AValue then Exit;
FObjectTypes:= AValue;
{$IF DEFINED(LCLWIN32)}
if HandleAllocated then RecreateWnd(Self);
if FAutoComplete then
{$ENDIF}
FAutoComplete:= (FObjectTypes <> []);
end;
procedure TKASPathEdit.FormChangeBoundsEvent(Sender: TObject);
begin
HideListBox;
end;
procedure TKASPathEdit.ListBoxClick(Sender: TObject);
begin
if FListBox.ItemIndex >= 0 then
begin
Text:= FListBox.Items[FListBox.ItemIndex];
SelStart:= UTF8Length(Text);
HideListBox;
SetFocus;
end;
end;
procedure TKASPathEdit.ListBoxMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
FListBox.ItemIndex:= FListBox.ItemAtPos(Classes.Point(X, Y), True);
end;
procedure TKASPathEdit.ShowListBox;
begin
if (FPanel = nil) then
begin
FPanel:= THintWindow.Create(Self);
FPanel.Color:= clForm;
FListBox.Parent:= FPanel;
with Parent.ClientToScreen(CLasses.Point(Left, Top)) do
begin
FPanel.Left:= X;
FPanel.Top:= Y + Height;
end;
FPanel.Width:= Width;
FPanel.Visible:= True;
Application.AddOnDeactivateHandler(FormChangeBoundsEvent, True);
GetParentForm(Self).AddHandlerOnChangeBounds(FormChangeBoundsEvent, True);
end;
end;
procedure TKASPathEdit.HideListBox;
begin
if (FPanel <> nil) then
begin
FPanel.Visible:= False;
FListBox.Parent:= nil;
FreeAndNil(FPanel);
Application.RemoveOnDeactivateHandler(FormChangeBoundsEvent);
GetParentForm(Self).RemoveHandlerOnChangeBounds(FormChangeBoundsEvent);
end;
end;
{$IF DEFINED(LCLWIN32)}
procedure TKASPathEdit.CreateWnd;
begin
inherited CreateWnd;
FAutoComplete:= not SHAutoCompleteX(Handle, FObjectTypes);
end;
{$ENDIF}
procedure TKASPathEdit.DoExit;
begin
HideListBox;
inherited DoExit;
end;
procedure TKASPathEdit.KeyDown(var Key: Word; Shift: TShiftState);
begin
FKeyDown:= Key;
case Key of
VK_ESCAPE,
VK_RETURN,
VK_SELECT:
begin
HideListBox;
end;
VK_UP:
if Assigned(FPanel) then
begin
Key:= 0;
if FListBox.ItemIndex = -1 then
FListBox.ItemIndex:= FListBox.Items.Count - 1
else if FListBox.ItemIndex - 1 < 0 then
FListBox.ItemIndex:= - 1
else
FListBox.ItemIndex:= FListBox.ItemIndex - 1;
if FListBox.ItemIndex >= 0 then
Text:= FListBox.Items[FListBox.ItemIndex]
else
Text:= ExtractFilePath(Text);
SelStart:= UTF8Length(Text);
end;
VK_DOWN:
if Assigned(FPanel) then
begin
Key:= 0;
if FListBox.ItemIndex + 1 >= FListBox.Items.Count then
FListBox.ItemIndex:= -1
else if FListBox.ItemIndex = -1 then
FListBox.ItemIndex:= IfThen(FListBox.Items.Count > 0, 0, -1)
else
FListBox.ItemIndex:= FListBox.ItemIndex + 1;
if FListBox.ItemIndex >= 0 then
Text:= FListBox.Items[FListBox.ItemIndex]
else
Text:= ExtractFilePath(Text);
SelStart:= UTF8Length(Text);
end;
end;
inherited KeyDown(Key, Shift);
{$IFDEF LCLGTK2}
// Workaround for GTK2 - up and down arrows moving through controls.
if Key in [VK_UP, VK_DOWN] then Key:= 0;
{$ENDIF}
end;
procedure TKASPathEdit.KeyUpAfterInterface(var Key: Word; Shift: TShiftState);
begin
if (FKeyDown = Key) and FAutoComplete and not (Key in [VK_ESCAPE, VK_RETURN, VK_SELECT, VK_UP, VK_DOWN]) then
begin
if Modified then
begin
Modified:= False;
AutoComplete(Text);
end;
end;
inherited KeyUpAfterInterface(Key, Shift);
{$IF DEFINED(LCLWIN32)}
// Windows auto-completer eats the TAB so LCL doesn't get it and doesn't move to next control.
if not FAutoComplete and (Key = VK_TAB) then
GetParentForm(Self).SelectNext(Self, True, True);
{$ENDIF}
end;
constructor TKASPathEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FListBox:= TListBox.Create(Self);
FListBox.TabStop:= False;
FListBox.Align:= alClient;
FListBox.ClickOnSelChange:= False;
FListBox.OnClick:= ListBoxClick;
FListBox.OnMouseMove:= ListBoxMouseMove;
FAutoComplete:= True;
FFileSortType:= fstFoldersFirst;
FObjectTypes:= [otNonFolders, otFolders];
end;
end.
|