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 
00025 
00026 
00027 static void
00028 print_buttons(WINDOW *dialog, int height, int width, int selected)
00029 {
00030     int x = width / 2 - 10;
00031     int y = height - 2;
00032 
00033     print_button (dialog, " Yes ", y, x, selected == 0);
00034     print_button (dialog, "  No  ", y, x + 13, selected == 1);
00035 
00036     wmove(dialog, y, x+1 + 13*selected );
00037     wrefresh (dialog);
00038 }
00039 
00040 
00041 
00042 
00043 int
00044 dialog_yesno (const char *title, const char *prompt, int height, int width)
00045 {
00046     int i, x, y, key = 0, button = 0;
00047     WINDOW *dialog;
00048 
00049     
00050     x = (COLS - width) / 2;
00051     y = (LINES - height) / 2;
00052 
00053     draw_shadow (stdscr, y, x, height, width);
00054 
00055     dialog = newwin (height, width, y, x);
00056     keypad (dialog, TRUE);
00057 
00058     draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
00059     wattrset (dialog, border_attr);
00060     mvwaddch (dialog, height-3, 0, ACS_LTEE);
00061     for (i = 0; i < width - 2; i++)
00062     waddch (dialog, ACS_HLINE);
00063     wattrset (dialog, dialog_attr);
00064     waddch (dialog, ACS_RTEE);
00065 
00066     if (title != NULL && strlen(title) >= width-2 ) {
00067     
00068     char * title2 = malloc(width-2+1);
00069     memcpy( title2, title, width-2 );
00070     title2[width-2] = '\0';
00071     title = title2;
00072     }
00073 
00074     if (title != NULL) {
00075     wattrset (dialog, title_attr);
00076     mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
00077     waddstr (dialog, (char *)title);
00078     waddch (dialog, ' ');
00079     }
00080 
00081     wattrset (dialog, dialog_attr);
00082     print_autowrap (dialog, prompt, width - 2, 1, 3);
00083 
00084     print_buttons(dialog, height, width, 0);
00085 
00086     while (key != ESC) {
00087     key = wgetch (dialog);
00088     switch (key) {
00089     case 'Y':
00090     case 'y':
00091         delwin (dialog);
00092         return 0;
00093     case 'N':
00094     case 'n':
00095         delwin (dialog);
00096         return 1;
00097 
00098     case TAB:
00099     case KEY_LEFT:
00100     case KEY_RIGHT:
00101         button = ((key == KEY_LEFT ? --button : ++button) < 0)
00102             ? 1 : (button > 1 ? 0 : button);
00103 
00104         print_buttons(dialog, height, width, button);
00105         wrefresh (dialog);
00106         break;
00107     case ' ':
00108     case '\n':
00109         delwin (dialog);
00110         return button;
00111     case ESC:
00112         break;
00113     }
00114     }
00115 
00116     delwin (dialog);
00117     return -1;          
00118 }