2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021-2022 Alfonso Sabato Siciliano
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/param.h>
33 #include "bsddialog.h"
34 #include "bsddialog_theme.h"
38 message_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h,
39 int *w, const char *text, bool *hastext, struct buttons bs)
43 if (cols == BSDDIALOG_AUTOSIZE || rows == BSDDIALOG_AUTOSIZE ||
45 if (text_size(conf, rows, cols, text, &bs, 0, 1, &htext,
47 return (BSDDIALOG_ERROR);
49 *hastext = htext > 0 ? true : false;
52 if (cols == BSDDIALOG_AUTOSIZE)
53 *w = widget_min_width(conf, wtext, 0, &bs);
55 if (rows == BSDDIALOG_AUTOSIZE)
56 *h = widget_min_height(conf, htext, 0, true);
62 message_checksize(int rows, int cols, bool hastext, struct buttons bs)
67 mincols += buttons_min_width(bs);
70 RETURN_ERROR("Few cols, Msgbox and Yesno need at least width "
71 "for borders, buttons and spaces between buttons");
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");
82 textupdate(WINDOW *widget, WINDOW *textpad, int htextpad, int ytextpad,
87 getbegyx(widget, y, x);
88 getmaxyx(widget, h, w);
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);
98 pnoutrefresh(textpad, ytextpad, 0, y+1, x+2, y+h-4, x+w-2);
102 do_message(struct bsddialog_conf *conf, const char *text, int rows, int cols,
106 int y, x, h, w, retval, ytextpad, htextpad, printrows, unused;
107 WINDOW *widget, *textpad, *shadow;
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);
119 if (new_dialog(conf, &shadow, &widget, y, x, h, w, &textpad, text, &bs,
121 return (BSDDIALOG_ERROR);
125 getmaxyx(textpad, htextpad, unused);
126 unused++; /* fix unused error */
129 textupdate(widget, textpad, htextpad, ytextpad, hastext);
131 if (get_wch(&input) == ERR)
136 retval = bs.value[bs.curr];
140 if (conf->key.enable_esc) {
141 retval = BSDDIALOG_ESC;
146 bs.curr = (bs.curr + 1) % bs.nbuttons;
147 draw_buttons(widget, bs, true);
148 wnoutrefresh(widget);
153 draw_buttons(widget, bs, true);
154 wnoutrefresh(widget);
158 if (bs.curr < (int)bs.nbuttons - 1) {
160 draw_buttons(widget, bs, true);
161 wnoutrefresh(widget);
169 if (ytextpad + printrows < htextpad)
176 ytextpad = htextpad - printrows;
177 ytextpad = ytextpad < 0 ? 0 : ytextpad;
180 ytextpad -= printrows;
181 ytextpad = ytextpad < 0 ? 0 : ytextpad;
184 ytextpad += printrows;
185 if (ytextpad + printrows > htextpad)
186 ytextpad = htextpad - printrows;
189 if (conf->key.f1_file == NULL &&
190 conf->key.f1_message == NULL)
192 if (f1help(conf) != 0)
193 return (BSDDIALOG_ERROR);
194 /* No break, screen size can change */
196 /* Important for decreasing screen */
197 hide_widget(y, x, h, w, conf->shadow);
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,
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);
210 if (update_dialog(conf, shadow, widget, y, x, h, w,
211 textpad, text, &bs, true) != 0)
212 return (BSDDIALOG_ERROR);
215 getmaxyx(textpad, htextpad, unused);
217 textupdate(widget, textpad, htextpad, ytextpad, hastext);
219 /* Important to fix grey lines expanding screen */
223 if (shortcut_buttons(input, &bs)) {
224 retval = bs.value[bs.curr];
230 end_dialog(conf, shadow, widget, textpad);
237 bsddialog_msgbox(struct bsddialog_conf *conf, const char *text, int rows,
242 get_buttons(conf, &bs, BUTTON_OK_LABEL, NULL);
244 return (do_message(conf, text, rows, cols, bs));
248 bsddialog_yesno(struct bsddialog_conf *conf, const char *text, int rows,
253 get_buttons(conf, &bs, "Yes", "No");
255 return (do_message(conf, text, rows, cols, bs));