xref: /freebsd/contrib/bsddialog/lib/messagebox.c (revision be181ee2a28aa2b4b0e76684bce9f673ef668874)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021-2022 Alfonso Sabato Siciliano
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 
30 #include <curses.h>
31 #include <string.h>
32 
33 #include "bsddialog.h"
34 #include "bsddialog_theme.h"
35 #include "lib_util.h"
36 
37 static int
38 message_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h,
39     int *w, const char *text, bool *hastext, struct buttons bs)
40 {
41 	int htext, wtext;
42 
43 	if (cols == BSDDIALOG_AUTOSIZE || rows == BSDDIALOG_AUTOSIZE ||
44 	    hastext != NULL) {
45 		if (text_size(conf, rows, cols, text, &bs, 0, 1, &htext,
46 		    &wtext) != 0)
47 			return (BSDDIALOG_ERROR);
48 		if (hastext != NULL)
49 			*hastext = htext > 0 ? true : false;
50 	}
51 
52 	if (cols == BSDDIALOG_AUTOSIZE)
53 		*w = widget_min_width(conf, wtext, 0, &bs);
54 
55 	if (rows == BSDDIALOG_AUTOSIZE)
56 		*h = widget_min_height(conf, htext, 0, true);
57 
58 	return (0);
59 }
60 
61 static int
62 message_checksize(int rows, int cols, bool hastext, struct buttons bs)
63 {
64 	int mincols;
65 
66 	mincols = VBORDERS;
67 	mincols += buttons_min_width(bs);
68 
69 	if (cols < mincols)
70 		RETURN_ERROR("Few cols, Msgbox and Yesno need at least width "
71 		    "for borders, buttons and spaces between buttons");
72 
73 	if (rows < HBORDERS + 2 /* buttons */)
74 		RETURN_ERROR("Msgbox and Yesno need at least 4 rows");
75 	if (hastext && rows < HBORDERS + 2 /*buttons*/ + 1 /* text row */)
76 		RETURN_ERROR("Msgbox and Yesno with text need at least 5 rows");
77 
78 	return (0);
79 }
80 
81 static void
82 textupdate(WINDOW *widget, WINDOW *textpad, int htextpad, int ytextpad,
83     bool hastext)
84 {
85 	int y, x, h, w;
86 
87 	getbegyx(widget, y, x);
88 	getmaxyx(widget, h, w);
89 
90 	if (hastext && htextpad > h - 4) {
91 		wattron(widget, t.dialog.arrowcolor);
92 		mvwprintw(widget, h-3, w-6, "%3d%%",
93 		    100 * (ytextpad+h-4)/ htextpad);
94 		wattroff(widget, t.dialog.arrowcolor);
95 		wnoutrefresh(widget);
96 	}
97 
98 	pnoutrefresh(textpad, ytextpad, 0, y+1, x+2, y+h-4, x+w-2);
99 }
100 
101 static int
102 do_message(struct bsddialog_conf *conf, const char *text, int rows, int cols,
103     struct buttons bs)
104 {
105 	bool hastext, loop;
106 	int y, x, h, w, retval, ytextpad, htextpad, printrows, unused;
107 	WINDOW *widget, *textpad, *shadow;
108 	wint_t input;
109 
110 	if (set_widget_size(conf, rows, cols, &h, &w) != 0)
111 		return (BSDDIALOG_ERROR);
112 	if (message_autosize(conf, rows, cols, &h, &w, text, &hastext, bs) != 0)
113 		return (BSDDIALOG_ERROR);
114 	if (message_checksize(h, w, hastext, bs) != 0)
115 		return (BSDDIALOG_ERROR);
116 	if (set_widget_position(conf, &y, &x, h, w) != 0)
117 		return (BSDDIALOG_ERROR);
118 
119 	if (new_dialog(conf, &shadow, &widget, y, x, h, w, &textpad, text, &bs,
120 	    true) != 0)
121 		return (BSDDIALOG_ERROR);
122 
123 	printrows = h - 4;
124 	ytextpad = 0;
125 	getmaxyx(textpad, htextpad, unused);
126 	unused++; /* fix unused error */
127 	loop = true;
128 	while (loop) {
129 		textupdate(widget, textpad, htextpad, ytextpad, hastext);
130 		doupdate();
131 		if (get_wch(&input) == ERR)
132 			continue;
133 		switch (input) {
134 		case KEY_ENTER:
135 		case 10: /* Enter */
136 			retval = bs.value[bs.curr];
137 			loop = false;
138 			break;
139 		case 27: /* Esc */
140 			if (conf->key.enable_esc) {
141 				retval = BSDDIALOG_ESC;
142 				loop = false;
143 			}
144 			break;
145 		case '\t': /* TAB */
146 			bs.curr = (bs.curr + 1) % bs.nbuttons;
147 			draw_buttons(widget, bs, true);
148 			wnoutrefresh(widget);
149 			break;
150 		case KEY_LEFT:
151 			if (bs.curr > 0) {
152 				bs.curr--;
153 				draw_buttons(widget, bs, true);
154 				wnoutrefresh(widget);
155 			}
156 			break;
157 		case KEY_RIGHT:
158 			if (bs.curr < (int)bs.nbuttons - 1) {
159 				bs.curr++;
160 				draw_buttons(widget, bs, true);
161 				wnoutrefresh(widget);
162 			}
163 			break;
164 		case KEY_UP:
165 			if (ytextpad > 0)
166 				ytextpad--;
167 			break;
168 		case KEY_DOWN:
169 			if (ytextpad + printrows < htextpad)
170 				ytextpad++;
171 			break;
172 		case KEY_HOME:
173 			ytextpad = 0;
174 			break;
175 		case KEY_END:
176 			ytextpad = htextpad - printrows;
177 			ytextpad = ytextpad < 0 ? 0 : ytextpad;
178 			break;
179 		case KEY_PPAGE:
180 			ytextpad -= printrows;
181 			ytextpad = ytextpad < 0 ? 0 : ytextpad;
182 			break;
183 		case KEY_NPAGE:
184 			ytextpad += printrows;
185 			if (ytextpad + printrows > htextpad)
186 				ytextpad = htextpad - printrows;
187 			break;
188 		case KEY_F(1):
189 			if (conf->key.f1_file == NULL &&
190 			    conf->key.f1_message == NULL)
191 				break;
192 			if (f1help(conf) != 0)
193 				return (BSDDIALOG_ERROR);
194 			/* No break, screen size can change */
195 		case KEY_RESIZE:
196 			/* Important for decreasing screen */
197 			hide_widget(y, x, h, w, conf->shadow);
198 			refresh();
199 
200 			if (set_widget_size(conf, rows, cols, &h, &w) != 0)
201 				return (BSDDIALOG_ERROR);
202 			if (message_autosize(conf, rows, cols, &h, &w, text,
203 			    NULL, bs) != 0)
204 				return (BSDDIALOG_ERROR);
205 			if (message_checksize(h, w, hastext, bs) != 0)
206 				return (BSDDIALOG_ERROR);
207 			if (set_widget_position(conf, &y, &x, h, w) != 0)
208 				return (BSDDIALOG_ERROR);
209 
210 			if (update_dialog(conf, shadow, widget, y, x, h, w,
211 			    textpad, text, &bs, true) != 0)
212 				return (BSDDIALOG_ERROR);
213 
214 			printrows = h - 4;
215 			getmaxyx(textpad, htextpad, unused);
216 			ytextpad = 0;
217 			textupdate(widget, textpad, htextpad, ytextpad, hastext);
218 
219 			/* Important to fix grey lines expanding screen */
220 			refresh();
221 			break;
222 		default:
223 			if (shortcut_buttons(input, &bs)) {
224 				retval = bs.value[bs.curr];
225 				loop = false;
226 			}
227 		}
228 	}
229 
230 	end_dialog(conf, shadow, widget, textpad);
231 
232 	return (retval);
233 }
234 
235 /* API */
236 int
237 bsddialog_msgbox(struct bsddialog_conf *conf, const char *text, int rows,
238     int cols)
239 {
240 	struct buttons bs;
241 
242 	get_buttons(conf, &bs, BUTTON_OK_LABEL, NULL);
243 
244 	return (do_message(conf, text, rows, cols, bs));
245 }
246 
247 int
248 bsddialog_yesno(struct bsddialog_conf *conf, const char *text, int rows,
249     int cols)
250 {
251 	struct buttons bs;
252 
253 	get_buttons(conf, &bs, "Yes", "No");
254 
255 	return (do_message(conf, text, rows, cols, bs));
256 }