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