1c76f0793SBaptiste Daroussin /*- 2c76f0793SBaptiste Daroussin * SPDX-License-Identifier: BSD-2-Clause 3c76f0793SBaptiste Daroussin * 4*61ba55bcSBaptiste Daroussin * Copyright (c) 2021-2023 Alfonso Sabato Siciliano 5c76f0793SBaptiste Daroussin * 6c76f0793SBaptiste Daroussin * Redistribution and use in source and binary forms, with or without 7c76f0793SBaptiste Daroussin * modification, are permitted provided that the following conditions 8c76f0793SBaptiste Daroussin * are met: 9c76f0793SBaptiste Daroussin * 1. Redistributions of source code must retain the above copyright 10c76f0793SBaptiste Daroussin * notice, this list of conditions and the following disclaimer. 11c76f0793SBaptiste Daroussin * 2. Redistributions in binary form must reproduce the above copyright 12c76f0793SBaptiste Daroussin * notice, this list of conditions and the following disclaimer in the 13c76f0793SBaptiste Daroussin * documentation and/or other materials provided with the distribution. 14c76f0793SBaptiste Daroussin * 15c76f0793SBaptiste Daroussin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16c76f0793SBaptiste Daroussin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17c76f0793SBaptiste Daroussin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18c76f0793SBaptiste Daroussin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19c76f0793SBaptiste Daroussin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20c76f0793SBaptiste Daroussin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21c76f0793SBaptiste Daroussin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22c76f0793SBaptiste Daroussin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23c76f0793SBaptiste Daroussin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24c76f0793SBaptiste Daroussin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25c76f0793SBaptiste Daroussin * SUCH DAMAGE. 26c76f0793SBaptiste Daroussin */ 27c76f0793SBaptiste Daroussin 28c76f0793SBaptiste Daroussin #ifndef _LIBBSDDIALOG_UTIL_H_ 29c76f0793SBaptiste Daroussin #define _LIBBSDDIALOG_UTIL_H_ 30c76f0793SBaptiste Daroussin 31*61ba55bcSBaptiste Daroussin #define BORDER 1 32*61ba55bcSBaptiste Daroussin #define BORDERS (BORDER + BORDER) 33263660c0SAlfonso Siciliano #define TEXTHMARGIN 1 34263660c0SAlfonso Siciliano #define TEXTHMARGINS (TEXTHMARGIN + TEXTHMARGIN) 35*61ba55bcSBaptiste Daroussin #define HBUTTONS 2 36*61ba55bcSBaptiste Daroussin #define OK_LABEL "OK" 37*61ba55bcSBaptiste Daroussin #define CANCEL_LABEL "Cancel" 38c76f0793SBaptiste Daroussin 39*61ba55bcSBaptiste Daroussin /* theme util */ 40bce40c02SAlfonso S. Siciliano extern struct bsddialog_theme t; 41b319d934SAlfonso S. Siciliano extern bool hastermcolors; 42bce40c02SAlfonso S. Siciliano 43*61ba55bcSBaptiste Daroussin #define MIN(a,b) (((a)<(b))?(a):(b)) 44*61ba55bcSBaptiste Daroussin #define MAX(a,b) (((a)>(b))?(a):(b)) 45c76f0793SBaptiste Daroussin /* debug */ 46c76f0793SBaptiste Daroussin #define BSDDIALOG_DEBUG(y,x,fmt, ...) do { \ 47c76f0793SBaptiste Daroussin mvprintw(y, x, fmt, __VA_ARGS__); \ 48c76f0793SBaptiste Daroussin refresh(); \ 49c76f0793SBaptiste Daroussin } while (0) 50*61ba55bcSBaptiste Daroussin /* error and diagnostic */ 51*61ba55bcSBaptiste Daroussin #define RETURN_ERROR(str) do { \ 52*61ba55bcSBaptiste Daroussin set_error_string(str); \ 53*61ba55bcSBaptiste Daroussin return (BSDDIALOG_ERROR); \ 54*61ba55bcSBaptiste Daroussin } while (0) 55*61ba55bcSBaptiste Daroussin #define RETURN_FMTERROR(fmt, ...) do { \ 56*61ba55bcSBaptiste Daroussin set_fmt_error_string(fmt, __VA_ARGS__); \ 57*61ba55bcSBaptiste Daroussin return (BSDDIALOG_ERROR); \ 58*61ba55bcSBaptiste Daroussin } while (0) 59*61ba55bcSBaptiste Daroussin /* check ptr */ 60*61ba55bcSBaptiste Daroussin #define CHECK_PTR(p) do { \ 61*61ba55bcSBaptiste Daroussin if (p == NULL) \ 62*61ba55bcSBaptiste Daroussin RETURN_ERROR("*" #p " is NULL"); \ 63*61ba55bcSBaptiste Daroussin } while (0) 64*61ba55bcSBaptiste Daroussin #define CHECK_ARRAY(nitem, a) do { \ 65*61ba55bcSBaptiste Daroussin if(nitem > 0 && a == NULL) \ 66*61ba55bcSBaptiste Daroussin RETURN_FMTERROR(#nitem " is %d but *" #a " is NULL", nitem); \ 67*61ba55bcSBaptiste Daroussin } while (0) 68*61ba55bcSBaptiste Daroussin /* widget utils */ 69*61ba55bcSBaptiste Daroussin #define TEXTPAD(d, downnotext) rtextpad(d, 0, 0, 0, downnotext) 70*61ba55bcSBaptiste Daroussin #define SCREENLINES (getmaxy(stdscr)) 71*61ba55bcSBaptiste Daroussin #define SCREENCOLS (getmaxx(stdscr)) 72*61ba55bcSBaptiste Daroussin #define CHECK_STR(s) (s == NULL ? "" : s) 73*61ba55bcSBaptiste Daroussin #define DRAW_BUTTONS(d) do { \ 74*61ba55bcSBaptiste Daroussin draw_buttons(&d); \ 75*61ba55bcSBaptiste Daroussin wnoutrefresh(d.widget); \ 76*61ba55bcSBaptiste Daroussin } while (0) 77c76f0793SBaptiste Daroussin 78*61ba55bcSBaptiste Daroussin /* internal types */ 79*61ba55bcSBaptiste Daroussin enum elevation { RAISED, LOWERED }; 8084823cc7SAlfonso S. Siciliano 81*61ba55bcSBaptiste Daroussin struct buttons { 82*61ba55bcSBaptiste Daroussin unsigned int nbuttons; 83*61ba55bcSBaptiste Daroussin #define MAXBUTTONS 10 /* 3left + ok + extra + cancel + help + 3 right */ 84*61ba55bcSBaptiste Daroussin const char *label[MAXBUTTONS]; 85*61ba55bcSBaptiste Daroussin bool shortcut; 86*61ba55bcSBaptiste Daroussin wchar_t first[MAXBUTTONS]; 87*61ba55bcSBaptiste Daroussin int value[MAXBUTTONS]; 88*61ba55bcSBaptiste Daroussin int curr; 89*61ba55bcSBaptiste Daroussin #define BUTTONVALUE(bs) bs.value[bs.curr] 90*61ba55bcSBaptiste Daroussin unsigned int sizebutton; /* including left and right delimiters */ 91*61ba55bcSBaptiste Daroussin }; 92*61ba55bcSBaptiste Daroussin 93*61ba55bcSBaptiste Daroussin struct dialog { 94*61ba55bcSBaptiste Daroussin bool built; /* true after the first draw_dialog() */ 95*61ba55bcSBaptiste Daroussin struct bsddialog_conf *conf; /* Checked API conf */ 96*61ba55bcSBaptiste Daroussin WINDOW *widget; /* Size and position refer to widget */ 97*61ba55bcSBaptiste Daroussin int y, x; /* Current position, API conf.[y|x]: -1, >=0 */ 98*61ba55bcSBaptiste Daroussin int rows, cols; /* API rows and cols: -1, 0, >0 */ 99*61ba55bcSBaptiste Daroussin int h, w; /* Current height and width */ 100*61ba55bcSBaptiste Daroussin const char *text; /* Checked API text, at least "" */ 101*61ba55bcSBaptiste Daroussin WINDOW *textpad; /* Fake for textbox */ 102*61ba55bcSBaptiste Daroussin struct buttons bs; /* bs.nbuttons = 0 for no buttons */ 103*61ba55bcSBaptiste Daroussin WINDOW *shadow; 104*61ba55bcSBaptiste Daroussin }; 105*61ba55bcSBaptiste Daroussin 106*61ba55bcSBaptiste Daroussin /* error and diagnostic */ 107*61ba55bcSBaptiste Daroussin const char *get_error_string(void); 108*61ba55bcSBaptiste Daroussin void set_error_string(const char *string); 109*61ba55bcSBaptiste Daroussin void set_fmt_error_string(const char *fmt, ...); 110*61ba55bcSBaptiste Daroussin 111*61ba55bcSBaptiste Daroussin /* multicolumn character string */ 112b319d934SAlfonso S. Siciliano unsigned int strcols(const char *mbstring); 113b319d934SAlfonso S. Siciliano int str_props(const char *mbstring, unsigned int *cols, bool *has_multi_col); 114b319d934SAlfonso S. Siciliano void mvwaddwch(WINDOW *w, int y, int x, wchar_t wch); 115b319d934SAlfonso S. Siciliano wchar_t* alloc_mbstows(const char *mbstring); 116b319d934SAlfonso S. Siciliano 117263660c0SAlfonso Siciliano /* buttons */ 118c76f0793SBaptiste Daroussin void 119*61ba55bcSBaptiste Daroussin set_buttons(struct dialog *d, bool shortcut, const char *oklabel, 120*61ba55bcSBaptiste Daroussin const char *canclabel); 121*61ba55bcSBaptiste Daroussin void draw_buttons(struct dialog *d); 122b319d934SAlfonso S. Siciliano bool shortcut_buttons(wint_t key, struct buttons *bs); 123c76f0793SBaptiste Daroussin 124*61ba55bcSBaptiste Daroussin /* widget utils */ 125*61ba55bcSBaptiste Daroussin int hide_dialog(struct dialog *d); 126*61ba55bcSBaptiste Daroussin int f1help_dialog(struct bsddialog_conf *conf); 127c76f0793SBaptiste Daroussin 128*61ba55bcSBaptiste Daroussin void 129*61ba55bcSBaptiste Daroussin draw_borders(struct bsddialog_conf *conf, WINDOW *win, enum elevation elev); 130c76f0793SBaptiste Daroussin 131*61ba55bcSBaptiste Daroussin void 132*61ba55bcSBaptiste Daroussin update_box(struct bsddialog_conf *conf, WINDOW *win, int y, int x, int h, int w, 133*61ba55bcSBaptiste Daroussin enum elevation elev); 134263660c0SAlfonso Siciliano 135*61ba55bcSBaptiste Daroussin void 136*61ba55bcSBaptiste Daroussin rtextpad(struct dialog *d, int ytext, int xtext, int upnotext, int downnotext); 137c76f0793SBaptiste Daroussin 138*61ba55bcSBaptiste Daroussin /* (auto) sizing and (auto) position */ 139263660c0SAlfonso Siciliano int 140263660c0SAlfonso Siciliano set_widget_size(struct bsddialog_conf *conf, int rows, int cols, int *h, 141263660c0SAlfonso Siciliano int *w); 142c76f0793SBaptiste Daroussin 143c76f0793SBaptiste Daroussin int 144*61ba55bcSBaptiste Daroussin set_widget_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h, 145*61ba55bcSBaptiste Daroussin int *w, const char *text, int *rowstext, struct buttons *bs, int hnotext, 146*61ba55bcSBaptiste Daroussin int minw); 147*61ba55bcSBaptiste Daroussin 148*61ba55bcSBaptiste Daroussin int widget_checksize(int h, int w, struct buttons *bs, int hnotext, int minw); 149*61ba55bcSBaptiste Daroussin 150*61ba55bcSBaptiste Daroussin int 151f499134dSBaptiste Daroussin set_widget_position(struct bsddialog_conf *conf, int *y, int *x, int h, int w); 152c76f0793SBaptiste Daroussin 153*61ba55bcSBaptiste Daroussin int dialog_size_position(struct dialog *d, int hnotext, int minw, int *htext); 154c76f0793SBaptiste Daroussin 155*61ba55bcSBaptiste Daroussin /* dialog */ 156*61ba55bcSBaptiste Daroussin void end_dialog(struct dialog *d); 157*61ba55bcSBaptiste Daroussin int draw_dialog(struct dialog *d); 158c76f0793SBaptiste Daroussin 159c76f0793SBaptiste Daroussin int 160*61ba55bcSBaptiste Daroussin prepare_dialog(struct bsddialog_conf *conf, const char *text, int rows, 161*61ba55bcSBaptiste Daroussin int cols, struct dialog *d); 162c76f0793SBaptiste Daroussin 163c76f0793SBaptiste Daroussin #endif 164