FreeLaunch
189 строк · 5.8 Кб
1{
2##########################################################################
3# FreeLaunch is a free links manager for Microsoft Windows #
4# #
5# Copyright (C) 2022 Alexey Tatuyko <feedback@ta2i4.ru> #
6# Copyright (C) 2019 Mykola Petrivskiy #
7# Copyright (C) 2010 Joker-jar <joker-jar@yandex.ru> #
8# #
9# This file is part of FreeLaunch. #
10# #
11# FreeLaunch is free software: you can redistribute it and/or modify #
12# it under the terms of the GNU General Public License as published by #
13# the Free Software Foundation, either version 3 of the License, or #
14# (at your option) any later version. #
15# #
16# FreeLaunch is distributed in the hope that it will be useful, #
17# but WITHOUT ANY WARRANTY; without even the implied warranty of #
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
19# GNU General Public License for more details. #
20# #
21# You should have received a copy of the GNU General Public License #
22# along with FreeLaunch. If not, see <http://www.gnu.org/licenses/>. #
23##########################################################################
24}
25
26unit FLDialogs;
27
28interface
29
30function ProgramSelect(AFileName: string): string;
31function FileOrDirSelect(AFileName: string): string;
32
33implementation
34
35uses
36System.SysUtils, VCL.Dialogs, Vcl.Forms, Winapi.Windows, Winapi.Messages,
37Winapi.ShlObj, Winapi.ActiveX, System.Win.ComObj, FLLanguage, FLFunctions;
38
39type
40TFileOrDirDialog = class
41FOpenDialog: TFileOpenDialog;
42FFileName: string;
43procedure OpenDialogFolderChange(Sender: TObject);
44function BrowseDialog(Handle: HWnd; Title: string; var OutDir: string): boolean;
45public
46property FileName: string read FFileName;
47function Execute: Boolean;
48constructor Create(AFileName: string);
49destructor Destroy; override;
50end;
51
52function ProgramSelect(AFileName: string): string;
53var
54Dialog: TOpenDialog;
55CurrentFile: string;
56begin
57Dialog := TOpenDialog.Create(Nil);
58try
59Dialog.Filter := Language.Properties.ProgramFilter + '|*.exe;*.bat';
60Dialog.Options := Dialog.Options + [ofFileMustExist, ofNoDereferenceLinks];
61
62CurrentFile := GetAbsolutePath(AFileName);
63if FileExists(CurrentFile) then
64begin
65Dialog.FileName := CurrentFile;
66Dialog.InitialDir := ExtractFilePath(CurrentFile);
67end;
68
69if Dialog.Execute then
70begin
71Result := Dialog.FileName;
72
73if IsPortable then
74Result := PathToPortable(Dialog.FileName);
75end
76else
77Result := AFileName;
78finally
79Dialog.Free;
80end;
81end;
82
83function BffCallBackF(Wnd: HWND; uMsg: UINT; lParam, lpData: lParam): Integer; stdcall;
84begin
85if (uMsg = BFFM_INITIALIZED) then
86begin
87if (lpData <> 0) then
88begin
89SendMessage(Wnd, BFFM_SETSELECTION, 1, lpData);
90end;
91end;
92result := 0;
93end;
94
95function TFileOrDirDialog.BrowseDialog(Handle: HWnd; Title: string;
96var OutDir: string): boolean;
97var
98lpItemID: PItemIDList;
99BrowseInfo: TBrowseInfo;
100DisplayName, CurDir: array [0..MAX_PATH] of char;
101begin
102Result := false;
103FillChar(DisplayName, sizeof(DisplayName), #0);
104FillChar(BrowseInfo, sizeof(TBrowseInfo), #0);
105StrPCopy(CurDir, OutDir);
106with BrowseInfo do
107begin
108hwndOwner := Handle;
109pszDisplayName := @DisplayName;
110lpszTitle := PChar(Title);
111lParam := Winapi.Windows.LPARAM(@CurDir);
112lpfn := @BffCallBackF; //BrowseCallbackProc
113ulFlags := BIF_SHAREABLE or BIF_BROWSEINCLUDEFILES or BIF_USENEWUI or
114BIF_RETURNONLYFSDIRS;
115end;
116lpItemID := SHBrowseForFolder(BrowseInfo);
117if lpItemId <> nil then
118begin
119SHGetPathFromIDList(lpItemID, CurDir);
120OutDir := CurDir;
121Result := true;
122GlobalFreePtr(lpItemID);
123end;
124end;
125
126constructor TFileOrDirDialog.Create(AFileName: string);
127begin
128FFileName := AFileName;
129FOpenDialog := TFileOpenDialog.Create(Nil);
130FOpenDialog.Options := [fdoNoValidate, fdoPathMustExist];
131end;
132
133destructor TFileOrDirDialog.Destroy;
134begin
135FOpenDialog.Free;
136inherited;
137end;
138
139function TFileOrDirDialog.Execute: Boolean;
140var
141FilePath: string;
142begin
143FilePath := ExtractFilePath(FFileName);
144if DirectoryExists(FilePath) then
145FOpenDialog.DefaultFolder := FilePath;
146FOpenDialog.FileName := 'Select file or directory';
147Result := FOpenDialog.Execute;
148if Result then
149begin
150if FileExists(FOpenDialog.FileName) then
151FFileName := FOpenDialog.FileName
152else
153FFileName := ExtractFilePath(FOpenDialog.FileName);
154end;
155end;
156
157procedure TFileOrDirDialog.OpenDialogFolderChange(Sender: TObject);
158var
159DialogWnd: IOleWindow;
160DialogHandle, ComboHandle: HWND;
161begin
162DialogWnd := FOpenDialog.Dialog as IOleWindow;
163OleCheck(DialogWnd.GetWindow(DialogHandle));
164ComboHandle := FindWindowEx(DialogHandle, 0, 'ComboBoxEx32', nil);
165if ComboHandle <> 0 then
166SendMessage(ComboHandle, WM_SETTEXT, 0, LPARAM(PChar('Select file or directory')));
167end;
168
169function FileOrDirSelect(AFileName: string): string;
170var
171FileOrDirDialog: TFileOrDirDialog;
172begin
173FileOrDirDialog := TFileOrDirDialog.Create(GetAbsolutePath(AFileName));
174try
175if FileOrDirDialog.Execute then
176begin
177Result := FileOrDirDialog.FileName;
178
179if IsPortable then
180Result := PathToPortable(FileOrDirDialog.FileName);
181end
182else
183Result := AFileName;
184finally
185FileOrDirDialog.Free;
186end;
187end;
188
189end.
190