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.
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 $";
27 #include "dialog.priv.h"
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);
37 * Display a message box. Program will pause and display an "OK" button
38 * if the parameter 'pause' is non-zero.
40 int dialog_msgbox(unsigned char *title, unsigned char *prompt, int height, int width, int pause)
42 int i, j, x, y, key = 0;
46 height = strheight(prompt)+2+2*(!!pause);
49 j = ((title != NULL) ? strwidth(title) : 0);
53 width = MAX(width,10);
59 /* center dialog box on screen */
60 x = DialogX ? DialogX : (COLS - width)/2;
61 y = DialogY ? DialogY : (LINES - height)/2;
65 draw_shadow(stdscr, y, x, height, width);
67 dialog = newwin(height, width, y, x);
70 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
75 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
78 wattrset(dialog, title_attr);
79 wmove(dialog, 0, (width - strlen(title))/2 - 1);
81 waddstr(dialog, title);
84 wattrset(dialog, dialog_attr);
86 print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);
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++)
99 display_helpline(dialog, height-1, width);
100 print_button(dialog, " OK ", height-2, width/2-6, TRUE);
102 while (key != ESC && key != '\n' && key != ' ' && key != '\r')
103 key = wgetch(dialog);
113 return (key == ESC ? -1 : 0);
115 /* End of dialog_msgbox() */
118 dialog_mesgbox(unsigned char *title, unsigned char *prompt, int height, int width)
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
124 int i, j, x, y, key=0;
125 int theight, startline, hscroll, max_lines;
129 height = strheight(prompt)+2+2;
131 i = strwidth(prompt);
132 j = ((title != NULL) ? strwidth(title) : 0);
135 width = MAX(width,10);
141 /* center dialog box on screen */
142 x = (COLS - width)/2;
143 y = (LINES - height)/2;
147 draw_shadow(stdscr, y, x, height, width);
149 dialog = newwin(height, width, y, x);
150 if (dialog == NULL) {
152 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
155 keypad(dialog, TRUE);
157 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
160 wattrset(dialog, title_attr);
161 wmove(dialog, 0, (width - strlen(title))/2 - 1);
163 waddstr(dialog, title);
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++)
177 display_helpline(dialog, height-1, width);
178 print_button(dialog, " OK ", height-2, width/2-6, TRUE);
179 wattrset(dialog, dialog_attr);
181 theight = height - 4;
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);
189 while ((key != ESC) && (key != '\n') && (key != '\r') && (key != ' ')) {
190 key = wgetch(dialog);
197 startline = max_lines - theight;
198 if (startline < 0) startline = 0;
200 case '\020': /* ^P */
202 if (startline > 0) startline--;
204 case '\016': /* ^N */
206 if (startline < max_lines - theight) startline++;
212 if (hscroll > 0) hscroll-=5;
213 if (hscroll < 0) hscroll =0;
216 if (startline - height > 0) {
217 startline -= theight;
223 if (startline + theight < max_lines - theight) {
224 startline += theight;
226 startline = max_lines - theight;
227 if (startline < 0) startline = 0;
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);
242 return (key == ESC ? -1 : 0);
244 } /* dialog_mesgbox() */
247 print_perc(WINDOW *win, int y, int x, float p)
249 * Desc: print p as a percentage at the coordinates (y,x)
255 sprintf(ps, "(%3d%%)", (int) (p*100));
263 getnlines(unsigned char *buf)
265 * Desc: count the # of lines in <buf>
273 if (*buf == '\n' || *buf == '\r')
282 getline(unsigned char *buf, int n)
284 * Desc: return a pointer to the n'th line in <buf> or NULL if its
295 while (*buf && i<n) {
296 if (*buf == '\n' || *buf == '\r') {
309 print_page(WINDOW *win, int height, int width, unsigned char *buf, int startline, int hscroll)
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>
318 b = getline(buf, startline);
319 for (i=0; i<height; i++) {
322 for (j=0; j<width-2; j++) waddnstr(win, " ", 1);
325 /* scroll to the right */
326 while (*b && (*b != '\n') && (*b != '\r') && (j<hscroll)) {
332 while (*b && (*b != '\n') && (*b != '\r') && (j<width-2)) {
334 if (*b != '\t') { /* check for tabs */
337 j = ((int) (j+1)/8 + 1) * 8 - 1;
341 while (*b && (*b != '\n') && (*b != '\r')) b++;
342 if (*b) b++; /* skip over '\n', if it exists */