1 {
2 Copyright (C) Alexey Torgashin, uvviewsoft.com
3 License: MPL 2.0 or LGPL
4 }
5 unit ATSynEdit_Gutter;
6 
7 {$mode objfpc}{$H+}
8 
9 interface
10 
11 uses
12   Classes, SysUtils,
13   ATStringProc;
14 
15 type
16   TATGutterItem = class
17     Visible: boolean;
18     Size: integer;
19     Left, Right: integer;
20     Scaled: boolean;
21   end;
22 
23 type
24 
25   { TATGutter }
26 
27   TATGutter = class
28   private
29     FList: TFPList;
GetItemnull30     function GetItem(N: integer): TATGutterItem;
31   public
32     GutterLeft: integer;
33     constructor Create; virtual;
34     destructor Destroy; override;
IsIndexValidnull35     function IsIndexValid(N: integer): boolean; inline;
36     procedure Add(ASize: integer);
37     procedure Delete(N: integer);
38     procedure Clear;
Countnull39     function Count: integer; inline;
40     property Items[N: integer]: TATGutterItem read GetItem; default;
Widthnull41     function Width: integer;
42     procedure Update;
IndexAtnull43     function IndexAt(AX: integer): integer;
44   end;
45 
46 
47 implementation
48 
49 { TATGutter }
50 
IsIndexValidnull51 function TATGutter.IsIndexValid(N: integer): boolean; inline;
52 begin
53   Result:= (N>=0) and (N<FList.Count);
54 end;
55 
GetItemnull56 function TATGutter.GetItem(N: integer): TATGutterItem;
57 begin
58   if IsIndexValid(N) then
59     Result:= TATGutterItem(FList[N])
60   else
61     Result:= nil;
62 end;
63 
64 constructor TATGutter.Create;
65 begin
66   inherited;
67   FList:= TFPList.Create;
68 end;
69 
70 destructor TATGutter.Destroy;
71 begin
72   Clear;
73   FreeAndNil(FList);
74   inherited;
75 end;
76 
77 procedure TATGutter.Add(ASize: integer);
78 var
79   Item: TATGutterItem;
80 begin
81   Item:= TATGutterItem.Create;
82   Item.Size:= ASize;
83   Item.Visible:= true;
84   FList.Add(Item);
85   Update;
86 end;
87 
88 procedure TATGutter.Delete(N: integer);
89 begin
90   if IsIndexValid(N) then
91   begin
92     TObject(FList[N]).Free;
93     FList.Delete(N);
94   end;
95   Update;
96 end;
97 
98 procedure TATGutter.Clear;
99 var
100   i: integer;
101 begin
102   for i:= Count-1 downto 0 do
103     Delete(i);
104 end;
105 
TATGutter.Countnull106 function TATGutter.Count: integer; inline;
107 begin
108   Result:= FList.Count;
109 end;
110 
TATGutter.Widthnull111 function TATGutter.Width: integer;
112 begin
113   if Count>0 then
114     Result:= Items[Count-1].Right - GutterLeft
115   else
116     Result:= 0;
117 end;
118 
119 procedure TATGutter.Update;
120 var
121   i: integer;
122 begin
123   for i:= 0 to Count-1 do
124     with Items[i] do
125     begin
126       if i>0 then
127         Left:= Items[i-1].Right
128       else
129         Left:= GutterLeft;
130       Right:= Left;
131       if Visible then
132       begin
133         if Scaled then
134           Inc(Right, EditorScale(Size))
135         else
136           Inc(Right, Size);
137       end;
138     end;
139 end;
140 
TATGutter.IndexAtnull141 function TATGutter.IndexAt(AX: integer): integer;
142 var
143   i: integer;
144 begin
145   Result:= -1;
146   for i:= 0 to Count-1 do
147     with Items[i] do
148       if (AX>=Left) and (AX<Right) then
149       begin
150         Result:= i;
151         Exit
152       end;
153 end;
154 
155 end.
156 
157