xref: /freebsd/contrib/bsddialog/lib/lib_util.h (revision 61ba55bc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021-2023 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 */
40 extern struct bsddialog_theme t;
41 extern 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 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 {                                                   \
74 	draw_buttons(&d);                                                      \
75 	wnoutrefresh(d.widget);                                                \
76 } while (0)
77 
78 /* internal types */
79 enum elevation { RAISED, LOWERED };
80 
81 struct buttons {
82 	unsigned int nbuttons;
83 #define MAXBUTTONS 10 /* 3left + ok + extra + cancel + help + 3 right */
84 	const char *label[MAXBUTTONS];
85 	bool shortcut;
86 	wchar_t first[MAXBUTTONS];
87 	int value[MAXBUTTONS];
88 	int curr;
89 #define BUTTONVALUE(bs) bs.value[bs.curr]
90 	unsigned int sizebutton; /* including left and right delimiters */
91 };
92 
93 struct dialog {
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 */
103 	WINDOW *shadow;
104 };
105 
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, ...);
110 
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);
116 
117 /* buttons */
118 void
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);
123 
124 /* widget utils */
125 int hide_dialog(struct dialog *d);
126 int f1help_dialog(struct bsddialog_conf *conf);
127 
128 void
129 draw_borders(struct bsddialog_conf *conf, WINDOW *win, enum elevation elev);
130 
131 void
132 update_box(struct bsddialog_conf *conf, WINDOW *win, int y, int x, int h, int w,
133     enum elevation elev);
134 
135 void
136 rtextpad(struct dialog *d, int ytext, int xtext, int upnotext, int downnotext);
137 
138 /* (auto) sizing and (auto) position */
139 int
140 set_widget_size(struct bsddialog_conf *conf, int rows, int cols, int *h,
141     int *w);
142 
143 int
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,
146     int minw);
147 
148 int widget_checksize(int h, int w, struct buttons *bs, int hnotext, int minw);
149 
150 int
151 set_widget_position(struct bsddialog_conf *conf, int *y, int *x, int h, int w);
152 
153 int dialog_size_position(struct dialog *d, int hnotext, int minw, int *htext);
154 
155 /* dialog */
156 void end_dialog(struct dialog *d);
157 int draw_dialog(struct dialog *d);
158 
159 int
160 prepare_dialog(struct bsddialog_conf *conf, const char *text, int rows,
161     int cols, struct dialog *d);
162 
163 #endif
164