Our first data entry program is vvbasics.c and is listed here. It creates a data form with a field for entering a person's name, a field for entering a phone number, and some background text. Figure 6.1 illustrates how the form appears on the screen when it is first displayed.
vvbasics.c is a tutorial program. Try compiling, linking, and running this program. You will find it helpful to run the program before reading the explanation.

Figure 6.1: vvbasics Form
This program illustrates the steps to creating and using data forms in your programs. They are as follows:
The following sections detail the above steps and provide further information about how the user interface works.
|
Step 1: Include the header files for each field type in your data form.
Include the header files for each field type in your data form. In this case, only string fields are used in the form; therefore, only vv_str.h needs to be #included.
As always, vv_main.h must be included in the main program as the last Vermont Views header file.
Step 2: Declare a pointer to a form structure.
Declare a variable of type DFORMPTR. DFORMPTR is a typedef for a pointer to a data form structure.
Naming Tip: In Vermont Views, data forms and menus are both implemented using the same form structure. To avoid confusion, DFORMPTR dfmp is used when referring to data forms, MFORMPTR mfmp is used when referring to menu forms, and FORMPTR fmp is used when either a data or menu form can be used.
Step 3: Declare, allocate storage for, and initialize the data variables.
If you want to collect information typed by the user in the field, you must declare the data variables in which you want to store information. The type of the data variable depends on what kind of information you are collecting from the field. (Alternatively, you can have fields with no underlying data variable, but then you are responsible for writing code that displays and collects information in that field.)
By default, the current values of the data variables are converted to strings (edit buffers) and displayed in the fields when a form is displayed. Therefore, you should initialize the data variables to the values you want initially displayed in the field.
Also, memory must be allocated for the string data variables. This step can be done earlier in the program, for instance, immediately after calling vv_init(). However, it must be done before fld_def() is called.
A function is provided that both allocates string variables and initializes the string to blanks. This function is mem_stblank():
|
mem_stblank() allocates a string of the specified length (plus an additional space for the null terminator '\0') and sets all positions in the string to spaces. A pointer to the string is returned.
A function is provided that allocates and initializes a block of memory from the heap. This function is mem_get():
|
mem_get() allocates the specified number of bytes from the heap, initializes to null, and returns a pointer to the allocated memory. If you use mem_get() to allocate memory for string data variables, you must remember to add an additional byte for the null terminator.
Caution: String data variables must be long enough to hold the data entered by the user in the field plus the null terminator or memory will be corrupted.
Step 4: Define a data form with fm_def().
A data form is defined using the form definition function, fm_def():
|
Naming Tip: All functions that deal with data forms contain the abbreviation "fm" for form.
Specifying the fm_def() Arguments
Defining a Form Larger Than a Window
Defining Data Fields and Prompt Text with fld_def()