00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #include "dialog.h"
00060
00061 static int menu_width, item_x;
00062
00063
00064
00065
00066 static void
00067 print_item (WINDOW * win, const char *item, int choice, int selected, int hotkey)
00068 {
00069 int j;
00070 char menu_item[menu_width+1];
00071
00072 strncpy(menu_item, item, menu_width);
00073 menu_item[menu_width] = 0;
00074 j = first_alpha(menu_item, "YyNnMm");
00075
00076
00077 wattrset (win, menubox_attr);
00078 wmove (win, choice, 0);
00079 #if OLD_NCURSES
00080 {
00081 int i;
00082 for (i = 0; i < menu_width; i++)
00083 waddch (win, ' ');
00084 }
00085 #else
00086 wclrtoeol(win);
00087 #endif
00088 wattrset (win, selected ? item_selected_attr : item_attr);
00089 mvwaddstr (win, choice, item_x, menu_item);
00090 if (hotkey) {
00091 wattrset (win, selected ? tag_key_selected_attr : tag_key_attr);
00092 mvwaddch(win, choice, item_x+j, menu_item[j]);
00093 }
00094 }
00095
00096
00097
00098
00099 static void
00100 print_arrows (WINDOW * win, int item_no, int scroll,
00101 int y, int x, int height)
00102 {
00103 int cur_y, cur_x;
00104
00105 getyx(win, cur_y, cur_x);
00106
00107 wmove(win, y, x);
00108
00109 if (scroll > 0) {
00110 wattrset (win, uarrow_attr);
00111 waddch (win, ACS_UARROW);
00112 waddstr (win, "(-)");
00113 }
00114 else {
00115 wattrset (win, menubox_attr);
00116 waddch (win, ACS_HLINE);
00117 waddch (win, ACS_HLINE);
00118 waddch (win, ACS_HLINE);
00119 waddch (win, ACS_HLINE);
00120 }
00121
00122 y = y + height + 1;
00123 wmove(win, y, x);
00124
00125 if ((height < item_no) && (scroll + height < item_no)) {
00126 wattrset (win, darrow_attr);
00127 waddch (win, ACS_DARROW);
00128 waddstr (win, "(+)");
00129 }
00130 else {
00131 wattrset (win, menubox_border_attr);
00132 waddch (win, ACS_HLINE);
00133 waddch (win, ACS_HLINE);
00134 waddch (win, ACS_HLINE);
00135 waddch (win, ACS_HLINE);
00136 }
00137
00138 wmove(win, cur_y, cur_x);
00139 }
00140
00141
00142
00143
00144 static void
00145 print_buttons (WINDOW *win, int height, int width, int selected)
00146 {
00147 int x = width / 2 - 16;
00148 int y = height - 2;
00149
00150 print_button (win, "Select", y, x, selected == 0);
00151 print_button (win, " Exit ", y, x + 12, selected == 1);
00152 print_button (win, " Help ", y, x + 24, selected == 2);
00153
00154 wmove(win, y, x+1+12*selected);
00155 wrefresh (win);
00156 }
00157
00158
00159
00160
00161 int
00162 dialog_menu (const char *title, const char *prompt, int height, int width,
00163 int menu_height, const char *current, int item_no,
00164 const char * const * items)
00165
00166 {
00167 int i, j, x, y, box_x, box_y;
00168 int key = 0, button = 0, scroll = 0, choice = 0, first_item = 0, max_choice;
00169 WINDOW *dialog, *menu;
00170 FILE *f;
00171
00172 max_choice = MIN (menu_height, item_no);
00173
00174
00175 x = (COLS - width) / 2;
00176 y = (LINES - height) / 2;
00177
00178 draw_shadow (stdscr, y, x, height, width);
00179
00180 dialog = newwin (height, width, y, x);
00181 keypad (dialog, TRUE);
00182
00183 draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
00184 wattrset (dialog, border_attr);
00185 mvwaddch (dialog, height - 3, 0, ACS_LTEE);
00186 for (i = 0; i < width - 2; i++)
00187 waddch (dialog, ACS_HLINE);
00188 wattrset (dialog, dialog_attr);
00189 wbkgdset (dialog, dialog_attr & A_COLOR);
00190 waddch (dialog, ACS_RTEE);
00191
00192 if (title != NULL && strlen(title) >= width-2 ) {
00193
00194 char * title2 = malloc(width-2+1);
00195 memcpy( title2, title, width-2 );
00196 title2[width-2] = '\0';
00197 title = title2;
00198 }
00199
00200 if (title != NULL) {
00201 wattrset (dialog, title_attr);
00202 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
00203 waddstr (dialog, (char *)title);
00204 waddch (dialog, ' ');
00205 }
00206
00207 wattrset (dialog, dialog_attr);
00208 print_autowrap (dialog, prompt, width - 2, 1, 3);
00209
00210 menu_width = width - 6;
00211 box_y = height - menu_height - 5;
00212 box_x = (width - menu_width) / 2 - 1;
00213
00214
00215 menu = subwin (dialog, menu_height, menu_width,
00216 y + box_y + 1, x + box_x + 1);
00217 keypad (menu, TRUE);
00218
00219
00220 draw_box (dialog, box_y, box_x, menu_height + 2, menu_width + 2,
00221 menubox_border_attr, menubox_attr);
00222
00223
00224
00225
00226
00227 item_x = 0;
00228 for (i = 0; i < item_no; i++) {
00229 item_x = MAX (item_x, MIN(menu_width, strlen (items[i * 2 + 1]) + 2));
00230 if (strcmp(current, items[i*2]) == 0) choice = i;
00231 }
00232
00233 item_x = (menu_width - item_x) / 2;
00234
00235
00236 if ( (f=fopen("lxdialog.scrltmp","r")) != NULL ) {
00237 if ( (fscanf(f,"%d\n",&scroll) == 1) && (scroll <= choice) &&
00238 (scroll+max_choice > choice) && (scroll >= 0) &&
00239 (scroll+max_choice <= item_no) ) {
00240 first_item = scroll;
00241 choice = choice - scroll;
00242 fclose(f);
00243 } else {
00244 scroll=0;
00245 remove("lxdialog.scrltmp");
00246 fclose(f);
00247 f=NULL;
00248 }
00249 }
00250 if ( (choice >= max_choice) || (f==NULL && choice >= max_choice/2) ) {
00251 if (choice >= item_no-max_choice/2)
00252 scroll = first_item = item_no-max_choice;
00253 else
00254 scroll = first_item = choice - max_choice/2;
00255 choice = choice - scroll;
00256 }
00257
00258
00259 for (i=0; i < max_choice; i++) {
00260 print_item (menu, items[(first_item + i) * 2 + 1], i, i == choice,
00261 (items[(first_item + i)*2][0] != ':'));
00262 }
00263
00264 wnoutrefresh (menu);
00265
00266 print_arrows(dialog, item_no, scroll,
00267 box_y, box_x+item_x+1, menu_height);
00268
00269 print_buttons (dialog, height, width, 0);
00270
00271 while (key != ESC) {
00272 key = wgetch(dialog);
00273
00274 if (key < 256 && isalpha(key)) key = tolower(key);
00275
00276 if (strchr("ynm", key))
00277 i = max_choice;
00278 else {
00279 for (i = choice+1; i < max_choice; i++) {
00280 j = first_alpha(items[(scroll+i)*2+1], "YyNnMm");
00281 if (key == tolower(items[(scroll+i)*2+1][j]))
00282 break;
00283 }
00284 if (i == max_choice)
00285 for (i = 0; i < max_choice; i++) {
00286 j = first_alpha(items[(scroll+i)*2+1], "YyNnMm");
00287 if (key == tolower(items[(scroll+i)*2+1][j]))
00288 break;
00289 }
00290 }
00291
00292 if (i < max_choice ||
00293 key == KEY_UP || key == KEY_DOWN ||
00294 key == '-' || key == '+' ||
00295 key == KEY_PPAGE || key == KEY_NPAGE) {
00296
00297 print_item (menu, items[(scroll+choice)*2+1], choice, FALSE,
00298 (items[(scroll+choice)*2][0] != ':'));
00299
00300 if (key == KEY_UP || key == '-') {
00301 if (choice < 2 && scroll) {
00302
00303 scrollok (menu, TRUE);
00304 wscrl (menu, -1);
00305 scrollok (menu, FALSE);
00306
00307 scroll--;
00308
00309 print_item (menu, items[scroll * 2 + 1], 0, FALSE,
00310 (items[scroll*2][0] != ':'));
00311 } else
00312 choice = MAX(choice - 1, 0);
00313
00314 } else if (key == KEY_DOWN || key == '+') {
00315
00316 print_item (menu, items[(scroll+choice)*2+1], choice, FALSE,
00317 (items[(scroll+choice)*2][0] != ':'));
00318
00319 if ((choice > max_choice-3) &&
00320 (scroll + max_choice < item_no)
00321 ) {
00322
00323 scrollok (menu, TRUE);
00324 scroll (menu);
00325 scrollok (menu, FALSE);
00326
00327 scroll++;
00328
00329 print_item (menu, items[(scroll+max_choice-1)*2+1],
00330 max_choice-1, FALSE,
00331 (items[(scroll+max_choice-1)*2][0] != ':'));
00332 } else
00333 choice = MIN(choice+1, max_choice-1);
00334
00335 } else if (key == KEY_PPAGE) {
00336 scrollok (menu, TRUE);
00337 for (i=0; (i < max_choice); i++) {
00338 if (scroll > 0) {
00339 wscrl (menu, -1);
00340 scroll--;
00341 print_item (menu, items[scroll * 2 + 1], 0, FALSE,
00342 (items[scroll*2][0] != ':'));
00343 } else {
00344 if (choice > 0)
00345 choice--;
00346 }
00347 }
00348 scrollok (menu, FALSE);
00349
00350 } else if (key == KEY_NPAGE) {
00351 for (i=0; (i < max_choice); i++) {
00352 if (scroll+max_choice < item_no) {
00353 scrollok (menu, TRUE);
00354 scroll(menu);
00355 scrollok (menu, FALSE);
00356 scroll++;
00357 print_item (menu, items[(scroll+max_choice-1)*2+1],
00358 max_choice-1, FALSE,
00359 (items[(scroll+max_choice-1)*2][0] != ':'));
00360 } else {
00361 if (choice+1 < max_choice)
00362 choice++;
00363 }
00364 }
00365
00366 } else
00367 choice = i;
00368
00369 print_item (menu, items[(scroll+choice)*2+1], choice, TRUE,
00370 (items[(scroll+choice)*2][0] != ':'));
00371
00372 print_arrows(dialog, item_no, scroll,
00373 box_y, box_x+item_x+1, menu_height);
00374
00375 wnoutrefresh (menu);
00376 wrefresh (dialog);
00377
00378 continue;
00379 }
00380
00381 switch (key) {
00382 case KEY_LEFT:
00383 case TAB:
00384 case KEY_RIGHT:
00385 button = ((key == KEY_LEFT ? --button : ++button) < 0)
00386 ? 2 : (button > 2 ? 0 : button);
00387
00388 print_buttons(dialog, height, width, button);
00389 wrefresh (dialog);
00390 break;
00391 case ' ':
00392 case 's':
00393 case 'y':
00394 case 'n':
00395 case 'm':
00396
00397 if ( (f=fopen("lxdialog.scrltmp","w")) != NULL ) {
00398 fprintf(f,"%d\n",scroll);
00399 fclose(f);
00400 }
00401 delwin (dialog);
00402 fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);
00403 switch (key) {
00404 case 's': return 3;
00405 case 'y': return 3;
00406 case 'n': return 4;
00407 case 'm': return 5;
00408 case ' ': return 6;
00409 }
00410 return 0;
00411 case 'h':
00412 case '?':
00413 button = 2;
00414 case '\n':
00415 delwin (dialog);
00416 if (button == 2)
00417 fprintf(stderr, "%s \"%s\"\n",
00418 items[(scroll + choice) * 2],
00419 items[(scroll + choice) * 2 + 1] +
00420 first_alpha(items[(scroll + choice) * 2 + 1],""));
00421 else
00422 fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);
00423
00424 remove("lxdialog.scrltmp");
00425 return button;
00426 case 'e':
00427 case 'x':
00428 key = ESC;
00429 case ESC:
00430 break;
00431 }
00432 }
00433
00434 delwin (dialog);
00435 remove("lxdialog.scrltmp");
00436 return -1;
00437 }