;; $Id: dbcompon.dsl 0.91 1997/08/28 14:09:41 nwalsh Exp $

;; ============================= COMPONENTS =============================
;;
;; in docbook, components are containers at the chapter/appendix level

(define ($title-header-footer$)
  (make sequence
    use: para-style
    font-posture: 'italic
    (with-mode hf-mode (process-first-descendant "TITLE"))))

(define ($page-number-header-footer$) 
  (make sequence
    use: para-style
    font-posture: 'italic
    (literal 
     (string-append
      (string-with-space %gentext-page%)
      (if %page-number-restart%
	  (string-append
	   (cond
	    ((equal? (gi) "CHAPTER")
	     (format-number (child-number) "1"))
	    ((equal? (gi) "APPENDIX")
	     (format-number (child-number) "A"))
	    ((equal? (gi) "GLOSSARY") "Glossary")
	    ((equal? (gi) "PREFACE") "Preface")
	    (else ""))
	   (gentext-intra-label-sep "_pagenumber"))
	  "")))
    (page-number-sosofo)))

(define ($side-header$ outer?)
  (if outer?
      ($title-header-footer$)
      (empty-sosofo)))

(define ($center-header$)
  (make sequence
    use: para-style
    font-posture: 'italic
    (literal "")))

(define ($side-footer$ outer?) 
  (if outer?
      ($page-number-header-footer$)
      (empty-sosofo)))

(define ($center-footer$)
  (make sequence
    use: para-style
    font-posture: 'italic
    (literal "")))

(define ($page-side-header$ lhs?)
  (if %two-side%
      (if-front-page
       ($side-header$ (not lhs?))
       ($side-header$ $lhs))

      ;; If printing is only on one side of the paper, if the writing mode
      ;; is 'left-to-right, the headers and footers are printed on the left
      ;; hand side of the page; if the writing mode is 'right-to-left, they
      ;; are printed on the left hand side. [i.e. They are printed on the
      ;; outside of the page.]
      ;;
      (if (equal? %writing-mode% 'left-to-right)
	  ($side-header$ (not lhs?))
	  ($side-header$ lhs?))))

(define ($page-side-footer$ lhs?)
  (if %two-side%
      (if-front-page
       ($side-footer$ (not lhs?))
       ($side-footer$ $lhs))

      ;; If printing is only on one side of the paper, if the writing mode
      ;; is 'left-to-right, the headers and footers are printed on the left
      ;; hand side of the page; if the writing mode is 'right-to-left, they
      ;; are printed on the left hand side. [i.e. They are printed on the
      ;; outside of the page.]
      ;;
      (if (equal? %writing-mode% 'left-to-right)
	  ($side-footer$ (not lhs?))
	  ($side-footer$ lhs?))))

(define ($component$)
  (make simple-page-sequence
    page-number-restart?: (or %page-number-restart% (book-start?))
    use: para-style
    start-indent: %body-start-indent%
    left-header:  ($page-side-header$ #t)
    center-header: ($center-header$)
    right-header: ($page-side-header$ #f)
    left-footer: ($page-side-footer$ #t)
    center-footer: ($center-footer$)
    right-footer: ($page-side-footer$ #f)
    input-whitespace-treatment: 'collapse
    quadding: %default-quadding%
    (process-children-trim)))

;; this is how we prevent the title in the header from acquiring the
;;   display treatment that it receives in the body of the document
;;
(mode hf-mode
  (element TITLE
    (if %chap-app-running-heads%
	(make sequence
	  (if (or (have-ancestor? "CHAPTER")
		  (have-ancestor? "APPENDIX"))
	      (literal (gentext-element-name-space (gi (parent)))
		       (element-label (parent))
		       (gentext-label-title-sep (gi (parent))))
	      (empty-sosofo))
	  (process-children-trim))
	(empty-sosofo))))

(define ($comptitle$)
  (make paragraph
	font-family-name: %title-font-family%
	font-weight: 'bold
	font-size: (HSIZE 4)
	line-spacing: (* (HSIZE 4) %line-spacing-factor%)
	space-before: (* (HSIZE 4) %head-before-factor%)
	space-after: (* (HSIZE 4) %head-after-factor%)
	start-indent: 0pt
	first-line-start-indent: 0pt
	quadding: 'start
	keep-with-next?: #t
	(if (or (have-ancestor? "CHAPTER")
		(have-ancestor? "APPENDIX"))
	    (literal (gentext-element-name-space (gi (parent)))
		     (element-label (parent))
		     (gentext-label-title-sep (gi (parent))))
	    (empty-sosofo))
	(process-children-trim)))

(define (CHAP-APP-HEAD-LABEL chap-or-app)
  (let ((label
	 (attribute-string "label" (ancestor chap-or-app))))
    (if (or label %chap-app-running-head-autolabel%)
	(string-append 
	 (gentext-element-name-space chap-or-app)
	 (if (or (not label) (equal? label "auto"))
	     (format-number
	      (child-number (ancestor chap-or-app))
	      (if (equal? chap-or-app "CHAPTER") "1" "A"))
	     label)
	 (gentext-label-title-sep chap-or-app))
	"")))

(element APPENDIX ($component$))
(element (APPENDIX TITLE) ($comptitle$))
(element CHAPTER ($component$))
(element (CHAPTER TITLE) ($comptitle$))
(element PREFACE ($component$))
(element (PREFACE TITLE) ($comptitle$))

(element ARTICLE ($component$))
(element (ARTHEADER TITLE) ($comptitle$))
