As you have seen, in the above example, display attributes for the
fields can be set with
set-field-back!. These functions set foreground and background
attributes of the fields. You can also specify a pad character that
will be filled in the unfilled portion of the field. The pad
character is set with a call to
set-field-pad!. Default pad
value is space. The functions
field-pad can be used to query the present foreground
background attributes and pad character for the field.
Though the functions seem quite simple, using colors with
set-field-fore! may be frustrating in the beginning. Let me
first explain about foreground and background attributes of a field.
The foreground attribute is associated with the character. That means
a character in the field is printed with the attribute you have set
set-field-fore!. Background attribute is the attribute
used to fill background of field, whether any character is there or
not. So what about colors? Since colors are always defined in pairs,
what is the right way to display colored fields? Here’s an example
clarifying color attributes.
#!/usr/bin/guile !# (use-modules (srfi srfi-1) (ncurses curses) (ncurses form)) ;; Initialize curses (define stdscr (initscr)) (start-color!) (cbreak!) (noecho!) (keypad! stdscr #t) ;; Initialize the color pairs (init-pair! 1 COLOR_WHITE COLOR_BLUE) (init-pair! 2 COLOR_WHITE COLOR_BLUE) ;; 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 (set-field-fore! (first field) (color-pair 1)) (set-field-back! (first field) (color-pair 2)) (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))) (cond ((eqv? ch KEY_DOWN) (begin ;; 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) (begin ;; 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)))) (else (begin ;; Print any normal character (form-driver my-form ch) (loop (getch stdscr))))))) ;; Unpost the form (unpost-form my-form) (endwin)
Play with the color pairs and try to understand the foreground and background attributes. Color pair 1 is used for the character that have been typed into the form, and color pair 2 is used for the empty spaces in the form.