//---------------------------------------------------------------------------
#include <fmx.h>
#pragma hdrstop

#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FBarcode"
#pragma resource "*.fmx"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ShowBarcode()
{
  if (Initialized)
    try
    {
      LabelStatus->Text = "";
      Image->Bitmap->Assign(Barcode->Bitmap);
    }
    catch (Exception &ex)
    {
      LabelStatus->Text = ex.Message;
      Image->Bitmap->Assign(NULL);
    }
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormShow(TObject *Sender)
{
  if (!Initialized)
  {
    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 / 90;
    CheckBoxShowText->IsChecked = Barcode->ShowText;
    CheckBoxSmallText->IsChecked = Barcode->OutputOptions.Contains(opSmallText);
    CheckBoxBoldText->IsChecked = Barcode->OutputOptions.Contains(opBoldText);
    CheckBoxBars->IsChecked = Barcode->OutputOptions.Contains(opBars);
    CheckBoxBox->IsChecked = Barcode->OutputOptions.Contains(opBox);
    CheckBoxDotty->IsChecked = Barcode->OutputOptions.Contains(opDotty);
    MemoInputText->Text = Barcode->InputText;
    MemoPrimaryMessage->Text = Barcode->PrimaryMessage;

    TStringList *items = new TStringList();
    try
    {
      for (TSymbology symbology = syCode11; symbology <= syRMQR; symbology = (TSymbology)(symbology + 1))
        items->Add(Barcode->SymbologyNames[symbology]);
      items->Sort();

      ComboBoxSymbology->Items->Assign(items);
      ComboBoxSymbology->ItemIndex = ComboBoxSymbology->Items->IndexOf(Barcode->SymbologyName);
    }
    __finally
    {
      delete items;
    }

    Initialized = true;
    ShowBarcode();
  }
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ComboBoxSymbologyChange(TObject *Sender)
{
  Barcode->SymbologyName = ComboBoxSymbology->Selected->Text;

  CheckBoxDotty->Visible = Barcode->Capabilities.Contains(caDotty);
  LabelDotSize->Visible = CheckBoxDotty->Visible;
  SpinBoxDotSize->Visible = CheckBoxDotty->Visible;
  if (!CheckBoxDotty->Visible)
    Barcode->OutputOptions >> opDotty;
  else if (CheckBoxDotty->IsChecked)
    Barcode->OutputOptions << opDotty;

  CheckBoxShowText->Visible = Barcode->Capabilities.Contains(caHumanReadable);
  CheckBoxSmallText->Visible = CheckBoxShowText->Visible;
  CheckBoxBoldText->Visible = CheckBoxShowText->Visible;

  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ComboColorBoxForeColorChange(TObject *Sender)
{
  Barcode->ForeColor = ComboColorBoxForeColor->Color;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ComboColorBoxBackColorChange(TObject *Sender)
{
  Barcode->BackColor = ComboColorBoxBackColor->Color;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SpinBoxBorderChange(TObject *Sender)
{
  Barcode->BorderWidth = floor(SpinBoxBorder->Value);
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SpinBoxWhiteSpaceChange(TObject *Sender)
{
  Barcode->WhiteSpaceWidth = floor(SpinBoxWhiteSpace->Value);
  Barcode->WhiteSpaceHeight = floor(SpinBoxWhiteSpace->Value);
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SpinBoxScaleChange(TObject *Sender)
{
  Barcode->Scale = SpinBoxScale->Value;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SpinBoxDotSizeChange(TObject *Sender)
{
  Barcode->DotSize = SpinBoxDotSize->Value;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SpinBoxHeightChange(TObject *Sender)
{
  Barcode->Height = floor(SpinBoxHeight->Value);
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ComboBoxRotationChange(TObject *Sender)
{
  switch (ComboBoxRotation->ItemIndex)
  {
    case 0: Barcode->Rotation = 0; break;
    case 1: Barcode->Rotation = 90; break;
    case 2: Barcode->Rotation = 180; break;
    case 3: Barcode->Rotation = 270; break;
  }
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::CheckBoxShowTextChange(TObject *Sender)
{
  Barcode->ShowText = CheckBoxShowText->IsChecked;
  CheckBoxSmallText->Enabled = CheckBoxShowText->IsChecked;
  CheckBoxBoldText->Enabled = CheckBoxShowText->IsChecked;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::CheckBoxSmallTextChange(TObject *Sender)
{
  TOutputOptions options = Barcode->OutputOptions;
  if (CheckBoxSmallText->IsChecked)
    Barcode->OutputOptions = options << opSmallText;
  else
    Barcode->OutputOptions = options >> opSmallText;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::CheckBoxBoldTextChange(TObject *Sender)
{
  TOutputOptions options = Barcode->OutputOptions;
  if (CheckBoxBoldText->IsChecked)
    Barcode->OutputOptions = options << opBoldText;
  else
    Barcode->OutputOptions = options >> opBoldText;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::CheckBoxBarsChange(TObject *Sender)
{
  TOutputOptions options = Barcode->OutputOptions;
  if (CheckBoxBars->IsChecked)
    Barcode->OutputOptions = options << opBars;
  else
    Barcode->OutputOptions = options >> opBars;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::CheckBoxBoxChange(TObject *Sender)
{
  TOutputOptions options = Barcode->OutputOptions;
  if (CheckBoxBox->IsChecked)
    Barcode->OutputOptions = options << opBox;
  else
    Barcode->OutputOptions = options >> opBox;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::CheckBoxDottyChange(TObject *Sender)
{
  TOutputOptions options = Barcode->OutputOptions;
  if (CheckBoxDotty->IsChecked)
    Barcode->OutputOptions = options << opDotty;
  else
    Barcode->OutputOptions = options >> opDotty;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonSaveBmpClick(TObject *Sender)
{
  SaveDialog->Filter = "Bitmap (*.bmp)|*.bmp";
  SaveDialog->DefaultExt = "bmp";
  if (SaveDialog->Execute())
    Barcode->Save(SaveDialog->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonSaveEpsClick(TObject *Sender)
{
  SaveDialog->Filter = "EPS (*.eps)|*.eps";
  SaveDialog->DefaultExt = "eps";
  if (SaveDialog->Execute())
    Barcode->Save(SaveDialog->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonSaveGifClick(TObject *Sender)
{
  SaveDialog->Filter = "GIF (*.gif)|*.gif";
  SaveDialog->DefaultExt = "gif";
  if (SaveDialog->Execute())
    Barcode->Save(SaveDialog->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonSavePcxClick(TObject *Sender)
{
  SaveDialog->Filter = "PCX (*.pcx)|*.pcx";
  SaveDialog->DefaultExt = "pcx";
  if (SaveDialog->Execute())
    Barcode->Save(SaveDialog->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonSaveTxtClick(TObject *Sender)
{
  SaveDialog->Filter = "Text (*.txt)|*.txt";
  SaveDialog->DefaultExt = "txt";
  if (SaveDialog->Execute())
    Barcode->Save(SaveDialog->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonSaveSvgClick(TObject *Sender)
{
  SaveDialog->Filter = "SVG (*.svg)|*.svg";
  SaveDialog->DefaultExt = "svg";
  if (SaveDialog->Execute())
    Barcode->Save(SaveDialog->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::MemoInputTextChangeTracking(TObject *Sender)
{
  Barcode->InputText = MemoInputText->Text;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::MemoPrimaryMessageChangeTracking(TObject *Sender)
{
  Barcode->PrimaryMessage = MemoPrimaryMessage->Text;
  ShowBarcode();
}
//---------------------------------------------------------------------------
