-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflexlistviewbase.pas
231 lines (184 loc) · 6.17 KB
/
flexlistviewbase.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
{
This unit is part of the flexlistview component.
Copyright (c) 2017 by Andrew Haines.
See the file COPYING.modifiedLGPL, included in this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Description:
This is the base flexlistview classes which are not dependant on any gui
components specifically.
FlexListView aims to simplify mapping arbitrary data into custom views
which can be scrolled using adapters.
If you create a new adapter type it must implement IFlexListAdapter or
alternatively you can descend from TFlexBaseAdapter and override the
needed methods.
}
unit FlexListViewBase;
{$mode objfpc}{$H+}
{$interfaces corba}
interface
uses
Classes, SysUtils, fgl;
type
TAdapterChangeEvent = (acGrow, acShrink, acRebuild, acClear, acAttach, acDetach);
IFlexListAdapterHolder = interface
['{F5B6ADF7-F068-404A-9118-F4637C35CB99}']
procedure AdapterDataChanged(const AChange: TAdapterChangeEvent; const AData: Variant);
end;
IFlexListAdapter = interface
['{BF18BBE2-C395-4F27-ADDC-CC4BDB2318D3}']
procedure AddListHolder(const AValue: IFlexListAdapterHolder);
{ Removes AValue from the list of interest viewers. Returns the number of
Holders remaining. }
function RemoveListHolder(const AValue: IFlexListAdapterHolder): Integer;
function GetCount: Integer;
{ View is a TWincontrol to reuse. the returned value is owned and will be
freed by the ListHolder that called GetView }
function GetView(const View: TObject; const AIndex: Integer): TObject;
function GetObject(const AIndex: Integer): TObject;
procedure SetData(AData: TObject; AOwns: Boolean);
procedure Free; // so we can free the adapter when we are done with it
end;
TFlexListAdapterHolderList = specialize TFPGList<IFlexListAdapterHolder>;
TFlexGetViewEvent = function (const View: TObject; const AIndex: Integer): TObject of object;
{ TFlexBaseAdapter }
TFlexBaseAdapter = class(IFlexListAdapter)
private
FHolders: TFlexListAdapterHolderList;
FIsDestroying: Boolean;
FOnGetView: TFlexGetViewEvent;
FOwnsData: Boolean;
FData: TObject;
procedure AddListHolder(const AValue: IFlexListAdapterHolder);
function RemoveListHolder(const AValue: IFlexListAdapterHolder): Integer;
protected
procedure DoEvent(AEvent: TAdapterChangeEvent; AData: Variant);
procedure SetData(AData: TObject; AOwns: Boolean); virtual;
function GetCount: Integer; virtual; abstract;
function GetView(const View: TObject; const AIndex: Integer): TObject; virtual;
function GetObject(const AIndex: Integer): TObject; virtual;
property OwnsData: Boolean read FOwnsData;
property Data: TObject read FData;
public
constructor Create(AData: TObject; AOwnsData: Boolean); virtual;
destructor Destroy; override;
property OnGetView: TFlexGetViewEvent read FOnGetView write FOnGetView;
procedure Free;
end;
{ TFlexViewListEntry }
TFlexViewListEntry = class(TObject)
private
FData: TObject;
FIndex: Integer;
FView: TObject;
function GetTop: Integer;
function GetHeight: Integer;
procedure SetTop(AValue: Integer);
published
property View: TObject read FView write FView;
property Data: TObject read FData write FData;
property Index: Integer read FIndex write FIndex;
property Height: Integer read GetHeight;
property Top: Integer read GetTop write SetTop;
public
constructor Create(AView: TObject; AData: TObject; AIndex: Integer);
destructor Destroy; override;
end;
TFlexViewListEntryList = specialize TFPGObjectList<TFlexViewListEntry>;
implementation
uses
typinfo;
{ TFlexBaseAdapter }
procedure TFlexBaseAdapter.AddListHolder(const AValue: IFlexListAdapterHolder);
begin
if FHolders.IndexOf(AValue) = -1 then
FHolders.Add(AValue);
AValue.AdapterDataChanged(acAttach, False);
end;
function TFlexBaseAdapter.RemoveListHolder(const AValue: IFlexListAdapterHolder): Integer;
begin
FHolders.Remove(AValue);
Result := FHolders.Count; // if this is zero then the holder will free this adapter
AValue.AdapterDataChanged(acDetach, False);
end;
procedure TFlexBaseAdapter.DoEvent(AEvent: TAdapterChangeEvent; AData: Variant);
var
I : IFlexListAdapterHolder;
begin
try
for IFlexListAdapterHolder(I) in FHolders do
I.AdapterDataChanged(AEvent, AData);
finally
end;
end;
procedure TFlexBaseAdapter.SetData(AData: TObject; AOwns: Boolean);
begin
if Assigned(FData) and FOwnsData then
FreeAndNil(FData);
FData := AData;
FOwnsData:=AOwns;
DoEvent(acRebuild, True);
end;
function TFlexBaseAdapter.GetView(const View: TObject; const AIndex: Integer): TObject;
begin
if Assigned(FOnGetView) then
Result := FOnGetView(View, AIndex)
else
Result := nil;
end;
function TFlexBaseAdapter.GetObject(const AIndex: Integer): TObject;
begin
Result := nil;
end;
constructor TFlexBaseAdapter.Create(AData: TObject; AOwnsData: Boolean);
begin
FHolders := TFlexListAdapterHolderList.Create;
SetData(AData, AOwnsData);
end;
destructor TFlexBaseAdapter.Destroy;
begin
FIsDestroying:= True;
if FOwnsData and Assigned(FData) then
FreeAndNil(FData);
// notify all interested observers we are going away.
DoEvent(acDetach, nil);
FHolders.Free;
end;
procedure TFlexBaseAdapter.Free;
begin
if not FIsDestroying then
inherited Free;
end;
{ TFlexViewListEntry }
function TFlexViewListEntry.GetTop: Integer;
begin
if FView = nil then
Result := 0
else
Result := GetPropValue(FView, 'Top', False);
end;
function TFlexViewListEntry.GetHeight: Integer;
begin
if FView = nil then
Result := 0
else
Result := GetPropValue(FView, 'Height', False);
end;
procedure TFlexViewListEntry.SetTop(AValue: Integer);
begin
SetPropValue(View, 'Top', AValue);
end;
constructor TFlexViewListEntry.Create(AView: TObject; AData: TObject;
AIndex: Integer);
begin
FView := AView;
FData := AData;
FIndex:= AIndex;
end;
destructor TFlexViewListEntry.Destroy;
begin
inherited Destroy;
View.Free;
end;
end.