@@ -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 ;
117121end ;
118122
119123procedure TfrmExtFileDialog.FormDestroy (Sender: TObject);
@@ -130,17 +134,15 @@ procedure TfrmExtFileDialog.FormShow(Sender: TObject);
130134 LineBreakIndexInt: Integer;
131135begin
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);
158160var
159161 FileMask: String;
160162begin
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);
166171end ;
167172
168173procedure TfrmExtFileDialog.comboLineBreaksChange (Sender: TObject);
@@ -226,17 +231,32 @@ procedure TfrmExtFileDialog.ShellListViewSelectItem(Sender: TObject;
226231 end ;
227232end ;
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+
229246procedure TfrmExtFileDialog.SetTitle (AValue: String);
230247begin
231248 Caption := AValue;
232249end ;
233250
234251function TfrmExtFileDialog.GetFileName : String;
235252begin
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 := ' ' ;
242262end ;
@@ -246,35 +266,58 @@ procedure TfrmExtFileDialog.SetFileName(const AValue: String);
246266 fn: String;
247267begin
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);
252272end ;
253273
254274procedure TfrmExtFileDialog.SetInitialDir (const AValue: String);
255275begin
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 ;
257286end ;
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;
264302begin
265303 inherited ;
266- lblEncoding.Visible := True;
267- comboEncoding.Visible := True;
304+ EncodingVisible := comboEncoding.Items.Count > 0 ;
305+ lblEncoding.Visible := EncodingVisible;
306+ comboEncoding.Visible := EncodingVisible;
268307end ;
269308
270309
310+
271311{ TExtFileSaveDialog }
272312
273- procedure TExtFileSaveDialog.FormCreate (Sender: TObject);
313+ procedure TExtFileSaveDialog.FormShow (Sender: TObject);
314+ var
315+ LinebreaksVisible: Boolean;
274316begin
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;
278321end ;
279322
280323end .
0 commit comments