unit Main;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  FMX.ListBox, FBarcode, FMX.Edit, FMX.Controls.Presentation,
  FMX.Layouts, FMX.ExtCtrls, FMX.Objects, FMX.ScrollBox, FMX.Memo, FMX.Colors,
  FMX.EditBox, FMX.SpinBox, FMX.Memo.Types;

type
  TFormMain = class(TForm)
    ComboBoxSymbology: TComboBox;
    Barcode: TFBarcode;
    Image: TImage;
    StatusBar: TStatusBar;
    MemoInputText: TMemo;
    LabelStatus: TLabel;
    LabelSymbology: TLabel;
    ComboColorBoxForeColor: TComboColorBox;
    LabelForeColor: TLabel;
    LabelBackColor: TLabel;
    ComboColorBoxBackColor: TComboColorBox;
    LabelBorder: TLabel;
    SpinBoxBorder: TSpinBox;
    LabelWhiteSpace: TLabel;
    SpinBoxWhiteSpace: TSpinBox;
    LabelScale: TLabel;
    LabelDotSize: TLabel;
    SpinBoxDotSize: TSpinBox;
    SpinBoxScale: TSpinBox;
    LabelHeight: TLabel;
    SpinBoxHeight: TSpinBox;
    LabelRotation: TLabel;
    ComboBoxRotation: TComboBox;
    CheckBoxShowText: TCheckBox;
    CheckBoxSmallText: TCheckBox;
    CheckBoxBoldText: TCheckBox;
    CheckBoxBars: TCheckBox;
    CheckBoxBox: TCheckBox;
    CheckBoxDotty: TCheckBox;
    ButtonSaveBmp: TButton;
    ButtonSaveEps: TButton;
    ButtonSaveGif: TButton;
    ButtonSavePcx: TButton;
    ButtonSaveTxt: TButton;
    ButtonSaveSvg: TButton;
    SaveDialog: TSaveDialog;
    MemoPrimaryMessage: TMemo;
    procedure ComboBoxSymbologyChange(Sender: TObject);
    procedure MemoInputTextChangeTracking(Sender: TObject);
    procedure ComboColorBoxForeColorChange(Sender: TObject);
    procedure ComboColorBoxBackColorChange(Sender: TObject);
    procedure SpinBoxBorderChange(Sender: TObject);
    procedure SpinBoxWhiteSpaceChange(Sender: TObject);
    procedure SpinBoxScaleChange(Sender: TObject);
    procedure SpinBoxDotSizeChange(Sender: TObject);
    procedure SpinBoxHeightChange(Sender: TObject);
    procedure ComboBoxRotationChange(Sender: TObject);
    procedure CheckBoxShowTextChange(Sender: TObject);
    procedure CheckBoxSmallTextChange(Sender: TObject);
    procedure CheckBoxBoldTextChange(Sender: TObject);
    procedure CheckBoxBarsChange(Sender: TObject);
    procedure CheckBoxBoxChange(Sender: TObject);
    procedure CheckBoxDottyChange(Sender: TObject);
    procedure ButtonSaveBmpClick(Sender: TObject);
    procedure ButtonSaveEpsClick(Sender: TObject);
    procedure ButtonSaveGifClick(Sender: TObject);
    procedure ButtonSavePcxClick(Sender: TObject);
    procedure ButtonSaveTxtClick(Sender: TObject);
    procedure ButtonSaveSvgClick(Sender: TObject);
    procedure MemoPrimaryMessageChangeTracking(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    Initialized: Boolean;
    procedure ShowBarcode;
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

{$R *.fmx}

procedure TFormMain.ShowBarcode;
begin
  if Initialized then
  try
    LabelStatus.Text := '';
    Image.Bitmap.Assign(Barcode.Bitmap);
  except
    on E: Exception do
    begin
      LabelStatus.Text := E.Message;
      Image.Bitmap.Assign(nil);
    end;
  end;
end;

procedure TFormMain.FormShow(Sender: TObject);
var
  Symbology: TSymbology;
  Items: TStringList;
begin
  if not Initialized then
  begin
    ComboColorBoxForeColor.Color := Barcode.ForeColor;
    ComboColorBoxBackColor.Color := Barcode.BackColor;
    SpinBoxBorder.Value := Barcode.BorderWidth;
    SpinBoxWhiteSpace.Value := Barcode.WhiteSpaceWidth;
    SpinBoxScale.Value := Barcode.Scale;
    SpinBoxDotSize.Value := Barcode.DotSize;
    SpinBoxHeight.Value := Barcode.Height;
    ComboBoxRotation.ItemIndex := Barcode.Rotation div 90;
    CheckBoxShowText.IsChecked := Barcode.ShowText;
    CheckBoxSmallText.IsChecked := opSmallText in Barcode.OutputOptions;
    CheckBoxBoldText.IsChecked := opBoldText in Barcode.OutputOptions;
    CheckBoxBars.IsChecked := opBars in Barcode.OutputOptions;
    CheckBoxBox.IsChecked := opBox in Barcode.OutputOptions;
    CheckBoxDotty.IsChecked := opDotty in Barcode.OutputOptions;
    MemoInputText.Text := Barcode.InputText;
    MemoPrimaryMessage.Text := Barcode.PrimaryMessage;

    Items := TStringList.Create;
    try
      for Symbology := Low(Symbology) to High(Symbology) do
        Items.Add(Barcode.SymbologyNames[Symbology]);
      Items.Sort;

      ComboBoxSymbology.Items.Assign(Items);
      ComboBoxSymbology.ItemIndex := ComboBoxSymbology.Items.IndexOf(Barcode.SymbologyName);
    finally
      Items.Free;
    end;

    Initialized := True;
    ShowBarcode;
  end;
end;

procedure TFormMain.ComboBoxSymbologyChange(Sender: TObject);
begin
  Barcode.SymbologyName := ComboBoxSymbology.Selected.Text;

  CheckBoxDotty.Visible := caDotty in Barcode.Capabilities;
  LabelDotSize.Visible := CheckBoxDotty.Visible;
  SpinBoxDotSize.Visible := CheckBoxDotty.Visible;
  if not CheckBoxDotty.Visible then
    Barcode.OutputOptions := Barcode.OutputOptions - [opDotty]
  else if CheckBoxDotty.IsChecked then
    Barcode.OutputOptions := Barcode.OutputOptions + [opDotty];

  CheckBoxShowText.Visible := caHumanReadable in Barcode.Capabilities;
  CheckBoxSmallText.Visible := CheckBoxShowText.Visible;
  CheckBoxBoldText.Visible := CheckBoxShowText.Visible;

  ShowBarcode;
end;

procedure TFormMain.ComboColorBoxForeColorChange(Sender: TObject);
begin
  Barcode.ForeColor := ComboColorBoxForeColor.Color;
  ShowBarcode;
end;

procedure TFormMain.ComboColorBoxBackColorChange(Sender: TObject);
begin
  Barcode.BackColor := ComboColorBoxBackColor.Color;
  ShowBarcode;
end;

procedure TFormMain.SpinBoxBorderChange(Sender: TObject);
begin
  Barcode.BorderWidth := Round(SpinBoxBorder.Value);
  ShowBarcode;
end;

procedure TFormMain.SpinBoxWhiteSpaceChange(Sender: TObject);
begin
  Barcode.WhiteSpaceWidth := Round(SpinBoxWhiteSpace.Value);
  Barcode.WhiteSpaceHeight := Round(SpinBoxWhiteSpace.Value);
  ShowBarcode;
end;

procedure TFormMain.SpinBoxScaleChange(Sender: TObject);
begin
  Barcode.Scale := SpinBoxScale.Value;
  ShowBarcode;
end;

procedure TFormMain.SpinBoxDotSizeChange(Sender: TObject);
begin
  Barcode.DotSize := SpinBoxDotSize.Value;
  ShowBarcode;
end;

procedure TFormMain.SpinBoxHeightChange(Sender: TObject);
begin
  Barcode.Height := Round(SpinBoxHeight.Value);
  ShowBarcode;
end;

procedure TFormMain.ComboBoxRotationChange(Sender: TObject);
begin
  case ComboBoxRotation.ItemIndex of
    0: Barcode.Rotation := 0;
    1: Barcode.Rotation := 90;
    2: Barcode.Rotation := 180;
    3: Barcode.Rotation := 270;
  end;
  ShowBarcode;
end;

procedure TFormMain.CheckBoxShowTextChange(Sender: TObject);
begin
  Barcode.ShowText := CheckBoxShowText.IsChecked;
  CheckBoxSmallText.Enabled := CheckBoxShowText.IsChecked;
  CheckBoxBoldText.Enabled := CheckBoxShowText.IsChecked;
  ShowBarcode;
end;

procedure TFormMain.CheckBoxSmallTextChange(Sender: TObject);
begin
  if CheckBoxSmallText.IsChecked then
    Barcode.OutputOptions := Barcode.OutputOptions + [opSmallText]
  else
    Barcode.OutputOptions := Barcode.OutputOptions - [opSmallText];
  ShowBarcode;
end;

procedure TFormMain.CheckBoxBoldTextChange(Sender: TObject);
begin
  if CheckBoxBoldText.IsChecked then
    Barcode.OutputOptions := Barcode.OutputOptions + [opBoldText]
  else
    Barcode.OutputOptions := Barcode.OutputOptions - [opBoldText];
  ShowBarcode;
end;

procedure TFormMain.CheckBoxBarsChange(Sender: TObject);
begin
  if CheckBoxBars.IsChecked then
    Barcode.OutputOptions := Barcode.OutputOptions + [opBars]
  else
    Barcode.OutputOptions := Barcode.OutputOptions - [opBars];
  ShowBarcode;
end;

procedure TFormMain.CheckBoxBoxChange(Sender: TObject);
begin
  if CheckBoxBox.IsChecked then
    Barcode.OutputOptions := Barcode.OutputOptions + [opBox]
  else
    Barcode.OutputOptions := Barcode.OutputOptions - [opBox];
  ShowBarcode;
end;

procedure TFormMain.CheckBoxDottyChange(Sender: TObject);
begin
  if CheckBoxDotty.IsChecked then
    Barcode.OutputOptions := Barcode.OutputOptions + [opDotty]
  else
    Barcode.OutputOptions := Barcode.OutputOptions - [opDotty];
  ShowBarcode;
end;

procedure TFormMain.ButtonSaveBmpClick(Sender: TObject);
begin
  SaveDialog.Filter := 'Bitmap (*.bmp)|*.bmp';
  SaveDialog.DefaultExt := 'bmp';
  if SaveDialog.Execute then
    Barcode.Save(SaveDialog.FileName);
end;

procedure TFormMain.ButtonSaveEpsClick(Sender: TObject);
begin
  SaveDialog.Filter := 'EPS (*.eps)|*.eps';
  SaveDialog.DefaultExt := 'eps';
  if SaveDialog.Execute then
    Barcode.Save(SaveDialog.FileName);
end;

procedure TFormMain.ButtonSaveGifClick(Sender: TObject);
begin
  SaveDialog.Filter := 'GIF (*.gif)|*.gif';
  SaveDialog.DefaultExt := 'gif';
  if SaveDialog.Execute then
    Barcode.Save(SaveDialog.FileName);
end;

procedure TFormMain.ButtonSavePcxClick(Sender: TObject);
begin
  SaveDialog.Filter := 'PCX (*.pcx)|*.pcx';
  SaveDialog.DefaultExt := 'pcx';
  if SaveDialog.Execute then
    Barcode.Save(SaveDialog.FileName);
end;

procedure TFormMain.ButtonSaveTxtClick(Sender: TObject);
begin
  SaveDialog.Filter := 'Text (*.txt)|*.txt';
  SaveDialog.DefaultExt := 'txt';
  if SaveDialog.Execute then
    Barcode.Save(SaveDialog.FileName);
end;

procedure TFormMain.ButtonSaveSvgClick(Sender: TObject);
begin
  SaveDialog.Filter := 'SVG (*.svg)|*.svg';
  SaveDialog.DefaultExt := 'svg';
  if SaveDialog.Execute then
    Barcode.Save(SaveDialog.FileName);
end;

procedure TFormMain.MemoInputTextChangeTracking(Sender: TObject);
begin
  Barcode.InputText := MemoInputText.Text;
  ShowBarcode;
end;

procedure TFormMain.MemoPrimaryMessageChangeTracking(Sender: TObject);
begin
  Barcode.PrimaryMessage := MemoPrimaryMessage.Text;
  ShowBarcode;
end;

end.
