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 $
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;
44 height
= strheight(prompt
)+2+2*(!!pause
);
47 j
= ((title
!= NULL
) ? strwidth(title
) : 0);
51 width
= MAX(width
,10);
57 /* center dialog box on screen */
58 x
= DialogX
? DialogX
: (COLS
- width
)/2;
59 y
= DialogY
? DialogY
: (LINES
- height
)/2;
63 draw_shadow(stdscr
, y
, x
, height
, width
);
65 dialog
= newwin(height
, width
, y
, x
);
68 fprintf(stderr
, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height
,width
,y
,x
);
73 draw_box(dialog
, 0, 0, height
, width
, dialog_attr
, border_attr
);
76 wattrset(dialog
, title_attr
);
77 wmove(dialog
, 0, (width
- strlen(title
))/2 - 1);
79 waddstr(dialog
, title
);
82 wattrset(dialog
, dialog_attr
);
84 print_autowrap(dialog
, prompt
, height
-1, width
-2, width
, 1, 2, TRUE
, FALSE
);
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
++)
97 display_helpline(dialog
, height
-1, width
);
98 print_button(dialog
, " OK ", height
-2, width
/2-6, TRUE
);
100 while (key
!= ESC
&& key
!= '\n' && key
!= ' ' && key
!= '\r')
101 key
= wgetch(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
;
127 height
= strheight(prompt
)+2+2;
129 i
= strwidth(prompt
);
130 j
= ((title
!= NULL
) ? strwidth(title
) : 0);
133 width
= MAX(width
,10);
139 /* center dialog box on screen */
140 x
= (COLS
- width
)/2;
141 y
= (LINES
- height
)/2;
145 draw_shadow(stdscr
, y
, x
, height
, width
);
147 dialog
= newwin(height
, width
, y
, x
);
148 if (dialog
== NULL
) {
150 fprintf(stderr
, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height
,width
,y
,x
);
153 keypad(dialog
, TRUE
);
155 draw_box(dialog
, 0, 0, height
, width
, dialog_attr
, border_attr
);
158 wattrset(dialog
, title_attr
);
159 wmove(dialog
, 0, (width
- strlen(title
))/2 - 1);
161 waddstr(dialog
, title
);
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
++)
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;
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);
187 while ((key
!= ESC
) && (key
!= '\n') && (key
!= '\r') && (key
!= ' ')) {
188 key
= wgetch(dialog
);
195 startline
= max_lines
- theight
;
196 if (startline
< 0) startline
= 0;
198 case '\020': /* ^P */
200 if (startline
> 0) startline
--;
202 case '\016': /* ^N */
204 if (startline
< max_lines
- theight
) startline
++;
210 if (hscroll
> 0) hscroll
-=5;
211 if (hscroll
< 0) hscroll
=0;
214 if (startline
- height
> 0) {
215 startline
-= theight
;
221 if (startline
+ theight
< max_lines
- theight
) {
222 startline
+= theight
;
224 startline
= max_lines
- theight
;
225 if (startline
< 0) startline
= 0;
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);
240 return (key
== ESC
? -1 : 0);
242 } /* dialog_mesgbox() */
245 print_perc(WINDOW
*win
, int y
, int x
, float p
)
247 * Desc: print p as a percentage at the coordinates (y,x)
253 sprintf(ps
, "(%3d%%)", (int) (p
*100));
261 getnlines(unsigned char *buf
)
263 * Desc: count the # of lines in <buf>
271 if (*buf
== '\n' || *buf
== '\r')
280 getline(unsigned char *buf
, int n
)
282 * Desc: return a pointer to the n'th line in <buf> or NULL if its
293 while (*buf
&& i
<n
) {
294 if (*buf
== '\n' || *buf
== '\r') {
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>
316 b
= getline(buf
, startline
);
317 for (i
=0; i
<height
; i
++) {
320 for (j
=0; j
<width
-2; j
++) waddnstr(win
, " ", 1);
323 /* scroll to the right */
324 while (*b
&& (*b
!= '\n') && (*b
!= '\r') && (j
<hscroll
)) {
330 while (*b
&& (*b
!= '\n') && (*b
!= '\r') && (j
<width
-2)) {
332 if (*b
!= '\t') { /* check for tabs */
335 j
= ((int) (j
+1)/8 + 1) * 8 - 1;
339 while (*b
&& (*b
!= '\n') && (*b
!= '\r')) b
++;
340 if (*b
) b
++; /* skip over '\n', if it exists */