unit VarWin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, Engine;

type
{  TVarWinItem = Record
   SensorName: String;
   SafeModeName: String;
   SensorID: String;
   Value: String;
   Update: String;
   Param: String;
   Source: TEdRawNode;
  end;
  TVarWinItemList = Class(TObject)
  private
   fList: Array of TVarWinItem;
   function GETCOUNT: Cardinal;
  public
   Property Count: Cardinal read GETCOUNT;
   Procedure Delete(Index: Integer);
  end;}
  TVariableWin = class(TForm)
    TrackBar1: TTrackBar;
    VarWinListview: TListView;
    procedure TrackBar1Change(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure VarWinListviewDblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
    procedure VarWinListviewMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    IsExtended: boolean;
    procedure WMNCLButtondblclk(var Msg: TWMNCLButtondblclk); message WM_NCLBUTTONDBLCLK;
  public
    oldHeight: integer;
//   VWIL: TVarWinItemList;
   procedure SetRoll(Extended: Boolean);
   procedure WM(Var msg: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
   Procedure BuildLV;
   Procedure OnEvent(Source: TEdSensorNode; Delete: Boolean = false);
   Procedure OnParameter(Source: TEdSensorNode; NewParam: string);
  end;

var
  VariableWin: TVariableWin;

implementation

{$R *.dfm}

Uses SnapTo, main, CM2PlugIntypes, CM2VariableManager, oi;

procedure TVariableWin.TrackBar1Change(Sender: TObject);
begin
  AlphaBlendValue := TrackBar1.Position;
end;

procedure TVariableWin.WM(var msg: TWMWindowPosChanging);
begin
 SnapTo.WMWindowPosChanging(MSG, self);
end;

procedure TVariableWin.FormHide(Sender: TObject);
begin
 oeMain.actionSensorListing.Checked := False;
end;

procedure TVariableWin.FormShow(Sender: TObject);
begin
 oeMain.actionSensorListing.Checked := True;
 BuildLV;
end;

procedure TVariableWin.BuildLV;
Var
 I: Integer;
 LI: TListItem;
 S: String;
begin
 if not assigned(oeMain.VarMgr) then exit;
 VarWinListview.Items.BeginUpdate;
 VarWinListview.Items.Clear;
 S := '';
 For I := 0 to oeMain.VarMgr.Count - 1 do begin
  LI := VarWinListview.Items.Add;
  LI.Caption := oeMain.VarMgr[I].Name;
  LI.SubItems.Add(oeMain.VarMgr[I].ID);
  Case oeMain.VarMgr[I].Result.SensorStyle of
   ssNonNumeric: Begin
                  S := oeMain.VarMgr[I].Result.Str;
                 end;
   ssNumeric: Begin
               S := FloatToStr(oeMain.VarMgr[I].Result.Value);
              end;
  end;
  LI.SubItems.Add(S);
  S := '';
  S := oeMain.VarMgr[I].GetUpdateString;
  LI.SubItems.Add(S);
  LI.SubItems.Add(oeMain.VarMgr[I].Parameters);
 end;
 VarWinListview.Items.EndUpdate;
end;

procedure TVariableWin.OnEvent(Source: TEdSensorNode; Delete: Boolean = False);
Var
 S, SMN: String;
 I, Sel: Integer;
 Res: TResult;
 LI: TListItem;
begin
 If Not Visible then exit; // No need to use CPU power if it's not showing
 SMN := Source.SafeModeName;
 If Delete then begin
  Sel := -1;
  For I := 0 to VarWinListview.Items.count - 1 do begin
   If VarWinListview.Items[I].Caption = SMN then begin
    Sel := I;
   end;
  end;
  If Sel > -1 then begin
   VarWinListview.Items[Sel].Delete;
  end;
  Exit;
 end;
 For I := 0 to VarWinListview.Items.count -1 do begin
  If VarWinListview.Items[I].Caption = SMN then begin
   Res := Source.Result;
   Case Res.SensorStyle of
    ssNonNumeric: Begin
                   S := Res.Str;
                  end;
    ssNumeric: Begin
                S := FloatToStr(Res.Value);
               end;
   end;
   VarWinListview.Items[I].SubItems[1] := S;
   exit;
  end;
 end;
 VarWinListview.Items.BeginUpdate;
 LI := VarWinListview.Items.Add;
 LI.Caption := Source.SafeModeName;
 LI.SubItems.Add(Source.IDName);
 S := '';
 Res := Source.Result;
 Case Res.SensorStyle of
  ssNonNumeric: Begin
                 S := Res.Str;
                end;
  ssNumeric: Begin
              S := FloatToStr(Res.Value);
             end;
 end;
 LI.SubItems.Add(S);
 S := Source.UpdateStr;
 LI.SubItems.Add(S);
 LI.SubItems.Add(Source.ResolveParameters);
 VarWinListview.Items.EndUpdate;
end;

procedure TVariableWin.OnParameter(Source: TEdSensorNode; NewParam: String);
Var
 S, SMN: String;
 I: Integer;
 Res: TResult;
 LI: TListItem;
begin
 If Not Visible then exit; // No need to use CPU power if it's not showing
 SMN := Source.SafeModeName;
 For I := 0 to VarWinListview.Items.count -1 do begin
  If VarWinListview.Items[I].Caption = SMN then begin
   VarWinListview.Items[I].SubItems[3] := NewParam;
   VarWinListview.Items[I].SubItems[0] := Source.IDName;
   exit;
  end;
 end;
 VarWinListview.Items.BeginUpdate;
 LI := VarWinListview.Items.Add;
 LI.Caption := Source.SafeModeName;
 LI.SubItems.Add(Source.IDName);
 S := '';
 Res := Source.Result;
 Case Res.SensorStyle of
  ssNonNumeric: Begin
                 S := Res.Str;
                end;
  ssNumeric: Begin
              S := FloatToStr(Res.Value);
             end;
 end;
 LI.SubItems.Add(S);
 S := Source.UpdateStr;
 LI.SubItems.Add(S);
 LI.SubItems.Add(NewParam);
 VarWinListview.Items.EndUpdate;
end;

procedure TVariableWin.VarWinListviewDblClick(Sender: TObject);
begin
 If VarWinListview.Selected = nil then exit;
 oeMain.Select.CurrentSelectedNode := (oeMain.VarMgr.VarItem[VarWinListview.Selected.Index].Node[0] as TEdSensorNode);
 oeInspector.PageControl1.ActivePageIndex := 0;
 If oeInspector.Visible then begin
  oeInspector.SetFocus;
  oeInspector.listProps.SetFocus;
 end;
end;

procedure TVariableWin.FormCreate(Sender: TObject);
begin
 left := 50;
 top := 50;
 oldheight := clientheight;
 IsExtended := true;
end;

procedure TVariableWin.WMNCLButtondblclk(var Msg: TWMNCLButtondblclk);
begin
 If ((msg.HitTest = HTCAPTION) and (not(Options.ToolWindows.AutoRollUp and (not Options.ToolWindows.ARUExcept.VarWin)))) then begin
  SetRoll(not IsExtended);
 end;
end;

procedure TVariableWin.SetRoll(Extended: Boolean);
begin
 If not extended then begin
  If not IsExtended then exit;
  oldHeight := ClientHeight;
  ClientHeight := TrackBar1.Height;
  IsExtended := false;
 end
  else
 begin
  If IsExtended then exit;
  ClientHeight := oldHeight;
  IsExtended := true;
 end;
end;

{ TVarWinItemList }
{
procedure TVarWinItemList.Delete(Index: Integer);
Var
 I: Integer;
begin
 For I := Index to Count - 2 do begin
  fList[I] := fList[I+1];
 end;
 SetLength(fList, Count - 1);
end;

function TVarWinItemList.GETCOUNT: Cardinal;
begin
 Result := length(fList);
end;
}
procedure TVariableWin.FormActivate(Sender: TObject);
begin
 oeMain.SetHotKeyStates(self);
 If (Options.ToolWindows.AutoRollUp and (not Options.ToolWindows.ARUExcept.VarWin)) then begin
  SetRoll(true);
 end;
end;

procedure TVariableWin.FormDeactivate(Sender: TObject);
begin
 If (Options.ToolWindows.AutoRollUp and (not Options.ToolWindows.ARUExcept.VarWin)) then begin
  SetRoll(false);
 end;
end;

procedure TVariableWin.VarWinListviewMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then VarWinListview.BeginDrag(false, 5);
end;

end.
