Compile and run a first menu
Adapted from Writing Programs with NCURSES, by Eric S. Raymond and Zeyd M. Ben-Halim and NCURSES Programming HOWTO, by Pradeep Padala.
Your menu-using modules must import the menu library declarations with
#include <menu.h>and must be linked explicitly with the menus library using an -lmenu argument. Note that they must also link the ncurses library with -lncurses. Many linkers are two-pass and will accept either order, but it is still good practice to put -lmenu first and -lncurses second:
gcc program.c -lmenu -lncursesHere is a program which prints a simple menu and moves the current selection with the up and down arrow keys.
Example: Menu Basics
#include <stdlib.h>#include <curses.h>#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))#define CTRLD 4
const char *choices[] = { "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Exit",};
intmain(void){ int c; int n_choices, i;
n_choices = ARRAY_SIZE(choices); if ((my_items = calloc((size_t) (n_choices + 1), perror("my_items"); return EXIT_FAILURE; }
for (i = 0; i < n_choices; ++i)
switch (c) { break; break; } }
return EXIT_SUCCESS;}This program demonstrates the basic concepts involved in creating a menu using the menus library. First we create the items using new_item(), which takes an item’s name and its description. This program passes each choice as both, so every row shows the choice twice: the name, then the same text as its description. Then we attach them to the menu with the new_menu() function. After posting the menu and refreshing the screen, the main processing loop starts. It reads user input and takes corresponding action. The function menu_driver() is the main workhorse of the menu system. The second parameter to this function tells what’s to be done with the menu. According to the parameter, menu_driver() does the corresponding task. The value can be either a menu navigational request, a printable character, or a KEY_MOUSE special key associated with a mouse event.
The requests of interest in this example are REQ_UP_ITEM and REQ_DOWN_ITEM. When either is passed to menu_driver(), the menu driver moves the current item one item up or down. Process input with the menu driver describes the rest.
When the user presses F1, the program unposts the menu, frees it, and then frees each item.