unit fmuMain;

////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                               //
////////////////////////////////////////////////////////////////////////////////////////////////////

interface

uses
  // VCL
  Windows, ActiveX, SysUtils, Classes, Graphics, Forms, ComObj, Registry, Messages,
  StdCtrls, Controls, Dialogs, ComCtrls, ExtCtrls, ToolWin, ExtDlgs,
  // Shared
  PrnRegStr,
  // This
  untUtil,
  fmuPrint,
  fmuStatus,
  fmuDevices,
  fmuModelInformation,
  fmuDemo,
  xObj;

const
  WM_SHOWDEMO = WM_USER + 1;

type

  { TfmMain }

  TfmMain = class(TForm)
    Panel2: TPanel;
    Shape1: TShape;
    Label9: TLabel;
    Label10: TLabel;
    Panel3: TPanel;
    PanelLeft: TPanel;
    Panel4: TPanel;
    FontDialog: TFontDialog;
    pnlButtons: TPanel;
    pnlDescription: TPanel;
    edtResultCode: TEdit;
    edtResultDescription: TEdit;
    btnShowProperties: TButton;
    btnClose: TButton;
    btnFont: TButton;
    btnDevices: TButton;
    chbDeviceEnabled: TCheckBox;
    chbLockDevices: TCheckBox;
    Panel5: TPanel;
    Panel7: TPanel;
    Panel6: TPanel;
    Panel8: TPanel;
    ToolBar: TToolBar;
    btnClearTask: TToolButton;
    btnLoadTask: TToolButton;
    btnSaveTask: TToolButton;
    btnPrint: TToolButton;
    ToolButton7: TToolButton;
    ToolButton1: TToolButton;
    pnlPageParent: TPanel;
    btnShowSpooler: TToolButton;
    btnTestConnection: TToolButton;
    SaveTaskDialog: TSaveDialog;
    OpenTaskDialog: TOpenDialog;
    ImageList: TImageList;
    chbRaiseException: TCheckBox;
    chbShowMessage: TCheckBox;
    PageControl: TPageControl;
    tbCommon: TTabSheet;
    tbText: TTabSheet;
    btnAddText: TButton;
    TextCaption: TLabel;
    chbTextWrap: TCheckBox;
    tbPict: TTabSheet;
    ScrollBox1: TScrollBox;
    btnAddPicture: TButton;
    Image: TImage;
    OpenPictureDialog: TOpenPictureDialog;
    tbAdvenced: TTabSheet;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    cmbCutValue: TComboBox;
    btnAddCut: TButton;
    btnAddFeed: TButton;
    Label1: TLabel;
    Label2: TLabel;
    edtFeedValue: TEdit;
    cmbCodePage: TComboBox;
    lblCodePage: TLabel;
    lblCharSet: TLabel;
    cmbCharSet: TComboBox;
    edtCaptionTxt: TEdit;
    chbTextNewLine: TCheckBox;
    Label3: TLabel;
    edtFontIndex: TEdit;
    chbZeroSlashed: TCheckBox;
    Bevel1: TBevel;
    chbFontBold: TCheckBox;
    chbFontItalic: TCheckBox;
    chbFontDblHeight: TCheckBox;
    chbFontDblWidth: TCheckBox;
    chbFontOverLine: TCheckBox;
    chbFontUnderLine: TCheckBox;
    chbFontNegative: TCheckBox;
    chbTextUpSideDown: TCheckBox;
    Label5: TLabel;
    cmbAlignmentTxt: TComboBox;
    cmbCharRotation: TComboBox;
    lbTextAligment: TLabel;
    lbTextRotate: TLabel;
    cmbColorTxt: TComboBox;
    GroupBox3: TGroupBox;
    Label6: TLabel;
    edtBeepValue: TEdit;
    btnAddBeep: TButton;
    edtLineSpacing: TEdit;
    Label8: TLabel;
    Label11: TLabel;
    cmbAlignmentPic: TComboBox;
    Label12: TLabel;
    cmbRotationPIc: TComboBox;
    btnInformation: TToolButton;
    tbDr: TTabSheet;
    GroupBox4: TGroupBox;
    Label13: TLabel;
    cmbDrawerValue: TComboBox;
    btnAddDrawer: TButton;
    GroupBox6: TGroupBox;
    Label15: TLabel;
    cmbSlipValue: TComboBox;
    btnAddSlip: TButton;
    tbBarCode: TTabSheet;
    edtCaptionBC: TEdit;
    Label14: TLabel;
    chbBarCodePrintText: TCheckBox;
    Label16: TLabel;
    edtBarCodeHeight: TEdit;
    Label17: TLabel;
    cmbColorPIc: TComboBox;
    btnAddBarCode: TButton;
    Label18: TLabel;
    cmbColorBC: TComboBox;
    cmbAlignmentBC: TComboBox;
    Label19: TLabel;
    Label20: TLabel;
    cmbRotationBC: TComboBox;
    lblIsDemoDscr: TLabel;
    lblIsDemo: TLabel;
    lblDemoDescription: TPanel;
    Label23: TLabel;
    btnPrint2: TButton;
    btnTestConnection2: TButton;
    btnShowSpooler2: TButton;
    chbBarCodeControlCode: TCheckBox;
    cmbBarCodeType: TComboBox;
    Label7: TLabel;
    Bevel2: TBevel;
    btnDemo: TToolButton;
    btnInformation2: TButton;
    btnDemo2: TButton;
    chbAutoSizePIC: TCheckBox;
    chbAutoSizeBC: TCheckBox;
    btnResetPrintSettings: TButton;
    procedure btnFontClick(Sender: TObject);
    procedure btnAboutClick(Sender: TObject);
    procedure EditorClick(Sender: TObject);
    procedure EditorExit(Sender: TObject);
    procedure EditorKeyPress(Sender: TObject; var Key: Char);
    procedure EditorChange(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure chlTextStyleClickCheck(Sender: TObject);
    procedure udEditorClick(Sender: TObject; Button: TUDBtnType);
  private  // Driver
    fCmdWaiting: Boolean;
    fShowDemoOnStart: Boolean;
    fDriver: TObjX;
    function DriverException(E: Exception): Boolean;
    property Driver: TObjX read fDriver;
    procedure ReadFromRegistry;
    procedure SaveToRegistry;
    procedure ShowDemo(var Message: TMessage); message WM_SHOWDEMO;
  private  // Values
    function ChangePropertyValue(Sender: TObject): Boolean;
    function  IsEditorControl(Value: TControl): Boolean;
    procedure PerformMethodValue(Sender: TObject);
    procedure UpdateControls;
  private
    procedure UpdateObject_DeviceEnabled;
    procedure UpdateObject_RaiseException;
    procedure UpdateObject_CodePage;
    procedure UpdateObject_CharSet;
    procedure UpdateObject_Caption(Value: TCustomEdit);
    procedure UpdateObject_TextWrap;
    procedure UpdateObject_TextNewLine;
    procedure UpdateObject_FontIndex;
    procedure UpdateObject_FontDblWidth;
    procedure UpdateObject_FontDblHeight;
    procedure UpdateObject_FontUnderLine;
    procedure UpdateObject_FontOverLine;
    procedure UpdateObject_FontNegative;
    procedure UpdateObject_TextUpSideDown;
    procedure UpdateObject_FontBold;
    procedure UpdateObject_FontItalic;
    procedure UpdateObject_ZeroSlashed;
    procedure UpdateObject_Color(Value: TComboBox);
    procedure UpdateObject_Rotation(Value: TComboBox);
    procedure UpdateObject_AutoSize(Value: TCheckBox);
    procedure UpdateObject_CharRotation;
    procedure UpdateObject_Alignment(Value: TComboBox);
    procedure UpdateObject_LockDevices;
    procedure UpdateObject_CutValue;
    procedure UpdateObject_FeedValue;
    procedure UpdateObject_BeepValue;
    procedure UpdateObject_SlipValue;
    procedure UpdateObject_LineSpacing;
    procedure UpdateObject_DrawerValue;
    procedure UpdateObject_BarCodeHeight;
    procedure UpdateObject_BarCodePrintText;
    procedure UpdateObject_BarCodeControlCode;
    procedure UpdateObject_BarCodeType;
    procedure Perform_AddText;
    procedure Perform_AddPicture;
    procedure Perform_AddCut;
    procedure Perform_AddFeed;
    procedure Perform_AddBeep;
    procedure Perform_AddDrawer;
    procedure Perform_AddSlip;
    procedure Perform_AddBarCode;
    procedure Perform_ClearTask;
    procedure Perform_ShowSpooler;
    procedure Perform_ShowProperties;
    procedure Perform_LoadTaskFromFile;
    procedure Perform_SaveTaskToFile;
    procedure Perform_ResetPrintSettings;
    //
    procedure Perform_ExecutePrintDialog;
    procedure Perform_ExecuteStatusDialog;
    procedure Perform_ExecuteInformationDialog;
    procedure Perform_ExecuteDemoDialog;
    procedure Perform_ExecuteDevices;
  public  // Standart
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

procedure ExecuteWinMain;

implementation

const
  PROPERTY_EDIT_TAG = 12345;

{$R *.DFM}

procedure ExecuteWinMain;
var
  fmMain: TfmMain;
begin
  Application.Initialize;
  Application.Title := ':   ';
  try
    Application.CreateForm(TfmMain,fmMain);
    try
      Application.Run;
    finally
      fmMain.Free;
    end;
  except
    Application.HandleException(nil);
  end;
end;

{ TfmMain }

constructor TfmMain.Create(AOwner: TComponent);

  procedure UpdateView;

    procedure UpdateColorItems(Value: TComboBox);
    begin
      with Value.Items do
      begin
        BeginUpdate;
        try
          Clear;
            AddObject('',         TObject(0));
            AddObject('', TObject(1));
        finally
          EndUpdate;
        end;
      end;
    end;

    procedure UpdateRotationItems(Value: TComboBox);
    begin
      with Value.Items do
      begin
        BeginUpdate;
        try
          Clear;
          AddObject(' ',    TObject(0));
          AddObject('  90',  TObject(1));
          AddObject('  180', TObject(2));
          AddObject('  270', TObject(3));
        finally
          EndUpdate;
        end;
      end;
    end;

    procedure UpdateAlignmentItems(Value: TComboBox);
    begin
      with Value.Items do
      begin
        BeginUpdate;
        try
          Clear;
          AddObject('  ',  TObject(0));
          AddObject(' ',       TObject(1));
          AddObject('  ', TObject(2));
        finally
          EndUpdate;
        end;
      end;
    end;


  begin
    PageControl.ActivePage := tbCommon;

    edtCaptionTxt.Tag := PROPERTY_EDIT_TAG;
    edtCaptionBC.Tag := PROPERTY_EDIT_TAG;
    edtFontIndex.Tag := PROPERTY_EDIT_TAG;
    edtLineSpacing.Tag := PROPERTY_EDIT_TAG;
    edtBeepValue.Tag := PROPERTY_EDIT_TAG;
    edtFeedValue.Tag := PROPERTY_EDIT_TAG;
    edtBarCodeHeight.Tag := PROPERTY_EDIT_TAG;

    with cmbCodePage.Items do
    begin
      BeginUpdate;
      try
        Clear;
        AddObject('Normal (Default)',         TObject(0));
        AddObject('437 (USA, Std. Europe)/IBM Character', TObject(1));
        AddObject('Katakana',                 TObject(2));
        AddObject('IBM Character Set #1',     TObject(3));
        AddObject('858 (Multilingual)',       TObject(4));
        AddObject('852 (Latin-2)',            TObject(5));
        AddObject('860 (Portuguese)',         TObject(6));
        AddObject('861 (Icelandic)',          TObject(7));
        AddObject('863 (Canadian French)',    TObject(8));
        AddObject('865 (Nordic)',             TObject(9));
        AddObject('866 (Cyrillic Russian)',   TObject(10));
        AddObject('855 (Cyrillic Bulgarian)', TObject(11));
        AddObject('857 (Turkish)',            TObject(12));
        AddObject('862 (Hebrew)',             TObject(13));
        AddObject('864 (Arabic)',             TObject(14));
        AddObject('737 (Greek)',              TObject(15));
        AddObject('851 (Greek)',              TObject(16));
        AddObject('869 (Greek)',              TObject(17));
        AddObject('928 (Greek)',              TObject(18));
        AddObject('772 (Lithuanian)',         TObject(19));
        AddObject('774 (Lithuanian)',         TObject(20));
        AddObject('874 (Thai)',               TObject(21));
        AddObject('1252 (Windows Latin-1)',   TObject(22));
        AddObject('1250 (Windows Latin-2)',   TObject(23));
        AddObject('1251 (Windows Cyrillic)',  TObject(24));
        AddObject('3840 (IBM-Russian)',       TObject(25));
        AddObject('3841 (Gost)',              TObject(26));
        AddObject('3843 (Polish)',            TObject(27));
        AddObject('3844 (CS2)',               TObject(28));
        AddObject('3845 (Hungarian)',         TObject(29));
        AddObject('3846 (Turkish)',           TObject(30));
        AddObject('3847 (Brazil-ABNT)',       TObject(31));
        AddObject('3848 (Brazil-ABICOMP)',    TObject(32));
        AddObject('1001 (Arabic)',            TObject(33));
        AddObject('2001 (Lithuanian-KBL)',    TObject(34));
        AddObject('3001 (Estonian-1)',        TObject(35));
        AddObject('3002 (Estonian-2)',        TObject(36));
        AddObject('3011 (Latvian-1)',         TObject(37));
        AddObject('3012 (Latvian-2)',         TObject(38));
        AddObject('3021 (Bulgarian)',         TObject(39));
        AddObject('3041 (Maltese)',           TObject(40));
      finally
        EndUpdate;
      end;
    end;

    with cmbCharSet.Items do
    begin
      BeginUpdate;
      try
        Clear;
        AddObject('USA',          TObject(0));
        AddObject('France',       TObject(1));
        AddObject('Germany',      TObject(2));
        AddObject('England',      TObject(3));
        AddObject('Denmark-1',    TObject(4));
        AddObject('Sweden',       TObject(5));
        AddObject('Italy',        TObject(6));
        AddObject('Spain-1',      TObject(7));
        AddObject('Japan',        TObject(8));
        AddObject('Norway',       TObject(9));
        AddObject('Denmark-2',    TObject(10));
        AddObject('Spain-2',      TObject(11));
        AddObject('LatinAmerica', TObject(12));
        AddObject('Korea',        TObject(13));
        AddObject('Ireland',      TObject(14));
        AddObject('Legal',        TObject(15));
      finally
        EndUpdate;
      end;
    end;

    UpdateColorItems(cmbColorTxt);
    UpdateColorItems(cmbColorPic);
    UpdateColorItems(cmbColorBC);

    UpdateRotationItems(cmbCharRotation);
    UpdateRotationItems(cmbRotationPic);
    UpdateRotationItems(cmbRotationBC);

    UpdateAlignmentItems(cmbAlignmentTxt);
    UpdateAlignmentItems(cmbAlignmentPic);
    UpdateAlignmentItems(cmbAlignmentBC);

    with cmbCutValue.Items do
    begin
      BeginUpdate;
      try
        Clear;
        AddObject(' ',        TObject(0));
        AddObject('',                   TObject(1));
        AddObject('',                TObject(2));
        AddObject('  ',       TObject(3));
        AddObject('  ',    TObject(4));
      finally
        EndUpdate;
      end;
    end;

    with cmbDrawerValue.Items do
    begin
      BeginUpdate;
      try
        Clear;
        AddObject(' ',          TObject(0));
        AddObject(' ',              TObject(1));
        AddObject(' ',              TObject(2));
      finally
        EndUpdate;
      end;
    end;

    with cmbSlipValue.Items do
    begin
      BeginUpdate;
      try
        Clear;
        AddObject(' ',          TObject(0));
        AddObject(' ',           TObject(1));
        AddObject(' ',           TObject(2));
        AddObject(' ',          TObject(3));
        AddObject(' ',         TObject(4));
      finally
        EndUpdate;
      end;
    end;

    with cmbBarCodeType.Items do
    begin
      BeginUpdate;
      try
        Clear;
        AddObject('EAN / UPC', TObject(0));
        AddObject('Code 39',   TObject(1));
      finally
        EndUpdate;
      end;
    end;

  end;

  procedure CreateDriver;
  begin
    fDriver := TObjX.Create;
    fDriver.ControlInterface.RaiseException := True;
    fCmdWaiting := False;
  end;

begin
  inherited Create(AOwner);
  GetRegistryFont(Font);
  ReadFromRegistry;
  UpdateView;
  CreateDriver;
  UpdateControls;
  PostMessage(Handle, WM_SHOWDEMO, 0, 0);
end;

destructor TfmMain.Destroy;

  procedure DoneDriver;
  begin
    fDriver.Free;
  end;

begin
  SaveToRegistry;
  DoneDriver;
  inherited Destroy;
end;

procedure TfmMain.ReadFromRegistry;

  procedure InitValues;
  begin
    fShowDemoOnStart := True;
  end;
var
  fRegistry: TRegistry;
begin
  InitValues;
  fRegistry:= TRegistry.Create;
  try
    fRegistry.RootKey:= HKEY_CURRENT_USER;
    fRegistry.OpenKey(REGSTR_KEY_PRNCHECK50_T, TRUE);
    if fRegistry.ValueExists('ShowDemoOnStart') then
      fShowDemoOnStart := fRegistry.ReadBool('ShowDemoOnStart')
  finally
    fRegistry.Free;
  end;
end;

procedure TfmMain.SaveToRegistry;
var
  fRegistry: TRegistry;
begin
  fRegistry := TRegistry.create;
  try
    fRegistry.RootKey := HKEY_CURRENT_USER;
    fRegistry.OpenKey(REGSTR_KEY_PRNCHECK50_T, True);
    fRegistry.WriteBool('ShowDemoOnStart', fShowDemoOnStart);
  finally
    fRegistry.Free;
  end;
end;

(****************************      ********************************)

function TfmMain.DriverException(E: Exception): Boolean;
begin
  Result := (E is EOleSysError)
    and (ResultFacility(EOleSysError(E).ErrorCode) = FACILITY_ITF);
  if Result then
  begin
    if chbShowMessage.Checked then
    begin
      with Application do
        MessageBox(PChar(E.Message), PChar(Title), mb_IconStop or mb_Ok);
    end;
  end
  else
  begin
    if E is EConvertError then
    begin
      E.Message := '    .' + #13 +
        ': ' + E.Message;
      Result := not chbShowMessage.Checked;
    end
    else
    begin
      E.Message := '    .' + #13 + ': ' + E.Message;
      Result := not chbShowMessage.Checked;
    end;
  end;
end;

function TfmMain.ChangePropertyValue(Sender: TObject): Boolean;
begin
  Result := True;
  try
    try
      if (Sender = edtCaptionTxt) or (Sender = edtCaptionBC)
        then UpdateObject_Caption(Sender as TCustomEdit)
      else if Sender = chbTextWrap then UpdateObject_TextWrap
      else if Sender = chbTextNewLine then UpdateObject_TextNewLine
      else if Sender = edtFontIndex then UpdateObject_FontIndex
      else if Sender = edtLineSpacing then UpdateObject_LineSpacing
      else if Sender = chbFontDblWidth then UpdateObject_FontDblWidth
      else if Sender = chbFontDblHeight then UpdateObject_FontDblHeight
      else if Sender = chbFontUnderLine then UpdateObject_FontUnderLine
      else if Sender = chbFontOverLine then UpdateObject_FontOverLine
      else if Sender = chbFontNegative then UpdateObject_FontNegative
      else if Sender = chbTextUpSideDown then UpdateObject_TextUpSideDown
      else if Sender = chbFontBold then UpdateObject_FontBold
      else if Sender = chbFontItalic then UpdateObject_FontItalic
      else if Sender = chbZeroSlashed then UpdateObject_ZeroSlashed
      else if (Sender = cmbColorTxt) or (Sender = cmbColorPic) or (Sender = cmbColorBC)
        then UpdateObject_Color(Sender as TComboBox)
      else if (Sender = cmbRotationPic) or (Sender = cmbRotationBC)
        then UpdateObject_Rotation(Sender as TComboBox)
      else if (Sender = chbAutoSizePic) or (Sender = chbAutoSizeBC)
        then UpdateObject_AutoSize(Sender as TCheckBox)
      else if Sender = cmbCharRotation then UpdateObject_CharRotation
      else if (Sender = cmbAlignmentTxt) or (Sender = cmbAlignmentPic) or (Sender = cmbAlignmentBC)
        then UpdateObject_Alignment(Sender as TComboBox)
      else if Sender = cmbCodePage then UpdateObject_CodePage
      else if Sender = cmbCharSet then UpdateObject_CharSet
      else if Sender = chbRaiseException then UpdateObject_RaiseException
      else if Sender = chbDeviceEnabled then UpdateObject_DeviceEnabled
      else if Sender = chbLockDevices then UpdateObject_LockDevices
      else if Sender = cmbCutValue then UpdateObject_CutValue
      else if Sender = edtFeedValue then UpdateObject_FeedValue
      else if Sender = edtBeepValue then UpdateObject_BeepValue
      else if Sender = cmbDrawerValue then UpdateObject_DrawerValue
      else if Sender = cmbSlipValue then UpdateObject_SlipValue
      else if Sender = edtBarCodeHeight then UpdateObject_BarCodeHeight
      else if Sender = chbBarCodePrintText then UpdateObject_BarCodePrintText
      else if Sender = chbBarCodeControlCode then UpdateObject_BarCodeControlCode
      else if Sender = cmbBarCodeType then UpdateObject_BarCodeType;
    finally
      if Sender is TCustomEdit then TCustomEdit(Sender).Modified := False;
      UpdateControls;
    end;
  except
    on E: Exception do
    begin
      Result := False;
      if (Sender is TWinControl)
         and (TWinControl(Sender).Visible)
         and (TWinControl(Sender).Enabled) then TWinControl(Sender).SetFocus;
      if not DriverException(E) then raise;
    end;
  end;
end;

procedure TfmMain.PerformMethodValue(Sender: TObject);

   function SavePropertyValues: Boolean;
   var
     I: Integer;
     cmp: TComponent;
   begin
     for I := 0 to ComponentCount - 1 do
     begin
       cmp := Components[I];
       if (cmp is TCustomEdit)
          and IsEditorControl(TCustomEdit(cmp))
          and TCustomEdit(cmp).Modified
           then
           begin
             Result := ChangePropertyValue(cmp);
             Exit;
           end;
     end;
     Result := True;
   end;

begin
  if not SavePropertyValues then Exit;
  try
    try
      if Sender = btnAddText then Perform_AddText
      else if Sender = btnClearTask then Perform_ClearTask
      else if (Sender = btnShowSpooler) or (Sender = btnShowSpooler2) then Perform_ShowSpooler
      else if Sender = btnShowProperties then Perform_ShowProperties
      else if Sender = btnAddPicture then Perform_AddPicture
      else if Sender = btnAddBeep then Perform_AddBeep
      else if Sender = btnAddFeed then Perform_AddFeed
      else if Sender = btnAddCut then Perform_AddCut
      else if Sender = btnAddDrawer then Perform_AddDrawer
      else if Sender = btnAddSlip then Perform_AddSlip
      else if Sender = btnAddBarCode then Perform_AddBarCode
      else if (Sender = btnPrint) or (Sender = btnPrint2) then Perform_ExecutePrintDialog
      else if (Sender = btnInformation) or (Sender = btnInformation2) then Perform_ExecuteInformationDialog
      else if (Sender = btnDemo) or (Sender = btnDemo2) then Perform_ExecuteDemoDialog
      else if (Sender = btnTestConnection) or (Sender = btnTestConnection2) then Perform_ExecuteStatusDialog
      else if Sender = btnDevices then Perform_ExecuteDevices
      else if Sender = btnResetPrintSettings then Perform_ResetPrintSettings
      else if Sender = btnLoadTask then Perform_LoadTaskFromFile
      else if Sender = btnSaveTask then Perform_SaveTaskToFile;
    finally
      UpdateControls;
    end;
   except
    on E: Exception do
    begin
      if (Sender is TWinControl)
         and TWinControl(Sender).Visible
         and (TWinControl(Sender).Enabled) then TWinControl(Sender).SetFocus;
      if not DriverException(E) then raise;
    end;
  end;
end;

procedure TfmMain.UpdateControls;
var
  V: OleVariant;
begin
  V := Driver.ControlInterface;
  SafeSetEdit(edtResultCode, IntToStr(V.ResultCode));
  SafeSetEdit(edtResultDescription, V.ResultDescription);
  SafeSetEdit(edtCaptionTxt, V.Caption);
  SafeSetEdit(edtCaptionBC, V.Caption);
  SafeSetChecked(chbDeviceEnabled, V.DeviceEnabled);
  SafeSetChecked(chbRaiseException, V.RaiseException);
  SafeSetChecked(chbLockDevices, V.LockDevices);
  SafeSetChecked(chbTextWrap, V.TextWrap);
  SafeSetChecked(chbTextNewLine, V.TextNewLine);
  SafeSetEdit(edtFontIndex, IntToStr(V.FontIndex));
  SafeSetEdit(edtLineSpacing, IntToStr(V.LineSpacing));
  SafeSetChecked(chbFontDblWidth, V.FontDblWidth);
  SafeSetChecked(chbFontDblHeight, V.FontDblHeight);
  SafeSetChecked(chbFontUnderLine, V.FontUnderLine);
  SafeSetChecked(chbFontOverLine, V.FontOverLine);
  SafeSetChecked(chbFontNegative, V.FontNegative);
  SafeSetChecked(chbTextUpSideDown, V.TextUpSideDown);
  SafeSetChecked(chbFontBold, V.FontBold);
  SafeSetChecked(chbFontItalic, V.FontItalic);
  SafeSetChecked(chbZeroSlashed, V.ZeroSlashed);
  SafeSetComboBoxObj(cmbColorTxt, TObject(Integer(V.Color)));
  SafeSetComboBoxObj(cmbColorPic, TObject(Integer(V.Color)));
  SafeSetComboBoxObj(cmbColorBC, TObject(Integer(V.Color)));
  SafeSetComboBoxObj(cmbRotationPic, TObject(Integer(V.Rotation)));
  SafeSetComboBoxObj(cmbRotationBC, TObject(Integer(V.Rotation)));
  SafeSetChecked(chbAutoSizePIC, V.AutoSize);
  SafeSetChecked(chbAutoSizeBC, V.AutoSize);
  SafeSetComboBoxObj(cmbCharRotation, TObject(Integer(V.CharRotation)));
  SafeSetComboBoxObj(cmbAlignmentTxt, TObject(Integer(V.Alignment)));
  SafeSetComboBoxObj(cmbAlignmentPic, TObject(Integer(V.Alignment)));
  SafeSetComboBoxObj(cmbAlignmentBC, TObject(Integer(V.Alignment)));
  SafeSetComboBoxObj(cmbCutValue, TObject(Integer(V.CutValue)));
  SafeSetComboBoxObj(cmbSlipValue, TObject(Integer(V.SlipValue)));
  SafeSetComboBoxObj(cmbDrawerValue, TObject(Integer(V.DrawerValue)));
  SafeSetEdit(edtFeedValue, IntToStr(V.FeedValue));
  SafeSetEdit(edtBeepValue, IntToStr(V.BeepValue));
  SafeSetEdit(edtBarCodeHeight, IntToStr(V.BarCodeHeight));
  SafeSetChecked(chbBarCodePrintText, V.BarCodePrintText);
  SafeSetChecked(chbBarCodeControlCode, V.BarCodeControlCode);
  SafeSetComboBoxObj(cmbBarCodeType, TObject(Integer(V.BarCodeType)));
  chbBarCodeControlCode.Enabled := (V.BarCodeType = 0); //  EAN/UPC
  SafeSetComboBoxObj(cmbCodePage, TObject(Integer(V.CodePage)));
  SafeSetComboBoxObj(cmbCharSet, TObject(Integer(V.CharSet)));
  //
  if V.DeviceEnabled then
  begin
    lblIsDemo.Visible := True;
    lblIsDemoDscr.Visible := True;
    lblDemoDescription.Visible := V.IsDemo;
    if V.IsDemo then lblIsDemo.Caption := '' else lblIsDemo.Caption := '';
  end
  else
  begin
    lblIsDemoDscr.Visible := False;
    lblIsDemo.Visible := False;
    lblDemoDescription.Visible := False;
  end;
  //
  //chbShowMessage.Enabled := chbRaiseException.Checked;
  //
end;

function TfmMain.IsEditorControl(Value: TControl): Boolean;
begin
  Result := (Value <> nil) and (Value.Tag = PROPERTY_EDIT_TAG);
end;

(*************************************    ******************************************)

procedure TfmMain.UpdateObject_DeviceEnabled;
begin
  Driver.ControlInterface.DeviceEnabled := chbDeviceEnabled.Checked;
end;

procedure TfmMain.UpdateObject_LockDevices;
begin
  Driver.ControlInterface.LockDevices := chbLockDevices.Checked;
end;

procedure TfmMain.UpdateObject_RaiseException;
begin
  Driver.ControlInterface.RaiseException := chbRaiseException.Checked;
  if not Driver.ControlInterface.RaiseException then
    with Application do
      MessageBox('!    " "     .',
        Pchar(Title), mb_IconWarning or mb_OK);
end;

procedure TfmMain.UpdateObject_CodePage;
begin
  Driver.ControlInterface.CodePage := Integer(GetSelectedComboBoxObject(cmbCodePage));
end;

procedure TfmMain.UpdateObject_CharSet;
begin
  Driver.ControlInterface.CharSet := Integer(GetSelectedComboBoxObject(cmbCharSet));
end;

procedure TfmMain.UpdateObject_Caption(Value: TCustomEdit);
begin
  Driver.ControlInterface.Caption := Value.Text;
end;

procedure TfmMain.UpdateObject_TextWrap;
begin
  Driver.ControlInterface.TextWrap := chbTextWrap.Checked;
end;

procedure TfmMain.UpdateObject_TextNewLine;
begin
  Driver.ControlInterface.TextNewLine := chbTextNewLine.Checked;
end;

procedure TfmMain.UpdateObject_FontIndex;
begin
  Driver.ControlInterface.FontIndex := strToInt(edtFontIndex.Text);
end;

procedure TfmMain.UpdateObject_LineSpacing;
begin
  Driver.ControlInterface.LineSpacing := strToInt(edtLineSpacing.Text);
end;

procedure TfmMain.UpdateObject_FontDblWidth;
begin
  Driver.ControlInterface.FontDblWidth := chbFontDblWidth.Checked;
end;

procedure TfmMain.UpdateObject_FontDblHeight;
begin
  Driver.ControlInterface.FontDblHeight := chbFontDblHeight.Checked;
end;

procedure TfmMain.UpdateObject_FontUnderLine;
begin
  Driver.ControlInterface.FontUnderLine := chbFontUnderLine.Checked;
end;

procedure TfmMain.UpdateObject_FontOverLine;
begin
  Driver.ControlInterface.FontOverLine := chbFontOverLine.Checked;
end;

procedure TfmMain.UpdateObject_FontNegative;
begin
  Driver.ControlInterface.FontNegative := chbFontNegative.Checked;
end;

procedure TfmMain.UpdateObject_TextUpSideDown;
begin
  Driver.ControlInterface.TextUpSideDown := chbTextUpSideDown.Checked;
end;

procedure TfmMain.UpdateObject_FontBold;
begin
  Driver.ControlInterface.FontBold := chbFontBold.Checked;
end;

procedure TfmMain.UpdateObject_FontItalic;
begin
  Driver.ControlInterface.FontItalic := chbFontItalic.Checked;
end;

procedure TfmMain.UpdateObject_ZeroSlashed;
begin
  Driver.ControlInterface.ZeroSlashed := chbZeroSlashed.Checked;
end;

procedure TfmMain.UpdateObject_Color(Value: TComboBox);
begin
  Driver.ControlInterface.Color := Integer(GetSelectedComboBoxObject(Value));
end;

procedure TfmMain.UpdateObject_Rotation(Value: TComboBox);
begin
  Driver.ControlInterface.Rotation := Integer(GetSelectedComboBoxObject(Value));
end;

procedure TfmMain.UpdateObject_AutoSize(Value: TCheckBox);
begin
  Driver.ControlInterface.AutoSize := Value.Checked;
end;

procedure TfmMain.UpdateObject_CharRotation;
begin
  Driver.ControlInterface.CharRotation := Integer(GetSelectedComboBoxObject(cmbCharRotation));
end;

procedure TfmMain.UpdateObject_Alignment(Value: TComboBox);
begin
  Driver.ControlInterface.Alignment := Integer(GetSelectedComboBoxObject(Value));
end;

procedure TfmMain.UpdateObject_CutValue;
begin
  Driver.ControlInterface.CutValue := Integer(GetSelectedComboBoxObject(cmbCutValue));
end;

procedure TfmMain.UpdateObject_DrawerValue;
begin
  Driver.ControlInterface.DrawerValue := Integer(GetSelectedComboBoxObject(cmbDrawerValue));
end;

procedure TfmMain.UpdateObject_SlipValue;
begin
  Driver.ControlInterface.SlipValue := Integer(GetSelectedComboBoxObject(cmbSlipValue));
end;

procedure TfmMain.UpdateObject_FeedValue;
begin
  Driver.ControlInterface.FeedValue := strToInt(edtFeedValue.Text);
end;

procedure TfmMain.UpdateObject_BeepValue;
begin
  Driver.ControlInterface.BeepValue := strToInt(edtBeepValue.Text);
end;

procedure TfmMain.UpdateObject_BarCodeHeight;
begin
  Driver.ControlInterface.BarCodeHeight := strToInt(edtBarCodeHeight.Text);
end;

procedure TfmMain.UpdateObject_BarCodePrintText;
begin
  Driver.ControlInterface.BarCodePrintText := chbBarCodePrintText.Checked;
end;

procedure TfmMain.UpdateObject_BarCodeControlCode;
begin
  Driver.ControlInterface.BarCodeControlCode := chbBarCodeControlCode.Checked;
end;

procedure TfmMain.UpdateObject_BarCodeType;
begin
  Driver.ControlInterface.BarCodeType := Integer(GetSelectedComboBoxObject(cmbBarCodeType));
end;

//

procedure TfmMain.Perform_AddText;
begin
  Driver.ControlInterface.AddText;
end;

procedure TfmMain.Perform_AddPicture;
var
  bmp: TBitmap;
begin
  if OpenPictureDialog.Execute then
  begin
    bmp := TBitmap.Create;
    try
      bmp.LoadFromFile(OpenPictureDialog.FileName);
      if bmp.Monochrome then
      begin
        Image.Picture.Assign(bmp);
        Driver.ControlInterface.FileName := OpenPictureDialog.FileName;
        Driver.ControlInterface.AddPicture;
      end
      else
        raise Exception.Create('    ');
    finally
      bmp.Free;
    end;
  end;
end;

procedure TfmMain.Perform_AddBeep;
begin
  Driver.ControlInterface.AddBeep;
end;

procedure TfmMain.Perform_AddCut;
begin
  Driver.ControlInterface.AddCut;
end;

procedure TfmMain.Perform_AddFeed;
begin
  Driver.ControlInterface.AddFeed;
end;

procedure TfmMain.Perform_AddDrawer;
begin
  Driver.ControlInterface.AddDrawer;
end;

procedure TfmMain.Perform_AddSlip;
begin
  Driver.ControlInterface.AddSlip;
end;

procedure TfmMain.Perform_AddBarCode;
begin
  Driver.ControlInterface.AddBarCode;
end;

procedure TfmMain.Perform_ResetPrintSettings;
begin
  Driver.ControlInterface.ResetPrintSettings;
end;

procedure TfmMain.Perform_LoadTaskFromFile;
var
  V: OleVariant;
begin
  if OpenTaskDialog.Execute then
  begin
    V := Driver.ControlInterface;
    V.FileName := OpenTaskDialog.FileName;
    V.LoadTaskFromFile;
    Image.Picture.Assign(nil);
    Image.Height := 0;
    Image.Width := 0;
  end;
end;

procedure TfmMain.Perform_SaveTaskToFile;
var
  V: OleVariant;
begin
  if SaveTaskDialog.Execute then
  begin
    V := Driver.ControlInterface;
    V.FileName := SaveTaskDialog.FileName;
    V.SaveTaskToFile;
  end;
end;

procedure TfmMain.Perform_ClearTask;
begin
  Driver.ControlInterface.ClearTask;
  //  
  Image.Picture.Assign(nil);
  Image.Height := 0;
  Image.Width := 0;
end;

procedure TfmMain.Perform_ShowSpooler;
begin
  Driver.ControlInterface.ShowSpooler;
end;

procedure TfmMain.Perform_ShowProperties;
begin
  Driver.ControlInterface.ShowProperties;
end;

//

procedure TfmMain.Perform_ExecutePrintDialog;
begin
  ExecutePrintDialog(Driver, chbShowMessage.Checked, Font);
end;

procedure TfmMain.Perform_ExecuteInformationDialog;
begin
  ExecuteInformationDialog(Driver, chbShowMessage.Checked, Font);
end;

procedure TfmMain.Perform_ExecuteDemoDialog;
begin
  ExecuteDemoDialog(Driver, chbShowMessage.Checked, Font, fShowDemoOnStart);
end;

procedure TfmMain.Perform_ExecuteStatusDialog;
begin
  ExecuteStatusDialog(Driver, chbShowMessage.Checked, Font);
end;

procedure TfmMain.Perform_ExecuteDevices;
begin
  ExecuteDeviceDialog(Driver, chbShowMessage.Checked, Font);
end;

(************************************   *****************************************)

procedure TfmMain.btnFontClick(Sender: TObject);
begin
  FontDialog.Font.Assign(Font);
  if FontDialog.Execute then
  begin
    Font.Assign(FontDialog.Font);
    SetRegistryFont(Font);
  end;
end;

procedure TfmMain.btnAboutClick(Sender: TObject);
begin
  { No source in the Sample }
end;

procedure TfmMain.EditorClick(Sender: TObject);
begin
  PerformMethodValue(Sender);
end;

procedure TfmMain.EditorChange(Sender: TObject);
begin
  ChangePropertyValue(Sender);
end;

procedure TfmMain.EditorExit(Sender: TObject);
begin
  if (Sender is TCustomEdit) and TCustomEdit(Sender).Modified
    then ChangePropertyValue(Sender);
end;

procedure TfmMain.EditorKeyPress(Sender: TObject; var Key: Char);
begin
  case ord(Key) of
    VK_RETURN: ChangePropertyValue(Sender);
    VK_ESCAPE: UpdateControls;
  end;
end;

procedure TfmMain.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfmMain.chlTextStyleClickCheck(Sender: TObject);
begin
  ChangePropertyValue(Sender);
end;

procedure TfmMain.udEditorClick(Sender: TObject; Button: TUDBtnType);
begin
  ChangePropertyValue(Sender);
end;

procedure TfmMain.ShowDemo(var Message: TMessage); 
begin
  if fShowDemoOnStart then PerformMethodValue(btnDemo2);
end;

end.
