Next: , Previous: The Basics (Forms), Up: Forms Library   [Contents][Index]

4.15.2 A Form Example

To use form library functions, you have to include (ncurses form).


(use-modules (srfi srfi-1)
             (ncurses curses)
             (ncurses form))

;; Initialize curses
(define stdscr (initscr))
(keypad! stdscr #t)

;; Initialize the fields
(define field (list
               (new-field 1 10 4 18 0 0)
               (new-field 1 10 6 18 0 0)))

;; Set field options
;; Print a line for the options
(set-field-back! (first field) A_UNDERLINE)
;; Don't go to the next field when this field is filled up
(field-opts-off! (first field) O_AUTOSKIP)
(set-field-back! (second field) A_UNDERLINE)
(field-opts-off! (second field) O_AUTOSKIP)

;; Create the new form and post it
(define my-form (new-form field))
(post-form my-form)
(refresh stdscr)

(addstr stdscr "Value 1:" #:y 4 #:x 10)
(addstr stdscr "Value 2:" #:y 6 #:x 10)
(refresh stdscr)

;; Loop through to get user requests
(let loop ((ch (getch stdscr)))
  (if (not (eqv? ch (key-f 1)))
       ((eqv? ch KEY_DOWN)
          ;; Go to the end of the next field
          (form-driver my-form REQ_NEXT_FIELD)
          (form-driver my-form REQ_END_LINE)
          (loop (getch stdscr))))
       ((eqv? ch KEY_UP)
          ;; Go to the end of the previous field
          (form-driver my-form REQ_PREV_FIELD)
          (form-driver my-form REQ_END_LINE)
          (loop (getch stdscr))))
          ;; Print any normal character
          (form-driver my-form ch)
          (loop (getch stdscr)))))))

;; Unpost the form
(unpost-form my-form)


The above example is pretty straightforward. It creates two fields with new-field. The procedure new-field takes height, width startx, starty, the number of off-screen rows, and number of additional working buffers. The fifth argument number of off-screen rows specified how much of the field to be shown. If it is zero, the entire field is always displayed; otherwise, the form will be scrollable when the user accesses undisplayed part of the field. The forms library allocates one buffer per field to store the data which the user enters. Using the last parameter to new-field we can specify it to allocate some additional buffers. These can be used for any purpose you like.

After creating the fields, the background attribute of both of them is set to an underscore with set-field-back!. The AUTOSKIP option is turned off using field-opts-off!. If this option is turned of, focus will move to the next field in the form once the active field is filled up completely.

After attaching the fields to the form, it is posted. Here on, user inputs are processed in the loop, by making corresponding request to form-driver. The details of all the request to form-driver are explained later.

Next: , Previous: The Basics (Forms), Up: Forms Library   [Contents][Index]