-
Notifications
You must be signed in to change notification settings - Fork 6
/
recording.lisp
2534 lines (2254 loc) · 101 KB
/
recording.lisp
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; -*- Mode: Lisp; Package: CLIM-INTERNALS -*-
;;; (c) copyright 1998,1999,2000,2001 by Michael McDonald ([email protected])
;;; (c) copyright 2000 by
;;; Robert Strandh ([email protected])
;;; (c) copyright 2001 by
;;; Arnaud Rouanet ([email protected])
;;; Lionel Salabartan ([email protected])
;;; (c) copyright 2001, 2002 by Alexey Dejneka ([email protected])
;;; (c) copyright 2003 by Gilbert Baumann <[email protected]>
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library 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. See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307 USA.
;;; TODO:
;;;
;;; - Scrolling does not work correctly. Region is given in "window"
;;; coordinates, without bounding-rectangle-position transformation.
;;; (Is it still valid?)
;;;
;;; - Redo setf*-output-record-position, extent recomputation for
;;; compound records
;;;
;;; - When DRAWING-P is NIL, should stream cursor move?
;;;
;;; - :{X,Y}-OFFSET.
;;;
;;; - (SETF OUTPUT-RECORD-START-CURSOR-POSITION) does not affect the
;;; bounding rectangle. What does it affect?
;;;
;;; - How should (SETF OUTPUT-RECORD-POSITION) affect the bounding
;;; rectangle of the parent? Now its bounding rectangle is accurately
;;; recomputed, but it is very inefficient for table formatting. It
;;; seems that CLIM is supposed to keep a "large enougn" rectangle and
;;; to shrink it to the correct size only when the layout is complete
;;; by calling TREE-RECOMPUTE-EXTENT.
;;;
;;; - Computation of the bounding rectangle of lines/polygons ignores
;;; LINE-STYLE-CAP-SHAPE.
;;;
;;; - Rounding of coordinates.
;;;
;;; - Document carefully the interface of
;;; STANDARD-OUTPUT-RECORDING-STREAM.
;;;
;;; - COORD-SEQ is a sequence, not a list.
;;; Troubles
;;; DC
;;;
;;; Some GFs are defined to have "a default method on CLIM's standard
;;; output record class". What does it mean? What is "CLIM's standard
;;; output record class"? Is it OUTPUT-RECORD or BASIC-OUTPUT-RECORD?
;;; Now they are defined on OUTPUT-RECORD.
(in-package :clim-internals)
;;; 16.2.1. The Basic Output Record Protocol
#+:cmu(declaim (ftype (function (output-record) (values rational rational))
output-record-position))
;; XXX What does #+:CMU mean? FTYPE was excluded from ANSI CL? Other
;; compilers try to check type declarations?
(defgeneric output-record-position (record)
(:documentation
"Returns the x and y position of RECORD. The position is the
position of the upper-left corner of its bounding rectangle. The
position is relative to the stream, where (0,0) is (initially) the
upper-left corner of the stream."))
(defgeneric* (setf output-record-position) (x y record)
(:documentation
"Changes the x and y position of the RECORD to be X and Y, and
updates the bounding rectangle to reflect the new position (and saved
cursor positions, if the output record stores it). If RECORD has any
children, all of the children (and their descendants as well) will be
moved by the same amount as RECORD was moved. The bounding rectangles
of all of RECORD's ancestors will also be updated to be large enough
to contain RECORD."))
#+:cmu(declaim (ftype (function (output-record) (values integer integer))
output-record-start-cursor-position))
(defgeneric output-record-start-cursor-position (record)
(:documentation
"Returns the x and y starting cursor position of RECORD. The
positions are relative to the stream, where (0,0) is (initially) the
upper-left corner of the stream."))
(defgeneric* (setf output-record-start-cursor-position) (x y record))
#+:cmu(declaim (ftype (function (output-record) (values integer integer))
output-record-end-cursor-position))
(defgeneric output-record-end-cursor-position (record)
(:documentation
"Returns the x and y ending cursor position of RECORD. The
positions are relative to the stream, where (0,0) is (initially) the
upper-left corner of the stream."))
(defgeneric* (setf output-record-end-cursor-position) (x y record))
(defgeneric output-record-parent (record)
(:documentation
"Returns the output record that is the parent of RECORD, or NIL if
RECORD has no parent."))
(defgeneric (setf output-record-parent) (parent record)
(:documentation "Non-standard function."))
(defgeneric replay-output-record (record stream
&optional region x-offset y-offset)
(:documentation "Displays the output captured by RECORD on the
STREAM, exactly as it was originally captured. The current user
transformation, line style, text style, ink and clipping region of
STREAM are all ignored. Instead, these are gotten from the output
record.
Only those records that overlap REGION are displayed."))
(defgeneric output-record-hit-detection-rectangle* (record))
(defgeneric output-record-refined-position-test (record x y))
(defgeneric highlight-output-record (record stream state))
(defgeneric displayed-output-record-ink (displayed-output-record))
;;; 16.2.2. Output Record "Database" Protocol
(defgeneric output-record-children (record))
(defgeneric add-output-record (child record))
(defgeneric delete-output-record (child record &optional errorp))
(defgeneric clear-output-record (record))
(defgeneric output-record-count (record))
(defgeneric map-over-output-records-containing-position
(function record x y &optional x-offset y-offset &rest function-args)
(:documentation "Maps over all of the children of RECORD that
contain the point at (X,Y), calling FUNCTION on each one. FUNCTION is
a function of one or more arguments, the first argument being the
record containing the point. FUNCTION is also called with all of
FUNCTION-ARGS as APPLY arguments.
If there are multiple records that contain the point,
MAP-OVER-OUTPUT-RECORDS-CONTAINING-POSITION hits the most recently
inserted record first and the least recently inserted record
last. Otherwise, the order in which the records are traversed is
unspecified."))
(defgeneric map-over-output-records-overlapping-region
(function record region &optional x-offset y-offset &rest function-args)
(:documentation "Maps over all of the children of the RECORD that
overlap the REGION, calling FUNCTION on each one. FUNCTION is a
function of one or more arguments, the first argument being the record
overlapping the region. FUNCTION is also called with all of
FUNCTION-ARGS as APPLY arguments.
If there are multiple records that overlap the region and that overlap
each other, MAP-OVER-OUTPUT-RECORDS-OVERLAPPING-REGION hits the least
recently inserted record first and the most recently inserted record
last. Otherwise, the order in which the records are traversed is
unspecified. "))
;;; From the Franz CLIM user's guide but not in the spec... clearly necessary.
(defgeneric map-over-output-records-1
(continuation record continuation-args))
(defun map-over-output-records
(continuation record &optional x-offset y-offset &rest continuation-args)
(declare (ignore x-offset y-offset))
(map-over-output-records-1 continuation record continuation-args))
;;; 16.2.3. Output Record Change Notification Protocol
(defgeneric recompute-extent-for-new-child (record child))
(defgeneric recompute-extent-for-changed-child
(record child old-min-x old-min-y old-max-x old-max-y))
(defgeneric tree-recompute-extent (record))
;;; 21.3 Incremental Redisplay Protocol. These generic functions need
;;; to be implemented for all the basic displayed-output-records, so they are
;;; defined in this file.
;;;
;;; match-output-records and find-child-output-record, as defined in
;;; the CLIM spec, are pretty silly. How does incremental redisplay know
;;; what keyword arguments to supply to find-child-output-record? Through
;;; a gf specialized on the type of the record it needs to match... why
;;; not define the search function and the predicate on two records then!
;;;
;;; We'll implement match-output-records and find-child-output-record,
;;; but we won't actually use them. Instead, output-record-equal will
;;; match two records, and find-child-record-equal will search for the
;;; equivalent record.
(defgeneric match-output-records (record &rest args))
;;; These gf's use :most-specific-last because one of the least
;;; specific methods will check the bounding boxes of the records, which
;;; should cause an early out most of the time.
(defgeneric match-output-records-1 (record &key)
(:method-combination and :most-specific-last))
(defgeneric output-record-equal (record1 record2)
(:method-combination and :most-specific-last))
(defmethod output-record-equal :around (record1 record2)
(cond ((eq record1 record2)
;; Some unusual record -- like a Goatee screen line -- might
;; exist in two trees at once
t)
((eq (class-of record1) (class-of record2))
(let ((result (call-next-method)))
(if (eq result 'maybe)
nil
result)))
(t nil)))
;;; A fallback method so that something's always applicable.
(defmethod output-record-equal and (record1 record2)
(declare (ignore record1 record2))
'maybe)
;;; The code for match-output-records-1 and output-record-equal
;;; methods are very similar, hence this macro. In order to exploit
;;; the similarities, it's necessary to treat the slots of the second
;;; record like variables, so for convenience the macro will use
;;; slot-value on both records.
(defmacro defrecord-predicate (record-type slots &body body)
"Each element of SLOTS is either a symbol or (:initarg-name slot-name)."
(let* ((slot-names (mapcar #'(lambda (slot-spec)
(if (consp slot-spec)
(cadr slot-spec)
slot-spec))
slots))
(supplied-vars (mapcar #'(lambda (slot)
(gensym (symbol-name
(symbol-concat slot '#:-p))))
slot-names))
(key-args (mapcar #'(lambda (slot-spec supplied)
`(,slot-spec nil ,supplied))
slots supplied-vars))
(key-arg-alist (mapcar #'cons slot-names supplied-vars)))
`(progn
(defmethod output-record-equal and ((record ,record-type)
(record2 ,record-type))
(macrolet ((if-supplied ((var &optional (type t)) &body supplied-body)
(declare (ignore var type))
`(progn ,@supplied-body)))
(with-slots ,slot-names
record2
,@body)))
(defmethod match-output-records-1 and ((record ,record-type)
&key ,@key-args)
(macrolet ((if-supplied ((var &optional (type t)) &body supplied-body)
(let ((supplied-var (cdr (assoc var ',key-arg-alist))))
(unless supplied-var
(error "Unknown slot ~S" var))
`(or (null ,supplied-var)
,@(if (eq type t)
`((progn ,@supplied-body))
`((if (typep ,var ',type)
(progn ,@supplied-body)
(error 'type-error
:datum ,var
:expected-type ',type))))))))
,@body)))
))
;;; Macros
(defmacro with-output-recording-options ((stream
&key (record nil record-supplied-p)
(draw nil draw-supplied-p))
&body body)
(setq stream (stream-designator-symbol stream '*standard-output*))
(with-gensyms (continuation)
`(flet ((,continuation (,stream)
,(declare-ignorable-form* stream)
,@body))
(declare (dynamic-extent #',continuation))
(invoke-with-output-recording-options
,stream #',continuation
,(if record-supplied-p record `(stream-recording-p ,stream))
,(if draw-supplied-p draw `(stream-drawing-p ,stream))))))
;;; Macro masturbation...
(defmacro define-invoke-with (macro-name func-name record-type doc-string)
`(defmacro ,macro-name ((stream
&optional
(record-type '',record-type)
(record (gensym))
&rest initargs)
&body body)
,doc-string
(setq stream (stream-designator-symbol stream '*standard-output*))
(with-gensyms (constructor continuation)
(multiple-value-bind (bindings m-i-args)
(rebind-arguments initargs)
`(let ,bindings
(flet ((,constructor ()
(make-instance ,record-type ,@m-i-args))
(,continuation (,stream ,record)
,(declare-ignorable-form* stream record)
,@body))
(declare (dynamic-extent #',constructor #',continuation))
(,',func-name ,stream #',continuation ,record-type #',constructor
,@m-i-args)))))))
(define-invoke-with with-new-output-record invoke-with-new-output-record
standard-sequence-output-record
"Creates a new output record of type RECORD-TYPE and then captures
the output of BODY into the new output record, and inserts the new
record into the current \"open\" output record assotiated with STREAM.
If RECORD is supplied, it is the name of a variable that will be
lexically bound to the new output record inside the body. INITARGS are
CLOS initargs that are passed to MAKE-INSTANCE when the new output
record is created.
It returns the created output record.
The STREAM argument is a symbol that is bound to an output
recording stream. If it is T, *STANDARD-OUTPUT* is used.")
(define-invoke-with with-output-to-output-record
invoke-with-output-to-output-record
standard-sequence-output-record
"Creates a new output record of type RECORD-TYPE and then captures
the output of BODY into the new output record. The cursor position of
STREAM is initially bound to (0,0)
If RECORD is supplied, it is the name of a variable that will be
lexically bound to the new output record inside the body. INITARGS are
CLOS initargs that are passed to MAKE-INSTANCE when the new output
record is created.
It returns the created output record.
The STREAM argument is a symbol that is bound to an output
recording stream. If it is T, *STANDARD-OUTPUT* is used.")
;;;; Implementation
(defclass basic-output-record (standard-bounding-rectangle output-record)
((parent :initarg :parent ; XXX
:initform nil
:accessor output-record-parent)) ; XXX
(:documentation "Implementation class for the Basic Output Record Protocol."))
(defmethod initialize-instance :after ((record basic-output-record)
&key (x-position 0.0d0)
(y-position 0.0d0))
(setf (rectangle-edges* record)
(values x-position y-position x-position y-position)))
;;; XXX I'd really like to get rid of the x and y slots. They are surely
;;; redundant with the bounding rectangle coordinates.
(defclass compound-output-record (basic-output-record)
((x :initarg :x-position
:initform 0.0d0
:documentation "X-position of the empty record.")
(y :initarg :y-position
:initform 0.0d0
:documentation "Y-position of the empty record.")
(in-moving-p :initform nil
:documentation "Is set while changing the position."))
(:documentation "Implementation class for output records with children."))
;;; 16.2.1. The Basic Output Record Protocol
(defmethod output-record-position ((record basic-output-record))
(bounding-rectangle-position record))
(defmethod* (setf output-record-position) (nx ny (record basic-output-record))
(with-standard-rectangle (x1 y1 x2 y2)
record
(let ((dx (- nx x1))
(dy (- ny y1)))
(setf (rectangle-edges* record)
(values nx ny (+ x2 dx) (+ y2 dy)))))
(values nx ny))
(defmethod* (setf output-record-position) :around
(nx ny (record basic-output-record))
(with-bounding-rectangle* (min-x min-y max-x max-y) record
(call-next-method)
(let ((parent (output-record-parent record)))
(when (and parent (not (and (typep parent 'compound-output-record)
(slot-value parent 'in-moving-p)))) ; XXX
(recompute-extent-for-changed-child parent record
min-x min-y max-x max-y)))
(values nx ny)))
(defmethod* (setf output-record-position)
:before (nx ny (record compound-output-record))
(with-standard-rectangle* (:x1 x1 :y1 y1)
record
(letf (((slot-value record 'in-moving-p) t))
(let ((dx (- nx x1))
(dy (- ny y1)))
(map-over-output-records
(lambda (child)
(multiple-value-bind (x y) (output-record-position child)
(setf (output-record-position child)
(values (+ x dx) (+ y dy)))))
record)))))
(defmethod output-record-start-cursor-position ((record basic-output-record))
(values nil nil))
(defmethod* (setf output-record-start-cursor-position)
(x y (record basic-output-record))
(values x y))
(defmethod output-record-end-cursor-position ((record basic-output-record))
(values nil nil))
(defmethod* (setf output-record-end-cursor-position)
(x y (record basic-output-record))
(values x y))
#+cmu
(progn
;; Sometimes CMU's PCL fails with forward reference classes, so this
;; is a kludge to keep it happy.
;;
;; This was reported as a bug to cmucl-imp [<[email protected]>]
;;
;; In short it exposes itself when you compile and load into a
;; _virgin_ lisp the following:
;;
;; (defclass foo (bar) ())
;; (defun barz () (make-instance 'foo))
;; (defclass bar () ())
;;
;; --GB 2003-03-18
;;
(defclass gs-ink-mixin () ())
(defclass gs-clip-mixin () ())
(defclass gs-line-style-mixin () ())
(defclass gs-text-style-mixin () ()))
;;; Humph. It'd be nice to tie this to the actual definition of a
;;; medium. -- moore
(defclass complete-medium-state
(gs-ink-mixin gs-clip-mixin gs-line-style-mixin gs-text-style-mixin)
())
(defun replay (record stream &optional (region (or (pane-viewport-region stream)
(sheet-region stream))))
(if (typep stream 'encapsulating-stream)
(replay record (encapsulating-stream-stream stream) region)
(progn
(stream-close-text-output-record stream)
(when (stream-drawing-p stream)
(with-cursor-off stream ;;FIXME?
(letf (((stream-cursor-position stream) (values 0 0))
((stream-recording-p stream) nil)
;; Is there a better value to bind to baseline?
((slot-value stream 'baseline) (slot-value stream 'baseline)))
(with-sheet-medium (medium stream)
(let ((transformation (medium-transformation medium)))
(unwind-protect
(progn
(setf (medium-transformation medium)
+identity-transformation+)
(replay-output-record record stream region))
(setf (medium-transformation medium) transformation))))))))))
(defmethod replay-output-record ((record compound-output-record) stream
&optional region (x-offset 0) (y-offset 0))
(when (null region)
(setq region (or (pane-viewport-region stream) +everywhere+)))
(with-drawing-options (stream :clipping-region region)
(map-over-output-records-overlapping-region
#'replay-output-record record region x-offset y-offset
stream region x-offset y-offset)))
(defmethod output-record-hit-detection-rectangle* ((record output-record))
;; XXX DC
(bounding-rectangle* record))
(defmethod output-record-refined-position-test ((record basic-output-record)
x y)
(declare (ignore x y))
t)
(defun highlight-output-record-rectangle (record stream state)
(with-identity-transformation (stream)
(multiple-value-bind (x1 y1 x2 y2)
(output-record-hit-detection-rectangle* record)
(ecase state
(:highlight
(draw-rectangle* (sheet-medium stream) x1 y1 (1- x2) (1- y2)
:filled nil :ink +foreground-ink+)) ; XXX +FLIPPING-INK+?
(:unhighlight
;; FIXME: repaint the hit detection rectangle. It could be bigger than
;; the bounding rectangle.
(repaint-sheet stream record)
;; Using queue-repaint should be faster in apps (such as clouseau) that
;; highlight/unhighlight many bounding rectangles at once. The event
;; code should merge these into a single larger repaint. Unfortunately,
;; since an enqueued repaint does not occur immediately, and highlight
;; rectangles are not recorded, newer highlighting gets wiped out
;; shortly after being drawn. So, we aren't ready for this yet.
;; ..Actually, it isn't necessarily faster. Depends on the app.
#+NIL
(queue-repaint stream (make-instance 'window-repaint-event
:sheet stream
:region (transform-region
(sheet-native-transformation stream)
record))))))))
;;; XXX Should this only be defined on recording streams?
(defmethod highlight-output-record ((record output-record) stream state)
;; XXX DC
;; XXX Disable recording?
(highlight-output-record-rectangle record stream state))
;;; 16.2.2. The Output Record "Database" Protocol
;; These two aren't in the spec, but are needed to make indirect adding/deleting
;; of GADGET-OUTPUT-RECORDs work:
(defgeneric note-output-record-lost-sheet (record sheet))
(defgeneric note-output-record-got-sheet (record sheet))
(defmethod note-output-record-lost-sheet ((record output-record) sheet)
(declare (ignore record sheet))
(values))
(defmethod note-output-record-lost-sheet :after ((record compound-output-record) sheet)
(map-over-output-records #'note-output-record-lost-sheet record 0 0 sheet))
(defmethod note-output-record-got-sheet ((record output-record) sheet)
(declare (ignore record sheet))
(values))
(defmethod note-output-record-got-sheet :after ((record compound-output-record) sheet)
(map-over-output-records #'note-output-record-got-sheet record 0 0 sheet))
(defun find-output-record-sheet (record)
"Walks up the parents of RECORD, searching for an output history from which
the associated sheet can be determined."
(typecase record
(stream-output-history-mixin (output-history-stream record))
(basic-output-record (find-output-record-sheet (output-record-parent record)))))
(defmethod output-record-children ((record basic-output-record))
nil)
(defmethod add-output-record (child (record basic-output-record))
(declare (ignore child))
(error "Cannot add a child to ~S." record))
(defmethod add-output-record :before (child (record compound-output-record))
(let ((parent (output-record-parent child)))
(cond (parent
(restart-case
(error "~S already has a parent ~S." child parent)
(delete ()
:report "Delete from the old parent."
(delete-output-record child parent))))
((eq record child)
(error "~S is being added to itself" record))
((eq (output-record-parent record) child)
(error "child ~S is being added to its own child ~S"
child record)))))
(defmethod add-output-record :after (child (record compound-output-record))
(recompute-extent-for-new-child record child)
(when (eq record (output-record-parent child))
(let ((sheet (find-output-record-sheet record)))
(when sheet (note-output-record-got-sheet child sheet)))))
(defmethod delete-output-record :before (child (record basic-output-record)
&optional (errorp t))
(declare (ignore errorp))
(let ((sheet (find-output-record-sheet record)))
(when sheet
(note-output-record-lost-sheet child sheet))))
(defmethod delete-output-record (child (record basic-output-record)
&optional (errorp t))
(declare (ignore child))
(when errorp (error "Cannot delete a child from ~S." record)))
(defmethod delete-output-record :after (child (record compound-output-record)
&optional (errorp t))
(declare (ignore errorp))
(with-bounding-rectangle* (x1 y1 x2 y2) child
(recompute-extent-for-changed-child record child x1 y1 x2 y2)))
(defmethod clear-output-record ((record basic-output-record))
(error "Cannot clear ~S." record))
(defmethod clear-output-record :before ((record compound-output-record))
(let ((sheet (find-output-record-sheet record)))
(when sheet
(map-over-output-records #'note-output-record-lost-sheet record 0 0 sheet))))
(defmethod clear-output-record :around ((record compound-output-record))
(multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* record)
(call-next-method)
(assert (null-bounding-rectangle-p record))
(when (output-record-parent record)
(recompute-extent-for-changed-child
(output-record-parent record) record x1 y1 x2 y2))))
(defmethod clear-output-record :after ((record compound-output-record))
;; XXX banish x and y
(with-slots (x y) record
(setf (rectangle-edges* record) (values x y x y))))
(defmethod output-record-count ((record displayed-output-record))
0)
(defmethod map-over-output-records-1
(function (record displayed-output-record) function-args)
(declare (ignore function function-args))
nil)
;;; This needs to work in "most recently added last" order. Is this
;;; implementation right? -- APD, 2002-06-13
#+nil
(defmethod map-over-output-records
(function (record compound-output-record)
&optional (x-offset 0) (y-offset 0)
&rest function-args)
(declare (ignore x-offset y-offset))
(map nil (lambda (child) (apply function child function-args))
(output-record-children record)))
(defmethod map-over-output-records-containing-position
(function (record displayed-output-record) x y
&optional (x-offset 0) (y-offset 0)
&rest function-args)
(declare (ignore function x y x-offset y-offset function-args))
nil)
;;; This needs to work in "most recently added first" order. Is this
;;; implementation right? -- APD, 2002-06-13
#+nil
(defmethod map-over-output-records-containing-position
(function (record compound-output-record) x y
&optional (x-offset 0) (y-offset 0)
&rest function-args)
(declare (ignore x-offset y-offset))
(map nil
(lambda (child)
(when (and (multiple-value-bind (min-x min-y max-x max-y)
(output-record-hit-detection-rectangle* child)
(and (<= min-x x max-x) (<= min-y y max-y)))
(output-record-refined-position-test child x y))
(apply function child function-args)))
(output-record-children record)))
(defmethod map-over-output-records-overlapping-region
(function (record displayed-output-record) region
&optional (x-offset 0) (y-offset 0)
&rest function-args)
(declare (ignore function region x-offset y-offset function-args))
nil)
;;; This needs to work in "most recently added last" order. Is this
;;; implementation right? -- APD, 2002-06-13
#+nil
(defmethod map-over-output-records-overlapping-region
(function (record compound-output-record) region
&optional (x-offset 0) (y-offset 0)
&rest function-args)
(declare (ignore x-offset y-offset))
(map nil
(lambda (child) (when (region-intersects-region-p region child)
(apply function child function-args)))
(output-record-children record)))
;;; XXX Dunno about this definition... -- moore
;;; Your apprehension is justified, but we lack a better means by which
;;; to distinguish "empty" compound records (roots of trees of compound
;;; records, containing no non-compound records). Such subtrees should
;;; not affect bounding rectangles. -- Hefner
(defun null-bounding-rectangle-p (bbox)
(with-bounding-rectangle* (x1 y1 x2 y2) bbox
(and (= x1 x2)
(= y1 y2))))
;;; 16.2.3. Output Record Change Notification Protocol
(defmethod recompute-extent-for-new-child
((record compound-output-record) child)
(unless (null-bounding-rectangle-p child)
(with-bounding-rectangle* (old-x1 old-y1 old-x2 old-y2) record
(cond
((null-bounding-rectangle-p record)
(setf (rectangle-edges* record) (bounding-rectangle* child)))
((not (null-bounding-rectangle-p child))
(assert (not (null-bounding-rectangle-p record))) ; important.
(with-bounding-rectangle* (x1-child y1-child x2-child y2-child)
child
(setf (rectangle-edges* record)
(values (min old-x1 x1-child) (min old-y1 y1-child)
(max old-x2 x2-child) (max old-y2 y2-child))))))
(let ((parent (output-record-parent record)))
(when parent
(recompute-extent-for-changed-child
parent record old-x1 old-y1 old-x2 old-y2)))))
record)
(defmethod %tree-recompute-extent* ((record compound-output-record))
;; Internal helper function
(let ((new-x1 0)
(new-y1 0)
(new-x2 0)
(new-y2 0)
(first-time t))
(map-over-output-records
(lambda (child)
(unless (null-bounding-rectangle-p child)
(if first-time
(progn
(multiple-value-setq (new-x1 new-y1 new-x2 new-y2)
(bounding-rectangle* child))
(setq first-time nil))
(with-bounding-rectangle* (cx1 cy1 cx2 cy2) child
(minf new-x1 cx1)
(minf new-y1 cy1)
(maxf new-x2 cx2)
(maxf new-y2 cy2)))))
record)
(if first-time
;; XXX banish x y
(with-slots (x y) record
(values x y x y))
(values new-x1 new-y1 new-x2 new-y2))))
(defgeneric tree-recompute-extent-aux (record))
(defmethod tree-recompute-extent-aux (record)
(bounding-rectangle* record))
(defmethod tree-recompute-extent-aux ((record compound-output-record))
(let ((new-x1 0)
(new-y1 0)
(new-x2 0)
(new-y2 0)
(first-time t))
(map-over-output-records
(lambda (child)
(if first-time
(progn
(multiple-value-setq (new-x1 new-y1 new-x2 new-y2)
(tree-recompute-extent-aux child))
(setq first-time nil))
(multiple-value-bind (cx1 cy1 cx2 cy2)
(tree-recompute-extent-aux child)
(minf new-x1 cx1)
(minf new-y1 cy1)
(maxf new-x2 cx2)
(maxf new-y2 cy2))))
record)
(with-slots (x y)
record
(if first-time ;No children
(bounding-rectangle* record)
(progn
;; XXX banish x,y
(setf x new-x1 y new-y1)
(setf (rectangle-edges* record)
(values new-x1 new-y1 new-x2 new-y2)))))))
(defmethod recompute-extent-for-changed-child
((record compound-output-record) changed-child
old-min-x old-min-y old-max-x old-max-y)
(with-bounding-rectangle* (ox1 oy1 ox2 oy2) record
(with-bounding-rectangle* (cx1 cy1 cx2 cy2) changed-child
;; If record is currently empty, use the child's bbox directly. Else..
;; Does the new rectangle of the child contain the original rectangle?
;; If so, we can use min/max to grow record's current rectangle.
;; If not, the child has shrunk, and we need to fully recompute.
(multiple-value-bind (nx1 ny1 nx2 ny2)
(cond
;; The child has been deleted; who knows what the
;; new bounding box might be.
;; This case shouldn't be really necessary.
((not (output-record-parent changed-child))
(%tree-recompute-extent* record))
;; Only one child of record, and we already have the bounds.
((eql (output-record-count record) 1)
;; See output-record-children for why this assert breaks:
;; (assert (eq changed-child (elt (output-record-children record) 0)))
(values cx1 cy1 cx2 cy2))
;; If our record occupied no space (had no children, or had only
;; children similarly occupying no space, hackishly determined by
;; null-bounding-rectangle-p), recompute the extent now, otherwise
;; the next COND clause would, as an optimization, attempt to extend
;; our current bounding rectangle, which is invalid.
((null-bounding-rectangle-p record)
(%tree-recompute-extent* record))
;; In the following cases, we can grow the new bounding rectangle
;; from its previous state:
((or
;; If the child was originally empty, it could not have affected
;; previous computation of our bounding rectangle.
;; This is hackish for reasons similar to the above.
(and (= old-min-x old-max-x) (= old-min-y old-max-y))
;; For each edge of the original child bounds, if it was within
;; its respective edge of the old parent bounding rectangle,
;; or if it has not changed:
(and (or (> old-min-x ox1) (= old-min-x cx1))
(or (> old-min-y oy1) (= old-min-y cy1))
(or (< old-max-x ox2) (= old-max-x cx2))
(or (< old-max-y oy2) (= old-max-y cy2)))
;; New child bounds contain old child bounds, so use min/max
;; to extend the already-calculated rectangle.
(and (<= cx1 old-min-x) (<= cy1 old-min-y)
(>= cx2 old-max-x) (>= cy2 old-max-y)))
(values (min cx1 ox1) (min cy1 oy1)
(max cx2 ox2) (max cy2 oy2)))
;; No shortcuts - we must compute a new bounding box from those of
;; all our children. We want to avoid this - in worst cases, such as
;; a toplevel output history, large graph, or table, there may exist
;; thousands of children. Without the above optimizations,
;; construction becomes O(N^2) due to bounding rectangle calculation.
(t (%tree-recompute-extent* record)))
;; XXX banish x, y
(with-slots (x y)
record
(setf x nx1 y ny1)
(setf (rectangle-edges* record) (values nx1 ny1 nx2 ny2))
(let ((parent (output-record-parent record)))
(unless (or (null parent)
(and (= nx1 ox1) (= ny1 oy1)
(= nx2 ox2) (= nx2 oy2)))
(recompute-extent-for-changed-child parent record
ox1 oy1 ox2 oy2)))))))
record)
(defmethod tree-recompute-extent ((record compound-output-record))
(tree-recompute-extent-aux record)
record)
(defmethod tree-recompute-extent :around ((record compound-output-record))
(with-bounding-rectangle* (old-x1 old-y1 old-x2 old-y2)
record
(call-next-method)
(with-bounding-rectangle* (x1 y1 x2 y2)
record
(let ((parent (output-record-parent record)))
(when (and parent
(not (and (= old-x1 x1)
(= old-y1 y1)
(= old-x2 x2)
(= old-y2 y2))))
(recompute-extent-for-changed-child parent record
old-x1 old-y1
old-x2 old-y2)))))
record)
;;; 16.3.1. Standard output record classes
(defclass standard-sequence-output-record (compound-output-record)
((children :initform (make-array 8 :adjustable t :fill-pointer 0)
:reader output-record-children)))
(defmethod add-output-record (child (record standard-sequence-output-record))
(vector-push-extend child (output-record-children record))
(setf (output-record-parent child) record))
(defmethod delete-output-record (child (record standard-sequence-output-record)
&optional (errorp t))
(with-slots (children) record
(let ((pos (position child children :test #'eq)))
(if (null pos)
(when errorp
(error "~S is not a child of ~S" child record))
(progn
(setq children (replace children children
:start1 pos
:start2 (1+ pos)))
(decf (fill-pointer children))
(setf (output-record-parent child) nil))))))
(defmethod clear-output-record ((record standard-sequence-output-record))
(let ((children (output-record-children record)))
(map 'nil (lambda (child) (setf (output-record-parent child) nil))
children)
(fill children nil)
(setf (fill-pointer children) 0)))
(defmethod output-record-count ((record standard-sequence-output-record))
(length (output-record-children record)))
(defmethod map-over-output-records-1
(function (record standard-sequence-output-record) function-args)
"Applies FUNCTION to all children in the order they were added."
(if function-args
(loop with children = (output-record-children record)
for child across children
do (apply function child function-args))
(loop with children = (output-record-children record)
for child across children
do (funcall function child))))
(defmethod map-over-output-records-containing-position
(function (record standard-sequence-output-record) x y
&optional (x-offset 0) (y-offset 0)
&rest function-args)
"Applies FUNCTION to children, containing (X,Y), in the reversed
order they were added."
(declare (ignore x-offset y-offset))
(loop with children = (output-record-children record)
for i from (1- (length children)) downto 0
for child = (aref children i)
when (and (multiple-value-bind (min-x min-y max-x max-y)
(output-record-hit-detection-rectangle* child)
(and (<= min-x x max-x) (<= min-y y max-y)))
(output-record-refined-position-test child x y))
do (apply function child function-args)))
(defmethod map-over-output-records-overlapping-region
(function (record standard-sequence-output-record) region
&optional (x-offset 0) (y-offset 0)
&rest function-args)
"Applies FUNCTION to children, overlapping REGION, in the order they
were added."
(declare (ignore x-offset y-offset))
(loop with children = (output-record-children record)
for child across children
when (region-intersects-region-p region child)
do (apply function child function-args)))
;;; tree output recording
(defclass tree-output-record-entry ()
((record :initarg :record :reader tree-output-record-entry-record)
(cached-rectangle :initform nil :accessor tree-output-record-entry-cached-rectangle)
(inserted-nr :initarg :inserted-nr :accessor tree-output-record-entry-inserted-nr)))
(defun make-tree-output-record-entry (record inserted-nr)
(make-instance 'tree-output-record-entry :record record :inserted-nr inserted-nr))
(defun %record-to-spatial-tree-rectangle (r)
(rectangles:make-rectangle
:lows `(,(bounding-rectangle-min-x r)
,(bounding-rectangle-min-y r))
:highs `(,(bounding-rectangle-max-x r)
,(bounding-rectangle-max-y r))))
(defun %output-record-entry-to-spatial-tree-rectangle (r)
(when (null (tree-output-record-entry-cached-rectangle r))
(let* ((record (tree-output-record-entry-record r)))
(setf (tree-output-record-entry-cached-rectangle r) (%record-to-spatial-tree-rectangle record))))
(tree-output-record-entry-cached-rectangle r))
(defun %make-tree-output-record-tree ()
(spatial-trees:make-spatial-tree :r
:rectfun #'%output-record-entry-to-spatial-tree-rectangle))
(defclass standard-tree-output-record (compound-output-record)
((children :initform (%make-tree-output-record-tree)
:accessor %tree-record-children)
(children-hash :initform (make-hash-table :test #'eql) :reader %tree-record-children-cache)
(child-count :initform 0)
(last-insertion-nr :initform 0 :accessor last-insertion-nr)))
(defun %entry-in-children-cache (record entry)
(gethash entry (%tree-record-children-cache record)))
(defun (setf %entry-in-children-cache) (new-val record entry)
(setf (gethash entry (%tree-record-children-cache record)) new-val))
(defun %remove-entry-from-children-cache (record entry)
(remhash entry (%tree-record-children-cache record)))
(defmethod output-record-children ((record standard-tree-output-record))
(with-bounding-rectangle* (min-x min-y max-x max-y) record
(map 'list
#'tree-output-record-entry-record
(spatial-trees:search
;; Originally, (%record-to-spatial-tree-rectangle record).
;; The form below intends to fix output-record-children not
;; reporting empty children, which may lie outside the reported