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