flockfile(3): Update FreeBSD CVS ID, bump .Dd and add some references.
[dragonfly.git] / gnu / lib / libdialog / msgbox.c
blob086751e17aa71e3fb210ac300a75934696874097
1 /*
2 * msgbox.c -- implements the message box and info box
4 * AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
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.
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.
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.
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 $
24 #include <dialog.h>
25 #include "dialog.priv.h"
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);
35 * Display a message box. Program will pause and display an "OK" button
36 * if the parameter 'pause' is non-zero.
38 int dialog_msgbox(unsigned char *title, unsigned char *prompt, int height, int width, int pause)
40 int i, j, x, y, key = 0;
41 WINDOW *dialog;
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;
50 if (pause)
51 width = MAX(width,10);
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;
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);
71 keypad(dialog, TRUE);
73 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
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, ' ');
82 wattrset(dialog, dialog_attr);
83 wmove(dialog, 1, 2);
84 print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);
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';
105 else {
106 key = '\n';
107 wrefresh(dialog);
110 delwin(dialog);
111 return (key == ESC ? -1 : 0);
113 /* End of dialog_msgbox() */
116 dialog_mesgbox(unsigned char *title, unsigned char *prompt, int height, int width)
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
122 int i, j, x, y, key=0;
123 int theight, startline, hscroll, max_lines;
124 WINDOW *dialog;
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;
133 width = MAX(width,10);
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;
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);
153 keypad(dialog, TRUE);
155 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
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, ' ');
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);
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;
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;
227 break;
228 case KEY_F(1):
229 case '?':
230 display_helpfile();
231 break;
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);
239 delwin(dialog);
240 return (key == ESC ? -1 : 0);
242 } /* dialog_mesgbox() */
244 static void
245 print_perc(WINDOW *win, int y, int x, float p)
247 * Desc: print p as a percentage at the coordinates (y,x)
250 char ps[10];
252 if (p>1.0) p=1.0;
253 sprintf(ps, "(%3d%%)", (int) (p*100));
254 wmove(win, y, x);
255 waddstr(win, ps);
257 return;
258 } /* print_perc() */
260 static int
261 getnlines(unsigned char *buf)
263 * Desc: count the # of lines in <buf>
266 int i = 0;
268 if (*buf)
269 i++;
270 while (*buf) {
271 if (*buf == '\n' || *buf == '\r')
272 i++;
273 buf++;
275 return(i);
276 } /* getlines() */
279 unsigned char *
280 getline(unsigned char *buf, int n)
282 * Desc: return a pointer to the n'th line in <buf> or NULL if its
283 * not there
286 int i;
288 if (n<0) {
289 return(NULL);
292 i=0;
293 while (*buf && i<n) {
294 if (*buf == '\n' || *buf == '\r') {
295 i++;
297 buf++;
299 if (i<n) {
300 return(NULL);
301 } else {
302 return(buf);
304 } /* getline() */
306 static void
307 print_page(WINDOW *win, int height, int width, unsigned char *buf, int startline, int hscroll)
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>
313 int i, j;
314 unsigned char *b;
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++;
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;
337 b++;
339 while (*b && (*b != '\n') && (*b != '\r')) b++;
340 if (*b) b++; /* skip over '\n', if it exists */
342 } /* print_page() */