xref: /freebsd/contrib/bsddialog/lib/lib_util.h (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021-2022 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 HBORDERS	2
32 #define VBORDERS	2
33 #define TEXTHMARGIN     1
34 #define TEXTHMARGINS    (TEXTHMARGIN + TEXTHMARGIN)
35 
36 /* theme utils */
37 extern struct bsddialog_theme t;
38 extern bool hastermcolors;
39 
40 /* debug */
41 #define BSDDIALOG_DEBUG(y,x,fmt, ...) do {	\
42 	mvprintw(y, x, fmt, __VA_ARGS__);	\
43 	refresh();				\
44 } while (0)
45 
46 /* date */
47 #define ISLEAP(year) ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
48 
49 /* unicode */
50 unsigned int strcols(const char *mbstring);
51 int str_props(const char *mbstring, unsigned int *cols, bool *has_multi_col);
52 void mvwaddwch(WINDOW *w, int y, int x, wchar_t wch);
53 wchar_t* alloc_mbstows(const char *mbstring);
54 
55 /* error buffer */
56 const char *get_error_string(void);
57 void set_error_string(const char *string);
58 
59 #define RETURN_ERROR(str) do {			\
60 	set_error_string(str);			\
61 	return (BSDDIALOG_ERROR);		\
62 } while (0)
63 
64 /* buttons */
65 struct buttons {
66 	unsigned int nbuttons;
67 #define MAXBUTTONS 6 /* ok + extra + cancel + help + 2 generics */
68 	const char *label[MAXBUTTONS];
69 	wchar_t first[MAXBUTTONS];
70 	int value[MAXBUTTONS];
71 	int curr;
72 	unsigned int sizebutton; /* including left and right delimiters */
73 };
74 
75 #define BUTTON_OK_LABEL      "OK"
76 #define BUTTON_CANCEL_LABEL  "Cancel"
77 void
78 get_buttons(struct bsddialog_conf *conf, struct buttons *bs,
79     const char *yesoklabel, const char *nocancellabel);
80 
81 void
82 draw_buttons(WINDOW *window, struct buttons bs, bool shortcut);
83 
84 int buttons_min_width(struct buttons bs);
85 bool shortcut_buttons(wint_t key, struct buttons *bs);
86 
87 /* help window with F1 key */
88 int f1help(struct bsddialog_conf *conf);
89 
90 /* cleaner */
91 int hide_widget(int y, int x, int h, int w, bool withshadow);
92 
93 /* (auto) size and (auto) position */
94 #define SCREENLINES (getmaxy(stdscr))
95 #define SCREENCOLS  (getmaxx(stdscr))
96 
97 int
98 text_size(struct bsddialog_conf *conf, int rows, int cols, const char *text,
99     struct buttons *bs, int rowsnotext, int startwtext, int *htext, int *wtext);
100 
101 int widget_max_height(struct bsddialog_conf *conf);
102 int widget_max_width(struct bsddialog_conf *conf);
103 
104 int
105 widget_min_height(struct bsddialog_conf *conf, int htext, int minwidget,
106     bool withbuttons);
107 
108 int
109 widget_min_width(struct bsddialog_conf *conf, int wtext, int minwidget,
110     struct buttons *bs);
111 
112 int
113 set_widget_size(struct bsddialog_conf *conf, int rows, int cols, int *h,
114     int *w);
115 
116 int
117 set_widget_position(struct bsddialog_conf *conf, int *y, int *x, int h, int w);
118 
119 /* widget builders */
120 enum elevation { RAISED, LOWERED };
121 
122 void
123 draw_borders(struct bsddialog_conf *conf, WINDOW *win, int rows, int cols,
124     enum elevation elev);
125 
126 WINDOW *
127 new_boxed_window(struct bsddialog_conf *conf, int y, int x, int rows, int cols,
128     enum elevation elev);
129 
130 int
131 new_dialog(struct bsddialog_conf *conf, WINDOW **shadow, WINDOW **widget, int y,
132     int x, int h, int w, WINDOW **textpad, const char *text, struct buttons *bs,
133     bool shortcutbuttons);
134 
135 int
136 update_dialog(struct bsddialog_conf *conf, WINDOW *shadow, WINDOW *widget,
137     int y, int x, int h, int w, WINDOW *textpad, const char *text,
138     struct buttons *bs, bool shortcutbuttons);
139 
140 void
141 end_dialog(struct bsddialog_conf *conf, WINDOW *shadow, WINDOW *widget,
142     WINDOW *textpad);
143 
144 #endif
145