-
Notifications
You must be signed in to change notification settings - Fork 1
/
outline.pas
705 lines (582 loc) · 16.6 KB
/
outline.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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
unit outline;
//{$CODEPAGE utf8}
{***************************************************************************}
interface
{***************************************************************************}
uses drivers,objects,views;
type Pnode=^Tnode;
Tnode=record
next:Pnode;
text:Pstring;
childlist:Pnode;
expanded:boolean;
end;
Poutlineviewer=^Toutlineviewer;
Toutlineviewer=object(Tscroller)
foc:sw_integer;
constructor init(var bounds:Trect;
AHscrollbar,AVscrollbar:Pscrollbar);
procedure adjust(node:pointer;expand:boolean);virtual;
function creategraph(level:integer;lines:longint;
flags:word;levwidth,endwidth:integer;
const chars:string):string;
procedure draw;virtual;
procedure expandall(node:pointer);
function firstthat(test:pointer):pointer;
procedure focused(i:sw_integer);virtual;
procedure foreach(action:pointer);
function getchild(node:pointer;i:sw_integer):pointer;virtual;
function getgraph(level:integer;lines:longint;flags:word):string;
function getnode(i:sw_integer):pointer;virtual;
function getnumchildren(node:pointer):sw_integer;virtual;
function getpalette:Ppalette;virtual;
function getroot:pointer;virtual;
function gettext(node:pointer):string;virtual;
procedure handleevent(var event:Tevent);virtual;
function haschildren(node:pointer):boolean;virtual;
function isexpanded(node:pointer):boolean;virtual;
function isselected(i:sw_integer):boolean;virtual;
procedure selected(i:sw_integer);virtual;
procedure setstate(Astate:word;enable:boolean);virtual;
procedure update;
private
procedure set_focus(Afocus:sw_integer);
function do_recurse(action,callerframe:pointer;
stop_if_found:boolean):pointer;
end;
Poutline=^Toutline;
Toutline=object(Toutlineviewer)
root:Pnode;
constructor init(var bounds:Trect;
AHscrollbar,AVscrollbar:Pscrollbar;
Aroot:Pnode);
procedure adjust(node:pointer;expand:boolean);virtual;
function getchild(node:pointer;i:sw_integer):pointer;virtual;
function getnumchildren(node:pointer):sw_integer;virtual;
function getroot:pointer;virtual;
function gettext(node:pointer):string;virtual;
function haschildren(node:pointer):boolean;virtual;
function isexpanded(node:pointer):boolean;virtual;
destructor done;virtual;
end;
const ovExpanded = $1;
ovChildren = $2;
ovLast = $4;
Coutlineviewer=Cscroller+#8#8;
function newnode(const Atext:string;Achildren,Anext:Pnode):Pnode;
procedure disposenode(node:Pnode);
{***************************************************************************}
implementation
{***************************************************************************}
type TMyFunc = function(_EBP: Pointer; Cur: Pointer;
Level, Position: sw_integer; Lines: LongInt;
Flags: Word): Boolean;
function newnode(const Atext:string;Achildren,Anext:Pnode):Pnode;
begin
newnode:=new(Pnode);
with newnode^ do
begin
next:=Anext;
text:=newstr(Atext);
childlist:=Achildren;
expanded:=true;
end;
end;
procedure disposenode(node:Pnode);
var next:Pnode;
begin
while node<>nil do
begin
disposenode(node^.childlist);
disposestr(node^.text);
next:=node^.next;
dispose(node);
node:=next;
end;
end;
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
{ Toutlineviewer object methods }
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
constructor Toutlineviewer.init(var bounds:Trect;
AHscrollbar,AVscrollbar:Pscrollbar);
begin
inherited init(bounds,AHscrollbar,AVscrollbar);
foc:=0;
growmode:=gfGrowHiX+gfGrowHiY;
end;
procedure Toutlineviewer.adjust(node:pointer;expand:boolean);
begin
abstract;
end;
function TOutlineViewer.CreateGraph(Level: Integer; Lines: LongInt;
Flags: Word; LevWidth, EndWidth: Integer;
const Chars: String): String;
const
FillerOrBar = 0;
YorL = 2;
StraightOrTee= 4;
Retracted = 6;
var
Last, Children, Expanded: Boolean;
I , J : Byte;
Graph : String;
begin
{ Load registers }
graph:=space(Level*LevWidth+EndWidth+1);
{ Write bar characters }
J := 1;
while (Level > 0) do
begin
Inc(J);
if (Lines and 1) <> 0 then
Graph[J] := Chars[FillerOrBar+2]
else
Graph[J] := Chars[FillerOrBar+1];
for I := 1 to LevWidth - 1 do
Graph[I]:= Chars[FillerOrBar+1];
J := J + LevWidth - 1;
Dec(Level);
Lines := Lines shr 1;
end;
{ Write end characters }
Dec(EndWidth);
if EndWidth > 0 then
begin
Inc(J);
if Flags and ovLast <> 0 then
Graph[J] := Chars[YorL+2]
else
Graph[J] := Chars[YorL+1];
Dec(EndWidth);
if EndWidth > 0 then
begin
Dec(EndWidth);
for I := 1 to EndWidth do
Graph[I]:= Chars[StraightOrTee+1];
J := J + EndWidth;
Inc(J);
if (Flags and ovChildren) <> 0 then
Graph[J] := Chars[StraightOrTee+2]
else
Graph[J] := Chars[StraightOrTee+1];
end;
Inc(J);
if Flags and ovExpanded <> 0 then
Graph[J] := Chars[Retracted+2]
else
Graph[J] := Chars[Retracted+1];
end;
Graph[0] := Char(J);
CreateGraph := Graph;
end;
function Toutlineviewer.do_recurse(action,callerframe:pointer;
stop_if_found:boolean):pointer;
var position:sw_integer;
r:pointer;
function recurse(cur:pointer;level:integer;lines:longint;lastchild:boolean):pointer;
var i,childcount:sw_integer;
child:pointer;
flags:word;
children,expanded,found:boolean;
begin
inc(position);
recurse:=nil;
children:=haschildren(cur);
expanded:=isexpanded(cur);
{Determine flags.}
flags:=0;
if not children or expanded then
inc(flags,ovExpanded);
if children and expanded then
inc(flags,ovChildren);
if lastchild then
inc(flags,ovLast);
{Call the function.}
found:=TMyFunc(action)(callerframe,cur,level,position,lines,flags);
if stop_if_found and found then
recurse:=cur
else if children and expanded then {Recurse children?}
begin
if not lastchild then
lines:=lines or (1 shl level);
{Iterate all childs.}
childcount:=getnumchildren(cur);
for i:=0 to childcount-1 do
begin
child:=getchild(cur,i);
if (child<>nil) and (level<31) then
recurse:=recurse(child,level+1,lines,i=childcount-1);
{Did we find a node?}
if recurse<>nil then
break;
end;
end;
end;
begin
position:=-1;
r:=getroot;
if r<>nil then
do_recurse:=recurse(r,0,0,true)
else
do_recurse:=nil;
end;
procedure Toutlineviewer.draw;
var c_normal,c_normal_x,c_select,c_focus:byte;
maxpos:sw_integer;
b:Tdrawbuffer;
function draw_item(cur:pointer;level,position:sw_integer;
lines:longint;flags:word):boolean;
var c,i:byte;
s,t:string;
begin
draw_item:=position>=delta.y+size.y;
if (position<delta.y) or draw_item then
exit;
maxpos:=position;
s:=getgraph(level,lines,flags);
t:=gettext(cur);
{Determine text colour.}
if (foc=position) and (state and sffocused<>0) then
c:=c_focus
else if isselected(position) then
c:=c_select
else if flags and ovexpanded<>0 then
c:=c_normal_x
else
c:=c_normal;
{Fill drawbuffer with graph and text to draw.}
for i:=0 to size.x-1 do
begin
wordrec(b[i]).hi:=c;
if i+delta.x<length(s) then
wordrec(b[i]).lo:=byte(s[1+i+delta.x])
else if 1+i+delta.x-length(s)<=length(t) then
wordrec(b[i]).lo:=byte(t[1+i+delta.x-length(s)])
else
wordrec(b[i]).lo:=byte(' ');
end;
{Draw!}
writeline(0,position-delta.y,size.x,1,b);
end;
begin
c_normal:=getcolor(4);
c_normal_x:=getcolor(1);
c_focus:=getcolor(2);
c_select:=getcolor(3);
maxpos:=-1;
foreach(@draw_item);
movechar(b,' ',c_normal,size.x);
writeline(0,maxpos+1,size.x,size.y-(maxpos-delta.y),b);
end;
procedure Toutlineviewer.expandall(node:pointer);
var i:sw_integer;
begin
if haschildren(node) then
begin
for i:=0 to getnumchildren(node)-1 do
expandall(getchild(node,i));
adjust(node,true);
end;
end;
function Toutlineviewer.firstthat(test:pointer):pointer;
begin
firstthat:=do_recurse(test,
{ On most systems, locals are accessed relative to base pointer,
but for MIPS cpu, they are accessed relative to stack pointer.
This needs adaptation for so low level routines,
like MethodPointerLocal and related objects unit functions. }
{$ifndef FPC_LOCALS_ARE_STACK_REG_RELATIVE}
get_caller_frame(get_frame,get_pc_addr)
{$else}
get_frame
{$endif}
,true);
end;
procedure Toutlineviewer.focused(i:sw_integer);
begin
foc:=i;
end;
procedure Toutlineviewer.foreach(action:pointer);
begin
do_recurse(action,
{ On most systems, locals are accessed relative to base pointer,
but for MIPS cpu, they are accessed relative to stack pointer.
This needs adaptation for so low level routines,
like MethodPointerLocal and related objects unit functions. }
{$ifndef FPC_LOCALS_ARE_STACK_REG_RELATIVE}
get_caller_frame(get_frame,get_pc_addr)
{$else}
get_frame
{$endif}
,false);
end;
function Toutlineviewer.getchild(node:pointer;i:sw_integer):pointer;
begin
abstract;
end;
function Toutlineviewer.getgraph(level:integer;lines:longint;
flags:word):string;
begin
getgraph:=creategraph(level,lines,flags,3,3,' │├└──+─');
end;
function Toutlineviewer.getnode(i:sw_integer):pointer;
function test_position(node:pointer;level,position:sw_integer;lines:longInt;
flags:word):boolean;
begin
test_position:=position=i;
end;
begin
getnode:=firstthat(@test_position);
end;
function Toutlineviewer.getnumchildren(node:pointer):sw_integer;
begin
abstract;
end;
function Toutlineviewer.getpalette:Ppalette;
const p:string[length(Coutlineviewer)]=Coutlineviewer;
begin
getpalette:=@p;
end;
function Toutlineviewer.getroot:pointer;
begin
abstract;
end;
function Toutlineviewer.gettext(node:pointer):string;
begin
abstract;
end;
procedure Toutlineviewer.handleevent(var event:Tevent);
var mouse:Tpoint;
cur:pointer;
new_focus:sw_integer;
count:byte;
handled,m,mouse_drag:boolean;
graph:string;
function graph_of_focus(var graph:string):pointer;
var _level:sw_integer;
_lines:longInt;
_flags:word;
function find_focused(cur:pointer;level,position:sw_integer;
lines:longint;flags:word):boolean;
begin
find_focused:=position=foc;
if find_focused then
begin
_level:=level;
_lines:=lines;
_flags:=flags;
end;
end;
begin
graph_of_focus:=firstthat(@find_focused);
graph:=getgraph(_level,_lines,_flags);
end;
const skip_mouse_events=3;
begin
inherited handleevent(event);
case event.what of
evKeyboard:
begin
new_focus:=foc;
handled:=true;
case ctrltoarrow(event.keycode) of
kbUp,kbLeft:
dec(new_focus);
kbDown,kbRight:
inc(new_focus);
kbPgDn:
inc(new_focus,size.y-1);
kbPgUp:
dec(new_focus,size.y-1);
kbCtrlPgUp:
new_focus:=0;
kbCtrlPgDn:
new_focus:=limit.y-1;
kbHome:
new_focus:=delta.y;
kbEnd:
new_focus:=delta.y+size.y-1;
kbCtrlEnter,kbEnter:
selected(new_focus);
else
case event.charcode of
'-','+':
begin
adjust(getnode(new_focus),event.charcode='+');
update;
end;
'*':
begin
expandall(getnode(new_focus));
update;
end;
else
handled:=false;
end;
end;
if new_focus<0 then
new_focus:=0;
if new_focus>=limit.y then
new_focus:=limit.y-1;
if foc<>new_focus then
set_focus(new_focus);
if handled then
clearevent(event);
end;
evMouseDown:
begin
count:=1;
mouse_drag:=false;
repeat
makelocal(event.where,mouse);
if mouseinview(event.where) then
new_focus:=delta.y+mouse.y
else
begin
inc(count,byte(event.what=evMouseAuto));
if count and skip_mouse_events=0 then
begin
if mouse.y<0 then
dec(new_focus);
if mouse.y>=size.y then
inc(new_focus);
end;
end;
if new_focus<0 then
new_focus:=0;
if new_focus>=limit.y then
new_focus:=limit.y-1;
if foc<>new_focus then
set_focus(new_focus);
m:=mouseevent(event,evMouseMove+evMouseAuto);
if m then
mouse_drag:=true;
until not m;
if event.double then
selected(foc)
else if not mouse_drag then
begin
cur:=graph_of_focus(graph);
if mouse.x<length(graph) then
begin
adjust(cur,not isexpanded(cur));
update;
end;
end;
end;
end;
end;
function Toutlineviewer.haschildren(node:pointer):boolean;
begin
abstract;
end;
function Toutlineviewer.isexpanded(node:pointer):boolean;
begin
abstract;
end;
function Toutlineviewer.isselected(i:sw_integer):boolean;
begin
isselected:=foc=i;
end;
procedure Toutlineviewer.selected(i:sw_integer);
begin
{Does nothing by default.}
end;
procedure Toutlineviewer.set_focus(Afocus:sw_integer);
begin
assert((Afocus>=0) and (Afocus<limit.y));
focused(Afocus);
if Afocus<delta.y then
scrollto(delta.x,Afocus)
else if Afocus-size.y>=delta.y then
scrollto(delta.x,Afocus-size.y+1);
drawview;
end;
procedure Toutlineviewer.setstate(Astate:word;enable:boolean);
begin
if Astate and sffocused<>0 then
drawview;
inherited setstate(Astate,enable);
end;
procedure Toutlineviewer.update;
var count:sw_integer;
maxwidth:byte;
procedure check_item(cur:pointer;level,position:sw_integer;
lines:longint;flags:word);
var width:word;
begin
inc(count);
width:=length(gettext(cur))+length(getgraph(level,lines,flags));
if width>maxwidth then
maxwidth:=width;
end;
begin
count:=0;
maxwidth:=0;
foreach(@check_item);
setlimit(maxwidth,count);
set_focus(foc);
end;
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
{ Toutline object methods }
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
constructor Toutline.init(var bounds:Trect;
AHscrollbar,AVscrollbar:Pscrollbar;
Aroot:Pnode);
begin
inherited init(bounds,AHscrollbar,AVscrollbar);
root:=Aroot;
update;
end;
procedure Toutline.adjust(node:pointer;expand:boolean);
begin
assert(node<>nil);
Pnode(node)^.expanded:=expand;
end;
function Toutline.getnumchildren(node:pointer):sw_integer;
var p:Pnode;
begin
assert(node<>nil);
p:=Pnode(node)^.childlist;
getnumchildren:=0;
while p<>nil do
begin
inc(getnumchildren);
p:=p^.next;
end;
end;
function Toutline.getchild(node:pointer;i:sw_integer):pointer;
begin
assert(node<>nil);
getchild:=Pnode(node)^.childlist;
while i<>0 do
begin
dec(i);
getchild:=Pnode(getchild)^.next;
end;
end;
function Toutline.getroot:pointer;
begin
getroot:=root;
end;
function Toutline.gettext(node:pointer):string;
begin
assert(node<>nil);
gettext:=Pnode(node)^.text^;
end;
function Toutline.haschildren(node:pointer):boolean;
begin
assert(node<>nil);
haschildren:=Pnode(node)^.childlist<>nil;
end;
function Toutline.isexpanded(node:pointer):boolean;
begin
assert(node<>nil);
isexpanded:=Pnode(node)^.expanded;
end;
destructor Toutline.done;
begin
disposenode(root);
inherited done;
end;
end.