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.
23 #include "dialog.priv.h"
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);
33 * Display a message box. Program will pause and display an "OK" button
34 * if the parameter 'pause' is non-zero.
36 int dialog_msgbox(unsigned char *title, unsigned char *prompt, int height, int width, int pause)
38 int i, j, x, y, key = 0;
42 height = strheight(prompt)+2+2*(!!pause);
45 j = ((title != NULL) ? strwidth(title) : 0);
49 width = MAX(width,10);
55 /* center dialog box on screen */
56 x = DialogX ? DialogX : (COLS - width)/2;
57 y = DialogY ? DialogY : (LINES - height)/2;
61 draw_shadow(stdscr, y, x, height, width);
63 dialog = newwin(height, width, y, x);
66 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
71 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
74 wattrset(dialog, title_attr);
75 wmove(dialog, 0, (width - strlen(title))/2 - 1);
77 waddstr(dialog, title);
80 wattrset(dialog, dialog_attr);
82 print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);
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++)
95 display_helpline(dialog, height-1, width);
96 print_button(dialog, " OK ", height-2, width/2-6, TRUE);
98 while (key != ESC && key != '\n' && key != ' ' && key != '\r')
109 return (key == ESC ? -1 : 0);
111 /* End of dialog_msgbox() */
114 dialog_mesgbox(unsigned char *title, unsigned char *prompt, int height, int width)
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
120 int i, j, x, y, key=0;
121 int theight, startline, hscroll, max_lines;
125 height = strheight(prompt)+2+2;
127 i = strwidth(prompt);
128 j = ((title != NULL) ? strwidth(title) : 0);
131 width = MAX(width,10);
137 /* center dialog box on screen */
138 x = (COLS - width)/2;
139 y = (LINES - height)/2;
143 draw_shadow(stdscr, y, x, height, width);
145 dialog = newwin(height, width, y, x);
146 if (dialog == NULL) {
148 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
151 keypad(dialog, TRUE);
153 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
156 wattrset(dialog, title_attr);
157 wmove(dialog, 0, (width - strlen(title))/2 - 1);
159 waddstr(dialog, title);
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++)
173 display_helpline(dialog, height-1, width);
174 print_button(dialog, " OK ", height-2, width/2-6, TRUE);
175 wattrset(dialog, dialog_attr);
177 theight = height - 4;
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);
185 while ((key != ESC) && (key != '\n') && (key != '\r')) {
186 key = wgetch(dialog);
193 startline = max_lines - theight;
194 if (startline < 0) startline = 0;
196 case '\020': /* ^P */
198 if (startline > 0) startline--;
200 case '\016': /* ^N */
202 if (startline < max_lines - theight) startline++;
208 if (hscroll > 0) hscroll-=5;
209 if (hscroll < 0) hscroll =0;
212 if (startline - height > 0) {
213 startline -= theight;
219 if (startline + theight < max_lines - theight) {
220 startline += theight;
222 startline = max_lines - theight;
223 if (startline < 0) startline = 0;
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);
238 return (key == ESC ? -1 : 0);
240 } /* dialog_mesgbox() */
243 print_perc(WINDOW *win, int y, int x, float p)
245 * Desc: print p as a percentage at the coordinates (y,x)
251 sprintf(ps, "(%3d%%)", (int) (p*100));
259 getnlines(unsigned char *buf)
261 * Desc: count the # of lines in <buf>
269 if (*buf == '\n' || *buf == '\r')
278 getline(unsigned char *buf, int n)
280 * Desc: return a pointer to the n'th line in <buf> or NULL if its
291 while (*buf && i<n) {
292 if (*buf == '\n' || *buf == '\r') {
305 print_page(WINDOW *win, int height, int width, unsigned char *buf, int startline, int hscroll)
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>
314 b = getline(buf, startline);
315 for (i=0; i<height; i++) {
318 for (j=0; j<width-2; j++) waddnstr(win, " ", 1);
321 /* scroll to the right */
322 while (*b && (*b != '\n') && (*b != '\r') && (j<hscroll)) {
328 while (*b && (*b != '\n') && (*b != '\r') && (j<width-2)) {
330 if (*b != '\t') { /* check for tabs */
333 j = ((int) (j+1)/8 + 1) * 8 - 1;
337 while (*b && (*b != '\n') && (*b != '\r')) b++;
338 if (*b) b++; /* skip over '\n', if it exists */