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, 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 ytextpad = 0; 124 getmaxyx(textpad, htextpad, unused); 125 unused++; /* fix unused error */ 126 textupdate(widget, textpad, htextpad, ytextpad, hastext); 127 loop = true; 128 while (loop) { 129 doupdate(); 130 if (get_wch(&input) == ERR) 131 continue; 132 switch (input) { 133 case KEY_ENTER: 134 case 10: /* Enter */ 135 retval = bs.value[bs.curr]; 136 loop = false; 137 break; 138 case 27: /* Esc */ 139 if (conf->key.enable_esc) { 140 retval = BSDDIALOG_ESC; 141 loop = false; 142 } 143 break; 144 case '\t': /* TAB */ 145 bs.curr = (bs.curr + 1) % bs.nbuttons; 146 draw_buttons(widget, bs, true); 147 wnoutrefresh(widget); 148 break; 149 case KEY_LEFT: 150 if (bs.curr > 0) { 151 bs.curr--; 152 draw_buttons(widget, bs, true); 153 wnoutrefresh(widget); 154 } 155 break; 156 case KEY_RIGHT: 157 if (bs.curr < (int)bs.nbuttons - 1) { 158 bs.curr++; 159 draw_buttons(widget, bs, true); 160 wnoutrefresh(widget); 161 } 162 break; 163 case KEY_F(1): 164 if (conf->key.f1_file == NULL && 165 conf->key.f1_message == NULL) 166 break; 167 if (f1help(conf) != 0) 168 return (BSDDIALOG_ERROR); 169 /* No break, screen size can change */ 170 case KEY_RESIZE: 171 /* Important for decreasing screen */ 172 hide_widget(y, x, h, w, conf->shadow); 173 refresh(); 174 175 if (set_widget_size(conf, rows, cols, &h, &w) != 0) 176 return (BSDDIALOG_ERROR); 177 if (message_autosize(conf, rows, cols, &h, &w, text, 178 NULL, bs) != 0) 179 return (BSDDIALOG_ERROR); 180 if (message_checksize(h, w, hastext, bs) != 0) 181 return (BSDDIALOG_ERROR); 182 if (set_widget_position(conf, &y, &x, h, w) != 0) 183 return (BSDDIALOG_ERROR); 184 185 if (update_dialog(conf, shadow, widget, y, x, h, w, 186 textpad, text, &bs, true) != 0) 187 return (BSDDIALOG_ERROR); 188 189 getmaxyx(textpad, htextpad, unused); 190 ytextpad = 0; 191 textupdate(widget, textpad, htextpad, ytextpad, hastext); 192 193 /* Important to fix grey lines expanding screen */ 194 refresh(); 195 break; 196 case KEY_UP: 197 if (ytextpad == 0) 198 break; 199 ytextpad--; 200 textupdate(widget, textpad, htextpad, ytextpad, hastext); 201 break; 202 case KEY_DOWN: 203 if (ytextpad + h - 4 >= htextpad) 204 break; 205 ytextpad++; 206 textupdate(widget, textpad, htextpad, ytextpad, hastext); 207 break; 208 default: 209 if (shortcut_buttons(input, &bs)) { 210 retval = bs.value[bs.curr]; 211 loop = false; 212 } 213 } 214 } 215 216 end_dialog(conf, shadow, widget, textpad); 217 218 return (retval); 219 } 220 221 /* API */ 222 int 223 bsddialog_msgbox(struct bsddialog_conf *conf, const char *text, int rows, 224 int cols) 225 { 226 struct buttons bs; 227 228 get_buttons(conf, &bs, BUTTON_OK_LABEL, NULL); 229 230 return (do_message(conf, text, rows, cols, bs)); 231 } 232 233 int 234 bsddialog_yesno(struct bsddialog_conf *conf, const char *text, int rows, 235 int cols) 236 { 237 struct buttons bs; 238 239 get_buttons(conf, &bs, "Yes", "No"); 240 241 return (do_message(conf, text, rows, cols, bs)); 242 }