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
28 #ifndef _LIBBSDDIALOG_UTIL_H_
29 #define _LIBBSDDIALOG_UTIL_H_
32 #define BORDERS (BORDER + BORDER)
34 #define TEXTHMARGINS (TEXTHMARGIN + TEXTHMARGIN)
37 #define CANCEL_LABEL "Cancel"
40 extern struct bsddialog_theme t;
41 extern bool hastermcolors;
43 #define MIN(a,b) (((a)<(b))?(a):(b))
44 #define MAX(a,b) (((a)>(b))?(a):(b))
46 #define BSDDIALOG_DEBUG(y,x,fmt, ...) do { \
47 mvprintw(y, x, fmt, __VA_ARGS__); \
50 /* error and diagnostic */
51 #define RETURN_ERROR(str) do { \
52 set_error_string(str); \
53 return (BSDDIALOG_ERROR); \
55 #define RETURN_FMTERROR(fmt, ...) do { \
56 set_fmt_error_string(fmt, __VA_ARGS__); \
57 return (BSDDIALOG_ERROR); \
60 #define CHECK_PTR(p) do { \
62 RETURN_ERROR("*" #p " is NULL"); \
64 #define CHECK_ARRAY(nitem, a) do { \
65 if(nitem > 0 && a == NULL) \
66 RETURN_FMTERROR(#nitem " is %d but *" #a " is NULL", nitem); \
69 #define TEXTPAD(d, downnotext) rtextpad(d, 0, 0, 0, downnotext)
70 #define SCREENLINES (getmaxy(stdscr))
71 #define SCREENCOLS (getmaxx(stdscr))
72 #define CHECK_STR(s) (s == NULL ? "" : s)
73 #define DRAW_BUTTONS(d) do { \
75 wnoutrefresh(d.widget); \
79 enum elevation { RAISED, LOWERED };
82 unsigned int nbuttons;
83 #define MAXBUTTONS 10 /* 3left + ok + extra + cancel + help + 3 right */
84 const char *label[MAXBUTTONS];
86 wchar_t first[MAXBUTTONS];
87 int value[MAXBUTTONS];
89 #define BUTTONVALUE(bs) bs.value[bs.curr]
90 unsigned int sizebutton; /* including left and right delimiters */
94 bool built; /* true after the first draw_dialog() */
95 struct bsddialog_conf *conf; /* Checked API conf */
96 WINDOW *widget; /* Size and position refer to widget */
97 int y, x; /* Current position, API conf.[y|x]: -1, >=0 */
98 int rows, cols; /* API rows and cols: -1, 0, >0 */
99 int h, w; /* Current height and width */
100 const char *text; /* Checked API text, at least "" */
101 WINDOW *textpad; /* Fake for textbox */
102 struct buttons bs; /* bs.nbuttons = 0 for no buttons */
106 /* error and diagnostic */
107 const char *get_error_string(void);
108 void set_error_string(const char *string);
109 void set_fmt_error_string(const char *fmt, ...);
111 /* multicolumn character string */
112 unsigned int strcols(const char *mbstring);
113 int str_props(const char *mbstring, unsigned int *cols, bool *has_multi_col);
114 void mvwaddwch(WINDOW *w, int y, int x, wchar_t wch);
115 wchar_t* alloc_mbstows(const char *mbstring);
119 set_buttons(struct dialog *d, bool shortcut, const char *oklabel,
120 const char *canclabel);
121 void draw_buttons(struct dialog *d);
122 bool shortcut_buttons(wint_t key, struct buttons *bs);
125 int hide_dialog(struct dialog *d);
126 int f1help_dialog(struct bsddialog_conf *conf);
129 draw_borders(struct bsddialog_conf *conf, WINDOW *win, enum elevation elev);
132 update_box(struct bsddialog_conf *conf, WINDOW *win, int y, int x, int h, int w,
133 enum elevation elev);
136 rtextpad(struct dialog *d, int ytext, int xtext, int upnotext, int downnotext);
138 /* (auto) sizing and (auto) position */
140 set_widget_size(struct bsddialog_conf *conf, int rows, int cols, int *h,
144 set_widget_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h,
145 int *w, const char *text, int *rowstext, struct buttons *bs, int hnotext,
148 int widget_checksize(int h, int w, struct buttons *bs, int hnotext, int minw);
151 set_widget_position(struct bsddialog_conf *conf, int *y, int *x, int h, int w);
153 int dialog_size_position(struct dialog *d, int hnotext, int minw, int *htext);
156 void end_dialog(struct dialog *d);
157 int draw_dialog(struct dialog *d);
160 prepare_dialog(struct bsddialog_conf *conf, const char *text, int rows,
161 int cols, struct dialog *d);