home *** CD-ROM | disk | FTP | other *** search
- (in-package :oou)
- (oou-provide :te-dim)
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; te-dim.Lisp
- ;;
- ;; Copyright ⌐ 1992 Northwestern University Institute for the Learning Sciences
- ;; All Rights Reserved
- ;;
- ;; author: Michael S. Engber
- ;;
- ;; mixin for adding text edit functionality to views
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (oou-dependencies :simple-view-ce
- :resources-u
- :te-blob)
-
- (export '(te-dim te-margins
- te-set-font te-selection te-set-selection
- te-set-text-rsrc te-save-text-rsrc te-string
- ))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defclass te-dim (key-handler-mixin)
- ((te-blob :accessor te-blob)
- (te-just :initarg :te-just
- :accessor te-just)
- (te-word-wrap-p :initarg :te-word-wrap-p
- :accessor te-word-wrap-p)
- (te-init-string :initarg :te-init-string
- :initarg :dialog-item-text
- :accessor te-init-string)
- (te-init-rsrc :initarg :te-init-rsrc
- :accessor te-init-rsrc)
- (te-read-only-p :initarg :te-read-only-p
- :accessor te-read-only-p)
- (te-v-scroll-bar :initarg :te-v-scroll-bar
- :accessor te-v-scroll-bar)
- (te-h-scroll-bar :initarg :te-h-scroll-bar
- :accessor te-h-scroll-bar)
- (te-v-line-size :initarg :te-v-line-size
- :accessor te-v-line-size)
- (te-h-line-size :initarg :te-h-line-size
- :accessor te-h-line-size))
- (:default-initargs
- :te-init-string "hi,ho"
- :te-just :default
- :te-word-wrap-p t
- :te-read-only-p nil
- :te-v-line-size 5
- :te-h-line-size 5
- ))
-
-
- (defmethod te-fix-scroll-limit ((di te-dim) direction max)
- (ecase direction
- (:vertical
- (when (slot-boundp di 'te-v-scroll-bar)
- (set-scroll-bar-max (te-v-scroll-bar di) max)
- ))
- (:horizontal
- (when (slot-boundp di 'te-h-scroll-bar)
- (set-scroll-bar-max (te-h-scroll-bar di) max)))))
-
- (defmethod te-fix-scroll-value ((di te-dim) direction value)
- (ecase direction
- (:vertical
- (when (slot-boundp di 'te-v-scroll-bar)
- (set-scroll-bar-setting (te-v-scroll-bar di) value)))
- (:horizontal
- (when (slot-boundp di 'te-h-scroll-bar)
- (set-scroll-bar-setting (te-h-scroll-bar di) value)))))
-
-
- (defmethod install-view-in-window :after ((di te-dim) window)
- (multiple-value-bind (topLeft botRight) (te-corners di)
- (let ((te-size (subtract-points botRight topLeft)))
-
- ;set up the vertical scroll bar
- (when (slot-boundp di 'te-v-scroll-bar)
- (when (symbolp (te-v-scroll-bar di))
- (unless (setf (te-v-scroll-bar di) (find-named-sibling di (te-v-scroll-bar di)))
- (slot-makunbound di 'te-v-scroll-bar)
- (error "unable to find scroll bar item named ~a." (te-v-scroll-bar di))))
- (set-scroll-bar-scrollee (te-v-scroll-bar di) di)
- (set-scroll-bar-min (te-v-scroll-bar di) 0)
- (setf (scroll-bar-page-size (te-v-scroll-bar di)) (- (point-v te-size) (te-v-line-size di)))
- (setf (slot-value (te-v-scroll-bar di) 'ccl::scroll-size) (te-v-line-size di)))
-
- ;set up the horizontal scroll bar
- (when (slot-boundp di 'te-h-scroll-bar)
- (when (symbolp (te-h-scroll-bar di))
- (unless (setf (te-h-scroll-bar di) (find-named-sibling di (te-h-scroll-bar di)))
- (slot-makunbound di 'te-h-scroll-bar)
- (error "unable to find scroll bar item named ~a." (te-h-scroll-bar di))))
- (set-scroll-bar-scrollee (te-h-scroll-bar di) di)
- (set-scroll-bar-min (te-h-scroll-bar di) 0)
- (setf (scroll-bar-page-size (te-h-scroll-bar di)) (- (point-h te-size) (te-h-line-size di)))
- (setf (slot-value (te-h-scroll-bar di) 'ccl::scroll-size) (te-h-line-size di)))
-
- (flet ((fix-scroll-limit (direction max)
- (te-fix-scroll-limit di direction max))
- (fix-scroll-value (direction value)
- (te-fix-scroll-value di direction value)))
-
- (setf (te-blob di) (make-instance 'te-blob
- :te-port (wptr window)
- :te-topLeft topLeft
- :te-botRight botRight
- :te-just (te-just di)
- :te-v-scroll-p (slot-boundp di 'te-v-scroll-bar)
- :te-h-scroll-p (slot-boundp di 'te-h-scroll-bar)
- :te-word-wrap-p (te-word-wrap-p di)
- :te-scroll-value-fn #'fix-scroll-value
- :te-scroll-limit-fn #'fix-scroll-limit)))))
- (te-init-text di))
-
- (defmethod remove-view-from-window :after ((di te-dim))
- (te-free (te-blob di))
- (slot-makunbound di 'te-blob))
-
- (defmethod te-init-text ((di te-dim))
- (if (slot-boundp di 'te-init-rsrc)
- (te-set-text-rsrc di (te-init-rsrc di))
- (set-dialog-item-text di (te-init-string di))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- ;;returns the init-rsrc's id (useful if it was specified by name)
- (defmethod te-init-rsrc-id ((di te-dim))
- (when (slot-boundp di 'te-init-rsrc)
- (etypecase (te-init-rsrc di)
- (fixnum (te-init-rsrc di))
- (string (get-resource-id "TEXT" (te-init-rsrc di))))))
-
- ;;returns the init-rsrc's name (useful if it was specified by id)
- (defmethod te-init-rsrc-name ((di te-dim))
- (when (slot-boundp di 'te-init-rsrc)
- (etypecase (te-init-rsrc di)
- (fixnum (get-resource-name "TEXT" (te-init-rsrc di)))
- (string (te-init-rsrc di)))))
-
- (defmethod te-set-text-rsrc ((di te-dim) rsrc-id-or-name)
- (with-focused-view (focusing-view di)
- (with-font-spec (view-font di)
- (with-macptrs ((text-handle (get-resource "TEXT" rsrc-id-or-name :errorp t))
- (style-handle (get-resource "styl" rsrc-id-or-name :errorp nil)))
- (set-te-handles (te-blob di) text-handle (unless (%null-ptr-p style-handle) style-handle))
- (release-resource text-handle)
- (unless (%null-ptr-p style-handle) (release-resource style-handle)))
- rsrc-id-or-name)))
-
- (defmethod te-set-text-rsrc :after ((di te-dim) rsrc-id-or-name)
- (declare (ignore rsrc-id-or-name))
- (invalidate-view di t))
-
- (defmethod te-save-text-rsrc ((di te-dim) &key rsrc-id rsrc-name)
-
- ;determine id & name of 'TEXT' & 'styl' resource
- (if (slot-boundp di 'te-init-rsrc)
- (setf rsrc-id (or rsrc-id (te-init-rsrc-id di))
- rsrc-name (or rsrc-name (te-init-rsrc-name di)))
- (setf rsrc-id (or rsrc-id (#_UniqueId "TEXT"))
- rsrc-name (or rsrc-name "un-named")))
-
- ;remove any existing 'TEXT' & 'styl' resources with same id
- (with-macptrs ((old-text-handle (get-resource "TEXT" rsrc-id :errorp nil))
- (old-style-handle (get-resource "styl" rsrc-id :errorp nil)))
- (unless (%null-ptr-p old-text-handle) (#_RmveResource old-text-handle))
- (unless (%null-ptr-p old-style-handle) (#_RmveResource old-style-handle)))
-
- ;add the 'TEXT' & 'styl' resources to current res file
- (multiple-value-bind (text-handle style-handle) (te-handles (te-blob di))
- (with-pstrs ((name-ptr rsrc-name))
- (#_AddResource text-handle "TEXT" rsrc-id name-ptr)
- (#_WriteResource text-handle)
- (#_AddResource style-handle "styl" rsrc-id name-ptr)
- (#_WriteResource style-handle))
- (#_DisposeHandle text-handle)
- (#_DisposeHandle style-handle))
-
- rsrc-id)
-
-
- (defmethod point-in-te-region-p ((di te-dim) pt)
- (multiple-value-bind (topLeft botRight) (te-corners di)
- (rlet ((r :Rect
- :topLeft topLeft
- :botRight botRight))
- (#_PtInRect pt r))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defmethod view-click-event-handler ((di te-dim) where)
- (if (point-in-te-region-p di where)
- (te-click-handler (te-blob di) where)
- (call-next-method)))
-
- (defmethod view-key-event-handler ((di te-dim) char)
- (unless (te-read-only-p di)
- (with-focused-view (focusing-view di)
- (te-key (te-blob di) char))))
-
- (defmethod key-handler-idle ((di te-dim) &optional dialog)
- (declare (ignore dialog))
- (with-focused-view (focusing-view di)
- (te-idle (te-blob di))))
-
- (defmethod view-activate-event-handler ((di te-dim))
- (te-activate (te-blob di)))
-
- (defmethod view-deactivate-event-handler ((di te-dim))
- (te-deactivate (te-blob di)))
-
- (defmethod set-view-position :after ((di te-dim) h &optional v)
- (declare (ignore h v))
- (multiple-value-bind (topLeft botRight) (te-corners di)
- (set-te-corners (te-blob di) topLeft botRight)))
-
- (defmethod set-view-size :after ((di te-dim) h &optional v)
- (multiple-value-bind (topLeft botRight) (te-corners di)
- (set-te-corners (te-blob di) topLeft botRight))
- (let ((te-size (make-point h v)))
- (when (slot-boundp di 'te-v-scroll-bar)
- (setf (scroll-bar-page-size (te-v-scroll-bar di)) (- (point-v te-size) (te-v-line-size di))))
- (when (slot-boundp di 'te-h-scroll-bar)
- (setf (scroll-bar-page-size (te-h-scroll-bar di)) (- (point-h te-size) (te-h-line-size di)))))
- (invalidate-view di t))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defmethod cut ((di te-dim))
- (unless (te-read-only-p di)
- (with-focused-view (focusing-view di)
- (te-cut (te-blob di)))))
-
- (defmethod copy ((di te-dim))
- (te-copy (te-blob di)))
-
- (defmethod clear ((di te-dim))
- (unless (te-read-only-p di)
- (with-focused-view (focusing-view di)
- (te-clear (te-blob di)))))
-
- (defmethod paste ((di te-dim))
- (unless (te-read-only-p di)
- (with-focused-view (focusing-view di)
- (te-paste (te-blob di)))))
-
- (defmethod select-all ((di te-dim))
- (with-focused-view (focusing-view di)
- (te-select-all (te-blob di))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defmethod te-margins ((di te-dim))
- (declare (ignore di))
- (values #@(0 0) #@(0 0)))
-
- (defmethod te-corners ((di te-dim))
- (multiple-value-bind (topLeft botRight) (focused-corners di)
- (multiple-value-bind (tl-margin br-margin) (te-margins di)
- (values (add-points topLeft tl-margin) (subtract-points botRight br-margin)))))
-
- (defmethod view-draw-contents :after ((di te-dim))
- (te-draw (te-blob di)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defmethod scroll-bar-changed ((di te-dim) scroll-bar)
- (ecase (ccl::scroll-bar-direction scroll-bar)
- (:vertical
- (setf (te-v-scroll-value (te-blob di)) (scroll-bar-setting scroll-bar)))
- (:horizontal
- (setf (te-h-scroll-value (te-blob di)) (scroll-bar-setting scroll-bar)))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defmethod te-set-font ((di te-dim) font-spec &key (font-color *black-color*) (mode #$doAll))
- (with-focused-view (focusing-view di)
- (te-set-font (te-blob di) font-spec :font-color font-color :mode mode)))
-
- (defmethod te-selection ((di te-dim))
- (with-focused-view (focusing-view di)
- (te-selection (te-blob di))))
-
- (defmethod te-set-selection ((di te-dim) sel-start sel-end)
- (with-focused-view (focusing-view di)
- (te-set-selection (te-blob di) sel-start sel-end)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defmethod dialog-item-text ((di te-dim))
- (te-string (te-blob di)))
-
- (defmethod set-dialog-item-text ((di te-dim) string)
- (with-focused-dialog-item (di)
- (setf (te-string (te-blob di)) string)))
-
- (defmethod set-dialog-item-text :after ((di te-dim) string)
- (declare (ignore string))
- (invalidate-view di t))
-
- (defmethod te-string ((di te-dim))
- (dialog-item-text di))
-
- (defmethod (setf te-string) (string (di te-dim))
- (set-dialog-item-text di string))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- #|
-
- (open-res-file "oou:examples;examples.rsrc")
-
- (defclass te (te-dim dialog-item) ())
-
- (defparameter *test-w* (make-instance 'window
- :view-size #@(250 250)
- :view-subviews
- (list
- (make-instance 'scroll-bar-dialog-item
- :view-position #@(5 5)
- :view-size #@(16 100)
- :view-nick-name :vsb)
- (make-instance 'te
- :te-init-rsrc "example style text"
- :view-position #@(30 5)
- :view-size #@(200 100)
- :view-nick-name :te
- :allow-returns t
- :te-word-wrap-p t
- :te-v-scroll-bar :vsb
- )
- )))
-
-
-
- ;change the font attrs of current selection
- (te-set-font (view-named :te *test-w*)
- '("Times" 18 :shadow) :font-color *red-color*)
-
- (close-res-file "oou:examples;examples.rsrc")
-
-
- |#