unit U_NewLayer;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxLookAndFeelPainters, StdCtrls, cxButtons, cxControls,
  cxContainer, cxEdit, cxTextEdit, ComCtrls,
  // POWERCAD
  PCPanel, PCDrawBox, PCDrawing, PowerCad, pcMsbar, XP_Panel, PCTypesUtils,
  DrawObjects, DlgBase,  ExtDlgs, PCLayerDlg, OleCtnrs, PCgui, GuiStrings,
  DrawEngine, U_ESCadClasess, siComp, siLngLnk, cxGraphics, cxLookAndFeels,
  Menus;

type
  TF_NewLayer = class(TForm)
    edNewLayerName: TcxTextEdit;
    bOK: TcxButton;
    bCancel: TcxButton;
    lng_Forms: TsiLangLinked;
    procedure bCancelClick(Sender: TObject);
    procedure bOKClick(Sender: TObject);
    procedure edNewLayerNameKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    function CheckLayerNameNotExist(aName: string): Boolean;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  F_NewLayer: TF_NewLayer;

implementation
uses USCS_main, U_CAD, U_Common, U_Layers, U_BaseCommon, U_Constants;
{$R *.dfm}

procedure TF_NewLayer.bCancelClick(Sender: TObject);
begin
  try
    edNewLayerName.Clear;
    Close;
  except
    on E: Exception do AddExceptionToLogEx('TF_NewLayer.bCancelClick', E.Message);
  end;
end;

procedure TF_NewLayer.bOKClick(Sender: TObject);
var
  str: string;
  //06.08.2012 NewLayer: TLayer;
  //06.08.2012 Item: TListItem;
begin
  try
    str := edNewLayerName.Text;
    if (str <> '') and CheckLayerNameNotExist(str) then
    begin
      {//06.08.2012
      NewLayer := TLayer.create(edNewLayerName.Text);
      GCadForm.PCad.Layers.Add(NewLayer);
      FSCS_Main.cbLayers.Properties.Items.Add(edNewLayerName.Text);
      edNewLayerName.Clear;
      Item := F_LayersDialog.lvLayersList.Items.Add;
      Item.Caption := NewLayer.Name;
      Item.ImageIndex := 176; //06.08.2012 -1;
      Item.SubItems.Add('');
      Item.SubItems.Add('');
      Item.SubItemImages[0] := 136;
      Item.SubItemImages[1] := -1;
      Item.Data := NewLayer;
      F_LayersDialog.listGrayed.Add('seen');
      SetProjectChanged(True);}

      F_LayersDialog.AddLayer(edNewLayerName.Text);
      edNewLayerName.Clear;
    end
    else
    if not CheckLayerNameNotExist(str) then
    begin
      ShowMessage(cLayers_Mes12);
    end
  except
    on E: Exception do AddExceptionToLogEx('TF_NewLayer.bOKClick', E.Message);
  end;
end;

procedure TF_NewLayer.edNewLayerNameKeyPress(Sender: TObject; var Key: Char);
begin
  try
    if Key = #13 then
    begin
      bOK.SetFocus;
      bOK.Click;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_NewLayer.edNewLayerNameKeyPress', E.Message);
  end;
end;

procedure TF_NewLayer.FormShow(Sender: TObject);
begin
  try
    edNewLayerName.SetFocus;
    edNewLayerName.SelStart := 0;
    edNewLayerName.SelLength := Length(edNewLayerName.Text) + 1;
  except
    on E: Exception do AddExceptionToLogEx('TF_NewLayer.FormShow', E.Message);
  end;
end;

procedure TF_NewLayer.FormKeyPress(Sender: TObject; var Key: Char);
begin
  try
    if Key = #27 then
      Close;
  except
    on E: Exception do AddExceptionToLogEx('TF_NewLayer.FormKeyPress', E.Message);
  end;
end;

function TF_NewLayer.CheckLayerNameNotExist(aName: string): Boolean;
var
  i: integer;
  Layer: Tlayer;
begin
  try
    Result := True;
    for i := 0 to GCadForm.PCad.LayerCount - 1 do
    begin
      Layer := TLayer(GCadForm.PCad.Layers[i]);
      if Layer.name = aName then
      begin
        Result := false;
        break;
      end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_NewLayer.CheckLayerNameNotExist', E.Message);
  end;
end;

end.
