xref: /freebsd/contrib/bsddialog/lib/textbox.c (revision 7be9a3b45356747f9fcb6d69a722c1c95f8060bf)
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 extern struct bsddialog_theme t;
38 
39 static void
40 textbox_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h,
41     int *w, int hpad, int wpad, struct buttons bs)
42 {
43 	if (cols == BSDDIALOG_AUTOSIZE)
44 		*w = widget_min_width(conf, 0, wpad, &bs);
45 
46 	if (rows == BSDDIALOG_AUTOSIZE)
47 		*h = widget_min_height(conf, 0, hpad, true);
48 }
49 
50 static int
51 textbox_checksize(int rows, int cols, int hpad, struct buttons bs)
52 {
53 	int mincols;
54 
55 	mincols = VBORDERS + bs.sizebutton;
56 
57 	if (cols < mincols)
58 		RETURN_ERROR("Few cols for the textbox");
59 
60 	if (rows < 4 /* HBORDERS + button*/ + (hpad > 0 ? 1 : 0))
61 		RETURN_ERROR("Few rows for the textbox");
62 
63 	return (0);
64 }
65 
66 /* API */
67 int
68 bsddialog_textbox(struct bsddialog_conf *conf, const char* file, int rows,
69     int cols)
70 {
71 	bool loop;
72 	int i, output, input;
73 	int y, x, h, w, hpad, wpad, ypad, xpad, ys, ye, xs, xe, printrows;
74 	char buf[BUFSIZ];
75 	FILE *fp;
76 	struct buttons bs;
77 	WINDOW *shadow, *widget, *pad;
78 
79 	if ((fp = fopen(file, "r")) == NULL)
80 		RETURN_ERROR("Cannot open file");
81 
82 	hpad = 1;
83 	wpad = 1;
84 	pad = newpad(hpad, wpad);
85 	wbkgd(pad, t.dialog.color);
86 	i = 0;
87 	while (fgets(buf, BUFSIZ, fp) != NULL) {
88 		if ((int) strlen(buf) > wpad) {
89 			wpad = strlen(buf);
90 			wresize(pad, hpad, wpad);
91 		}
92 		if (i > hpad-1) {
93 			hpad++;
94 			wresize(pad, hpad, wpad);
95 		}
96 		mvwaddstr(pad, i, 0, buf);
97 		i++;
98 	}
99 	fclose(fp);
100 
101 	bs.nbuttons = 1;
102 	bs.label[0] = "EXIT";
103 	if (conf->button.ok_label != NULL)
104 		bs.label[0] = conf->button.ok_label;
105 	bs.value[0] = BSDDIALOG_OK;
106 	bs.curr = 0;
107 	bs.sizebutton = strlen(bs.label[0]) + 2;
108 
109 	if (set_widget_size(conf, rows, cols, &h, &w) != 0)
110 		return (BSDDIALOG_ERROR);
111 	textbox_autosize(conf, rows, cols, &h, &w, hpad, wpad, bs);
112 	if (textbox_checksize(h, w, hpad, bs) != 0)
113 		return (BSDDIALOG_ERROR);
114 	if (set_widget_position(conf, &y, &x, h, w) != 0)
115 		return (BSDDIALOG_ERROR);
116 
117 	if (new_dialog(conf, &shadow, &widget, y, x, h, w, NULL, NULL, &bs,
118 	    true) != 0)
119 		return (BSDDIALOG_ERROR);
120 
121 	ys = y + 1;
122 	xs = x + 1;
123 	ye = ys + h - 5;
124 	xe = xs + w - 3;
125 	ypad = xpad = 0;
126 	printrows = h-4;
127 	loop = true;
128 	while (loop) {
129 		wnoutrefresh(widget);
130 		pnoutrefresh(pad, ypad, xpad, ys, xs, ye, xe);
131 		doupdate();
132 		input = getch();
133 		switch(input) {
134 		case KEY_ENTER:
135 		case 10: /* Enter */
136 			output = BSDDIALOG_OK;
137 			loop = false;
138 			break;
139 		case 27: /* Esc */
140 			if (conf->key.enable_esc) {
141 				output = BSDDIALOG_ESC;
142 				loop = false;
143 			}
144 			break;
145 		case KEY_HOME:
146 			ypad = 0;
147 			break;
148 		case KEY_END:
149 			ypad = hpad - printrows;
150 			ypad = ypad < 0 ? 0 : ypad;
151 			break;
152 		case KEY_PPAGE:
153 			ypad -= printrows;
154 			ypad = ypad < 0 ? 0 : ypad;
155 			break;
156 		case KEY_NPAGE:
157 			ypad += printrows;
158 			if (ypad + printrows > hpad)
159 				ypad = hpad - printrows;
160 			break;
161 		case '0':
162 			xpad = 0;
163 		case KEY_LEFT:
164 		case 'h':
165 			xpad = xpad > 0 ? xpad - 1 : 0;
166 			break;
167 		case KEY_RIGHT:
168 		case 'l':
169 			xpad = (xpad + w-2) < wpad-1 ? xpad + 1 : xpad;
170 			break;
171 		case KEY_UP:
172 		case 'k':
173 			ypad = ypad > 0 ? ypad - 1 : 0;
174 			break;
175 		case KEY_DOWN:
176 		case'j':
177 			ypad = ypad + printrows <= hpad -1 ? ypad + 1 : ypad;
178 			break;
179 		case KEY_F(1):
180 			if (conf->f1_file == NULL && conf->f1_message == NULL)
181 				break;
182 			if (f1help(conf) != 0)
183 				return (BSDDIALOG_ERROR);
184 			/* No break, screen size can change */
185 		case KEY_RESIZE:
186 			/* Important for decreasing screen */
187 			hide_widget(y, x, h, w, conf->shadow);
188 			refresh();
189 
190 			if (set_widget_size(conf, rows, cols, &h, &w) != 0)
191 				return (BSDDIALOG_ERROR);
192 			textbox_autosize(conf, rows, cols, &h, &w, hpad, wpad,
193 			    bs);
194 			if (textbox_checksize(h, w, hpad, bs) != 0)
195 				return (BSDDIALOG_ERROR);
196 			if (set_widget_position(conf, &y, &x, h, w) != 0)
197 				return (BSDDIALOG_ERROR);
198 
199 			ys = y + 1;
200 			xs = x + 1;
201 			ye = ys + h - 5;
202 			xe = xs + w - 3;
203 			ypad = xpad = 0;
204 			printrows = h - 4;
205 
206 			if (update_dialog(conf, shadow, widget, y, x, h, w,
207 			    NULL, NULL, &bs, true) != 0)
208 				return (BSDDIALOG_ERROR);
209 
210 			/* Important to fix grey lines expanding screen */
211 			refresh();
212 			break;
213 		default:
214 			if (shortcut_buttons(input, &bs)) {
215 				output = bs.value[bs.curr];
216 				loop = false;
217 			}
218 		}
219 	}
220 
221 	end_dialog(conf, shadow, widget, pad);
222 
223 	return (output);
224 }