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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
program treeviewtest;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes,
SysUtils,
fpg_base,
fpg_main,
fpg_form,
fpg_tree,
fpg_checkbox,
fpg_button,
fpg_imagelist,
fpg_label,
fpg_dialogs,
fpg_combobox,
fpg_utils,
stateimages;
type
TMainForm = class(TfpgForm)
private
{@VFD_HEAD_BEGIN: MainForm}
tree: TfpgTreeView;
cbShowImages: TfpgCheckBox;
cbIndentNode: TfpgCheckBox;
btnClear: TfpgButton;
Label1: TfpgLabel;
Label2: TfpgLabel;
Label3: TfpgLabel;
Label4: TfpgLabel;
Label5: TfpgLabel;
lblSource: TfpgLabel;
lblDestination: TfpgLabel;
btnSource: TfpgButton;
btnDest: TfpgButton;
btnMoveTo: TfpgButton;
cbMoveToTypes: TfpgComboBox;
Label6: TfpgLabel;
Label7: TfpgLabel;
Label8: TfpgLabel;
btnCollapseAll: TfpgButton;
btnExpandAll: TfpgButton;
btnShowChecked: TfpgButton;
{@VFD_HEAD_END: MainForm}
FImagelist: TfpgImageList;
FStateImagelist: TfpgImageList;
FSrcNode: TfpgTreeNode;
FDestnode: TfpgTreeNode;
procedure cbShowImagesChange(Sender: TObject);
procedure cbIndentNodeChange(Sender: TObject);
procedure btnClearClicked(Sender: TObject);
procedure TreeNodeChanged(Sender: TObject);
procedure PopulateTree;
procedure btnSourceClicked(Sender: TObject);
procedure btnDestinationClicked(Sender: TObject);
procedure btnMoveToClicked(Sender: TObject);
procedure btnCollapseAllClicked(Sender: TObject);
procedure btnExpandAllClicked(Sender: TObject);
procedure StateImageClicked(Sender: TObject; ANode: TfpgTreeNode);
procedure btnShowCheckedClicked(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure AfterCreate; override;
end;
{ TMainForm }
procedure TMainForm.cbIndentNodeChange(Sender: TObject);
begin
tree.IndentNodeWithNoImage := cbIndentNode.Checked;
end;
procedure TMainForm.btnClearClicked(Sender: TObject);
var
n: TfpgTreeNode;
begin
if tree.Selection <> nil then
begin
n := tree.Selection;
tree.Selection.Clear;
tree.Invalidate;
tree.Selection := n;
end;
end;
procedure TMainForm.TreeNodeChanged(Sender: TObject);
const
cParent = 'Parent = %s';
cPrev = 'Prev = %s';
cNext = 'Next = %s';
cFirstSubNode = 'FirstSubnode = %s';
cLastSubNode = 'LastSubnode = %s';
var
n: TfpgTreeNode;
procedure PrintNodeInfo(ALabel: TfpgLabel; AFormat: string; ANode: TfpgTreeNode);
begin
if ANode = nil then
ALabel.Text := Format(AFormat, ['nil'])
else
ALabel.Text := Format(AFormat, [ANode.Text]);
end;
begin
n := Tree.Selection;
PrintNodeInfo(Label1, cParent, n.Parent);
PrintNodeInfo(Label2, cPrev, n.Prev);
PrintNodeInfo(Label3, cNext, n.Next);
PrintNodeInfo(Label4, cFirstSubNode, n.FirstSubNode);
PrintNodeInfo(Label5, cLastSubNode, n.LastSubNode);
end;
procedure TMainForm.PopulateTree;
var
n: TfpgTreeNode;
i: integer;
n2: TfpgTreeNode;
s: TfpgString;
begin
n := tree.RootNode.AppendText('Node 1');
n.ImageIndex := 0;
n.StateImageIndex := 1;
n2 := n.AppendText('Node 1.1');
n2.ImageIndex := 1;
n2.StateImageIndex := 1;
n2 := n.AppendText('Node 1.2');
n2.ImageIndex := 1;
n2.StateImageIndex := 0;
n := tree.RootNode.AppendText('Node 2');
n.ImageIndex := 0;
n.AppendText('Node 2.1').ImageIndex := 1;
n := n.AppendText('Node 2.2 The quick brownfox jumps over the...');
n.ImageIndex := 1;
for i := 1 to 3 do
begin
s := Format('Node 2.2.%d', [i]);
if i = 2 then
n2 := n.AppendText(s)
else
n.AppendText(s);
end;
n2 := n.Parent.AppendText('Node 2.3');
n2.StateImageIndex := 0;
tree.RootNode.FirstSubNode.Next.Collapse;
tree.RootNode.AppendText('Node 3').ImageIndex := 0;
tree.Selection := n;
// n := tree.RootNode.FindSubNode('Node 2.2.2', True);
if Assigned(n2) then
begin
n2.AppendText('Child 1').AppendText('Child 2');
n2.Collapsed := False;
end;
TreeNodeChanged(nil);
end;
procedure TMainForm.btnSourceClicked(Sender: TObject);
begin
FSrcNode := tree.Selection;
if Assigned(FSrcNode) then
lblSource.Text := FSrcNode.Text
else
lblSource.Text := '--';
end;
procedure TMainForm.btnDestinationClicked(Sender: TObject);
begin
FDestNode := tree.Selection;
if Assigned(FDestNode) then
lblDestination.Text := FDestNode.Text
else
lblDestination.Text := '--';
end;
procedure TMainForm.btnMoveToClicked(Sender: TObject);
const
cEmpty = '--';
var
i: integer;
begin
if FSrcNode = FDestNode then
begin
TfpgMessageDialog.Warning('', 'Source and Destination may not be the same');
exit;
end;
if (FSrcNode = nil) or (FDestnode = nil) then
begin
TfpgMessageDialog.Warning('', 'Both Source and Destintation needs to be set first');
exit;
end;
i := cbMoveToTypes.FocusItem;
FSrcNode.MoveTo(FDestnode, TfpgNodeAttachMode(i)); // This cast is a hack! Do not do this in real-world apps!!
tree.FullExpand;
tree.Invalidate;
// reset values
FSrcNode := nil;
FDestnode := nil;
lblSource.Text := cEmpty;
lblDestination.Text := cEmpty;
TreeNodeChanged(nil);
end;
procedure TMainForm.StateImageClicked(Sender: TObject; ANode: TfpgTreeNode);
begin
case ANode.StateImageIndex of
0: ANode.StateImageIndex := 1;
1: ANode.StateImageIndex := 0;
end;
tree.Invalidate;
end;
procedure TMainForm.btnShowCheckedClicked(Sender: TObject);
var
n: TfpgTreeNode;
s: string;
begin
s := 'These are all the nodes that have checkboxes and are checked:' + LineEnding;
n := tree.RootNode;
while n <> nil do
begin
if n.StateImageIndex = 1 then
s += LineEnding + n.Text;
n := Tree.NextNode(n);
end;
ShowMessage(s);
end;
procedure TMainForm.cbShowImagesChange(Sender: TObject);
begin
tree.ShowImages := cbShowImages.Checked;
end;
constructor TMainForm.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// create a image list
FImagelist := TfpgImageList.Create;
if fpgFileExists('../../../images/folder_16.bmp') then
FImagelist.AddItemFromFile(SetDirSeparators('../../../images/folder_16.bmp'), 0);
if fpgFileExists('../../../images/menu_preferences_16.bmp') then
begin
FImagelist.AddItemFromFile(SetDirSeparators('../../../images/menu_preferences_16.bmp'), 1);
FImagelist.Items[1].Image.CreateMaskFromSample(0, 0);
FImagelist.Items[1].Image.UpdateImage;
end;
InitializeCustomImages;
FStateImagelist := TfpgImageList.Create;
FStateImagelist.AddImage(fpgImages.GetImage('usr.state0'));
FStateImagelist.AddImage(fpgImages.GetImage('usr.state1'));
end;
destructor TMainForm.Destroy;
var
i: integer;
begin
tree.ImageList := nil;
tree.StateImageList := nil;
FImagelist.Free;
for i := FStateImageList.Count-1 downto 0 do
FStateImageList[i].Image := nil;
FStateImageList.Free;
inherited Destroy;
end;
procedure TMainForm.AfterCreate;
begin
inherited AfterCreate;
{@VFD_BODY_BEGIN: MainForm}
Name := 'MainForm';
SetPosition(399, 184, 709, 333);
WindowTitle := 'Treeview Test';
Hint := '';
WindowPosition := wpScreenCenter;
tree := TfpgTreeView.Create(self);
with tree do
begin
Name := 'tree';
SetPosition(8, 8, 284, 267);
Anchors := [anLeft,anTop,anBottom];
FontDesc := '#Label1';
Hint := '';
ScrollWheelDelta := 30;
ShowImages := True;
TabOrder := 0;
ImageList := FImagelist;
StateImageList := FStateImagelist;
OnChange := @TreeNodeChanged;
OnStateImageClicked := @StateImageClicked;
end;
cbShowImages := TfpgCheckBox.Create(self);
with cbShowImages do
begin
Name := 'cbShowImages';
SetPosition(8, 283, 109, 20);
Anchors := [anLeft,anBottom];
Checked := True;
FontDesc := '#Label1';
Hint := '';
TabOrder := 1;
Text := 'Show images';
OnChange := @cbShowImagesChange;
end;
cbIndentNode := TfpgCheckBox.Create(self);
with cbIndentNode do
begin
Name := 'cbIndentNode';
SetPosition(120, 283, 179, 20);
Anchors := [anLeft,anBottom];
Checked := True;
FontDesc := '#Label1';
Hint := '';
TabOrder := 2;
Text := 'Indent node with no image';
OnChange := @cbIndentNodeChange;
end;
btnClear := TfpgButton.Create(self);
with btnClear do
begin
Name := 'btnClear';
SetPosition(8, 305, 144, 23);
Anchors := [anLeft,anBottom];
Text := 'Clear Selected Node';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 3;
OnClick := @btnClearClicked;
end;
Label1 := TfpgLabel.Create(self);
with Label1 do
begin
Name := 'Label1';
SetPosition(316, 24, 292, 16);
FontDesc := '#Label1';
Hint := '';
Text := 'Label';
end;
Label2 := TfpgLabel.Create(self);
with Label2 do
begin
Name := 'Label2';
SetPosition(316, 40, 288, 16);
FontDesc := '#Label1';
Hint := '';
Text := 'Label';
end;
Label3 := TfpgLabel.Create(self);
with Label3 do
begin
Name := 'Label3';
SetPosition(316, 56, 272, 16);
FontDesc := '#Label1';
Hint := '';
Text := 'Label';
end;
Label4 := TfpgLabel.Create(self);
with Label4 do
begin
Name := 'Label4';
SetPosition(316, 72, 300, 16);
FontDesc := '#Label1';
Hint := '';
Text := 'Label';
end;
Label5 := TfpgLabel.Create(self);
with Label5 do
begin
Name := 'Label5';
SetPosition(316, 88, 320, 16);
FontDesc := '#Label1';
Hint := '';
Text := 'Label';
end;
lblSource := TfpgLabel.Create(self);
with lblSource do
begin
Name := 'lblSource';
SetPosition(316, 172, 140, 16);
FontDesc := '#Label1';
Hint := '';
Text := '--';
end;
lblDestination := TfpgLabel.Create(self);
with lblDestination do
begin
Name := 'lblDestination';
SetPosition(468, 172, 128, 16);
FontDesc := '#Label1';
Hint := '';
Text := '--';
end;
btnSource := TfpgButton.Create(self);
with btnSource do
begin
Name := 'btnSource';
SetPosition(316, 144, 80, 24);
Text := 'Source';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 11;
OnClick := @btnSourceClicked;
end;
btnDest := TfpgButton.Create(self);
with btnDest do
begin
Name := 'btnDest';
SetPosition(468, 144, 80, 24);
Text := 'Destination';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 12;
OnClick := @btnDestinationClicked;
end;
btnMoveTo := TfpgButton.Create(self);
with btnMoveTo do
begin
Name := 'btnMoveTo';
SetPosition(612, 144, 80, 24);
Text := 'MoveTo';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 13;
OnClick := @btnMoveToClicked;
end;
cbMoveToTypes := TfpgComboBox.Create(self);
with cbMoveToTypes do
begin
Name := 'cbMoveToTypes';
SetPosition(612, 172, 96, 22);
ExtraHint := '';
FontDesc := '#List';
Hint := '';
Items.Add('naAdd');
Items.Add('naAddFirst');
Items.Add('naAddChild');
Items.Add('naAddChildFirst');
Items.Add('naInsert');
FocusItem := 0;
TabOrder := 14;
end;
Label6 := TfpgLabel.Create(self);
with Label6 do
begin
Name := 'Label6';
SetPosition(300, 4, 348, 16);
FontDesc := '#Label2';
Hint := '';
Text := 'Selected Node Information';
end;
Label7 := TfpgLabel.Create(self);
with Label7 do
begin
Name := 'Label7';
SetPosition(300, 120, 348, 16);
FontDesc := '#Label2';
Hint := '';
Text := 'Reorder Nodes (testing MovoTo() function)';
end;
Label8 := TfpgLabel.Create(self);
with Label8 do
begin
Name := 'Label8';
SetPosition(300, 200, 308, 16);
FontDesc := '#Label2';
Hint := '';
Text := 'Test various Tree functions';
end;
btnCollapseAll := TfpgButton.Create(self);
with btnCollapseAll do
begin
Name := 'btnCollapseAll';
SetPosition(316, 224, 92, 24);
Text := 'Collapse All';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 19;
OnClick := @btnCollapseAllClicked;
end;
btnExpandAll := TfpgButton.Create(self);
with btnExpandAll do
begin
Name := 'btnExpandAll';
SetPosition(416, 224, 92, 24);
Text := 'Expand All';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 20;
OnClick := @btnExpandAllClicked;
end;
btnShowChecked := TfpgButton.Create(self);
with btnShowChecked do
begin
Name := 'btnShowChecked';
SetPosition(160, 304, 100, 24);
Text := 'Show Checked';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 21;
OnClick := @btnShowCheckedClicked;
end;
{@VFD_BODY_END: MainForm}
PopulateTree;
end;
procedure TMainForm.btnCollapseAllClicked(Sender: TObject);
begin
tree.FullCollapse;
end;
procedure TMainForm.btnExpandAllClicked(Sender: TObject);
begin
tree.FullExpand;
end;
procedure MainProc;
var
frm: TMainForm;
begin
fpgApplication.Initialize;
frm := TMainForm.Create(nil);
try
frm.Show;
fpgApplication.Run;
finally
frm.Free;
end;
end;
begin
MainProc;
end.
|