1/*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2021-2024 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#ifndef _LIBBSDDIALOG_UTIL_H_ 29#define _LIBBSDDIALOG_UTIL_H_ 30 31#define BORDER 1 32#define BORDERS (BORDER + BORDER) 33#define TEXTHMARGIN 1 34#define TEXTHMARGINS (TEXTHMARGIN + TEXTHMARGIN) 35#define HBUTTONS 2 36#define OK_LABEL "OK" 37#define CANCEL_LABEL "Cancel" 38 39/* theme util */ 40extern struct bsddialog_theme t; 41extern bool hastermcolors; 42 43#define MIN(a,b) (((a)<(b))?(a):(b)) 44#define MAX(a,b) (((a)>(b))?(a):(b)) 45/* debug */ 46#define BSDDIALOG_DEBUG(y,x,fmt, ...) do { \ 47 mvprintw(y, x, fmt, __VA_ARGS__); \ 48 refresh(); \ 49} while (0) 50/* error and diagnostic */ 51#define RETURN_ERROR(str) do { \ 52 set_error_string(str); \ 53 return (BSDDIALOG_ERROR); \ 54} while (0) 55#define RETURN_FMTERROR(fmt, ...) do { \ 56 set_fmt_error_string(fmt, __VA_ARGS__); \ 57 return (BSDDIALOG_ERROR); \ 58} while (0) 59/* check ptr */ 60#define CHECK_PTR(p) do { \ 61 if (p == NULL) \ 62 RETURN_ERROR("*" #p " is NULL"); \ 63} while (0) 64#define CHECK_ARRAY(nitem, a) do { \ 65 if (nitem > 0 && a == NULL) \ 66 RETURN_FMTERROR(#nitem " is %d but *" #a " is NULL", nitem); \ 67} while (0) 68/* widget utils */ 69#define KEY_CTRL(c) (c & 037) 70#define TEXTPAD(d, downnotext) rtextpad(d, 0, 0, 0, downnotext) 71#define SCREENLINES (getmaxy(stdscr)) 72#define SCREENCOLS (getmaxx(stdscr)) 73#define CHECK_STR(s) (s == NULL ? "" : s) 74#define UARROW(c) (c->ascii_lines ? '^' : ACS_UARROW) 75#define DARROW(c) (c->ascii_lines ? 'v' : ACS_DARROW) 76#define LARROW(c) (c->ascii_lines ? '<' : ACS_LARROW) 77#define RARROW(c) (c->ascii_lines ? '>' : ACS_RARROW) 78#define DRAW_BUTTONS(d) do { \ 79 draw_buttons(&d); \ 80 wnoutrefresh(d.widget); \ 81} while (0) 82 83/* internal types */ 84enum elevation { RAISED, LOWERED }; 85 86struct buttons { 87 unsigned int nbuttons; 88#define MAXBUTTONS 10 /* 3left + ok + extra + cancel + help + 3 right */ 89 const char *label[MAXBUTTONS]; 90 bool shortcut; 91 wchar_t first[MAXBUTTONS]; 92 int value[MAXBUTTONS]; 93 int curr; 94#define BUTTONVALUE(bs) bs.value[bs.curr] 95 unsigned int sizebutton; /* including left and right delimiters */ 96}; 97 98struct dialog { 99 bool built; /* true after the first draw_dialog() */ 100 struct bsddialog_conf *conf; /* Checked API conf */ 101 WINDOW *widget; /* Size and position refer to widget */ 102 int y, x; /* Current position, API conf.[y|x]: -1, >=0 */ 103 int rows, cols; /* API rows and cols: -1, 0, >0 */ 104 int h, w; /* Current height and width */ 105 const char *text; /* Checked API text, at least "" */ 106 WINDOW *textpad; /* Fake for textbox */ 107 struct buttons bs; /* bs.nbuttons = 0 for no buttons */ 108 WINDOW *shadow; 109}; 110 111/* error and diagnostic */ 112const char *get_error_string(void); 113void set_error_string(const char *string); 114void set_fmt_error_string(const char *fmt, ...); 115 116/* multicolumn character string */ 117unsigned int strcols(const char *mbstring); 118int str_props(const char *mbstring, unsigned int *cols, bool *has_multi_col); 119void mvwaddwch(WINDOW *w, int y, int x, wchar_t wch); 120wchar_t* alloc_mbstows(const char *mbstring); 121 122/* buttons */ 123void 124set_buttons(struct dialog *d, bool shortcut, const char *oklabel, 125 const char *canclabel); 126void draw_buttons(struct dialog *d); 127bool shortcut_buttons(wint_t key, struct buttons *bs); 128 129/* widget utils */ 130int hide_dialog(struct dialog *d); 131int f1help_dialog(struct bsddialog_conf *conf); 132 133void 134draw_borders(struct bsddialog_conf *conf, WINDOW *win, enum elevation elev); 135 136void 137update_box(struct bsddialog_conf *conf, WINDOW *win, int y, int x, int h, int w, 138 enum elevation elev); 139 140void 141rtextpad(struct dialog *d, int ytext, int xtext, int upnotext, int downnotext); 142 143/* (auto) sizing and (auto) position */ 144int 145set_widget_size(struct bsddialog_conf *conf, int rows, int cols, int *h, 146 int *w); 147 148int 149set_widget_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h, 150 int *w, const char *text, int *rowstext, struct buttons *bs, int hnotext, 151 int minw); 152 153int widget_checksize(int h, int w, struct buttons *bs, int hnotext, int minw); 154 155int 156set_widget_position(struct bsddialog_conf *conf, int *y, int *x, int h, int w); 157 158int dialog_size_position(struct dialog *d, int hnotext, int minw, int *htext); 159 160/* dialog */ 161void end_dialog(struct dialog *d); 162int draw_dialog(struct dialog *d); 163 164int 165prepare_dialog(struct bsddialog_conf *conf, const char *text, int rows, 166 int cols, struct dialog *d); 167 168#endif 169