unit Main;

interface

uses

  // Borland Software Corporation
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ActnList, XPMan, ExtCtrls, ComCtrls, StdActns, ImgList, StdCtrls,

  // TBX Package
  PngImageList, TB2Item, TBX, TBXDkPanels, TB2Dock, TB2Toolbar, TBXStatusBars,

  // Berg
  NxScrollControl, NxCustomGridControl, NxCustomGrid, NxGrid, NxColumns, NxColumnClasses,

  // Unit
  Modules;

type

//----------------------------------------------------------------------------
//  TMainForm = class(TForm)
//----------------------------------------------------------------------------
  TMainForm = class(TForm)
    Timer: TTimer;
    dTop: TTBXDock;
    tbStandard: TTBXToolbar;
    tbMenu: TTBXToolbar;
    TBXSubmenuItem2: TTBXSubmenuItem;
    TBXSubmenuItem3: TTBXSubmenuItem;
    sbMain: TTBXStatusBar;
    XPManifest: TXPManifest;
    alMain: TActionList;
    acDeviceSearch: TAction;
    acDisconnect: TAction;
    acAutoDeviceSearch: TAction;
    acExit: TFileExit;
    TBXItem1: TTBXItem;
    TBXItem2: TTBXItem;
    TBXItem3: TTBXItem;
    TBXSeparatorItem1: TTBXSeparatorItem;
    TBXSeparatorItem2: TTBXSeparatorItem;
    TBXItem4: TTBXItem;
    acAboutBox: TAction;
    PngImageList: TPngImageList;
    PngImageCollection: TPngImageCollection;
    TBXItem7: TTBXItem;
    TBXItem8: TTBXItem;
    TBXSeparatorItem3: TTBXSeparatorItem;
    TBXItem9: TTBXItem;
    TBXSeparatorItem8: TTBXSeparatorItem;
    TBXSubmenuItem4: TTBXSubmenuItem;
    miStandard: TTBXItem;
    miMeasuring: TTBXItem;
    TBXItem17: TTBXItem;
    acStarusBar: TAction;
    acShowHint: TAction;
    TBXItem18: TTBXItem;
    acStart: TAction;
    acPause: TAction;
    acStop: TAction;
    acProperties: TAction;
    TBXSubmenuItem5: TTBXSubmenuItem;
    TBXItem13: TTBXItem;
    acPlugins: TAction;
    TBXItem15: TTBXItem;
    TBXSeparatorItem5: TTBXSeparatorItem;
    TBXItem16: TTBXItem;
    acSettings: TAction;
    acOscilloscope: TAction;
    TBXItem19: TTBXItem;
    TBXItem20: TTBXItem;
    NextGrid: TNextGrid;
    TBXSubmenuItem6: TTBXSubmenuItem;
    TBXItem21: TTBXItem;
    TBXItem22: TTBXItem;
    TBXItem23: TTBXItem;
    tbView: TTBXToolbar;
    tbMeasuring: TTBXToolbar;
    miView: TTBXItem;
    procedure FormCreate(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
    procedure acAboutBoxExecute(Sender: TObject);
    procedure acDeviceSearchExecute(Sender: TObject);
    procedure acDisconnectExecute(Sender: TObject);
    procedure acDeviceSearchUpdate(Sender: TObject);
    procedure acDisconnectUpdate(Sender: TObject);
    procedure acAutoDeviceSearchExecute(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure miStandardClick(Sender: TObject);
    procedure miMeasuringClick(Sender: TObject);
    procedure TBXSubmenuItem4Popup(Sender: TTBCustomItem;
      FromLink: Boolean);
    procedure acStarusBarUpdate(Sender: TObject);
    procedure acStarusBarExecute(Sender: TObject);
    procedure acShowHintExecute(Sender: TObject);
    procedure acShowHintUpdate(Sender: TObject);
    procedure acPluginsExecute(Sender: TObject);
    procedure acPropertiesUpdate(Sender: TObject);
    procedure acPropertiesExecute(Sender: TObject);
    procedure acStartUpdate(Sender: TObject);
    procedure acPauseUpdate(Sender: TObject);
    procedure acStopUpdate(Sender: TObject);
    procedure acStopExecute(Sender: TObject);
    procedure acStartExecute(Sender: TObject);
    procedure acSettingsExecute(Sender: TObject);
    procedure acSettingsUpdate(Sender: TObject);
    procedure acPluginsUpdate(Sender: TObject);
    procedure acOscilloscopeExecute(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ListView1CustomDrawSubItem(Sender: TCustomListView;
      Item: TListItem; SubItem: Integer; State: TCustomDrawState;
      var DefaultDraw: Boolean);
    procedure miViewClick(Sender: TObject);
    procedure acPauseExecute(Sender: TObject);
  private
    { Private declarations }

    procedure DisplayHint(Sender: TObject);

    // Загрузить конфигурацию из реестра
    procedure LoadConfiguration;

    // Сохранить конфигурацию в реестр
    procedure SaveConfiguration;
  public
    Connected  : boolean;
    { Public declarations }
  end; //  TMainForm = class(TForm)

var
  MainForm: TMainForm;

implementation

uses
  Config, DeviceList, Plugins, Settings, Osc_DLL;

{$R *.dfm}


//----------------------------------------------------------------------------
//  TMainForm = class(TForm)
//----------------------------------------------------------------------------

procedure TMainForm.FormCreate(Sender: TObject);
{  var    ProcessID : DWORD;
   ProcessHandle : THandle;
   ThreadHandle : THandle;
   }
 var i : integer;
begin
  DecimalSeparator := '.';
{
  ProcessID := GetCurrentProcessID;
  ProcessHandle := OpenProcess(PROCESS_SET_INFORMATION,
                                        false,  ProcessID);
  SetPriorityClass(ProcessHandle, REALTIME_PRIORITY_CLASS);
  ThreadHandle := GetCurrentThread;
  SetThreadPriority(ThreadHandle, THREAD_PRIORITY_TIME_CRITICAL);
}

  LoadConfiguration;

  for i := 0 to sbMain.Panels.Count - 1 do
      sbMain.Panels.Items[i].Caption := '';
  sbMain.Panels.Items[0].Caption := 'Off-Line';

  Application.OnHint := DisplayHint;

  Scopes := TScopes.Create;

  if Configuration.MainItem.DeviceName <> '' then
      with TDeviceListForm.Create(self) do
      try
          acRefresh.Execute;
          ListBox1.ItemIndex := ListBox1.Items.IndexOf(Configuration.MainItem.DeviceName);
          bOk.Click;
          TimerTimer(Sender);
      finally
        Free;
      end;

end;


procedure TMainForm.LoadConfiguration;
begin
  with Configuration do
  begin
    if not IsSafeMode then
       TBRegLoadPositions(self, RootKey, Key);

    WindowState := TWindowState(MainItem.WindowState);

    if (WindowState <> wsMaximized) then
    begin
      Top    := MainItem.Top;
      Left   := MainItem.Left;
      Width  := MainItem.Width;
      Height := MainItem.Height;
    end;

    acAutoDeviceSearch.Checked := MainItem.AutoDeviceSearch;

    sbMain.Visible             := MainItem.StatusBar;
    Application.ShowHint       := MainItem.ShowHint;
  end;
end;

procedure TMainForm.SaveConfiguration;
begin
  with Configuration do
  begin

    TBRegSavePositions(Self, RootKey, Key);

    MainItem.WindowState := integer(WindowState);
    if (WindowState <> wsMaximized) then
    begin
      MainItem.Left   := Left;
      MainItem.Top    := Top;
      MainItem.Width  := Width;
      MainItem.Height := Height;
    end;

    MainItem.AutoDeviceSearch := acAutoDeviceSearch.Checked;

    MainItem.StatusBar := sbMain.Visible;
    MainItem.ShowHint  := Application.ShowHint;
  end;
end;

procedure TMainForm.DisplayHint(Sender: TObject);
begin
  sbMain.Panels.Items[2].Caption := GetLongHint(Application.Hint);
end;

procedure TMainForm.TimerTimer(Sender: TObject);
begin
  Timer.Enabled := false;
  try
    if Assigned(ModuleList.ActiveModule) then
       Connected := ModuleList.ActiveModule.Connected(@Configuration.MainItem^.DeviceName[1])
    else
      Connected := false;
{    if (acAutoDeviceSearch.Checked) and (not FADCDevice.Connected) then
        FConnected := FADCDevice.DeviceSearch
    else
        FConnected := FADCDevice.Connected;
}
    if Connected then
    begin
      sbMain.Panels.Items[0].Caption := 'On-Line';
      sbMain.Panels.Items[0].ImageIndex := 7;
    end
    else
    begin
      sbMain.Panels.Items[0].Caption := 'Off-Line';
      sbMain.Panels.Items[0].ImageIndex := 8;
      if Assigned(Process) then
          Process.Terminate;
    end;
    sbMain.Panels.Items[1].Caption := Configuration.MainItem.DeviceName;
  finally
    Timer.Enabled := true;
  end;
end;

function GetFileVersion: String;
var
  hHandle,
  Size     : LongWord;
  Buffer   : PChar;
  P        : pointer;
begin
  Size := GetFileVersionInfoSize(PChar(Application.ExeName), hHandle);
  GetMem(Buffer, Size);
  try
    if GetFileVersionInfo(PChar(Application.ExeName), hHandle, Size, Buffer) then
    begin
      VerQueryValue(Buffer, '\VarFileInfo\Translation', P, Size);
      if (p <> nil) and
        (VerQueryValue(Buffer,
           PChar('\StringFileInfo\' + IntToHex(MakeLong(HiWord(Longint(P^)), LoWord(Longint(P^))), 8) + '\FileVersion'),
           P,
           Size))
      then
          Result := StrPas(PChar(P));
    end;
  finally
    FreeMem(Buffer);
  end;
end;

procedure TMainForm.acAboutBoxExecute(Sender: TObject);
begin
  Application.MessageBox(PChar('Version: '+ GetFileVersion + #13 +
                               #13 +
                               'Copyright 2006 Spetspribor Co.'#13 +
                               #13 +
                               'http://www.spetspribor.com'#13 +
                               'mailto:info@spetspribor.com'),
                           PChar(Caption), MB_OK );
end;

procedure TMainForm.acDeviceSearchExecute(Sender: TObject);
begin
  if Assigned(Process) then
      Process.Terminate;

  if Assigned(ModuleList.ActiveModule) then
  begin
    ModuleList.ActiveModule.Disconnect;
    ModuleList.ActiveModule := nil;
  end;
  Configuration.MainItem.DeviceName := '';
  Connected := false;

  with TDeviceListForm.Create(self) do
  try
    Timer.Enabled := false;
    ShowModal;
    Timer.Enabled := true;
    TimerTimer(Sender);
  finally
    Free;
  end;

end;

procedure TMainForm.acDisconnectExecute(Sender: TObject);
begin
  if Assigned(Process) then
      Process.Terminate;

  acAutoDeviceSearch.Checked := false;
  if assigned(ModuleList.ActiveModule) then
  begin
    ModuleList.ActiveModule.Disconnect;
    ModuleList.ActiveModule := nil;
  end;
  Configuration.MainItem.DeviceName := '';
  Connected := false;

  TimerTimer(Sender);
end;

procedure TMainForm.acDeviceSearchUpdate(Sender: TObject);
begin
  acDeviceSearch.Enabled :=  not Connected;
end;

procedure TMainForm.acDisconnectUpdate(Sender: TObject);
begin
  acDisconnect.Enabled := Connected;
end;

procedure TMainForm.acAutoDeviceSearchExecute(Sender: TObject);
begin
  if acAutoDeviceSearch.Enabled then
      TimerTimer(Sender);
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveConfiguration;
end;

procedure TMainForm.miStandardClick(Sender: TObject);
begin
  tbStandard.Visible := TTBXItem(Sender).Checked;
end;

procedure TMainForm.miViewClick(Sender: TObject);
begin
  tbView.Visible := TTBXItem(Sender).Checked;
end;

procedure TMainForm.miMeasuringClick(Sender: TObject);
begin
  tbMeasuring.Visible := TTBXItem(Sender).Checked;
end;

procedure TMainForm.TBXSubmenuItem4Popup(Sender: TTBCustomItem;
  FromLink: Boolean);
begin
  miStandard.Checked  := tbStandard.Visible;
  miMeasuring.Checked := tbMeasuring.Visible;
  miView.Checked      := tbView.Visible;
end;

procedure TMainForm.acStarusBarExecute(Sender: TObject);
begin
  sbMain.Visible := TAction(Sender).Checked;
end;

procedure TMainForm.acStarusBarUpdate(Sender: TObject);
begin
  TAction(Sender).Checked := sbMain.Visible;
end;

procedure TMainForm.acShowHintExecute(Sender: TObject);
begin
  Application.ShowHint := TAction(Sender).Checked;
end;

procedure TMainForm.acShowHintUpdate(Sender: TObject);
begin
  TAction(Sender).Checked := Application.ShowHint;
end;

procedure TMainForm.acPluginsExecute(Sender: TObject);
begin
  with TPluginsForm.Create(self) do
  try
    ShowModal;
  finally
    free;
  end;

end;

procedure TMainForm.acPropertiesUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := Assigned(ModuleList.ActiveModule) and Assigned(@ModuleList.ActiveModule.ShowSettings) and (not Assigned(Process));
end;

procedure TMainForm.acPropertiesExecute(Sender: TObject);
begin
  ModuleList.ActiveModule.ShowSettings;
end;

procedure TMainForm.acStartUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := assigned(ModuleList.ActiveModule) and Connected;
  TAction(Sender).Checked := assigned(Process);
end;

procedure TMainForm.acPauseUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := Assigned(Process) and assigned(ModuleList.ActiveModule) and Connected;
  TAction(Sender).Checked := Assigned(Process) and Process.isPause;
end;

procedure TMainForm.acStopUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := Assigned(Process) and assigned(ModuleList.ActiveModule) and Connected;
end;

procedure TMainForm.acStopExecute(Sender: TObject);
begin
  if Assigned(@ModuleList.ActiveModule.Stop) then
      ModuleList.ActiveModule.Stop;
  Process.Terminate;
end;

procedure TMainForm.acStartExecute(Sender: TObject);
begin
  if not Assigned(Process) then
  begin
    try
      NextGrid.ClearRows;
      NextGrid.Columns.Clear;

      if Assigned(@ModuleList.ActiveModule.Start) then
          try
             Screen.Cursor := crHourGlass;
             ModuleList.ActiveModule.Start;
          finally;
             Screen.Cursor := crDefault;
          end;

      Process := TProcess.Create;

    except on e: Exception do
      begin
        Application.ShowException(e);
        acStart.Checked := false;
      end;
    end;

  end
  else
      if (Process.isPause) then
          Process.Pause := false
      else
          Process.Terminate;
end;

procedure TMainForm.acSettingsExecute(Sender: TObject);
begin
  with TSettingsForm.Create(Self) do
  try
    ShowModal;
  finally
    free;
  end;
end;

procedure TMainForm.acSettingsUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := (not Assigned(Process));
end;

procedure TMainForm.acPluginsUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := Assigned(ModuleList) and (not Assigned(Process));
end;

procedure TMainForm.acOscilloscopeExecute(Sender: TObject);
begin
  if TAction(Sender).Checked then
    Scopes.Show
  else
    Scopes.Hide;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  Scopes.Free;
end;

procedure TMainForm.ListView1CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
begin
  case SubItem of
    1 : Sender.Canvas.Font.Color := clGreen;
    2 : Sender.Canvas.Font.Color := clPurple;
    3 : Sender.Canvas.Font.Color := clOlive;
    else
      Sender.Canvas.Font.Color := clBlack;
  end;
end;

procedure TMainForm.acPauseExecute(Sender: TObject);
begin
  Process.Pause := TAction(Sender).Checked;
end;

end.


