home *** CD-ROM | disk | FTP | other *** search
- unit uSelectDir;
- // pulled out the select directory code and modified it so I can show a title
- interface
- uses filectrl;
-
- function MySelectDirectory(const aTitle : string; var Directory: string;
- Options: TSelectDirOpts; HelpCtx: Longint): Boolean;
-
- function SlashSep(const Path, S: String): String;
-
- implementation
- uses Windows, Messages, SysUtils, Classes, Controls, Graphics, Forms,
- Menus, StdCtrls, Buttons, dialogs;
-
- function SlashSep(const Path, S: String): String;
- begin
- if AnsiLastChar(Path)^ <> '\' then
- Result := Path + '\' + S
- else
- Result := Path + S;
- end;
-
-
-
- type
- TPathLabel = class(TCustomLabel)
- protected
- procedure Paint; override;
- public
- constructor Create(AnOwner: TComponent); override;
- published
- property Alignment;
- property Transparent;
- end;
-
-
- { TSelectDirDlg }
- TSelectDirDlg = class(TForm)
- DirList: TDirectoryListBox;
- DirEdit: TEdit;
- DriveList: TDriveComboBox;
- DirLabel: TPathLabel;
- OKButton: TButton;
- CancelButton: TButton;
- HelpButton: TButton;
- NetButton: TButton;
- FileList: TFileListBox;
- procedure DirListChange(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure DriveListChange(Sender: TObject);
- procedure NetClick(Sender: TObject);
- procedure OKClick(Sender: TObject);
- procedure HelpButtonClick(Sender: TObject);
- private
- { Private declarations }
- FAllowCreate: Boolean;
- FPrompt: Boolean;
- WNetConnectDialog: function (WndParent: HWND; IType: Longint): Longint;
- procedure SetAllowCreate(Value: Boolean);
- procedure SetDirectory(const Value: string);
- function GetDirectory: string;
- public
- { Public declarations }
- constructor Create(AOwner: TComponent); override;
- property Directory: string read GetDirectory write SetDirectory;
- property AllowCreate: Boolean read FAllowCreate write SetAllowCreate default False;
- property Prompt: Boolean read FPrompt write FPrompt default False;
- end;
-
-
- { TPathLabel }
-
- constructor TPathLabel.Create(AnOwner: TComponent);
- begin
- inherited Create(AnOwner);
- WordWrap := False;
- AutoSize := False;
- ShowAccelChar := False;
- end;
-
- procedure TPathLabel.Paint;
- const
- Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
- var
- Rect: TRect;
- Temp: String;
- begin
- with Canvas do
- begin
- Rect := ClientRect;
- if not Transparent then
- begin
- Brush.Color := Self.Color;
- Brush.Style := bsSolid;
- FillRect(Rect);
- end;
- Brush.Style := bsClear;
- Temp := MinimizeName(Caption, Canvas, Rect.Right - Rect.Left);
- DrawText(Canvas.Handle, PChar(Temp), Length(Temp), Rect,
- DT_NOPREFIX or Alignments[Alignment]);
- end;
- end;
-
-
-
- { TSelectDirDlg }
- constructor TSelectDirDlg.Create(AOwner: TComponent);
- begin
- inherited CreateNew(AOwner);
- // Caption := SSelectDirCap;
- BorderStyle := bsDialog;
- ClientWidth := 424;
- ClientHeight := 255;
- Position := poScreenCenter;
-
- DirEdit := TEdit.Create(Self);
- with DirEdit do
- begin
- Parent := Self;
- SetBounds(8, 24, 313, 20);
- Visible := False;
- TabOrder := 1;
- end;
-
- with TLabel.Create(Self) do
- begin
- Parent := Self;
- SetBounds(8, 8, 92, 13);
- FocusControl := DirEdit;
- Caption := 'directories'; //SDirNameCap;
- end;
-
- DriveList := TDriveComboBox.Create(Self);
- with DriveList do
- begin
- Parent := Self;
- SetBounds(232, 192, 185, 19);
- TabOrder := 2;
- OnChange := DriveListChange;
- end;
-
- with TLabel.Create(Self) do
- begin
- Parent := Self;
- SetBounds(232, 176, 41, 13);
- Caption := 'Drives'; //SDrivesCap;
- FocusControl := DriveList;
- end;
-
- DirLabel := TPathLabel.Create(Self);
- with DirLabel do
- begin
- Parent := Self;
- SetBounds(120, 8, 213, 13);
- end;
-
- DirList := TDirectoryListBox.Create(Self);
- with DirList do
- begin
- Parent := Self;
- SetBounds(8, 72, 213, 138);
- TabOrder := 0;
- TabStop := True;
- ItemHeight := 17;
- IntegralHeight := True;
- OnChange := DirListChange;
- end;
-
- with TLabel.Create(Self) do
- begin
- Parent := Self;
- SetBounds(8, 56, 66, 13);
- Caption := 'Directories'; //SDirsCap;
- FocusControl := DirList;
- end;
-
- FileList := TFileListBox.Create(Self);
- with FileList do
- begin
- Parent := Self;
- SetBounds(232, 72, 185, 93);
- TabOrder := 6;
- TabStop := True;
- FileType := [ftNormal];
- Mask := '*.*';
- Font.Color := clGrayText;
- ItemHeight := 13;
- end;
-
- with TLabel.Create(Self) do
- begin
- Parent := Self;
- SetBounds(232, 56, 57, 13);
- Caption := 'Files';//SFilesCap;
- FocusControl := FileList;
- end;
-
- NetButton := TButton.Create(Self);
- with NetButton do
- begin
- Parent := Self;
- SetBounds(8, 224, 77, 27);
- Visible := False;
- TabOrder := 3;
- Caption := 'Network';//SNetworkCap;
- OnClick := NetClick;
- end;
-
- OKButton := TButton.Create(Self);
- with OKButton do
- begin
- Parent := Self;
- SetBounds(172, 224, 77, 27);
- TabOrder := 4;
- OnClick := OKClick;
- Caption := '&OK'; //SOKButton;
- ModalResult := 1;
- Default := True;
- end;
-
- CancelButton := TButton.Create(Self);
- with CancelButton do
- begin
- Parent := Self;
- SetBounds(256, 224, 77, 27);
- TabOrder := 5;
- Cancel := True;
- Caption := '&Cancel';//SCancelButton;
- ModalResult := 2;
- end;
-
- HelpButton := TButton.Create(Self);
- with HelpButton do
- begin
- Parent := Self;
- SetBounds(340, 224, 77, 27);
- TabOrder := 7;
- Caption := '&Help';//SHelpButton;
- OnClick := HelpButtonClick;
- end;
-
- FormCreate(Self);
- ActiveControl := DirList;
- end;
-
- procedure TSelectDirDlg.HelpButtonClick(Sender: TObject);
- begin
- Application.HelpContext(HelpContext);
- end;
-
- procedure TSelectDirDlg.DirListChange(Sender: TObject);
- begin
- DirLabel.Caption := DirList.Directory;
- FileList.Directory := DirList.Directory;
- DirEdit.Text := DirLabel.Caption;
- DirEdit.SelectAll;
- end;
-
- procedure TSelectDirDlg.FormCreate(Sender: TObject);
- var
- UserHandle: THandle;
- NetDriver: THandle;
- WNetGetCaps: function (Flags: Word): Word;
- begin
- { is network access enabled? }
- UserHandle := GetModuleHandle(User32);
- @WNetGetCaps := GetProcAddress(UserHandle, 'WNETGETCAPS');
- if @WNetGetCaps <> nil then
- begin
- NetDriver := WNetGetCaps(Word(-1));
- if NetDriver <> 0 then
- begin
- @WNetConnectDialog := GetProcAddress(NetDriver, 'WNETCONNECTDIALOG');
- NetButton.Visible := @WNetConnectDialog <> nil;
- end;
- end;
-
- FAllowCreate := False;
- DirLabel.BoundsRect := DirEdit.BoundsRect;
- DirListChange(Self);
- end;
-
- procedure TSelectDirDlg.DriveListChange(Sender: TObject);
- begin
- DirList.Drive := DriveList.Drive;
- end;
-
- procedure TSelectDirDlg.SetAllowCreate(Value: Boolean);
- begin
- if Value <> FAllowCreate then
- begin
- FAllowCreate := Value;
- DirLabel.Visible := not FAllowCreate;
- DirEdit.Visible := FAllowCreate;
- end;
- end;
-
- procedure TSelectDirDlg.SetDirectory(const Value: string);
- var
- Temp: string;
- begin
- if Value <> '' then
- begin
- Temp := ExpandFileName(SlashSep(Value,'*.*'));
- if (Length(Temp) >= 3) and (Temp[2] = ':') then
- begin
- DriveList.Drive := Temp[1];
- Temp := ExtractFilePath(Temp);
- try
- DirList.Directory := Copy(Temp, 1, Length(Temp) - 1);
- except
- on EInOutError do
- begin
- GetDir(0, Temp);
- DriveList.Drive := Temp[1];
- DirList.Directory := Temp;
- end;
- end;
- end;
- end;
- end;
-
- function TSelectDirDlg.GetDirectory: string;
- begin
- if FAllowCreate then
- Result := DirEdit.Text
- else
- Result := DirLabel.Caption;
- end;
-
- procedure TSelectDirDlg.NetClick(Sender: TObject);
- begin
- if Assigned(WNetConnectDialog) then
- WNetConnectDialog(Handle, WNTYPE_DRIVE);
- end;
-
- procedure TSelectDirDlg.OKClick(Sender: TObject);
- begin
- if AllowCreate and Prompt and (not DirectoryExists(Directory)) and
- (MessageDlg('Create this new directory?', mtConfirmation, [mbYes, mbNo],
- 0) <> mrYes) then
- ModalResult := 0;
- end;
-
- function MySelectDirectory(const aTitle : string; var Directory: string;
- Options: TSelectDirOpts; HelpCtx: Longint): Boolean;
- // direct copy of filectrl execpt added prompt
- var
- D: TSelectDirDlg;
- begin
- D := TSelectDirDlg.Create(Application);
- d.caption := aTitle;
- try
- D.Directory := Directory;
- D.AllowCreate := sdAllowCreate in Options;
- D.Prompt := sdPrompt in Options;
-
- { scale to screen res }
- if Screen.PixelsPerInch <> 96 then
- begin
- D.ScaleBy(Screen.PixelsPerInch, 96);
- D.FileList.ParentFont := True;
- D.Left := (Screen.Width div 2) - (D.Width div 2);
- D.Top := (Screen.Height div 2) - (D.Height div 2);
- D.FileList.Font.Color := clGrayText;
- end;
-
- if HelpCtx = 0 then
- begin
- D.HelpButton.Visible := False;
- D.OKButton.Left := D.CancelButton.Left;
- D.CancelButton.Left := D.HelpButton.Left;
- end
- else D.HelpContext := HelpCtx;
-
- Result := D.ShowModal = mrOK;
- if Result then
- begin
- Directory := D.Directory;
- if sdPerformCreate in Options then
- ForceDirectories(Directory);
- end;
- finally
- D.Free;
- end;
- end;
-
-
-
- end.
-