{**********************************************}
{   TeeChart and TeeTree About Form            }
{   Copyright (c) 1995-2020 by Steema Software }
{**********************************************}
unit TeeAbout;
{$I TeeDefs.inc}

interface

uses {$IFDEF LINUX}
     //Libc,  <-- not valid for FPC (obsolete: Kylix)
     {$ELSE}
     {$IFDEF LCL}
     LCLIntf,
     {$ELSE}
     Windows, Messages,
     {$ENDIF}
     {$ENDIF}

     Graphics, Controls, Forms, ExtCtrls, StdCtrls, Buttons,

     {$IFDEF LCL}
     jpeglib,
     {$ELSE}
     jpeg,
     {$ENDIF}

     Classes, SysUtils;

type
  TTeeAboutForm = class(TForm)
    BClose: TButton;
    LabelCopy: TLabel;
    LabelVersion: TLabel;
    LabelRights: TLabel;
    Labelwww: TLabel;
    Image1: TImage;
    Bevel1: TBevel;
    LabelExtra: TLabel;
    LabelUpdate: TLabel;
    {$IFDEF TEEOCX}
    Image2: TImage;
    {$ENDIF}
    LabelLicense: TLabel;
    LabelEval: TLabel;
    Timer1: TTimer;
    procedure LabelwwwClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure LabelUpdateClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  protected
    {$IFDEF TEEEXCEPTIONS}
    UpdateURL : String;

    function UpdateAvailable(out Build:String):String; virtual;
    {$ENDIF}

    function GetLicense:String; virtual;
    procedure Prepare(Const ACaption,AVersion,AExtra:String);
  public
    { Public declarations }
  end;

{ Displays the TeeChart about-box dialog }
Procedure TeeShowAboutBox(Const ACaption:String=''; Const AVersion:String='';
                          Const AExtra:String='');

Var TeeAboutBoxProc:Procedure=nil;
    TeeIsTrial:Boolean=False;

implementation

{$IFNDEF LCL}
{$R *.DFM}
{$ELSE}
{$R *.lfm}
{$ENDIF}

Uses {$IFDEF TEEEXCEPTIONS}
     {$IFNDEF FPC}
     TeeException,
     {$ENDIF}
     {$ENDIF}
     TeeProcs, TeeConst;

Procedure TeeShowAboutBox(Const ACaption:String=''; Const AVersion:String='';
                          Const AExtra:String='');
var tmp : TTeeAboutForm;
begin
  tmp:=TTeeAboutForm.Create(nil);

  with tmp do
  try
    Prepare(ACaption,AVersion,AExtra);
    ShowModal;
  finally
    Free;
  end;
end;

procedure TTeeAboutForm.LabelwwwClick(Sender: TObject);
begin
  {$IFDEF TEELITE}
  TeeGotoURL(Handle,TeeRADXE2_SpecialWWW);
  {$ELSE}
  TeeGotoURL(Handle,LabelWWW.Caption);
  {$ENDIF}
end;

procedure TTeeAboutForm.FormCreate(Sender: TObject);
begin
  DoubleBuffered:=False;

  Caption:=Caption+' '+TeeMsg_Version; // Do not localize
  LabelVersion.Caption:=TeeMsg_Version;  // Do not localize

  LabelCopy.Caption:=TeeMsg_Copyright;
  BorderStyle:=bsDialog;

  {$IFDEF TEEOCX}
  Image1.Visible := False;
  Image2.Visible := True;
  {$ENDIF}

  {$IFDEF TEELITE}
  LabelUpdate.Visible:=False;
  LabelExtra.Visible:=False;
  
  Labelwww.Alignment:=taLeftJustify;
  Labelwww.Left:=LabelVersion.Left;
  Labelwww.Top:=LabelRights.Top+LabelRights.Height+6;
  Labelwww.Caption:=TeeRADXE2_SpecialOffer;

  BClose.Top:=LabelCopy.Top;
  {$ENDIF}
end;

Procedure TeeShowAboutBox2;
begin
  if Assigned(TeeAboutBoxProc) then
  begin
    TeeShowAboutBox;
    TeeAboutBoxProc:=nil;
  end;
end;

function TTeeAboutForm.GetLicense:String;
begin
  {$IFDEF TEEEXCEPTIONS}
  result:=TTeeExceptionForm.License;

  if result='' then
     result:='Unregistered'
  else
     result:='License: '+result;
  {$ELSE}
  result:='';
  {$ENDIF}
end;

procedure TTeeAboutForm.FormShow(Sender: TObject);
begin
  LabelLicense.Caption:=GetLicense;
end;

procedure TTeeAboutForm.LabelUpdateClick(Sender: TObject);
begin
  {$IFNDEF TEELITE}
  TeeGotoURL(Handle, UpdateURL);
  {$ENDIF}
end;

{$IFDEF TEEEXCEPTIONS}
function TTeeAboutForm.UpdateAvailable(out Build:String): String;
var tmp : TStringStream;
    URL,
    s   : String;
begin
  result:='';

  if Assigned(TeeDownloadURLProc) then
  begin
    tmp:=TStringStream.Create('');
    try
      URL:='http://steema.com/exceptions/latest.php?ide='+
                         TTeeExceptionForm.IDE+
                         '&prod='+TTeeExceptionForm.Product+
                         '&sub='+TTeeExceptionForm.SubProduct;

      if TeeDownloadURLProc(URL, tmp) = 0 then
      begin
        s:=tmp.DataString;

        if Copy(s,1,2)='OK' then
        begin
          Build:=TeeExtractField(s,2,Chr($A));

          if Build <> TeeBuild then
             result:=TeeExtractField(s,3,Chr($A));
        end;
      end;

    finally
      tmp.Free;
    end;
  end;
end;
{$ENDIF}

procedure TTeeAboutForm.Timer1Timer(Sender: TObject);
{$IFDEF TEEEXCEPTIONS}
var tmpBuild : String;
{$ENDIF}
begin
  Timer1.Enabled:=False;

  {$IFDEF TEEEXCEPTIONS}
  UpdateURL:=UpdateAvailable(tmpBuild);

  if UpdateURL<>'' then
     LabelUpdate.Caption:='Update v'+tmpBuild+' available';
  {$ENDIF}
end;

procedure TTeeAboutForm.Prepare;
begin
  if ACaption<>'' then
     Caption:=ACaption;

  if AVersion<>'' then
     LabelVersion.Caption:=AVersion;

  if AExtra<>'' then
     LabelExtra.Caption:=AExtra;

  {$IFDEF TEEBETA}
  LabelEval.Caption:='(BETA)'; // Do not localize
  {$ENDIF}

  if TeeIsTrial then
  begin
    if LabelEval.Caption<>'' then
       LabelEval.Caption:=LabelEval.Caption+' ';

    LabelEval.Caption:=LabelEval.Caption+'TRIAL'; // Do not localize
  end;

  TeeTranslateControl(Self);
end;

initialization
  TeeAboutBoxProc:=TeeShowAboutBox2;
finalization
  TeeAboutBoxProc:=nil;
end.
