13515Sache/* 23515Sache * inputbox.c -- implements the input box 33515Sache * 43515Sache * AUTHOR: Savio Lam (lam836@cs.cuhk.hk) 53515Sache * 63515Sache * This program is free software; you can redistribute it and/or 73515Sache * modify it under the terms of the GNU General Public License 83515Sache * as published by the Free Software Foundation; either version 2 93515Sache * of the License, or (at your option) any later version. 103515Sache * 113515Sache * This program is distributed in the hope that it will be useful, 123515Sache * but WITHOUT ANY WARRANTY; without even the implied warranty of 133515Sache * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 143515Sache * GNU General Public License for more details. 153515Sache * 163515Sache * You should have received a copy of the GNU General Public License 173515Sache * along with this program; if not, write to the Free Software 183515Sache * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 193515Sache */ 203515Sache 213515Sache 223740Sache#include <dialog.h> 233515Sache#include "dialog.priv.h" 243515Sache 253515Sache 263515Sache/* 273515Sache * Display a dialog box for inputing a string 283515Sache */ 293515Sacheint dialog_inputbox(unsigned char *title, unsigned char *prompt, int height, int width, unsigned char *result) 303515Sache{ 314527Sache int i, j, x, y, box_y, box_x, box_width, first, 323754Sache key = 0, button = -1; 333515Sache unsigned char instr[MAX_LEN+1]; 343515Sache WINDOW *dialog; 353515Sache 364527Sache if (height < 0) 374527Sache height = strheight(prompt)+2+4; 384527Sache if (width < 0) { 394527Sache i = strwidth(prompt); 406035Sache j = ((title != NULL) ? strwidth(title) : 0); 414527Sache width = MAX(i,j) + 4; 424527Sache } 436035Sache width = MAX(width,24); 444527Sache 454658Sache if (width > COLS) 464658Sache width = COLS; 474658Sache if (height > LINES) 484658Sache height = LINES; 493515Sache /* center dialog box on screen */ 5013135Sjkh x = DialogX ? DialogX : (COLS - width)/2; 5113135Sjkh y = DialogY ? DialogY : (LINES - height)/2; 523515Sache 533515Sache#ifdef HAVE_NCURSES 543515Sache if (use_shadow) 553515Sache draw_shadow(stdscr, y, x, height, width); 563515Sache#endif 573515Sache dialog = newwin(height, width, y, x); 584024Sache if (dialog == NULL) { 594024Sache endwin(); 604024Sache fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x); 614024Sache exit(1); 624024Sache } 633515Sache keypad(dialog, TRUE); 643515Sache 653515Sache draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); 663515Sache wattrset(dialog, border_attr); 673515Sache wmove(dialog, height-3, 0); 683515Sache waddch(dialog, ACS_LTEE); 693515Sache for (i = 0; i < width-2; i++) 703515Sache waddch(dialog, ACS_HLINE); 713515Sache wattrset(dialog, dialog_attr); 723515Sache waddch(dialog, ACS_RTEE); 733515Sache wmove(dialog, height-2, 1); 743515Sache for (i = 0; i < width-2; i++) 753515Sache waddch(dialog, ' '); 763515Sache 773515Sache if (title != NULL) { 783515Sache wattrset(dialog, title_attr); 793515Sache wmove(dialog, 0, (width - strlen(title))/2 - 1); 803515Sache waddch(dialog, ' '); 813515Sache waddstr(dialog, title); 823515Sache waddch(dialog, ' '); 833515Sache } 843515Sache wattrset(dialog, dialog_attr); 853950Sache wmove(dialog, 1, 2); 863950Sache print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE); 873515Sache 883515Sache /* Draw the input field box */ 893515Sache box_width = width-6; 903515Sache getyx(dialog, y, x); 913515Sache box_y = y + 2; 923515Sache box_x = (width - box_width)/2; 933515Sache draw_box(dialog, y+1, box_x-1, 3, box_width+2, border_attr, dialog_attr); 943515Sache 956458Sache display_helpline(dialog, height-1, width); 966458Sache 973515Sache x = width/2-11; 983515Sache y = height-2; 993515Sache print_button(dialog, "Cancel", y, x+14, FALSE); 1003515Sache print_button(dialog, " OK ", y, x, TRUE); 1013515Sache 1023754Sache first = 1; 1034071Sache strcpy(instr, result); 1044591Sache wattrset(dialog, dialog_attr); 1054591Sache 1063515Sache while (key != ESC) { 1073515Sache 1083515Sache if (button == -1) { /* Input box selected */ 10920442Sjkh key = line_edit(dialog, box_y, box_x, -1, box_width, inputbox_attr, first, instr, DialogInputAttrs); 1103754Sache first = 0; 1113515Sache } 1123754Sache else 1133754Sache key = wgetch(dialog); 1143515Sache 1153515Sache switch (key) { 1163515Sache case 'O': 1173515Sache case 'o': 1183515Sache delwin(dialog); 1193515Sache strcpy(result, instr); 1203515Sache return 0; 1213515Sache case 'C': 1223515Sache case 'c': 1233515Sache delwin(dialog); 1243515Sache return 1; 1253515Sache case KEY_UP: 1263515Sache case KEY_LEFT: 1273515Sache case KEY_BTAB: 1283515Sache switch (button) { 1293515Sache case -1: 1303515Sache button = 1; /* Indicates "Cancel" button is selected */ 1313515Sache print_button(dialog, " OK ", y, x, FALSE); 1323515Sache print_button(dialog, "Cancel", y, x+14, TRUE); 1333515Sache wrefresh(dialog); 1343515Sache break; 1353515Sache case 0: 1363515Sache button = -1; /* Indicates input box is selected */ 1373515Sache print_button(dialog, "Cancel", y, x+14, FALSE); 1383515Sache print_button(dialog, " OK ", y, x, TRUE); 1393515Sache break; 1403515Sache case 1: 1413515Sache button = 0; /* Indicates "OK" button is selected */ 1423515Sache print_button(dialog, "Cancel", y, x+14, FALSE); 1433515Sache print_button(dialog, " OK ", y, x, TRUE); 1443515Sache wrefresh(dialog); 1453515Sache break; 1463515Sache } 1473515Sache break; 1483515Sache case TAB: 1493515Sache case KEY_DOWN: 1503515Sache case KEY_RIGHT: 1513515Sache switch (button) { 1523515Sache case -1: 1533515Sache button = 0; /* Indicates "OK" button is selected */ 1543515Sache print_button(dialog, "Cancel", y, x+14, FALSE); 1553515Sache print_button(dialog, " OK ", y, x, TRUE); 1563515Sache wrefresh(dialog); 1573515Sache break; 1583515Sache case 0: 1593515Sache button = 1; /* Indicates "Cancel" button is selected */ 1603515Sache print_button(dialog, " OK ", y, x, FALSE); 1613515Sache print_button(dialog, "Cancel", y, x+14, TRUE); 1623515Sache wrefresh(dialog); 1633515Sache break; 1643515Sache case 1: 1653515Sache button = -1; /* Indicates input box is selected */ 1663515Sache print_button(dialog, "Cancel", y, x+14, FALSE); 1673515Sache print_button(dialog, " OK ", y, x, TRUE); 1683515Sache break; 1693515Sache } 1703515Sache break; 1713515Sache case ' ': 1723515Sache case '\n': 1733950Sache case '\r': 1743515Sache delwin(dialog); 1753515Sache if (button < 1) 1763515Sache strcpy(result, instr); 1773515Sache return (button == -1 ? 0 : button); 1783515Sache case ESC: 1793515Sache break; 1806458Sache case KEY_F(1): 1816458Sache case '?': 1826458Sache display_helpfile(); 1836458Sache break; 1843515Sache } 1853515Sache } 1863515Sache 1873515Sache delwin(dialog); 1883515Sache return -1; /* ESC pressed */ 1893515Sache} 1903515Sache/* End of dialog_inputbox() */ 191