{**********************************************}
{   TeeChart Link Tool Editor                  }
{   Copyright (c) 2007-2020 by Steema Software }
{**********************************************}
unit TeeLinkTool;
{$I TeeDefs.inc}

interface

uses
  {$IFNDEF LINUX}
  {$IFNDEF FMX}
  Windows, Messages,
  {$ENDIF}
  {$ENDIF}
  SysUtils, Classes,
             
  {$IFDEF D17}
  System.UIConsts,
  {$ENDIF}
  
  {$IFDEF FMX}
  FMX.Types, System.UITypes,
  {$ELSE}
  Graphics, Controls, Forms,
  {$ENDIF}

  {$IFDEF FMX}
  FMXTee.Canvas, FMXTee.Engine, FMXTee.Chart, FMXTee.Procs, FMXTee.Tools
  {$ELSE}
  TeeTools, TeeProcs, TeCanvas, TeEngine, Chart
  {$ENDIF}
  ;

type
  TLinkTool=class(TAnnotationTool)
  private
    FHint : String;
    FURL  : String;

    {$IFNDEF FMX}
    IOldHint : String;
    {$ENDIF}
    
    procedure SetHint(const Value: String);
    procedure SetURL(const Value: String);
  protected
    Procedure ChartMouseEvent( AEvent: TChartMouseEvent;
                               Button:TMouseButton;
                               Shift: TShiftState; X, Y: Integer); override;
    Procedure DoDrawText(const AParent:TCustomAxisPanel); overload; override;
    class Function GetEditorClass:String; override;
  public
    Constructor Create(AOwner:TComponent); override;

    procedure Assign(Source:TPersistent); override;
    class Function Description:String; override;
    class Function LongDescription:String; override;
  published
    property Cursor default crHandPoint;
    property Hint:String read FHint write SetHint;
    property URL:String read FURL write SetURL;
  end;

implementation

uses
  {$IFDEF FMX}
  FMXTee.ProConstants
  {$ELSE}
  TeeProCo
  {$ENDIF}
  ;

{ TLinkTool }

Constructor TLinkTool.Create(AOwner: TComponent);
begin
  inherited;

  Cursor:=crHandPoint;

  with Shape do
  begin
    Transparent:=True;
    Shadow.Visible:=False;
    Font.Color:={$IFDEF FMX}claNavy{$ELSE}clNavy{$ENDIF};
    Font.Style:=[{$IFDEF FMX}TFontStyle.{$ENDIF}fsUnderline];
  end;
end;

class Function TLinkTool.Description:String;
begin
  result:=TeeMsg_LinkTool;
end;

class Function TLinkTool.LongDescription:String;
begin
  result:=TeeMsg_LinkToolDesc;
end;

procedure TLinkTool.SetHint(const Value: String);
begin
  FHint:=Value;
end;

procedure TLinkTool.SetURL(const Value: String);
begin
  if FURL<>Value then
  begin
    FURL:=Value;

    if (Text='') and (URL<>'') then
       Text:=URL;
  end;
end;

procedure TLinkTool.ChartMouseEvent(AEvent: TChartMouseEvent;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var tmp  : String;
    tmp2 : String;

    {$IFNDEF FPC}
    {$IFNDEF D9}
    tmpW    : TWMMouse;
    {$ENDIF}
    {$ENDIF}
begin
  inherited;

  if (AEvent=cmeDown) and (Button={$IFDEF FMX}TMouseButton.{$ENDIF}mbLeft) and
     Clicked(X,Y) then
  begin
    tmp:=Trim(URL);

    if tmp='' then
    begin
      tmp2:=UpperCase(Trim(Text));

      if (Copy(tmp2,1,4)='WWW.') or (Copy(tmp2,1,7)='HTTP://') then
          tmp:=Trim(Text);
    end;

    if tmp<>'' then
       TeeGotoURL({$IFDEF FMX}0{$ELSE}{$IFDEF LCL}0{$ELSE}Application.Handle{$ENDIF}{$ENDIF},tmp);

    ParentChart.CancelMouse:=True;
  end
  else
  if (AEvent=cmeMove) then
     {$IFNDEF FMX}
     if Clicked(X,Y) then
     begin
       if Hint<>'' then
       begin
         if ParentChart.Hint<>Hint then
         begin
           Application.CancelHint;

           ParentChart.ShowHint:=True;

           IOldHint:=ParentChart.Hint;
           ParentChart.Hint:=Hint;

           {$IFNDEF D9}
           // Workaround a bug before Delphi 2005

           {$IFNDEF LCL}
           tmpW.Msg:=WM_MOUSEMOVE;
           tmpW.XPos:=X;
           tmpW.YPos:=Y;

           Application.HintMouseMessage(ParentChart,TMessage(tmpW));
           {$ENDIF}

           {$ENDIF}
         end;

          // Application.ActivateHint(ParentChart.ClientToScreen(TeePoint(X,Y)));
       end;
     end
     else
     if Hint<>'' then
        if ParentChart.Hint=Hint then
        begin
          Application.CancelHint;
          ParentChart.Hint:=IOldHint;
        end;
     {$ENDIF}
end;

class function TLinkTool.GetEditorClass: String;
begin
  result:='TLinkToolEditor';
end;

type
  TTeePanelAccess=class(TCustomTeePanel);

procedure TLinkTool.DoDrawText(const AParent:TCustomAxisPanel);
begin
  if Supports(AParent.Canvas,ICanvasHyperlinks) then
  begin
    TTeePanelAccess(AParent).TryAssignFont(Shape.Font);
    (AParent.Canvas as ICanvasHyperlinks).AddLink(Left,Top,Text,URL,Hint);
  end
  else
     inherited;
end;

procedure TLinkTool.Assign(Source: TPersistent);
begin
  if Source is TLinkTool then
  with TLinkTool(Source) do
  begin
    Self.FHint:=FHint;
    Self.FURL:=FURL;
  end;

  inherited;
end;

initialization
  RegisterTeeTools([TLinkTool]);
finalization
  UnRegisterTeeTools([TLinkTool]);
end.
