unit progress;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Constants;

type
  TForm5 = class(TForm)
    ProgressBar1: TProgressBar;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form5: TForm5;

implementation
uses JHInterface, JHParams;

// used because appending to memo control adds random newlines to text.
// we just append progress text to this and than sent it to memo.
var lastLine:string='';

var progressPercent:real = 0.0;
var taskCount:integer = 0;
const PROGRESS_DELTA = 0.056;  // 1 / number of tasks

//==============================================================================
procedure StatusMsg(const msg:string);
begin
    lastLine := msg;
    Form5.Memo1.Lines.Add(msg);
    SendMessage(Form5.Memo1.Handle, EM_SCROLLCARET, 0, 0);
    Form5.Update;
end;

//==============================================================================
// pos < 0   is automatic progress increase
// pos >= 0  is manual progress setting 
//==============================================================================
procedure StatusEnd(const pos:real = -1);
var i : integer;
begin
  i := Form5.Memo1.Lines.Count-1;
  Form5.Memo1.Lines.Delete(i);
  taskCount := taskCount + 1;
  //Form5.Memo1.Lines.Add(lastLine+msg+IntToStr(taskCount));
  Form5.Memo1.Lines.Add(lastLine+'OK');
  SendMessage(Form5.Memo1.Handle, EM_SCROLLCARET, 0, 0);
  if pos < 0 then begin
    progressPercent := progressPercent + PROGRESS_DELTA;
  end else begin
    progressPercent := pos;
  end;
  Form5.ProgressBar1.Position:=Round(progressPercent*100);
  Form5.Update;
end;

//==============================================================================
procedure show(const title:string);
begin
    taskCount := 0;
    progressPercent := 0;
    Form5:=TForm5.Create(nil);
    Form5.Caption := title;
    Form5.Show;
end;

//==============================================================================
procedure BusyWait(const ms:Cardinal);
var endTime : Cardinal;
begin
  endTime := GetTickCount()+ms;
  while GetTickCount() < endTime do
    begin
      // do nothing
    end;
end;

//==============================================================================
procedure stop;
begin
    Form5.ProgressBar1.Position:=100; // full bar
    Form5.Update;
    // half a second delay before turning off progress
    // gives mapmaker a chance to see last messages.
		//if IsDebugMode() then BusyWait(500);
    Form5.Destroy();
end;

{$R *.dfm}

//==============================================================================
procedure TForm5.FormCreate(Sender: TObject);
begin
    Top:=(Screen.Height - Height) dIV 2-14;
    Left:=(Screen.Width - Width) dIV 2;
    Memo1.Text := '';
end;

//==============================================================================
//  Override default console progress display
//==============================================================================
begin
    JHInterface.ProInit := show;
    //JHInterface.ProPosition := SetPosition;
    JHInterface.ProStatus := StatusMsg;
    JHInterface.ProEnd := StatusEnd;
    JHInterface.ProRelease := stop;
end.
