ezd /src/view.sc

Language Lisp Lines 806
MD5 Hash b64a080e0d453e6b037334fe48bdba73 Estimated Cost $15,580 (why?)
Repository https://bitbucket.org/bunny351/ezd.git View Raw File View Project SPDX
  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
;;; ezd - easy drawing for X11 displays.
;;;
;;; A VIEW object maps a DRAWING onto a WINDOW object.

;*           Copyright 1990-1993 Digital Equipment Corporation
;*                         All Rights Reserved
;*
;* Permission to use, copy, and modify this software and its documentation is
;* hereby granted only under the following terms and conditions.  Both the
;* above copyright notice and this permission notice must appear in all copies
;* of the software, derivative works or modified versions, and any portions
;* thereof, and both notices must appear in supporting documentation.
;*
;* Users of this software agree to the terms and conditions set forth herein,
;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
;* right and license under any changes, enhancements or extensions made to the
;* core functions of the software, including but not limited to those affording
;* compatibility with other hardware or software environments, but excluding
;* applications which incorporate this software.  Users further agree to use
;* their best efforts to return to Digital any such changes, enhancements or
;* extensions that they make and inform Digital of noteworthy uses of this
;* software.  Correspondence should be provided to Digital at:
;* 
;*                       Director of Licensing
;*                       Western Research Laboratory
;*                       Digital Equipment Corporation
;*                       250 University Avenue
;*                       Palo Alto, California  94301  
;* 
;* This software may be distributed (but not offered for sale or transferred
;* for compensation) to third parties, provided such third parties agree to
;* abide by the terms and conditions of this notice.  
;* 
;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
;* MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
;* SOFTWARE.

(module view)

(include "struct.sch")
(include "display.sch")
(include "window.sch")
(include "drawing.sch")
(include "graphic.sch")
(include "commands.sch")
(include "events.sch")
(include "xternal.sch")

;;; Creates a VIEW composed of the following fields:
;;;
;;;	DRAWING-NAME	drawing name associated with the view.
;;;	WINDOW-NAME	window name associated with the view.
;;;	CLIP-MINX	X window bounding box for the view or #f.
;;;	CLIP-MINY
;;;	CLIP-MAXX
;;;	CLIP-MAXY
;;;	DRAWING		drawing associated with the view.
;;;	WINDOW		window associated with the view.
;;;	USER->X		convert user x coordinate to X coordinate system.
;;;	USER->Y		convert user y coordinate to X coordinate system.
;;;	USER->LW	convert user line width coordinate to X pixels.
;;;	X->USER		convert X x coordinate to user coordinate system.
;;;	Y->USER		convert X y coordinate to user coordinate system.
;;;	USER->WIDTH	convert user x units to X pixels.
;;;	USER->HEIGHT	convert user y units to X pixels.
;;;     WIDTH->USER	convert X pixels to user x units.
;;;	HEIGHT->USER	convert X pixels to user y units.
;;;	ORIGINX		X coordinates for drawing's origin.
;;;	ORIGINY
;;;	SCALEX		Scale factors to convert drawing units to X.
;;;	SCALEY
;;;	SCALELW
;;;	NEW		indicates that this is a newly created view.
;;;	BB-HEAD		head of list of BBGRAPHICs in the view.
;;;	BB-TAIL		tail of list of BBGRAPHICS in the view.
;;;	STIPPLE-X	X coordinates of stipple offset
;;;	STIPPLE-Y
;;;	NEW-TRANSFORM	argument list for new transformation

(define-structure VIEW
    drawing-name
    window-name
    (clip-minx #f)
    (clip-miny #f)
    (clip-maxx #f)
    (clip-maxy #f)
    (drawing (define (view-drawing self)
		     (name->drawing (view-drawing-name self)))
	     #f)
    (window (define (view-window self)
		    (name->window (view-window-name self)))
	    #f)
    (user->x (lambda (x) (inexact->exact x)))
    (user->y (lambda (x) (inexact->exact x)))
    (user->lw (lambda (x) (if x (inexact->exact x) 0)))
    (x->user (lambda (x) x))
    (y->user (lambda (x) x))
    (user->width (lambda (x) (inexact->exact x)))
    (user->height (lambda (x) (inexact->exact x)))
    (width->user (lambda (x) x))
    (height->user (lambda (x) x))
    (originx 0)
    (originy 0)
    (scalex 1)
    (scaley 1)
    (scalelw 1)
    (new #t)
    (bb-head '())
    (bb-tail '())
    (stipple-x 0)
    (stipple-y 0)
    (new-transform #f))

(define-in-line-structure-access VIEW
    drawing-name
    window-name
    clip-minx
    clip-miny
    clip-maxx
    clip-maxy
    #f
    #f
    user->x
    user->y
    user->lw
    x->user
    y->user
    user->width
    user->height
    width->user
    height->user
    originx
    originy
    scalex
    scaley
    scalelw
    new
    bb-head
    bb-tail
    stipple-x
    stipple-y
    new-transform)

;;; All procedures that draw into objects assume a "current view".  The
;;; information related to the current view is stored in the following
;;; global variables.

(define *CURRENT-VIEW* #f)

(define *WINDOW* #f)		;;; From the WINDOW object.
(define *WIDTH* #f)
(define *HEIGHT* #f)
(define *NAME* #f)
(define *FOREGROUND-NAME* #f)
(define *BACKGROUND-NAME* #f)
(define *FOREGROUND* #f)
(define *BACKGROUND* #f)
(define *XWINDOW* #f)

(define USER->X #f)		;;; From the VIEW object.
(define USER->Y #f)
(define USER->LW #f)
(define X->USER #f)
(define Y->USER #f)
(define USER->WIDTH #f)
(define USER->HEIGHT #f)
(define WIDTH->USER #f)
(define HEIGHT->USER #f)
(define STIPPLE-X #f)
(define STIPPLE-Y #f)

(define *CLIP-BBL* #f)		;;; From the call to SET-VIEW.

;;; The current view is swapped by the following function.  One can force
;;; the cached values to be returned to their structure by supplying #f as the
;;; argument.  The previous value of *CURRENT-VIEW* is returned as the
;;; value of the function.

(define (SET-VIEW cview clip-bbl)
    (unless (eq? cview *current-view*)
	    (when *current-view*
		  (view-user->x! *current-view* user->x)
		  (view-user->y! *current-view* user->y)
		  (view-user->lw! *current-view* user->lw)
		  (view-x->user! *current-view* x->user)
		  (view-y->user! *current-view* y->user)
		  (view-user->width! *current-view* user->width)
		  (view-user->height! *current-view* user->height)
		  (view-width->user! *current-view* width->user)
		  (view-height->user! *current-view* height->user)
		  (view-stipple-x! *current-view* stipple-x)
		  (view-stipple-y! *current-view* stipple-y))
	    (when cview
		  (set! user->x (view-user->x cview))
		  (set! user->y (view-user->y cview))
		  (set! user->lw (view-user->lw cview))
		  (set! x->user (view-x->user cview))
		  (set! y->user (view-y->user cview))
		  (set! user->width (view-user->width cview))
		  (set! user->height (view-user->height cview))
		  (set! width->user (view-width->user cview))
		  (set! height->user (view-height->user cview))
		  (set! stipple-x (view-stipple-x cview))
		  (set! stipple-y (view-stipple-y cview))
		  (set! *current-drawing* (view-drawing cview))
		  (set! *window* (view-window cview))
		  (set! *width* (window-width *window*))
		  (set! *height* (window-height *window*))
		  (set! *name* (window-name *window*))
		  (set! *foreground-name* (window-foreground-name *window*))
		  (set! *background-name* (window-background-name *window*))
		  (set! *foreground* (window-foreground *window*))
		  (set! *background* (window-background *window*))
		  (set! *xwindow* (window-xwindow *window*))))
    (let ((return *current-view*))
	 (set! *current-view* cview)
	 (if cview (set! *clip-bbl*
			 (let ((clipped (clip-bbl-to-view cview clip-bbl)))
			      (if (and (null? clipped) (view-clip-minx cview))
				  (list (list (view-clip-minx cview)
					      (view-clip-miny cview)
					      (view-clip-maxx cview)
					      (view-clip-maxy cview)))
				  clipped))))
	 return))

;;; A bounding box list is clipped to a view by the following procedure.  A
;;; newly constructed list of bounding boxes is returned.

(define (CLIP-BBL-TO-VIEW view bbl)
    (if (view-clip-minx view)
	(let ((clip-minx (view-clip-minx view))
	      (clip-miny (view-clip-miny view))
	      (clip-maxx (view-clip-maxx view))
	      (clip-maxy (view-clip-maxy view)))
	     (let loop ((bbl bbl))
		  (if (pair? bbl)
		      (let* ((bb (car bbl))
			     (minx (car bb))
			     (miny (cadr bb))
			     (maxx (caddr bb))
			     (maxy (cadddr bb)))
			    (if (or (<= maxx clip-minx)
				    (<= maxy clip-miny)
				    (>= minx clip-maxx)
				    (>= miny clip-maxy))
				(loop (cdr bbl))
				(cons (list (max minx clip-minx)
					    (max miny clip-miny)
					    (min maxx clip-maxx)
					    (min maxy clip-maxy))
				      (loop (cdr bbl)))))
		      '())))
	bbl))

;;; A bounding box is clipped to a view by the following procedure.  Either a
;;; bounding box of #f is returned.

(define (CLIP-BB-TO-VIEW view minx miny maxx maxy)
    (if (view-clip-minx view)
	(let ((clip-minx (view-clip-minx view))
	      (clip-miny (view-clip-miny view))
	      (clip-maxx (view-clip-maxx view))
	      (clip-maxy (view-clip-maxy view)))
	     (if (or (<= maxx clip-minx)
		     (<= maxy clip-miny)
		     (>= minx clip-maxx)
		     (>= miny clip-maxy))
		 #f
		 (list (max minx clip-minx)
		       (max miny clip-miny)
		       (min maxx clip-maxx)
		       (min maxy clip-maxy))))
	(list minx miny maxx maxy)))

;;; The following function converts a list of views into a list of lists of
;;; views, where each list is a list of intersecting views.  Each sublist is
;;; ordered as was the original list of views.

(define (PARTITION-VIEWS views)
    
    (define (INTERSECT? view views)
	    (if (pair? views)
		(let ((v2 (car views)))
		     (if (and (view-clip-minx view)
			      (view-clip-minx v2)
			      (or  (<= (view-clip-maxx view)
				       (view-clip-minx v2))
				   (<= (view-clip-maxy view)
				       (view-clip-miny v2))
				   (>= (view-clip-minx view)
				       (view-clip-maxx v2))
				   (>= (view-clip-miny view)
				       (view-clip-maxy v2))))
			 (intersect? view (cdr views))
			 #t))
		#f))
    
    (if (pair? views)
	(let loop ((intersect (list (car views)))
		   (disjoint '())
		   (views (cdr views)))
	     (if (pair? views)
		 (let ((view (car views)))
		      (if (intersect? view intersect)
			  (loop (append intersect (list view)) disjoint
				(cdr views))
			  (loop intersect (append disjoint (list view))
				(cdr views))))
		 (cons intersect (partition-views disjoint))))
	'()))

;;; Graphics contexts are managed by the following procedure.  Given the
;;; appropriate options, it will return a graphics context.  If needed a
;;; new one will be created.  Note that the GC's are actually owned and
;;; managed by the view's display object.

(define (CV-GC width color stipple dash font arc)
    (display-gc *display* width (or color *foreground*) *background*
	stipple stipple-x stipple-y dash font arc *clip-bbl*))

;;; Points are converted to pixels by the following function.  A line width
;;; of #f converts to 0.

(define (POINTS->PIXELS x)
    (if x (inexact->exact (round (* *pixels/point* x))) 0))

;;; The following procedure checks to see if a drawing name exists in the
;;; last checked window name.  It is used in conjuction with WINDOW-EXISTS?
;;; to parse the window and drawing within the window names in a command.

(define (DRAWING-IN-LAST-EXISTING-WINDOW? x)
    (and (symbol? x)
	 (let loop ((vl (window-views
			    (name->window last-existing-window-name))))
	      (if (pair? vl)
		  (let ((view (car vl)))
		       (if (eq? x (view-drawing-name view))
			   #t
			   (loop (cdr vl))))
		  #f))))

;;; A drawing is shown in a window by defining a "view" of a drawing in a
;;; window.  This is done by the OVERLAY and UNDERLAY commands that place the
;;; drawing over or under the drawings already in the window.  If the drawing
;;; is already visible in the window, it will be repositioned.

(define (OVER/UNDER-LAY wname dname over bb)
    (let* ((window (name->window wname))
	   (drawing (name->drawing dname))
	   (views (window-views window)))
	  
	  (define (CVT x) (if (list-ref bb 4) (points->pixels x) x))
	  
	  (define (ADD-VIEW views view)
		  (window-views! window (if over
					    (append views (list view))
					    (cons view views)))
		  (when bb
			(view-clip-minx! view (cvt (car bb)))
			(view-clip-miny! view (cvt (cadr bb)))
			(view-clip-maxx! view (cvt (+ (car bb) (caddr bb))))
			(view-clip-maxy! view (cvt (+ (cadr bb) (cadddr bb)))))
		  (handle-view-events view 'overlay #f
		      (if (view-clip-minx view)
			  `(,(view-clip-minx view) ,(view-clip-miny view)
			    ,(- (view-clip-maxx view) (view-clip-minx view))
			    ,(- (view-clip-maxy view)(view-clip-miny view)))
			  `(0 0 ,(window-width (view-window view))
				,(window-height (view-window view)))))
		  (handle-visible-events view))
		      
	  
	  (let loop ((vl views))
	       (if (pair? vl)
		   (let ((view (car vl)))
			(if (eq? (view-drawing-name view) dname)
			    (begin (damage-view-area view)
				   (set-view #f '())
				   (add-view (remq view views) view))
			    (loop (cdr vl))))
		   (let ((view (make-view dname wname)))
			(if (null? views)
			    (xmapraised *dpy* (window-xwindow window))
			    (if (not over) (damage-view-area view)))
			(add-view views view)))))
    (set! *update-display* #t))

(define-ezd-command
    `(overlay ,window-exists? ,drawing-exists?
	 (optional ,non-negative? ,non-negative? ,positive-number?
	     ,positive-number? (optional POINTS)))
    "(overlay window-name drawing-name [ x y width height [ POINTS ] ])"
    (lambda (w-name d-name bb) (over/under-lay w-name d-name #t bb)))

(define-ezd-command
    `(underlay ,window-exists? ,drawing-exists?
	 (optional ,non-negative? ,non-negative? ,positive-number?
	     ,positive-number? (optional POINTS)))
    "(underlay window-name drawing-name [ x y width height [ POINTS ] ])"
    (lambda (w-name d-name bb) (over/under-lay w-name d-name #f bb)))

;;; The X window area occupied by a view is "damaged" by the following
;;; procedure to force it to be updated when the window is redrawn.

(define (DAMAGE-VIEW-AREA view)
    (let ((drawing (view-drawing view))
	  (window (view-window view)))
	 (set-view view '())
	 (if (not (drawing-is-clear drawing))
	     (for-each
		 (lambda (bbl)
			 (window-damage-bbl! window
			     (merge-bbl (car bbl) (cadr bbl) (caddr bbl)
				 (cadddr bbl) (window-damage-bbl window))))
		 (clip-bbl-to-view view
		     (map (lambda (g) ((graphic-compute-bb g)))
			  (drawing-head drawing)))))))

;;; A window name and drawing name is translated to a view by the following
;;; procedure.

(define (WINDOW-DRAWING->VIEW window-name drawing-name)
    (let loop ((vl (window-views (name->window window-name))))
	 (if (pair? vl)
	     (let ((view (car vl)))
		  (if (eq? drawing-name (view-drawing-name view))
		      view
		      (loop (cdr vl))))
	     (ezd-error 'WINDOW-DRAWING->VIEW
		    "DRAWING ~a is not visible in WINDOW ~s"
		    drawing-name window-name))))

;;; A view is deleted by the ezd command DELETE-VIEW.

(define (DELETE-VIEW window-name drawing-name)
    (let* ((view (window-drawing->view window-name drawing-name))
	   (window (view-window view)))
	  (window-views! window (remq view (window-views window)))
	  (if (null? (window-views window))
	      (begin (window-exposed! window #f)
		     (xunmapwindow *dpy* (window-xwindow window)))
	      (damage-view-area view))
	  (handle-view-events view 'overlay #f (list #f #f #f #f))
	  (handle-view-events view 'visible #f (list #f #f #f #f))
	  (set! *update-display* #t)))

(define-ezd-command
    `(delete-view ,window-exists? ,drawing-in-last-existing-window?)
    "(delete-view window-name drawing-name)"
    delete-view)

;;; When an area of a drawing is made visible by either making it a view, or
;;; changing it's coordinate system, then this event must be reported to any
;;; objects in the drawing that expect it.  The arguments are a bounding box
;;; (in the drawing's coordinate system) of the area of the drawing visible
;;; in the view.

(define (HANDLE-VISIBLE-EVENTS view)
    (handle-view-events view 'visible #f
	(list ((view-x->user view) (or (view-clip-minx view) 0))
	      ((view-y->user view) (or (view-clip-miny view) 0))
	      ((view-width->user view)
	       (- (or (view-clip-maxx view) (window-width (view-window view)))
		  (or (view-clip-minx view) 0)))
	      ((view-height->user view)
	       (- (or (view-clip-maxy view)(window-height (view-window view)))
		  (or (view-clip-miny view) 0))))))

;;; The coordinate system of a view is modified by the ezd commands
;;; ORIGIN and SCALE.  The ORIGIN command provides the X coordinates of the
;;; origin of the user's coordinate system.  The SCALE command provide the
;;; scale factors for scaling the x and y coordinates.  The coordinate
;;; transformations are:
;;;
;;;	x11 = user-x * scale-x + origin-x
;;;	y11 = user-y * scale-y + origin-y

(define-ezd-command
    `(origin ,window-exists? ,drawing-in-last-existing-window?
	     ,number? ,number? (optional points))
    "(origin window-name drawing-name x y [ points ])"
    (lambda (w d x y points)
	    (if points
		(queue-transform w d (points->pixels x)
		    (points->pixels y) #f #f #f)
		(queue-transform w d x y #f #f #f))))

(define-ezd-command
    `(scale ,window-exists? ,drawing-in-last-existing-window?
	    ,non-zero? ,non-zero? ,positive-number? (optional points))
    "(scale window-name drawing-name x-scale y-scale line-width-scale [ points ])"
    (lambda (w d x y lw points)
	    (if points
		(queue-transform w d #f #f (* *pixels/point* x)
		    (* *pixels/point* y) (* *pixels/point* lw))
		(queue-transform w d #f #f x y lw))))

;;; The first step when performing a transformation is to save the new
;;; transform in the view.  The actual transformation occurs the next time the
;;; display is updated.

(define (QUEUE-TRANSFORM window-name drawing-name originx originy scalex
	    scaley scalelw)
    (let ((view (window-drawing->view window-name drawing-name))
	  (originx (and originx (inexact->exact (round originx))))
	  (originy (and originy (inexact->exact (round originy)))))
	 (when (or (and originx
			(or (not (equal? originx (view-originx view)))
			    (not (equal? originy (view-originy view)))))
		   (and scalex
			(or (not (equal? scalex (view-scalex view)))
			    (not (equal? scaley (view-scaley view)))
			    (not (equal? scalelw (view-scalelw view))))))
	       (view-new-transform! view
		   (let loop ((old (or (view-new-transform view)
				       '(#f #f #f #f #f)))
			      (new (list originx originy scalex scaley
					 scalelw)))
			(if (pair? new)
			    (cons (or (car new) (car old))
				  (loop (cdr old) (cdr new)))
			    '())))
	       (set! *update-display* #t))))

;;; When the display is updated, the transformations on all views in a window
;;; are done together.  The first step is to compute the new coordinate
;;; transformation functions.  Once this is done, the display is updated by
;;; moving existing bits, or damaging it and forcing it to be redrawn.  A list
;;; of views needing visible events is returned.  The events are sent after
;;; drawing completes.

(define VISIBLE-EVENT-QUEUE '())

(define (TRANSFORM-VIEWS views)
    (set! visible-event-queue '())
    (for-each transform-a-partition (partition-views views))
    visible-event-queue)

(define (TRANSFORM-A-PARTITION views)
    
    (define (MERGE x l) (if (member x l) l (cons x l)))
    
    (define (ACCUM func l x default)
	    (let loop ((v #f) (l l))
		 (if (pair? l)
		     (let ((next-v (or (list-ref (car l) x) default)))
			  (loop (if v (func v next-v) next-v) (cdr l)))
		     v)))
    
    (let ((rescaled #f)
	  (deltax '())
	  (deltay '())
	  (clip '()))
	 (for-each
	     (lambda (view)
		     (let ((transform (view-new-transform view)))
			  (view-new-transform! view #f)
			  (cond ((drawing-is-clear (view-drawing view))
				 (if (pair? transform)
				     (apply transform-a-view view transform)))
				((pair? transform)
				 (let* ((result (apply transform-a-view
						       view transform))
					(rs (car result))
					(dx (cadr result))
					(dy (caddr result)))
				       (set! rescaled (or rs rescaled))
				       (set! deltax (merge dx deltax))
				       (set! deltay (merge dy deltay))
				       (set! clip
					     (merge `(,(view-clip-minx view)
						      ,(view-clip-miny view)
						      ,(view-clip-maxx view)
						      ,(view-clip-maxy view))
						    clip))))
				(else (set! rescaled #t)))))
	     views)
	 (if (not (null? deltax))
	     (let* ((window (view-window (car views)))
		    (minx (accum min clip 0 0))
		    (miny (accum min clip 1 0))
		    (maxx (accum max clip 2 (window-width window)))
		    (maxy (accum max clip 3 (window-height window))))
		   (transform-redisplay views  
		       (or rescaled (> (length deltax) 1)
			   (> (length deltay) 1) (> (length clip) 1))
		       (car deltax) (car deltay) minx miny maxx maxy)))))

;;; Coordinate transformations are performed on an existing view by the
;;; following function.

(define (TRANSFORM-A-VIEW view originx originy scalex scaley scalelw)
    (set-view view '())
    (let ((was-originx (view-originx *current-view*))
	  (was-originy (view-originy *current-view*)))
	 (if originx (view-originx! *current-view* originx))
	 (if originy (view-originy! *current-view* originy))
	 (if scalex (view-scalex! *current-view* scalex))
	 (if scaley (view-scaley! *current-view* scaley))
	 (if scalelw (view-scalelw! *current-view* scalelw))
	 (let ((originx (view-originx *current-view*))
	       (originy (view-originy *current-view*))
	       (rescaled (or scalex scaley scalelw))
	       (scalex (view-scalex *current-view*))
	       (scaley (view-scaley *current-view*))
	       (scalelw (view-scalelw *current-view*)))
	      (set! user->x
		    (lambda (x) (inexact->exact (+ (* x scalex) originx))))
	      (set! user->y
		    (lambda (y) (inexact->exact (+ (* y scaley) originy))))
	      (set! user->lw
		    (lambda (x) (if x (inexact->exact (* scalelw x)) 0)))
	      (set! x->user (lambda (x) (/ (- x originx) scalex)))
	      (set! y->user (lambda (y) (/ (- y originy) scaley)))
	      (set! user->width
		    (lambda (w) (inexact->exact (abs (* w scalex)))))
	      (set! user->height
		    (lambda (h) (inexact->exact (abs (* h scaley)))))
	      (set! width->user (lambda (w) (abs (/ w scalex))))
	      (set! height->user (lambda (h) (abs (/ h scaley))))
	      (set-view #f '())
	      (set-view view '())
	      (set! visible-event-queue (cons view visible-event-queue))
	      (list rescaled (- originx was-originx)
		    (- originy was-originy)))))

;;; Once the coordinate transformation is complete, the following procedure
;;; is called to change the window.  When a partition of a window is being
;;; uniformly transformed and no scaling is being done, then bits will be
;;; moved using xcopyarea.  All other transformations will result in the
;;; window being damaged and redrawn.
;;;
;;; N.B.: VIEW-COMPILED is required as the optimum case that uses XIFEVENT
;;; can only be used if TRANSFORM-REDISPLAY is compiled.

(eval-when (load) (define VIEW-COMPILED #t))
(eval-when (eval) (define VIEW-COMPILED #f))

(define (TRANSFORM-REDISPLAY views damage-all deltax deltay
	    minx miny maxx maxy)
    
    (define (GRAPHICS-EVENT? dpy event any)
	    (let* ((event (cons 'xeventp
				((lap (x) (POINTER_TSCP (UNSIGNED x))) event)))
		   (type (xevent-type event)))
		  (if (or (and (eq? type graphicsexpose)
			       (eq? (xevent-xgraphicsexpose-drawable event)
				    *xwindow*))
			  (and (eq? type noexpose)
			       (eq? (xevent-xnoexpose-drawable event)
				    *xwindow*)))
		      1
		      0)))
    
    (define (HANDLE-GRAPHICS-EVENTS)
	    (let ((event (xifevent *dpy* graphics-event? (cons 'charp 0))))
		 (when (eq? (xevent-type event) graphicsexpose)
		       (window-expose-bbl! *window*
			   (merge-bbl (xevent-xgraphicsexpose-x event)
			       (xevent-xgraphicsexpose-y event)
			       (+ (xevent-xgraphicsexpose-x event)
				  (xevent-xgraphicsexpose-width
				      event))
			       (+ (xevent-xgraphicsexpose-y event)
				  (xevent-xgraphicsexpose-height
				      event))
			       (window-expose-bbl *window*)))
		       (unless (zero? (xevent-xgraphicsexpose-count
					  event))
			       (handle-graphics-events)))))
    
    (define (DAMAGEAREA x y width height)
	    (window-damage-bbl! *window*
		(merge-bbl x y (+ x width) (+ y height)
		    (window-damage-bbl *window*))))
    
    (set-view (car views) '())
    (let ((width (- maxx minx))
	  (height (- maxy miny)))
	 (if (or damage-all (not view-compiled) (not (window-exposed *window*))
		 (>= (abs deltax) width) (>= (abs deltay) height))
	     (begin (for-each
			(lambda (view)
				(set-view view '())
				(view-new! *current-view* #t)
				(view-bb-head! *current-view* '())
				(view-bb-tail! *current-view* '()))
			views)
		    (if (window-exposed *window*)
			(damagearea minx miny width height)))
	     (begin  (xcopyarea *dpy* *xwindow* *xwindow*
			 (cv-gc #f #f #f #f #f #f) minx miny width height
			 (+ minx deltax) (+ miny deltay))
		     (set! stipple-x (+ stipple-x deltax))
		     (set! stipple-y (+ stipple-y deltay))
		     (if (negative? deltax)
			 (damagearea (+ maxx deltax) miny (abs deltax) height))
		     (if (positive? deltax)
			 (damagearea minx miny deltax height))
		     (if (negative? deltay)
			 (damagearea minx (+ maxy deltay) width (abs deltay)))
		     (if (positive? deltay)
			 (damagearea minx miny width deltay))
		     (if (eq? *clean-mouse-window* *window*)
			 (set! *clean-mouse-window* #f))
		     (for-each
			 (lambda (view)
				 (set-view view '())
				 (for-each
				     bbgraphic-bounding-box
				     (view-bb-head *current-view*)))
			 views)
		     (handle-graphics-events)))
	 (set! *update-display* #t)))

;;; Redraw those portions of the drawing inside the update bounding boxes as
;;; well as those additions to the drawing outside the bounding boxes.  The
;;; bounding boxes are specified in X's coordinate system.  If no bounding
;;; box list is specified, then simply write the additions.

(define (REDRAW-A-VIEW view bbl)
    
    (define (RECOMPUTE-BBGL gl stop)
	    (let loop ((gl gl) (head '()) (tail '()))
		 (if (and (pair? gl) (not (eq? gl stop)))
		     (let* ((g (car gl))
			    (bbgl (list (make-bbgraphic g))))
			   (if (null? head)
			       (loop (cdr gl) bbgl bbgl)
			       (loop (cdr gl) head (begin (set-cdr! tail bbgl)
							  bbgl))))
		     (begin (view-bb-head! view head)
			    (view-bb-tail! view tail)))))
    
    (define (ADD-AND-DRAW gl)
	    (let loop ((gl gl) (head (view-bb-head view))
		       (tail (view-bb-tail view)))
		 (if (pair? gl)
		     (let* ((g (car gl))
			    (bbg (make-bbgraphic g))
			    (bbgl (list bbg)))
			   ((graphic-xdraw g))
			   (if (null? head)
			       (loop (cdr gl) bbgl bbgl)
			       (loop (cdr gl) head (begin (set-cdr! tail bbgl)
							  bbgl))))
		     (begin (view-bb-head! view head)
			    (view-bb-tail! view tail)))))
    
    (define (XDRAW-INTERSECTING-BBGRAPHICS)
	    (let loop ((minx #f) (miny #f) (maxx #f) (maxy #f) (l bbl))
		 (if (pair? l)
		     (let ((h (car l)))
			  (loop (bbmin minx (car h)) (bbmin miny (cadr h))
				(bbmax maxx (caddr h)) (bbmax maxy (cadddr h))
				(cdr l)))
		     (bbgraphics-intersect (view-bb-head view)
			 minx miny maxx maxy
			 (if (= (length bbl) 1)
			     xdraw-bbgraphic
			     if-in-any-xdraw-bbgraphic)))))
    
    (define (IF-IN-ANY-XDRAW-BBGRAPHIC bbg)
	    (let ((minx (bbgraphic-minx bbg))
		  (miny (bbgraphic-miny bbg))
		  (maxx (bbgraphic-maxx bbg))
		  (maxy (bbgraphic-maxy bbg)))
		 (let loop ((bbl bbl))
		      (if (pair? bbl)
			  (let ((bb (car bbl)))
			       (if (or (>= (car bb) maxx) (>= (cadr bb) maxy)
				       (<= (caddr bb) minx)
				       (<= (cadddr bb) miny))
				   (loop (cdr bbl))
				   (xdraw-bbgraphic bbg)))))))
    
    (let ((drawing (view-drawing view)))
	 (set-view view '())
	 (cond ((view-new view)
		(view-new! view #f)
		(add-and-draw (drawing-head drawing)))
	       ((drawing-cleared drawing)
		(view-bb-head! view '())
		(view-bb-tail! view '())
		(add-and-draw (drawing-head drawing)))
	       ((not (null? bbl))
		(if  (drawing-zmotion drawing)
		     (recompute-bbgl (drawing-head drawing)
			 (drawing-added-head drawing)))
		(set-view view bbl)
		(xdraw-intersecting-bbgraphics)
		(set-view view '())
		(add-and-draw (drawing-added-head drawing)))
	       (else (add-and-draw (drawing-added-head drawing))))))

;;; Module reset/initialization procedure.

(define (VIEW-MODULE-INIT)
    (set! *current-view* #f))
Back to Top