unit U_Projectmanager;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, ComCtrls,
  PCPanel, PCDrawBox, PCDrawing, PowerCad, pcMsbar,
  XP_Panel, ToolWin, PCTypesUtils, DrawObjects, Menus, DlgBase,  ExtDlgs,
  PCLayerDlg, OleCtnrs, Buttons, PCgui, GuiStrings, DrawEngine, U_ESCadClasess;


type
  TF_ProjectManager = class(TForm)
    tvPrjManager: TTreeView;
    procedure FormStartDock(Sender: TObject; var DragObject: TDragDockObject);
    procedure FormEndDock(Sender, Target: TObject; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure tvPrjManagerClick(Sender: TObject);
  private
    { Private declarations }
  public
    Docking: Bool;
    { Public declarations }
  end;

var
  F_ProjectManager: TF_ProjectManager;

implementation

uses U_Cad, U_Main, USCS_Main, U_Common;
{$R *.dfm}

procedure TF_ProjectManager.FormStartDock(Sender: TObject;
  var DragObject: TDragDockObject);
begin
  Docking := True;
end;

procedure TF_ProjectManager.FormEndDock(Sender, Target: TObject; X,
  Y: Integer);
begin
  Docking := False;
  FSCS_Main.PDock1.DockSite := true;
  FSCS_Main.PDock2.DockSite := true;
  FSCS_Main.cbMainPanel.DockSite := true;
  F_Main.Panel_Main.DockSite := true;
  if TForm(Sender).Left > Screen.Width - 100 then
    TForm(Sender).Left := TForm(Sender).Left - 50;
  if TForm(Sender).Top > Screen.Height - 100 then
    TForm(Sender).Top := TForm(Sender).Top - 50;
end;

procedure TF_ProjectManager.FormCreate(Sender: TObject);
var
  Node, SubNode: TTreeNode;
begin
  Docking := False;
end;

procedure TF_ProjectManager.FormPaint(Sender: TObject);
begin
  if TForm(Sender).Left > Screen.Width - 100 then
    TForm(Sender).Left := TForm(Sender).Left - 50;
  if TForm(Sender).Top > Screen.Height - 100 then
    TForm(Sender).Top := TForm(Sender).Top - 50;
end;

procedure TF_ProjectManager.FormClose(Sender: TObject; var Action: TCloseAction);
var
  ParentPanel: TComponent;
begin
  FSCS_Main.aViewProjectManager.Checked := false;
  ParentPanel := TForm(Sender).GetParentComponent;

  if ParentPanel = FSCS_Main.pDock1 then
    FSCS_Main.CountDock1 := FSCS_Main.CountDock1 - 1;
  if ParentPanel = FSCS_Main.pDock2 then
    FSCS_Main.CountDock2 := FSCS_Main.CountDock2 - 1;

  if FSCS_Main.CountDock1 = 0 then
  begin
    FSCS_Main.pDock1.Width := 1;
    FSCS_Main.sDiv1.Visible := False;
  end;
  if FSCS_Main.CountDock2 = 0 then
  begin
    FSCS_Main.sDiv2.Visible := False;
    FSCS_Main.pDock2.Width := 1;
  end;
end;

procedure TF_ProjectManager.FormShow(Sender: TObject);
begin
  FSCS_Main.aViewProjectManager.Checked := true;
end;

procedure TF_ProjectManager.tvPrjManagerClick(Sender: TObject);
var
  Parent: TTreeNode;
  CurNode: TTreeNode;
  PageIndex: integer;
  CurPCad: TPowerCad;
begin
  // наименования листов проекта
  if (tvPrjManager.Selected <> nil) then
  begin
    PageIndex := 0;
    SubNode := tvPrjManager.Selected;
    Parent := SubNode.Parent;
    if Parent = nil then
      Exit;
    // получить PageIndex
    CurNode := Parent.getFirstChild;
    for PageIndex := 0 to Parent.Count - 1 do
    begin
      if CurNode = SubNode then
        break;
      CurNode := CurNode.getNextSibling;
    end;
    // переключить на активный лист
    FSCS_Main.pageCADList.ActivePageIndex := PageIndex;
    tvPrjManager.Selected := SubNode;
    // переключить в главном меню
    FSCS_Main.nWindow[PageIndex + 3].Checked := true;
    // переключить КАД
    PageIndex := FSCS_Main.pageCADList.ActivePageIndex;
    CurPCad := CADList.Items[PageIndex];
    CurPCad.BringToFront;
  end;
end;

end.
