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 bool use_colors = 1;
00027 
00028 const char *backtitle = NULL;
00029 
00030 const char *dialog_result;
00031 
00032 
00033 
00034 
00035 chtype attributes[] =
00036 {
00037     A_NORMAL,           
00038     A_NORMAL,           
00039     A_NORMAL,           
00040     A_BOLD,         
00041     A_NORMAL,           
00042     A_REVERSE,          
00043     A_DIM,          
00044     A_REVERSE,          
00045     A_BOLD,         
00046     A_REVERSE,          
00047     A_NORMAL,           
00048     A_NORMAL,           
00049     A_NORMAL,           
00050     A_NORMAL,           
00051     A_BOLD,         
00052     A_NORMAL,           
00053     A_BOLD,         
00054     A_NORMAL,           
00055     A_NORMAL,           
00056     A_NORMAL,           
00057     A_REVERSE,          
00058     A_BOLD,         
00059     A_REVERSE,          
00060     A_BOLD,         
00061     A_REVERSE,          
00062     A_BOLD,         
00063     A_REVERSE,          
00064     A_BOLD,         
00065     A_BOLD          
00066 };
00067 
00068 
00069 #include "colors.h"
00070 
00071 
00072 
00073 
00074 int color_table[][3] =
00075 {
00076     {SCREEN_FG, SCREEN_BG, SCREEN_HL},
00077     {SHADOW_FG, SHADOW_BG, SHADOW_HL},
00078     {DIALOG_FG, DIALOG_BG, DIALOG_HL},
00079     {TITLE_FG, TITLE_BG, TITLE_HL},
00080     {BORDER_FG, BORDER_BG, BORDER_HL},
00081     {BUTTON_ACTIVE_FG, BUTTON_ACTIVE_BG, BUTTON_ACTIVE_HL},
00082     {BUTTON_INACTIVE_FG, BUTTON_INACTIVE_BG, BUTTON_INACTIVE_HL},
00083     {BUTTON_KEY_ACTIVE_FG, BUTTON_KEY_ACTIVE_BG, BUTTON_KEY_ACTIVE_HL},
00084     {BUTTON_KEY_INACTIVE_FG, BUTTON_KEY_INACTIVE_BG, BUTTON_KEY_INACTIVE_HL},
00085     {BUTTON_LABEL_ACTIVE_FG, BUTTON_LABEL_ACTIVE_BG, BUTTON_LABEL_ACTIVE_HL},
00086     {BUTTON_LABEL_INACTIVE_FG, BUTTON_LABEL_INACTIVE_BG,
00087      BUTTON_LABEL_INACTIVE_HL},
00088     {INPUTBOX_FG, INPUTBOX_BG, INPUTBOX_HL},
00089     {INPUTBOX_BORDER_FG, INPUTBOX_BORDER_BG, INPUTBOX_BORDER_HL},
00090     {SEARCHBOX_FG, SEARCHBOX_BG, SEARCHBOX_HL},
00091     {SEARCHBOX_TITLE_FG, SEARCHBOX_TITLE_BG, SEARCHBOX_TITLE_HL},
00092     {SEARCHBOX_BORDER_FG, SEARCHBOX_BORDER_BG, SEARCHBOX_BORDER_HL},
00093     {POSITION_INDICATOR_FG, POSITION_INDICATOR_BG, POSITION_INDICATOR_HL},
00094     {MENUBOX_FG, MENUBOX_BG, MENUBOX_HL},
00095     {MENUBOX_BORDER_FG, MENUBOX_BORDER_BG, MENUBOX_BORDER_HL},
00096     {ITEM_FG, ITEM_BG, ITEM_HL},
00097     {ITEM_SELECTED_FG, ITEM_SELECTED_BG, ITEM_SELECTED_HL},
00098     {TAG_FG, TAG_BG, TAG_HL},
00099     {TAG_SELECTED_FG, TAG_SELECTED_BG, TAG_SELECTED_HL},
00100     {TAG_KEY_FG, TAG_KEY_BG, TAG_KEY_HL},
00101     {TAG_KEY_SELECTED_FG, TAG_KEY_SELECTED_BG, TAG_KEY_SELECTED_HL},
00102     {CHECK_FG, CHECK_BG, CHECK_HL},
00103     {CHECK_SELECTED_FG, CHECK_SELECTED_BG, CHECK_SELECTED_HL},
00104     {UARROW_FG, UARROW_BG, UARROW_HL},
00105     {DARROW_FG, DARROW_BG, DARROW_HL},
00106 };              
00107 
00108 
00109 
00110 
00111 void
00112 attr_clear (WINDOW * win, int height, int width, chtype attr)
00113 {
00114     int i, j;
00115 
00116     wattrset (win, attr);
00117     for (i = 0; i < height; i++) {
00118     wmove (win, i, 0);
00119     for (j = 0; j < width; j++)
00120         waddch (win, ' ');
00121     }
00122     touchwin (win);
00123 }
00124 
00125 void dialog_clear (void)
00126 {
00127     attr_clear (stdscr, LINES, COLS, screen_attr);
00128     
00129     if (backtitle != NULL) {
00130         int i;
00131 
00132         wattrset (stdscr, screen_attr);
00133         mvwaddstr (stdscr, 0, 1, (char *)backtitle);
00134         wmove (stdscr, 1, 1);
00135         for (i = 1; i < COLS - 1; i++)
00136             waddch (stdscr, ACS_HLINE);
00137     }
00138     wnoutrefresh (stdscr);
00139 }
00140 
00141 
00142 
00143 
00144 void
00145 init_dialog (void)
00146 {
00147     initscr ();         
00148     keypad (stdscr, TRUE);
00149     cbreak ();
00150     noecho ();
00151 
00152 
00153     if (use_colors) 
00154     color_setup ();
00155 
00156 
00157     dialog_clear ();
00158 }
00159 
00160 
00161 
00162 
00163 void
00164 color_setup (void)
00165 {
00166     int i;
00167 
00168     if (has_colors ()) {    
00169     start_color ();
00170 
00171     
00172     for (i = 0; i < ATTRIBUTE_COUNT; i++)
00173         init_pair (i + 1, color_table[i][0], color_table[i][1]);
00174 
00175     
00176     for (i = 0; i < ATTRIBUTE_COUNT; i++)
00177         attributes[i] = C_ATTR (color_table[i][2], i + 1);
00178     }
00179 }
00180 
00181 
00182 
00183 
00184 void
00185 end_dialog (void)
00186 {
00187     endwin ();
00188 }
00189 
00190 
00191 
00192 
00193 
00194 
00195 
00196 
00197 void
00198 print_autowrap (WINDOW * win, const char *prompt, int width, int y, int x)
00199 {
00200     int newl, cur_x, cur_y;
00201     int i, prompt_len, room, wlen;
00202     char tempstr[MAX_LEN + 1], *word, *sp, *sp2;
00203 
00204     strcpy (tempstr, prompt);
00205 
00206     prompt_len = strlen(tempstr);
00207     
00208     
00209 
00210 
00211     for(i=0; i<prompt_len; i++) {
00212     if(tempstr[i] == '\n') tempstr[i] = ' ';
00213     }
00214 
00215     if (prompt_len <= width - x * 2) {  
00216     wmove (win, y, (width - prompt_len) / 2);
00217     waddstr (win, tempstr);
00218     } else {
00219     cur_x = x;
00220     cur_y = y;
00221     newl = 1;
00222     word = tempstr;
00223     while (word && *word) {
00224         sp = index(word, ' ');
00225         if (sp)
00226             *sp++ = 0;
00227 
00228         
00229 
00230 
00231         room = width - cur_x;
00232         wlen = strlen(word);
00233         if (wlen > room ||
00234            (newl && wlen < 4 && sp && wlen+1+strlen(sp) > room
00235              && (!(sp2 = index(sp, ' ')) || wlen+1+(sp2-sp) > room))) {
00236         cur_y++;
00237         cur_x = x;
00238         }
00239         wmove (win, cur_y, cur_x);
00240         waddstr (win, word);
00241         getyx (win, cur_y, cur_x);
00242         cur_x++;
00243         if (sp && *sp == ' ') {
00244             cur_x++;    
00245         while (*++sp == ' ');
00246         newl = 1;
00247         } else
00248             newl = 0;
00249         word = sp;
00250     }
00251     }
00252 }
00253 
00254 
00255 
00256 
00257 void
00258 print_button (WINDOW * win, const char *label, int y, int x, int selected)
00259 {
00260     int i, temp;
00261 
00262     wmove (win, y, x);
00263     wattrset (win, selected ? button_active_attr : button_inactive_attr);
00264     waddstr (win, "<");
00265     temp = strspn (label, " ");
00266     label += temp;
00267     wattrset (win, selected ? button_label_active_attr
00268           : button_label_inactive_attr);
00269     for (i = 0; i < temp; i++)
00270     waddch (win, ' ');
00271     wattrset (win, selected ? button_key_active_attr
00272           : button_key_inactive_attr);
00273     waddch (win, label[0]);
00274     wattrset (win, selected ? button_label_active_attr
00275           : button_label_inactive_attr);
00276     waddstr (win, (char *)label + 1);
00277     wattrset (win, selected ? button_active_attr : button_inactive_attr);
00278     waddstr (win, ">");
00279     wmove (win, y, x + temp + 1);
00280 }
00281 
00282 
00283 
00284 
00285 void
00286 draw_box (WINDOW * win, int y, int x, int height, int width,
00287       chtype box, chtype border)
00288 {
00289     int i, j;
00290 
00291     wattrset (win, 0);
00292     for (i = 0; i < height; i++) {
00293     wmove (win, y + i, x);
00294     for (j = 0; j < width; j++)
00295         if (!i && !j)
00296         waddch (win, border | ACS_ULCORNER);
00297         else if (i == height - 1 && !j)
00298         waddch (win, border | ACS_LLCORNER);
00299         else if (!i && j == width - 1)
00300         waddch (win, box | ACS_URCORNER);
00301         else if (i == height - 1 && j == width - 1)
00302         waddch (win, box | ACS_LRCORNER);
00303         else if (!i)
00304         waddch (win, border | ACS_HLINE);
00305         else if (i == height - 1)
00306         waddch (win, box | ACS_HLINE);
00307         else if (!j)
00308         waddch (win, border | ACS_VLINE);
00309         else if (j == width - 1)
00310         waddch (win, box | ACS_VLINE);
00311         else
00312         waddch (win, box | ' ');
00313     }
00314 }
00315 
00316 
00317 
00318 
00319 
00320 void
00321 draw_shadow (WINDOW * win, int y, int x, int height, int width)
00322 {
00323     int i;
00324 
00325     if (has_colors ()) {    
00326     wattrset (win, shadow_attr);
00327     wmove (win, y + height, x + 2);
00328     for (i = 0; i < width; i++)
00329         waddch (win, winch (win) & A_CHARTEXT);
00330     for (i = y + 1; i < y + height + 1; i++) {
00331         wmove (win, i, x + width);
00332         waddch (win, winch (win) & A_CHARTEXT);
00333         waddch (win, winch (win) & A_CHARTEXT);
00334     }
00335     wnoutrefresh (win);
00336     }
00337 }
00338 
00339 
00340 
00341 
00342 int
00343 first_alpha(const char *string, const char *exempt)
00344 {
00345     int i, in_paren=0, c;
00346 
00347     for (i = 0; i < strlen(string); i++) {
00348         c = tolower(string[i]);
00349 
00350         if (strchr("<[(", c)) ++in_paren;
00351         if (strchr(">])", c) && in_paren > 0) --in_paren;
00352 
00353         if ((! in_paren) && isalpha(c) && 
00354              strchr(exempt, c) == 0)
00355             return i;
00356     }
00357 
00358     return 0;
00359 }