1 {
2 Copyright (C) Alexey Torgashin, uvviewsoft.com
3 License: MPL 2.0 or LGPL
4 }
5 unit ATSynEdit_Hotspots;
6 
7 {$mode objfpc}{$H+}
8 {$ModeSwitch advancedrecords}
9 {$Z1}
10 
11 interface
12 
13 uses
14   Classes, SysUtils,
15   ATSynEdit_fgl,
16   ATSynEdit_Carets;
17 
18 type
19   PATHotspotItem = ^TATHotspotItem;
20   TATHotspotItem = record
21     PosX, PosY: integer;
22     EndX, EndY: integer;
23     Tag: Int64;
24     TagString: string;
25     class operator=(const A, B: TATHotspotItem): boolean;
26   end;
27 
28 type
29 
30   { TATHotspotItems }
31 
32   TATHotspotItems = class(specialize TFPGList<TATHotspotItem>)
33   public
ItemPtrnull34     function ItemPtr(AIndex: integer): PATHotspotItem; inline;
35   end;
36 
37 type
38   { TATHotspots }
39 
40   TATHotspots = class
41   private
42     FList: TATHotspotItems;
GetItemnull43     function GetItem(AIndex: integer): TATHotspotItem;
44   public
45     constructor Create; virtual;
46     destructor Destroy; override;
47     procedure Clear;
Countnull48     function Count: integer; inline;
IsIndexValidnull49     function IsIndexValid(N: integer): boolean; inline;
50     property Items[N: integer]: TATHotspotItem read GetItem; default;
51     procedure Add(const AItem: TATHotspotItem);
52     procedure Insert(N: integer; const AItem: TATHotspotItem);
53     procedure Delete(N: integer);
54     procedure DeleteByTag(const ATag: Int64);
FindByPosnull55     function FindByPos(AX, AY: integer): integer;
FindByTagIntnull56     function FindByTagInt(const ATag: Int64): integer;
FindByTagStringnull57     function FindByTagString(const ATagString: string): integer;
58   end;
59 
60 
61 implementation
62 
63 { TATHotspotItems }
64 
ItemPtrnull65 function TATHotspotItems.ItemPtr(AIndex: integer): PATHotspotItem;
66 begin
67   Result:= PATHotspotItem(InternalGet(AIndex));
68 end;
69 
70 { TATHotspotItem }
71 
72 class operator TATHotspotItem.=(const A, B: TATHotspotItem): boolean;
73 begin
74   Result:= false;
75 end;
76 
77 { TATHotspots }
78 
79 constructor TATHotspots.Create;
80 begin
81   FList:= TATHotspotItems.Create;
82 end;
83 
84 destructor TATHotspots.Destroy;
85 begin
86   Clear;
87   FreeAndNil(FList);
88   inherited;
89 end;
90 
91 procedure TATHotspots.Clear; inline;
92 begin
93   FList.Clear;
94 end;
95 
GetItemnull96 function TATHotspots.GetItem(AIndex: integer): TATHotspotItem;
97 begin
98   Result:= FList[AIndex];
99 end;
100 
TATHotspots.Countnull101 function TATHotspots.Count: integer; inline;
102 begin
103   Result:= FList.Count;
104 end;
105 
IsIndexValidnull106 function TATHotspots.IsIndexValid(N: integer): boolean; inline;
107 begin
108   Result:= (N>=0) and (N<Count);
109 end;
110 
111 procedure TATHotspots.Add(const AItem: TATHotspotItem); inline;
112 begin
113   FList.Add(AItem);
114 end;
115 
116 procedure TATHotspots.Delete(N: integer); inline;
117 begin
118   FList.Delete(N);
119 end;
120 
121 procedure TATHotspots.DeleteByTag(const ATag: Int64);
122 var
123   N: integer;
124 begin
125   repeat
126     N:= FindByTagInt(ATag);
127     if N<0 then Break;
128     Delete(N);
129   until false;
130 end;
131 
132 procedure TATHotspots.Insert(N: integer; const AItem: TATHotspotItem); inline;
133 begin
134   if N>=FList.Count then
135     FList.Add(AItem)
136   else
137     FList.Insert(N, AItem);
138 end;
139 
FindByPosnull140 function TATHotspots.FindByPos(AX, AY: integer): integer;
141 var
142   Item: PATHotspotItem;
143   i: integer;
144 begin
145   Result:= -1;
146   for i:= 0 to Count-1 do
147   begin
148     Item:= FList.ItemPtr(i);
149     if IsPosInRange(AX, AY, Item^.PosX, Item^.PosY, Item^.EndX, Item^.EndY, false) = cRelateInside then
150       exit(i);
151   end;
152 end;
153 
TATHotspots.FindByTagIntnull154 function TATHotspots.FindByTagInt(const ATag: Int64): integer;
155 var
156   i: integer;
157 begin
158   Result:= -1;
159   for i:= 0 to Count-1 do
160     if ATag=FList.ItemPtr(i)^.Tag then
161       exit(i);
162 end;
163 
FindByTagStringnull164 function TATHotspots.FindByTagString(const ATagString: string): integer;
165 var
166   i: integer;
167 begin
168   Result:= -1;
169   for i:= 0 to Count-1 do
170     if ATagString=FList.ItemPtr(i)^.TagString then
171       exit(i);
172 end;
173 
174 end.
175 
176