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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
program listviewtest;
{$mode objfpc}{$H+}
uses
Classes, sysutils,
fpg_base, fpg_main, fpg_listview, fpg_form, fpg_button, fpg_edit,
fpg_checkbox, fpg_splitter, fpg_panel, fpg_imagelist, fpg_stringutils;
type
{ TMainForm }
TMainForm = class(TfpgForm)
procedure ShowFocusItemChange(Sender: TObject);
private
FEdit: TfpgEdit;
FAddButton: TfpgButton;
FListView: TfpgListView;
FSplitter: TfpgSplitter;
FTmpListView: TfpgListView;
FQuitButton: TfpgButton;
FCheck: TfpgCheckBox;
FShowFocus: TfpgCheckBox;
procedure LVColumnClicked(Listview: TfpgListView; Column: TfpgLVColumn; Button: Integer);
procedure CloseBttn(Sender: TObject);
procedure AddBttn(Sender: TObject);
procedure SortButton(Sender: TObject);
procedure ShowHeadersChange(Sender: TObject);
procedure PaintItem(ListView: TfpgListView; ACanvas: TfpgCanvas; Item: TfpgLVItem;
ItemIndex: Integer; Area:TfpgRect; var PaintPart: TfpgLVItemPaintPart);
procedure ItemSelectionChanged(ListView: TfpgListView; Item: TfpgLVItem;
ItemIndex: Integer; Selected: Boolean);
public
constructor Create(AOwner: TComponent); override;
end;
{ TMainForm }
procedure TMainForm.ShowFocusItemChange(Sender: TObject);
begin
FListView.ShowFocusRect:=TfpgCheckBox(Sender).Checked;
end;
procedure TMainForm.LVColumnClicked(Listview: TfpgListView; Column: TfpgLVColumn;
Button: Integer);
begin
if Column.ColumnIndex = 0 then
begin
SortButton(nil);
end;
end;
procedure TMainForm.CloseBttn(Sender: TObject);
begin
Close;
end;
procedure TMainForm.AddBttn(Sender: TObject);
var
Item: TfpgLVItem;
I: Integer;
begin
FListView.BeginUpdate;
FTmpListView.BeginUpdate;
//FListView.Items.Capacity := FListView.Items.Capacity + 2000000;
//for I := 0 to 1999999 do begin
Item := FListView.ItemAdd;
Item.Caption := FEdit.Text + IntToStr(Random(1000));
Item.SubItems.Add('c0');
Item.SubItems.Add('c1');
Item.SubItems.Add('c2');
Item.SubItems.Add('c3');
Item.SubItems.Add('c4');
//end;
FListView.EndUpdate;
FTmpListView.EndUpdate;
end;
function CompareProc(Item1, Item2: Pointer): Integer;
var
a, b: TfpgLVItem;
begin
a := TfpgLVItem(Item1);
b := TfpgLVItem(Item2);
if UTF8Length(a.Caption) < UTF8Length(b.Caption) then
Result := -1
else if UTF8Length(a.Caption) > UTF8Length(b.Caption) then
Result := 1
else
Result := CompareText(a.Caption, b.Caption);
end;
procedure TMainForm.SortButton(Sender: TObject);
begin
FListView.Items.Sort(@CompareProc);
end;
procedure TMainForm.ShowHeadersChange(Sender: TObject);
begin
FListView.ShowHeaders := TfpgCheckBox(Sender).Checked;
end;
procedure TMainForm.PaintItem(ListView: TfpgListView; ACanvas: TfpgCanvas;
Item: TfpgLVItem; ItemIndex: Integer; Area: TfpgRect; var PaintPart: TfpgLVItemPaintPart);
begin
if ItemIndex mod 2 = 0 then ACanvas.TextColor := clRed;
if ItemIndex mod 3 = 0 then ACanvas.TextColor := clBlue;
if ItemIndex mod 4 = 0 then ACanvas.TextColor := clGray;
if ItemIndex mod 5 = 0 then ACanvas.TextColor := clPink;
end;
procedure TMainForm.ItemSelectionChanged(ListView: TfpgListView;
Item: TfpgLVItem; ItemIndex: Integer; Selected: Boolean);
begin
//WriteLn('Item changed: ', ItemIndex, ' ', Item.Caption, ' ',Selected);
end;
constructor TMainForm.Create(AOwner: TComponent);
var
LVColumn: TfpgLVColumn;
TopPanel,
BottomPanel: TfpgPanel;
IL: TStringList;
i: Integer;
FImageList: TfpgImageList;
FSelectedImageList: TfpgImageList;
TmpImage: TfpgImage;
begin
inherited Create(AOwner);
Randomize;
WindowTitle := 'ListView Test';
SetPosition(200, 200, 640, 480);
IL := TStringList.Create;
fpgImages.ListImages(IL);
FImageList := TfpgImageList.Create;
FSelectedImageList := TfpgImageList.Create;
for i := 0 to IL.Count-1 do
FImageList.AddImage(fpgImages.GetImage(IL.Strings[i]));
IL.Free;
// invert the items for the 'selected' images
for i := 0 to FImageList.Count-1 do
begin
TmpImage := FImageList.Items[i].Image.ImageFromSource;
TmpImage.Invert;
FSelectedImageList.AddImage(TmpImage);
end;
BottomPanel := TfpgPanel.Create(Self);
BottomPanel.Align := alBottom;
BottomPanel.Height := 40;
BottomPanel.Parent := Self;
BottomPanel.Text := '';
TopPanel := TfpgPanel.Create(Self);
TopPanel.Align := alClient;
TopPanel.Parent := Self;
TopPanel.Text := '';
FListView := TfpgListView.Create(TopPanel);
with FListView do begin
Parent := TopPanel;
Align := alLeft;
Width := 320;
OnPaintItem := @PaintItem;
OnSelectionChanged := @ItemSelectionChanged;
MultiSelect := True;
Images := FImageList;
SubItemImages := FImageList;
ImagesSelected := FSelectedImageList;
OnColumnClick := @LVColumnClicked;
end;
FSplitter := TfpgSplitter.Create(TopPanel);
with FSplitter do begin
Parent := TopPanel;
Align:=alLeft;
end;
FTmpListView := TfpgListView.Create(TopPanel);
with FTmpListView do begin
Parent := TopPanel;
Align := alClient;
//OnPaintItem := @PaintItem;
Items := FListView.Items;
end;
LVColumn := TfpgLVColumn.Create(FListView.Columns);
LVColumn.Caption := 'Column 1';
LVColumn.Width := 150;
LVColumn.Height := 50;
LVColumn.Resizable := True;
LVColumn.Alignment := taLeftJustify;
LVColumn.ColumnIndex := 0;
FListView.Columns.Add(LVColumn);
FTmpListView.Columns.Add(LVColumn);
LVColumn := TfpgLVColumn.Create(FListView.Columns);
LVColumn.Caption := 'Column 2';
LVColumn.Width := 100;
LVColumn.Height := 50;
LVColumn.Alignment := taCenter;
LVColumn.ColumnIndex := 1;
//LVColumn.Visible := False;
FListView.Columns.Add(LVColumn);
//FTmpListView.Columns.Add(LVColumn);
LVColumn := TfpgLVColumn.Create(FListView.Columns);
LVColumn.Caption := 'Column 3';
LVColumn.Width := 200;
LVColumn.Height := 50;
//LVColumn.Visible := False;
LVColumn.Resizable := True;
LVColumn.Alignment := taRightJustify;
LVColumn.ColumnIndex := 2;
FListView.Columns.Add(LVColumn);
FTmpListView.Columns.Add(LVColumn);
FEdit := TfpgEdit.Create(BottomPanel);
with FEdit do begin
Parent := BottomPanel;
Top := 10;
Left := 10;
Width := 100;
end;
FAddButton := TfpgButton.Create(BottomPanel);
with FAddButton do begin
Parent := BottomPanel;
Top := 10;
Left := 120;
Width := 80;
Text := 'Add';
OnClick := @AddBttn;
end;
FQuitButton := TfpgButton.Create(BottomPanel);
with FQuitButton do begin
Parent := BottomPanel;
ImageName := 'stdimg.Quit';
ShowImage := True;
Top := 10;
Left := -10;
Width := 80;
Text := 'Quit';
Anchors := [anRight, anBottom];
OnClick := @CloseBttn;
end;
FCheck := TfpgCheckBox.Create(BottomPanel);
with FCheck do begin
Parent := BottomPanel;
Top := 10;
Left := 205;
Width := 110;
Checked := True;
Text := 'Show Headers';
OnChange := @ShowHeadersChange;
end;
FShowFocus := TfpgCheckBox.Create(BottomPanel);
with FShowFocus do begin
Parent := BottomPanel;
Top := 10;
Left := 320;
Width := 130;
Checked := True;
Text := 'Show Item Focus';
OnChange:=@ShowFocusItemChange;
end;
end;
begin
fpgApplication.Initialize;
with TMainForm.Create(nil) do
begin
Show;
fpgApplication.Run;
Free;
end;
end.
|