program lc4hs_dw32;
//just to prove that delphi produces the most compact windows applications!
uses
  Messages,
  Windows,
  lc4hs in '..\Delphi\lc4hs.pas';

{$R lc4hs_dw32.res}
{$I lc4hs_dw32.Inc}

const
  IconName: PChar = 'MAINICON';
  AppName: PChar = 'LC4HSAPP';

var
  hDriver: THandle = INVALID_HANDLE_VALUE;
  hWindow: HWND;

function IntToHex(Val: Integer): String;
var
  i, j: Integer; Buf: array[0..8] of Char;
begin
  for i:= 0 to 7 do
  begin
    j:= Val and $f;
    if j < $A then Buf[7-i]:= Char(Ord('0') + j)
    else Buf[7-i]:= Char(Ord('A') - $A + j);
    Val:= Val shr 4
  end;
  Buf[8]:= #0; Result:= Buf
end;

function Checkit(const Call: String; Res: BOOL): Boolean;
var
  Msg: String;
  Err: Integer;
  Buf: array[0..255] of Char;
begin
  if not Res then
  begin
    Err:= GetLastError;
    if not Lc4hsGetErrorMsg(Err, Buf, SizeOf(Buf)) then
      Msg:= 'none'
    else
      Msg:= Buf;
    Msg:= Call + ' failed. code = 0x' + IntToHex(Err) +  ' Msg = ' + Msg;
    MessageBox(hWindow, PChar(Msg), 'Error', MB_OK or MB_ICONEXCLAMATION)
  end;
  Result:= Res
end;

procedure IDInit;
var
  hButton: HWND;
  Buf: array[0..40] of Char;
begin
  hButton:= GetDlgItem(hWindow, id_Init);
  GetWindowText(hButton, Buf, SizeOf(Buf));
  SetWindowText(hButton, 'Opening...');
  UpdateWindow(hButton);
  try
    if CheckIt('Init', Lc4hsInit(hDriver)) then
      MessageBox(hWindow, 'Initialised OK', 'Information', MB_OK or MB_ICONINFORMATION)
  finally
    SetWindowText(hButton, Buf);
  end
end;

procedure IDFinish;
begin
  if CheckIt('Finish', Lc4hsFinish(hDriver)) then
    MessageBox(hWindow, 'Finished OK', 'Information', MB_OK or MB_ICONINFORMATION)
end;

procedure IDRead;
var
  Val: FLOAT4;
  Msg: String;
  StrVal: String[10];
  i: Integer;
begin
  if CheckIt('GetCurrentWeight', Lc4hsGetCurrentWeight4(hDriver, Val)) then
  Msg:= '';
  for i:= 0 to 3 do
  begin
    Str(Val[i]:8:3, StrVal);
    Msg:= Msg + StrVal
  end;
  SetWindowText(GetDlgItem(hWindow, id_FloatLabel), PChar(Msg))
end;

function WindowProc( hWindow: HWND;
                        iMsg: UINT;
                   WordParam: WPARAM;
                   LongParam: LPARAM): LRESULT; stdcall;
begin
  Result:= 0;
  case iMsg of
    WM_COMMAND:
    case Lo(WordParam) of
      id_Init: IDInit;
      id_Read: IDRead;
      id_Finish: IDFinish;
      IDCANCEL: SendMessage(hWindow, WM_CLOSE, 0, 0);
    end;
    WM_CLOSE:
      Result:= DefWindowProc(hWindow, iMsg, WordParam, LongParam);
    WM_DESTROY: PostQuitMessage(0);
  else
    Result:= DefWindowProc(hWindow, iMsg, WordParam, LongParam);
  end
end;

procedure InitApp;
var
  WndClass: TWndClassEx;
begin
  with WndClass do
  begin
    cbSize:= SizeOf(WndClass);
    Style:= CS_HREDRAW or CS_VREDRAW;
    lpfnWndProc:= @WindowProc;
    cbClsExtra:= 0;
    cbWndExtra:= DLGWINDOWEXTRA;
    {$IFDEF VER90}  {delphi 2}
    hInstance:= System.HInstance;
    {$ELSE}
    hInstance:= System.MainInstance;
    {$ENDIF}
    hIcon:= LoadIcon(hInstance, IconName);
    hCursor:= LoadCursor(0, IDC_ARROW);
    hbrBackground:= HBRUSH(COLOR_WINDOW);
    lpszMenuName:= nil;
    lpszClassName:= AppName;
    hIconSm:= LoadIcon(hInstance, IconName);
  end;
  RegisterClassEx(WndClass)
end;

procedure MessageLoop;
var
  Msg: TMsg;
begin
  while GetMessage(Msg, 0, 0, 0) do
  if not IsDialogMessage(hWindow, Msg) then
  begin
    TranslateMessage(Msg);
    DispatchMessage(Msg)
  end
end;

begin
  hDriver:= Lc4hsOpen('4HS1');
  if hDriver = INVALID_HANDLE_VALUE then
  begin
    MessageBox(0, 'Could not open device 4HS1', 'Fatal error', MB_OK or MB_ICONSTOP)
  end else
  begin
    InitApp;
    {$IFDEF VER90}
    hWindow:= CreateDialog( HInstance, AppName, 0, nil);
    {$ELSE}
    hWindow:= CreateDialog( System.MainInstance, AppName, 0, nil);
    {$ENDIF}
    ShowWindow(hWindow, System.CmdShow);
    MessageLoop
  end;
  CloseHandle(hDriver)
end.
