Changeset 6748


Ignore:
Timestamp:
Jun 16, 2007, 7:18:58 PM (17 years ago)
Author:
Gary Byers
Message:

No constraints on cocoa-defaults. Add optional change-hook mechanism.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ide-1.0/ccl/examples/cocoa-defaults.lisp

    r6733 r6748  
    2929  value                                 ; the "standard" initial value
    3030  doc                                   ; a doc string
    31   constraint                            ; an optional type constraint.
     31  change-hook                           ; an optional hook function
    3232  )
    3333
     
    4343  (defun %clear-cocoa-defaults () (setq cocoa-defaults nil)))
    4444
    45 (defun set-cocoa-default (name string type value doc &optional constraint)
     45(defun set-cocoa-default (name string type value doc &optional change-hook)
    4646  (check-type name symbol)
    4747  (check-type string objc-constant-string)
     
    5454                                          :value value
    5555                                          :doc doc
    56                                           :constraint constraint))
     56                                          :change-hook change-hook))
    5757  value)
    5858
     
    6262 
    6363
    64 (defun %define-cocoa-default (name type value doc &optional constraint)
     64(defun %define-cocoa-default (name type value doc &optional change-hook)
    6565  (proclaim `(special ,name))
    6666  ;; Make the variable "GLOBAL": its value can be changed, but it can't
     
    7070  (record-source-file name 'variable)
    7171  (setf (documentation name 'variable) doc)
    72   (set name (set-cocoa-default name (objc-default-key name) type value doc constraint))
     72  (set name (set-cocoa-default name (objc-default-key name) type value doc change-hook))
    7373  name)
    7474 
    7575 
    7676
    77 (defmacro def-cocoa-default (name type value  doc &optional constraint &environment env)
     77(defmacro def-cocoa-default (name type value  doc &optional change-hook &environment env)
    7878  `(progn
    7979     (eval-when (:compile-toplevel)
    8080       (note-variable-info ',name :global ,env))
    8181    (declaim (special ,name))
    82     (%define-cocoa-default ',name  ',type ,value ',doc ,@(when constraint `((specifier-type ',constraint))))))
     82    (defloadvar ,name nil)
     83    (%define-cocoa-default ',name  ',type ,value ',doc ,change-hook)))
    8384
    8485   
     
    9495             (key (objc-constant-string-nsstringptr (cocoa-default-string d))))
    9596        (if (%null-ptr-p (#/objectForKey:  domain key))
    96           (progn
    97             (#/setObject:forKey: domain
    98                                  (case (cocoa-default-type d)
    99                                    (:color (#/archivedDataWithRootObject:
     97          (let* ((value (cocoa-default-value d)))
     98            (case (cocoa-default-type d)
     99              (:int (#/setInteger:forKey: domain value key))
     100              (:float (#/setFloat:forKey: domain value key))
     101              (:bool (#/setBool:forKey: domain value key))
     102              (:color (#/setObject:forKey: domain
     103                                           (#/archivedDataWithRootObject:
    100104                                            ns:ns-archiver
    101                                             (cocoa-default-value d)))
    102                                    (t
    103                                     (%make-nsstring (format nil "~a" (cocoa-default-value d)))))
    104                                    key)
     105                                            value)
     106                                           key))
     107              (t
     108               (#/setObject:forKey: domain
     109                                    (%make-nsstring (format nil "~a" (cocoa-default-value d)))
     110
     111                                    key)))
    105112            (setq need-synch t))
    106           (case (cocoa-default-type d)
    107             (:int
    108              (set name (#/integerForKey: domain key)))
    109             (:float
    110              (set name (#/floatForKey: domain key)))
    111             (:string
    112              (let* ((nsstring (#/stringForKey: domain key)))
    113                (unless (%null-ptr-p nsstring)
    114                  (set name (lisp-string-from-nsstring nsstring)))))
    115             (:color
    116              (let* ((data (#/dataForKey: domain key)))
    117                (unless (%null-ptr-p data)
    118                  (set name (#/retain (#/unarchiveObjectWithData: ns:ns-unarchiver data))))))))))
     113          (let* ((hook (cocoa-default-change-hook d))
     114                 (old-value (symbol-value name)))
     115            (case (cocoa-default-type d)
     116              (:int
     117               (set name (#/integerForKey: domain key)))
     118              (:float
     119               (set name (#/floatForKey: domain key)))
     120              (:bool
     121               (set name (#/boolForKey: domain key)))
     122              (:string
     123               (let* ((nsstring (#/stringForKey: domain key)))
     124                 (unless (%null-ptr-p nsstring)
     125                   (set name (lisp-string-from-nsstring nsstring)))))
     126              (:color
     127               (let* ((data (#/dataForKey: domain key)))
     128                 (unless (%null-ptr-p data)
     129                   (set name (#/retain (#/unarchiveObjectWithData: ns:ns-unarchiver data)))))))
     130            (when hook (funcall hook old-value (symbol-value name)))))))
    119131    (when need-synch (#/synchronize domain))))
    120132
     
    125137            (:int
    126138             (set name (#/intValue newval)))
     139            (:bool
     140             (#_NSLog #@"newvalue = %@" :id newval)
     141             (set name (coerce-from-bool (#/intValue newval))))
    127142            (:float
    128143             (set name (#/floatValue newval)))
     
    140155                              :with-capacity (length defaults))))
    141156    (dolist (d defaults dict)
     157      (let* ((value (cocoa-default-value d)))
    142158      (#/setObject:forKey: dict
    143159                           (case (cocoa-default-type d)
    144160                             (:color (#/archivedDataWithRootObject:
    145161                                      ns:ns-archiver
    146                                       (cocoa-default-value d)))
     162                                      value))
     163                             (:int (make-instance 'ns:ns-number
     164                                                  :with-int value))
     165                             (:float (make-instance 'ns:ns-number
     166                                                    :with-float value))
     167                             (:bool (make-instance 'ns:ns-number
     168                                                   :with-bool (coerce-to-bool value)))
    147169                             (t
    148170                              (%make-nsstring (format nil "~a" (cocoa-default-value d)))))
    149                            (objc-constant-string-nsstringptr (cocoa-default-string d))))))
     171                           (objc-constant-string-nsstringptr (cocoa-default-string d)))))))
Note: See TracChangeset for help on using the changeset viewer.