|
| 1 | +unit extfiledialog; |
| 2 | + |
| 3 | +{$mode delphi}{$H+} |
| 4 | + |
| 5 | +interface |
| 6 | + |
| 7 | +uses |
| 8 | + Classes, SysUtils, LCLType, Forms, Controls, Graphics, Dialogs, |
| 9 | + ExtCtrls, StdCtrls, ShellCtrls, ComCtrls, apphelpers; |
| 10 | + |
| 11 | +type |
| 12 | + |
| 13 | + { TfrmExtFileDialog } |
| 14 | + |
| 15 | + TfrmExtFileDialog = class(TForm) |
| 16 | + btnCancel: TButton; |
| 17 | + btnOk: TButton; |
| 18 | + comboFileType: TComboBox; |
| 19 | + editFilename: TEdit; |
| 20 | + lblFilename: TLabel; |
| 21 | + pnlBottom: TPanel; |
| 22 | + ShellListView: TShellListView; |
| 23 | + ShellTreeView: TShellTreeView; |
| 24 | + splitterMain: TSplitter; |
| 25 | + procedure comboFileTypeChange(Sender: TObject); |
| 26 | + procedure FormCreate(Sender: TObject); |
| 27 | + procedure FormDestroy(Sender: TObject); |
| 28 | + procedure FormShow(Sender: TObject); |
| 29 | + procedure ShellListViewClick(Sender: TObject); |
| 30 | + procedure ShellListViewDblClick(Sender: TObject); |
| 31 | + procedure ShellListViewSelectItem(Sender: TObject; Item: TListItem; |
| 32 | + Selected: Boolean); |
| 33 | + private |
| 34 | + FInitialDir: String; |
| 35 | + FFilterNames: TStringList; |
| 36 | + FFilterMasks: TStringList; |
| 37 | + FDefaultExt: String; |
| 38 | + FEncodings: TStringList; |
| 39 | + FEncodingIndex: Cardinal; |
| 40 | + FOptions: TOpenOptions; |
| 41 | + FFiles: TStringList; |
| 42 | + function GetFileName: String; |
| 43 | + procedure SetFileName(const AValue: String); |
| 44 | + procedure SetInitialDir(const AValue: String); |
| 45 | + public |
| 46 | + function Execute: Boolean; |
| 47 | + procedure AddFileType(FileMask, DisplayName: String); |
| 48 | + property FileName: String read GetFileName write SetFileName; |
| 49 | + property InitialDir: String read FInitialDir write SetInitialDir; |
| 50 | + class var PreviousDir: String; |
| 51 | + property DefaultExt: String read FDefaultExt write FDefaultExt; |
| 52 | + property Encodings: TStringList read FEncodings write FEncodings; |
| 53 | + property EncodingIndex: Cardinal read FEncodingIndex write FEncodingIndex; |
| 54 | + property Options: TOpenOptions read FOptions write FOptions; |
| 55 | + property Files: TStringList read FFiles; |
| 56 | + |
| 57 | + end; |
| 58 | + |
| 59 | + // File-open-dialog with encoding selector |
| 60 | + TExtFileOpenDialog = class(TfrmExtFileDialog); |
| 61 | + |
| 62 | + |
| 63 | +implementation |
| 64 | + |
| 65 | +{$R *.lfm} |
| 66 | + |
| 67 | +function TfrmExtFileDialog.Execute: Boolean; |
| 68 | +begin |
| 69 | + Result := ShowModal = mrOK; |
| 70 | +end; |
| 71 | + |
| 72 | +procedure TfrmExtFileDialog.AddFileType(FileMask, DisplayName: String); |
| 73 | +begin |
| 74 | + FFilterNames.Add(DisplayName); |
| 75 | + FFilterMasks.Add(FileMask); |
| 76 | + comboFileType.Items.Add(DisplayName + ' (' + FileMask + ')'); |
| 77 | +end; |
| 78 | + |
| 79 | +procedure TfrmExtFileDialog.FormCreate(Sender: TObject); |
| 80 | +begin |
| 81 | + FFilterNames := TStringList.Create; |
| 82 | + FFilterMasks := TStringList.Create; |
| 83 | + FEncodings := TStringList.Create; |
| 84 | + FFiles := TStringList.Create; |
| 85 | + comboFileType.Items.Clear; |
| 86 | + editFilename.Text := ''; |
| 87 | +end; |
| 88 | + |
| 89 | +procedure TfrmExtFileDialog.FormDestroy(Sender: TObject); |
| 90 | +begin |
| 91 | + PreviousDir := ShellTreeView.Path; |
| 92 | + FFilterNames.Free; |
| 93 | + FFilterMasks.Free; |
| 94 | + FEncodings.Free; |
| 95 | + FFiles.Free; |
| 96 | +end; |
| 97 | + |
| 98 | +procedure TfrmExtFileDialog.FormShow(Sender: TObject); |
| 99 | +begin |
| 100 | + ShellListView.MultiSelect := ofAllowMultiSelect in FOptions; |
| 101 | + ShellTreeView.Enabled := not (ofNoChangeDir in FOptions); |
| 102 | + // Todo: support ofOverwritePrompt, ofFileMustExist |
| 103 | + if FInitialDir.IsEmpty then begin |
| 104 | + if not PreviousDir.IsEmpty then |
| 105 | + SetInitialDir(PreviousDir) |
| 106 | + else |
| 107 | + SetInitialDir(GetUserDir); |
| 108 | + end; |
| 109 | + comboFileType.ItemIndex := 0; |
| 110 | + comboFileType.OnChange(Sender); |
| 111 | +end; |
| 112 | + |
| 113 | +procedure TfrmExtFileDialog.comboFileTypeChange(Sender: TObject); |
| 114 | +var |
| 115 | + FileMask: String; |
| 116 | +begin |
| 117 | + if (comboFileType.ItemIndex >= 0) and (FFilterMasks.Count > comboFileType.ItemIndex) then |
| 118 | + FileMask := FFilterMasks[comboFileType.ItemIndex] |
| 119 | + else |
| 120 | + FileMask := '*.*'; |
| 121 | + ShellListView.Mask := FileMask; |
| 122 | +end; |
| 123 | + |
| 124 | +procedure TfrmExtFileDialog.ShellListViewClick(Sender: TObject); |
| 125 | +begin |
| 126 | + if ShellListView.Selected <> nil then |
| 127 | + editFilename.Text := ShellListView.Selected.Caption |
| 128 | + else |
| 129 | + editFilename.Text := ''; |
| 130 | +end; |
| 131 | + |
| 132 | +procedure TfrmExtFileDialog.ShellListViewDblClick(Sender: TObject); |
| 133 | +begin |
| 134 | + ModalResult := mrOK; |
| 135 | +end; |
| 136 | + |
| 137 | +procedure TfrmExtFileDialog.ShellListViewSelectItem(Sender: TObject; |
| 138 | + Item: TListItem; Selected: Boolean); |
| 139 | +var |
| 140 | + ListItem: TListItem; |
| 141 | +begin |
| 142 | + FFiles.Clear; |
| 143 | + for ListItem in ShellListView.Items do begin |
| 144 | + if ListItem.Selected then begin |
| 145 | + FFiles.Add(ShellListView.GetPathFromItem(ListItem)); |
| 146 | + end; |
| 147 | + end; |
| 148 | +end; |
| 149 | + |
| 150 | +function TfrmExtFileDialog.GetFileName: String; |
| 151 | +begin |
| 152 | + if ShellListView.Selected <> nil then |
| 153 | + Result := ShellListView.GetPathFromItem(ShellListView.Selected) |
| 154 | + else |
| 155 | + Result := ''; |
| 156 | +end; |
| 157 | + |
| 158 | +procedure TfrmExtFileDialog.SetFileName(const AValue: String); |
| 159 | +var |
| 160 | + fn: String; |
| 161 | +begin |
| 162 | + fn := ExpandFileName(AValue); |
| 163 | + ShellTreeView.Path := ExtractFilePath(fn); |
| 164 | + editFilename.Text := ExtractFileName(fn); |
| 165 | + ShellListView.Selected := ShellListView.FindCaption(0, fn, false, true, true); |
| 166 | +end; |
| 167 | + |
| 168 | +procedure TfrmExtFileDialog.SetInitialDir(const AValue: String); |
| 169 | +begin |
| 170 | + ShellTreeView.Path := AValue; |
| 171 | +end; |
| 172 | + |
| 173 | +end. |
| 174 | + |
0 commit comments