Changeset main,80 for main


Ignore:
Timestamp:
12/13/2007 12:47:56 AM (18 years ago)
Author:
David Owen <dsowen@…>
branch-nick:
tui
revision id:
dsowen@fugue88.ws-20071213004756-lr4quvaf0hy1zpub
Message:

Widgets now support validation (#1).

Location:
main
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/form.lisp

    r79 r80  
    5555
    5656(defun make-textbox-def (row column name display-width
    57                          &key data-width read-only (validate 'nothing)
     57                         &key data-width read-only (validator 'identity)
    5858                         (inactive-background
    5959                          *default-inactive-widget-background*)
     
    6464                  :display-width ,display-width :data-width ,data-width
    6565                  :read-only ,read-only
    66                   :validator ',validate
     66                  :validator ',validator
    6767                  :inactive-background (list ,@inactive-background)
    6868                  :active-background (list ,@active-background)))
     
    7070(defun make-numberbox-def (row column name display-width
    7171                           &key data-width precision read-only
    72                            (validate 'nothing)
     72                           (validator 'identity)
    7373                           (inactive-background
    7474                            *default-inactive-widget-background*)
     
    7979                  :display-width ,display-width :data-width ,data-width
    8080                  :precision ,precision :read-only ,read-only
    81                   :validator ',validate
     81                  :validator ',validator
    8282                  :inactive-background (list ,@inactive-background)
    8383                  :active-background (list ,@active-background)))
     
    130130        (create-label window row column text))))
    131131  (:method ((tbd textbox-def) form)
    132     (with-slots (row column name display-width inactive-background
     132    (with-slots (row column name validator display-width inactive-background
    133133                     active-background) tbd
    134134      (with-slots (data window scroll) form
    135135        (let ((r (make-reflector data name)))
    136136          (create-textbox window (- row scroll) column r display-width
     137                          :validator validator
    137138                          :inactive-background inactive-background
    138139                          :active-background active-background)))))
    139140  (:method ((nbd numberbox-def) form)
    140     (with-slots (row column name display-width inactive-background
     141    (with-slots (row column name validator display-width inactive-background
    141142                     active-background) nbd
    142143      (with-slots (data window scroll) form
    143144        (let ((r (make-reflector data name)))
    144145          (create-numberbox window (- row scroll) column r display-width
     146                            :validator validator
    145147                            :inactive-background inactive-background
    146148                            :active-background active-background))))))
     
    256258        (assert (> n 0) nil "The form has no fields.")
    257259        (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)))))
     260                 (f-peer () (aref peers (f-idx))))
    267261          (loop
    268262             (ensure-widget-visible form (f-idx))
     
    271265               (case key
    272266                 ((#\Return #\Newline #\Tab :key-down)
    273                   #|(setf focus (mod (1+ focus) n))|#
    274                   (validate)
    275267                  (incf focus)
    276268                  (boundf focus 0 (1- n)))
    277269                 ((:key-btab :key-up)
    278                   #|(setf focus (mod (1- focus) n))|#
    279                   (validate)
    280270                  (decf focus)
    281271                  (boundf focus 0 (1- n)))
  • main/widget/textbox.lisp

    r65 r80  
    1010(defclass textbox (widget scroll insertion-point)
    1111  ((data :initarg :data)
     12   (validator :initform 'identity :initarg :validator :reader validator)
    1213   (window :initarg :window)
    1314   (active :type boolean :initform nil)))
     
    6364
    6465
    65 (defun create-textbox (parent-window y x data width &key
     66(defun create-textbox (parent-window y x data width &rest args &key validator
    6667                       (inactive-background '(#\Space 0))
    6768                       (active-background '(#\Space 0)))
     69  (declare (ignore validator inactive-background active-background))
    6870  (let* ((window (tui-window::create-subwindow parent-window 1 width y x))
    69          (inst (make-instance 'textbox
    70                               :data data
    71                               :window window
    72                               :inactive-background inactive-background
    73                               :active-background active-background)))
     71         (inst (apply 'make-instance 'textbox :data data :window window args)))
    7472    (draw inst)
    7573    inst))
     
    123121                       (let ((r (funcall key-callback key)))
    124122                         (when r
    125                            (setf (insertion-point textbox) 0)
    126                            (return-from activate r)))
     123                           (let ((q (funcall (validator textbox) (text data))))
     124                             (when q
     125                               (setf (text data) q)
     126                               (setf (insertion-point textbox) 0)
     127                               (return-from activate r)))))
    127128                       (multiple-value-bind (left right) (split textbox)
    128129                         (setf (text data)
Note: See TracChangeset for help on using the changeset viewer.