Full autosizing support, now you can pass -1, -1 for any

string
This commit is contained in:
Andrey A. Chernov 1994-11-16 14:37:37 +00:00
parent 0066ef2281
commit 5f5d7caab1
9 changed files with 134 additions and 24 deletions

View File

@ -34,7 +34,7 @@ static int list_width, check_x, item_x;
*/
int dialog_checklist(unsigned char *title, unsigned char *prompt, int height, int width, int list_height, int item_no, unsigned char **items, unsigned char *result)
{
int i, x, y, cur_x, cur_y, box_x, box_y, key = 0, button = 0, choice = 0,
int i, j, x, y, cur_x, cur_y, box_x, box_y, key = 0, button = 0, choice = 0,
scroll = 0, max_choice, *status;
WINDOW *dialog, *list;
@ -50,6 +50,22 @@ int dialog_checklist(unsigned char *title, unsigned char *prompt, int height, in
max_choice = MIN(list_height, item_no);
check_x = 0;
item_x = 0;
/* Find length of longest item in order to center checklist */
for (i = 0; i < item_no; i++) {
check_x = MAX(check_x, strlen(items[i*3]) + strlen(items[i*3 + 1]) + 6);
item_x = MAX(item_x, strlen(items[i*3]));
}
if (height < 0)
height = strheight(prompt)+list_height+4+2;
if (width < 0) {
i = strwidth(prompt);
j = strwidth(title);
width = MAX(i,j);
width = MAX(width,check_x+10)+4;
}
/* center dialog box on screen */
x = (COLS - width)/2;
y = (LINES - height)/2;

View File

@ -80,15 +80,17 @@ void draw_shadow(WINDOW *win, int y, int x, int height, int width);
#endif
void draw_box(WINDOW *win, int y, int x, int height, int width, chtype box, chtype border);
int line_edit(WINDOW* dialog, int box_y, int box_x, int box_width, chtype attrs, int first, unsigned char *result);
int strheight(const char *p);
int strwidth(const char *p);
void dialog_create_rc(unsigned char *filename);
int dialog_yesno(unsigned char *title, unsigned char *prompt, int height, int width);
int dialog_prgbox(unsigned char *title, const char *line, int height, int width, int pause, int use_shell);
int dialog_prgbox(unsigned char *title, const unsigned char *line, int height, int width, int pause, int use_shell);
int dialog_msgbox(unsigned char *title, unsigned char *prompt, int height, int width, int pause);
int dialog_textbox(unsigned char *title, unsigned char *file, int height, int width);
int dialog_menu(unsigned char *title, unsigned char *prompt, int height, int width, int menu_height, int item_no, unsigned char **items, unsigned char *result);
int dialog_checklist(unsigned char *title, unsigned char *prompt, int height, int width, int list_height, int item_no, unsigned char **items, unsigned char *result);
int dialog_radiolist(char *title, char *prompt, int height, int width, int list_height, int item_no, unsigned char **items, unsigned char *result);
int dialog_radiolist(unsigned char *title, unsigned char *prompt, int height, int width, int list_height, int item_no, unsigned char **items, unsigned char *result);
int dialog_inputbox(unsigned char *title, unsigned char *prompt, int height, int width, unsigned char *result);
void dialog_clear(void);
void dialog_update(void);

View File

@ -28,11 +28,19 @@
*/
int dialog_inputbox(unsigned char *title, unsigned char *prompt, int height, int width, unsigned char *result)
{
int i, x, y, box_y, box_x, box_width, first,
int i, j, x, y, box_y, box_x, box_width, first,
key = 0, button = -1;
unsigned char instr[MAX_LEN+1];
WINDOW *dialog;
if (height < 0)
height = strheight(prompt)+2+4;
if (width < 0) {
i = strwidth(prompt);
j = strwidth(title);
width = MAX(i,j) + 4;
}
/* center dialog box on screen */
x = (COLS - width)/2;
y = (LINES - height)/2;

View File

@ -422,3 +422,53 @@ void end_dialog(void)
{
endwin();
}
int strwidth(const char *p)
{
int i = 0, len, incr;
const char *start, *s, *s1, *s2;
for (start = s = p; ; start = (s += incr)) {
s1 = strchr(s, '\n');
s2 = strstr(s, "\\n");
if (s2 == NULL)
s = s1;
else if (s1 == NULL)
s = s2;
else
s = MIN(s1, s2);
if (s == NULL)
break;
incr = 1 + (s == s2);
len = s - start;
if (len > i)
i = len;
}
len = strlen(start);
if (len > i)
i = len;
return i;
}
int strheight(const char *p)
{
int i = 1, incr;
const char *s, *s1, *s2;
for (s = p; ; s += incr) {
s1 = strchr(s, '\n');
s2 = strstr(s, "\\n");
if (s2 == NULL)
s = s1;
else if (s1 == NULL)
s = s2;
else
s = MIN(s1, s2);
if (s == NULL)
break;
incr = 1 + (s == s2);
i++;
}
return i;
}

View File

@ -34,12 +34,28 @@ static int menu_width, tag_x, item_x;
*/
int dialog_menu(unsigned char *title, unsigned char *prompt, int height, int width, int menu_height, int item_no, unsigned char **items, unsigned char *result)
{
int i, x, y, cur_x, cur_y, box_x, box_y, key = 0, button = 0, choice = 0,
int i, j, x, y, cur_x, cur_y, box_x, box_y, key = 0, button = 0, choice = 0,
scroll = 0, max_choice;
WINDOW *dialog, *menu;
max_choice = MIN(menu_height, item_no);
tag_x = 0;
item_x = 0;
/* Find length of longest item in order to center menu */
for (i = 0; i < item_no; i++) {
tag_x = MAX(tag_x, strlen(items[i*2]) + strlen(items[i*2 + 1]) + 2);
item_x = MAX(item_x, strlen(items[i*2]));
}
if (height < 0)
height = strheight(prompt)+menu_height+4+2;
if (width < 0) {
i = strwidth(prompt);
j = strwidth(title);
width = MAX(i,j);
width = MAX(width,tag_x+4)+4;
}
/* center dialog box on screen */
x = (COLS - width)/2;
y = (LINES - height)/2;
@ -96,13 +112,6 @@ int dialog_menu(unsigned char *title, unsigned char *prompt, int height, int wid
/* draw a box around the menu items */
draw_box(dialog, box_y, box_x, menu_height+2, menu_width+2, menubox_border_attr, menubox_attr);
tag_x = 0;
item_x = 0;
/* Find length of longest item in order to center menu */
for (i = 0; i < item_no; i++) {
tag_x = MAX(tag_x, strlen(items[i*2]) + strlen(items[i*2 + 1]) + 2);
item_x = MAX(item_x, strlen(items[i*2]));
}
tag_x = (menu_width - tag_x) / 2;
item_x = tag_x + item_x + 2;

View File

@ -29,9 +29,17 @@
*/
int dialog_msgbox(unsigned char *title, unsigned char *prompt, int height, int width, int pause)
{
int i, x, y, key = 0;
int i, j, x, y, key = 0;
WINDOW *dialog;
if (height < 0)
height = strheight(prompt)+2+2*(!!pause);
if (width < 0) {
i = strwidth(prompt);
j = strwidth(title);
width = MAX(i,j)+4;
}
/* center dialog box on screen */
x = (COLS - width)/2;
y = (LINES - height)/2;

View File

@ -27,7 +27,7 @@
* Display a message box. Program will pause and display an "OK" button
* if the parameter 'pause' is non-zero.
*/
int dialog_prgbox(unsigned char *title, const char *line, int height, int width, int pause, int use_shell)
int dialog_prgbox(unsigned char *title, const unsigned char *line, int height, int width, int pause, int use_shell)
{
int i, x, y, key = 0;
WINDOW *dialog;

View File

@ -33,9 +33,9 @@ static int list_width, check_x, item_x;
/*
* Display a dialog box with a list of options that can be turned on or off
*/
int dialog_radiolist(char *title, char *prompt, int height, int width, int list_height, int item_no, unsigned char **items, unsigned char *result)
int dialog_radiolist(unsigned char *title, unsigned char *prompt, int height, int width, int list_height, int item_no, unsigned char **items, unsigned char *result)
{
int i, x, y, cur_x, cur_y, box_x, box_y, key = 0, button = 0, choice = 0,
int i, j, x, y, cur_x, cur_y, box_x, box_y, key = 0, button = 0, choice = 0,
scroll = 0, max_choice, *status, was_on = 0;
WINDOW *dialog, *list;
@ -58,6 +58,22 @@ int dialog_radiolist(char *title, char *prompt, int height, int width, int list_
}
max_choice = MIN(list_height, item_no);
check_x = 0;
item_x = 0;
/* Find length of longest item in order to center radiolist */
for (i = 0; i < item_no; i++) {
check_x = MAX(check_x, strlen(items[i*3]) + strlen(items[i*3 + 1]) + 6);
item_x = MAX(item_x, strlen(items[i*3]));
}
if (height < 0)
height = strheight(prompt)+list_height+4+2;
if (width < 0) {
i = strwidth(prompt);
j = strwidth(title);
width = MAX(i,j);
width = MAX(width,check_x+10)+4;
}
/* center dialog box on screen */
x = (COLS - width)/2;
y = (LINES - height)/2;
@ -114,13 +130,6 @@ int dialog_radiolist(char *title, char *prompt, int height, int width, int list_
/* draw a box around the list items */
draw_box(dialog, box_y, box_x, list_height+2, list_width+2, menubox_border_attr, menubox_attr);
check_x = 0;
item_x = 0;
/* Find length of longest item in order to center radiolist */
for (i = 0; i < item_no; i++) {
check_x = MAX(check_x, strlen(items[i*3]) + strlen(items[i*3 + 1]) + 6);
item_x = MAX(item_x, strlen(items[i*3]));
}
check_x = (list_width - check_x) / 2;
item_x = check_x + item_x + 6;

View File

@ -28,9 +28,17 @@
*/
int dialog_yesno(unsigned char *title, unsigned char * prompt, int height, int width)
{
int i, x, y, key = 0, button = 0;
int i, j, x, y, key = 0, button = 0;
WINDOW *dialog;
if (height < 0)
height = strheight(prompt)+4;
if (width < 0) {
i = strwidth(prompt);
j = strwidth(title);
width = MAX(i,j)+4;
}
/* center dialog box on screen */
x = (COLS - width)/2;
y = (LINES - height)/2;