home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue66 / Construc / Refactor / Source / uSelectDir.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-10-12  |  9.3 KB  |  391 lines

  1. unit uSelectDir;
  2. // pulled out the select directory code and modified it so I can show a title
  3. interface
  4. uses filectrl;
  5.  
  6. function MySelectDirectory(const aTitle : string; var Directory: string;
  7.   Options: TSelectDirOpts; HelpCtx: Longint): Boolean;
  8.  
  9. function SlashSep(const Path, S: String): String;
  10.  
  11. implementation
  12. uses Windows, Messages, SysUtils, Classes, Controls, Graphics, Forms,
  13.   Menus, StdCtrls, Buttons, dialogs;
  14.  
  15. function SlashSep(const Path, S: String): String;
  16. begin
  17.   if AnsiLastChar(Path)^ <> '\' then
  18.     Result := Path + '\' + S
  19.   else
  20.     Result := Path + S;
  21. end;
  22.  
  23.  
  24.  
  25. type
  26.   TPathLabel = class(TCustomLabel)
  27.   protected
  28.     procedure Paint; override;
  29.   public
  30.     constructor Create(AnOwner: TComponent); override;
  31.   published
  32.     property Alignment;
  33.     property Transparent;
  34.   end;
  35.  
  36.  
  37. { TSelectDirDlg }
  38.   TSelectDirDlg = class(TForm)
  39.     DirList: TDirectoryListBox;
  40.     DirEdit: TEdit;
  41.     DriveList: TDriveComboBox;
  42.     DirLabel: TPathLabel;
  43.     OKButton: TButton;
  44.     CancelButton: TButton;
  45.     HelpButton: TButton;
  46.     NetButton: TButton;
  47.     FileList: TFileListBox;
  48.     procedure DirListChange(Sender: TObject);
  49.     procedure FormCreate(Sender: TObject);
  50.     procedure DriveListChange(Sender: TObject);
  51.     procedure NetClick(Sender: TObject);
  52.     procedure OKClick(Sender: TObject);
  53.     procedure HelpButtonClick(Sender: TObject);
  54.   private
  55.     { Private declarations }
  56.     FAllowCreate: Boolean;
  57.     FPrompt: Boolean;
  58.     WNetConnectDialog: function (WndParent: HWND; IType: Longint): Longint;
  59.     procedure SetAllowCreate(Value: Boolean);
  60.     procedure SetDirectory(const Value: string);
  61.     function GetDirectory: string;
  62.   public
  63.     { Public declarations }
  64.     constructor Create(AOwner: TComponent); override;
  65.     property Directory: string read GetDirectory write SetDirectory;
  66.     property AllowCreate: Boolean read FAllowCreate write SetAllowCreate default False;
  67.     property Prompt: Boolean read FPrompt write FPrompt default False;
  68.   end;
  69.  
  70.  
  71. { TPathLabel }
  72.  
  73. constructor TPathLabel.Create(AnOwner: TComponent);
  74. begin
  75.   inherited Create(AnOwner);
  76.   WordWrap := False;
  77.   AutoSize := False;
  78.   ShowAccelChar := False;
  79. end;
  80.  
  81. procedure TPathLabel.Paint;
  82. const
  83.   Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
  84. var
  85.   Rect: TRect;
  86.   Temp: String;
  87. begin
  88.   with Canvas do
  89.   begin
  90.     Rect := ClientRect;
  91.     if not Transparent then
  92.     begin
  93.       Brush.Color := Self.Color;
  94.       Brush.Style := bsSolid;
  95.       FillRect(Rect);
  96.     end;
  97.     Brush.Style := bsClear;
  98.     Temp := MinimizeName(Caption, Canvas, Rect.Right - Rect.Left);
  99.     DrawText(Canvas.Handle, PChar(Temp), Length(Temp), Rect,
  100.       DT_NOPREFIX or Alignments[Alignment]);
  101.   end;
  102. end;
  103.  
  104.  
  105.  
  106. { TSelectDirDlg }
  107. constructor TSelectDirDlg.Create(AOwner: TComponent);
  108. begin
  109.   inherited CreateNew(AOwner);
  110. //  Caption := SSelectDirCap;
  111.   BorderStyle := bsDialog;
  112.   ClientWidth := 424;
  113.   ClientHeight := 255;
  114.   Position := poScreenCenter;
  115.  
  116.   DirEdit := TEdit.Create(Self);
  117.   with DirEdit do
  118.   begin
  119.     Parent := Self;
  120.     SetBounds(8, 24, 313, 20);
  121.     Visible := False;
  122.     TabOrder := 1;
  123.   end;
  124.  
  125.   with TLabel.Create(Self) do
  126.   begin
  127.     Parent := Self;
  128.     SetBounds(8, 8, 92, 13);
  129.     FocusControl := DirEdit;
  130.     Caption := 'directories'; //SDirNameCap;
  131.   end;
  132.  
  133.   DriveList := TDriveComboBox.Create(Self);
  134.   with DriveList do
  135.   begin
  136.     Parent := Self;
  137.     SetBounds(232, 192, 185, 19);
  138.     TabOrder := 2;
  139.     OnChange := DriveListChange;
  140.   end;
  141.  
  142.   with TLabel.Create(Self) do
  143.   begin
  144.     Parent := Self;
  145.     SetBounds(232, 176, 41, 13);
  146.     Caption := 'Drives'; //SDrivesCap;
  147.     FocusControl := DriveList;
  148.   end;
  149.  
  150.   DirLabel := TPathLabel.Create(Self);
  151.   with DirLabel do
  152.   begin
  153.     Parent := Self;
  154.     SetBounds(120, 8, 213, 13);
  155.   end;
  156.  
  157.   DirList := TDirectoryListBox.Create(Self);
  158.   with DirList do
  159.   begin
  160.     Parent := Self;
  161.     SetBounds(8, 72, 213, 138);
  162.     TabOrder := 0;
  163.     TabStop := True;
  164.     ItemHeight := 17;
  165.     IntegralHeight := True;
  166.     OnChange := DirListChange;
  167.   end;
  168.  
  169.   with TLabel.Create(Self) do
  170.   begin
  171.     Parent := Self;
  172.     SetBounds(8, 56, 66, 13);
  173.     Caption := 'Directories'; //SDirsCap;
  174.     FocusControl := DirList;
  175.   end;
  176.  
  177.   FileList := TFileListBox.Create(Self);
  178.   with FileList do
  179.   begin
  180.     Parent := Self;
  181.     SetBounds(232, 72, 185, 93);
  182.     TabOrder := 6;
  183.     TabStop := True;
  184.     FileType := [ftNormal];
  185.     Mask := '*.*';
  186.     Font.Color := clGrayText;
  187.     ItemHeight := 13;
  188.   end;
  189.  
  190.   with TLabel.Create(Self) do
  191.   begin
  192.     Parent := Self;
  193.     SetBounds(232, 56, 57, 13);
  194.     Caption := 'Files';//SFilesCap;
  195.     FocusControl := FileList;
  196.   end;
  197.  
  198.   NetButton := TButton.Create(Self);
  199.   with NetButton do
  200.   begin
  201.     Parent := Self;
  202.     SetBounds(8, 224, 77, 27);
  203.     Visible := False;
  204.     TabOrder := 3;
  205.     Caption := 'Network';//SNetworkCap;
  206.     OnClick := NetClick;
  207.   end;
  208.  
  209.   OKButton := TButton.Create(Self);
  210.   with OKButton do
  211.   begin
  212.     Parent := Self;
  213.     SetBounds(172, 224, 77, 27);
  214.     TabOrder := 4;
  215.     OnClick := OKClick;
  216.     Caption := '&OK'; //SOKButton;
  217.     ModalResult := 1;
  218.     Default := True;
  219.   end;
  220.  
  221.   CancelButton := TButton.Create(Self);
  222.   with CancelButton do
  223.   begin
  224.     Parent := Self;
  225.     SetBounds(256, 224, 77, 27);
  226.     TabOrder := 5;
  227.     Cancel := True;
  228.     Caption := '&Cancel';//SCancelButton;
  229.     ModalResult := 2;
  230.   end;
  231.  
  232.   HelpButton := TButton.Create(Self);
  233.   with HelpButton do
  234.   begin
  235.     Parent := Self;
  236.     SetBounds(340, 224, 77, 27);
  237.     TabOrder := 7;
  238.     Caption := '&Help';//SHelpButton;
  239.     OnClick := HelpButtonClick;
  240.   end;
  241.  
  242.   FormCreate(Self);
  243.   ActiveControl := DirList;
  244. end;
  245.  
  246. procedure TSelectDirDlg.HelpButtonClick(Sender: TObject);
  247. begin
  248.   Application.HelpContext(HelpContext);
  249. end;
  250.  
  251. procedure TSelectDirDlg.DirListChange(Sender: TObject);
  252. begin
  253.   DirLabel.Caption := DirList.Directory;
  254.   FileList.Directory := DirList.Directory;
  255.   DirEdit.Text := DirLabel.Caption;
  256.   DirEdit.SelectAll;
  257. end;
  258.  
  259. procedure TSelectDirDlg.FormCreate(Sender: TObject);
  260. var
  261.   UserHandle: THandle;
  262.   NetDriver: THandle;
  263.   WNetGetCaps: function (Flags: Word): Word;
  264. begin
  265.   { is network access enabled? }
  266.   UserHandle := GetModuleHandle(User32);
  267.   @WNetGetCaps := GetProcAddress(UserHandle, 'WNETGETCAPS');
  268.   if @WNetGetCaps <> nil then
  269.   begin
  270.     NetDriver := WNetGetCaps(Word(-1));
  271.     if NetDriver <> 0 then
  272.     begin
  273.       @WNetConnectDialog := GetProcAddress(NetDriver, 'WNETCONNECTDIALOG');
  274.       NetButton.Visible := @WNetConnectDialog <> nil;
  275.     end;
  276.   end;
  277.  
  278.   FAllowCreate := False;
  279.   DirLabel.BoundsRect := DirEdit.BoundsRect;
  280.   DirListChange(Self);
  281. end;
  282.  
  283. procedure TSelectDirDlg.DriveListChange(Sender: TObject);
  284. begin
  285.   DirList.Drive := DriveList.Drive;
  286. end;
  287.  
  288. procedure TSelectDirDlg.SetAllowCreate(Value: Boolean);
  289. begin
  290.   if Value <> FAllowCreate then
  291.   begin
  292.     FAllowCreate := Value;
  293.     DirLabel.Visible := not FAllowCreate;
  294.     DirEdit.Visible := FAllowCreate;
  295.   end;
  296. end;
  297.  
  298. procedure TSelectDirDlg.SetDirectory(const Value: string);
  299. var
  300.   Temp: string;
  301. begin
  302.   if Value <> '' then
  303.   begin
  304.     Temp := ExpandFileName(SlashSep(Value,'*.*'));
  305.     if (Length(Temp) >= 3) and (Temp[2] = ':') then
  306.     begin
  307.       DriveList.Drive := Temp[1];
  308.       Temp := ExtractFilePath(Temp);
  309.       try
  310.         DirList.Directory := Copy(Temp, 1, Length(Temp) - 1);
  311.       except
  312.         on EInOutError do
  313.         begin
  314.           GetDir(0, Temp);
  315.           DriveList.Drive := Temp[1];
  316.           DirList.Directory := Temp;
  317.         end;
  318.       end;
  319.     end;
  320.   end;
  321. end;
  322.  
  323. function TSelectDirDlg.GetDirectory: string;
  324. begin
  325.   if FAllowCreate then
  326.     Result := DirEdit.Text
  327.   else
  328.     Result := DirLabel.Caption;
  329. end;
  330.  
  331. procedure TSelectDirDlg.NetClick(Sender: TObject);
  332. begin
  333.   if Assigned(WNetConnectDialog) then
  334.     WNetConnectDialog(Handle, WNTYPE_DRIVE);
  335. end;
  336.  
  337. procedure TSelectDirDlg.OKClick(Sender: TObject);
  338. begin
  339.   if AllowCreate and Prompt and (not DirectoryExists(Directory)) and
  340.     (MessageDlg('Create this new directory?', mtConfirmation, [mbYes, mbNo],
  341.       0) <> mrYes) then
  342.     ModalResult := 0;
  343. end;
  344.  
  345. function MySelectDirectory(const aTitle : string; var Directory: string;
  346.   Options: TSelectDirOpts; HelpCtx: Longint): Boolean;
  347. // direct copy of filectrl execpt added prompt
  348. var
  349.   D: TSelectDirDlg;
  350. begin
  351.   D := TSelectDirDlg.Create(Application);
  352.   d.caption := aTitle;
  353.   try
  354.     D.Directory := Directory;
  355.     D.AllowCreate := sdAllowCreate in Options;
  356.     D.Prompt := sdPrompt in Options;
  357.  
  358.     { scale to screen res }
  359.     if Screen.PixelsPerInch <> 96 then
  360.     begin
  361.       D.ScaleBy(Screen.PixelsPerInch, 96);
  362.       D.FileList.ParentFont := True;
  363.       D.Left := (Screen.Width div 2) - (D.Width div 2);
  364.       D.Top := (Screen.Height div 2) - (D.Height div 2);
  365.       D.FileList.Font.Color := clGrayText;
  366.     end;
  367.  
  368.     if HelpCtx = 0 then
  369.     begin
  370.       D.HelpButton.Visible := False;
  371.       D.OKButton.Left := D.CancelButton.Left;
  372.       D.CancelButton.Left := D.HelpButton.Left;
  373.     end
  374.     else D.HelpContext := HelpCtx;
  375.  
  376.     Result := D.ShowModal = mrOK;
  377.     if Result then
  378.     begin
  379.       Directory := D.Directory;
  380.       if sdPerformCreate in Options then
  381.         ForceDirectories(Directory);
  382.     end;
  383.   finally
  384.     D.Free;
  385.   end;
  386. end;
  387.  
  388.  
  389.  
  390. end.
  391.