To use choice lists, you must first place the desired list of choices in a memory file and then define the choice list menu. You do not need to know the details about the memory file facility to use choice lists. If you want more information about what memory files are and how they can be manipulated, see Chapter 45, "Creating and Viewing Memory Files."
The tutorial program cl_menu.c, listed below, illustrates the code necessary to create and use a choice list menu.
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
Step 1: Include the choice list header file vv_clist.h.
To use choice lists, you must #include vv_clist.h in the main module and in all other modules using choice lists.
Step 2: Write the text for the choice list.
The text for the choice list is written in an ASCII text file that you construct with any text editor.
Step 3: Mark the choice list text with a keyword.
All choice lists can be placed in a single ASCII text file. Each section of the file can be marked by means of a keyword. When defining a choice list menu, you pass the keyword that corresponds to the choice list menu that you want to appear. Only the section of the file marked by the keyword is displayed in the choice list window. The keyword itself will not be displayed.
A keyword must be in the first column, have the system keyword character (an asterisk by default) as the first character, and be the only text on the line. The keyword can be more than one word in length.
Only those lines containing keywords can have the keyword character in the first column.
Below is an example of how choice lists should appear in an ASCII text file, named cl_menu.txt, where "*INTRO MENU" and "*FONT MENU (NORMAL)" are keywords for two choice lists.
|
Caution: The keyword character that marks the keyword must appear in the first column of the line.
Step 4: Read the ASCII text file into a memory file.
After you have constructed the file of choice lists, you must read it in from disk to a memory file. The steps involved are:
| 1 | Define a memory file with mf_def(). |
| 2 | Read the ASCII file into the memory file with mf_rd(). |
Alternatively, you can read into memory only the keyworded section in use. See the sections on dynamically loading sections of a memory file into memory in Chapter 45, "Creating and Viewing Memory Files."
mf_def() has the following calling convention:
|
where maxrows is the maximum number of rows the memory file could contain and maxcols is the maximum number of columns. Generally, you should specify the maximum number of columns to be the number of characters in the longest line in your ASCII text file. You can specify maxrows to be the number of lines in the ASCII text file, but you may want to specify a larger number in case you insert more items later. You can determine now many rows and columns the memory file must contain by calling the function mf_size():
|
where filespec is the path (optional) and the filename of the ASCII file containing the choice list, and rowq_ptr and colq_ptr are pointers to integer variables to contain the number of rows and columns, respectively.
mf_rd() reads the ASCII text file and places the text into the memory file you defined. The call for mf_rd() is as follows:
|
where filespec is the path (optional) and the filename of the ASCII file containing the choice list and mfp is a pointer to the memory file that you just defined.
Step 5: Define a choice list with cl_def().
After you have the choice lists in a memory file, you define the choice list with the choice list definition function, cl_def():
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For the argument keyword you should specify the keyword for the section of the memory file that contains the text for the choice list in question. You must include the keyword character in the string, e.g., "*INTRO MENU". You should set the keyword to NULLP for a memory file that contains only a single choice list and has no keywords.
Step 6: Process the choice list with cl_proc().
To display the choice list and pass control to the user, call the choice list processing function, cl_proc(). You specify the item that you want initially highlighted. Numbering of items starts at 0. The call for cl_proc() is as follows:
|
||
|
|
|
|
|
|
cl_proc() displays the choice list, if it is not already displayed, and passes control to the user. The user can scroll through the items and select one or can leave the choice list without selecting an item. cl_proc() returns when the user has either exited the choice list (i.e., made a selection) or quit the choice list (i.e., left without making a selection).
cl_proc() will leave the choice list in the state it was found. If the choice list was already displayed when cl_proc() was called, the choice list will remain on the screen. Otherwise, it is taken down.
Step 7: Determine the item number chosen or the text chosen.
cl_proc() returns the #defined value AC_EXIT if the user made a choice from the list and AC_QUIT if the user quit without choosing an item from the list. If the return value of cl_proc() tests positively for AC_EXIT, you can determine the number of the choice list item that was chosen or the string for that item, depending on what is needed for your application.
To determine the item number chosen, use the choice list current item number function, cl_curnum():
|
The choice list number is returned by this function.
To determine the text string associated with the chosen item, use the choice list current text function, cl_curtxt():
|
The text for the chosen item is placed in the string buffer specified as an argument in the function. Take care that the length of the string buffer is at least one character longer than the longest string in the choice list. The extra character space is for the null terminator.
Step 8: Free the choice list with cl_free().
When the choice list menu is no longer needed, free the list structure with cl_free():
|
This function does NOT free the memory file that is used to create the choice list text.
Step 9: Free the memory file with mf_free().
If you are finished with all of the contents of the memory file, free this memory before continuing. To do this call the free memory file function, mf_free():
|
If you have other sections in the memory file marked with keywords, make sure you do not need these sections anymore.