//---------------------------------------------------------------------------
#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 TFormMain::ShowBarcode(void)
{
  try
  {
    LabelError->Text = "";

    Image->BeginUpdate();
    try
    {
      Image->Bitmap->Assign(NULL);
      Image->SetBounds(
       (PanelBarcode->Width - FBarcode->Bitmap->Width / Scale) / 2.0,
       (PanelBarcode->Height - FBarcode->Bitmap->Height / Scale) / 2.0,
       FBarcode->Bitmap->Width / Scale,
       FBarcode->Bitmap->Height / Scale);
      Image->Bitmap->Assign(FBarcode->Bitmap);
    }
    __finally
    {
      Image->EndUpdate();
    }
  }
  catch (Exception &ex)
  {
    LabelError->Text = ex.Message;
    Image->Bitmap->Assign(NULL);
  }
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::TrackBarChange(TObject *Sender)
{
  FBarcode->Scale = TrackBar->Value / 10.0;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ComboBoxChange(TObject *Sender)
{
  FBarcode->SymbologyName = ComboBox->Selected->Text;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ComboColorBoxBackColorChange(TObject *Sender)
{
  FBarcode->BackColor = ComboColorBoxBackColor->Color;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ComboColorBoxForeColorChange(TObject *Sender)
{
  FBarcode->ForeColor = ComboColorBoxForeColor->Color;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::EditChange(TObject *Sender)
{
  FBarcode->InputText = Edit->Text;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::EditTyping(TObject *Sender)
{
  FBarcode->InputText = Edit->Text;
  ShowBarcode();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
  Scale = 1.0;
  if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXScreenService)))
  {
    _di_IFMXScreenService screenService = TPlatformServices::Current->GetPlatformService(__uuidof(IFMXScreenService));
    if (screenService)
      Scale = screenService->GetScreenScale();
  }

  FBarcode->InputText = Edit->Text;

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

    ComboBox->Items->Assign(items);
    ComboBox->ItemIndex = ComboBox->Items->IndexOf("QR Code");
  }
  __finally
  {
    delete items;
  }
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormResize(TObject *Sender)
{
  ShowBarcode();
}
//---------------------------------------------------------------------------