11
Forms, ShellCtrls, Buttons, StdCtrls, ExtCtrls, FileCtrl, ComCtrls,
12
Dialogs, ButtonPanel, lclstrconsts, FileUtil, Controls;
15
TLazFileDialogKind = (
16
ldkOpenDesktop, ldkSaveDesktop, ldkOpenPDA, ldkSavePDA, ldkSelectDirectory);
18
{ TLazarusFileDialogForm }
20
TLazarusFileDialogForm = class(TForm)
22
FKind: TLazFileDialogKind;
23
procedure SetFilter(AFilter: string);
26
ButtonPanel: TButtonPanel;
27
ShellTreeView: TShellTreeView;
28
ShellListView: TShellListView;
30
FilterComboBox: TFilterComboBox;
37
constructor CreateNew(AOwner: TComponent; Num: Integer = 0); override;
38
procedure Initialize(AKind: TLazFileDialogKind);
39
procedure HandleOkClick(ASender: TObject);
40
procedure HandleCancelClick(ASender: TObject);
41
procedure HandleCloseQuery(Sender : TObject; var CanClose : boolean);
42
procedure HandleEditChange(ASender: TObject);
43
procedure HandleSelectItem(Sender: TObject;
44
Item: TListItem; Selected: Boolean);
45
procedure HandleTreeViewSelectionChanged(ASender: TObject);
50
TLazOpenDialog = class(TOpenDialog)
52
FForm: TLazarusFileDialogForm;
53
class procedure WSRegisterClass; override;
54
function DoExecute: boolean; override;
55
procedure DoInitialize; virtual;
57
constructor Create(TheOwner: TComponent); override;
62
TLazSaveDialog = class(TLazOpenDialog)
64
procedure DoInitialize; override;
67
{ TLazSelectDirectoryDialog }
69
TLazSelectDirectoryDialog = class(TLazOpenDialog)
71
procedure DoInitialize; override;
76
{ TLazarusFileDialogForm }
78
procedure TLazarusFileDialogForm.SetFilter(AFilter: string);
81
FilterComboBox.Filter := Format(rsAllFiles,[GetAllFilesMask, GetAllFilesMask,''])
83
FilterComboBox.Filter := AFilter;
87
The size of the window is determined only when creating the
88
handle, so any reference to TForm.Width and TForm.Height
89
here doesnt correspond to the final value.
91
constructor TLazarusFileDialogForm.CreateNew(AOwner: TComponent; Num: Integer = 0);
93
inherited CreateNew(AOwner, Num);
95
Self.Position := poScreenCenter;
98
procedure TLazarusFileDialogForm.Initialize(AKind: TLazFileDialogKind);
102
ButtonPanel := TButtonPanel.Create(Self);
103
ButtonPanel.Parent := Self;
104
ButtonPanel.Left := 0;
105
ButtonPanel.Height := 20;
106
ButtonPanel.Top := Height - ButtonPanel.Height;
107
ButtonPanel.Width := Width;
108
ButtonPanel.Align := alBottom;
109
ButtonPanel.ShowButtons := [pbOK, pbCancel];
110
ButtonPanel.OKButton.OnClick := HandleOkClick;
111
ButtonPanel.CancelButton.OnClick := HandleCancelClick;
113
if AKind in [ldkOpenDesktop, ldkSaveDesktop, ldkOpenPDA, ldkSavePDA] then
115
// Add the ShellTreeView to the dialog
116
ShellTreeView := TShellTreeView.Create(Self);
117
ShellTreeView.Parent := Self;
118
ShellTreeView.Left := 0;
119
ShellTreeView.Top := 0;
120
ShellTreeView.Width := Width;
121
ShellTreeView.Height := 100;
122
ShellTreeView.Align := alTop;
124
// Add the ShellListView to the dialog
125
ShellListView := TShellListView.Create(Self);
126
ShellListView.Parent := Self;
127
ShellListView.Left := 0;
128
ShellListView.Top := ShellTreeView.Height;
129
ShellListView.Width := Width;
130
ShellListView.Height := Height - ShellTreeView.Height - ButtonPanel.Height;
131
ShellListView.Align := alClient;
132
ShellListView.ShellTreeView := ShellTreeView;
133
ShellListView.ScrollBars := ssVertical;
134
ShellListView.OnSelectItem := HandleSelectItem;
136
// TEdit for save dialog
137
if AKind in [ldkSaveDesktop, ldkSavePDA] then
139
SaveEdit := TEdit.Create(Self);
140
SaveEdit.Parent := Self;
142
SaveEdit.Height := 20;
143
SaveEdit.Top := Height - ButtonPanel.Height - SaveEdit.Height;
144
SaveEdit.Width := Width;
145
SaveEdit.Align := alBottom;
146
SaveEdit.Text := SysUtils.ExtractFileName(FileName);
147
SaveEdit.OnChange := HandleEditChange;
151
FilterComboBox := TFilterComboBox.Create(Self);
152
FilterComboBox.Parent := Self;
153
FilterComboBox.Left := 0;
154
FilterComboBox.Height := 20;
155
FilterComboBox.Top := Height - ButtonPanel.Height - FilterComboBox.Height;
156
if SaveEdit <> nil then
157
FilterComboBox.Top := FilterComboBox.Top - SaveEdit.Height;
158
FilterComboBox.Width := Width;
159
FilterComboBox.Align := alBottom;
161
FilterComboBox.ShellListView := ShellListView;
163
// In the save dialog it is enabled when there is a text in the TEdit
164
if AKind in [ldkSaveDesktop, ldkSavePDA] then
165
ButtonPanel.OKButton.Enabled := SaveEdit.Text <> ''
166
// In a TOpenDialog the Ok button is only enabled when a file is selected
168
ButtonPanel.OkButton.Enabled := False;
170
else if FKind = ldkSelectDirectory then
172
// Add the ShellTreeView to the dialog
173
ShellTreeView := TShellTreeView.Create(Self);
174
ShellTreeView.Parent := Self;
175
ShellTreeView.Left := 0;
176
ShellTreeView.Top := 0;
177
ShellTreeView.Align := alClient;
178
ShellTreeView.OnSelectionChanged := HandleTreeViewSelectionChanged;
180
ButtonPanel.OKButton.Enabled := False;
184
OnCloseQuery := HandleCloseQuery;
187
// The Ok button code should be only a simple mrOk,
188
// because there is the dialog Ok button, which will
189
// always be active and will set the ModalResult to mrOk
190
// so the code needs to affect it too, and this can be
191
// done in CloseQuery
192
procedure TLazarusFileDialogForm.HandleOkClick(ASender: TObject);
197
procedure TLazarusFileDialogForm.HandleCancelClick(ASender: TObject);
199
ModalResult := mrCancel;
202
procedure TLazarusFileDialogForm.HandleCloseQuery(Sender: TObject;
203
var CanClose: boolean);
205
if ModalResult = mrCancel then
213
if FKind in [ldkSaveDesktop, ldkSavePDA] then
215
if SaveEdit.Text = '' then Exit;
217
FileName := ShellTreeView.GetPathFromNode(ShellTreeView.Selected);
218
FileName := IncludeTrailingPathDelimiter(FileName);
219
FileName := FileName + SaveEdit.Text;
222
else if FKind in [ldkOpenDesktop, ldkOpenPDA] then
224
if ShellListView.Selected = nil then Exit;
226
FileName := ShellListView.GetPathFromItem(ShellListView.Selected);
231
if ShellTreeView.Selected = nil then Exit;
233
FileName := ShellTreeView.GetPathFromNode(ShellTreeView.Selected);
238
procedure TLazarusFileDialogForm.HandleEditChange(ASender: TObject);
240
ButtonPanel.OkButton.Enabled := SaveEdit.Text <> '';
243
procedure TLazarusFileDialogForm.HandleSelectItem(Sender: TObject;
244
Item: TListItem; Selected: Boolean);
246
// Selecting an item changes the filename in the TEdit
248
if (FKind in [ldkSaveDesktop, ldkSavePDA]) and Selected then
250
SaveEdit.Text := Item.Caption;
252
// In the OpenDialog the state of the Ok button is dependent
253
// on the selection of an item
256
ButtonPanel.OkButton.Enabled := Selected;
260
// Used only in the TLazSelectDirectoryDialog
261
procedure TLazarusFileDialogForm.HandleTreeViewSelectionChanged(ASender: TObject);
263
ButtonPanel.OKButton.Enabled := True;
268
class procedure TLazOpenDialog.WSRegisterClass;
270
// Do nothing, because this dialog doesn't require a WS implementation
273
function TLazOpenDialog.DoExecute: boolean;
275
Result := FForm.ShowModal <> mrCancel;
276
FileName := FForm.FileName;
279
procedure TLazOpenDialog.DoInitialize;
281
FForm.Initialize(ldkOpenDesktop);
284
constructor TLazOpenDialog.Create(TheOwner: TComponent);
286
inherited Create(TheOwner);
287
FForm := TLazarusFileDialogForm.CreateNew(Self);
288
FForm.FileName := FileName;
289
FForm.Filter := Filter;
290
FForm.Title := Title;
297
procedure TLazSaveDialog.DoInitialize;
299
FForm.Initialize(ldkSaveDesktop);
302
{ TLazSelectDirectoryDialog }
304
procedure TLazSelectDirectoryDialog.DoInitialize;
306
FForm.Initialize(ldkSelectDirectory);