unit objbrowsewin;

interface

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

type
  TObjBrowser = class(TForm)
    ObjTree: TTreeView;
    TrackBar1: TTrackBar;
    PopupMenu1: TPopupMenu;
    AddTemplate1: TMenuItem;
    N1: TMenuItem;
    Delete1: TMenuItem;
    procedure FormShow(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure ObjTreeCollapsing(Sender: TObject; Node: TTreeNode;
      var AllowCollapse: Boolean);
    procedure ObjTreeDblClick(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
    procedure Delete1Click(Sender: TObject);
    procedure PopupMenu1Popup(Sender: TObject);
  private
    IsExtended: boolean;
    Procedure LoadNode(Node: TEdRawNode; TreeNode: TTreeNode; StrPath: String);
    procedure WMNCLButtondblclk(var Msg: TWMNCLButtondblclk); message WM_NCLBUTTONDBLCLK;
  public
    StrArray: Array of String;
    oldHeight: integer;
    procedure SetRoll(Extended: Boolean);
    procedure ToggleRoll;
    procedure WM(Var msg: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
    Procedure LoadFromNode(Node: TEdRawNode = nil);
    Function ObjStringToNode(ObjStr: String): TEdRawNode;
    Function TreeNodeToRawNode(TreeNode: TTreeNode): TEdRawNode;
    Procedure ClearMenu;
    Procedure BuildTemplateMenu(NodeType: TNodetype);
    Procedure OnTempMenu(Sender: TObject);
  end;
  PStr = ^String;

var
  ObjBrowser: TObjBrowser;

implementation

uses filter, oi, snapto;

{$R *.dfm}

{ TObjBrowser }

procedure TObjBrowser.LoadFromNode(Node: TEdRawNode = nil);
begin
 SetLength(StrArray, 0);
 ObjTree.Items.BeginUpdate;
 ObjTree.Items.Clear;
 If Node = nil then begin
  Node := oeMain.Tree.GetDisplayNode;
 end;
 LoadNode(Node, nil, '');
 ObjTree.Items.EndUpdate;
 ObjTree.FullExpand;
end;

procedure TObjBrowser.LoadNode(Node: TEdRawNode; TreeNode: TTreeNode; StrPath: String);
Var
 Img, I: Integer;
 TN: TTreeNode;
 S: string;
 Add: boolean;
begin
 // Add to tree and Add StrPath as Object
 TN := TreeNode;
 Add := false;
 Img := -1;
 S := '';
 Case Node.NodeType of
   ntForm: begin
            Add := true;
            Img := 0;
            S  := (Node as TEdFormNode).FormName + ' [Form]';
           end;
   ntVisual: Begin
              Add := true;
              Img := 3;
              S := (Node as TEdVisualNode).VisualName + ' [Visual]';
             end;
   ntSensor: begin
              Add := true;
              If (Node As TEdSensorNode).LinkedTo = nil then begin
               Img := 4;
               S := (Node as TEdSensorNode).PlugInName + '.' + (Node as TEdSensorNode).SensorName + ' [Sensor]';
              end
               else
              begin
               Img := 6;
               S := 'Linked Sensor: "@' + (Node as TEdSensorNode).LinkedTo.IDName + '"'
              end;
             end;
   ntText: Begin
            Add := True;
            Img := 5;
            S := (Node as TEdTextNode).TextValue + ' [Text]';
           end;
 end;
 If Add then begin
  SetLength(StrArray, length(StrArray) + 1);
  StrArray[Length(StrArray)-1] := StrPath;
  TN := ObjTree.Items.AddChild(TreeNode, S);
  TN.StateIndex := Length(StrArray)-1;
  TN.ImageIndex := Img;
  TN.SelectedIndex := Img;
  If Node = oeMain.CurrentSelectNode then begin
   TN.Selected := true;
  end;
 end;
 // Scan children
 For I := 0 to Node.Count - 1 do begin
  LoadNode(Node[I], TN, StrPath + '\' + IntTostr(I));
 end;
end;

procedure TObjBrowser.FormShow(Sender: TObject);
begin
 oeMain.actionObjectBrowser.Checked := True;
end;

procedure TObjBrowser.FormHide(Sender: TObject);
begin
 oeMain.actionObjectBrowser.Checked := False;
end;

procedure TObjBrowser.ObjTreeCollapsing(Sender: TObject; Node: TTreeNode;
  var AllowCollapse: Boolean);
begin
 allowcollapse := false;
end;

procedure TObjBrowser.ObjTreeDblClick(Sender: TObject);
Var
 Str: String;
 Node: TEdRawNode;
begin
 If objtree.Selected = nil then exit;
 Str := StrArray[ObjTree.Selected.StateIndex];
 Try
  Node := ObjStringToNode(Str);
 except
  Node := nil;
 end;
 If Node = nil then begin
  Str := 'NIL!!!';
 end
  else
 begin
  oeMain.Select.CurrentSelectedNode := Node;
  oeInspector.PageControl1.ActivePageIndex := 0;
  If oeInspector.Visible then begin
   oeInspector.SetFocus;
   oeInspector.listProps.SetFocus;
  end;
 end;
end;

function TObjBrowser.ObjStringToNode(ObjStr: String): TEdRawNode;
Var
 Node: TEdRawNode;
 I: Integer;
begin
 Result := nil;
 If ObjStr = '' then exit;
 If ObjStr[1] = '\' then begin
  ObjStr := Copy(ObjStr, 2, length(objStr));
 end;
 Node := oeMain.Tree.GetDisplayNode;
 For I := 1 to CountFilt(ObjStr, '\') do begin
  Node := Node[StrToInt(GetFilt(ObjStr, I, '\'))];
 end;
 Result := Node;
end;

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

procedure TObjBrowser.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;

procedure TObjBrowser.ToggleRoll;
begin
  if ClientHeight = TrackBar1.Height then ClientHeight := oldHeight
    else ClientHeight := TrackBar1.Height;
end;

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

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

procedure TObjBrowser.FormCreate(Sender: TObject);
begin
  IsExtended := true;
  left := 50;
  top := 50;
  DoubleBuffered := true;
  oldHeight := ClientHeight;
end;

procedure TObjBrowser.FormActivate(Sender: TObject);
begin
 oeMain.SetHotKeyStates(self);
 If (Options.ToolWindows.AutoRollUp and (not Options.ToolWindows.ARUExcept.ObjBrowser)) then begin
  SetRoll(true);
 end;
end;

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

procedure TObjBrowser.Delete1Click(Sender: TObject);
Var
 Str: String;
 Node: TEdRawNode;
begin
 If objtree.Selected = nil then exit;
 Str := StrArray[ObjTree.Selected.StateIndex];
 Try
  Node := ObjStringToNode(Str);
 except
  Node := nil;
 end;
 If Node = nil then begin
  Str := 'NIL!!!';
 end
  else
 begin
  If MessageDlg('Are you sure you wish to delete this object and all it''s children ?', mtConfirmation, [mbYes, mbNo], 0) = mrNo then exit;
  Node.DeleteSelf;
  LoadFromNode();
 end;
end;

function TObjBrowser.TreeNodeToRawNode(TreeNode: TTreeNode): TEdRawNode;
Var
 Str: String;
 Node: TEdRawNode;
begin
 Node := Nil;
 If TreeNode = nil then begin
  Result := nil;
  exit;
 end;
 Str := StrArray[TreeNode.StateIndex];
 Try
  Node := ObjStringToNode(Str);
 except

 end;
 Result := Node;
end;

procedure TObjBrowser.PopupMenu1Popup(Sender: TObject);
Var
 Node: TEdRawNode;
begin
 Node := TreeNodeToRawNode(ObjTree.Selected);
 If Node = nil then exit;
 BuildTemplateMenu(Node.NodeType);
end;

procedure TObjBrowser.ClearMenu;
Var
 I: Integer;
begin
 For I := 0 to AddTemplate1.Count - 1 do begin
  AddTemplate1.Delete(0);
 end;
end;

procedure TObjBrowser.BuildTemplateMenu(NodeType: TNodetype);
Var
 I: Integer;
 MI: TMenuItem;
begin
 ClearMenu;
 For I := 0 to oeMain.TemplateList.Count - 1 do begin
  Case NodeType of
   ntForm: Begin
            If uppercase(oeMain.TemplateList[I].Information.CMLType) = uppercase('VISUAL') then begin
             MI := TMenuItem.Create(addTemplate1);
             MI.Caption := oeMain.TemplateList[I].Information.ScreenName;
             MI.Tag := I;
             MI.OnClick := OnTempMenu;
             AddTemplate1.Add(MI);
            end;
           end;
   ntVisual: Begin
              If Uppercase(oeMain.TemplateList[I].Information.CMLType) = uppercase('sensor') then begin
               MI := TMenuItem.Create(addTemplate1);
               MI.Caption := oeMain.TemplateList[I].Information.ScreenName;
               MI.Tag := I;
               MI.OnClick := OnTempMenu;
               AddTemplate1.Add(MI);
              end;
             end;
  end;
 end;
end;

procedure TObjBrowser.OnTempMenu(Sender: TObject);
Var
 Node: TEdRawNode;
begin
 If objTree.Selected = nil then exit;
 Node := TreeNodeToRawNode(objtree.Selected);
 oeMain.InsertTemplate(Node, (Sender as TMenuItem).Tag);
end;

end.
