1c76f0793SBaptiste Daroussin /*- 2c76f0793SBaptiste Daroussin * SPDX-License-Identifier: BSD-2-Clause 3c76f0793SBaptiste Daroussin * 4*a6d8be45SAlfonso S. Siciliano * Copyright (c) 2021-2024 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 3161ba55bcSBaptiste Daroussin #define BORDER 1 3261ba55bcSBaptiste Daroussin #define BORDERS (BORDER + BORDER) 33263660c0SAlfonso Siciliano #define TEXTHMARGIN 1 34263660c0SAlfonso Siciliano #define TEXTHMARGINS (TEXTHMARGIN + TEXTHMARGIN) 3561ba55bcSBaptiste Daroussin #define HBUTTONS 2 3661ba55bcSBaptiste Daroussin #define OK_LABEL "OK" 3761ba55bcSBaptiste Daroussin #define CANCEL_LABEL "Cancel" 38c76f0793SBaptiste Daroussin 3961ba55bcSBaptiste Daroussin /* theme util */ 40bce40c02SAlfonso S. Siciliano extern struct bsddialog_theme t; 41b319d934SAlfonso S. Siciliano extern bool hastermcolors; 42bce40c02SAlfonso S. Siciliano 4361ba55bcSBaptiste Daroussin #define MIN(a,b) (((a)<(b))?(a):(b)) 4461ba55bcSBaptiste 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) 5061ba55bcSBaptiste Daroussin /* error and diagnostic */ 5161ba55bcSBaptiste Daroussin #define RETURN_ERROR(str) do { \ 5261ba55bcSBaptiste Daroussin set_error_string(str); \ 5361ba55bcSBaptiste Daroussin return (BSDDIALOG_ERROR); \ 5461ba55bcSBaptiste Daroussin } while (0) 5561ba55bcSBaptiste Daroussin #define RETURN_FMTERROR(fmt, ...) do { \ 5661ba55bcSBaptiste Daroussin set_fmt_error_string(fmt, __VA_ARGS__); \ 5761ba55bcSBaptiste Daroussin return (BSDDIALOG_ERROR); \ 5861ba55bcSBaptiste Daroussin } while (0) 5961ba55bcSBaptiste Daroussin /* check ptr */ 6061ba55bcSBaptiste Daroussin #define CHECK_PTR(p) do { \ 6161ba55bcSBaptiste Daroussin if (p == NULL) \ 6261ba55bcSBaptiste Daroussin RETURN_ERROR("*" #p " is NULL"); \ 6361ba55bcSBaptiste Daroussin } while (0) 6461ba55bcSBaptiste Daroussin #define CHECK_ARRAY(nitem, a) do { \ 6561ba55bcSBaptiste Daroussin if (nitem > 0 && a == NULL) \ 6661ba55bcSBaptiste Daroussin RETURN_FMTERROR(#nitem " is %d but *" #a " is NULL", nitem); \ 6761ba55bcSBaptiste Daroussin } while (0) 6861ba55bcSBaptiste Daroussin /* widget utils */ 69*a6d8be45SAlfonso S. Siciliano #define KEY_CTRL(c) (c & 037) 7061ba55bcSBaptiste Daroussin #define TEXTPAD(d, downnotext) rtextpad(d, 0, 0, 0, downnotext) 7161ba55bcSBaptiste Daroussin #define SCREENLINES (getmaxy(stdscr)) 7261ba55bcSBaptiste Daroussin #define SCREENCOLS (getmaxx(stdscr)) 7361ba55bcSBaptiste Daroussin #define CHECK_STR(s) (s == NULL ? "" : s) 74*a6d8be45SAlfonso S. Siciliano #define UARROW(c) (c->ascii_lines ? '^' : ACS_UARROW) 75*a6d8be45SAlfonso S. Siciliano #define DARROW(c) (c->ascii_lines ? 'v' : ACS_DARROW) 76*a6d8be45SAlfonso S. Siciliano #define LARROW(c) (c->ascii_lines ? '<' : ACS_LARROW) 77*a6d8be45SAlfonso S. Siciliano #define RARROW(c) (c->ascii_lines ? '>' : ACS_RARROW) 7861ba55bcSBaptiste Daroussin #define DRAW_BUTTONS(d) do { \ 7961ba55bcSBaptiste Daroussin draw_buttons(&d); \ 8061ba55bcSBaptiste Daroussin wnoutrefresh(d.widget); \ 8161ba55bcSBaptiste Daroussin } while (0) 82c76f0793SBaptiste Daroussin 8361ba55bcSBaptiste Daroussin /* internal types */ 8461ba55bcSBaptiste Daroussin enum elevation { RAISED, LOWERED }; 8584823cc7SAlfonso S. Siciliano 8661ba55bcSBaptiste Daroussin struct buttons { 8761ba55bcSBaptiste Daroussin unsigned int nbuttons; 8861ba55bcSBaptiste Daroussin #define MAXBUTTONS 10 /* 3left + ok + extra + cancel + help + 3 right */ 8961ba55bcSBaptiste Daroussin const char *label[MAXBUTTONS]; 9061ba55bcSBaptiste Daroussin bool shortcut; 9161ba55bcSBaptiste Daroussin wchar_t first[MAXBUTTONS]; 9261ba55bcSBaptiste Daroussin int value[MAXBUTTONS]; 9361ba55bcSBaptiste Daroussin int curr; 9461ba55bcSBaptiste Daroussin #define BUTTONVALUE(bs) bs.value[bs.curr] 9561ba55bcSBaptiste Daroussin unsigned int sizebutton; /* including left and right delimiters */ 9661ba55bcSBaptiste Daroussin }; 9761ba55bcSBaptiste Daroussin 9861ba55bcSBaptiste Daroussin struct dialog { 9961ba55bcSBaptiste Daroussin bool built; /* true after the first draw_dialog() */ 10061ba55bcSBaptiste Daroussin struct bsddialog_conf *conf; /* Checked API conf */ 10161ba55bcSBaptiste Daroussin WINDOW *widget; /* Size and position refer to widget */ 10261ba55bcSBaptiste Daroussin int y, x; /* Current position, API conf.[y|x]: -1, >=0 */ 10361ba55bcSBaptiste Daroussin int rows, cols; /* API rows and cols: -1, 0, >0 */ 10461ba55bcSBaptiste Daroussin int h, w; /* Current height and width */ 10561ba55bcSBaptiste Daroussin const char *text; /* Checked API text, at least "" */ 10661ba55bcSBaptiste Daroussin WINDOW *textpad; /* Fake for textbox */ 10761ba55bcSBaptiste Daroussin struct buttons bs; /* bs.nbuttons = 0 for no buttons */ 10861ba55bcSBaptiste Daroussin WINDOW *shadow; 10961ba55bcSBaptiste Daroussin }; 11061ba55bcSBaptiste Daroussin 11161ba55bcSBaptiste Daroussin /* error and diagnostic */ 11261ba55bcSBaptiste Daroussin const char *get_error_string(void); 11361ba55bcSBaptiste Daroussin void set_error_string(const char *string); 11461ba55bcSBaptiste Daroussin void set_fmt_error_string(const char *fmt, ...); 11561ba55bcSBaptiste Daroussin 11661ba55bcSBaptiste Daroussin /* multicolumn character string */ 117b319d934SAlfonso S. Siciliano unsigned int strcols(const char *mbstring); 118b319d934SAlfonso S. Siciliano int str_props(const char *mbstring, unsigned int *cols, bool *has_multi_col); 119b319d934SAlfonso S. Siciliano void mvwaddwch(WINDOW *w, int y, int x, wchar_t wch); 120b319d934SAlfonso S. Siciliano wchar_t* alloc_mbstows(const char *mbstring); 121b319d934SAlfonso S. Siciliano 122263660c0SAlfonso Siciliano /* buttons */ 123c76f0793SBaptiste Daroussin void 12461ba55bcSBaptiste Daroussin set_buttons(struct dialog *d, bool shortcut, const char *oklabel, 12561ba55bcSBaptiste Daroussin const char *canclabel); 12661ba55bcSBaptiste Daroussin void draw_buttons(struct dialog *d); 127b319d934SAlfonso S. Siciliano bool shortcut_buttons(wint_t key, struct buttons *bs); 128c76f0793SBaptiste Daroussin 12961ba55bcSBaptiste Daroussin /* widget utils */ 13061ba55bcSBaptiste Daroussin int hide_dialog(struct dialog *d); 13161ba55bcSBaptiste Daroussin int f1help_dialog(struct bsddialog_conf *conf); 132c76f0793SBaptiste Daroussin 13361ba55bcSBaptiste Daroussin void 13461ba55bcSBaptiste Daroussin draw_borders(struct bsddialog_conf *conf, WINDOW *win, enum elevation elev); 135c76f0793SBaptiste Daroussin 13661ba55bcSBaptiste Daroussin void 13761ba55bcSBaptiste Daroussin update_box(struct bsddialog_conf *conf, WINDOW *win, int y, int x, int h, int w, 13861ba55bcSBaptiste Daroussin enum elevation elev); 139263660c0SAlfonso Siciliano 14061ba55bcSBaptiste Daroussin void 14161ba55bcSBaptiste Daroussin rtextpad(struct dialog *d, int ytext, int xtext, int upnotext, int downnotext); 142c76f0793SBaptiste Daroussin 14361ba55bcSBaptiste Daroussin /* (auto) sizing and (auto) position */ 144263660c0SAlfonso Siciliano int 145263660c0SAlfonso Siciliano set_widget_size(struct bsddialog_conf *conf, int rows, int cols, int *h, 146263660c0SAlfonso Siciliano int *w); 147c76f0793SBaptiste Daroussin 148c76f0793SBaptiste Daroussin int 14961ba55bcSBaptiste Daroussin set_widget_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h, 15061ba55bcSBaptiste Daroussin int *w, const char *text, int *rowstext, struct buttons *bs, int hnotext, 15161ba55bcSBaptiste Daroussin int minw); 15261ba55bcSBaptiste Daroussin 15361ba55bcSBaptiste Daroussin int widget_checksize(int h, int w, struct buttons *bs, int hnotext, int minw); 15461ba55bcSBaptiste Daroussin 15561ba55bcSBaptiste Daroussin int 156f499134dSBaptiste Daroussin set_widget_position(struct bsddialog_conf *conf, int *y, int *x, int h, int w); 157c76f0793SBaptiste Daroussin 15861ba55bcSBaptiste Daroussin int dialog_size_position(struct dialog *d, int hnotext, int minw, int *htext); 159c76f0793SBaptiste Daroussin 16061ba55bcSBaptiste Daroussin /* dialog */ 16161ba55bcSBaptiste Daroussin void end_dialog(struct dialog *d); 16261ba55bcSBaptiste Daroussin int draw_dialog(struct dialog *d); 163c76f0793SBaptiste Daroussin 164c76f0793SBaptiste Daroussin int 16561ba55bcSBaptiste Daroussin prepare_dialog(struct bsddialog_conf *conf, const char *text, int rows, 16661ba55bcSBaptiste Daroussin int cols, struct dialog *d); 167c76f0793SBaptiste Daroussin 168c76f0793SBaptiste Daroussin #endif 169