Changeset f102b57751534d544f1270a3c10a048a29b3b3b7 for lxdialog/menubox.c
- Timestamp:
- 09/11/07 12:55:01 (1 year ago)
- git-parent:
- Files:
-
- lxdialog/menubox.c (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
lxdialog/menubox.c
r6ee1e19 rf102b57 27 27 * *) A bugfix for the Page-Down problem 28 28 * 29 * *) Formerly when I used Page Down and Page Up, the cursor would be set 29 * *) Formerly when I used Page Down and Page Up, the cursor would be set 30 30 * to the first position in the menu box. Now lxdialog is a bit 31 31 * smarter and works more like other menu systems (just have a look at … … 81 81 int i; 82 82 for (i = 0; i < menu_width; i++) 83 waddch (win, ' ');83 waddch (win, ' '); 84 84 } 85 85 #else … … 89 89 mvwaddstr (win, choice, item_x, menu_item); 90 90 if (hotkey) { 91 wattrset (win, selected ? tag_key_selected_attr : tag_key_attr);92 mvwaddch(win, choice, item_x+j, menu_item[j]);91 wattrset (win, selected ? tag_key_selected_attr : tag_key_attr); 92 mvwaddch(win, choice, item_x+j, menu_item[j]); 93 93 } 94 94 if (selected) { 95 wmove (win, choice, item_x+1);96 wrefresh (win);95 wmove (win, choice, item_x+1); 96 wrefresh (win); 97 97 } 98 98 } … … 103 103 static void 104 104 print_arrows (WINDOW * win, int item_no, int scroll, 105 int y, int x, int height)105 int y, int x, int height) 106 106 { 107 107 int cur_y, cur_x; … … 112 112 113 113 if (scroll > 0) { 114 wattrset (win, uarrow_attr);115 waddch (win, ACS_UARROW);116 waddstr (win, "(-)");114 wattrset (win, uarrow_attr); 115 waddch (win, ACS_UARROW); 116 waddstr (win, "(-)"); 117 117 } 118 118 else { 119 wattrset (win, menubox_attr);120 waddch (win, ACS_HLINE);121 waddch (win, ACS_HLINE);122 waddch (win, ACS_HLINE);123 waddch (win, ACS_HLINE);119 wattrset (win, menubox_attr); 120 waddch (win, ACS_HLINE); 121 waddch (win, ACS_HLINE); 122 waddch (win, ACS_HLINE); 123 waddch (win, ACS_HLINE); 124 124 } 125 125 … … 128 128 129 129 if ((height < item_no) && (scroll + height < item_no)) { 130 wattrset (win, darrow_attr);131 waddch (win, ACS_DARROW);132 waddstr (win, "(+)");130 wattrset (win, darrow_attr); 131 waddch (win, ACS_DARROW); 132 waddstr (win, "(+)"); 133 133 } 134 134 else { 135 wattrset (win, menubox_border_attr);136 waddch (win, ACS_HLINE);137 waddch (win, ACS_HLINE);138 waddch (win, ACS_HLINE);139 waddch (win, ACS_HLINE);135 wattrset (win, menubox_border_attr); 136 waddch (win, ACS_HLINE); 137 waddch (win, ACS_HLINE); 138 waddch (win, ACS_HLINE); 139 waddch (win, ACS_HLINE); 140 140 } 141 141 … … 165 165 int 166 166 dialog_menu (const char *title, const char *prompt, int height, int width, 167 int menu_height, const char *current, int item_no,168 const char * const * items)167 int menu_height, const char *current, int item_no, 168 const char * const * items) 169 169 170 170 { … … 189 189 mvwaddch (dialog, height - 3, 0, ACS_LTEE); 190 190 for (i = 0; i < width - 2; i++) 191 waddch (dialog, ACS_HLINE);191 waddch (dialog, ACS_HLINE); 192 192 wattrset (dialog, dialog_attr); 193 193 wbkgdset (dialog, dialog_attr & A_COLOR); … … 195 195 196 196 if (title != NULL && strlen(title) >= width-2 ) { 197 /* truncate long title -- mec */198 char * title2 = malloc(width-2+1);199 memcpy( title2, title, width-2 );200 title2[width-2] = '\0';201 title = title2;197 /* truncate long title -- mec */ 198 char * title2 = malloc(width-2+1); 199 memcpy( title2, title, width-2 ); 200 title2[width-2] = '\0'; 201 title = title2; 202 202 } 203 203 204 204 if (title != NULL) { 205 wattrset (dialog, title_attr);206 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');207 waddstr (dialog, (char *)title);208 waddch (dialog, ' ');205 wattrset (dialog, title_attr); 206 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' '); 207 waddstr (dialog, (char *)title); 208 waddch (dialog, ' '); 209 209 } 210 210 … … 218 218 /* create new window for the menu */ 219 219 menu = subwin (dialog, menu_height, menu_width, 220 y + box_y + 1, x + box_x + 1);220 y + box_y + 1, x + box_x + 1); 221 221 keypad (menu, TRUE); 222 222 223 223 /* draw a box around the menu items */ 224 224 draw_box (dialog, box_y, box_x, menu_height + 2, menu_width + 2, 225 menubox_border_attr, menubox_attr);225 menubox_border_attr, menubox_attr); 226 226 227 227 /* 228 228 * Find length of longest item in order to center menu. 229 * Set 'choice' to default item. 229 * Set 'choice' to default item. 230 230 */ 231 231 item_x = 0; 232 232 for (i = 0; i < item_no; i++) { 233 item_x = MAX (item_x, MIN(menu_width, strlen (items[i * 2 + 1]) + 2));234 if (strcmp(current, items[i*2]) == 0) choice = i;233 item_x = MAX (item_x, MIN(menu_width, strlen (items[i * 2 + 1]) + 2)); 234 if (strcmp(current, items[i*2]) == 0) choice = i; 235 235 } 236 236 … … 239 239 /* get the scroll info from the temp file */ 240 240 if ( (f=fopen("lxdialog.scrltmp","r")) != NULL ) { 241 if ( (fscanf(f,"%d\n",&scroll) == 1) && (scroll <= choice) &&242 (scroll+max_choice > choice) && (scroll >= 0) &&243 (scroll+max_choice <= item_no) ) {244 first_item = scroll;245 choice = choice - scroll;246 fclose(f);247 } else {248 scroll=0;249 remove("lxdialog.scrltmp");250 fclose(f);251 f=NULL;252 }241 if ( (fscanf(f,"%d\n",&scroll) == 1) && (scroll <= choice) && 242 (scroll+max_choice > choice) && (scroll >= 0) && 243 (scroll+max_choice <= item_no) ) { 244 first_item = scroll; 245 choice = choice - scroll; 246 fclose(f); 247 } else { 248 scroll=0; 249 remove("lxdialog.scrltmp"); 250 fclose(f); 251 f=NULL; 252 } 253 253 } 254 254 if ( (choice >= max_choice) || (f==NULL && choice >= max_choice/2) ) { 255 if (choice >= item_no-max_choice/2)256 scroll = first_item = item_no-max_choice;257 else258 scroll = first_item = choice - max_choice/2;259 choice = choice - scroll;255 if (choice >= item_no-max_choice/2) 256 scroll = first_item = item_no-max_choice; 257 else 258 scroll = first_item = choice - max_choice/2; 259 choice = choice - scroll; 260 260 } 261 261 262 262 /* Print the menu */ 263 263 for (i=0; i < max_choice; i++) { 264 print_item (menu, items[(first_item + i) * 2 + 1], i, i == choice,264 print_item (menu, items[(first_item + i) * 2 + 1], i, i == choice, 265 265 (items[(first_item + i)*2][0] != ':')); 266 266 } … … 269 269 270 270 print_arrows(dialog, item_no, scroll, 271 box_y, box_x+item_x+1, menu_height);271 box_y, box_x+item_x+1, menu_height); 272 272 273 273 print_buttons (dialog, height, width, 0); … … 276 276 277 277 while (key != ESC) { 278 key = wgetch(menu);279 280 if (key < 256 && isalpha(key)) key = tolower(key);281 282 if (strchr("ynm", key))283 i = max_choice;284 else {278 key = wgetch(menu); 279 280 if (key < 256 && isalpha(key)) key = tolower(key); 281 282 if (strchr("ynm", key)) 283 i = max_choice; 284 else { 285 285 for (i = choice+1; i < max_choice; i++) { 286 j = first_alpha(items[(scroll+i)*2+1], "YyNnMm");287 if (key == tolower(items[(scroll+i)*2+1][j]))288 break;289 }290 if (i == max_choice)291 for (i = 0; i < max_choice; i++) {292 j = first_alpha(items[(scroll+i)*2+1], "YyNnMm");293 if (key == tolower(items[(scroll+i)*2+1][j]))294 break;295 }296 }297 298 if (i < max_choice || 286 j = first_alpha(items[(scroll+i)*2+1], "YyNnMm"); 287 if (key == tolower(items[(scroll+i)*2+1][j])) 288 break; 289 } 290 if (i == max_choice) 291 for (i = 0; i < max_choice; i++) { 292 j = first_alpha(items[(scroll+i)*2+1], "YyNnMm"); 293 if (key == tolower(items[(scroll+i)*2+1][j])) 294 break; 295 } 296 } 297 298 if (i < max_choice || 299 299 key == KEY_UP || key == KEY_DOWN || 300 300 key == '-' || key == '+' || … … 304 304 (items[(scroll+choice)*2][0] != ':')); 305 305 306 if (key == KEY_UP || key == '-') {306 if (key == KEY_UP || key == '-') { 307 307 if (choice < 2 && scroll) { 308 /* Scroll menu down */308 /* Scroll menu down */ 309 309 scrollok (menu, TRUE); 310 310 wscrl (menu, -1); … … 315 315 print_item (menu, items[scroll * 2 + 1], 0, FALSE, 316 316 (items[scroll*2][0] != ':')); 317 } else318 choice = MAX(choice - 1, 0);319 320 } else if (key == KEY_DOWN || key == '+') {321 322 print_item (menu, items[(scroll+choice)*2+1], choice, FALSE,317 } else 318 choice = MAX(choice - 1, 0); 319 320 } else if (key == KEY_DOWN || key == '+') { 321 322 print_item (menu, items[(scroll+choice)*2+1], choice, FALSE, 323 323 (items[(scroll+choice)*2][0] != ':')); 324 324 … … 326 326 (scroll + max_choice < item_no) 327 327 ) { 328 /* Scroll menu up */329 scrollok (menu, TRUE);328 /* Scroll menu up */ 329 scrollok (menu, TRUE); 330 330 scroll (menu); 331 331 scrollok (menu, FALSE); … … 339 339 choice = MIN(choice+1, max_choice-1); 340 340 341 } else if (key == KEY_PPAGE) {342 scrollok (menu, TRUE);341 } else if (key == KEY_PPAGE) { 342 scrollok (menu, TRUE); 343 343 for (i=0; (i < max_choice); i++) { 344 344 if (scroll > 0) { 345 wscrl (menu, -1);346 scroll--;347 print_item (menu, items[scroll * 2 + 1], 0, FALSE,348 (items[scroll*2][0] != ':'));345 wscrl (menu, -1); 346 scroll--; 347 print_item (menu, items[scroll * 2 + 1], 0, FALSE, 348 (items[scroll*2][0] != ':')); 349 349 } else { 350 350 if (choice > 0) … … 357 357 for (i=0; (i < max_choice); i++) { 358 358 if (scroll+max_choice < item_no) { 359 scrollok (menu, TRUE);360 scroll(menu);361 scrollok (menu, FALSE);362 scroll++;363 print_item (menu, items[(scroll+max_choice-1)*2+1],364 max_choice-1, FALSE,365 (items[(scroll+max_choice-1)*2][0] != ':'));366 } else {367 if (choice+1 < max_choice)368 choice++;369 }359 scrollok (menu, TRUE); 360 scroll(menu); 361 scrollok (menu, FALSE); 362 scroll++; 363 print_item (menu, items[(scroll+max_choice-1)*2+1], 364 max_choice-1, FALSE, 365 (items[(scroll+max_choice-1)*2][0] != ':')); 366 } else { 367 if (choice+1 < max_choice) 368 choice++; 369 } 370 370 } 371 371 … … 382 382 wrefresh (menu); 383 383 384 continue;/* wait for another key press */384 continue; /* wait for another key press */ 385 385 } 386 386 387 switch (key) {388 case KEY_LEFT:389 case TAB:390 case KEY_RIGHT:391 button = ((key == KEY_LEFT ? --button : ++button) < 0)392 ? 2 : (button > 2 ? 0 : button);393 394 print_buttons(dialog, height, width, button);395 wrefresh (menu);396 break;397 case ' ':398 case 's':399 case 'y':400 case 'n':401 case 'm':402 /* save scroll info */403 if ( (f=fopen("lxdialog.scrltmp","w")) != NULL ) {404 fprintf(f,"%d\n",scroll);405 fclose(f);406 }407 delwin (dialog);387 switch (key) { 388 case KEY_LEFT: 389 case TAB: 390 case KEY_RIGHT: 391 button = ((key == KEY_LEFT ? --button : ++button) < 0) 392 ? 2 : (button > 2 ? 0 : button); 393 394 print_buttons(dialog, height, width, button); 395 wrefresh (menu); 396 break; 397 case ' ': 398 case 's': 399 case 'y': 400 case 'n': 401 case 'm': 402 /* save scroll info */ 403 if ( (f=fopen("lxdialog.scrltmp","w")) != NULL ) { 404 fprintf(f,"%d\n",scroll); 405 fclose(f); 406 } 407 delwin (dialog); 408 408 fprintf(stderr, "%s\n", items[(scroll + choice) * 2]); 409 409 switch (key) { … … 414 414 case ' ': return 6; 415 415 } 416 return 0;417 case 'h':418 case '?':419 button = 2;420 case '\n':421 delwin (dialog);422 if (button == 2) 423 fprintf(stderr, "%s \"%s\"\n",424 items[(scroll + choice) * 2],425 items[(scroll + choice) * 2 + 1] +426 first_alpha(items[(scroll + choice) * 2 + 1],""));427 else428 fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);429 430 remove("lxdialog.scrltmp");431 return button;432 case 'e':433 case 'x':434 key = ESC;435 case ESC:436 break;437 }416 return 0; 417 case 'h': 418 case '?': 419 button = 2; 420 case '\n': 421 delwin (dialog); 422 if (button == 2) 423 fprintf(stderr, "%s \"%s\"\n", 424 items[(scroll + choice) * 2], 425 items[(scroll + choice) * 2 + 1] + 426 first_alpha(items[(scroll + choice) * 2 + 1],"")); 427 else 428 fprintf(stderr, "%s\n", items[(scroll + choice) * 2]); 429 430 remove("lxdialog.scrltmp"); 431 return button; 432 case 'e': 433 case 'x': 434 key = ESC; 435 case ESC: 436 break; 437 } 438 438 } 439 439 440 440 delwin (dialog); 441 441 remove("lxdialog.scrltmp"); 442 return -1; /* ESC pressed */442 return -1; /* ESC pressed */ 443 443 }
