Menu display, scrolling and columns
Adapted from Writing Programs with NCURSES, by Eric S. Raymond and Zeyd M. Ben-Halim and NCURSES Programming HOWTO, by Pradeep Padala.
The menu library calculates a minimum display size for your window, based on the following variables:
- The number and maximum length of the menu items
- Whether the
O_ROWMAJORoption is enabled - Whether display of descriptions is enabled
- Whatever menu format may have been set by the programmer
- The length of the menu mark string used for highlighting selected items
The function set_menu_format() allows you to set the maximum size of the viewport or menu page that will be used to display menu items. You can retrieve any format associated with a menu with menu_format(). The default format is rows=16, columns=1.
The actual menu page may be smaller than the format size. This depends on the item number and size and whether O_ROWMAJOR is on. This option (on by default) causes menu items to be displayed in a “raster-scan” pattern, so that if more than one item will fit horizontally the first couple of items are side-by-side in the top row. The alternative is column-major display, which tries to put the first several items in the first column.
Each menu has a mark string used to visually tag selected items; see the menu_mark(3X) manual page for details. The mark string length also influences the menu page size.
The function scale_menu() returns the minimum display size that the menu code computes from all these factors. There are other menu display attributes including a select attribute, an attribute for selectable items, an attribute for unselectable items, and a pad character used to separate item name text from description text. These have reasonable defaults which the library allows you to change (see the menu_attributes(3X) manual page).
Scrolling menus
Section titled “Scrolling menus”A menu format not large enough to allow all items to fit on-screen will result in a menu display that is vertically scrollable. When you are on the last item in the present list and send REQ_DOWN_ITEM, it is translated into REQ_SCR_DLINE and the menu scrolls by one item. You can also scroll it with the REQ_SCR_ requests to the menu driver, described in Process input with the menu driver.
Example: Scrolling Menus example
#include <stdlib.h>#include <string.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", "Choice 5", "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", "Exit", (char *) NULL,};
static void{ int length, x, y; float temp;
if (win == NULL) if (startx != 0) x = startx; if (starty != 0) y = starty; if (width == 0) width = 80;
length = (int) strlen(string); temp = (float) (width - length) / 2; x = startx + (int) temp;}
intmain(void){ int c; int n_choices, i;
/* Create items */ n_choices = ARRAY_SIZE(choices); perror("my_items"); return EXIT_FAILURE; }
/* Initialize curses */
for (i = 0; i < n_choices; ++i) { }
/* Create menu */
/* Create the window to be associated with the menu */
/* Set main window and sub window */
/* Set menu mark to the string " * " */
/* Print a border around the main window and print a title */
/* Post the menu */
"Use PageUp and PageDown to scroll down or up a page of items");
switch (c) { break; break; break; break; } }
/* Unpost and free all the memory taken up */ for (i = 0; i < n_choices; ++i)}In this example the number of choices has been increased to ten, which is larger than the subwindow, which can hold six items. Tell the menu system so with set_menu_format(): its arguments are the number of rows and columns to display on a single page. The rows argument can be any number of items, as long as it is less than the height of the subwindow. If the key pressed by the user is Page Up or Page Down, the menu is scrolled a page by the requests (REQ_SCR_UPAGE and REQ_SCR_DPAGE) given to menu_driver().
Multi-column menus
Section titled “Multi-column menus”The third argument of set_menu_format() is the number of columns. If your subwindow is wide enough, you can display more than one item per row. To keep things simple, the following example doesn’t show descriptions for the items.
Example: Multi Columnar Menus Example
#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", "Choice 5", "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", "Exit", (char *) NULL};
intmain(void){ int c; int n_choices, i;
n_choices = ARRAY_SIZE(choices); perror("my_items"); return EXIT_FAILURE; }
/* Initialize curses */
for (i = 0; i < n_choices; ++i) { }
/* Create menu */
/* Set menu option not to show the description */
/* Create the window to be associated with the menu */
/* Set main window and sub window */
/* Print a border around the main window and print a title */
/* Post the menu */
switch (c) { break; break; break; break; break; break; } }
/* Unpost and free all the memory taken up */ for (i = 0; i < n_choices; ++i)}The call to set_menu_format() sets the number of columns to 3, so 3 items are displayed per row. The example also switches off the display of descriptions with menu_opts_off().
Menu options
Section titled “Menu options”The functions set_menu_opts(), menu_opts_on(), menu_opts_off() and menu_opts() manipulate menu options. The following options are defined:
O_ONEVALUE Only one item can be selected for this menu.
O_SHOWDESC Display the item descriptions when the menu is posted.
O_ROWMAJOR Display the menu in row-major order.
O_IGNORECASE Ignore the case when pattern-matching.
O_SHOWMATCH Move the cursor to within the item name while pattern-matching.
O_NONCYCLIC Don't wrap around next-item and previous-item, requests to the other end of the menu.
O_MOUSE_MENU If the user clicks with the mouse and it does not fall on the currently active menu, push KEY_MOUSE and the MEVENT data back on the queue to allow processing in another part of the program.All options are on by default. You can switch specific options on or off with menu_opts_on() and menu_opts_off(). You can also use set_menu_opts() to directly specify the options. The argument to this function should be an OR-ed value of some of the constants above. The function menu_opts() returns a menu’s present options. See menu_opts(3X) for details.