(define-record-type window-map :window-map
  (make-window-map children)
  window-map?
  (children window-map-children set-window-map-children!))
	      
(define (window-map-find-by-point window-map point)
  (let loop ((children (window-map-children window-map)))
    (if (null? children)
	#f
	(let* ((child (car children))
	       (rect (bitmap-rectangle (widget-env-bitmap child))))
	  (if (point-in-rectangle? point rect)
	      child
	      (loop (cdr children)))))))

(define (window-map-widget-to-front! window-map widget-env)
  (window-map-delete-widget! window-map widget-env)
  (window-map-insert-widget! window-map widget-env))

(define (window-map-widget-to-back! window-map widget-env)
  (window-map-delete-widget! window-map widget-env)
  (set-window-map-children!
   window-map
   (append (window-map-children window-map)
	   (list widget-env)))
  (bitmap-to-back! (widget-env-bitmap widget-env)))

(define (window-map-insert-widget! window-map widget-env)
  (set-window-map-children!
   window-map
   (cons widget-env 
	 (window-map-children window-map)))
  (bitmap-to-front! (widget-env-bitmap widget-env)))

(define (window-map-delete-widget! window-map widget-env)
  (set-window-map-children!
   window-map
   (delete widget-env (window-map-children window-map))))

(define (window-map-move-widget! window-map widget-env point)
  (let loop ((children (window-map-children window-map)))
    (if (not (null? children))
	(if (equal? (car children) widget-env)
	    (bitmap-move! (widget-env-bitmap (car children))
			  point)
	    (loop (cdr children))))))

	  


