unit U_PageParams;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxLookAndFeelPainters, StdCtrls, cxRadioGroup, cxButtons,
  cxSpinEdit, cxControls, cxContainer, cxEdit, cxTextEdit, cxMaskEdit,
  cxDropDownEdit, PCTypesUtils;


type
  TF_PageParams = class(TForm)
    gbPageSize: TGroupBox;
    gbPageOrient: TGroupBox;
    cbPageSize: TcxComboBox;
    edWidth: TcxSpinEdit;
    edHeight: TcxSpinEdit;
    bDefault: TcxButton;
    bOK: TcxButton;
    bCancel: TcxButton;
    rbPortrait: TcxRadioButton;
    rbLandscape: TcxRadioButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure bCancelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure bDefaultClick(Sender: TObject);
    procedure bOKClick(Sender: TObject);
    procedure rbPortraitClick(Sender: TObject);
    procedure rbLandscapeClick(Sender: TObject);
    procedure cbPageSizePropertiesCloseUp(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure edWidthKeyPress(Sender: TObject; var Key: Char);
    procedure edHeightKeyPress(Sender: TObject; var Key: Char);
    procedure edWidthExit(Sender: TObject);
    procedure edHeightExit(Sender: TObject);
  private
    { Private declarations }
  public
    CurrentWorkWidth: string;
    CurrentWorkHeight: string;
    procedure SetPageParamsOnChange;
    { Public declarations }
  end;

var
  F_PageParams: TF_PageParams;

implementation
uses USCS_Main, U_CAD, U_Common;

{$R *.dfm}

procedure TF_PageParams.bCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TF_PageParams.FormShow(Sender: TObject);
begin
  try
    // показать текущие значения CAD
    if GCadForm.PCad.PageLayout = plA0 then
      cbPageSize.ItemIndex := 0;
    if GCadForm.PCad.PageLayout = plA1 then
      cbPageSize.ItemIndex := 1;
    if GCadForm.PCad.PageLayout = plA2 then
      cbPageSize.ItemIndex := 2;
    if GCadForm.PCad.PageLayout = plA3 then
      cbPageSize.ItemIndex := 3;
    if GCadForm.PCad.PageLayout = plA4 then
      cbPageSize.ItemIndex := 4;
    if GCadForm.PCad.PageLayout = plA5 then
      cbPageSize.ItemIndex := 5;
    if GCadForm.PCad.PageLayout = plA6 then
      cbPageSize.ItemIndex := 6;
    if GCadForm.PCad.PageLayout = plB4 then
      cbPageSize.ItemIndex := 7;
    if GCadForm.PCad.PageLayout = plB5 then
      cbPageSize.ItemIndex := 8;
    if GCadForm.PCad.PageLayout = plLetter then
      cbPageSize.ItemIndex := 9;
    if GCadForm.PCad.PageLayout = plTabloid then
      cbPageSize.ItemIndex := 10;
    if GCadForm.PCad.PageLayout = plCustom then
      cbPageSize.ItemIndex := 11;
    // размеры листа
    edWidth.Value := GCadForm.PCad.WorkWidth;
    edHeight.Value := GCadForm.PCad.WorkHeight;
    CurrentWorkWidth := edWidth.Value;
    CurrentWorkHeight := edHeight.Value;
    // ориентация листа
    if GCadForm.PCad.PageOrient = poPortrait then
      rbPortrait.Checked := true;
    if GCadForm.PCad.PageOrient = poLandscape then
      rbLandscape.Checked := true;
    bOK.SetFocus;
  except
    ShowMessage('EXCEPTION: TF_PageParams.FormShow');
  end;
end;

procedure TF_PageParams.bDefaultClick(Sender: TObject);
begin
  try
    cbPageSize.ItemIndex := 4;      // A4
    FSCS_Main.aA4Execute(Sender);
    edWidth.Value := 297;
    edHeight.Value := 210;
    rbLandscape.Checked := true;
    FSCS_Main.aLandscaleExecute(Sender);
  except
    ShowMessage('EXCEPTION: TF_PageParams.bDefaultClick');
  end;
end;

procedure TF_PageParams.bOKClick(Sender: TObject);
begin
  try
    if rbPortrait.Checked then
      FSCS_Main.aPortraitExecute(Sender)
    else
    if rbLandscape.Checked then
      FSCS_Main.aLandscaleExecute(Sender);

    if cbPageSize.ItemIndex = 0 then
      FSCS_Main.aA0Execute(Sender);
    if cbPageSize.ItemIndex = 1 then
      FSCS_Main.aA1Execute(Sender);
    if cbPageSize.ItemIndex = 2 then
      FSCS_Main.aA2Execute(Sender);
    if cbPageSize.ItemIndex = 3 then
      FSCS_Main.aA3Execute(Sender);
    if cbPageSize.ItemIndex = 4 then
      FSCS_Main.aA4Execute(Sender);
    if cbPageSize.ItemIndex = 5 then
      FSCS_Main.aA5Execute(Sender);
    if cbPageSize.ItemIndex = 6 then
      FSCS_Main.aA6Execute(Sender);
    if cbPageSize.ItemIndex = 7 then
      FSCS_Main.aB4Execute(Sender);
    if cbPageSize.ItemIndex = 8 then
      FSCS_Main.aB5Execute(Sender);
    if cbPageSize.ItemIndex = 9 then
      FSCS_Main.aLetterExecute(Sender);
    if cbPageSize.ItemIndex = 10 then
      FSCS_Main.aTabloidExecute(Sender);
    if cbPageSize.ItemIndex = 11 then
      FSCS_Main.aCustomExecute(Sender);

    if GCadForm.PCad.WorkWidth <> edWidth.Value then
      GCadForm.PCad.WorkWidth := edWidth.Value;
    if GCadForm.PCad.WorkHeight <> edHeight.Value then
      GCadForm.PCad.WorkHeight := edHeight.Value;
    GCadForm.ChangeScrollsOnChangeListSize;
    ReAssignNavigatorParams;     
  except
    ShowMessage('EXCEPTION: TF_PageParams.bOKClick');
  end;
end;

procedure TF_PageParams.rbPortraitClick(Sender: TObject);
begin
  SetPageParamsOnChange;
end;

procedure TF_PageParams.rbLandscapeClick(Sender: TObject);
begin
  SetPageParamsOnChange;
end;

procedure TF_PageParams.cbPageSizePropertiesCloseUp(Sender: TObject);
begin
  SetPageParamsOnChange;
end;

procedure TF_PageParams.SetPageParamsOnChange;
begin
  try
    if cbPageSize.ItemIndex = 0 then
    begin
      if rbLandscape.Checked then
      begin
        edWidth.Value := 1189;
        edHeight.Value := 841;
      end;
      if rbPortrait.Checked then
      begin
        edWidth.Value := 841;
        edHeight.Value := 1189;
      end;
    end;
    if cbPageSize.ItemIndex = 1 then
    begin
      if rbLandscape.Checked then
      begin
        edWidth.Value := 841;
        edHeight.Value := 594;
      end;
      if rbPortrait.Checked then
      begin
        edWidth.Value := 594;
        edHeight.Value := 841;
      end;
    end;
    if cbPageSize.ItemIndex = 2 then
    begin
      if rbLandscape.Checked then
      begin
        edWidth.Value := 594;
        edHeight.Value := 421;
      end;
      if rbPortrait.Checked then
      begin
        edWidth.Value := 421;
        edHeight.Value := 594;
      end;
    end;
    if cbPageSize.ItemIndex = 3 then
    begin
      if rbLandscape.Checked then
      begin
        edWidth.Value := 421;
        edHeight.Value := 297;
      end;
      if rbPortrait.Checked then
      begin
        edWidth.Value := 297;
        edHeight.Value := 421;
      end;
    end;
    if cbPageSize.ItemIndex = 4 then
    begin
      if rbLandscape.Checked then
      begin
        edWidth.Value := 297;
        edHeight.Value := 210;
      end;
      if rbPortrait.Checked then
      begin
        edWidth.Value := 210;
        edHeight.Value := 297;
      end;
    end;
    if cbPageSize.ItemIndex = 5 then
    begin
      if rbLandscape.Checked then
      begin
        edWidth.Value := 210;
        edHeight.Value := 148;
      end;
      if rbPortrait.Checked then
      begin
        edWidth.Value := 148;
        edHeight.Value := 210;
      end;
    end;
    if cbPageSize.ItemIndex = 6 then
    begin
      if rbLandscape.Checked then
      begin
        edWidth.Value := 105;
        edHeight.Value := 74;
      end;
      if rbPortrait.Checked then
      begin
        edWidth.Value := 74;
        edHeight.Value := 105;
      end;
    end;
    if cbPageSize.ItemIndex = 7 then
    begin
      if rbLandscape.Checked then
      begin
        edWidth.Value := 353;
        edHeight.Value := 250;
      end;
      if rbPortrait.Checked then
      begin
        edWidth.Value := 250;
        edHeight.Value := 353;
      end;
    end;
    if cbPageSize.ItemIndex = 8 then
    begin
      if rbLandscape.Checked then
      begin
        edWidth.Value := 250;
        edHeight.Value := 176;
      end;
      if rbPortrait.Checked then
      begin
        edWidth.Value := 176;
        edHeight.Value := 250;
      end;
    end;
    if cbPageSize.ItemIndex = 9 then
    begin
      if rbLandscape.Checked then
      begin
        edWidth.Value := 279;
        edHeight.Value := 215;
      end;
      if rbPortrait.Checked then
      begin
        edWidth.Value := 215;
        edHeight.Value := 279;
      end;
    end;
    if cbPageSize.ItemIndex = 10 then
    begin
      if rbLandscape.Checked then
      begin
        edWidth.Value := 431;
        edHeight.Value := 279;
      end;
      if rbPortrait.Checked then
      begin
        edWidth.Value := 279;
        edHeight.Value := 431;
      end;
    end;
  except
    ShowMessage('TF_PageParams.cbPageSizePropertiesCloseUp');
  end;
end;

procedure TF_PageParams.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #27 then
    Close;
end;

procedure TF_PageParams.edWidthKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    bOK.SetFocus;
    bOK.Click;        
  end;
end;

procedure TF_PageParams.edHeightKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    bOK.SetFocus;
    bOK.Click;        
  end;
end;

procedure TF_PageParams.edWidthExit(Sender: TObject);
begin
  if edWidth.Value = '' then
    edWidth.Value := CurrentWorkWidth;
end;

procedure TF_PageParams.edHeightExit(Sender: TObject);
begin
  if edHeight.Value = '' then
    edHeight.Value := CurrentWorkHeight;
end;

end.

