| 1 | Unit FileDialogForm;
|
|---|
| 2 |
|
|---|
| 3 | // NewView - a new OS/2 Help Viewer
|
|---|
| 4 | // Copyright 2003 Aaron Lawrence (aaronl at consultant dot com)
|
|---|
| 5 | // This software is released under the Gnu Public License - see readme.txt
|
|---|
| 6 |
|
|---|
| 7 | Interface
|
|---|
| 8 |
|
|---|
| 9 | // This form is a generic, sizeable file dialog. I never
|
|---|
| 10 | // got round to making it a seperate thing. Better would be
|
|---|
| 11 | // to enhance the one in SPCC.
|
|---|
| 12 |
|
|---|
| 13 | Uses
|
|---|
| 14 | Classes,
|
|---|
| 15 | Forms,
|
|---|
| 16 | StdCtrls,
|
|---|
| 17 | Buttons,
|
|---|
| 18 | Messages,
|
|---|
| 19 | CustomFileControls,
|
|---|
| 20 | SplitBar,
|
|---|
| 21 | MultiColumnListBox,
|
|---|
| 22 | ACLLanguageUnit;
|
|---|
| 23 |
|
|---|
| 24 | // Note: filters have the form:
|
|---|
| 25 | // <Description 1>|<filename mask 1>|<Description 2>|<filename mask 2>...
|
|---|
| 26 |
|
|---|
| 27 | function DoSaveFileDialog( const Caption: string;
|
|---|
| 28 | const Filters: string;
|
|---|
| 29 | const DefaultFilename: string;
|
|---|
| 30 | var Directory: string;
|
|---|
| 31 | var Filename: string ): boolean;
|
|---|
| 32 |
|
|---|
| 33 | function DoOpenFileDialog( const Caption: string;
|
|---|
| 34 | const Filters: string;
|
|---|
| 35 | const DefaultFilename: string;
|
|---|
| 36 | var Directory: string;
|
|---|
| 37 | var Filename: string ): boolean;
|
|---|
| 38 |
|
|---|
| 39 | function DoOpenMultiFileDialog( const Caption: string;
|
|---|
| 40 | const Filters: string;
|
|---|
| 41 | const DefaultFilename: string;
|
|---|
| 42 | Var Directory: string;
|
|---|
| 43 | Var KeepCurrent: boolean;
|
|---|
| 44 | Filenames: TStrings ): boolean;
|
|---|
| 45 |
|
|---|
| 46 | Const
|
|---|
| 47 | WM_CHECKFOCUS = WM_USER + 103;
|
|---|
| 48 |
|
|---|
| 49 | Type
|
|---|
| 50 | TFileDialogForm = Class (TForm)
|
|---|
| 51 | FilenameEdit: TEdit;
|
|---|
| 52 | FileNameLabel: TLabel;
|
|---|
| 53 | KeepCurrentCheckBox: TCheckBox;
|
|---|
| 54 | CompletionsListBox: TListBox;
|
|---|
| 55 | FilesLabel: TLabel;
|
|---|
| 56 | FilterLabel: TLabel;
|
|---|
| 57 | DrivesLabel: TLabel;
|
|---|
| 58 | DirectoriesLabel: TLabel;
|
|---|
| 59 | DirectoryListBox: TCustomDirectoryListBox;
|
|---|
| 60 | DriveComboBox: TCustomDriveComboBox;
|
|---|
| 61 | HelpButton: TButton;
|
|---|
| 62 | FileListBox: TMultiColumnListBox;
|
|---|
| 63 | FilterComboBox: TCustomFilterComboBox;
|
|---|
| 64 | OKButton: TButton;
|
|---|
| 65 | CancelButton: TButton;
|
|---|
| 66 | SplitBar: TSplitBar;
|
|---|
| 67 | Procedure FileDialogFormOnSetupShow (Sender: TObject);
|
|---|
| 68 | Procedure FilterComboBoxOnChange (Sender: TObject);
|
|---|
| 69 | Procedure DirectoryListBoxOnChange (Sender: TObject);
|
|---|
| 70 | Procedure CancelButtonOnEnter (Sender: TObject);
|
|---|
| 71 | Procedure OKButtonOnEnter (Sender: TObject);
|
|---|
| 72 | Procedure FilterComboBoxOnEnter (Sender: TObject);
|
|---|
| 73 | Procedure FileListBoxOnEnter (Sender: TObject);
|
|---|
| 74 | Procedure DirectoryListBoxOnEnter (Sender: TObject);
|
|---|
| 75 | Procedure DriveComboBoxOnEnter (Sender: TObject);
|
|---|
| 76 | Procedure CompletionsListBoxOnExit (Sender: TObject);
|
|---|
| 77 | Procedure FilenameEditOnChange (Sender: TObject);
|
|---|
| 78 | Procedure FilenameEditOnExit (Sender: TObject);
|
|---|
| 79 | Procedure FilenameEditOnScan (Sender: TObject; Var KeyCode: TKeyCode);
|
|---|
| 80 | Procedure CompletionsListBoxOnScan (Sender: TObject;
|
|---|
| 81 | Var KeyCode: TKeyCode);
|
|---|
| 82 | Procedure FileListBoxOnItemSelect (Sender: TObject; Index: LongInt);
|
|---|
| 83 | Procedure FileDialogFormOnDismissDlg (Sender: TObject);
|
|---|
| 84 | Procedure FileDialogFormOnResize (Sender: TObject);
|
|---|
| 85 | Procedure SplitBarOnChange (NewSplit: LongInt);
|
|---|
| 86 | Procedure FileListBoxOnDblClick (Sender: TObject);
|
|---|
| 87 | Procedure FileDialogFormOnShow (Sender: TObject);
|
|---|
| 88 | Procedure FileDialogFormOnDestroy (Sender: TObject);
|
|---|
| 89 | Procedure FileListBoxOnItemFocus (Sender: TObject; Index: LongInt);
|
|---|
| 90 | Procedure OKButtonOnClick (Sender: TObject);
|
|---|
| 91 | Procedure FileDialogFormOnCreate (Sender: TObject);
|
|---|
| 92 | Protected
|
|---|
| 93 |
|
|---|
| 94 | Split: real;
|
|---|
| 95 | Filenames: TStringList;
|
|---|
| 96 | RequireFileExists: boolean;
|
|---|
| 97 | DefaultFilename: string;
|
|---|
| 98 | FocussingFile: boolean;
|
|---|
| 99 | FileMask: string;
|
|---|
| 100 | Procedure LayoutControls;
|
|---|
| 101 | Procedure ShowCompletions;
|
|---|
| 102 | Procedure ReadFiles;
|
|---|
| 103 |
|
|---|
| 104 | Protected
|
|---|
| 105 | Procedure OnLanguageEvent( Language: TLanguageFile;
|
|---|
| 106 | const Apply: boolean );
|
|---|
| 107 | InvalidFilterErrorTitle: string;
|
|---|
| 108 | InvalidFilterError: string;
|
|---|
| 109 | FileNotFoundErrorTitle: string;
|
|---|
| 110 | FileNotFoundError: string;
|
|---|
| 111 | MultiSelectErrorTitle: string;
|
|---|
| 112 | MultiSelectError: string;
|
|---|
| 113 |
|
|---|
| 114 | Public
|
|---|
| 115 | Procedure WMCheckFocus( Var Msg: TMessage ); message WM_CHECKFOCUS;
|
|---|
| 116 | End;
|
|---|
| 117 |
|
|---|
| 118 | Implementation
|
|---|
| 119 |
|
|---|
| 120 | uses
|
|---|
| 121 | BseDos,
|
|---|
| 122 | OS2Def,
|
|---|
| 123 | PmWin,
|
|---|
| 124 | SysUtils,
|
|---|
| 125 | FileUtilsUnit,
|
|---|
| 126 | ACLDialogs,
|
|---|
| 127 | ACLString,
|
|---|
| 128 | AStringUtilityUnit,
|
|---|
| 129 | ControlsUtility,
|
|---|
| 130 | SettingsUnit,
|
|---|
| 131 | HelpFile,
|
|---|
| 132 | StringUtilsUnit,
|
|---|
| 133 | DebugUnit;
|
|---|
| 134 |
|
|---|
| 135 | Imports
|
|---|
| 136 | // Redeclared since the Sibyl declaration passes text as a cstring :(
|
|---|
| 137 | Function _WinSetWindowText( ahwnd: HWND;
|
|---|
| 138 | pszText: pchar ): BOOL;
|
|---|
| 139 | APIENTRY;
|
|---|
| 140 | 'PMWIN' index 877;
|
|---|
| 141 |
|
|---|
| 142 | Function _WinQueryWindowText( ahwnd: HWND;
|
|---|
| 143 | cchBufferMax: LONG;
|
|---|
| 144 | pchBuffer: pchar ): LONG;
|
|---|
| 145 | APIENTRY;
|
|---|
| 146 | 'PMWIN' index 841;
|
|---|
| 147 |
|
|---|
| 148 | end;
|
|---|
| 149 |
|
|---|
| 150 | Procedure TFileDialogForm.FileDialogFormOnSetupShow (Sender: TObject);
|
|---|
| 151 | Begin
|
|---|
| 152 | ScaleForm( self, 8, 16 );
|
|---|
| 153 |
|
|---|
| 154 | // ScaleForm seems to misposition the combo-boxes, so we fix that here
|
|---|
| 155 | DriveComboBox.Bottom := DrivesLabel.Bottom - DriveComboBox.Height;
|
|---|
| 156 | FilterComboBox.Bottom := FilterLabel.Bottom - FilterComboBox.Height;
|
|---|
| 157 |
|
|---|
| 158 | FileNameEdit.YAlign := yaTop;
|
|---|
| 159 | FileNameEdit.xStretch := xsFrame;
|
|---|
| 160 | FileNameLabel.YAlign := yaTop;
|
|---|
| 161 |
|
|---|
| 162 | CompletionsListBox.YAlign := yaTop;
|
|---|
| 163 | CompletionsListBox.xStretch := xsFrame;
|
|---|
| 164 |
|
|---|
| 165 | FilterComboBox.YAlign := yaTop;
|
|---|
| 166 | FilterLabel.YAlign := yaTop;
|
|---|
| 167 | DriveComboBox.YAlign := yaTop;
|
|---|
| 168 | DrivesLabel.YAlign := yaTop;
|
|---|
| 169 |
|
|---|
| 170 | DirectoryListBox.YStretch := ysFrame;
|
|---|
| 171 | DirectoriesLabel.YAlign := yaTop;
|
|---|
| 172 | FileListBox.yStretch := ysFrame;
|
|---|
| 173 | FilesLabel.YAlign := yaTop;
|
|---|
| 174 | FileNames := TStringList.Create;
|
|---|
| 175 | SplitBar.YStretch := ysFrame;
|
|---|
| 176 |
|
|---|
| 177 | LayoutControls;
|
|---|
| 178 | FileListBox.SetColumnWidth( 0, Canvas.TextWidth('A_Suitably_Long_FileName.EXT'));
|
|---|
| 179 |
|
|---|
| 180 | End;
|
|---|
| 181 |
|
|---|
| 182 | Procedure TFileDialogForm.FilterComboBoxOnChange (Sender: TObject);
|
|---|
| 183 | Begin
|
|---|
| 184 | FileMask := FilterComboBox.Mask;
|
|---|
| 185 | FileNameEdit.Text := FileMask;
|
|---|
| 186 | ReadFiles;
|
|---|
| 187 | End;
|
|---|
| 188 |
|
|---|
| 189 | Procedure TFileDialogForm.DirectoryListBoxOnChange (Sender: TObject);
|
|---|
| 190 | Begin
|
|---|
| 191 | ReadFiles;
|
|---|
| 192 | End;
|
|---|
| 193 |
|
|---|
| 194 | Procedure TFileDialogForm.CancelButtonOnEnter (Sender: TObject);
|
|---|
| 195 | Begin
|
|---|
| 196 | CompletionsListBox.Hide;
|
|---|
| 197 | End;
|
|---|
| 198 |
|
|---|
| 199 | Procedure TFileDialogForm.OKButtonOnEnter (Sender: TObject);
|
|---|
| 200 | Begin
|
|---|
| 201 | CompletionsListBox.Hide;
|
|---|
| 202 | End;
|
|---|
| 203 |
|
|---|
| 204 | Procedure TFileDialogForm.FilterComboBoxOnEnter (Sender: TObject);
|
|---|
| 205 | Begin
|
|---|
| 206 | CompletionsListBox.Hide;
|
|---|
| 207 | End;
|
|---|
| 208 |
|
|---|
| 209 | Procedure TFileDialogForm.FileListBoxOnEnter (Sender: TObject);
|
|---|
| 210 | Begin
|
|---|
| 211 | CompletionsListBox.Hide;
|
|---|
| 212 | End;
|
|---|
| 213 |
|
|---|
| 214 | Procedure TFileDialogForm.DirectoryListBoxOnEnter (Sender: TObject);
|
|---|
| 215 | Begin
|
|---|
| 216 | CompletionsListBox.Hide;
|
|---|
| 217 | End;
|
|---|
| 218 |
|
|---|
| 219 | Procedure TFileDialogForm.DriveComboBoxOnEnter (Sender: TObject);
|
|---|
| 220 | Begin
|
|---|
| 221 | CompletionsListBox.Hide;
|
|---|
| 222 | End;
|
|---|
| 223 |
|
|---|
| 224 | Procedure TFileDialogForm.CompletionsListBoxOnExit (Sender: TObject);
|
|---|
| 225 | Begin
|
|---|
| 226 | End;
|
|---|
| 227 |
|
|---|
| 228 | Procedure TFileDialogForm.FilenameEditOnChange (Sender: TObject);
|
|---|
| 229 | Begin
|
|---|
| 230 | if FocussingFile then
|
|---|
| 231 | exit;
|
|---|
| 232 | ShowCompletions;
|
|---|
| 233 | End;
|
|---|
| 234 |
|
|---|
| 235 | Procedure TFileDialogForm.FilenameEditOnExit (Sender: TObject);
|
|---|
| 236 | Begin
|
|---|
| 237 | End;
|
|---|
| 238 |
|
|---|
| 239 | Procedure TFileDialogForm.WMCheckFocus( Var Msg: TMessage );
|
|---|
| 240 | begin
|
|---|
| 241 | end;
|
|---|
| 242 |
|
|---|
| 243 | Procedure TFileDialogForm.FilenameEditOnScan (Sender: TObject;
|
|---|
| 244 | Var KeyCode: TKeyCode);
|
|---|
| 245 | Begin
|
|---|
| 246 | if KeyCode = kbCDown then
|
|---|
| 247 | begin
|
|---|
| 248 | if CompletionsListBox.Visible then
|
|---|
| 249 | begin
|
|---|
| 250 | CompletionsListBox.ItemIndex := 0;
|
|---|
| 251 | CompletionsListBox.Focus;
|
|---|
| 252 | KeyCode := kbNull;
|
|---|
| 253 | end;
|
|---|
| 254 | end
|
|---|
| 255 | else if KeyCode = kbTab then
|
|---|
| 256 | begin
|
|---|
| 257 | DriveComboBox.Focus;
|
|---|
| 258 | end
|
|---|
| 259 | else
|
|---|
| 260 | begin
|
|---|
| 261 | ShowCompletions;
|
|---|
| 262 | end;
|
|---|
| 263 | End;
|
|---|
| 264 |
|
|---|
| 265 | Procedure TFileDialogForm.CompletionsListBoxOnScan (Sender: TObject;
|
|---|
| 266 | Var KeyCode: TKeyCode);
|
|---|
| 267 | Begin
|
|---|
| 268 | if KeyCode = kbCR then
|
|---|
| 269 | begin
|
|---|
| 270 | if CompletionsListBox.ItemIndex <> -1 then
|
|---|
| 271 | begin
|
|---|
| 272 | FilenameEdit.Text := CompletionsListBox.Items[ CompletionsListBox.ItemIndex ];
|
|---|
| 273 | FilenameEdit.Focus;
|
|---|
| 274 | ShowCompletions;
|
|---|
| 275 | KeyCode := kbNull; // eat the keystroke
|
|---|
| 276 | end;
|
|---|
| 277 | end
|
|---|
| 278 | else if KeyCode = kbCUp then
|
|---|
| 279 | begin
|
|---|
| 280 | if CompletionsListBox.ItemIndex = 0 then
|
|---|
| 281 | begin
|
|---|
| 282 | CompletionsListBox.ItemIndex := -1;
|
|---|
| 283 | FilenameEdit.Focus;
|
|---|
| 284 | KeyCode := kbNull;
|
|---|
| 285 | end;
|
|---|
| 286 | end;
|
|---|
| 287 | End;
|
|---|
| 288 |
|
|---|
| 289 | Procedure TFileDialogForm.ShowCompletions;
|
|---|
| 290 | var
|
|---|
| 291 | i: integer;
|
|---|
| 292 | search: string;
|
|---|
| 293 | filename: string;
|
|---|
| 294 | ShowList: boolean;
|
|---|
| 295 | tmpNameAndTitle: string;
|
|---|
| 296 | Begin
|
|---|
| 297 | CompletionsListBox.Items.Clear;
|
|---|
| 298 | search := FilenameEdit.Text;
|
|---|
| 299 |
|
|---|
| 300 | if Search <> '' then
|
|---|
| 301 | begin
|
|---|
| 302 | for i := 0 to FileListBox.Items.Count - 1 do
|
|---|
| 303 | begin
|
|---|
| 304 | tmpNameAndTitle := FileListBox.Items[ i ];
|
|---|
| 305 | Filename := StrLeftUntil(tmpNameAndTitle, [StrTAB]);
|
|---|
| 306 |
|
|---|
| 307 | if StrStartsWithIgnoringCase(filename, search) then
|
|---|
| 308 | CompletionsListBox.Items.Add( filename );
|
|---|
| 309 | end;
|
|---|
| 310 | end;
|
|---|
| 311 |
|
|---|
| 312 | ShowList := false;
|
|---|
| 313 | if CompletionsListBox.Items.Count = 1 then
|
|---|
| 314 | begin
|
|---|
| 315 | if not StrEqualIgnoringCase(CompletionsListBox.Items[0], search) then
|
|---|
| 316 | ShowList := true;
|
|---|
| 317 | end
|
|---|
| 318 | else if CompletionsListBox.Items.Count > 0 then
|
|---|
| 319 | begin
|
|---|
| 320 | ShowList := true;
|
|---|
| 321 | end;
|
|---|
| 322 |
|
|---|
| 323 | if ShowList then
|
|---|
| 324 | begin
|
|---|
| 325 | CompletionsListBox.BringToFront;
|
|---|
| 326 | CompletionsListBox.Height := ( CompletionsListBox.Items.Count + 1 )
|
|---|
| 327 | * CompletionsListBox.ItemHeight
|
|---|
| 328 | + 6;
|
|---|
| 329 | CompletionsListBox.Bottom := FilenameEdit.Bottom
|
|---|
| 330 | - CompletionsListBox.Height;
|
|---|
| 331 | end;
|
|---|
| 332 |
|
|---|
| 333 | CompletionsListBox.Visible := ShowList;
|
|---|
| 334 |
|
|---|
| 335 | End;
|
|---|
| 336 |
|
|---|
| 337 | Procedure TFileDialogForm.FileListBoxOnItemSelect (Sender: TObject;
|
|---|
| 338 | Index: LongInt);
|
|---|
| 339 | Begin
|
|---|
| 340 | OKButton.Click;
|
|---|
| 341 | End;
|
|---|
| 342 |
|
|---|
| 343 | Procedure TFileDialogForm.FileDialogFormOnDismissDlg (Sender: TObject);
|
|---|
| 344 | Begin
|
|---|
| 345 | WriteWindowPos( self );
|
|---|
| 346 | Settings.FileDialogSplit := Split;
|
|---|
| 347 | End;
|
|---|
| 348 |
|
|---|
| 349 | Procedure TFileDialogForm.FileDialogFormOnResize (Sender: TObject);
|
|---|
| 350 | Begin
|
|---|
| 351 | LayoutControls;
|
|---|
| 352 | End;
|
|---|
| 353 |
|
|---|
| 354 | Procedure TFileDialogForm.SplitBarOnChange (NewSplit: LongInt);
|
|---|
| 355 | Begin
|
|---|
| 356 | Split := NewSplit / ClientWidth;
|
|---|
| 357 |
|
|---|
| 358 | if Split < 0.2 then
|
|---|
| 359 | Split := 0.2;
|
|---|
| 360 | if Split > 0.8 then
|
|---|
| 361 | Split := 0.8;
|
|---|
| 362 |
|
|---|
| 363 | LayoutControls;
|
|---|
| 364 | End;
|
|---|
| 365 |
|
|---|
| 366 | Procedure TFileDialogForm.LayoutControls;
|
|---|
| 367 | var
|
|---|
| 368 | SplitX: longint;
|
|---|
| 369 | LeftPaneWidth: longint;
|
|---|
| 370 | RightPaneWidth: longint;
|
|---|
| 371 | RightPaneX: longint;
|
|---|
| 372 | begin
|
|---|
| 373 | SplitX := round( ClientWidth * Split );
|
|---|
| 374 | LeftPaneWidth := SplitX - 8; // note we are not including borders here
|
|---|
| 375 | RightPaneWidth := ClientWidth - SplitX - 9;
|
|---|
| 376 | RightPaneX := SplitX + 4;
|
|---|
| 377 |
|
|---|
| 378 | DrivesLabel.Width := LeftPaneWidth;
|
|---|
| 379 | DriveComboBox.Width := LeftPaneWidth;
|
|---|
| 380 |
|
|---|
| 381 | DirectoriesLabel.Width := LeftPaneWidth;
|
|---|
| 382 | DirectoryListBox.Width := LeftPaneWidth;
|
|---|
| 383 |
|
|---|
| 384 | FilesLabel.Left := RightPaneX;
|
|---|
| 385 | FilesLabel.Width := RightPaneWidth;
|
|---|
| 386 | FileListBox.Left := RightPaneX;
|
|---|
| 387 | FileListBox.Width := RightPaneWidth;
|
|---|
| 388 |
|
|---|
| 389 | FilterLabel.Left := RightPaneX;
|
|---|
| 390 | FilterLabel.Width := RightPaneWidth;
|
|---|
| 391 | FilterComboBox.Left := RightPaneX;
|
|---|
| 392 | FilterComboBox.Width := RightPaneWidth;
|
|---|
| 393 |
|
|---|
| 394 | SplitBar.Left := SplitX - 3;
|
|---|
| 395 | end;
|
|---|
| 396 |
|
|---|
| 397 | Procedure TFileDialogForm.FileListBoxOnDblClick (Sender: TObject);
|
|---|
| 398 | Begin
|
|---|
| 399 | OKButton.Click;
|
|---|
| 400 | End;
|
|---|
| 401 |
|
|---|
| 402 | Procedure TFileDialogForm.FileDialogFormOnShow (Sender: TObject);
|
|---|
| 403 | Begin
|
|---|
| 404 | Split := Settings.FileDialogSplit;
|
|---|
| 405 |
|
|---|
| 406 | ReadWindowPos( self );
|
|---|
| 407 |
|
|---|
| 408 | OKButton.Default := true;
|
|---|
| 409 |
|
|---|
| 410 | // get some more space in the edit field
|
|---|
| 411 | SendMsg( FilenameEdit.Handle,
|
|---|
| 412 | EM_SETTEXTLIMIT,
|
|---|
| 413 | 1024,
|
|---|
| 414 | 0 );
|
|---|
| 415 |
|
|---|
| 416 | FilenameEdit.Text := DefaultFilename;
|
|---|
| 417 | FilenameEdit.Focus;
|
|---|
| 418 |
|
|---|
| 419 | // re-read files
|
|---|
| 420 | ReadFiles;
|
|---|
| 421 |
|
|---|
| 422 | ShowCompletions;
|
|---|
| 423 |
|
|---|
| 424 | KeepCurrentCheckBox.Checked := false;
|
|---|
| 425 | End;
|
|---|
| 426 |
|
|---|
| 427 | Procedure TFileDialogForm.FileDialogFormOnDestroy (Sender: TObject);
|
|---|
| 428 | Begin
|
|---|
| 429 | FileNames.Destroy;
|
|---|
| 430 | End;
|
|---|
| 431 |
|
|---|
| 432 | Procedure TFileDialogForm.FileListBoxOnItemFocus (Sender: TObject;
|
|---|
| 433 | Index: LongInt);
|
|---|
| 434 | var
|
|---|
| 435 | FileIndex: longint;
|
|---|
| 436 | NameAndTitle: string;
|
|---|
| 437 | FilenamesString: TAString;
|
|---|
| 438 | Begin
|
|---|
| 439 | FileNames.Clear;
|
|---|
| 440 | for FileIndex := 0 to FileListBox.Items.Count - 1 do
|
|---|
| 441 | begin
|
|---|
| 442 | if FileListBox.Selected[ FileIndex ] then
|
|---|
| 443 | begin
|
|---|
| 444 | NameAndTitle := FileListBox.Items[ FileIndex ];
|
|---|
| 445 | FileNames.Add(StrLeftUntil(NameAndTitle, [StrTAB]));
|
|---|
| 446 | end;
|
|---|
| 447 | end;
|
|---|
| 448 | FocussingFile := true;
|
|---|
| 449 | FilenamesString := TAString.Create;
|
|---|
| 450 | ListToAString( Filenames, FilenamesString, '+' );
|
|---|
| 451 | _WinSetWindowText( FileNameEdit.Handle,
|
|---|
| 452 | FilenamesString.AsPChar );
|
|---|
| 453 | FilenamesString.Destroy;
|
|---|
| 454 | FocussingFile := false;
|
|---|
| 455 | End;
|
|---|
| 456 |
|
|---|
| 457 | Procedure TFileDialogForm.OKButtonOnClick (Sender: TObject);
|
|---|
| 458 | var
|
|---|
| 459 | FileNameText: string;
|
|---|
| 460 | FileName: string;
|
|---|
| 461 | Directory: string;
|
|---|
| 462 | NewDirectory: string;
|
|---|
| 463 | FilePath: string;
|
|---|
| 464 | FilenameString: TAString;
|
|---|
| 465 | i: longint;
|
|---|
| 466 | Begin
|
|---|
| 467 | FileNameText := trim( FileNameEdit.Text );
|
|---|
| 468 | if FileNameText = '' then
|
|---|
| 469 | exit;
|
|---|
| 470 |
|
|---|
| 471 | if ( Pos( '*', FileNameText ) > 0 )
|
|---|
| 472 | or ( Pos( '?', FileNameText ) > 0 ) then
|
|---|
| 473 | begin
|
|---|
| 474 | if ( Pos( '\', FileNameText ) > 0 )
|
|---|
| 475 | or ( Pos( ':', FileNameText ) > 0 )
|
|---|
| 476 | or ( Pos( '/', FileNameText ) > 0 )
|
|---|
| 477 | then
|
|---|
| 478 | begin
|
|---|
| 479 | DoErrorDlg( InvalidFilterErrorTitle,
|
|---|
| 480 | StrInDoubleQuotes(FileNameText)
|
|---|
| 481 | + InvalidFilterError
|
|---|
| 482 | + StrCRLF
|
|---|
| 483 | + ' \ / :' );
|
|---|
| 484 | exit;
|
|---|
| 485 | end;
|
|---|
| 486 |
|
|---|
| 487 | // Treat as a filter
|
|---|
| 488 | FileMask := FileNameText;
|
|---|
| 489 | ReadFiles;
|
|---|
| 490 |
|
|---|
| 491 | exit;
|
|---|
| 492 | end;
|
|---|
| 493 |
|
|---|
| 494 | // First, see if it's a directory to change to
|
|---|
| 495 | // (in order to support typing directories, either full or relative)
|
|---|
| 496 | Directory := DirectoryListBox.Directory;
|
|---|
| 497 | NewDirectory := ExpandPath( Directory, FileNameText );
|
|---|
| 498 |
|
|---|
| 499 | DosErrorAPI( FERR_DISABLEHARDERR );
|
|---|
| 500 |
|
|---|
| 501 | if DirectoryExists( NewDirectory ) then
|
|---|
| 502 | begin
|
|---|
| 503 | // Yes, the typed text is a directory, so change to it.
|
|---|
| 504 | DirectoryListBox.Directory:= NewDirectory;
|
|---|
| 505 | FileNameEdit.Text := '';
|
|---|
| 506 | DosErrorAPI( FERR_ENABLEHARDERR );
|
|---|
| 507 |
|
|---|
| 508 | exit;
|
|---|
| 509 | end;
|
|---|
| 510 |
|
|---|
| 511 | // No, the text entered is a filename or set of filenames
|
|---|
| 512 | // Break it up into individual filenames at '+' char
|
|---|
| 513 | // Check each file exists
|
|---|
| 514 |
|
|---|
| 515 | FilenameString := TAString.Create;
|
|---|
| 516 | FilenameString.Length := WinQueryWindowTextLength( FileNameEdit.Handle );
|
|---|
| 517 | _WinQueryWindowText( FileNameEdit.Handle,
|
|---|
| 518 | FilenameString.Length + 1, // allow zero term
|
|---|
| 519 | FilenameString.AsPChar );
|
|---|
| 520 |
|
|---|
| 521 | FileNames.Clear;
|
|---|
| 522 |
|
|---|
| 523 | AStringToList( FilenameString, Filenames, '+' );
|
|---|
| 524 |
|
|---|
| 525 | for i := 0 to Filenames.Count - 1 do
|
|---|
| 526 | begin
|
|---|
| 527 | FileName := Filenames[ i ];
|
|---|
| 528 |
|
|---|
| 529 | // Work out directory
|
|---|
| 530 | FilePath := ExtractFilePath( FileName );
|
|---|
| 531 | FilePath := ExpandPath( Directory, FilePath );
|
|---|
| 532 |
|
|---|
| 533 | FileName := AddDirectorySeparator( FilePath )
|
|---|
| 534 | + ExtractFileName( FileName );
|
|---|
| 535 | if RequireFileExists then
|
|---|
| 536 | begin
|
|---|
| 537 | if not FileExists( FileName ) then
|
|---|
| 538 | begin
|
|---|
| 539 | DoErrorDlg( FileNotFoundErrorTitle,
|
|---|
| 540 | FileNotFoundError + Filename );
|
|---|
| 541 | DosErrorAPI( FERR_ENABLEHARDERR );
|
|---|
| 542 | FilenameString.Destroy;
|
|---|
| 543 | exit;
|
|---|
| 544 | end;
|
|---|
| 545 | end;
|
|---|
| 546 | FileNames[ i ] := FileName;
|
|---|
| 547 | end;
|
|---|
| 548 |
|
|---|
| 549 | FilenameString.Destroy;
|
|---|
| 550 |
|
|---|
| 551 | DosErrorAPI( FERR_ENABLEHARDERR );
|
|---|
| 552 |
|
|---|
| 553 | if not FilelistBox.MultiSelect then
|
|---|
| 554 | begin
|
|---|
| 555 | if FileNames.Count > 1 then
|
|---|
| 556 | begin
|
|---|
| 557 | DoErrorDlg( MultiSelectErrorTitle,
|
|---|
| 558 | MultiSelectError );
|
|---|
| 559 | exit;
|
|---|
| 560 | end;
|
|---|
| 561 | end;
|
|---|
| 562 | // Done
|
|---|
| 563 | DismissDlg( mrOK );
|
|---|
| 564 | End;
|
|---|
| 565 |
|
|---|
| 566 | Procedure TFileDialogForm.OnLanguageEvent( Language: TLanguageFile;
|
|---|
| 567 | const Apply: boolean );
|
|---|
| 568 | begin
|
|---|
| 569 | LogEvent(LogI18n, 'TFileDialogForm.OnLanguageEvent apply: "' + BoolToStr(Apply) + '"');
|
|---|
| 570 | Language.LoadComponentLanguage( self, Apply );
|
|---|
| 571 |
|
|---|
| 572 | Language.LL( Apply, InvalidFilterErrorTitle, 'InvalidFilterErrorTitle', 'File Filter Error' );
|
|---|
| 573 | Language.LL( Apply,
|
|---|
| 574 | InvalidFilterError,
|
|---|
| 575 | 'InvalidFilterError',
|
|---|
| 576 | ' is not a valid filename filter. '
|
|---|
| 577 | + 'You cannot use any of these characters: ' );
|
|---|
| 578 | Language.LL( Apply, FileNotFoundErrorTitle, 'FileNotFoundErrorTitle', 'File Not Found' );
|
|---|
| 579 | Language.LL( Apply, FileNotFoundError, 'FileNotFoundError', 'File does not exist:' );
|
|---|
| 580 | Language.LL( Apply, MultiSelectErrorTitle, 'MultiSelectErrorTitle', 'Multi-Select' );
|
|---|
| 581 | Language.LL( Apply, MultiSelectError, 'MultiSelectError', 'You can only select one file' );
|
|---|
| 582 | end;
|
|---|
| 583 |
|
|---|
| 584 | Procedure TFileDialogForm.FileDialogFormOnCreate (Sender: TObject);
|
|---|
| 585 | Begin
|
|---|
| 586 | RegisterForLanguages( OnLanguageEvent );
|
|---|
| 587 |
|
|---|
| 588 | FileMask := '*.*';
|
|---|
| 589 | End;
|
|---|
| 590 |
|
|---|
| 591 | Procedure TFileDialogForm.ReadFiles;
|
|---|
| 592 | var
|
|---|
| 593 | Filenames: TStringList;
|
|---|
| 594 | i: longint;
|
|---|
| 595 |
|
|---|
| 596 | Filename: string;
|
|---|
| 597 | Title: string;
|
|---|
| 598 | begin
|
|---|
| 599 | Filenames := TStringList.Create;
|
|---|
| 600 |
|
|---|
| 601 | DosErrorAPI( FERR_DISABLEHARDERR );
|
|---|
| 602 |
|
|---|
| 603 | ListFilesInDirectory( DirectoryListBox.Directory,
|
|---|
| 604 | FileMask,
|
|---|
| 605 | false,
|
|---|
| 606 | Filenames);
|
|---|
| 607 |
|
|---|
| 608 | Filenames.Sort;
|
|---|
| 609 |
|
|---|
| 610 | FileListBox.Items.BeginUpdate;
|
|---|
| 611 | FileListBox.Items.Clear;
|
|---|
| 612 |
|
|---|
| 613 | for i := 0 to Filenames.Count - 1 do
|
|---|
| 614 | begin
|
|---|
| 615 | Filename := Filenames[ i ];
|
|---|
| 616 |
|
|---|
| 617 | Title := GetHelpFileTitle( AddDirectorySeparator( DirectoryListBox.Directory )
|
|---|
| 618 | + Filename );
|
|---|
| 619 |
|
|---|
| 620 | FileListBox.Items.Add(Filename + StrTAB + Title );
|
|---|
| 621 | end;
|
|---|
| 622 | FileListBox.Items.EndUpdate;
|
|---|
| 623 |
|
|---|
| 624 | DosErrorAPI( FERR_ENABLEHARDERR );
|
|---|
| 625 |
|
|---|
| 626 | Filenames.Destroy;
|
|---|
| 627 | end;
|
|---|
| 628 |
|
|---|
| 629 | // ----------------------------------------------------------------------
|
|---|
| 630 |
|
|---|
| 631 | Var
|
|---|
| 632 | FileDialogForm: TFileDialogForm;
|
|---|
| 633 |
|
|---|
| 634 | procedure EnsureFileDialogFormLoaded;
|
|---|
| 635 | begin
|
|---|
| 636 | if FileDialogForm = nil then
|
|---|
| 637 | FileDialogForm := TFileDialogForm.Create( nil );
|
|---|
| 638 | end;
|
|---|
| 639 |
|
|---|
| 640 | function DoSaveFileDialog( const Caption: string;
|
|---|
| 641 | const Filters: string;
|
|---|
| 642 | const DefaultFilename: string;
|
|---|
| 643 | Var Directory: string;
|
|---|
| 644 | Var Filename: string ): boolean;
|
|---|
| 645 | begin
|
|---|
| 646 | EnsureFileDialogFormLoaded;
|
|---|
| 647 |
|
|---|
| 648 | FileDialogForm.Caption := Caption;
|
|---|
| 649 | FileDialogForm.FilelistBox.MultiSelect := false;
|
|---|
| 650 | FileDialogForm.RequireFileExists := false;
|
|---|
| 651 | FileDialogForm.FilterComboBox.Filter := Filters;
|
|---|
| 652 | FileDialogForm.DirectoryListBox.Directory := Directory;
|
|---|
| 653 | FileDialogForm.DefaultFilename := DefaultFilename;
|
|---|
| 654 | FileDialogForm.KeepCurrentCheckBox.Visible := false;
|
|---|
| 655 |
|
|---|
| 656 | Result := FileDialogForm.ShowModal = mrOK;
|
|---|
| 657 |
|
|---|
| 658 | if Result then
|
|---|
| 659 | begin
|
|---|
| 660 | Directory := FileDialogForm.DirectoryListBox.Directory;
|
|---|
| 661 | Filename := FileDialogForm.Filenames[ 0 ];
|
|---|
| 662 | end;
|
|---|
| 663 |
|
|---|
| 664 | end;
|
|---|
| 665 |
|
|---|
| 666 | function DoOpenFileDialog( const Caption: string;
|
|---|
| 667 | const Filters: string;
|
|---|
| 668 | const DefaultFilename: string;
|
|---|
| 669 | Var Directory: string;
|
|---|
| 670 | Var Filename: string ): boolean;
|
|---|
| 671 | begin
|
|---|
| 672 | EnsureFileDialogFormLoaded;
|
|---|
| 673 |
|
|---|
| 674 | FileDialogForm.Caption := Caption;
|
|---|
| 675 | FileDialogForm.FilelistBox.MultiSelect := false;
|
|---|
| 676 | FileDialogForm.RequireFileExists := true;
|
|---|
| 677 | FileDialogForm.FilterComboBox.Filter := Filters;
|
|---|
| 678 | FileDialogForm.DirectoryListBox.Directory := Directory;
|
|---|
| 679 | FileDialogForm.DefaultFilename := DefaultFilename;
|
|---|
| 680 | FileDialogForm.KeepCurrentCheckBox.Visible := false;
|
|---|
| 681 |
|
|---|
| 682 | Result := FileDialogForm.ShowModal = mrOK;
|
|---|
| 683 |
|
|---|
| 684 | if Result then
|
|---|
| 685 | begin
|
|---|
| 686 | Directory := FileDialogForm.DirectoryListBox.Directory;
|
|---|
| 687 | Filename := FileDialogForm.Filenames[ 0 ];
|
|---|
| 688 | end;
|
|---|
| 689 | end;
|
|---|
| 690 |
|
|---|
| 691 | function DoOpenMultiFileDialog( const Caption: string;
|
|---|
| 692 | const Filters: string;
|
|---|
| 693 | const DefaultFilename: string;
|
|---|
| 694 | Var Directory: string;
|
|---|
| 695 | Var KeepCurrent: boolean;
|
|---|
| 696 | Filenames: TStrings ): boolean;
|
|---|
| 697 | begin
|
|---|
| 698 | EnsureFileDialogFormLoaded;
|
|---|
| 699 |
|
|---|
| 700 | FileDialogForm.Caption := Caption;
|
|---|
| 701 | FileDialogForm.FilelistBox.MultiSelect := true;
|
|---|
| 702 | FileDialogForm.RequireFileExists := true;
|
|---|
| 703 | FileDialogForm.FilterComboBox.Filter := Filters;
|
|---|
| 704 | FileDialogForm.DirectoryListBox.Directory := Directory;
|
|---|
| 705 | FileDialogForm.DefaultFilename := DefaultFilename;
|
|---|
| 706 |
|
|---|
| 707 | FileDialogForm.KeepCurrentCheckBox.Checked := KeepCurrent;
|
|---|
| 708 | FileDialogForm.KeepCurrentCheckBox.Visible := true;
|
|---|
| 709 |
|
|---|
| 710 | Result := FileDialogForm.ShowModal = mrOK;
|
|---|
| 711 |
|
|---|
| 712 | if Result then
|
|---|
| 713 | begin
|
|---|
| 714 | Directory := FileDialogForm.DirectoryListBox.Directory;
|
|---|
| 715 | Filenames.Assign( FileDialogForm.Filenames );
|
|---|
| 716 | KeepCurrent := FileDialogForm.KeepCurrentCheckBox.Checked;
|
|---|
| 717 | end;
|
|---|
| 718 | end;
|
|---|
| 719 |
|
|---|
| 720 | Initialization
|
|---|
| 721 | RegisterClasses ([TFileDialogForm, TEdit, TLabel,
|
|---|
| 722 | TCustomDirectoryListBox, TCustomDriveComboBox,
|
|---|
| 723 | TCustomFilterComboBox, TSplitBar, TButton,
|
|---|
| 724 | TListBox, TMultiColumnListBox, TCheckBox]);
|
|---|
| 725 |
|
|---|
| 726 | RegisterUpdateProcForLanguages( EnsureFileDialogFormLoaded );
|
|---|
| 727 | End.
|
|---|