2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021-2023 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
30 #include "bsddialog.h"
31 #include "bsddialog_theme.h"
35 int ypad; /* y scrollable pad */
36 int htext; /* real h text to draw, to use with htextpad */
37 int htextpad; /* h textpad, draw_dialog() set at least 1 */
38 int printrows; /* h - BORDER - HBUTTONS - BORDER */
41 static void textupdate(struct dialog *d, struct scroll *s)
43 if (s->htext > 0 && s->htextpad > s->printrows) {
44 wattron(d->widget, t.dialog.arrowcolor);
45 mvwprintw(d->widget, d->h - HBUTTONS - BORDER,
46 d->w - 4 - TEXTHMARGIN - BORDER,
47 "%3d%%", 100 * (s->ypad + s->printrows) / s->htextpad);
48 wattroff(d->widget, t.dialog.arrowcolor);
49 wnoutrefresh(d->widget);
51 rtextpad(d, s->ypad, 0, 0, HBUTTONS);
54 static int message_size_position(struct dialog *d, int *htext)
58 if (set_widget_size(d->conf, d->rows, d->cols, &d->h, &d->w) != 0)
59 return (BSDDIALOG_ERROR);
60 if (set_widget_autosize(d->conf, d->rows, d->cols, &d->h, &d->w,
61 d->text, (*htext < 0) ? htext : NULL, &d->bs, 0, 0) != 0)
62 return (BSDDIALOG_ERROR);
63 minw = (*htext > 0) ? 1 + TEXTHMARGINS : 0 ;
64 if (widget_checksize(d->h, d->w, &d->bs, MIN(*htext, 1), minw) != 0)
65 return (BSDDIALOG_ERROR);
66 if (set_widget_position(d->conf, &d->y, &d->x, d->h, d->w) != 0)
67 return (BSDDIALOG_ERROR);
72 static int message_draw(struct dialog *d, struct scroll *s)
78 refresh(); /* Important for decreasing screen */
80 if (message_size_position(d, &s->htext) != 0)
81 return (BSDDIALOG_ERROR);
82 if (draw_dialog(d) != 0)
83 return (BSDDIALOG_ERROR);
85 refresh(); /* Important to fix grey lines expanding screen */
87 s->printrows = d->h - BORDER - HBUTTONS - BORDER;
89 getmaxyx(d->textpad, s->htextpad, unused);
90 unused++; /* fix unused error */
96 do_message(struct bsddialog_conf *conf, const char *text, int rows, int cols,
97 const char *oklabel, const char *cancellabel)
105 if (prepare_dialog(conf, text, rows, cols, &d) != 0)
106 return (BSDDIALOG_ERROR);
107 set_buttons(&d, true, oklabel, cancellabel);
109 if(message_draw(&d, &s) != 0)
110 return (BSDDIALOG_ERROR);
116 if (get_wch(&input) == ERR)
121 retval = BUTTONVALUE(d.bs);
125 if (d.conf->key.enable_esc) {
126 retval = BSDDIALOG_ESC;
132 d.bs.curr = (d.bs.curr + 1) % d.bs.nbuttons;
138 d.bs.curr = d.bs.nbuttons - 1;
146 if (s.ypad + s.printrows < s.htextpad)
153 s.ypad = MAX(s.htextpad - s.printrows, 0);
156 s.ypad = MAX(s.ypad - s.printrows, 0);
159 s.ypad += s.printrows;
160 if (s.ypad + s.printrows > s.htextpad)
161 s.ypad = s.htextpad - s.printrows;
164 if (d.conf->key.f1_file == NULL &&
165 d.conf->key.f1_message == NULL)
167 if (f1help_dialog(d.conf) != 0)
168 return (BSDDIALOG_ERROR);
169 if(message_draw(&d, &s) != 0)
170 return (BSDDIALOG_ERROR);
173 if(message_draw(&d, &s) != 0)
174 return (BSDDIALOG_ERROR);
177 if (shortcut_buttons(input, &d.bs)) {
180 retval = BUTTONVALUE(d.bs);
193 bsddialog_msgbox(struct bsddialog_conf *conf, const char *text, int rows,
196 return (do_message(conf, text, rows, cols, OK_LABEL, NULL));
200 bsddialog_yesno(struct bsddialog_conf *conf, const char *text, int rows,
203 return (do_message(conf, text, rows, cols, "Yes", "No"));
207 bsddialog_infobox(struct bsddialog_conf *conf, const char *text, int rows,
213 if (prepare_dialog(conf, text, rows, cols, &d) != 0)
214 return (BSDDIALOG_ERROR);
216 if (message_size_position(&d, &htext) != 0)
217 return (BSDDIALOG_ERROR);
218 if (draw_dialog(&d) != 0)
219 return (BSDDIALOG_ERROR);
225 return (BSDDIALOG_OK);