Skip to content
Snippets Groups Projects
frames.lisp 59 KiB
Newer Older
cer's avatar
cer committed
;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Package: CLIM-INTERNALS; Base: 10; Lowercase: Yes -*-

cer's avatar
 
cer committed
;; $fiHeader: frames.lisp,v 1.54 92/12/07 12:14:15 cer Exp $
cer's avatar
cer committed

(in-package :clim-internals)

cer's avatar
cer committed
"Copyright (c) 1990, 1991, 1992 Symbolics, Inc.  All rights reserved.
cer's avatar
cer committed
 Portions copyright (c) 1991, 1992 Franz, Inc.  All rights reserved.
 Portions copyright (c) 1989, 1990 International Lisp Associates."
cer's avatar
cer committed

cer's avatar
cer committed
(define-protocol-class application-frame ())
cer's avatar
cer committed

cer's avatar
cer committed
(defclass standard-application-frame (application-frame)
cer's avatar
cer committed
    ((name :initarg :name :accessor frame-name)
     (pretty-name :initarg :pretty-name :accessor frame-pretty-name)
     (command-table :initarg :command-table 
		    :initform (find-command-table 'user-command-table)
		    :accessor frame-command-table)
     (disabled-commands :initarg :disabled-commands :initform nil)
     ;; One of  T, NIL, or a command-table; used by the menu-bar
     (menu-bar :initarg :menu-bar :initform nil)
     (histories :initform nil)
cer's avatar
cer committed
     (frame-manager :initform nil :accessor frame-manager)
cer's avatar
cer committed
     (calling-frame :reader frame-calling-frame :initarg :calling-frame)
     ;; PANES is the description of all of the named panes,
     ;; ALL-PANES is an alist that stores all of the named panes that
     ;; have actually been realized so far
     (panes :initarg :panes :accessor frame-panes)
     (all-panes :initform nil)
     (current-panes :initform nil :accessor frame-current-panes)
     (initialized-panes :initform nil)
     (pane-constructors :initarg :pane-constructors)
cer's avatar
cer committed
     (top-level-sheet :accessor frame-top-level-sheet
		      :initarg :top-level-sheet :initform nil)
cer's avatar
cer committed
     (state :initform :disowned :accessor frame-state 
	    :type (member :disowned :disabled :enabled :shrunk))
     (top-level :initarg :top-level  :accessor frame-top-level)
     (current-layout :initarg :default-layout :initform nil
		     :reader frame-current-layout)
     (geometry :initform nil :initarg :geometry :reader frame-geometry)
cer's avatar
cer committed
     (icon :initform nil :initarg :icon :reader frame-icon)
cer's avatar
cer committed
     (user-specified-position-p :initform :unspecified
				:initarg :user-specified-position-p
				:reader frame-user-specified-position-p)
     (user-specified-size-p :initform :unspecified
			    :initarg :user-specified-size-p
			    :reader frame-user-specified-size-p)
cer's avatar
cer committed
     (shell :accessor frame-shell)
     (pointer-documentation-p :initarg :pointer-documentation
			      :reader frame-pointer-documentation-p)
     (properties :initform nil :initarg :properties
		 :accessor frame-properties)
     (resizable :initarg :resize-frame
		:reader frame-resizable)
cer's avatar
cer committed
     (layout :initarg :layouts :reader frame-layouts)
cer's avatar
cer committed
     (top-level-process :initform nil)
cer's avatar
cer committed
     (command-queue :initform (make-locking-queue) :reader frame-command-queue)
cer's avatar
cer committed
     (input-buffer :initform nil :initarg :input-buffer :reader frame-input-buffer))
cer's avatar
cer committed
  (:default-initargs :pointer-documentation nil
cer's avatar
cer committed
		     :layouts nil
		     :resize-frame nil
		     :top-level 'default-frame-top-level))
cer's avatar
cer committed

(defmethod port ((frame standard-application-frame))
  (port (frame-manager frame)))

(defmethod graft ((frame standard-application-frame))
  (graft (frame-manager frame)))

cer's avatar
cer committed
;;--- These should really be somewhere else
(defmethod frame-manager ((stream standard-encapsulating-stream))
cer's avatar
cer committed
  (frame-manager (encapsulating-stream-stream stream)))
cer's avatar
cer committed

(defmethod frame-manager ((stream t))
cer's avatar
cer committed
  (cond (*application-frame* (or (frame-manager *application-frame*)
				 (find-frame-manager)))
cer's avatar
cer committed
	(t (find-frame-manager))))
cer's avatar
cer committed

cer's avatar
cer committed
(defmethod initialize-instance :after ((frame standard-application-frame) 
cer's avatar
cer committed
				       &rest args
				       &key frame-manager
cer's avatar
cer committed
					    geometry icon
					    (parent frame-manager parent-p))
cer's avatar
cer committed
  (declare (ignore args))
cer's avatar
cer committed
  (destructuring-bind (&key left top width height) geometry
    (declare (ignore left top width height)))
cer's avatar
cer committed
  (destructuring-bind (&key name pixmap clipping-mask) icon
cer's avatar
cer committed
    (declare (ignore name pixmap clipping-mask)))
cer's avatar
cer committed
  (let ((frame-manager
cer's avatar
cer committed
	  (etypecase parent
	    (null (unless parent-p (find-frame-manager)))
	    (list (apply #'find-frame-manager parent))
	    (frame-manager parent)
	    (application-frame (frame-manager parent))
	    (port (find-frame-manager :port parent))
 	    (graft (find-frame-manager :port (port parent)))
	    (sheet (frame-manager (pane-frame parent))))))
cer's avatar
cer committed
    (when frame-manager
      (adopt-frame frame-manager frame))))
cer's avatar
cer committed

cer's avatar
 
cer committed
(defmethod find-named-color (name (frame standard-application-frame) &key (errorp t))
  (find-named-color name (frame-palette frame) :errorp errorp))

(defmethod frame-palette ((frame standard-application-frame))
  (let ((framem (frame-manager frame)))
    (if framem
	(frame-manager-palette framem)
	(and (port frame)
	     (port-default-palette (port frame))))))

cer's avatar
cer committed
;; Default method does nothing
(defmethod generate-panes ((framem standard-frame-manager)
			   (frame standard-application-frame))
cer's avatar
cer committed
  (setf (frame-panes frame) nil))

cer's avatar
cer committed
(eval-when (compile load eval)
cer's avatar
cer committed
(defun define-application-frame-1 (name state-variables pane-descriptions
cer's avatar
cer committed
				   &key top-level layouts
cer's avatar
cer committed
					command-table disabled-commands)
  (let* ((command-table-name (first command-table)))
    ;; If we're going to be defining commands for this application frame,
    ;; make sure there's an command table lying around so that all other
    ;; code doesn't have to be defensive against its absence.
    (when command-table
      (apply #'define-command-table-1 command-table))))
)	;eval-when

cer's avatar
cer committed
(defmacro define-application-frame (name superclasses slots &rest options)
cer's avatar
cer committed
  #+Genera (declare (zwei:indentation 1 25 2 3 3 1))
  (with-warnings-for-definition name define-application-frame
cer's avatar
cer committed
    (let (pane panes layouts top-level menu-bar pointer-documentation
cer's avatar
cer committed
	  command-definer command-table disabled-commands
	  icon geometry default-initargs)
cer's avatar
cer committed
      (macrolet ((extract (name keyword default &optional (pair t))
		   `(let ((entry (assoc ',keyword options)))
		      (cond ((null entry)
			     (setq ,name ,default))
			    (t
			     ,@(if pair
				   `((assert (= (length entry) 2) (entry)
					     "The length of the option ~S must be 2" entry)
				     (setq ,name (second entry)
					   options (delete entry options)))
				   `((assert (listp (rest entry)) (entry)
					     "The remainder of ~S must be a list" entry)
				     (setq ,name (rest entry)
cer's avatar
cer committed
					   options (delete entry options)))))))))
cer's avatar
cer committed
	(extract pane :pane nil)
	(extract panes :panes nil nil)
cer's avatar
cer committed
	(extract layouts :layouts nil nil)
cer's avatar
cer committed
	(extract top-level :top-level '(default-frame-top-level))
	(extract menu-bar :menu-bar t)
cer's avatar
cer committed
	(extract pointer-documentation :pointer-documentation nil)
cer's avatar
cer committed
	(extract command-definer :command-definer t)
	(extract command-table :command-table t)
cer's avatar
cer committed
	(extract disabled-commands :disabled-commands nil)
	(extract default-initargs :default-initargs nil nil)
	(extract icon :icon nil nil)
	(extract geometry :geometry nil nil))
cer's avatar
cer committed
      (check-type name symbol)
      (check-type superclasses list)
      (check-type slots list)
      (check-type pane list)
      (check-type panes list)
cer's avatar
cer committed
      (check-type layouts list)
cer's avatar
cer committed
      (check-type top-level list)
      (check-type disabled-commands list)
      (when (and pane panes)
	(error "The ~S and ~S options cannot be used together" :pane :panes))
cer's avatar
cer committed
      (when (and layouts
		 (null pane)
		 (null panes))
	(error "You must use either ~S or ~S with ~S" :pane :panes :layouts))
cer's avatar
cer committed
      (when (null superclasses)
	(setq superclasses '(standard-application-frame)))
      (when (eq command-definer 't)
	(setq command-definer (fintern "~A-~A-~A" 'define name 'command)))
      (check-type command-definer symbol)
      (cond ((null command-table))
	    ((symbolp command-table)
	     (setq command-table (list command-table)))
	    (t (warn-if-command-table-invalid name command-table)))
      (when (eq (first command-table) 't)
	(setq command-table (list* name (rest command-table))))
    #-Silica (warn-if-pane-descriptions-invalid name pane-descriptions)
cer's avatar
cer committed
    #-Silica (warn-if-layouts-invalid name layouts pane-descriptions)
cer's avatar
cer committed
    (let ((pane-constructors 
cer's avatar
cer committed
	    (cond (panes
cer's avatar
cer committed
		   (compute-pane-constructor-code panes))
		  (pane
cer's avatar
cer committed
 		   (compute-pane-constructor-code `((,name ,pane))))))
 	  (layout-value
	    `(list ,@(mapcar
		       #'(lambda (layout)
			   (destructuring-bind (name panes &rest sizes) layout
			     (if sizes
				 `(list ',name ',panes 
					,@(mapcar #'(lambda (pane-and-size)
						      `(list ',(car pane-and-size)
							     ,@(cdr pane-and-size)))
						  sizes))
				 `',layout)))
		       layouts))))
cer's avatar
cer committed
      `(progn
	 (eval-when (compile)
	   (when ',command-table
	     (setf (compile-time-property ',(first command-table) 'command-table-name) t))
	   (define-application-frame-1 ',name ',slots ,pane-constructors
cer's avatar
cer committed
				       :layouts ,layout-value
cer's avatar
cer committed
				       :top-level ',top-level
				       :command-table ',command-table))
	 (define-group ,name define-application-frame
	   (defclass ,name ,superclasses ,slots
	     ,@options
	     (:default-initargs
cer's avatar
cer committed
	       :layouts ,layout-value
cer's avatar
cer committed
	       :menu-bar ',menu-bar
	       :pointer-documentation ',pointer-documentation
cer's avatar
cer committed
	       ,@(and command-table `(:command-table ',(car command-table)))
	       ,@(and top-level `(:top-level ',top-level))
cer's avatar
cer committed
	       ,@(and panes `(:pane-constructors ,pane-constructors))
cer's avatar
cer committed
	       ,@(and layouts `(:default-layout ',(caar layouts)))
cer's avatar
cer committed
	       ,@(and icon `(:icon (list ,@icon)))
	       ,@(and geometry `(:geometry (list ,@geometry)))
	       ,@default-initargs))
cer's avatar
cer committed
	   ,@(when command-definer
	       `((defmacro ,command-definer (command-name arguments &body body)
		   #+Genera (declare (zwei:indentation 1 3 2 1))
		   `(define-frame-command ,',(first command-table)
					  ,command-name ,arguments ,@body))
		 #+Genera (scl:defprop ,command-definer
				       define-command
				       zwei:definition-function-spec-type)
		 #+Genera (scl:defprop ,command-definer
				       remove-command
				       zwei:kill-definition)
		 #+Genera (scl:defprop ,command-definer
				       zwei:defselect-function-spec-finder
				       zwei:definition-function-spec-finder)))
	   ;;--- Need to handle DISABLED-COMMANDS properly, 
	   ;;--- which entails doing a COPY-LIST
	   (define-application-frame-1 ',name ',slots ,pane-constructors
cer's avatar
cer committed
				       :layouts ,layout-value
cer's avatar
cer committed
				       :top-level ',top-level
				       :command-table ',command-table
				       :disabled-commands ',disabled-commands)
	   #+Cloe-Runtime
	   (cloe:define-program ,name ()
	     :main ,name
	     :debugger-hook cloe-debugger-hook)
cer's avatar
 
cer committed
	   ,@(compute-generate-panes-code name pane panes layouts)
	   ',name))))))
cer's avatar
cer committed

cer's avatar
cer committed
#+Genera
(scl:defprop define-application-frame "CLIM Application Frame" si:definition-type-name)

;; For now each application frame has its own command table named after the application
cer's avatar
cer committed
(defmacro define-frame-command (command-table-name name-and-options arguments &body body)
cer's avatar
cer committed
  #+Genera (declare (zwei:indentation 2 3 3 1))
cer's avatar
cer committed
  (multiple-value-bind (command-name command-options)
      (decode-name-and-options name-and-options command-table-name)
    `(define-command (,command-name :command-table ,command-table-name ,@command-options)
cer's avatar
cer committed
	 ,arguments
       ,@body)))

#+Genera
(scl::defprop define-frame-command define-command zwei:definition-function-spec-type)
#+Genera
(scl:defun (:property define-frame-command zwei:definition-function-spec-finder) (bp)
  (zwei:defselect-function-spec-finder (zwei:forward-sexp bp 1 t)))
cer's avatar
cer committed

cer's avatar
cer committed
(defun warn-if-command-table-invalid (frame-name command-table)
  (cond ((not (and (listp command-table)
		   (symbolp (first command-table))
		   (oddp (length command-table))))
	 (warn "The ~S option for frame ~S, ~S, is invalid.~@
		It is supposed to be a list of a command table name followed by ~
		keyword/value pairs."
	       :command-table frame-name command-table))
	(t
	 (do* ((options (cdr command-table) (cddr options))
	       (keyword (first options) (first options))
	       (value (second options) (second options)))
	      ((null options))
	   (case keyword
	     (:inherit-from
	       (if (listp value)
		   (dolist (item value)
		     (unless (typep item '(or symbol command-table))
		       (warn "The ~S keyword in the ~S option for frame ~S~@
			      is followed by a list containing ~S, which is not a ~
			      command table nor a command table name."
			     :inherit-from :command-table frame-name item)))
		   (warn "The ~S keyword in the ~S option for frame ~S~@
			  is followed by ~S, which is not a list of command tables ~
			  or command table names."
			 :inherit-from :command-table frame-name value)))
	     (:menu
	       (if (listp value)
		   (dolist (clause value)
		     (unless (and (listp clause)
				  (>= (length clause) 3)
				  (oddp (length clause))
				  (stringp (first clause))
				  (member (second clause)
					  '(:command :function :menu :divider)))
		       (warn "The ~S keyword in the ~S option for frame ~S~@
			      is followed by a list of menu clauses containing~@
			      an invalid clause ~S."
			     :menu :command-table frame-name clause)))
		   (warn "The ~S keyword in the ~S option for frame ~S~@
			  is followed by ~S, which is not a list of menu clauses."
			 :menu :command-table frame-name value)))
cer's avatar
cer committed
	     (:inherit-menu)
cer's avatar
cer committed
	     (otherwise
	       (warn "The keyword ~S in the ~S option for frame ~S is invalid.~@
		      The valid keywords are ~S and ~S."
		     keyword :command-table frame-name :inherit-from :menu))))))) 
cer's avatar
cer committed

cer's avatar
cer committed
(defun compute-generate-panes-code (name code panes layouts)
cer's avatar
cer committed
  (if layouts
cer's avatar
cer committed
      (compute-complex-generate-panes-code name panes layouts)
cer's avatar
cer committed
      (compute-simple-generate-panes-code name code)))
cer's avatar
cer committed

(defun compute-simple-generate-panes-code (name code)
cer's avatar
cer committed
  (and code
cer's avatar
cer committed
       (let ((frame '#:frame)
	     (framem '#:framem))
	 `((defmethod generate-panes ((,framem standard-frame-manager) (,frame ,name))
	     (let ((*application-frame* ,frame))
	       (setf (frame-panes ,frame)
		     (frame-wrapper ,framem ,frame
		       (with-look-and-feel-realization (,framem ,frame)
			 ,code)))))))))
cer's avatar
cer committed

cer's avatar
cer committed
(defun compute-complex-generate-panes-code (name panes layouts)
cer's avatar
 
cer committed
  (check-type layouts list)
cer's avatar
cer committed
  (let ((frame '#:frame)
	(framem '#:framem))
    `((defmethod generate-panes ((,framem standard-frame-manager) (,frame ,name))
cer's avatar
cer committed
	(symbol-macrolet
cer's avatar
cer committed
	  ,(mapcar #'(lambda (pane-spec)
cer's avatar
cer committed
		       (destructuring-bind (name code &rest ignore) pane-spec
			 (declare (ignore code ignore))
cer's avatar
cer committed
			 `(,name (find-or-make-pane-named ,frame ',name))))
		   panes)
	  (let ((*application-frame* ,frame))
	    (setf (frame-panes ,frame)
		  (frame-wrapper ,framem ,frame
		    (with-look-and-feel-realization (,framem ,frame)
		      (ecase (frame-current-layout ,frame)
cer's avatar
cer committed
			,@(mapcar 
			    #'(lambda (layout-spec)
				(destructuring-bind (name panes &rest ignore) layout-spec
				  (declare (ignore ignore))
				  `(,name ,panes)))
			    layouts)))))))))))
cer's avatar
cer committed

cer's avatar
cer committed
(defun compute-pane-constructor-code (panes)
cer's avatar
 
cer committed
  (check-type panes list)
cer's avatar
cer committed
  `(list ,@(mapcar #'(lambda (pane-spec)
cer's avatar
cer committed
		       (destructuring-bind (name code &rest options) pane-spec
			 (setq code (canonicalize-pane-spec name code options))
cer's avatar
cer committed
			 `(list ',name
				#'(lambda (frame framem)
				    (with-look-and-feel-realization (framem frame)
				      ,code)))))
		   panes)))
cer's avatar
cer committed
   
cer's avatar
cer committed
(defun canonicalize-pane-spec (name code rest)
  (cond ((symbolp code)
cer's avatar
cer committed
	 (unless (getf rest :name)
	   (setf (getf rest :name) `',name))
	 (apply #'find-pane-class-constructor code rest))
cer's avatar
cer committed
	((null rest) code)
cer's avatar
cer committed
	(t
	 (error "Invalid pane specification: ~S"
		(list* name code rest)))))

cer's avatar
cer committed
(defmethod find-pane-class-constructor ((type t) &rest options)
cer's avatar
cer committed
  (declare (dynamic-extent options))
cer's avatar
cer committed
  (error "Unknown pane type ~S with options ~S" type options))

cer's avatar
cer committed

;;--- Extend this to default the options from the lambda list
cer's avatar
cer committed
(defmacro define-pane-type (type lambda-list &body body)
  `(defmethod find-pane-class-constructor ((type (eql ',type)) ,@lambda-list)
     ,@body))

cer's avatar
cer committed
(define-pane-type :title (&rest options)
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-clim-stream-pane
     :type 'title-pane
cer's avatar
cer committed
     ,@options
cer's avatar
cer committed
     :display-function 'display-title
cer's avatar
cer committed
     :display-after-commands nil
cer's avatar
cer committed
     :text-style (parse-text-style '(:sans-serif :bold :large))
     :scroll-bars nil
cer's avatar
cer committed
     :width :compute :height :compute
cer's avatar
cer committed
     :end-of-page-action :allow 
     :end-of-line-action :allow))
cer's avatar
cer committed

cer's avatar
cer committed
(define-pane-type :command-menu (&rest options)
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-clim-stream-pane
     :type 'command-menu-pane
cer's avatar
cer committed
     ,@options
     :display-function `(display-command-menu :command-table ,(frame-command-table frame))
     :incremental-redisplay t
     :display-after-commands t
     :text-style *command-table-menu-text-style*
cer's avatar
cer committed
     :scroll-bars nil
cer's avatar
cer committed
     :width :compute :height :compute
cer's avatar
cer committed
     :end-of-page-action :allow 
     :end-of-line-action :allow))
cer's avatar
cer committed

cer's avatar
cer committed
(define-pane-type :interactor (&rest options &key (scroll-bars :vertical))
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-clim-interactor-pane
     ,@options
     :scroll-bars ,scroll-bars))
cer's avatar
cer committed

cer's avatar
cer committed
(define-pane-type :application (&rest options &key (scroll-bars :both))
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-clim-application-pane 
     ,@options
     :scroll-bars ,scroll-bars))
cer's avatar
cer committed

cer's avatar
cer committed
(define-pane-type :accept-values (&rest options &key (scroll-bars :vertical))
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-clim-stream-pane 
     :type 'accept-values-pane
     ,@options
cer's avatar
cer committed
     :display-after-commands :no-clear
cer's avatar
cer committed
     :scroll-bars ,scroll-bars
cer's avatar
cer committed
     :width :compute :height :compute
cer's avatar
cer committed
     :end-of-page-action :allow
     :end-of-line-action :allow))

cer's avatar
cer committed
(define-pane-type :pointer-documentation (&rest options)
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-clim-stream-pane
     :type 'pointer-documentation-pane
     ,@options
     :display-after-commands nil
     :text-style (parse-text-style '(:sans-serif :bold :normal))
     :scroll-bars nil
cer's avatar
cer committed
     :end-of-page-action :allow
cer's avatar
cer committed
     :end-of-line-action :allow))
cer's avatar
cer committed

cer's avatar
cer committed
(define-pane-type scroll-bar (&rest options)
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-pane 'scroll-bar ,@options))

cer's avatar
cer committed
(define-pane-type slider (&rest options)
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-pane 'slider ,@options))

cer's avatar
cer committed
(define-pane-type push-button (&rest options)
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-pane 'push-button ,@options))

cer's avatar
cer committed
(define-pane-type label-pane (&rest options)
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-pane 'label-pane ,@options))

cer's avatar
cer committed
(define-pane-type text-field (&rest options)
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-pane 'text-field ,@options))

cer's avatar
cer committed
(define-pane-type text-editor (&rest options)
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-pane 'text-editor ,@options))
cer's avatar
cer committed

cer's avatar
cer committed
(define-pane-type toggle-button (&rest options)
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-pane 'toggle-button ,@options))

cer's avatar
cer committed
(define-pane-type radio-box (&rest options)
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-pane 'radio-box ,@options))

cer's avatar
cer committed
(define-pane-type check-box (&rest options)
  (declare (non-dynamic-extent options))
  `(make-pane 'check-box ,@options))

(define-pane-type list-pane (&rest options)
  (declare (non-dynamic-extent options))
  `(make-pane 'list-pane ,@options))

(define-pane-type option-pane (&rest options)
  (declare (non-dynamic-extent options))
  `(make-pane 'option-pane ,@options))

cer's avatar
cer committed
(define-pane-type menu-bar (&rest options)
cer's avatar
cer committed
  (declare (non-dynamic-extent options))
cer's avatar
cer committed
  `(make-pane 'menu-bar ,@options))
cer's avatar
cer committed

cer's avatar
cer committed

cer's avatar
cer committed
(defmethod find-or-make-pane-named ((frame standard-application-frame) name)
  (with-slots (all-panes pane-constructors) frame
    (second (or (assoc name all-panes)
cer's avatar
cer committed
		(let ((new-pane
			(list name 
			      (funcall (second (assoc name pane-constructors))
				       frame (frame-manager frame)))))
cer's avatar
cer committed
		  ;; Maintain ALL-PANES in the order the panes are created
		  (setq all-panes (nconc all-panes (list new-pane)))
cer's avatar
cer committed
		  new-pane)))))
cer's avatar
cer committed
 
cer's avatar
cer committed
(defmethod layout-frame ((frame standard-application-frame) &optional width height)
cer's avatar
cer committed
  (let ((panes (frame-panes frame))
	(*application-frame* frame))
cer's avatar
cer committed
    (when panes
cer's avatar
cer committed
      (clear-space-requirement-caches-in-tree panes)
cer's avatar
cer committed
      (multiple-value-bind (graft-width graft-height) 
	  (bounding-rectangle-size (graft frame))
	(cond ((and width height)
	       (minf width graft-width) 
	       (minf height graft-height))
	      (t
	       (let ((sr (compose-space panes)))
		 (setq width  (or width (space-requirement-width sr))
		       height (or height (space-requirement-height sr)))
		 ;;--- This fudge factor stuff looks dubious  --SWM
		 (let ((fudge-factor #+Allegro 0.9 #-Allegro 1))
		   (minf-or width (* graft-width fudge-factor))
		   (minf-or height (* graft-height fudge-factor)))))))
cer's avatar
cer committed
      ;;--- Don't bother with this if the size didn't change?
cer's avatar
cer committed
      (let ((top-sheet (or (frame-top-level-sheet frame) panes)))
	(if (and (sheet-enabled-p top-sheet)
cer's avatar
cer committed
		 (not (frame-resizable frame)))
cer's avatar
cer committed
	    (multiple-value-call #'allocate-space 
cer's avatar
cer committed
	      top-sheet (bounding-rectangle-size top-sheet)) 
	    (resize-sheet top-sheet width height))))))
cer's avatar
cer committed

cer's avatar
cer committed
(defvar *frame-layout-changing-p* nil)

cer's avatar
cer committed
(defmethod (setf frame-current-layout) (layout (frame standard-application-frame))
cer's avatar
cer committed
  (unless (or (eq (frame-current-layout frame) layout)
	      (null (frame-top-level-sheet frame)))
cer's avatar
cer committed
    (setq *frame-layout-changing-p* t)
cer's avatar
cer committed
    (setf (slot-value frame 'current-layout) layout)
cer's avatar
cer committed
    ;; First disown all the children
    (dolist (name-and-pane (slot-value frame 'all-panes))
      (let ((sheet (second name-and-pane)))
	(when (sheet-parent sheet)
	  (sheet-disown-child (sheet-parent sheet) sheet))))
    (dolist (child (sheet-children (frame-top-level-sheet frame)))
      (sheet-disown-child (frame-top-level-sheet frame) child))
    ;; Now we want to give it some new ones
    (generate-panes (frame-manager frame) frame)
    (sheet-adopt-child (frame-top-level-sheet frame) (frame-panes frame))
    (multiple-value-call #'layout-frame
      frame (bounding-rectangle-size (frame-top-level-sheet frame)))
cer's avatar
cer committed
    (let ((layout-space-requirements 
cer's avatar
cer committed
	    (cddr (assoc layout (frame-layouts frame)))))
cer's avatar
cer committed
      (changing-space-requirements (:layout nil)
cer's avatar
cer committed
	(flet ((adjust-layout (sheet)
		 (change-space-requirements-to-default sheet)
		 (let ((x (and (panep sheet)
			       (assoc (pane-name sheet) layout-space-requirements))))
		   (when x (apply #'change-space-requirements sheet (cdr x))))))
	  (declare (dynamic-extent #'adjust-layout))
	  (map-over-sheets #'adjust-layout (frame-top-level-sheet frame)))))
cer's avatar
cer committed
    ;;--- Don't throw, just recompute stream bindings in a principled way
cer's avatar
cer committed
    (handler-case
        (throw 'layout-changed nil)
cer's avatar
cer committed
      (error () nil))))
cer's avatar
cer committed

cer's avatar
cer committed
(defmethod frame-all-layouts ((frame standard-application-frame))
  (mapcar #'first (frame-layouts frame)))
		 
cer's avatar
cer committed
#+Genera (zwei:defindentation (make-application-frame 1 1))
cer's avatar
cer committed
(defun make-application-frame (frame-name &rest options 
			       &key frame-class
				    enable pretty-name
cer's avatar
cer committed
			            left top right bottom width height
cer's avatar
cer committed
				    (user-specified-position-p :unspecified)
cer's avatar
cer committed
 				    (user-specified-size-p :unspecified)
				    save-under
cer's avatar
cer committed
			       &allow-other-keys)
  (declare (dynamic-extent options))
  (check-type pretty-name (or null string))
  (when (null frame-class)
    (setq frame-class frame-name))
cer's avatar
cer committed
  (when (or left top right bottom width height)
cer's avatar
cer committed
    (when (getf options :geometry)
cer's avatar
cer committed
      (error "Cannot specify ~S and ~S, S, ~S, ~S, ~S, or ~S at the same time"
	     :geometry :left :top :right :bottom :width :height))
    (macrolet ((check-conflict (edge1 edge2 size)
		 `(cond 
cer's avatar
cer committed
		    ((and ,edge1 ,size)
		     (if ,edge2
			 (error "Cannot specify ~S, ~S, and ~S together" ,edge1 ,size ,edge2)
			 (setq ,edge2 (+ ,edge1 ,size))))
		    ((and ,edge2 ,size)
		     (if ,edge1
			 (error "Cannot specify ~S, ~S, and ~S together" ,edge2 ,size ,edge1)
			 (setq ,edge1 (+ ,edge2 ,size))))
		    ((and ,edge2 ,edge1)
		     (if ,size
			 (error "Cannot specify ~S, ~S, and ~S together" ,edge2 ,edge1 ,size)
			 (setq ,size (- ,edge2 ,edge1)))))))
cer's avatar
cer committed
      (check-conflict left right width)
      (check-conflict top bottom height))
cer's avatar
cer committed
    (setf (getf options :geometry)
cer's avatar
cer committed
	  (append (and left `(:left ,left))
		  (and top `(:top ,top))
cer's avatar
cer committed
		  (and width `(:width ,width))
		  (and height `(:height ,height)))))
cer's avatar
cer committed
  (let ((geometry (getf options :geometry)))
cer's avatar
cer committed
    (when (and (eq user-specified-position-p :unspecified)
	       (or (and (getf geometry :left)
			(getf geometry :top))
		   (and left top)))
      (setf user-specified-position-p t))
    (when (and (eq user-specified-size-p :unspecified)
	       (or (and (getf geometry :width)
			(getf geometry :height))
		   (and width height)))
      (setf user-specified-size-p t)))
cer's avatar
cer committed
  (with-keywords-removed (options options 
cer's avatar
cer committed
			  '(:frame-class :pretty-name :enable :save-under
cer's avatar
cer committed
			    :left :top :right :bottom :width :height
cer's avatar
cer committed
			    :user-specified-position-p :user-specified-size-p))
cer's avatar
cer committed
    (let ((frame (apply #'make-instance
			frame-class
			:name frame-name
			;;--- Perhaps this should be a default-initarg?
			:pretty-name (or pretty-name
					 (title-capitalize (string frame-name)))
			:properties `(:save-under ,save-under)
			:user-specified-size-p user-specified-size-p
			:user-specified-position-p user-specified-position-p
			options)))
      (when enable 
	(enable-frame frame))
      frame)))

cer's avatar
cer committed
;; Create an application frame of the specified type if one does not already
;; exist, and then run it, possibly in its own process.  If one already exists,
;; just select it.
(defun find-application-frame (frame-name &rest options
			       &key (create t) (activate t) 
				    (own-process *multiprocessing-p*)
				    frame-class)
  (declare (dynamic-extent options))
  (when (null frame-class)
    (setq frame-class frame-name))
  (let ((frame
	  (block find-frame
	    (map-over-frames #'(lambda (frame)
				 (when (typep frame frame-class)
				   (return-from find-frame frame)))))))
    (when (or (eq create :force)
	      (and (null frame)
		   (eq create t)))
      (with-keywords-removed (options options '(:create :activate :own-process))
	(setq frame (apply #'make-application-frame frame-name options))))
    (when (and frame activate)
      (cond ((slot-value frame 'top-level-process)
	     (raise-frame frame))
	    (own-process
	     (make-process #'(lambda () (run-frame-top-level frame))
			   :name (frame-pretty-name frame)))
	    (t
	     (run-frame-top-level frame))))
    frame))

cer's avatar
cer committed
(defmethod enable-frame ((frame standard-application-frame))
cer's avatar
cer committed
  (unless (frame-manager frame)
cer's avatar
cer committed
    (error "Cannot enable a disowned frame ~S" frame))
cer's avatar
cer committed
  (destructuring-bind (&key left top width height &allow-other-keys)
cer's avatar
cer committed
      (frame-geometry frame)
cer's avatar
cer committed
    (ecase (frame-state frame)
      (:enabled)
      ((:disabled :disowned)
cer's avatar
cer committed
       (let ((old-state (frame-state frame)))
cer's avatar
cer committed
	 (setf (frame-state frame) :enabled)
cer's avatar
cer committed
	 ;; If this is a new frame then if the user specified a width
cer's avatar
cer committed
	 ;; then we should be using that
cer's avatar
cer committed
	 ;; If the frame already exists then we probably should be using
cer's avatar
cer committed
	 ;; the top level sheet size
cer's avatar
cer committed
	 (multiple-value-bind (width height)
	     (ecase old-state
cer's avatar
cer committed
	       (:disowned 
cer's avatar
cer committed
		 (if (and width height)
		     (values width height)
		     (values)))
cer's avatar
cer committed
	       (:disabled
cer's avatar
cer committed
		 (bounding-rectangle-size
		   (frame-top-level-sheet frame))))  
	   (layout-frame frame width height)
cer's avatar
cer committed
	   (when (and left top)
	     (move-sheet (frame-top-level-sheet frame) left top))
cer's avatar
cer committed
	   (note-frame-enabled (frame-manager frame) frame))))
      (:shrunk 
        (note-frame-deiconified (frame-manager frame) frame)))))
cer's avatar
cer committed

cer's avatar
cer committed
(defmethod iconify-frame ((frame standard-application-frame))
  (note-frame-iconified (frame-manager frame) frame))

cer's avatar
cer committed
(defmethod destroy-frame ((frame standard-application-frame))
  (when (eq (frame-state frame) :enabled)
    (disable-frame frame))
  (disown-frame (frame-manager frame) frame))

cer's avatar
cer committed
(defmethod disable-frame ((frame standard-application-frame))
cer's avatar
cer committed
  (ecase (frame-state frame)
cer's avatar
cer committed
    ((:disowned :disabled))
cer's avatar
cer committed
    ((:enabled :shrunk)
     (setf (frame-state frame) :disabled)
     (note-frame-disabled (frame-manager frame) frame))))

cer's avatar
cer committed
(defmethod reset-frame ((frame standard-application-frame) &rest ignore)
  (declare (ignore ignore))
  nil)
cer's avatar
cer committed

cer's avatar
cer committed
(defmethod raise-frame ((frame standard-application-frame))
  (raise-sheet (frame-top-level-sheet frame)))

(defmethod bury-frame ((frame standard-application-frame))
  (bury-sheet (frame-top-level-sheet frame)))

(defmethod note-frame-enabled 
	   ((framem standard-frame-manager) (frame standard-application-frame))
  )

(defmethod note-frame-disabled 
	   ((framem standard-frame-manager) (frame standard-application-frame))
  )

(defmethod note-frame-iconified 
	   ((framem standard-frame-manager) (frame standard-application-frame))
cer's avatar
cer committed
  )

cer's avatar
cer committed
(defmethod note-frame-deiconified
	   ((framem standard-frame-manager) (frame standard-application-frame))
cer's avatar
cer committed
  )
cer's avatar
cer committed

cer's avatar
cer committed

cer's avatar
cer committed
(defmethod port-note-frame-adopted ((port basic-port) (frame standard-application-frame))
  nil)


cer's avatar
cer committed
(defmethod (setf frame-pretty-name) :before (name (frame standard-application-frame))
  (check-type name string))
cer's avatar
cer committed
(defmethod (setf frame-pretty-name) :after (name (frame standard-application-frame))
  (declare (ignore name))
  (let ((framem (frame-manager frame)))
    (when framem (frame-manager-note-pretty-name-changed framem frame))))

(defmethod frame-manager-note-pretty-name-changed ((framem standard-frame-manager) 
						   (frame standard-application-frame))
  nil)

cer's avatar
cer committed

(eval-when (#-Allegro compile load eval)
cer's avatar
 
cer committed
(define-condition frame-exit (condition)
  ((frame :initarg :frame :reader frame-exit-frame))
  (:report (lambda (condition stream)
	     (format stream "Exit from frame ~A" (frame-exit-frame condition)))))
cer's avatar
cer committed
)	;eval-when
cer's avatar
cer committed
(defgeneric run-frame-top-level (frame &key &allow-other-keys))

cer's avatar
cer committed
;;--- It would be nice to have the CLIM 0.9 START-FRAME and STOP-FRAME functions
cer's avatar
cer committed
(defmethod run-frame-top-level :around ((frame standard-application-frame) &key)
cer's avatar
cer committed
  (with-simple-restart (nil "Exit ~A" (frame-pretty-name frame))
    (handler-bind ((frame-exit
		     #'(lambda (condition)
			 (let ((exit-frame (frame-exit-frame condition)))
			   (when (eq frame exit-frame)
			     (return-from run-frame-top-level nil))))))
      (unwind-protect
	  (let (;; Reset the state of the input editor and the presentation
		;; type system, etc., in case there is an entry into another
		;; application from inside the input editor, such as a Debugger
		;; written using CLIM.
		;;--- This should be done in a more modular way
		;;--- If you change this, change MENU-CHOOSE-FROM-DRAWER
		(*original-stream* nil)
		(*input-wait-test* nil)
		(*input-wait-handler* nil)
		(*pointer-button-press-handler* nil)
		(*numeric-argument* nil)
		(*delimiter-gestures* nil)
		(*activation-gestures* nil)
		(*accelerator-gestures* nil)
		(*accelerator-numeric-argument* nil)
		(*input-context* nil)
		(*accept-help* nil)
		(*assume-all-commands-enabled* nil)
		(*sizing-application-frame* nil)
		(*frame-layout-changing-p* *frame-layout-changing-p*)
		(*command-parser* 'command-line-command-parser)
		(*command-unparser* 'command-line-command-unparser)
		(*partial-command-parser*
		  'command-line-read-remaining-arguments-for-partial-command) 
		(*application-frame* frame))
	    (with-frame-manager ((frame-manager frame))
	      (loop
		(with-simple-restart (nil "~A top level" (frame-pretty-name frame))
		  (loop
		    (catch 'layout-changed
		      (let ((*application-frame* frame))
			;; We must return the values from CALL-NEXT-METHOD,
			;; or else ACCEPTING-VALUES will return NIL
			#-CCL-2
			(return-from run-frame-top-level (call-next-method))
			;; The (RETURN-FROM FOO (CALL-NEXT-METHOD)) form above
			;; doesn't work in Coral.  If the "top level" restart
			;; above is taken, the CALL-NEXT-METHOD form blows out
			;; the second time through this code, claiming that it
			;; can't find the next method.  Hoisting the
			;; CALL-NEXT-METHOD out of the RETURN-FROM form seems
			;; to fix it...  So it conses, big deal.
			#+CCL-2
			(let ((results (multiple-value-list (call-next-method))))
			  (return-from run-frame-top-level (values-list results))))))))))
	;; We disable the frame here, but it is the responsibility of the
	;; top-level function to enable the frame.  For example, if we
	;; called ENABLE-FRAME here, ACCEPTING-VALUES would disable the
	;; wrong frame.  Sigh.
	(queue-flush (frame-command-queue frame))
	(queue-flush (sheet-event-queue (frame-top-level-sheet frame)))
	(disable-frame frame)))))
cer's avatar
cer committed

cer's avatar
cer committed
(defmethod run-frame-top-level ((frame standard-application-frame) &rest args)
  (declare (dynamic-extent args))
cer's avatar
cer committed
  (with-slots (top-level-process) frame
    (when top-level-process
      (cerror "Bludgeon ahead, assuming the risk"
	      "The process ~S is already running the top-level function for frame ~S"
	      top-level-process frame))
    (unwind-protect
cer's avatar
cer committed
	(let* ((top-level (frame-top-level frame))
	       (tl-function (if (listp top-level) (first top-level) top-level))
	       (tl-args (if (listp top-level) (rest top-level) nil)))
cer's avatar
cer committed
	  (setq top-level-process (current-process))
cer's avatar
cer committed
	  ;; Cons as little as possible
	  (cond ((and (null args) (null tl-args))
		 (funcall tl-function frame))
		((null tl-args)
		 (apply tl-function frame args))
		((null args)
		 (apply tl-function frame tl-args))
		(t
		 (apply tl-function frame (append args tl-args)))))
cer's avatar
cer committed
      (setq top-level-process nil))))
cer's avatar
cer committed

(defmethod default-frame-top-level ((frame standard-application-frame)
				    &key command-parser command-unparser
					 partial-command-parser
cer's avatar
cer committed
					 (prompt "Command: "))
cer's avatar
cer committed
  ;; Enable the frame now
cer's avatar
cer committed
  (unless (eq (frame-state frame) :enabled)
    (enable-frame frame))
cer's avatar
cer committed
  (loop
cer's avatar
cer committed
    (let* ((*standard-output*
cer's avatar
cer committed
	     (or (frame-standard-output frame) *standard-output*))
	   (*standard-input* 
	     (or (frame-standard-input frame) *standard-output*))
	   (*query-io* 
	     (or (frame-query-io frame) *standard-input*))
	   (*error-output* 
	     (or (frame-error-output frame) *standard-output*))
	   (*pointer-documentation-output*
	     (frame-pointer-documentation-output frame))
	   (interactor
	     (not (null (find-frame-pane-of-type frame 'interactor-pane))))
	   (*command-parser*
	     (or command-parser
		 (if interactor
		     #'command-line-command-parser
		     #'menu-command-parser)))
	   (*command-unparser*
	     (or command-unparser 
		 #'command-line-command-unparser))
	   (*partial-command-parser* 
	     (or partial-command-parser
		 (if interactor
		     #'command-line-read-remaining-arguments-for-partial-command
		     #'menu-read-remaining-arguments-for-partial-command)))
	   (command-stream
	     ;;--- We have to ask the frame since we do not want to
	     ;;--- just pick up a stream from the dynamic environment
	     (let ((si (or (frame-standard-input frame)
			   (frame-standard-output frame))))
	       ;;--- I'm not really convinced that this is right  --SWM
	       (typecase si
		 (output-protocol-mixin si)
		 (t (frame-top-level-sheet frame))))))
      #+Allegro
      (unless (typep *standard-input* 'excl::bidirectional-terminal-stream)
	(assert (port *standard-input*)))
      #+Allegro
      (unless (typep *standard-output* 'excl::bidirectional-terminal-stream)
	(assert (port *standard-output*)))
      #+Allegro
      (unless (typep *query-io* 'excl::bidirectional-terminal-stream)
	(assert (port *query-io*)))
      ;; The read-eval-print loop for applications...
      (loop
	;; Redisplay all the panes
	(catch-abort-gestures ("Return to ~A command level" (frame-pretty-name frame))
	  (redisplay-frame-panes frame)
	  (when interactor
	    (fresh-line *standard-input*)
	    (if (stringp prompt)
		(write-string prompt *standard-input*)
		(funcall prompt *standard-input* frame)))
	  (let ((command (read-frame-command frame :stream command-stream)))
cer's avatar
cer committed
	    (when interactor
cer's avatar
cer committed
	      (terpri *standard-input*))
	    ;; Need this check in case the user aborted out of a command menu
	    (when command
	      (execute-frame-command frame command))))))))
cer's avatar
cer committed

cer's avatar
cer committed
;; Generic because someone might want :BEFORE or :AFTER
(defmethod frame-exit ((frame standard-application-frame))
cer's avatar
 
cer committed
  (signal 'frame-exit :frame frame))
cer's avatar
cer committed

cer's avatar
cer committed

;;; Sizing and moving of frames

;; Sizes an application frame based on the size of the contents of the 
;; output recording stream STREAM.
(defun size-frame-from-contents (stream 
				  &key width height
				       (right-margin 10) (bottom-margin 10)
				       (size-setter #'window-set-inside-size))
  (with-slots (output-record) stream
    (with-bounding-rectangle* (left top right bottom) output-record
      (let* ((graft (or (graft stream)
			(find-graft)))		;--- is this right?
	     (gw (bounding-rectangle-width (sheet-region graft)))
	     (gh (bounding-rectangle-height (sheet-region graft)))
	     ;;--- Does this need to account for the size of window decorations?
	     (width (min gw (+ (or width (- right left)) right-margin)))
	     (height (min gh (+ (or height (- bottom top)) bottom-margin))))
	;; The size-setter will typically resize the entire frame
	(funcall size-setter stream width height)
	(window-set-viewport-position stream left top)))))

;; Moves the sheet to the specified position, taking care not to move
;; it outside of the graft.  It's safest to use this on a top-level sheet.
(defun position-sheet-carefully (sheet x y)
  (multiple-value-bind (width height) (bounding-rectangle-size sheet)
    (multiple-value-bind (graft-width graft-height)
	(bounding-rectangle-size (or (graft sheet) (find-graft)))
      (let* ((left x)
	     (top y)
	     (right (+ left width))
	     (bottom (+ top height)))
	(when (> right graft-width)
	  (setq left (- graft-width width)))
	(when (> bottom graft-height)
	  (setq top (- graft-height height)))
cer's avatar
 
cer committed
	(port-move-frame (port sheet) (pane-frame sheet) (max 0 left) (max 0 top))))))

(defmethod port-move-frame ((port basic-port) frame x y)
  (move-sheet (frame-top-level-sheet frame) x y))
cer's avatar
cer committed

;; Moves the sheet to be near where the pointer is.  It's safest to use this
;; on a top-level sheet.
(defun position-sheet-near-pointer (sheet &optional x y)
  (unless (and x y)
    (multiple-value-setq (x y)
cer's avatar
cer committed
      (pointer-native-position (port-pointer (port sheet)))))
cer's avatar
cer committed
  (position-sheet-carefully sheet x y))

cer's avatar
cer committed

cer's avatar
cer committed
(defun display-title (frame stream &key max-width max-height)
  (declare (ignore max-width max-height))
  (with-slots (display-string) stream
    (let ((title display-string))
      (when (and (stringp title)
		 (not (string-equal title (frame-pretty-name frame))))
	;; On some hosts this will update the title bar 
	(setf (frame-pretty-name frame) title))
      (multiple-value-bind (width height)
	  (window-inside-size stream)
	(draw-text* stream (or title (frame-pretty-name frame))
		    (/ width 2) (/ height 2)
		    :align-x :center :align-y :center)))))
cer's avatar
cer committed

cer's avatar
cer committed
(defun title-capitalize (string)
  (let ((new-string (substitute #\Space #\- string)))
    (when (eq new-string string)
      (setq new-string (copy-seq new-string)))
    (nstring-capitalize new-string)))

cer's avatar
cer committed
(defun display-command-menu (frame stream &rest keys
cer's avatar
cer committed
			     &key command-table max-width max-height &allow-other-keys)
  (declare (dynamic-extent keys)