{------------------------------------------------------------------------------}
{
   Unit Name: DirEdit
   Purpose  : Edit field with save dialog and button to open dialog
   Author   : Vesa Lappalainen
   Date     : 10.06.2005
   Changed  :
   ToDo     :
   
}
{------------------------------------------------------------------------------}
unit kDirPnl;


interface
uses SysUtils, Classes,
{$ifdef CLX}
  QGraphics, QControls, QForms, QDialogs,  QExtCtrls, QStdCtrls,
{$else}
  Windows, Messages,
  Graphics, Controls, Forms, Dialogs,  ExtCtrls, StdCtrls,
{$endif}
  kEditPnl,kIniSave;


type
  TDirPanel = class(TEditPanel)
  private
    dirbutton : TButton;
    savedialog : TSaveDialog;
    procedure DirButtonPressed(sender: TObject);
    procedure SetDirectory(const Value: string);
    function GetDirectory: string;
  protected
    procedure CreateChild; override;
    function  GetCtrlNr : integer; override;
  public
    constructor Create(AOwner:TComponent); override;
  published
    property Directory : string read GetDirectory write SetDirectory;
  end;

  procedure Register;

implementation

{------------------------------------------------------------------------------}
procedure Register;
begin
  RegisterComponents('KaveOptions', [TDirPanel]);
end;

var globalSaveDialog : TSaveDialog = nil;

{ TDirPanel }

constructor TDirPanel.Create(AOwner: TComponent);
begin
  inherited;
  dirbutton := TButton.Create(self);
  dirbutton.Parent := self;
  dirbutton.Align := alRight;
  dirbutton.Caption := '...';
  dirbutton.Width := 25;
  dirbutton.OnClick := DirButtonPressed;
  if ( globalSaveDialog = nil ) then begin
    globalSaveDialog := TSaveDialog.Create(nil);
  end;
  savedialog := globalSaveDialog;

end;

{------------------------------------------------------------------------------}
function  TDirPanel.GetCtrlNr : integer;
{ Override this if you create new components in constructor                    }
begin
  Result := 3; // // 0=FLabel, 1=FFocCtrl, 2 = dirbutton
end;

procedure TDirPanel.DirButtonPressed(sender:TObject);
begin
   SaveDialog.InitialDir := Directory;
//   if ( AsString <> '' ) then
//     SaveDialog.FileName := AsString
//   else
     SaveDialog.FileName := 'g.plt';
   if (not SaveDialog.Execute ) then exit;
   Directory := ExtractFileDir(SaveDialog.FileName);
//   s := ExtractFileName(SaveDialog.FileName);
//   if ( s <> 'g.plt' ) then AsString := ChangeFileExt(s,'');
end;


procedure TDirPanel.CreateChild;
begin
  inherited;
end;

procedure TDirPanel.SetDirectory(const Value: string);
begin
  AsString := Value;
end;

function TDirPanel.GetDirectory: string;
begin
  Result := AsString;
end;

initialization begin
end;

finalization begin
  if ( globalSaveDialog = nil ) then globalSaveDialog.Free;
end;


end.
