unit fmuDemo;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,
  //
  cuUtils,
  //
  LineX,
  untTypes, ExtCtrls;

type
  TfmDemo = class(TForm)
    btnHorzMerquee: TButton;
    btnVertMarquee: TButton;
    btnMarqueeOld: TButton;
    chkDeviceEnabled: TCheckBox;
    btnProperties: TButton;
    btnClose: TButton;
    btnDeviceBlink: TButton;
    btnBlinkEmulation: TButton;
    Bevel2: TBevel;
    btnTeletype: TButton;
    btnSelfTest: TButton;
    chkShowOnStart: TCheckBox;
    btnViewPorts: TButton;
    procedure btnMarqueeOldClick(Sender: TObject);
    procedure ControlChanged(Sender: TObject);
    procedure btnHorzMerqueeClick(Sender: TObject);
    procedure btnVertMarqueeClick(Sender: TObject);
    procedure btnDeviceBlinkClick(Sender: TObject);
    procedure btnBlinkEmulationClick(Sender: TObject);
    procedure btnTeletypeClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure btnSelfTestClick(Sender: TObject);
    procedure btnViewPortsClick(Sender: TObject);
  private
    FLine: TLineX;
    FWindows: TList;
    FTeletype: Boolean;
    function CancelTeletype: Boolean;
    procedure AddWindow(AIndex: Integer);
    procedure DoneWindows;
    procedure Init(ALine: TLineX; AFont: TFont;
      AShowOnStart: Boolean);
    procedure UpdatePage;
    procedure EnableControls(Enable: Boolean);
  public
    destructor Destroy; override;  
  end;

procedure ExecuteDemo(ALine: TLineX; AFont: TFont;
  var AShowOnStart: Boolean);

implementation

{$R *.DFM}

const
  BoolToTeletypeStr: array[Boolean] of String = ('', '');

function Max(Value1, Value2: Integer): Integer;
begin
  if Value1 > Value2 then Result := Value1 else Result := Value2
end;

function Min(Value1, Value2: Integer): Integer;
begin
  if Value1 < Value2 then Result := Value1 else Result := Value2
end;

procedure ExecuteDemo(ALine: TLineX; AFont: TFont;
  var AShowOnStart: Boolean);
var
  fm: TfmDemo;
begin
  fm := TfmDemo.Create(nil);
  try
    fm.Init(ALine, AFont, AShowOnStart);
    fm.ShowModal;
    AShowOnStart := fm.chkShowOnStart.Checked;
  finally
    fm.Free;
  end;
end;

procedure TfmDemo.Init(ALine: TLineX; AFont: TFont;
  AShowOnStart: Boolean);
begin
  FLine := ALine;
  Font := AFont;
  chkShowOnStart.Checked := AShowOnStart;
  UpdatePage;
end;

procedure TfmDemo.btnMarqueeOldClick(Sender: TObject);
const
  S_ATOL_TEL = '234-40-64';
var
  wIndex: Integer;
  wWidth: Integer;
begin
  DoneWindows;
  {       
         }
  wWidth := Length(S_ATOL_TEL) + FLine.DeviceColumns;
  FLine.CreateWindow(0, 0, 1, wWidth, 1, wWidth);
  wIndex := FLine.CurrentWindow;
  AddWindow(wIndex);
  FLine.CurrentWindow := wIndex;
  FLine.MarqueeType := DISP_MT_INIT;
  FLine.ClearText;
  FLine.MarqueeRepeatWait := 250;
  FLine.MarqueeUnitWait := 150;
  FLine.CursorRow := 0;
  FLine.CursorColumn := 0;
  FLine.CursorUpdate := False;
  FLine.DisplayText(S_ATOL_TEL, DISP_DT_NORMAL);
  FLine.MarqueeType := DISP_MT_LEFT;
end;

procedure TfmDemo.ControlChanged(Sender: TObject);
begin
  DoneWindows;
  try
    if Sender = chkDeviceEnabled then
      FLine.DeviceEnabled := chkDeviceEnabled.Checked
    else
    if Sender = btnProperties then
      FLine.ShowProperties;
  finally
    UpdatePage;
  end;
end;

procedure TfmDemo.btnHorzMerqueeClick(Sender: TObject);
const
  S_ATOL_TEL1 = '234-40-64 232-96-88';
  S_ATOL_TEL2 = '232-96-88';
  S_ATOL_NAME = ' ';
  S_ATOL_UPPER_NAME = ' ';
var
  wIndex: Integer;
  wWidth: Integer;
  sLine1, sLine2: String;
begin
  //  ,    
  DoneWindows;
  case FLine.CapCharacterSet of
  DISP_CCS_NUMERIC:
    begin
      //   
      sLine1 := S_ATOL_TEL1;
      sLine2 := S_ATOL_TEL2;
    end;
  DISP_CCS_ALPHA:
    begin
      //    
      sLine1 := S_ATOL_UPPER_NAME;
      sLine2 := S_ATOL_TEL1;
    end;
  DISP_CCS_ASCII:
    begin
      //   
      sLine1 := S_ATOL_NAME;
      sLine2 := S_ATOL_TEL1;
    end;
  else
    sLine1 := S_ATOL_NAME;
    sLine2 := S_ATOL_TEL1;
  end;
  //   
  wWidth := Max(FLine.GetDeviceColumns(0), Length(sLine1));
  FLine.CreateWindow(0, 0, 1, wWidth, 1, wWidth);
  wIndex := FLine.CurrentWindow;
  AddWindow(wIndex);
  //      ,   
  FLine.MarqueeType := DISP_MT_RIGHT;
  FLine.MarqueeFormat := DISP_MF_PLACE;
  FLine.MarqueeRepeatWait := 250;
  FLine.MarqueeUnitWait := 250;
  FLine.CursorRow := 0;
  FLine.CursorColumn := 0;
  FLine.CursorUpdate := False;
  FLine.DisplayText(sLine1, DISP_DT_NORMAL);
  if FLine.DeviceRows > 1 then
  begin
    //      
    //   
    wWidth := Max(FLine.GetDeviceColumns(1), Length(sLine2));
    FLine.CreateWindow(1, 0, 1, wWidth, 1, wWidth);
    wIndex := FLine.CurrentWindow;
    AddWindow(wIndex);
    //   ,   
    FLine.MarqueeType := DISP_MT_LEFT;
    FLine.MarqueeFormat := DISP_MF_WALK;
    FLine.MarqueeRepeatWait := 250;
    FLine.MarqueeUnitWait := 250;
    FLine.CursorRow := 0;
    FLine.CursorColumn := 0;
    FLine.CursorUpdate := False;
    FLine.DisplayText(sLine2, DISP_DT_NORMAL);
  end;
end;

procedure TfmDemo.btnVertMarqueeClick(Sender: TObject);
const
  S_ATOL1 = 'ATOL'#13#10'234-40-64'#13#10'232-96-88';
  HEIGHT1 = 3;
  S_ATOL2 = ''#13#10+
   'www.atol.ru'#13#10+
   'www.barcode.ru'#13#10+
   '/:'#13#10+
   '(095) 234-4064'#13#10+
   '(095) 232-9688'#13#10+
   '(095) 285-0440'#13#10+
   '. '#13#10+
   '(095)285-9341';
  HEIGHT2 = 12;
var
  wIndex: Integer;
  wWidth: Integer;
  wHeight: Integer;
  S: String;
begin
  //  ,    
  DoneWindows;
  case FLine.CapCharacterSet of
  DISP_CCS_NUMERIC:
    begin
      //   
      S := S_ATOL1;
      wHeight := HEIGHT1;
    end;
  DISP_CCS_ALPHA:
    begin
      //    
      S := AnsiUpperCase(S_ATOL2);
      wHeight := HEIGHT2;
    end;
  DISP_CCS_ASCII:
    begin
      //   
      S := S_ATOL2;
      wHeight := HEIGHT2;
    end;
  else
    S := S_ATOL2;
    wHeight := HEIGHT2;
  end;
  //  
  wWidth := FLine.GetDeviceColumns(0);
  FLine.CreateWindow(0, 0, FLine.DeviceRows, wWidth, wHeight, wWidth);
  wIndex := FLine.CurrentWindow;
  AddWindow(wIndex);
  //      ,   
  FLine.MarqueeType := DISP_MT_UP;
  FLine.MarqueeFormat := DISP_MF_WALK;
  if FLine.DeviceRows > 1 then
  begin
    FLine.MarqueeRepeatWait := 500;
    FLine.MarqueeUnitWait := 500;
  end
  else
  begin
    FLine.MarqueeRepeatWait := 750;
    FLine.MarqueeUnitWait := 750;
  end;
  FLine.CursorRow := 0;
  FLine.CursorColumn := 0;
  FLine.CursorUpdate := False;
  FLine.DisplayText(S, DISP_DT_NORMAL);
end;

procedure TfmDemo.btnDeviceBlinkClick(Sender: TObject);
const
  S_ATOL_TEL1 = '234-40-64';
  S_ATOL_TEL2 = '232-96-88';
  S_ATOL_NAME = ' ';
  S_ATOL_UPPER_NAME = ' ';
var
  wIndex: Integer;
  wWidth: Integer;
  sLine1, sLine2: String;
begin
  //  ,    
  DoneWindows;
  case FLine.CapCharacterSet of
  DISP_CCS_NUMERIC:
    begin
      //   
      sLine1 := S_ATOL_TEL1;
      sLine2 := S_ATOL_TEL2;
    end;
  DISP_CCS_ALPHA:
    begin
      //    
      sLine1 := S_ATOL_UPPER_NAME;
      sLine2 := S_ATOL_TEL1;
    end;
  DISP_CCS_ASCII:
    begin
      //   
      sLine1 := S_ATOL_NAME;
      sLine2 := S_ATOL_TEL1;
    end;
  else
    sLine1 := S_ATOL_NAME;
    sLine2 := S_ATOL_TEL1;
  end;
  //   
  wWidth := Max(FLine.GetDeviceColumns(0), Length(sLine1));
  FLine.CreateWindow(0, 0, 1, wWidth, 1, wWidth);
  wIndex := FLine.CurrentWindow;
  AddWindow(wIndex);
  FLine.BlinkInterval := 500;
  //  
  FLine.MarqueeType := DISP_MT_NONE;
  FLine.CursorRow := 0;
  FLine.CursorColumn := 0;
  FLine.CursorUpdate := False;
  FLine.DisplayText(sLine1, DISP_DT_HBLINK);
  if FLine.DeviceRows > 1 then
  begin
    //      
    //   
    wWidth := Max(FLine.GetDeviceColumns(1), Length(sLine2));
    FLine.CreateWindow(1, 0, 1, wWidth, 1, wWidth);
    wIndex := FLine.CurrentWindow;
    AddWindow(wIndex);
    //   (      )
    FLine.BlinkInterval := 1000;
    FLine.MarqueeType := DISP_MT_NONE;
    FLine.CursorRow := 0;
    FLine.CursorColumn := 0;
    FLine.CursorUpdate := False;
    FLine.DisplayText(sLine2, DISP_DT_HBLINK);
  end;
end;

procedure TfmDemo.btnBlinkEmulationClick(Sender: TObject);
const
  S_ATOL_TEL1 = '234-40-64';
  S_ATOL_TEL2 = '232-96-88';
  S_ATOL_NAME = ' ';
  S_ATOL_UPPER_NAME = ' ';
  S_ATOL_BK1  = '--------';
  S_ATOL_BK2  = '---------------';
var
  wIndex: Integer;
  wWidth: Integer;
  sLine1, sLine2: String;
  sBk1, sBk2: String;
begin
  //  ,    
  DoneWindows;
  case FLine.CapCharacterSet of
  DISP_CCS_NUMERIC:
    begin
      //   
      sLine1 := S_ATOL_TEL1;
      sLine2 := S_ATOL_TEL2;
      sBk1 := S_ATOL_BK1;
      sBk2 := S_ATOL_BK1;
    end;
  DISP_CCS_ALPHA:
    begin
      //    
      sLine1 := S_ATOL_UPPER_NAME;
      sLine2 := S_ATOL_TEL1;
      sBk1 := S_ATOL_BK2;
      sBk2 := S_ATOL_BK1;
    end;
  DISP_CCS_ASCII:
    begin
      //   
      sLine1 := S_ATOL_NAME;
      sLine2 := S_ATOL_TEL1;
      sBk1 := S_ATOL_BK2;
      sBk2 := S_ATOL_BK1;
    end;
  else
    sLine1 := S_ATOL_NAME;
    sLine2 := S_ATOL_TEL1;
    sBk1 := S_ATOL_BK2;
    sBk2 := S_ATOL_BK1;
  end;
  //    
  wWidth := Max(FLine.GetDeviceColumns(0), Length(sLine1));
  FLine.CreateWindow(0, 0, 1, wWidth, 1, wWidth);
  wIndex := FLine.CurrentWindow;
  AddWindow(wIndex);
  FLine.MarqueeType := DISP_MT_NONE;
  FLine.CursorRow := 0;
  FLine.CursorColumn := 0;
  FLine.CursorUpdate := False;
  FLine.DisplayText(sBk1, DISP_DT_NORMAL);
  //    
  wWidth := Max(FLine.GetDeviceColumns(0), Length(sLine1));
  FLine.CreateWindow(0, 0, 1, wWidth, 1, wWidth);
  wIndex := FLine.CurrentWindow;
  AddWindow(wIndex);
  FLine.BlinkInterval := 500;
  //  ,  ,    
  FLine.MarqueeType := DISP_MT_NONE;
  FLine.CursorRow := 0;
  FLine.CursorColumn := 0;
  FLine.CursorUpdate := False;
  FLine.DisplayText(sLine1, DISP_DT_SWINDOW);
  if FLine.DeviceRows > 1 then
  begin
    //      
    //    
    wWidth := Max(FLine.GetDeviceColumns(1), Length(sLine2));
    FLine.CreateWindow(1, 0, 1, wWidth, 1, wWidth);
    wIndex := FLine.CurrentWindow;
    AddWindow(wIndex);
    FLine.MarqueeType := DISP_MT_NONE;
    FLine.CursorRow := 0;
    FLine.CursorColumn := 0;
    FLine.CursorUpdate := False;
    FLine.DisplayText(sBk2, DISP_DT_NORMAL);
    //   
    wWidth := Max(FLine.GetDeviceColumns(1), Length(sLine2));
    FLine.CreateWindow(1, 0, 1, wWidth, 1, wWidth);
    wIndex := FLine.CurrentWindow;
    AddWindow(wIndex);
    //  
    FLine.BlinkInterval := 1000;
    FLine.MarqueeType := DISP_MT_NONE;
    FLine.CursorRow := 0;
    FLine.CursorColumn := 0;
    FLine.CursorUpdate := False;
    FLine.DisplayText(sLine2, DISP_DT_SWINDOW);
  end;
end;

function TfmDemo.CancelTeletype: Boolean;
begin
  if FTeletype then
  begin
    FTeletype := False;
    Result := True;
    btnTeletype.Caption := BoolToTeletypeStr[FTeletype];
  end
  else
    Result := False;
end;

procedure TfmDemo.EnableControls(Enable: Boolean);
begin
  btnHorzMerquee.Enabled := Enable;
  btnVertMarquee.Enabled := Enable;
  btnMarqueeOld.Enabled := Enable;
  chkDeviceEnabled.Enabled := Enable;
  btnProperties.Enabled := Enable;
  btnDeviceBlink.Enabled := Enable;
  btnBlinkEmulation.Enabled := Enable;
  btnSelfTest.Enabled := Enable;
  btnViewPorts.Enabled := Enable;
  chkShowOnStart.Enabled := Enable;
end;

procedure TfmDemo.btnTeletypeClick(Sender: TObject);
const
  S_ATOL1 =
   'ATOL .....'+
   '234-40-64 '+
   '232-96-88 ';
  S_ATOL2 =
   '  ....'+
   'www.atol.ru ........'+
   'www.barcode.ru .....'+
   '/: .....'+
   '(095) 234-4064 .....'+
   '(095) 232-9688 .....'+
   '(095) 285-0440 .....'+
   '.  .....'+
   '(095)285-9341 ......';

var
  I: Integer;
  wWidth: Integer;
  wHeight: Integer;
  wIndex: Integer;
  S: String;
begin
  if CancelTeletype then Exit;
  DoneWindows;
  case FLine.CapCharacterSet of
  DISP_CCS_NUMERIC:
    begin
      //   
      S := S_ATOL1;
      wHeight := 1;
      wWidth := 10;
    end;
  DISP_CCS_ALPHA:
    begin
      //    
      S := AnsiUpperCase(S_ATOL2);
      wHeight := 1;
      wWidth := 10;
    end;
  DISP_CCS_ASCII:
    begin
      //   
      S := S_ATOL2;
      wHeight := 2;
      wWidth := 20;
    end;
  else
    S := S_ATOL2;
    wHeight := 2;
    wWidth := 20;
  end;
  FLine.CreateWindow(0, 0, wHeight, wWidth, wHeight, wWidth);
  wIndex := FLine.CurrentWindow;
  AddWindow(wIndex);
  FLine.CursorRow := 0;
  FLine.CursorColumn := 0;
  FLine.CursorUpdate := True;
  try
    EnableControls(False);
    FTeletype := True;
    btnTeletype.Caption := BoolToTeletypeStr[FTeletype];
    I := 1;
    FLine.MarqueeType := DISP_MT_NONE;
    try
      while FTeletype do
      begin
        if I > Length(S) then I := 1;
        FLine.DisplayText(S[I], DISP_DT_NORMAL);
        Application.ProcessMessages;
        Sleep(100); // 
        Inc(I);
      end;
    except
      FTeletype := False;
      btnTeletype.Caption := BoolToTeletypeStr[FTeletype];
      raise;
    end;
  finally
    EnableControls(True);
  end;
end;

destructor TfmDemo.Destroy;
begin
  DoneWindows;
  inherited Destroy;
end;

procedure TfmDemo.UpdatePage;
begin
  SafeSetChecked(chkDeviceEnabled, FLine.DeviceEnabled);
end;

procedure TfmDemo.AddWindow(AIndex: Integer);
begin
  if FWindows = nil then FWindows := TList.Create;
  FWindows.Add(Pointer(AIndex));
end;

procedure TfmDemo.DoneWindows;
var
  I: Integer;
begin
  if FWindows <> nil then
  begin
    if FLine.ServerLoaded then
      for I := FWindows.Count - 1 downto 0 do
      begin
        FLine.CurrentWindow := Integer(FWindows[I]);
        FLine.DestroyWindow;
      end;
    FWindows.Free;
    FWindows := nil;
  end;
end;

procedure TfmDemo.btnCloseClick(Sender: TObject);
begin
  CancelTeletype;
end;

procedure TfmDemo.btnSelfTestClick(Sender: TObject);
const
  S_QUERY = '      .'+
  '         '+
  '      .'#13#10+
  ' ?';
var
  Model: TLineModel;
begin
  CancelTeletype;
  DoneWindows;
  Model := IntToLineModel(FLine.Model);
  if (Model in [lnDPD201, lnVF2029]) and FLine.DeviceEnabled then
    with Application do
    if MessageBox(S_QUERY, PChar(Title),
       mb_IconWarning or mb_YesNo) = IdNo then Exit;
  FLine.SelfTest;
end;

procedure TfmDemo.btnViewPortsClick(Sender: TObject);
const
  // S_ATOL_TEL1 = '234-40-64 232-96-88';
  // S_ATOL_TEL2 = '232-96-88';
  S_ATOL_TEL1 = '0123456789';
  S_ATOL_TEL2 = '0123456789';
  S_ATOL_NAME = ' ';
  S_ATOL_UPPER_NAME = ' ';
var
  wIndex: Integer;
  wWidth: Integer;
  sLine1, sLine2: String;
  bAddView: Boolean;
  I: Integer;
  vWidth: Integer;
begin
  //  ,    
  DoneWindows;
  case FLine.CapCharacterSet of
  DISP_CCS_NUMERIC:
    begin
      //   
      sLine1 := S_ATOL_TEL1;
      sLine2 := S_ATOL_TEL2;
    end;
  DISP_CCS_ALPHA:
    begin
      //    
      sLine1 := S_ATOL_UPPER_NAME;
      sLine2 := S_ATOL_TEL1;
    end;
  DISP_CCS_ASCII:
    begin
      //   
      sLine1 := S_ATOL_NAME;
      sLine2 := S_ATOL_TEL1;
    end;
  else
    sLine1 := S_ATOL_NAME;
    sLine2 := S_ATOL_TEL1;
  end;
  //      
  wWidth := Max(FLine.GetDeviceColumns(0), Length(sLine1));
  FLine.CreateWindow(0, 0, 0, 0, 1, wWidth);
  //     
  wWidth := FLine.GetDeviceColumns(0);
  vWidth := wWidth div 3;
  bAddView := True;
  I := 0;
  while I < wWidth do
  begin
    if bAddView then
      FLine.AddViewPort(0, I, 1, vWidth);
    Inc(I, vWidth);  
    bAddView := not bAddView;
  end;
  wIndex := FLine.CurrentWindow;
  AddWindow(wIndex);
  //      ,   
  FLine.MarqueeType := DISP_MT_RIGHT;
  FLine.MarqueeFormat := DISP_MF_PLACE;
  FLine.MarqueeRepeatWait := 500;
  FLine.MarqueeUnitWait := 500;
  FLine.CursorRow := 0;
  FLine.CursorColumn := 0;
  FLine.CursorUpdate := False;
  FLine.DisplayText(sLine1, DISP_DT_NORMAL);
  if FLine.DeviceRows > 1 then
  begin
    //      
    //      
    wWidth := Max(FLine.GetDeviceColumns(1), Length(sLine2));
    FLine.CreateWindow(0, 0, 0, 0, 1, wWidth);
    wIndex := FLine.CurrentWindow;
    //     
    wWidth := FLine.GetDeviceColumns(1);
    vWidth := wWidth div 3;
    bAddView := False;
    I := 0;
    while I < wWidth do
    begin
      if bAddView then
        FLine.AddViewPort(1, I, 1, vWidth);
      bAddView := not bAddView;
      Inc(I, vWidth);
    end;
    AddWindow(wIndex);
    //   ,   
    FLine.MarqueeType := DISP_MT_LEFT;
    FLine.MarqueeFormat := DISP_MF_WALK;
    FLine.MarqueeRepeatWait := 500;
    FLine.MarqueeUnitWait := 500;
    FLine.CursorRow := 0;
    FLine.CursorColumn := 0;
    FLine.CursorUpdate := False;
    FLine.DisplayText(sLine2, DISP_DT_NORMAL);
  end; 
end;

end.
