(in-package "CLIM-USER")

;;; Toolbox gimmick

;;; DEFINE-PRESENTATION-METHOD is annoyingly verbose!
(defmacro defpresent (type (object stream view-type) &body body)
  (let ((view (gensym)))
   `(define-presentation-method present
        (,object (type ,type) ,stream (,view ,view-type) &key)
      ,@body)))


(defgeneric world-toolset (object)
  (:documentation "Tools available for the object.")
  (:method (object) nil))

(defclass toolbox-item ()
  ((tool-symbol :initarg :symbol :accessor tool-symbol)
   (tool-active :initarg :active :accessor tool-active)
   (tool-action :initarg :action :accessor tool-action))
  (:default-initargs :symbol "?"
                     :active nil
                     :action (constantly nil)))

(defparameter *draw-grid* nil)
(defparameter *toolbox-item/grid*
  (make-instance 'toolbox-item :symbol :grid
                               :active *draw-grid*
                               :action (lambda (tool)
                                         (setf *draw-grid* (not *draw-grid*))
                                         (setf (tool-active tool) *draw-grid*))))

;;; CLIM part
(defclass box-view (gadget-view) ())
(defvar +box-view+ (make-instance 'box-view) "Toolbox view.")

(define-command-table toolbox-table :inherit-from nil)

(defpresent toolbox-item (object stream box-view)
  (draw-rectangle* stream 0 0 50 50 :ink (if (tool-active object)
                                             +light-yellow+
                                             +light-slate-grey+) )
  (with-drawing-options (stream :clipping-region (make-rectangle* 0 0 50 50))
    (typecase (tool-symbol object)
      ((eql :image)
       (draw-rectangle* stream 10 10 40 40 :ink +light-sea-green+)
       (draw-rectangle* stream 20 20 30 30 :ink +light-coral+)
       (draw-rectangle* stream 20 20 30 30 :filled nil :line-thickness 2)
       (draw-rectangle* stream 10 10 40 40 :filled nil :line-thickness 2))    
      ((eql :rectangle)
       (draw-rectangle* stream 10 15 40 35 :filled nil :line-thickness 2))
      ((eql :circle)
       (draw-circle* stream 25 25 13 :filled nil :line-thickness 2))
      (symbol
       (draw-text* stream (princ-to-string (tool-symbol object))
                   25 25 :align-x :center :align-y :center :text-size :small))
      (otherwise
       (draw-text* stream (princ-to-string (tool-symbol object))
                   25 25 :align-x :center :align-y :center :text-size :huge)))))

(define-command (com-select-tool :command-table toolbox-table)
    ((arg 'toolbox-item))
  (funcall (tool-action arg) arg))

(define-presentation-to-command-translator act-select-tool
    (toolbox-item com-select-tool toolbox-table :gesture :select :menu nil :echo nil)
    (object)
  `(,object))

(defpresent toolbox-item (object stream box-view)
  (draw-rectangle* stream 0 0 50 50 :ink (if (tool-active object)
                                             +light-yellow+
                                             +light-slate-grey+) )
  (with-drawing-options (stream :clipping-region (make-rectangle* 0 0 50 50))
    (typecase (tool-symbol object)
      ((eql :image)
       (draw-rectangle* stream 10 10 40 40 :ink +light-sea-green+)
       (draw-rectangle* stream 20 20 30 30 :ink +light-coral+)
       (draw-rectangle* stream 20 20 30 30 :filled nil :line-thickness 2)
       (draw-rectangle* stream 10 10 40 40 :filled nil :line-thickness 2))    
      ((eql :rectangle)
       (draw-rectangle* stream 10 15 40 35 :filled nil :line-thickness 2))
      ((eql :circle)
       (draw-circle* stream 25 25 13 :filled nil :line-thickness 2))
      (symbol
       (draw-text* stream (princ-to-string (tool-symbol object))
                   25 25 :align-x :center :align-y :center :text-size :small))
      (otherwise
       (draw-text* stream (princ-to-string (tool-symbol object))
                   25 25 :align-x :center :align-y :center :text-size :huge)))))

;;; This will work on any object called with BOX-VIEW.
(defpresent t (object stream box-view)
  (loop for tool in (world-toolset object)
        for offset from 1 by 51 do
          (with-translation (stream offset 0)
            (draw-rectangle* stream -1 0 51 51)
            (present* tool stream :single-box t))))

(define-application-frame toolbox-example ()
  ((items :initarg :tools :reader world-toolset))
  (:panes (box :application :display-function #'present* :default-view +box-view+
               :scroll-bars nil :borders nil
               :min-height 50 :max-height 50)
          (app :application :display-function #'present*))
  (:command-table (toolbox-example :inherit-from (toolbox-table)))
  (:default-initargs :tools (list (make-instance 'toolbox-item :symbol "T")
                                  (make-instance 'toolbox-item :symbol :image)
                                  (make-instance 'toolbox-item :symbol :rectangle)
                                  (make-instance 'toolbox-item :symbol :circle)
                                  (make-instance 'toolbox-item :symbol :pick)
                                  (make-instance 'toolbox-item :symbol :move)
                                  (make-instance 'toolbox-item :symbol :kill)
                                  *toolbox-item/grid*)))
(find-application-frame 'toolbox-example)
