{ *************************************************************************** }
{                                                                             }
{ EControl Syntax Editor SDK                                                  }
{                                                                             }
{ Copyright (c) 2004 - 2008 EControl Ltd., Zaharov Michael                    }
{     www.econtrol.ru                                                         }
{     support@econtrol.ru                                                     }
{                                                                             }
{ *************************************************************************** }
{$I ecSyntEdit.INC}

unit ecPageSetupBase;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ecPrint, StdCtrls, Printers, ecSyntMemo, ecSyntAnal,
  ecPopupCtrl, ecStrUtils;

type
  TPageSetupBaseDlg = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    EditLeft: TEdit;
    EditRight: TEdit;
    EditTop: TEdit;
    EditBottom: TEdit;
    CBUnits: TComboBox;
    GroupBox1: TGroupBox;
    Label6: TLabel;
    ComboBox1: TComboBox;
    Button1: TButton;
    Button2: TButton;
    Label7: TLabel;
    ComboBox2: TComboBox;
    Bevel1: TBevel;
    GroupBox2: TGroupBox;
    SyntaxMemo1: TSyntaxMemo;
    GroupBox3: TGroupBox;
    SyntaxMemo2: TSyntaxMemo;
    GroupBox4: TGroupBox;
    Button4: TButton;
    Button5: TButton;
    FontDialog1: TFontDialog;
    SyntAnalyzer1: TSyntAnalyzer;
    AutoCompletePopup1: TAutoCompletePopup;
    procedure FormShow(Sender: TObject);
    procedure CBUnitsChange(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure SyntaxMemo1Enter(Sender: TObject);
    procedure AutoCompletePopup1CheckChar(Sender: TObject; C: Word;
      var IsWord: Boolean);
  private
    FUnits: TPrintUnits;
    function CheckMargins: Boolean;
  public
    procedure SetParams(ecPrinter: TecCustomPrinter); virtual;
    procedure GetParams(ecPrinter: TecCustomPrinter); virtual;
  end;

implementation

{$R *.dfm}

procedure TPageSetupBaseDlg.GetParams(ecPrinter: TecCustomPrinter);
begin
  with ecPrinter do
   begin
    Units := FUnits;
    MarginLeft := StrToFloat(EditLeft.Text);
    MarginRight := StrToFloat(EditRight.Text);
    MarginTop := StrToFloat(EditTop.Text);
    MarginBottom := StrToFloat(EditBottom.Text);

    Orientation := TPrinterOrientation(ComboBox1.ItemIndex);
    ColorMode := TPrintColorMode(ComboBox2.ItemIndex);

    FontHeader := Button4.Font;
    FontFooter := Button5.Font;

    Header := SyntaxMemo1.Lines.Text;
    Footer := SyntaxMemo2.Lines.Text;
   end;
end;

procedure TPageSetupBaseDlg.SetParams(ecPrinter: TecCustomPrinter);
var i: integer;
begin
  with ecPrinter do
   begin
    FUnits := Units;
    CBUnits.ItemIndex := integer(Units);
    EditLeft.Text := FormatFloat('0.###', MarginLeft);
    EditRight.Text := FormatFloat('0.###', MarginRight);
    EditTop.Text := FormatFloat('0.###', MarginTop);
    EditBottom.Text := FormatFloat('0.###', MarginBottom);

    ComboBox1.ItemIndex := integer(Orientation);
    ComboBox2.ItemIndex := integer(ColorMode);

    Button4.Font := FontHeader;
    Button5.Font := FontFooter;

    SyntaxMemo1.Lines.Text := Header;
    SyntaxMemo1.Font := FontHeader;

    SyntaxMemo2.Lines.Text := Footer;
    SyntaxMemo2.Font := FontFooter;
    for i := 0 to Fields.Count - 1 do
     begin
       AutoCompletePopup1.Items.Add('#' + Fields[i] + '#');
       AutoCompletePopup1.DisplayItems.Add(Fields[i]);
     end;
   end;
end;

procedure TPageSetupBaseDlg.FormShow(Sender: TObject);
begin
  AutoCompletePopup1.OnCheckChar := AutoCompletePopup1CheckChar;
end;

procedure TPageSetupBaseDlg.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  if ModalResult = mrOK then
    CanClose := CheckMargins;
end;

function TPageSetupBaseDlg.CheckMargins: Boolean;
var cur: TEdit;
  function GetValue(Edit: TEdit): double;
  begin
   cur := Edit;
   Result := StrToFloat(Edit.Text);
  end;
begin
  try
    GetValue(EditLeft);
    GetValue(EditRight);
    GetValue(EditTop);
    GetValue(EditBottom);
    Result := True;
  except
    MessageBox(Handle, 'Invalid value', 'Error', MB_OK or MB_ICONHAND);
    cur.SetFocus;
    Result := False;
  end;
end;

procedure TPageSetupBaseDlg.CBUnitsChange(Sender: TObject);
var NewUnits: TPrintUnits;
  procedure Convert(Edit: TEdit);
  var v: double;
  begin
    v := StrToFloat(Edit.Text);
    // to Inch
    case FUnits of
      puCM: v := v / 2.54;
      puMM: v := v / 25.4;
      puThousandthsOfInches: v := v / 1000;
    end;
    // to NewUnits
    case NewUnits of
      puCM: v := v * 2.54;
      puMM: v := v * 25.4;
      puThousandthsOfInches: v := v * 1000;
    end;
    Edit.Text := FormatFloat('0.###', v);
  end;
begin
 if CheckMargins then
  begin
   NewUnits := TPrintUnits(CBUnits.ItemIndex);
   Convert(EditLeft);
   Convert(EditRight);
   Convert(EditTop);
   Convert(EditBottom);
   FUnits := NewUnits;
  end else
   CBUnits.ItemIndex := integer(FUnits);
end;

procedure TPageSetupBaseDlg.Button3Click(Sender: TObject);
begin
  FontDialog1.Font := TButton(Sender).Font;
  if FontDialog1.Execute then
    begin
     TButton(Sender).Font := FontDialog1.Font;
     if Sender = Button4 then
       SyntaxMemo1.Font := FontDialog1.Font;
     if Sender = Button5 then
       SyntaxMemo2.Font := FontDialog1.Font;
    end;
end;

procedure TPageSetupBaseDlg.SyntaxMemo1Enter(Sender: TObject);
begin
  AutoCompletePopup1.SyntMemo := TSyntaxMemo(Sender);
end;

procedure TPageSetupBaseDlg.AutoCompletePopup1CheckChar(Sender: TObject;
  C: Word; var IsWord: Boolean);
begin
  IsWord := IsWordChar(ecChar(C)) or (ecChar(C) = '#');
end;

end.

