📄 menu.c
字号:
/**
* MENU.C Display several types of keyboard-activated
* menus.
*
* This program displays a series of menus on the screen, allowing
* the user to choose which of several types of menus will be
* displayed.
*
* Five menus are used. The main menu allows the user to select
* one of four other menus or to terminate the program. The
* main menu is an example of a vertical menu style; the other
* four exhibit horizontal, grid, Lotus-style, and virtual menus,
* respectively.
*
* The command line format is as follows:
*
* menu [/c | /d | /a]
*
* The user may specify either /c, /d, or /a, corresponding to the
* type of mouse style they wish to use; click, drag, or alternate
* drag, respectively. If no switch is specified, the default is
* click.
*
* The two purposes of MENU are to show off the menuing
* capabilities of Turbo C TOOLS, and to provide a working example
* of the proper method of construction and use of the menu
* functions.
*
* Version 6.00 (C)Copyright Blaise Computing Inc. 1987-1989
*
**/
#include "ctype.h"
#include "stdio.h"
#include "bkeybrd.h"
#include "bkeys.h"
#include "bmenu.h"
#include "butil.h"
#include "bmouse.h"
/* Text color is intense white on blue. */
#define MYTEXTATR (utnybbyt (SC_BLUE, NORMAL | INTENSITY))
/* "Lotus" description color is intense magenta on */
/* blue. */
#define MYLNGATTR (utnybbyt (SC_BLUE, SC_MAGENTA | INTENSITY))
/* Color of highlight bar is black text on a white */
/* background. */
#define MYHILATR (utnybbyt (NORMAL, SC_BLACK))
/* Color of "protected" items is green on blue. */
#define MYPROATR (utnybbyt (SC_BLUE, SC_GREEN))
/* Color of menu borders is cyan on black. */
#define MYBORDATR (utnybbyt (SC_BLACK, SC_CYAN))
/* Color of menu titles is the same as menu borders.*/
#define MYTITATR MYBORDATR
#define NUL '\0'
#define TRUE 1
#define FALSE 0
/* Terminate-on-error macro. */
#define exitBad() \
{ \
mohide(MO_HIDE); \
fprintf (stderr, \
"menu: died with b_wnerr = %d in line %d", \
b_wnerr, __LINE__); \
exit (b_wnerr); \
}
/* Declare function prototypes. */
void main (int, char **);
void horizontal (int, int, int, int, int *, int *);
void lotus (int, int, int, int, int *, int *);
void grid (int, int, int, int, int *, int *);
void virtual (int, int, int, int, int *, int *);
void main (argc, argv)
int argc;
char **argv;
{
BMENU *pmenu;
BORDER border;
WHERE where;
int ch, scan;
int row, col;
int rrow, rcol;
int showrow, showcol;
int adapter, mode, cols, apage;
int coff, crow, ccol, chigh, clow;
int done = FALSE;
int mouse_style = MN_MOU_CLICK;
int bad_command_line = 0;
/* First, check to see if the user specified a */
/* mouse style. */
if (argc == 2)
{
/* Make sure the switch character is valid. */
if ((argv[1][0] == '/') || (argv[1][0] == '-'))
{
/* Make sure the option character is valid. */
switch(toupper(argv[1][1]))
{
case 'C':
mouse_style = MN_MOU_CLICK;
break;
case 'D':
mouse_style = MN_MOU_DRAG;
break;
case 'A':
mouse_style = MN_MOU_ALT_DRAG;
break;
default:
bad_command_line = 1;
break;
}
}
else
bad_command_line = 1;
}
else
if (argc > 2)
bad_command_line = 1;
if (bad_command_line)
{
fprintf(stderr, "Usage: menu [/c | /d | /a].\n");
exit(1);
}
/* Now we make certain that we are in 80 column */
/* text video mode. */
adapter = scmode (&mode, &cols, &apage);
switch (mode)
{
case 2:
case 3:
case 7:
break;
default:
fprintf (stderr,
"menu: This demonstration works only in 80 column\n");
fprintf (stderr,
" text modes (modes 2, 3, and 7)\n");
exit (1);
}
/* Save cursor position and style to restore later. */
coff = sccurst (&crow, &ccol, &chigh, &clow);
/* Create the menu data structure. */
if ((pmenu = mncreate (8, 14,
MYTEXTATR, MYHILATR,
MYPROATR, MYLNGATTR)) == NIL)
exitBad ()
/* Set up items on the menu, and define */
/* corresponding keys (upper and lower case of the */
/* first letters of the items). */
if (mnitmkey (pmenu, 0, 1, 0, "Horizontal", "Hh", MN_NOMOVE) == NIL)
exitBad ()
if (mnitmkey (pmenu, 1, 1, 0, "Lotus-Style", "Ll", MN_NOMOVE) == NIL)
exitBad ()
if (mnitmkey (pmenu, 2, 1, 0, "Grid", "Gg", MN_NOMOVE) == NIL)
exitBad ()
if (mnitmkey (pmenu, 3, 1, 0, "Virtual", "Vv", MN_NOMOVE) == NIL)
exitBad ()
if (mnitmkey (pmenu, 5, 1, 0, "Quit", "QqXx", MN_NOMOVE | MN_BEEP)
== NIL)
exitBad ()
/* Define the following keys as selection & */
/* transmission keys: ALT-H, ALT-L, ALT-G, ALT-V, */
/* ALT-Q and X. */
if (mnkey (pmenu, 0, 1, KB_C_A_H, KB_S_A_H,
MN_SELECT | MN_TRANSMIT, MN_ADD) == NIL)
exitBad ()
if (mnkey (pmenu, 1, 1, KB_C_A_L, KB_S_A_L,
MN_SELECT | MN_TRANSMIT, MN_ADD) == NIL)
exitBad ()
if (mnkey (pmenu, 2, 1, KB_C_A_G, KB_S_A_G,
MN_SELECT | MN_TRANSMIT, MN_ADD) == NIL)
exitBad ()
if (mnkey (pmenu, 3, 1, KB_C_A_V, KB_S_A_V,
MN_SELECT | MN_TRANSMIT, MN_ADD) == NIL)
exitBad ()
if (mnkey (pmenu, 5, 1, KB_C_A_Q, KB_S_A_Q,
MN_SELECT | MN_TRANSMIT | MN_BEEP, MN_ADD) == NIL)
exitBad ()
if (mnkey (pmenu, 5, 1, KB_C_A_X, KB_S_A_X,
MN_SELECT | MN_TRANSMIT | MN_BEEP, MN_ADD) == NIL)
exitBad ()
/* Disable ESC key. */
if (mnkey (pmenu, 0, 0, KB_C_N_ESC, KB_S_N_ESC,
MN_ABORT, MN_DELETE) == NIL)
exitBad ()
/* Now that we have put all of the menu items on */
/* the menu in item color, set the native window */
/* color so that additional text will appear in the */
/* menu's window in "long-item" (description) color.*/
wnsetopt (pmenu->pwin, WN_ATTR, MYLNGATTR);
/* Figure out where to display the menu. */
where.dev = (adapter == 0) ? SC_MONO : SC_COLOR;
where.page = 0;
where.corner.row = 1;
where.corner.col = 1;
/* Make a border with a top centered title. */
border.type = BBRD_SSSS | BBRD_TCT;
border.attr = MYBORDATR;
border.ch = NUL;
border.pttitle = "Menu Styles";
border.ttattr = MYTITATR;
/* Display the menu on the screen. */
if (mndsplay (pmenu, &where, &border) == NIL)
exitBad ()
/* Set up starting row and column for highlight bar.*/
row = 0;
col = 1;
/* If the mouse is present, enable its cursor. */
if (MO_OK == mohide(MO_SHOW))
if (NULL == mnmstyle(pmenu, mouse_style, MO_LEFT))
exitBad ()
/* Leave this menu on the screen until they select */
/* the "Quit" entry. */
do
{
/* Read a response from the menu. */
if (mnread (pmenu, row, col, &row, &col, &ch, &scan,
MN_KEEP_HIGHLIGHT))
exitBad ()
/* Set up location to show sub-menu. */
showrow = where.corner.row + row + 2;
showcol = where.corner.col + col + 0;
/* Go do what was requested. */
switch (row)
{
case 0:
horizontal (where.dev, showrow, showcol, mouse_style,
&rrow, &rcol);
break;
case 1:
lotus (where.dev, showrow, showcol, mouse_style,
&rrow, &rcol);
break;
case 2:
grid (where.dev, showrow, showcol, mouse_style,
&rrow, &rcol);
break;
case 3:
virtual (where.dev, showrow, showcol, mouse_style,
&rrow, &rcol);
break;
case 5:
done = TRUE;
}
/* At this point, rrow and rcol contain the row and */
/* column of the item selected from the submenu. */
if (!done)
{ /* Tell the user what was selected from the submenu.*/
if (b_pcurwin != pmenu->pwin)
wnselect (pmenu->pwin);
wnscrblk (b_pcurwin, 6, 0, 6, 13, -1, -1, 0, 0, 0);
wncurmov (6, 1);
wnprintf ("xmit (%d %d)", rrow, rcol);
}
} while (!done);
mndstroy (pmenu);
mohide(MO_HIDE);
/* Restore cursor position and style. */
sccurset (crow, ccol);
scpgcur (coff, chigh, clow, CUR_NO_ADJUST);
}
/**
*
* Name HORIZONTAL -- Display and allow user selection from
* a simple horizontal menu.
*
* Synopsis horizontal (dev, row, col, prrow, prcol);
*
* int dev Device on which to display the
* menu. Either SC_COLOR or SC_MONO.
* int row, col Row and column where the upper-
* left corner of the menu's data
* area should appear.
* int mouse_style The mouse style to use.
* int *prrow, Pointers to variables in which to
* *prcol return the row and column selected
* from the menu.
*
* Description This function constructs a simple horizontal menu,
* and waits for user input. It then returns the row
* and column of the selection to its caller.
*
* Returns *prrow, *prcol Row and column (relative to menu) of
* user selection.
*
**/
void horizontal (dev, row, col, mouse_style, prrow, prcol)
int dev;
int row, col;
int mouse_style;
int *prrow, *prcol;
{
BMENU *pmenu;
BORDER border;
WHERE where;
int ch, scan;
/* Figure out where to display the menu. */
where.dev = dev;
where.page = 0;
where.corner.row = row;
where.corner.col = col;
/* Create the menu data structure. */
if ((pmenu = mncreate (1, 28,
MYTEXTATR, MYHILATR,
MYPROATR, MYLNGATTR)) == NIL)
exitBad ()
/* Set up items on the menu, and add keys to the key*/
/* binding list (upper and lower case of the first */
/* letters of the items. */
if (mnitmkey (pmenu, 0, 1, 0, "My dog", "MmDd", MN_NOMOVE) == NIL)
exitBad ()
if (mnitmkey (pmenu, 0, 9, 0, "has", "Hh", MN_NOMOVE) == NIL)
exitBad ()
if (mnitmkey (pmenu, 0, 14, 0, "itchy", "Ii", MN_NOMOVE) == NIL)
exitBad ()
if (mnitmkey (pmenu, 0, 21, 0, "fleas.", "Ff", MN_NOMOVE) == NIL)
exitBad ()
/* Disable ESC key. */
if (mnkey (pmenu, 0, 0, KB_C_N_ESC, KB_S_N_ESC,
MN_ABORT, MN_DELETE) == NIL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -