
(in-package :cl-user)

(require 'rg-cocoa-utils)

;;; Draggable

(setf times-48-italic
      (#/convertFont:toHaveTrait:
       (#/sharedFontManager ns:ns-font-manager)
       (#/fontWithName:size: ns:ns-font #@"Times New Roman" 48.0)
       #$NSItalicFontMask))
(setf d (make-instance ns:ns-mutable-dictionary))
(#/setValue:forKey: d times-48-italic #@"NSFont")

(define-class (testview text-view))
(setf w (make-window :title "Draggable"))
(setf v (make-testview :text #@"Lisp Rules!" :style d))
(add-subview w v)
(add-tracker v) ; Cheating just a little

; This is the cool part
(define-class (testview draggable text-view))
(define-class (testview highlighted draggable text-view))

; Just for fun
#|
(rotate v 45.0)
|#

;;; Scribble view

(define-class (scribble-view view) (path (make-instance ns:ns-bezier-path)))

(define-method (view-draw-contents (sv scribble-view path) &optional rect)
  (declare (ignore rect))
  (#/stroke path))

(define-method (view-click-event-handler (sv scribble-view path ns-view) loc)
  (when (> *multi-click-count* 1)
    (#/removeAllPoints path)
    (refresh sv))    
  (#/moveToPoint: path loc))

(define-method (view-drag-event-handler (sv scribble-view path ns-view) loc)
  (#/lineToPoint: path loc)
  (refresh sv))

(defun make-scribble-window ()
  (let ((w (make-window :title "Scribble"))
        (v (make-scribble-view)))
    (add-subview w v)
    (fill-superview v)
    w))

(make-scribble-window)

;;; PDF view

(setf d (pdf-from-url #@"http://www.flownet.com/ron/specials.pdf"))
(setf w (make-window :title "PDF"))
(setf p (make-pdf-view))
(add-subview w p)
(fill-superview p)
(set-content p d)

(setf sv (make-scribble-view))
(add-overlay p sv)
(fill-superview sv)

;;; Image view

(setf buzz (image-from-url #@"http://www.flownet.com/ron/buzz1.jpg"))
(setf iw (make-window :title "Image"))
(setf iv (make-image-view))
(set-content iv buzz)
(add-subview iw iv)

#|
; Alternative image drawing methods

(defconstant +ns-zero-rect+ (ns:make-ns-rect 0 0 0 0))

; Shrink to fit
(define-method (view-draw-contents (iv image-view image) &optional rect)
  (#/drawInRect:fromRect:operation:fraction: image rect +ns-zero-rect+ #$NSCompositeCopy 1.0))

; Shrink to fit, keeping proportions
(define-method (view-draw-contents (iv image-view image) &optional rect)
  (setf rect (or rect (bounds iv)))
  (when (typep image 'ns:ns-image)
    (bb
     size (#/size image)
     ix (pref size ns-size.width)
     iy (pref size ns-size.height)
     rsize (pref rect ns-rect.size)
     rx (pref rsize ns-size.width)
     ry (pref rsize ns-size.height)
     arr (/ (/ ix iy) (/ rx ry))
     tx (if (> arr 1) rx (* ix (/ ry iy)))
     ty (if (> arr 1) (* iy (/ rx ix)) ry)
     :with ns:ns-rect (trect 0 (- ry ty) tx ty)
     (#/drawInRect:fromRect:operation:fraction:
      image trect +ns-zero-rect+ #$NSCompositeCopy 1.0))))
|#

