Changeset main,78


Ignore:
Timestamp:
12/05/2007 03:05:40 AM (19 years ago)
Author:
David Owen <dsowen@…>
branch-nick:
tui
revision id:
dsowen@fugue88.ws-20071205030540-vtmraw1ijs01a9ec
Message:

Fixed form traversal bug.
Started adding validators.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/form.lisp

    r74 r78  
    2222(defclass value-widget-def (widget-def)
    2323  ((name :initarg :name)
    24    (read-only :type boolean :initarg :read-only)))
     24   (read-only :type boolean :initarg :read-only)
     25   (validator :initarg :validator)))
    2526
    2627
     
    5455
    5556(defun make-textbox-def (row column name display-width
    56                          &key data-width read-only
     57                         &key data-width read-only (validate 'nothing)
    5758                         (inactive-background
    5859                          *default-inactive-widget-background*)
     
    6364                  :display-width ,display-width :data-width ,data-width
    6465                  :read-only ,read-only
     66                  :validator ',validate
    6567                  :inactive-background (list ,@inactive-background)
    6668                  :active-background (list ,@active-background)))
     
    6870(defun make-numberbox-def (row column name display-width
    6971                           &key data-width precision read-only
     72                           (validate 'nothing)
    7073                           (inactive-background
    7174                            *default-inactive-widget-background*)
     
    7679                  :display-width ,display-width :data-width ,data-width
    7780                  :precision ,precision :read-only ,read-only
     81                  :validator ',validate
    7882                  :inactive-background (list ,@inactive-background)
    7983                  :active-background (list ,@active-background)))
     
    231235
    232236
     237(defun focusables (form)
     238  (with-slots (peers) form
     239    (let* ((n (length peers))
     240           (x (make-sequence 'vector n)))
     241      (dotimes (i n)
     242        (setf (aref x i) i))
     243      (delete-if (lambda (i)
     244                   (typep (aref peers i) 'label))
     245                 x))))
     246
    233247(defmethod activate ((form form) &key (key-callback 'nothing) &allow-other-keys)
    234   (with-slots (peers) form
     248  (with-slots (data peers widget-defs) form
    235249    (flet ((callback (key)
    236              (if (member key '(#\Return #\Newline #\Tab :key-btab))
    237                  key
     250             (or (find key '(#\Return #\Newline #\Tab :key-btab :key-down
     251                             :key-up))
    238252                 (funcall key-callback key))))
    239       (let ((focus 0)
    240             (n (length peers)))
    241         (loop
    242            (ensure-widget-visible form focus)
    243            (refresh form)
    244            (let ((key (activate (aref peers focus) :key-callback #'callback)))
    245              (case key
    246                ((#\Return #\Newline #\Tab)
    247                 (setf focus (mod (1+ focus) n)))
    248                (:key-btab
    249                 (setf focus (mod (1- focus) n)))
    250                (t
    251                 (return-from activate key)))))))))
     253      (let* ((focusables (focusables form))
     254             (focus 0)
     255             (n (length focusables)))
     256        (assert (> n 0) nil "The form has no fields.")
     257        (labels ((f-idx () (aref focusables focus))
     258                 (f-peer () (aref peers (f-idx)))
     259                 (f-def () (aref widget-defs (f-idx)))
     260                 (validate ()
     261                   (let* ((v (slot-value (f-def) 'validator))
     262                          (name (slot-value (f-def) 'name))
     263                          (text (form-value data name))
     264                          (r (funcall v text)))
     265                     (when r
     266                       (setf (form-value data name) r)))))
     267          (loop
     268             (ensure-widget-visible form (f-idx))
     269             (refresh form)
     270             (let ((key (activate (f-peer) :key-callback #'callback)))
     271               (case key
     272                 ((#\Return #\Newline #\Tab :key-down)
     273                  #|(setf focus (mod (1+ focus) n))|#
     274                  (validate)
     275                  (incf focus)
     276                  (boundf focus 0 (1- n)))
     277                 ((:key-btab :key-up)
     278                  #|(setf focus (mod (1- focus) n))|#
     279                  (validate)
     280                  (decf focus)
     281                  (boundf focus 0 (1- n)))
     282                 (t
     283                  (return-from activate key))))))))))
Note: See TracChangeset for help on using the changeset viewer.