Changeset main,8 for main


Ignore:
Timestamp:
10/18/2007 01:02:28 AM (19 years ago)
Author:
David Owen <dsowen@…>
branch-nick:
tui
revision id:
dsowen@tux-20071018010228-1zi84upn20zpkrom
Message:

Rework flat-menu to get common information out of the entries class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/flat-menu.lisp

    r7 r8  
    88
    99(defclass entry ()
     10  ((text :initarg :text)))
     11
     12(defun draw-entry (entry window row column)
     13  (with-slots (text) entry
     14    (cdk::move-and-add-string window row column text)))
     15
     16
     17
     18(defclass menu ()
    1019  ((window :initarg :window)
    1120   (row :initarg :row)
    1221   (column :initarg :column)
    13    (text :initarg :text)
    14    (highlight :accessor highlight)))
     22   (entries :type vector :initarg :entries)
     23   (selected :type (integer 0) :initform 0 :accessor selected)))
    1524
    16 (defun draw-entry (entry)
    17   (with-slots (window row column text highlight) entry
    18     (when highlight
     25(defun redraw-selected (menu with-highlight)
     26  (with-slots (window row column entries selected) menu
     27    (when with-highlight
    1928      (cdk::c-wattron window 262144))
    20     (cdk::move-and-add-string window row column text)
    21     (when highlight
     29    (draw-entry (aref entries selected) window (+ row selected) column)
     30    (when with-highlight
    2231      (cdk::c-wattroff window 262144))))
    2332
    24 (defmethod (setf highlight) :after (flag (entry entry))
    25   (draw-entry entry))
     33(defmethod (setf selected) :before (i (menu menu))
     34  (redraw-selected menu nil))
    2635
    27 (defun activate-entry (entry)
    28   (with-slots (window) entry
    29     (setf (highlight entry) t)
    30     (cdk::c-refresh-window window)
    31     (let ((ch (cdk::c-wgetch window)))
    32       (setf (highlight entry) nil)
    33       ch)))
     36(defun bound (a b c)
     37  (min (max a b) c))
    3438
    35 (defclass menu ()
    36   ((entries :type vector :initarg :entries)
    37    (selected :type (integer 0) :accessor selected)))
     39(defmethod (setf selected) :around (i (menu menu))
     40  (call-next-method (bound 0 i (1- (length (slot-value menu 'entries)))) menu))
     41
     42(defmethod (setf selected) :after (i (menu menu))
     43  (redraw-selected menu t))
    3844
    3945(defmethod initialize-instance :after ((menu menu) &rest initargs)
    40   (with-slots (entries) menu
    41     (dotimes (i (length entries))
    42       (setf (highlight (aref entries i)) nil))
    43     (setf (selected menu) 0)))
     46  (declare (ignore initargs))
     47  (setf (selected menu) 0))
    4448
    4549(defun activate-menu (menu)
    46   (with-slots (entries selected) menu
    47     (loop
    48        (multiple-value-bind (exit) (activate-entry (aref entries selected))
    49          (cond
    50            ((= exit 258)
    51            
    52             (incf selected))
    53            ((= exit 259)
    54             (decf selected))
    55            (t (break)))
    56          (setf selected (mod selected (length entries)))))))
     50  (with-slots (window) menu
     51    (with-accessors ((selected selected)) menu
     52      (loop
     53         (let ((ch (cdk::c-wgetch window)))
     54           (cond
     55             ((= ch 258)
     56              (incf selected))
     57             ((= ch 259)
     58              (decf selected))
     59             (t (break))))))))
    5760
    5861(defun test ()
     
    6467    (cdk::c-init-color)
    6568    (cdk::c-erase-window screen)
    66     (let ((entry1 (make-instance 'entry :window screen :row 5 :column 10 :text "Hello, world!"))
    67           (entry2 (make-instance 'entry :window screen :row 6 :column 10 :text "Goodbye, galaxy!")))
    68       (setf (highlight entry1) t)
    69       (let ((menu (make-instance 'menu :entries (vector entry1 entry2))))
     69    (let ((entry1 (make-instance 'entry :text "Hello, world!"))
     70          (entry2 (make-instance 'entry :text "Goodbye, galaxy!")))
     71      (let ((menu (make-instance 'menu :window screen :row 5 :column 10 :entries (vector entry1 entry2))))
    7072        (activate-menu menu)
    7173        (format t "Here~%")
Note: See TracChangeset for help on using the changeset viewer.