1998-07-23 21:56:00 +04:00
|
|
|
|
/* $NetBSD: menu_sys.def,v 1.15 1998/07/23 17:56:00 phil Exp $ */
|
1997-09-26 21:54:09 +04:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 1997 Piermont Information Systems Inc.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Written by Philip A. Nelson for Piermont Information Systems Inc.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
|
* This product includes software develooped for the NetBSD Project by
|
|
|
|
|
* Piermont Information Systems Inc.
|
|
|
|
|
* 4. The name of Piermont Information Systems Inc. may not be used to endorse
|
|
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
|
* written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
|
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* menu_sys.defs -- Menu system standard routines. */
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
1997-11-14 19:31:45 +03:00
|
|
|
|
#define REQ_EXECUTE 1000
|
|
|
|
|
#define REQ_NEXT_ITEM 1001
|
|
|
|
|
#define REQ_PREV_ITEM 1002
|
|
|
|
|
#define REQ_REDISPLAY 1003
|
1998-06-29 12:46:37 +04:00
|
|
|
|
#define REQ_SCROLLDOWN 1004
|
|
|
|
|
#define REQ_SCROLLUP 1005
|
|
|
|
|
#define REQ_HELP 1006
|
|
|
|
|
|
|
|
|
|
/* Multiple key support */
|
|
|
|
|
#define KEYSEQ_FIRST 256
|
|
|
|
|
#define KEYSEQ_DOWN_ARROW 256
|
|
|
|
|
#define KEYSEQ_UP_ARROW 257
|
|
|
|
|
#define KEYSEQ_LEFT_ARROW 258
|
|
|
|
|
#define KEYSEQ_RIGHT_ARROW 259
|
|
|
|
|
#define KEYSEQ_PAGE_DOWN 260
|
|
|
|
|
#define KEYSEQ_PAGE_UP 261
|
|
|
|
|
|
|
|
|
|
struct keyseq {
|
|
|
|
|
char *termcap_name;
|
|
|
|
|
char *chars;
|
|
|
|
|
int numchars;
|
|
|
|
|
int keyseq_val;
|
|
|
|
|
struct keyseq *next;
|
|
|
|
|
};
|
|
|
|
|
|
1998-06-30 10:57:57 +04:00
|
|
|
|
/* keypad and other definitions */
|
1998-06-29 12:46:37 +04:00
|
|
|
|
struct keyseq _mc_key_seq[] = {
|
1998-06-30 10:57:57 +04:00
|
|
|
|
/* Cludge for xterm ... */
|
|
|
|
|
{ NULL, "\033[B", 0, KEYSEQ_DOWN_ARROW, NULL },
|
|
|
|
|
{ NULL, "\033[D", 0, KEYSEQ_LEFT_ARROW, NULL },
|
|
|
|
|
{ NULL, "\033[C", 0, KEYSEQ_RIGHT_ARROW, NULL },
|
|
|
|
|
{ NULL, "\033[A", 0, KEYSEQ_UP_ARROW, NULL },
|
|
|
|
|
/* Termcap defined */
|
1998-06-29 12:46:37 +04:00
|
|
|
|
{ "kd", NULL, 0, KEYSEQ_DOWN_ARROW, NULL },
|
|
|
|
|
{ "kl", NULL, 0, KEYSEQ_LEFT_ARROW, NULL },
|
|
|
|
|
{ "kr", NULL, 0, KEYSEQ_RIGHT_ARROW, NULL },
|
|
|
|
|
{ "ku", NULL, 0, KEYSEQ_UP_ARROW, NULL },
|
|
|
|
|
{ "kf", NULL, 0, KEYSEQ_PAGE_DOWN, NULL }, /* scroll forward */
|
|
|
|
|
{ "kN", NULL, 0, KEYSEQ_PAGE_DOWN, NULL }, /* next page */
|
|
|
|
|
{ "kP", NULL, 0, KEYSEQ_PAGE_UP, NULL }, /* scroll backward */
|
|
|
|
|
{ "kR", NULL, 0, KEYSEQ_PAGE_UP, NULL }, /* prev page */
|
|
|
|
|
/* other definitions */
|
|
|
|
|
{ NULL, "\033v", 0, KEYSEQ_PAGE_UP, NULL }, /* ESC-v */
|
|
|
|
|
{ NULL, "\026", 0, KEYSEQ_PAGE_DOWN, NULL }, /* CTL-v */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int _mc_num_key_seq = sizeof(_mc_key_seq) / sizeof(struct keyseq);
|
|
|
|
|
struct keyseq *pad_list = NULL;
|
|
|
|
|
static char str_area [512];
|
|
|
|
|
static char *str_ptr = str_area;
|
|
|
|
|
|
|
|
|
|
/* Macros */
|
1997-09-26 21:54:09 +04:00
|
|
|
|
#define MAX(x,y) ((x)>(y)?(x):(y))
|
1998-06-25 13:58:57 +04:00
|
|
|
|
#define MIN(x,y) ((x)<(y)?(x):(y))
|
1997-09-26 21:54:09 +04:00
|
|
|
|
|
|
|
|
|
/* Initialization state. */
|
|
|
|
|
static int __menu_init = 0;
|
|
|
|
|
int __m_endwin = 0;
|
1998-06-24 10:46:23 +04:00
|
|
|
|
static int max_lines = 0;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
static char *scrolltext = " <: page up, >: page down";
|
1997-09-26 21:54:09 +04:00
|
|
|
|
|
1998-07-16 11:08:26 +04:00
|
|
|
|
static menudesc *menus = menu_def;
|
|
|
|
|
|
|
|
|
|
#ifdef DYNAMIC_MENUS
|
|
|
|
|
static int num_menus = 0;
|
|
|
|
|
static int num_avail = 0;
|
|
|
|
|
#define DYN_INIT_NUM 32
|
|
|
|
|
#endif
|
|
|
|
|
|
1997-09-26 21:54:09 +04:00
|
|
|
|
/* prototypes for in here! */
|
1998-06-29 12:46:37 +04:00
|
|
|
|
static void ins_keyseq (struct keyseq **seq, struct keyseq *ins);
|
|
|
|
|
static void init_keyseq (void);
|
1997-09-26 21:54:09 +04:00
|
|
|
|
static void init_menu (struct menudesc *m);
|
1998-07-16 11:08:26 +04:00
|
|
|
|
static char opt_ch (int op_no);
|
1997-09-26 21:54:09 +04:00
|
|
|
|
static void post_menu (struct menudesc *m);
|
1998-06-24 10:46:23 +04:00
|
|
|
|
static void process_help (struct menudesc *m, int num);
|
1997-11-14 21:27:17 +03:00
|
|
|
|
static void process_req (struct menudesc *m, int num, int req);
|
1997-09-26 21:54:09 +04:00
|
|
|
|
static void mbeep (void);
|
|
|
|
|
static int menucmd (WINDOW *w);
|
|
|
|
|
|
|
|
|
|
#ifndef NULL
|
|
|
|
|
#define NULL (void *)0
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* menu system processing routines */
|
|
|
|
|
|
|
|
|
|
static void mbeep (void)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr,"\a");
|
|
|
|
|
}
|
|
|
|
|
|
1998-06-29 12:46:37 +04:00
|
|
|
|
static void ins_keyseq (struct keyseq **seq, struct keyseq *ins)
|
|
|
|
|
{
|
|
|
|
|
if (*seq == NULL) {
|
|
|
|
|
ins->next = NULL;
|
|
|
|
|
*seq = ins;
|
|
|
|
|
} else if (ins->numchars <= (*seq)->numchars) {
|
|
|
|
|
ins->next = *seq;
|
|
|
|
|
*seq = ins;
|
|
|
|
|
} else
|
|
|
|
|
ins_keyseq (&(*seq)->next, ins);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void init_keyseq (void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i=0; i<_mc_num_key_seq; i++) {
|
|
|
|
|
if (_mc_key_seq[i].termcap_name)
|
|
|
|
|
_mc_key_seq[i].chars =
|
|
|
|
|
tgetstr (_mc_key_seq[i].termcap_name,
|
|
|
|
|
&str_ptr);
|
|
|
|
|
if (_mc_key_seq[i].chars != NULL &&
|
|
|
|
|
(_mc_key_seq[i].numchars = strlen(_mc_key_seq[i].chars))
|
|
|
|
|
> 0)
|
|
|
|
|
ins_keyseq (&pad_list,&_mc_key_seq[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-26 21:54:09 +04:00
|
|
|
|
static int mgetch(WINDOW *w)
|
|
|
|
|
{
|
|
|
|
|
static char buf[20];
|
|
|
|
|
static int num = 0;
|
1998-06-29 12:46:37 +04:00
|
|
|
|
struct keyseq *list = pad_list;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
int i, ret;
|
|
|
|
|
|
1998-06-29 12:46:37 +04:00
|
|
|
|
/* key pad processing */
|
|
|
|
|
while (list) {
|
|
|
|
|
for (i=0; i< list->numchars; i++) {
|
|
|
|
|
if (i >= num)
|
|
|
|
|
buf[num++] = wgetch(w);
|
|
|
|
|
if (buf[i] != list->chars[i])
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (i == list->numchars) {
|
|
|
|
|
num = 0;
|
|
|
|
|
return list->keyseq_val;
|
|
|
|
|
}
|
|
|
|
|
list = list->next;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = buf[0];
|
|
|
|
|
for (i = 0; i < strlen(buf); i++)
|
|
|
|
|
buf[i] = buf[i+1];
|
|
|
|
|
num--;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int menucmd (WINDOW *w)
|
|
|
|
|
{
|
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
|
ch = mgetch(w);
|
|
|
|
|
|
|
|
|
|
switch (ch) {
|
|
|
|
|
case '\n':
|
|
|
|
|
return REQ_EXECUTE;
|
1998-07-01 11:46:02 +04:00
|
|
|
|
case '\016': /* Contnrol-P */
|
1998-06-29 12:46:37 +04:00
|
|
|
|
case KEYSEQ_DOWN_ARROW:
|
1997-09-26 21:54:09 +04:00
|
|
|
|
return REQ_NEXT_ITEM;
|
1998-07-01 11:46:02 +04:00
|
|
|
|
case '\020': /* Control-N */
|
1998-06-29 12:46:37 +04:00
|
|
|
|
case KEYSEQ_UP_ARROW:
|
1997-09-26 21:54:09 +04:00
|
|
|
|
return REQ_PREV_ITEM;
|
1998-07-01 11:46:02 +04:00
|
|
|
|
case '\014': /* Control-L */
|
1997-11-14 19:31:45 +03:00
|
|
|
|
return REQ_REDISPLAY;
|
1998-06-29 12:46:37 +04:00
|
|
|
|
case '<':
|
1998-07-01 11:46:02 +04:00
|
|
|
|
case '\010': /* Control-H (backspace) */
|
1998-06-29 12:46:37 +04:00
|
|
|
|
case KEYSEQ_PAGE_UP:
|
|
|
|
|
return REQ_SCROLLUP;
|
|
|
|
|
case '>':
|
1998-07-01 11:46:02 +04:00
|
|
|
|
case ' ':
|
1998-06-29 12:46:37 +04:00
|
|
|
|
case KEYSEQ_PAGE_DOWN:
|
|
|
|
|
return REQ_SCROLLDOWN;
|
|
|
|
|
case '?':
|
|
|
|
|
return REQ_HELP;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
}
|
|
|
|
|
|
1998-06-29 12:46:37 +04:00
|
|
|
|
if (isalpha(ch))
|
1997-09-26 21:54:09 +04:00
|
|
|
|
return (ch);
|
|
|
|
|
|
|
|
|
|
mbeep();
|
|
|
|
|
wrefresh(w);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void init_menu (struct menudesc *m)
|
|
|
|
|
{
|
|
|
|
|
int max;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
int add, exitadd;
|
1998-07-16 11:08:26 +04:00
|
|
|
|
int i;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
|
1998-07-16 11:08:26 +04:00
|
|
|
|
add = ((m->mopt & MC_NOBOX) ? 2 : 4);
|
|
|
|
|
exitadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1);
|
1997-09-26 21:54:09 +04:00
|
|
|
|
max = strlen(m->title);
|
|
|
|
|
|
1998-06-25 13:58:57 +04:00
|
|
|
|
/* Calculate h? h == number of visible options. */
|
1997-09-26 21:54:09 +04:00
|
|
|
|
if (m->h == 0) {
|
1998-06-25 13:58:57 +04:00
|
|
|
|
m->h = m->numopts + exitadd;
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->h + m->y + add >= max_lines && (m->mopt & MC_SCROLL))
|
1998-06-29 12:46:37 +04:00
|
|
|
|
m->h = max_lines - m->y - add ;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
}
|
1998-06-25 13:58:57 +04:00
|
|
|
|
|
|
|
|
|
/* Check window heights and set scrolling */
|
|
|
|
|
if (m->h < m->numopts + exitadd) {
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (!(m->mopt & MC_SCROLL) || m->h < 3) {
|
1998-06-25 13:58:57 +04:00
|
|
|
|
endwin();
|
|
|
|
|
(void) fprintf (stderr,
|
1998-06-29 12:46:37 +04:00
|
|
|
|
"Window too small for menu \"%s\"\n",
|
|
|
|
|
m->title);
|
1998-06-25 13:58:57 +04:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
} else
|
1998-07-16 11:08:26 +04:00
|
|
|
|
m->mopt &= ~MC_SCROLL;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
|
1998-06-29 12:46:37 +04:00
|
|
|
|
/* check for screen fit */
|
|
|
|
|
if (m->y + m->h + add > max_lines) {
|
|
|
|
|
endwin();
|
|
|
|
|
(void) fprintf (stderr,
|
|
|
|
|
"Screen too small for menu \"%s\"\n", m->title);
|
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-26 21:54:09 +04:00
|
|
|
|
/* Calculate w? */
|
|
|
|
|
if (m->w == 0) {
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->mopt & MC_SCROLL)
|
1998-06-25 13:58:57 +04:00
|
|
|
|
max = strlen(scrolltext);
|
1998-07-16 11:08:26 +04:00
|
|
|
|
for (i=0; i < m->numopts; i++ )
|
|
|
|
|
max = MAX(max,strlen(m->opts[i].opt_name)+3);
|
1997-09-26 21:54:09 +04:00
|
|
|
|
m->w = max;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get the windows. */
|
|
|
|
|
m->mw = newwin(m->h+add, m->w+add, m->y, m->x);
|
|
|
|
|
|
|
|
|
|
if (m->mw == NULL) {
|
|
|
|
|
endwin();
|
|
|
|
|
(void) fprintf (stderr,
|
|
|
|
|
"Could not create window for window with title "
|
|
|
|
|
" \"%s\"\n", m->title);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1998-07-16 11:08:26 +04:00
|
|
|
|
static char opt_ch (int op_no)
|
|
|
|
|
{
|
|
|
|
|
char c;
|
|
|
|
|
if (op_no < 25) {
|
|
|
|
|
c = 'a' + op_no;
|
|
|
|
|
if (c >= 'x') c++;
|
|
|
|
|
} else
|
|
|
|
|
c = 'A' + op_no - 25;
|
|
|
|
|
return (char) c;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-26 21:54:09 +04:00
|
|
|
|
static void post_menu (struct menudesc *m)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
int hasbox, cury, maxy, selrow, lastopt;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
int tadd;
|
1998-07-16 11:08:26 +04:00
|
|
|
|
char optstr[5];
|
1997-09-26 21:54:09 +04:00
|
|
|
|
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->mopt & MC_NOBOX) {
|
1997-09-26 21:54:09 +04:00
|
|
|
|
cury = 0;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
maxy = m->h;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
hasbox = 0;
|
|
|
|
|
} else {
|
|
|
|
|
cury = 1;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
maxy = m->h+1;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
hasbox = 1;
|
|
|
|
|
}
|
|
|
|
|
|
1998-06-29 12:46:37 +04:00
|
|
|
|
/* Clear the window */
|
|
|
|
|
wclear (m->mw);
|
|
|
|
|
|
1997-09-26 21:54:09 +04:00
|
|
|
|
tadd = strlen(m->title) ? 2 : 0;
|
|
|
|
|
|
|
|
|
|
if (tadd) {
|
|
|
|
|
mvwaddstr(m->mw, cury, cury, m->title);
|
|
|
|
|
cury += 2;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
maxy += 2;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
}
|
|
|
|
|
|
1998-06-25 13:58:57 +04:00
|
|
|
|
/* Set defaults, calculate lastopt. */
|
|
|
|
|
selrow = -1;
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->mopt & MC_SCROLL) {
|
1998-06-25 13:58:57 +04:00
|
|
|
|
lastopt = MIN(m->numopts, m->topline+m->h-1);
|
|
|
|
|
maxy -= 1;
|
|
|
|
|
} else
|
|
|
|
|
lastopt = m->numopts;
|
|
|
|
|
|
|
|
|
|
for (i=m->topline; i<lastopt; i++, cury++) {
|
1997-09-26 21:54:09 +04:00
|
|
|
|
if (m->cursel == i) {
|
|
|
|
|
mvwaddstr (m->mw, cury, hasbox, ">");
|
|
|
|
|
wstandout(m->mw);
|
1998-06-25 13:58:57 +04:00
|
|
|
|
selrow = cury;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
} else
|
|
|
|
|
mvwaddstr (m->mw, cury, hasbox, " ");
|
1998-07-16 11:08:26 +04:00
|
|
|
|
(void) sprintf (optstr, "%c: ", opt_ch(i));
|
|
|
|
|
waddstr (m->mw, optstr);
|
|
|
|
|
waddstr (m->mw, m->opts[i].opt_name);
|
1997-09-26 21:54:09 +04:00
|
|
|
|
if (m->cursel == i)
|
|
|
|
|
wstandend(m->mw);
|
|
|
|
|
}
|
1998-06-25 13:58:57 +04:00
|
|
|
|
|
|
|
|
|
/* Add the exit option. */
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (!(m->mopt & MC_NOEXITOPT) && cury < maxy) {
|
1998-06-25 13:58:57 +04:00
|
|
|
|
if (m->cursel >= m->numopts) {
|
|
|
|
|
mvwaddstr (m->mw, cury, hasbox, ">");
|
|
|
|
|
wstandout(m->mw);
|
|
|
|
|
selrow = cury;
|
|
|
|
|
} else
|
|
|
|
|
mvwaddstr (m->mw, cury, hasbox, " ");
|
|
|
|
|
waddstr (m->mw, "x: Exit");
|
|
|
|
|
if (m->cursel >= m->numopts)
|
|
|
|
|
wstandend(m->mw);
|
|
|
|
|
cury++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add the scroll line */
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->mopt & MC_SCROLL) {
|
1998-06-25 13:58:57 +04:00
|
|
|
|
mvwaddstr (m->mw, cury, hasbox, scrolltext);
|
|
|
|
|
if (selrow < 0)
|
|
|
|
|
selrow = cury;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add the box. */
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (!(m->mopt & MC_NOBOX))
|
1997-09-26 21:54:09 +04:00
|
|
|
|
box(m->mw, '*', '*');
|
|
|
|
|
|
1998-06-25 13:58:57 +04:00
|
|
|
|
wmove(m->mw, selrow, hasbox);
|
1997-09-26 21:54:09 +04:00
|
|
|
|
}
|
|
|
|
|
|
1998-06-24 10:46:23 +04:00
|
|
|
|
static void process_help (struct menudesc *m, int num)
|
|
|
|
|
{
|
|
|
|
|
char *help = m->helpstr;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
int lineoff = 0;
|
1998-06-24 10:46:23 +04:00
|
|
|
|
int curoff = 0;
|
|
|
|
|
int again;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
int winin;
|
1998-06-24 10:46:23 +04:00
|
|
|
|
|
1998-06-24 18:44:52 +04:00
|
|
|
|
/* Is there help? */
|
|
|
|
|
if (!help) {
|
|
|
|
|
mbeep();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Display the help information. */
|
1998-06-25 13:58:57 +04:00
|
|
|
|
do {
|
1998-06-24 10:46:23 +04:00
|
|
|
|
if (lineoff < curoff) {
|
|
|
|
|
help = m->helpstr;
|
|
|
|
|
curoff = 0;
|
|
|
|
|
}
|
|
|
|
|
while (*help && curoff < lineoff) {
|
|
|
|
|
if (*help == '\n')
|
|
|
|
|
curoff++;
|
|
|
|
|
help++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wclear(stdscr);
|
|
|
|
|
mvwaddstr (stdscr, 0, 0,
|
1998-06-29 12:46:37 +04:00
|
|
|
|
"Help: exit: x, page up: u <, page down: d >");
|
1998-06-24 10:46:23 +04:00
|
|
|
|
mvwaddstr (stdscr, 2, 0, help);
|
|
|
|
|
wmove (stdscr, 1, 0);
|
|
|
|
|
wrefresh(stdscr);
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
winin = mgetch(stdscr);
|
1998-06-29 12:46:37 +04:00
|
|
|
|
if (winin < KEYSEQ_FIRST)
|
|
|
|
|
winin = tolower(winin);
|
1998-06-24 10:46:23 +04:00
|
|
|
|
again = 0;
|
|
|
|
|
switch (winin) {
|
|
|
|
|
case '<':
|
|
|
|
|
case 'u':
|
1998-06-29 12:46:37 +04:00
|
|
|
|
case KEYSEQ_UP_ARROW:
|
|
|
|
|
case KEYSEQ_LEFT_ARROW:
|
|
|
|
|
case KEYSEQ_PAGE_UP:
|
1998-06-24 10:46:23 +04:00
|
|
|
|
if (lineoff)
|
|
|
|
|
lineoff -= max_lines - 2;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
else
|
|
|
|
|
again = 1;
|
1998-06-24 10:46:23 +04:00
|
|
|
|
break;
|
|
|
|
|
case '>':
|
|
|
|
|
case 'd':
|
1998-06-29 12:46:37 +04:00
|
|
|
|
case KEYSEQ_DOWN_ARROW:
|
|
|
|
|
case KEYSEQ_RIGHT_ARROW:
|
|
|
|
|
case KEYSEQ_PAGE_DOWN:
|
1998-06-24 10:46:23 +04:00
|
|
|
|
if (*help)
|
|
|
|
|
lineoff += max_lines - 2;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
else
|
|
|
|
|
again = 1;
|
1998-06-24 10:46:23 +04:00
|
|
|
|
break;
|
|
|
|
|
case 'q':
|
|
|
|
|
break;
|
1998-06-30 10:57:57 +04:00
|
|
|
|
case 'x':
|
|
|
|
|
winin = 'q';
|
|
|
|
|
break;
|
1998-06-24 10:46:23 +04:00
|
|
|
|
default:
|
|
|
|
|
again = 1;
|
|
|
|
|
}
|
1998-06-29 12:46:37 +04:00
|
|
|
|
if (again)
|
|
|
|
|
mbeep();
|
1998-06-24 10:46:23 +04:00
|
|
|
|
} while (again);
|
|
|
|
|
} while (winin != 'q');
|
|
|
|
|
|
|
|
|
|
/* Restore current menu */
|
|
|
|
|
wclear(stdscr);
|
|
|
|
|
wrefresh(stdscr);
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->post_act)
|
|
|
|
|
(*m->post_act)();
|
1998-06-24 10:46:23 +04:00
|
|
|
|
}
|
|
|
|
|
|
1997-11-14 21:27:17 +03:00
|
|
|
|
static void process_req (struct menudesc *m, int num, int req)
|
1997-09-26 21:54:09 +04:00
|
|
|
|
{
|
|
|
|
|
int ch;
|
1998-07-16 11:08:26 +04:00
|
|
|
|
int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1 );
|
1998-06-25 13:58:57 +04:00
|
|
|
|
int refresh = 0;
|
1998-06-29 12:46:37 +04:00
|
|
|
|
int scroll_sel = 0;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
|
|
|
|
|
if (req == REQ_EXECUTE)
|
|
|
|
|
return;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
|
1997-09-26 21:54:09 +04:00
|
|
|
|
else if (req == REQ_NEXT_ITEM) {
|
1998-06-25 13:58:57 +04:00
|
|
|
|
if (m->cursel < m->numopts + hasexit - 1) {
|
1997-09-26 21:54:09 +04:00
|
|
|
|
m->cursel++;
|
1998-06-29 12:46:37 +04:00
|
|
|
|
scroll_sel = 1;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
refresh = 1;
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->mopt & MC_SCROLL &&
|
1998-07-02 03:02:50 +04:00
|
|
|
|
m->cursel >= m->topline + m->h -1 )
|
1998-07-01 11:46:02 +04:00
|
|
|
|
m->topline += 1;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
} else
|
1997-09-26 21:54:09 +04:00
|
|
|
|
mbeep();
|
1998-06-25 13:58:57 +04:00
|
|
|
|
|
1997-09-26 21:54:09 +04:00
|
|
|
|
} else if (req == REQ_PREV_ITEM) {
|
1998-06-25 13:58:57 +04:00
|
|
|
|
if (m->cursel > 0) {
|
1997-09-26 21:54:09 +04:00
|
|
|
|
m->cursel--;
|
1998-06-29 12:46:37 +04:00
|
|
|
|
scroll_sel = 1;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
refresh = 1;
|
1998-07-01 11:46:02 +04:00
|
|
|
|
if (m->cursel < m->topline )
|
|
|
|
|
m->topline -= 1;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
} else
|
1997-09-26 21:54:09 +04:00
|
|
|
|
mbeep();
|
1997-11-14 19:31:45 +03:00
|
|
|
|
|
|
|
|
|
} else if (req == REQ_REDISPLAY) {
|
|
|
|
|
wclear(stdscr);
|
|
|
|
|
wrefresh(stdscr);
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->post_act)
|
|
|
|
|
(*m->post_act)();
|
1998-06-25 13:58:57 +04:00
|
|
|
|
refresh = 1;
|
1997-11-14 19:31:45 +03:00
|
|
|
|
|
1998-06-29 12:46:37 +04:00
|
|
|
|
} else if (req == REQ_HELP) {
|
1998-06-24 10:46:23 +04:00
|
|
|
|
process_help (m, num);
|
1998-06-25 13:58:57 +04:00
|
|
|
|
refresh = 1;
|
|
|
|
|
|
1998-06-29 12:46:37 +04:00
|
|
|
|
} else if (req == REQ_SCROLLUP) {
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (!(m->mopt & MC_SCROLL))
|
1998-06-29 12:46:37 +04:00
|
|
|
|
mbeep();
|
|
|
|
|
else if (m->topline == 0)
|
1998-06-25 13:58:57 +04:00
|
|
|
|
mbeep();
|
|
|
|
|
else {
|
1998-07-16 11:08:26 +04:00
|
|
|
|
m->topline = MAX(0,m->topline-m->h+1);
|
1998-06-25 13:58:57 +04:00
|
|
|
|
wclear (m->mw);
|
|
|
|
|
refresh = 1;
|
|
|
|
|
}
|
|
|
|
|
|
1998-06-29 12:46:37 +04:00
|
|
|
|
} else if (req == REQ_SCROLLDOWN) {
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (!(m->mopt & MC_SCROLL))
|
1998-06-29 12:46:37 +04:00
|
|
|
|
mbeep();
|
1998-07-03 19:20:30 +04:00
|
|
|
|
else if (m->topline + m->h - 1 >= m->numopts + hasexit)
|
1998-06-25 13:58:57 +04:00
|
|
|
|
mbeep();
|
|
|
|
|
else {
|
1998-07-03 19:20:30 +04:00
|
|
|
|
m->topline = MIN(m->topline+m->h-1,
|
|
|
|
|
m->numopts+hasexit-m->h+1);
|
1998-06-25 13:58:57 +04:00
|
|
|
|
wclear (m->mw);
|
|
|
|
|
refresh = 1;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-26 21:54:09 +04:00
|
|
|
|
} else {
|
1998-06-29 12:46:37 +04:00
|
|
|
|
ch = req;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
if (ch == 'x' && hasexit) {
|
1997-09-26 21:54:09 +04:00
|
|
|
|
m->cursel = m->numopts;
|
1998-06-29 12:46:37 +04:00
|
|
|
|
scroll_sel = 1;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
refresh = 1;
|
|
|
|
|
} else {
|
1998-06-25 23:57:10 +04:00
|
|
|
|
if (ch > 'z')
|
|
|
|
|
ch = 255;
|
|
|
|
|
if (ch >= 'a') {
|
|
|
|
|
if (ch > 'x') ch--;
|
|
|
|
|
ch = ch - 'a';
|
|
|
|
|
} else
|
|
|
|
|
ch = 25 + ch - 'A';
|
1997-09-26 21:54:09 +04:00
|
|
|
|
if (ch < 0 || ch >= m->numopts)
|
|
|
|
|
mbeep();
|
1998-06-25 13:58:57 +04:00
|
|
|
|
else {
|
1997-09-26 21:54:09 +04:00
|
|
|
|
m->cursel = ch;
|
1998-06-29 12:46:37 +04:00
|
|
|
|
scroll_sel = 1;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
refresh = 1;
|
|
|
|
|
}
|
1997-09-26 21:54:09 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
1998-06-29 12:46:37 +04:00
|
|
|
|
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->mopt & MC_SCROLL && scroll_sel) {
|
1998-06-29 12:46:37 +04:00
|
|
|
|
while (m->cursel >= m->topline + m->h -1 )
|
1998-07-03 19:20:30 +04:00
|
|
|
|
m->topline = MIN(m->topline+m->h-1,
|
|
|
|
|
m->numopts+hasexit-m->h+1);
|
1998-06-29 12:46:37 +04:00
|
|
|
|
while (m->cursel < m->topline)
|
1998-07-03 19:20:30 +04:00
|
|
|
|
m->topline = MAX(0,m->topline-m->h+1);
|
1998-06-29 12:46:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
1998-06-25 13:58:57 +04:00
|
|
|
|
if (refresh) {
|
|
|
|
|
post_menu (m);
|
|
|
|
|
wrefresh (m->mw);
|
1997-09-26 21:54:09 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void process_menu (int num)
|
|
|
|
|
{
|
|
|
|
|
int sel = 0;
|
|
|
|
|
int req, done;
|
|
|
|
|
int last_num;
|
|
|
|
|
|
1998-07-16 11:08:26 +04:00
|
|
|
|
struct menudesc *m;
|
|
|
|
|
|
|
|
|
|
m = &menus[num];
|
1997-09-26 21:54:09 +04:00
|
|
|
|
|
|
|
|
|
done = FALSE;
|
|
|
|
|
|
|
|
|
|
/* Initialize? */
|
|
|
|
|
if (!__menu_init) {
|
1997-11-09 23:59:11 +03:00
|
|
|
|
if (initscr() == NULL) {
|
|
|
|
|
__menu_initerror();
|
|
|
|
|
return;
|
|
|
|
|
}
|
1997-09-26 21:54:09 +04:00
|
|
|
|
cbreak();
|
|
|
|
|
noecho();
|
1998-06-24 10:46:23 +04:00
|
|
|
|
max_lines = stdscr->maxy;
|
1998-06-29 12:46:37 +04:00
|
|
|
|
init_keyseq();
|
1998-07-16 11:08:26 +04:00
|
|
|
|
#ifdef DYNAMIC_MENUS
|
|
|
|
|
num_menus = DYN_INIT_NUM;
|
|
|
|
|
while (num_menus < DYN_MENU_START)
|
|
|
|
|
num_menus *= 2;
|
|
|
|
|
menus = (menudesc *) malloc(sizeof(menudesc)*num_menus);
|
|
|
|
|
if (menus == NULL) {
|
|
|
|
|
__menu_initerror();
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
(void) memset ((void *)menus, 0, sizeof(menudesc)*num_menus);
|
|
|
|
|
(void) memcpy ((void *)menus, (void *)menu_def,
|
|
|
|
|
sizeof(menudesc)*DYN_MENU_START);
|
|
|
|
|
num_avail = num_menus - DYN_MENU_START;
|
|
|
|
|
#endif
|
1997-09-26 21:54:09 +04:00
|
|
|
|
__menu_init = 1;
|
|
|
|
|
}
|
|
|
|
|
if (__m_endwin) {
|
1998-06-25 13:58:57 +04:00
|
|
|
|
wclear(stdscr);
|
1997-09-26 21:54:09 +04:00
|
|
|
|
wrefresh(stdscr);
|
|
|
|
|
__m_endwin = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m->mw == NULL)
|
|
|
|
|
init_menu (m);
|
|
|
|
|
|
1998-06-25 13:58:57 +04:00
|
|
|
|
/* Always preselect option 0 and display from 0! */
|
1997-09-26 21:54:09 +04:00
|
|
|
|
m->cursel = 0;
|
1998-06-25 13:58:57 +04:00
|
|
|
|
m->topline = 0;
|
1997-09-26 21:54:09 +04:00
|
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
|
last_num = num;
|
|
|
|
|
if (__m_endwin) {
|
|
|
|
|
wclear(stdscr);
|
|
|
|
|
wrefresh(stdscr);
|
|
|
|
|
__m_endwin = 0;
|
|
|
|
|
}
|
|
|
|
|
/* Process the display action */
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->post_act)
|
|
|
|
|
(*m->post_act)();
|
1997-09-26 21:54:09 +04:00
|
|
|
|
post_menu (m);
|
|
|
|
|
wrefresh (m->mw);
|
|
|
|
|
|
|
|
|
|
while ((req = menucmd (m->mw)) != REQ_EXECUTE)
|
1997-11-14 21:27:17 +03:00
|
|
|
|
process_req (m, num, req);
|
1997-09-26 21:54:09 +04:00
|
|
|
|
|
|
|
|
|
sel = m->cursel;
|
|
|
|
|
wclear (m->mw);
|
|
|
|
|
wrefresh (m->mw);
|
|
|
|
|
|
|
|
|
|
/* Process the items */
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (sel < m->numopts) {
|
|
|
|
|
if (m->opts[sel].opt_flags & OPT_ENDWIN) {
|
|
|
|
|
endwin();
|
|
|
|
|
__m_endwin = 1;
|
|
|
|
|
}
|
|
|
|
|
if (m->opts[sel].opt_action)
|
1998-07-23 21:56:00 +04:00
|
|
|
|
done = (*m->opts[sel].opt_action)();
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->opts[sel].opt_menu != -1) {
|
|
|
|
|
if (m->opts[sel].opt_flags & OPT_SUB)
|
|
|
|
|
process_menu (m->opts[sel].opt_menu);
|
|
|
|
|
else
|
|
|
|
|
num = m->opts[sel].opt_menu;
|
|
|
|
|
}
|
1998-07-23 21:56:00 +04:00
|
|
|
|
|
|
|
|
|
if (m->opts[sel].opt_flags & OPT_EXIT)
|
|
|
|
|
done = TRUE;
|
|
|
|
|
|
1998-07-16 11:08:26 +04:00
|
|
|
|
} else
|
1997-09-26 21:54:09 +04:00
|
|
|
|
done = TRUE;
|
|
|
|
|
|
|
|
|
|
/* Reselect m just in case */
|
|
|
|
|
if (num != last_num) {
|
|
|
|
|
m = &menus[num];
|
1998-07-16 11:08:26 +04:00
|
|
|
|
|
1997-09-26 21:54:09 +04:00
|
|
|
|
/* Initialize? */
|
|
|
|
|
if (m->mw == NULL)
|
|
|
|
|
init_menu (m);
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->post_act)
|
|
|
|
|
(*m->post_act)();
|
1997-09-26 21:54:09 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Process the exit action */
|
1998-07-16 11:08:26 +04:00
|
|
|
|
if (m->exit_act)
|
|
|
|
|
(*m->exit_act)();
|
1997-09-26 21:54:09 +04:00
|
|
|
|
}
|
1998-06-25 13:58:57 +04:00
|
|
|
|
|
1998-07-03 19:20:30 +04:00
|
|
|
|
/* Control L is end of standard routines, remaining only for dynamic. */
|
|
|
|
|
|
|
|
|
|
/* Beginning of routines for dynamic menus. */
|
|
|
|
|
|
1998-07-16 11:08:26 +04:00
|
|
|
|
/* local prototypes */
|
|
|
|
|
static int double_menus (void);
|
|
|
|
|
|
|
|
|
|
static int double_menus (void)
|
|
|
|
|
{
|
|
|
|
|
menudesc *temp;
|
|
|
|
|
|
|
|
|
|
temp = (menudesc *) malloc(sizeof(menudesc)*num_menus*2);
|
|
|
|
|
if (temp == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
(void) memset ((void *)temp, 0,
|
|
|
|
|
sizeof(menudesc)*num_menus*2);
|
|
|
|
|
(void) memcpy ((void *)temp, (void *)menus,
|
|
|
|
|
sizeof(menudesc)*num_menus);
|
|
|
|
|
free (menus);
|
|
|
|
|
menus = temp;
|
|
|
|
|
num_avail = num_menus;
|
|
|
|
|
num_menus *= 2;
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
1998-07-03 19:20:30 +04:00
|
|
|
|
|
1998-07-16 11:08:26 +04:00
|
|
|
|
int new_menu (char * title, menu_ent * opts, int numopts,
|
|
|
|
|
int x, int y, int h, int w, int mopt,
|
|
|
|
|
void (*post_act)(void), void (*exit_act), char * help)
|
|
|
|
|
{
|
|
|
|
|
int ix;
|
|
|
|
|
|
|
|
|
|
/* Check for free menu entry. */
|
|
|
|
|
if (num_avail == 0)
|
|
|
|
|
if (!double_menus ())
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
/* Find free menu entry. */
|
|
|
|
|
for (ix = DYN_MENU_START; ix < num_menus && menus[ix].mopt & MC_VALID;
|
|
|
|
|
ix++) /* do nothing */;
|
|
|
|
|
|
|
|
|
|
/* if ix == num_menus ... panic */
|
|
|
|
|
|
|
|
|
|
/* Set Entries */
|
|
|
|
|
menus[ix].title = title;
|
|
|
|
|
menus[ix].opts = opts;
|
|
|
|
|
menus[ix].numopts = numopts;
|
|
|
|
|
menus[ix].x = x;
|
|
|
|
|
menus[ix].y = y;
|
|
|
|
|
menus[ix].h = h;
|
|
|
|
|
menus[ix].w = w;
|
|
|
|
|
menus[ix].mopt = mopt | MC_VALID;
|
|
|
|
|
menus[ix].post_act = post_act;
|
|
|
|
|
menus[ix].exit_act = exit_act;
|
|
|
|
|
menus[ix].helpstr = help;
|
|
|
|
|
|
|
|
|
|
init_menu (&menus[ix]);
|
|
|
|
|
|
|
|
|
|
return ix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void free_menu (int menu_no)
|
|
|
|
|
{
|
|
|
|
|
if (menu_no < num_menus) {
|
|
|
|
|
menus[menu_no].mopt &= ~MC_VALID;
|
|
|
|
|
if (menus[menu_no].mw != NULL)
|
|
|
|
|
delwin (menus[menu_no].mw);
|
|
|
|
|
}
|
|
|
|
|
}
|