Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.33 KB | None | 0 0
  1. ;; DoWhateverTheFuckYouWantPublicLicense
  2.  
  3. (defun builder-object (builder name)
  4.   (or (builder-get-object builder name)
  5.       (error "Widget ~S not found" name)))
  6.  
  7. (eval-when (:compile-toplevel :load-toplevel :execute)
  8.   (defun %expand-bind (widget/s access &optional (bind 'identity))
  9.     (cond
  10.       ((stringp widget/s)
  11.        `(setf (widget-value (builder-object builder ,widget/s))
  12.               (funcall #',bind ,access)))
  13.       ((listp widget/s)
  14.        `(setf (widget-value (builder-object builder (nth (funcall #',bind ,access) ',widget/s)))
  15.               t))
  16.       (t (error "Unhandled"))))
  17.   (defun %expand-extract (widget/s access &optional (bind 'identity))
  18.     (let ((extract (inverse bind)))
  19.       (cond ((stringp widget/s)
  20.              `(setf ,access (funcall #',extract (widget-value (builder-object builder ,widget/s)))))
  21.             ((listp widget/s)
  22.              `(loop for index from 0
  23.                  for widget in ',widget/s
  24.                  when (widget-value (builder-object builder widget))
  25.                  do (progn (setf ,access (funcall #',extract index))
  26.                            (return))
  27.                  finally (error "There is no active widget.")))
  28.             (t (error "Unhandled"))))))
  29.  
  30. (defmacro define-widget (class name &body connections)
  31.   `(progn
  32.      (defun ,(symbolicate "BIND-" name) (builder ,name)
  33.        ,@(loop for connection in connections
  34.             collect (apply #'%expand-bind connection)))
  35.      (defun ,(symbolicate "EXTRACT-" name) (builder)
  36.        (let ((,name (make-instance ',class)))
  37.          ,@(loop for connection in connections
  38.               collect (apply #'%expand-extract connection))
  39.          ,name))))
  40.  
  41. (defun widget-value (widget)
  42.   (funcall
  43.    (typecase widget
  44.      (toggle-button #'toggle-button-active)
  45.      (combo-box #'combo-box-active)
  46.      (adjustment #'adjustment-value)
  47.      (otherwise (error "Unhandled widget type.~%~A" widget)))
  48.    widget))
  49.  
  50. (defun (setf widget-value) (value widget)
  51.   (funcall
  52.    (typecase widget
  53.      (toggle-button #'(setf toggle-button-active))
  54.      (combo-box #'(setf combo-box-active))
  55.      (adjustment (lambda (value widget)
  56.                    (declare (type double-float value))
  57.                    (setf (adjustment-value widget) value)))
  58.      (otherwise (error "Unhandled widget type.~%~A" widget)))
  59.    value widget))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement