Initial import from FreeBSD RELENG_4:
[dragonfly.git] / gnu / lib / libdialog / msgbox.c
1 /*
2  *  msgbox.c -- implements the message box and info box
3  *
4  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License
8  *  as published by the Free Software Foundation; either version 2
9  *  of the License, or (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #ifndef lint
22 static const char rcsid[] =
23   "$FreeBSD: src/gnu/lib/libdialog/msgbox.c,v 1.17.6.1 2001/08/12 22:48:02 eric Exp $";
24 #endif
25
26 #include <dialog.h>
27 #include "dialog.priv.h"
28
29
30 /* local prototypes */
31 static int      getnlines(unsigned char *buf);
32 static void     print_page(WINDOW *win, int height, int width, unsigned char *buf, int startline, int hscroll);
33 static void     print_perc(WINDOW *win, int y, int x, float p);
34
35
36 /*
37  * Display a message box. Program will pause and display an "OK" button
38  * if the parameter 'pause' is non-zero.
39  */
40 int dialog_msgbox(unsigned char *title, unsigned char *prompt, int height, int width, int pause)
41 {
42   int i, j, x, y, key = 0;
43   WINDOW *dialog;
44
45   if (height < 0)
46         height = strheight(prompt)+2+2*(!!pause);
47   if (width < 0) {
48         i = strwidth(prompt);
49         j = ((title != NULL) ? strwidth(title) : 0);
50         width = MAX(i,j)+4;
51   }
52   if (pause)
53         width = MAX(width,10);
54
55   if (width > COLS)
56         width = COLS;
57   if (height > LINES)
58         height = LINES;
59   /* center dialog box on screen */
60   x = DialogX ? DialogX : (COLS - width)/2;
61   y = DialogY ? DialogY : (LINES - height)/2;
62
63 #ifdef HAVE_NCURSES
64   if (use_shadow)
65     draw_shadow(stdscr, y, x, height, width);
66 #endif
67   dialog = newwin(height, width, y, x);
68   if (dialog == NULL) {
69     endwin();
70     fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
71     exit(1);
72   }
73   keypad(dialog, TRUE);
74
75   draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
76
77   if (title != NULL) {
78     wattrset(dialog, title_attr);
79     wmove(dialog, 0, (width - strlen(title))/2 - 1);
80     waddch(dialog, ' ');
81     waddstr(dialog, title);
82     waddch(dialog, ' ');
83   }
84   wattrset(dialog, dialog_attr);
85   wmove(dialog, 1, 2);
86   print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);
87
88   if (pause) {
89     wattrset(dialog, border_attr);
90     wmove(dialog, height-3, 0);
91     waddch(dialog, ACS_LTEE);
92     for (i = 0; i < width-2; i++)
93       waddch(dialog, ACS_HLINE);
94     wattrset(dialog, dialog_attr);
95     waddch(dialog, ACS_RTEE);
96     wmove(dialog, height-2, 1);
97     for (i = 0; i < width-2; i++)
98     waddch(dialog, ' ');
99     display_helpline(dialog, height-1, width);
100     print_button(dialog, "  OK  ", height-2, width/2-6, TRUE);
101     wrefresh(dialog);
102     while (key != ESC && key != '\n' && key != ' ' && key != '\r')
103       key = wgetch(dialog);
104     if (key == '\r')
105       key = '\n';
106   }
107   else {
108     key = '\n';
109     wrefresh(dialog);
110   }
111
112   delwin(dialog);
113   return (key == ESC ? -1 : 0);
114 }
115 /* End of dialog_msgbox() */
116
117 int
118 dialog_mesgbox(unsigned char *title, unsigned char *prompt, int height, int width)
119 /*
120  * Desc: basically the same as dialog_msgbox, but ... can use PGUP, PGDN and
121  *       arrowkeys to move around the text and pause is always enabled
122  */
123 {
124     int         i, j, x, y, key=0;
125     int         theight, startline, hscroll, max_lines;
126     WINDOW      *dialog;
127
128     if (height < 0)
129         height = strheight(prompt)+2+2;
130     if (width < 0) {
131         i = strwidth(prompt);
132         j = ((title != NULL) ? strwidth(title) : 0);
133         width = MAX(i,j)+4;
134     }
135     width = MAX(width,10);
136
137     if (width > COLS)
138         width = COLS;
139     if (height > LINES)
140         height = LINES;
141     /* center dialog box on screen */
142     x = (COLS - width)/2;
143     y = (LINES - height)/2;
144
145 #ifdef HAVE_NCURSES
146     if (use_shadow)
147         draw_shadow(stdscr, y, x, height, width);
148 #endif
149     dialog = newwin(height, width, y, x);
150     if (dialog == NULL) {
151         endwin();
152         fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
153         exit(1);
154     }
155     keypad(dialog, TRUE);
156
157     draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
158
159     if (title != NULL) {
160         wattrset(dialog, title_attr);
161         wmove(dialog, 0, (width - strlen(title))/2 - 1);
162         waddch(dialog, ' ');
163         waddstr(dialog, title);
164         waddch(dialog, ' ');
165     }
166
167     wattrset(dialog, border_attr);
168     wmove(dialog, height-3, 0);
169     waddch(dialog, ACS_LTEE);
170     for (i = 0; i < width-2; i++)
171       waddch(dialog, ACS_HLINE);
172     wattrset(dialog, dialog_attr);
173     waddch(dialog, ACS_RTEE);
174     wmove(dialog, height-2, 1);
175     for (i = 0; i < width-2; i++)
176     waddch(dialog, ' ');
177     display_helpline(dialog, height-1, width);
178     print_button(dialog, "  OK  ", height-2, width/2-6, TRUE);
179     wattrset(dialog, dialog_attr);
180
181     theight = height - 4;
182     startline = 0;
183     hscroll = 0;
184     max_lines = getnlines(prompt);
185     print_page(dialog, theight, width, prompt, startline, hscroll);
186     print_perc(dialog, height-3, width-9, (float) (startline+theight)/max_lines);
187     wmove(dialog, height-2, width/2-3);
188     wrefresh(dialog);
189     while ((key != ESC) && (key != '\n') && (key != '\r') && (key != ' ')) {
190         key = wgetch(dialog);
191         switch(key) {
192         case KEY_HOME:
193             startline=0;
194             hscroll=0;
195             break;
196         case KEY_END:
197             startline = max_lines - theight;
198             if (startline < 0) startline = 0;
199             break;
200         case '\020':    /* ^P */
201         case KEY_UP:
202             if (startline > 0) startline--;
203             break;
204         case '\016':    /* ^N */
205         case KEY_DOWN:
206             if (startline < max_lines - theight) startline++;
207             break;
208         case KEY_RIGHT:
209             hscroll+=5;
210             break;
211         case KEY_LEFT:
212             if (hscroll > 0) hscroll-=5;
213             if (hscroll < 0) hscroll =0;
214             break;
215         case KEY_PPAGE:
216             if (startline - height > 0) {
217                 startline -= theight;
218             } else {
219                 startline = 0;
220             }
221             break;
222         case KEY_NPAGE:
223             if (startline + theight < max_lines - theight) {
224                 startline += theight;
225             } else {
226                 startline = max_lines - theight;
227                 if (startline < 0) startline = 0;
228             }
229             break;
230         case KEY_F(1):
231         case '?':
232             display_helpfile();
233             break;
234         }
235         print_page(dialog, theight, width, prompt, startline, hscroll);
236         print_perc(dialog, height-3, width-9, (float) (startline+theight)/max_lines);
237         wmove(dialog, height-2, width/2-3);
238         wrefresh(dialog);
239     }
240
241     delwin(dialog);
242     return (key == ESC ? -1 : 0);
243
244 } /* dialog_mesgbox() */
245
246 static void
247 print_perc(WINDOW *win, int y, int x, float p)
248 /*
249  * Desc: print p as a percentage at the coordinates (y,x)
250  */
251 {
252     char        ps[10];
253
254     if (p>1.0) p=1.0;
255     sprintf(ps, "(%3d%%)", (int) (p*100));
256     wmove(win, y, x);
257     waddstr(win, ps);
258
259     return;
260 } /* print_perc() */
261
262 static int
263 getnlines(unsigned char *buf)
264 /*
265  * Desc: count the # of lines in <buf>
266  */
267 {
268     int i = 0;
269
270     if (*buf)
271         i++;
272     while (*buf) {
273         if (*buf == '\n' || *buf == '\r')
274             i++;
275         buf++;
276     }
277     return(i);
278 } /* getlines() */
279
280
281 unsigned char *
282 getline(unsigned char *buf, int n)
283 /*
284  * Desc: return a pointer to the n'th line in <buf> or NULL if its
285  *       not there
286  */
287 {
288     int i;
289
290     if (n<0) {
291         return(NULL);
292     }
293
294     i=0;
295     while (*buf && i<n) {
296         if (*buf == '\n' || *buf == '\r') {
297             i++;
298         }
299         buf++;
300     }
301     if (i<n) {
302         return(NULL);
303     } else {
304         return(buf);
305     }
306 } /* getline() */
307
308 static void
309 print_page(WINDOW *win, int height, int width, unsigned char *buf, int startline, int hscroll)
310 /*
311  * Desc: Print a page of text in the current window, starting at line <startline>
312  *       with a <horizontal> scroll of hscroll from buffer <buf>
313  */
314 {
315     int i, j;
316     unsigned char *b;
317
318     b = getline(buf, startline);
319     for (i=0; i<height; i++) {
320         /* clear line */
321         wmove(win, 1+i, 1);
322         for (j=0; j<width-2; j++) waddnstr(win, " ", 1);
323         wmove(win, 1+i, 1);
324         j = 0;
325         /* scroll to the right */
326         while (*b && (*b != '\n') && (*b != '\r') && (j<hscroll)) {
327             b++;
328             j++;
329         }
330         /* print new line */
331         j = 0;
332         while (*b && (*b != '\n') && (*b != '\r') && (j<width-2)) {
333             waddnstr(win, b, 1);
334             if (*b != '\t') {   /* check for tabs */
335                 j++;
336             } else {
337                 j = ((int) (j+1)/8 + 1) * 8 - 1;
338             }
339             b++;
340         }
341         while (*b && (*b != '\n') && (*b != '\r')) b++;
342         if (*b) b++;    /* skip over '\n', if it exists */
343     }
344 } /* print_page() */
345
346
347
348