r4958: Auto commit for Debian build
[hyperobject.git] / views.lisp
index 2286d9bdd6481119c290b34566ba2c2c00326b68..d5536ce698344e96f249bdcc8f05eaa58319e35e 100644 (file)
@@ -7,10 +7,9 @@
 ;;;; Programmer:    Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: views.lisp,v 1.32 2003/05/13 23:10:44 kevin Exp $
-;;;;
-;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
+;;;; $Id: views.lisp,v 1.44 2003/05/14 21:18:12 kevin Exp $
 ;;;;
+;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
 ;;;; *************************************************************************
  
 (in-package :hyperobject)
                   :accessor obj-end-indent)
    (obj-data-indent :initform nil :initarg :obj-data-indent
                    :accessor obj-data-indent)
-   (obj-data-fmtstr :type (or string null) :initform nil :initarg :obj-data-fmtstr
-                   :accessor obj-data-fmtstr)
-   (obj-data-value-func :type (or function symbol null) :initform nil
-                       :initarg :obj-data-value-func
-                       :accessor obj-data-value-func)
+   (obj-data-func :type (or function null) :initform nil
+                       :initarg :obj-data-func
+                       :accessor obj-data-func)
+   (obj-data-print-code :type (or function null) :initform nil
+                 :initarg :obj-data-print-code
+                 :accessor obj-data-print-code)
    (obj-data-end-str :type (or string null) :initform nil
                        :initarg :obj-data-end-str
                        :accessor obj-data-end-str)
     )
   )
 
+(defmacro write-simple (v s)
+  `(typecase ,v
+    (string
+     (write-string ,v ,s))
+    #+allegro
+    (fixnum
+     (excl::print-fixnum ,s 10 ,v)) 
+    (symbol
+     (write-string (symbol-name ,v) ,s))
+    (t
+     (write-string (write-to-string ,v) ,s))))
+
+(defun write-ho-value (obj name type formatter cdata strm)
+  (declare (ignorable type))
+  (let* ((slot-data (slot-value obj name))
+        (fmt-data (if formatter
+                      (funcall formatter slot-data)
+                  slot-data))
+        (data (if cdata
+                  (kmrcl:xml-cdata fmt-data)
+                  fmt-data)))
+    (write-simple data strm)))
+
+
+(defun ppfc-html (title name type formatter cdata print-func)
+  (vector-push-extend '(write-string "<span class=\"" s) print-func)
+  (vector-push-extend `(write-string ,title s) print-func)
+  (vector-push-extend '(write-string "\">" s) print-func)
+  (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
+  (vector-push-extend '(write-string "</span>" s) print-func))
+
+(defun ppfc-xml (tag name type formatter cdata print-func)
+  (vector-push-extend '(write-char #\< s) print-func)
+  (vector-push-extend `(write-string ,tag s) print-func)
+  (vector-push-extend '(write-char #\> s) print-func)
+  (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
+  (vector-push-extend '(write-string "</" s) print-func)
+  (vector-push-extend `(write-string ,tag s) print-func)
+  (vector-push-extend '(write-char #\> s) print-func))
+
+(defun ppfc-html-labels (label name type formatter cdata print-func)
+  (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
+  (vector-push-extend `(write-string ,label s) print-func)
+  (vector-push-extend '(write-string "</span> " s) print-func)
+  (ppfc-html label name type formatter cdata print-func))
+
+(defun ppfc-xhtml-labels (label tag name type formatter cdata print-func)
+  (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
+  (vector-push-extend `(write-string ,label s) print-func)
+  (vector-push-extend '(write-string "</span> " s) print-func)
+  (ppfc-html tag name type formatter cdata print-func))
+
+(defun ppfc-xml-labels (label tag name type formatter cdata print-func)
+  (vector-push-extend '(write-string "<label>" s) print-func)
+  (vector-push-extend `(write-string ,label s) print-func)
+  (vector-push-extend '(write-string "</label> " s) print-func)
+  (ppfc-xml tag name type formatter cdata print-func))
+
+(defun ppfc-html-link (name type formatter cdata nlink print-func)
+  (declare (fixnum nlink))
+  (vector-push-extend '(write-char #\< s) print-func)
+  (vector-push-extend `(write-string (nth ,(+ nlink nlink) links) s) print-func) 
+  (vector-push-extend '(write-char #\> s) print-func)
+  (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
+  (vector-push-extend '(write-string "</" s) print-func)
+  (vector-push-extend `(write-string (nth ,(+ nlink nlink 1) links) s) print-func) 
+  (vector-push-extend '(write-char #\> s) print-func))
+
+(defun ppfc-html-link-labels (label name type formatter cdata nlink print-func)
+  (vector-push-extend '(write-string "<label>" s) print-func)
+  (vector-push-extend `(write-string ,label s) print-func)
+  (vector-push-extend '(write-string "</label> " s) print-func)
+  (ppfc-html-link name type formatter cdata nlink print-func))
+
+(defun push-print-fun-code (category slot nlink print-func)
+  (let* ((formatter (esd-print-formatter slot))
+        (name (slot-definition-name slot))
+        (namestr-lower (string-downcase (symbol-name name)))
+        (xml-namestr (escape-xml-string namestr-lower))
+        (xml-tag (escape-xml-string namestr-lower))
+        (type (slot-value slot 'type))
+        (cdata (not (null
+                     (and (in category :xml :xhtml :xml-link :xhtml-link
+                              :xml-labels :ie-xml-labels
+                              :xhtml-link-labels :xml-link-labels :ie-xml-link
+                              :ie-xml-link-labels)
+                          (or formatter
+                              (lisp-type-is-a-string type))))))
+        (hyperlink (esd-hyperlink slot)))
+    
+    (case category
+      (:compact-text
+       (vector-push-extend
+       `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
+      (:compact-text-labels
+       (vector-push-extend `(write-string ,namestr-lower s) print-func)
+       (vector-push-extend '(write-char #\space s) print-func)
+       (vector-push-extend
+       `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
+      ((or :html :xhtml)
+       (ppfc-html namestr-lower name type formatter cdata print-func))
+      (:xml
+       (ppfc-xml xml-tag name type formatter cdata print-func))
+      (:html-labels
+       (ppfc-html-labels namestr-lower name type formatter cdata print-func))
+      (:xhtml-labels
+       (ppfc-xhtml-labels xml-namestr namestr-lower name type formatter cdata print-func))
+      (:xml-labels
+       (ppfc-xml-labels xml-namestr xml-tag name type formatter cdata print-func))
+      ((or :html-link :xhtml-link)
+       (if hyperlink
+          (ppfc-html-link name type formatter cdata nlink print-func)
+          (ppfc-html namestr-lower name type formatter cdata print-func)))
+      ((or :xml-link :ie-xml-link)
+       (if hyperlink
+          (ppfc-html-link name type formatter cdata nlink print-func)
+          (ppfc-xml xml-tag name type formatter cdata print-func)))
+      (:html-link-labels
+       (if hyperlink
+          (ppfc-html-link-labels namestr-lower name type formatter cdata nlink
+                                 print-func)
+          (ppfc-html-labels namestr-lower name type formatter cdata print-func)))
+      (:xhtml-link-labels
+       (if hyperlink
+          (ppfc-html-link-labels xml-namestr name type formatter cdata nlink
+                                 print-func)
+          (ppfc-xhtml-labels xml-tag namestr-lower name type formatter cdata
+                             print-func)))
+      ((or :xml-link-labels :ie-xml-link-labels)
+       (if hyperlink
+          (ppfc-html-link-labels xml-namestr name type formatter cdata nlink
+                                 print-func)
+          (ppfc-xml-labels xml-tag namestr-lower name type formatter cdata
+                           print-func))))))
+
+
+(defun view-has-links-p (view)
+  (in (category view) :html-link :xhtml-link :xml-link :ie-xml-link
+      :html-link-labels :xhtml-link-labels :xml-link-labels
+      :ie-xml-link-labels))
+
+(defun initialize-view-by-category (obj-cl view)
+  "Initialize a view based upon a preset category"
+  (unless (in (category view) :compact-text :compact-text-labels
+             :html :html-labels :html-link-labels
+             :xhtml :xhtml-labels :xhtml-link-labels
+             :xhtml-link :html-link
+             :xml :xml-labels :xml-link :ie-xml-link
+             :xml-link-labels :ie-xml-link-labels)
+    (error "Unknown view category ~A" (category view)))
+  
+  (unless (slots view) (setf (slots view) (default-print-slots obj-cl)))
+
+  (let ((links '())
+       (print-func (make-array 10 :fill-pointer 0 :adjustable t)))
+
+    (do* ((slots (slots view) (cdr slots))
+         (slot-name (car slots) (car slots))
+         (slot (find-slot-by-name obj-cl slot-name)
+               (find-slot-by-name obj-cl slot-name)))
+        ((null slots))
+      (unless slot
+       (error "Slot ~A is not found in class ~S" slot-name obj-cl))
+      
+      (push-print-fun-code (category view) slot (length links) print-func)
+      (when (> (length slots) 1)
+       (vector-push-extend '(write-char #\space s) print-func))
+
+      (when (and (view-has-links-p view) (esd-hyperlink slot))
+       (push (slot-definition-name slot) links)))
+
+    (when (plusp (length print-func))
+      (setf (obj-data-print-code view) `(lambda (x s links)
+                                        (declare (ignorable links))
+                                        ,@(map 'list #'identity print-func)))
+      (setf (obj-data-func view)
+           (compile nil (eval (obj-data-print-code view)))))
+    
+    (setf (link-slots view) (nreverse links)))
+
+  (finalize-view-by-category view)
+  view)
 
+(defun finalize-view-by-category (view)
+  (case (category view)
+    ((or :compact-text :compact-text-labels)
+     (initialize-text-view view))
+    ((or :html :xhtml :html-labels :xhtml-labels)
+     (initialize-html-view view))
+    ((or :xml :xml-labels)
+     (initialize-xml-view view))
+    ((or :html-link :html-link-labels)
+     (initialize-html-view view)
+     (setf (link-href-start view) "a href=")
+     (setf (link-href-end view) "a")
+     (setf (link-ampersand view) "&"))
+    ((or :xhtml-link :xhtml-link-labels)
+     (initialize-html-view view)
+     (setf (link-href-start view) "a href=")
+     (setf (link-href-end view) "a")
+     (setf (link-ampersand view) "&amp;"))
+    ((or :xml-link :xml-link-labels)
+     (initialize-xml-view view)
+     (setf (link-href-start view)
+          "xmllink xlink:type=\"simple\" xlink:href=")
+     (setf (link-href-end view) "xmllink")
+     (setf (link-ampersand view) "&amp;"))
+    ((or :ie-xml-link :ie-xml-link-labels)
+     (initialize-xml-view view)
+     (setf (link-href-start view) "html:a href=")
+     (setf (link-href-end view) "html:a")
+     (setf (link-ampersand view) "&amp;"))))
+
+#+ignore
 (defun initialize-view-by-category (obj-cl view)
   "Initialize a view based upon a preset category"
   (let ((fmtstr nil)
 
 (defvar +newline-string+ (format nil "~%"))
 
+(defun write-user-name-maybe-plural (obj nitems strm)
+  (write-string
+   (if (> nitems 1)
+       (hyperobject-class-user-name-plural obj)
+       (hyperobject-class-user-name obj))
+   strm))
+
 (defun initialize-text-view (view)
   (setf (list-start-str-or-func view)
        (compile nil
-                #'(lambda (obj nitems strm)
-                    (format strm "~a~P:~%"
-                            (hyperobject-class-user-name obj) nitems))))
+                (eval '(lambda (obj nitems strm)
+                        (write-user-name-maybe-plural obj nitems strm)
+                        (write-char #\: strm)
+                        (write-char #\Newline strm)))))
   (setf (list-start-indent view) t)
   (setf (obj-data-indent view) t)
   (setf (obj-data-end-str view) +newline-string+))
 
 (defun html-list-start-func (obj nitems strm)
-  (format strm "<p><b>~a~p:</b></p><div class=\""
-         (hyperobject-class-user-name obj) nitems)
-  (write-string (class-name-of obj))
-  (write-string "\"><ul>~%" strm))
+  (write-string "<p><b>" strm)
+  (write-user-name-maybe-plural obj nitems strm)
+  (write-string ":</b></p><div class=\"" strm)
+  (write-string (class-name-of obj) strm)
+  (write-string "\"><ul>" strm)
+  (write-char #\newline strm))
 
 (defun initialize-html-view (view)
   (initialize-text-view view)
   (setf (obj-start-str-or-func view) "<li>")
   (setf (obj-end-indent view)  t)
   (setf (obj-end-str-or-func view)  (format nil "</li>~%"))
-  (setf (obj-data-indent view) t))
+  (setf (obj-data-indent view) nil))
 
 (defun initialize-xhtml-view (view)
   (initialize-text-view view)
   (setf (obj-start-str-or-func view) "<li>")
   (setf (obj-end-indent view)  t)
   (setf (obj-end-str-or-func view) (format nil "</li>~%"))
-  (setf (obj-data-indent view) t))
+  (setf (obj-data-indent view) nil))
 
 (defun xmlformat-list-end-func (x strm)
   (write-string "</" strm)
   (write-char #\< strm)
   (write-string (class-name-of x) strm)
   (write-string "list><title>" strm)
-  (format strm "~A~P:</title> ~%"
-         (hyperobject-class-user-name x) nitems))
+  (write-user-name-maybe-plural x nitems strm)
+  (write-string ":</title>" strm)
+  (write-char #\newline strm))
 
 (defun initialize-xml-view (view)
   (initialize-text-view view)
   
 ;;; Object Data 
 
-(defun make-link-start (view fieldfunc fieldvalue refvars)
-  (format nil "~a\"~a?func=~a~akey=~a~a\"" 
-         (link-href-start view)
-         (make-url (link-page-name view))
-         fieldfunc 
-         (link-ampersand view) fieldvalue
-         (if refvars
-             (let ((varstr ""))
-               (dolist (var refvars)
-                 (string-append
-                  varstr (link-ampersand view)
-                  (format nil "~a=~a" (car var) (cadr var))))
-               varstr)
-             "")))
 
+(defun make-link-start (view fieldfunc fieldvalue refvars)
+  (with-output-to-string (s)
+    (write-string (link-href-start view) s)
+    (write-char #\" s)
+    (write-string (make-url (link-page-name view)) s)
+    (write-string "?func=" s)
+    (write-simple fieldfunc s)
+    (write-string (link-ampersand view) s)
+    (write-string "key=" s)
+    (write-simple fieldvalue s)
+    (dolist (var refvars)
+      (write-string (link-ampersand view) s)
+      (write-simple (car var) s)
+      (write-char #\= s)
+      (write-simple (cdr var) s))
+    (write-char #\" s)))
+  
 (defun make-link-end (obj view fieldname)
   (declare (ignore obj fieldname))
   (link-href-end view))
         (write-string it strm)))
 
 (defun fmt-obj-data-plain (obj view strm)
-  (awhen (obj-data-value-func view)
-        (multiple-value-call #'format strm (obj-data-fmtstr view)
-                             (funcall it obj))))
+  (awhen (obj-data-func view)
+        (funcall it obj strm nil)))
 
 (defun fmt-obj-data-with-link (obj view strm refvars)
   (let ((refvalues '()))
     ;; make list of hyperlink link fields for printing to refstr template
     (dolist (name (link-slots view))
-      (let-when (hyperlink
-                (find name (hyperobject-class-hyperlinks obj) :key #'name))
-               (push  (make-link-start view (lookup hyperlink) (slot-value obj name)
-                                       (append (link-parameters hyperlink) refvars))
-                      refvalues)
-               (push (make-link-end obj view name) refvalues)))
-    (setq refvalues (nreverse refvalues))
-    (apply #'format strm (make-link-data-str obj view) refvalues)))
+      (awhen (find name (hyperobject-class-hyperlinks obj) :key #'name)
+            (push (make-link-start view (lookup it) (slot-value obj name)
+                                   (append (link-parameters it) refvars))
+                  refvalues)
+            (push (make-link-end obj view name) refvalues)))
+    (funcall (obj-data-func view) obj strm (nreverse refvalues))))
 
 (defun obj-data (obj view)
   "Returns the objects data as a string. Used by common-graphics outline function"
-  (awhen (obj-data-value-func view)
-        (multiple-value-call #'format nil (funcall (obj-data-fmtstr view))
-                             (funcall it obj))))
-
-(defun make-link-data-str (obj view)
-  "Return fmt string for that contains ~a slots for hyperlink link start and end"
-  (awhen (obj-data-value-func view)
-        (multiple-value-call #'format nil (obj-data-fmtstr view)
-                             (funcall it obj))))
+  (with-output-to-string (s) (fmt-obj-data-plain obj view s)))
 
 ;;; Display method for objects
 
 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
                         subobjects refvars)
   "Display a single or list of hyperobject-class instances and their subobjects"
+  (declare (fixnum indent))
   (let-when (objlist (mklist objs))
     (let ((nobjs (length objlist))
          (*print-pretty* nil)