00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "dialog.h"
00023
00024 unsigned char dialog_input_result[MAX_LEN + 1];
00025
00026
00027
00028
00029 static void
00030 print_buttons(WINDOW *dialog, int height, int width, int selected)
00031 {
00032 int x = width / 2 - 11;
00033 int y = height - 2;
00034
00035 print_button (dialog, " Ok ", y, x, selected==0);
00036 print_button (dialog, " Help ", y, x + 14, selected==1);
00037
00038 wmove(dialog, y, x+1+14*selected);
00039 wrefresh(dialog);
00040 }
00041
00042
00043
00044
00045 int
00046 dialog_inputbox (const char *title, const char *prompt, int height, int width,
00047 const char *init)
00048 {
00049 int i, x, y, box_y, box_x, box_width;
00050 int input_x = 0, scroll = 0, key = 0, button = -1;
00051 unsigned char *instr = dialog_input_result;
00052 WINDOW *dialog;
00053
00054
00055 x = (COLS - width) / 2;
00056 y = (LINES - height) / 2;
00057
00058
00059 draw_shadow (stdscr, y, x, height, width);
00060
00061 dialog = newwin (height, width, y, x);
00062 keypad (dialog, TRUE);
00063
00064 draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
00065 wattrset (dialog, border_attr);
00066 mvwaddch (dialog, height-3, 0, ACS_LTEE);
00067 for (i = 0; i < width - 2; i++)
00068 waddch (dialog, ACS_HLINE);
00069 wattrset (dialog, dialog_attr);
00070 waddch (dialog, ACS_RTEE);
00071
00072 if (title != NULL && strlen(title) >= width-2 ) {
00073
00074 char * title2 = malloc(width-2+1);
00075 memcpy( title2, title, width-2 );
00076 title2[width-2] = '\0';
00077 title = title2;
00078 }
00079
00080 if (title != NULL) {
00081 wattrset (dialog, title_attr);
00082 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
00083 waddstr (dialog, (char *)title);
00084 waddch (dialog, ' ');
00085 }
00086
00087 wattrset (dialog, dialog_attr);
00088 print_autowrap (dialog, prompt, width - 2, 1, 3);
00089
00090
00091 box_width = width - 6;
00092 getyx (dialog, y, x);
00093 box_y = y + 2;
00094 box_x = (width - box_width) / 2;
00095 draw_box (dialog, y + 1, box_x - 1, 3, box_width + 2,
00096 border_attr, dialog_attr);
00097
00098 print_buttons(dialog, height, width, 0);
00099
00100
00101 wmove (dialog, box_y, box_x);
00102 wattrset (dialog, inputbox_attr);
00103
00104 if (!init)
00105 instr[0] = '\0';
00106 else
00107 strcpy (instr, init);
00108
00109 input_x = strlen (instr);
00110
00111 if (input_x >= box_width) {
00112 scroll = input_x - box_width + 1;
00113 input_x = box_width - 1;
00114 for (i = 0; i < box_width - 1; i++)
00115 waddch (dialog, instr[scroll + i]);
00116 } else
00117 waddstr (dialog, instr);
00118
00119 wmove (dialog, box_y, box_x + input_x);
00120
00121 wrefresh (dialog);
00122
00123 while (key != ESC) {
00124 key = wgetch (dialog);
00125
00126 if (button == -1) {
00127 switch (key) {
00128 case TAB:
00129 case KEY_UP:
00130 case KEY_DOWN:
00131 break;
00132 case KEY_LEFT:
00133 continue;
00134 case KEY_RIGHT:
00135 continue;
00136 case KEY_BACKSPACE:
00137 case 127:
00138 if (input_x || scroll) {
00139 wattrset (dialog, inputbox_attr);
00140 if (!input_x) {
00141 scroll = scroll < box_width - 1 ?
00142 0 : scroll - (box_width - 1);
00143 wmove (dialog, box_y, box_x);
00144 for (i = 0; i < box_width; i++)
00145 waddch (dialog, instr[scroll + input_x + i] ?
00146 instr[scroll + input_x + i] : ' ');
00147 input_x = strlen (instr) - scroll;
00148 } else
00149 input_x--;
00150 instr[scroll + input_x] = '\0';
00151 mvwaddch (dialog, box_y, input_x + box_x, ' ');
00152 wmove (dialog, box_y, input_x + box_x);
00153 wrefresh (dialog);
00154 }
00155 continue;
00156 default:
00157 if (key < 0x100 && isprint (key)) {
00158 if (scroll + input_x < MAX_LEN) {
00159 wattrset (dialog, inputbox_attr);
00160 instr[scroll + input_x] = key;
00161 instr[scroll + input_x + 1] = '\0';
00162 if (input_x == box_width - 1) {
00163 scroll++;
00164 wmove (dialog, box_y, box_x);
00165 for (i = 0; i < box_width - 1; i++)
00166 waddch (dialog, instr[scroll + i]);
00167 } else {
00168 wmove (dialog, box_y, input_x++ + box_x);
00169 waddch (dialog, key);
00170 }
00171 wrefresh (dialog);
00172 } else
00173 flash ();
00174 continue;
00175 }
00176 }
00177 }
00178 switch (key) {
00179 case 'O':
00180 case 'o':
00181 delwin (dialog);
00182 return 0;
00183 case 'H':
00184 case 'h':
00185 delwin (dialog);
00186 return 1;
00187 case KEY_UP:
00188 case KEY_LEFT:
00189 switch (button) {
00190 case -1:
00191 button = 1;
00192 print_buttons(dialog, height, width, 1);
00193 break;
00194 case 0:
00195 button = -1;
00196 print_buttons(dialog, height, width, 0);
00197 wmove (dialog, box_y, box_x + input_x);
00198 wrefresh (dialog);
00199 break;
00200 case 1:
00201 button = 0;
00202 print_buttons(dialog, height, width, 0);
00203 break;
00204 }
00205 break;
00206 case TAB:
00207 case KEY_DOWN:
00208 case KEY_RIGHT:
00209 switch (button) {
00210 case -1:
00211 button = 0;
00212 print_buttons(dialog, height, width, 0);
00213 break;
00214 case 0:
00215 button = 1;
00216 print_buttons(dialog, height, width, 1);
00217 break;
00218 case 1:
00219 button = -1;
00220 print_buttons(dialog, height, width, 0);
00221 wmove (dialog, box_y, box_x + input_x);
00222 wrefresh (dialog);
00223 break;
00224 }
00225 break;
00226 case ' ':
00227 case '\n':
00228 delwin (dialog);
00229 return (button == -1 ? 0 : button);
00230 case 'X':
00231 case 'x':
00232 key = ESC;
00233 case ESC:
00234 break;
00235 }
00236 }
00237
00238 delwin (dialog);
00239 return -1;
00240 }