unit Config;

interface

uses
  // Borland Software Corporation
  Windows, Registry, Forms;

type

//----------------------------------------------------------------------------
//  TConfig = class(TRegistry); Класс данных настроек
//----------------------------------------------------------------------------
  PMainItem = ^TMainItem;
  TMainItem = record
  //  StartupScreen : boolean;
    Left        : integer;
    Top         : integer;
    Width       : integer;
    Height      : integer;
    WindowState : integer;

    FileName    : string[255];
    DeviceName  : string[255];
    SaveToFile  : boolean;
    OnScreen    : boolean;

    TimeInterval : double;             // Период опроса, с:
    SampleRate   : double;             // Частота опроса, Гц.:
    AcquisitionInterval : double;      // Время регистрации, с.:
    Count        : double;             // Число выборок, шт:

    ShowHint    : boolean;
    StatusBar   : boolean;

    AutoDeviceSearch : boolean;
    Explorer         : boolean;
  end;

  TConfiguration = class(TRegistry)
  private
    FKey      : string;
    FMainItem : PMainItem;
    FIsSafeMode : boolean;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Load(Default: boolean);
    procedure Save;

    property MainItem : PMainItem read FMainItem write FMainItem;
    property Key : string read FKey;
    property IsSafeMode : boolean read FIsSafeMode;
  end; //  TConfig = class(TRegistry)


var
  Configuration : TConfiguration;

implementation

//----------------------------------------------------------------------------
//  TConfig = class(TRegistry); Класс данных настроек
//----------------------------------------------------------------------------

constructor TConfiguration.Create;
begin
  inherited Create;
  RootKey := HKEY_CURRENT_USER;
  FKey := '\Software\Spetspribor\Data Logger';

  New(FMainItem);

  if (GetKeyState(VK_CONTROL) < 0) and
    (MessageBox(Application.Handle, 'Программой обнаружено, что нажата клавиша CTRL. Запустить программу в безопасном режиме?', PChar(Application.Title), MB_YESNO or MB_TOPMOST or MB_ICONQUESTION) = IDYES) then
      FIsSafeMode := true;

  Load(FIsSafeMode);
end;

destructor TConfiguration.Destroy;
begin
  Save;
  Dispose(FMainItem);
  inherited Destroy;
end;

procedure TConfiguration.Load(Default: boolean);
begin
  if OpenKey(FKey, true) then
  begin
    if not ValueExists('MainItem') or Default then
        with FMainItem^ do
        begin
          Width            := 800; // 640 | 800 | 1024
          Height           := 600; // 480 | 600 | 768
          Left             := (GetSystemMetrics(SM_CXSCREEN) shr 1) - (Width shr 1);
          Top              := (GetSystemMetrics(SM_CYSCREEN) shr 1) - (Height shr 1);
          WindowState      := 0;   // (0 = wsNormal);
          StatusBar        := True;
          ShowHint         := True;
          AutoDeviceSearch := False;
          Explorer         := True;
          FileName         := '';
          SaveToFile       := False;
          OnScreen         := True;
          TimeInterval     := 1;
          Count            := 0;
          SampleRate       := 1;
          AcquisitionInterval := 0;
        end
    else
        ReadBinaryData('MainItem', FMainItem^, sizeof(TMainItem));

    CloseKey;
  end;

end;

procedure TConfiguration.Save;
begin
 if OpenKey(FKey, true) then
  begin
    WriteBinaryData('MainItem', FMainItem^, sizeof(TMainItem));

    CloseKey();
  end;
end;

initialization
  Configuration := TConfiguration.Create;

finalization
  Configuration.Free;

end.
