Skip to content

Commit 4a8c53c

Browse files
committed
feat: convert all save/open dialogs to the new custom one
* TOpenDialog => TExtFileOpenDialog * TSaveDialog => TExtFileSaveDialog * show encodings and linebreaks only when the caller sets data for these * show selected path from tree in label at top * support DefaultExt string * support OnTypeChange event * disallow changing tree folder for ofNoChangeDir * catch EInvalidPath in SetInitialDir * show hidden folders in tree * enable sorting in file listing Refs #2268
1 parent 91de2fc commit 4a8c53c

File tree

9 files changed

+379
-316
lines changed

9 files changed

+379
-316
lines changed

source/bineditor.pas

Lines changed: 220 additions & 221 deletions
Large diffs are not rendered by default.

source/connections.pas

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface
1313
SysUtils, Classes, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls,
1414
laz.VirtualTrees, Menus, Graphics, extra_controls, lazaruscompat,
1515
dbconnection, RegExpr, Types, FileUtil,
16-
Math, ActnList, ComboEx, EditBtn, Buttons, ColorBox;
16+
Math, ActnList, ComboEx, EditBtn, Buttons, ColorBox, extfiledialog;
1717

1818
type
1919

@@ -1720,26 +1720,29 @@ procedure Tconnform.PageControlDetailsChange(Sender: TObject);
17201720

17211721
procedure Tconnform.PickFile(Sender: TObject);
17221722
var
1723-
Selector: TOpenDialog;
1723+
Selector: TExtFileOpenDialog;
17241724
Edit: TEditButton;
17251725
i: Integer;
17261726
Control: TControl;
17271727
FileNames: TStringList;
17281728
begin
17291729
// Select startup SQL file, SSL file or whatever button clicked
17301730
Edit := Sender as TEditButton;
1731-
Selector := TOpenDialog.Create(Self);
1731+
Selector := TExtFileOpenDialog.Create(Self);
17321732
if Edit = editHost then begin
1733-
Selector.Filter := 'SQLite databases ('+FILEFILTER_SQLITEDB+')|'+FILEFILTER_SQLITEDB+'|'+_('All files')+' (*.*)|*.*';
1733+
Selector.AddFileType(FILEFILTER_SQLITEDB, 'SQLite databases');
17341734
Selector.Options := Selector.Options - [ofFileMustExist];
17351735
Selector.Options := Selector.Options + [ofAllowMultiSelect];
17361736
Selector.DefaultExt := FILEEXT_SQLITEDB;
17371737
end else if (Edit = editStartupScript) or (Edit = editLogFilePath) then
1738-
Selector.Filter := _('SQL files')+' (*.sql)|*.sql|'+_('All files')+' (*.*)|*.*'
1739-
else if Edit = editSSHPrivateKey then
1740-
Selector.Filter := _('All files')+' (*.*)|*.*'
1741-
else
1742-
Selector.Filter := _('Privacy Enhanced Mail certificates')+' (*.pem)|*.pem|'+_('Certificates')+' (*.crt)|*.crt|'+_('All files')+' (*.*)|*.*';
1738+
Selector.AddFileType('*.sql', _('SQL files'))
1739+
else if Edit = editSSHPrivateKey then begin
1740+
end
1741+
else begin
1742+
Selector.AddFileType('*.pem', _('Privacy Enhanced Mail certificates'));
1743+
Selector.AddFileType('*.crt', _('Certificates'));
1744+
end;
1745+
Selector.AddFileType('*.*', _('All files'));
17431746
// Find relevant label and set open dialog's title
17441747
for i:=0 to Edit.Parent.ControlCount - 1 do begin
17451748
Control := Edit.Parent.Controls[i];
@@ -1750,8 +1753,6 @@ procedure Tconnform.PickFile(Sender: TObject);
17501753
end;
17511754
// Set initial directory to the one from the edit's file
17521755
Selector.InitialDir := ExtractFilePath(Edit.Text);
1753-
//if Selector.InitialDir.IsEmpty then
1754-
// Selector.InitialDir := TPath.GetPathRoot(Application.ExeName);
17551756
if Selector.Execute then begin
17561757
FileNames := TStringList.Create;
17571758
FileNames.Assign(Selector.Files);

source/exportgrid.pas

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ interface
77
uses
88
SysUtils, Variants, Classes, Graphics, Controls, Forms,
99
Dialogs, StdCtrls, ExtCtrls, Menus, ComCtrls, laz.VirtualTrees, SynExportHTML,
10-
extra_controls, dbstructures, lazaruscompat, RegExpr, StrUtils, comboex, EditBtn, Buttons, LCLIntf;
10+
extra_controls, dbstructures, lazaruscompat, RegExpr, StrUtils, comboex, EditBtn, Buttons, LCLIntf,
11+
extfiledialog;
1112

1213
type
1314
TGridExportFormat = (
@@ -390,21 +391,20 @@ procedure TfrmExportGrid.FormDestroy(Sender: TObject);
390391

391392
procedure TfrmExportGrid.editFilenameRightButtonClick(Sender: TObject);
392393
var
393-
Dialog: TSaveDialog;
394+
Dialog: TExtFileSaveDialog;
394395
ef: TGridExportFormat;
395396
Filename: String;
396397
begin
397398
// Select file target
398-
Dialog := TSaveDialog.Create(Self);
399+
Dialog := TExtFileSaveDialog.Create(Self);
399400
Filename := GetOutputFilename(editFilename.Text, MainForm.ActiveDbObj);
400401
Dialog.InitialDir := ExtractFilePath(Filename);
401402
Dialog.FileName := GetFileNameWithoutExtension(Filename);
402-
Dialog.Filter := '';
403403
for ef:=Low(TGridExportFormat) to High(TGridExportFormat) do
404-
Dialog.Filter := Dialog.Filter + FormatToDescription[ef] + ' (*.'+FormatToFileExtension[ef]+')|*.'+FormatToFileExtension[ef]+'|';
405-
Dialog.Filter := Dialog.Filter + _('All files')+' (*.*)|*.*';
404+
Dialog.AddFileType('*.'+FormatToFileExtension[ef], FormatToDescription[ef]);
405+
Dialog.AddFileType('*.*', _('All files'));
406406
Dialog.OnTypeChange := SaveDialogTypeChange;
407-
Dialog.FilterIndex := comboFormat.ItemIndex+1;
407+
Dialog.FilterIndex := comboFormat.ItemIndex;
408408
Dialog.OnTypeChange(Dialog);
409409
if Dialog.Execute then begin
410410
editFilename.Text := Dialog.FileName;
@@ -558,13 +558,13 @@ procedure TfrmExportGrid.editCSVChange(Sender: TObject);
558558

559559
procedure TfrmExportGrid.SaveDialogTypeChange(Sender: TObject);
560560
var
561-
Dialog: TSaveDialog;
561+
Dialog: TExtFileSaveDialog;
562562
ef: TGridExportFormat;
563563
begin
564564
// Set default file-extension of saved file and options on the dialog to show
565-
Dialog := Sender as TSaveDialog;
565+
Dialog := Sender as TExtFileSaveDialog;
566566
for ef:=Low(TGridExportFormat) to High(TGridExportFormat) do begin
567-
if Dialog.FilterIndex = Integer(ef)+1 then
567+
if Dialog.FilterIndex = Integer(ef) then
568568
Dialog.DefaultExt := FormatToFileExtension[ef];
569569
end;
570570
end;

source/extfiledialog.lfm

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,38 +165,53 @@ object frmExtFileDialog: TfrmExtFileDialog
165165
end
166166
end
167167
object ShellTreeView: TShellTreeView
168+
AnchorSideTop.Control = lblPath
169+
AnchorSideTop.Side = asrBottom
168170
Left = 10
169-
Height = 446
170-
Top = 10
171+
Height = 416
172+
Top = 40
171173
Width = 261
172174
Align = alLeft
173175
BorderSpacing.Left = 10
174-
BorderSpacing.Top = 10
175176
HideSelection = False
176177
ReadOnly = False
177178
TabOrder = 0
178179
Options = [tvoAutoItemHeight, tvoKeepCollapsedNodes, tvoShowButtons, tvoShowLines, tvoShowRoot, tvoToolTips, tvoThemedDraw]
180+
ObjectTypes = [otFolders, otHidden]
179181
ShellListView = ShellListView
182+
OnChange = ShellTreeViewChange
183+
OnChanging = ShellTreeViewChanging
180184
end
181185
object splitterMain: TSplitter
182186
Left = 271
183-
Height = 456
184-
Top = 0
187+
Height = 416
188+
Top = 40
185189
Width = 6
186190
end
187191
object ShellListView: TShellListView
192+
AnchorSideTop.Control = lblPath
193+
AnchorSideTop.Side = asrBottom
188194
Left = 277
189-
Height = 446
190-
Top = 10
195+
Height = 416
196+
Top = 40
191197
Width = 585
192198
Align = alClient
193-
BorderSpacing.Top = 10
194199
BorderSpacing.Right = 10
195200
Color = clDefault
201+
SortType = stText
196202
TabOrder = 1
197203
ShellTreeView = ShellTreeView
198204
OnClick = ShellListViewClick
199205
OnDblClick = ShellListViewDblClick
200206
OnSelectItem = ShellListViewSelectItem
201207
end
208+
object lblPath: TLabel
209+
Left = 10
210+
Height = 20
211+
Top = 10
212+
Width = 852
213+
Align = alTop
214+
BorderSpacing.Around = 10
215+
Caption = 'lblPath'
216+
end
202217
end

source/extfiledialog.pas

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ TfrmExtFileDialog = class(TExtForm)
1919
comboEncoding: TComboBox;
2020
comboFileType: TComboBox;
2121
editFilename: TEdit;
22+
lblPath: TLabel;
2223
lblLinebreaks: TLabel;
2324
lblEncoding: TLabel;
2425
lblFilename: TLabel;
@@ -37,21 +38,28 @@ TfrmExtFileDialog = class(TExtForm)
3738
procedure ShellListViewDblClick(Sender: TObject);
3839
procedure ShellListViewSelectItem(Sender: TObject; Item: TListItem;
3940
Selected: Boolean);
41+
procedure ShellTreeViewChange(Sender: TObject; Node: TTreeNode);
42+
procedure ShellTreeViewChanging(Sender: TObject; Node: TTreeNode;
43+
var AllowChange: Boolean);
4044
private
4145
FInitialDir: String;
4246
FFilterNames: TStringList;
4347
FFilterMasks: TStringList;
48+
FFilterIndex: Integer;
4449
FDefaultExt: String;
4550
FEncodings: TStringList;
4651
FEncodingIndex: Integer;
4752
FLineBreakIndex: TLineBreaks;
4853
FOptions: TOpenOptions;
4954
FFiles: TStringList;
55+
FOnTypeChange: TNotifyEvent;
5056
procedure SetTitle(AValue: String);
5157
function GetFileName: String;
5258
procedure SetFileName(const AValue: String);
5359
procedure SetInitialDir(const AValue: String);
60+
procedure SetFilterIndex(AValue: Integer);
5461
public
62+
property OnTypeChange: TNotifyEvent read FOnTypeChange write FOnTypeChange;
5563
property Title: String write SetTitle;
5664
function Execute: Boolean;
5765
procedure AddFileType(FileMask, DisplayName: String);
@@ -61,18 +69,19 @@ TfrmExtFileDialog = class(TExtForm)
6169
property DefaultExt: String read FDefaultExt write FDefaultExt;
6270
property Options: TOpenOptions read FOptions write FOptions;
6371
property Files: TStringList read FFiles;
72+
property FilterIndex: Integer read FFilterIndex write SetFilterIndex;
6473
end;
6574

6675
// File-open-dialog with encoding selector
6776
TExtFileOpenDialog = class(TfrmExtFileDialog)
68-
procedure FormCreate(Sender: TObject); overload;
77+
procedure FormShow(Sender: TObject); overload;
6978
public
7079
property Encodings: TStringList read FEncodings write FEncodings;
7180
property EncodingIndex: Integer read FEncodingIndex write FEncodingIndex;
7281
end;
7382

7483
TExtFileSaveDialog = class(TfrmExtFileDialog)
75-
procedure FormCreate(Sender: TObject); overload;
84+
procedure FormShow(Sender: TObject); overload;
7685
public
7786
property LineBreakIndex: TLineBreaks read FLineBreakIndex write FLineBreakIndex;
7887
end;
@@ -100,20 +109,15 @@ procedure TfrmExtFileDialog.FormCreate(Sender: TObject);
100109
raise Exception.CreateFmt('Constructor of base class %s called. Use one of its descendants instead.', [ClassName]);
101110
FFilterNames := TStringList.Create;
102111
FFilterMasks := TStringList.Create;
112+
FFilterIndex := 0;
113+
FDefaultExt := '';
103114
FEncodings := TStringList.Create;
104-
{$IFDEF LINUX}
105-
FLineBreakIndex := lbsUnix;
106-
{$ENDIF}
107-
{$IFDEF WINDOWS}
108-
FLineBreakIndex := lbsWindows;
109-
{$ENDIF}
110-
{$IFDEF DARWIN}
111-
FLineBreakIndex := lbsMac;
112-
{$ENDIF}
115+
FLineBreakIndex := lbsNone;
113116
FFiles := TStringList.Create;
114117
comboFileType.Items.Clear;
115118
editFilename.Text := '';
116119
comboLineBreaks.Items.Clear;
120+
FOnTypeChange := nil;
117121
end;
118122

119123
procedure TfrmExtFileDialog.FormDestroy(Sender: TObject);
@@ -130,17 +134,15 @@ procedure TfrmExtFileDialog.FormShow(Sender: TObject);
130134
LineBreakIndexInt: Integer;
131135
begin
132136
ShellListView.MultiSelect := ofAllowMultiSelect in FOptions;
133-
ShellTreeView.Enabled := not (ofNoChangeDir in FOptions);
134-
// Todo: support ofFileMustExist and convert usages of TOpenDialog and TSaveDialog
135-
if FInitialDir.IsEmpty then begin
136-
if not PreviousDir.IsEmpty then
137-
SetInitialDir(PreviousDir)
138-
else
139-
SetInitialDir(GetUserDir);
140-
end;
137+
// Todo: support ofFileMustExist
138+
if not FInitialDir.IsEmpty then
139+
SetInitialDir(FInitialDir)
140+
else if not PreviousDir.IsEmpty then
141+
SetInitialDir(PreviousDir)
142+
else
143+
SetInitialDir(GetUserDir);
141144

142-
comboFileType.ItemIndex := 0;
143-
comboFileType.OnChange(Sender);
145+
SetFilterIndex(FFilterIndex);
144146

145147
comboEncoding.Items.AddStrings(FEncodings, True);
146148
if (FEncodingIndex >=0) and (FEncodingIndex < comboEncoding.Items.Count) then
@@ -158,11 +160,14 @@ procedure TfrmExtFileDialog.comboFileTypeChange(Sender: TObject);
158160
var
159161
FileMask: String;
160162
begin
163+
FFilterIndex := comboFileType.ItemIndex;
161164
if (comboFileType.ItemIndex >= 0) and (FFilterMasks.Count > comboFileType.ItemIndex) then
162165
FileMask := FFilterMasks[comboFileType.ItemIndex]
163166
else
164167
FileMask := '*.*';
165168
ShellListView.Mask := FileMask;
169+
if Assigned(FOnTypeChange) then
170+
FOnTypeChange(Self);
166171
end;
167172

168173
procedure TfrmExtFileDialog.comboLineBreaksChange(Sender: TObject);
@@ -226,17 +231,32 @@ procedure TfrmExtFileDialog.ShellListViewSelectItem(Sender: TObject;
226231
end;
227232
end;
228233

234+
procedure TfrmExtFileDialog.ShellTreeViewChange(Sender: TObject; Node: TTreeNode
235+
);
236+
begin
237+
lblPath.Caption := ShellTreeView.Path;
238+
end;
239+
240+
procedure TfrmExtFileDialog.ShellTreeViewChanging(Sender: TObject;
241+
Node: TTreeNode; var AllowChange: Boolean);
242+
begin
243+
AllowChange := not (ofNoChangeDir in FOptions);
244+
end;
245+
229246
procedure TfrmExtFileDialog.SetTitle(AValue: String);
230247
begin
231248
Caption := AValue;
232249
end;
233250

234251
function TfrmExtFileDialog.GetFileName: String;
235252
begin
236-
if ShellListView.Selected <> nil then
253+
if (editFilename.Text <> '') and (ShellTreeView.Selected <> nil) then begin
254+
Result := ShellTreeView.Path + editFilename.Text;
255+
if IsEmpty(ExtractFileExt(Result)) and (not FDefaultExt.IsEmpty) then
256+
Result := Result + '.' + FDefaultExt;
257+
end
258+
else if ShellListView.Selected <> nil then
237259
Result := ShellListView.GetPathFromItem(ShellListView.Selected)
238-
else if (editFilename.Text <> '') and (ShellTreeView.Selected <> nil) then
239-
Result := ShellTreeView.Path + editFilename.Text
240260
else
241261
Result := '';
242262
end;
@@ -246,35 +266,58 @@ procedure TfrmExtFileDialog.SetFileName(const AValue: String);
246266
fn: String;
247267
begin
248268
fn := ExpandFileName(AValue);
249-
ShellTreeView.Path := ExtractFilePath(fn);
269+
SetInitialDir(ExtractFilePath(fn));
250270
editFilename.Text := ExtractFileName(fn);
251271
ShellListView.Selected := ShellListView.FindCaption(0, fn, false, true, true);
252272
end;
253273

254274
procedure TfrmExtFileDialog.SetInitialDir(const AValue: String);
255275
begin
256-
ShellTreeView.Path := AValue;
276+
FInitialDir := AValue;
277+
try
278+
ShellTreeView.Path := FInitialDir;
279+
except
280+
on E:EInvalidPath do begin
281+
ErrorDialog(E.Message);
282+
// In case, re-enable changing directory in tree
283+
Exclude(FOptions, ofNoChangeDir);
284+
end;
285+
end;
257286
end;
258287

288+
procedure TfrmExtFileDialog.SetFilterIndex(AValue: Integer);
289+
begin
290+
if (AValue >= 0) and (AValue < comboFileType.Items.Count) then begin
291+
comboFileType.ItemIndex := AValue;
292+
comboFileTypeChange(Self);
293+
end;
294+
end;
259295

260296

261297
{ TExtFileOpenDialog }
262298

263-
procedure TExtFileOpenDialog.FormCreate(Sender: TObject);
299+
procedure TExtFileOpenDialog.FormShow(Sender: TObject);
300+
var
301+
EncodingVisible: Boolean;
264302
begin
265303
inherited;
266-
lblEncoding.Visible := True;
267-
comboEncoding.Visible := True;
304+
EncodingVisible := comboEncoding.Items.Count > 0;
305+
lblEncoding.Visible := EncodingVisible;
306+
comboEncoding.Visible := EncodingVisible;
268307
end;
269308

270309

310+
271311
{ TExtFileSaveDialog }
272312

273-
procedure TExtFileSaveDialog.FormCreate(Sender: TObject);
313+
procedure TExtFileSaveDialog.FormShow(Sender: TObject);
314+
var
315+
LinebreaksVisible: Boolean;
274316
begin
275317
inherited;
276-
lblLinebreaks.Visible := True;
277-
comboLineBreaks.Visible := True;
318+
LinebreaksVisible := FLineBreakIndex in [lbsWindows, lbsUnix, lbsMac];
319+
lblLinebreaks.Visible := LinebreaksVisible;
320+
comboLineBreaks.Visible := LinebreaksVisible;
278321
end;
279322

280323
end.

0 commit comments

Comments
 (0)