xref: /freebsd-src/contrib/bsddialog/lib/lib_util.h (revision c76f07938c44264c7ebd400c23f218e561960d23)
1*c76f0793SBaptiste Daroussin /*-
2*c76f0793SBaptiste Daroussin  * SPDX-License-Identifier: BSD-2-Clause
3*c76f0793SBaptiste Daroussin  *
4*c76f0793SBaptiste Daroussin  * Copyright (c) 2021 Alfonso Sabato Siciliano
5*c76f0793SBaptiste Daroussin  *
6*c76f0793SBaptiste Daroussin  * Redistribution and use in source and binary forms, with or without
7*c76f0793SBaptiste Daroussin  * modification, are permitted provided that the following conditions
8*c76f0793SBaptiste Daroussin  * are met:
9*c76f0793SBaptiste Daroussin  * 1. Redistributions of source code must retain the above copyright
10*c76f0793SBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer.
11*c76f0793SBaptiste Daroussin  * 2. Redistributions in binary form must reproduce the above copyright
12*c76f0793SBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer in the
13*c76f0793SBaptiste Daroussin  *    documentation and/or other materials provided with the distribution.
14*c76f0793SBaptiste Daroussin  *
15*c76f0793SBaptiste Daroussin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*c76f0793SBaptiste Daroussin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*c76f0793SBaptiste Daroussin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*c76f0793SBaptiste Daroussin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*c76f0793SBaptiste Daroussin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*c76f0793SBaptiste Daroussin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*c76f0793SBaptiste Daroussin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*c76f0793SBaptiste Daroussin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*c76f0793SBaptiste Daroussin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*c76f0793SBaptiste Daroussin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*c76f0793SBaptiste Daroussin  * SUCH DAMAGE.
26*c76f0793SBaptiste Daroussin  */
27*c76f0793SBaptiste Daroussin 
28*c76f0793SBaptiste Daroussin #ifndef _LIBBSDDIALOG_UTIL_H_
29*c76f0793SBaptiste Daroussin #define _LIBBSDDIALOG_UTIL_H_
30*c76f0793SBaptiste Daroussin 
31*c76f0793SBaptiste Daroussin /*
32*c76f0793SBaptiste Daroussin  * Utils to implement widgets - Internal library  API
33*c76f0793SBaptiste Daroussin  */
34*c76f0793SBaptiste Daroussin 
35*c76f0793SBaptiste Daroussin #define HBORDERS	2
36*c76f0793SBaptiste Daroussin #define VBORDERS	2
37*c76f0793SBaptiste Daroussin 
38*c76f0793SBaptiste Daroussin /* ncurses has not a Ctrl key macro */
39*c76f0793SBaptiste Daroussin #define KEY_CTRL(x) ((x) & 0x1f)
40*c76f0793SBaptiste Daroussin 
41*c76f0793SBaptiste Daroussin /* Set default aspect ratio to 9 */
42*c76f0793SBaptiste Daroussin #define GET_ASPECT_RATIO(conf) (conf.aspect_ratio > 0 ? conf.aspect_ratio : 9)
43*c76f0793SBaptiste Daroussin 
44*c76f0793SBaptiste Daroussin /* debug */
45*c76f0793SBaptiste Daroussin #define BSDDIALOG_DEBUG(y,x,fmt, ...) do {	\
46*c76f0793SBaptiste Daroussin 	mvprintw(y, x, fmt, __VA_ARGS__);	\
47*c76f0793SBaptiste Daroussin 	refresh();				\
48*c76f0793SBaptiste Daroussin } while (0)
49*c76f0793SBaptiste Daroussin 
50*c76f0793SBaptiste Daroussin /* error buffer */
51*c76f0793SBaptiste Daroussin const char *get_error_string(void);
52*c76f0793SBaptiste Daroussin void set_error_string(char *string);
53*c76f0793SBaptiste Daroussin 
54*c76f0793SBaptiste Daroussin #define RETURN_ERROR(str) do {			\
55*c76f0793SBaptiste Daroussin 	set_error_string(str);			\
56*c76f0793SBaptiste Daroussin 	return BSDDIALOG_ERROR;			\
57*c76f0793SBaptiste Daroussin } while (0)
58*c76f0793SBaptiste Daroussin 
59*c76f0793SBaptiste Daroussin /* Buttons */
60*c76f0793SBaptiste Daroussin #define LABEL_cancel_label	"Cancel"
61*c76f0793SBaptiste Daroussin #define LABEL_exit_label	"EXIT"
62*c76f0793SBaptiste Daroussin #define LABEL_extra_label	"Extra"
63*c76f0793SBaptiste Daroussin #define LABEL_help_label	"Help"
64*c76f0793SBaptiste Daroussin #define LABEL_no_label		"No"
65*c76f0793SBaptiste Daroussin #define LABEL_ok_label		"OK"
66*c76f0793SBaptiste Daroussin #define LABEL_yes_label		"Yes"
67*c76f0793SBaptiste Daroussin #define BUTTONLABEL(l) (conf.button.l != NULL ? conf.button.l : LABEL_ ##l)
68*c76f0793SBaptiste Daroussin 
69*c76f0793SBaptiste Daroussin #define MAXBUTTONS		4 /* yes|ok - extra - no|cancel - help */
70*c76f0793SBaptiste Daroussin struct buttons {
71*c76f0793SBaptiste Daroussin 	unsigned int nbuttons;
72*c76f0793SBaptiste Daroussin 	char *label[MAXBUTTONS];
73*c76f0793SBaptiste Daroussin 	int value[MAXBUTTONS];
74*c76f0793SBaptiste Daroussin 	int curr;
75*c76f0793SBaptiste Daroussin 	unsigned int sizebutton; /* including left and right delimiters */
76*c76f0793SBaptiste Daroussin };
77*c76f0793SBaptiste Daroussin 
78*c76f0793SBaptiste Daroussin void
79*c76f0793SBaptiste Daroussin get_buttons(struct bsddialog_conf conf, struct buttons *bs, char *yesoklabel,
80*c76f0793SBaptiste Daroussin     char *extralabel, char *nocancellabel, char *helplabel);
81*c76f0793SBaptiste Daroussin 
82*c76f0793SBaptiste Daroussin void
83*c76f0793SBaptiste Daroussin draw_button(WINDOW *window, int y, int x, int size, char *text, bool selected,
84*c76f0793SBaptiste Daroussin     bool shortkey);
85*c76f0793SBaptiste Daroussin 
86*c76f0793SBaptiste Daroussin void
87*c76f0793SBaptiste Daroussin draw_buttons(WINDOW *window, int y, int cols, struct buttons bs, bool shortkey);
88*c76f0793SBaptiste Daroussin 
89*c76f0793SBaptiste Daroussin /* help window with F1 key */
90*c76f0793SBaptiste Daroussin int f1help(struct bsddialog_conf conf);
91*c76f0793SBaptiste Daroussin 
92*c76f0793SBaptiste Daroussin /* cleaner */
93*c76f0793SBaptiste Daroussin int hide_widget(int y, int x, int h, int w, bool withshadow);
94*c76f0793SBaptiste Daroussin 
95*c76f0793SBaptiste Daroussin /* (auto) size and (auto) position */
96*c76f0793SBaptiste Daroussin int
97*c76f0793SBaptiste Daroussin get_text_properties(struct bsddialog_conf conf, char *text, int *maxword,
98*c76f0793SBaptiste Daroussin     int *maxline, int *nlines);
99*c76f0793SBaptiste Daroussin 
100*c76f0793SBaptiste Daroussin int widget_max_height(struct bsddialog_conf conf);
101*c76f0793SBaptiste Daroussin int widget_max_width(struct bsddialog_conf conf);
102*c76f0793SBaptiste Daroussin 
103*c76f0793SBaptiste Daroussin int
104*c76f0793SBaptiste Daroussin set_widget_size(struct bsddialog_conf conf, int rows, int cols, int *h, int *w);
105*c76f0793SBaptiste Daroussin 
106*c76f0793SBaptiste Daroussin int
107*c76f0793SBaptiste Daroussin set_widget_position(struct bsddialog_conf conf, int *y, int *x, int h, int w);
108*c76f0793SBaptiste Daroussin 
109*c76f0793SBaptiste Daroussin /* widget builders */
110*c76f0793SBaptiste Daroussin void
111*c76f0793SBaptiste Daroussin print_text(struct bsddialog_conf conf, WINDOW *pad, int starty, int minx,
112*c76f0793SBaptiste Daroussin     int maxx, char *text);
113*c76f0793SBaptiste Daroussin 
114*c76f0793SBaptiste Daroussin enum elevation { RAISED, LOWERED };
115*c76f0793SBaptiste Daroussin 
116*c76f0793SBaptiste Daroussin void
117*c76f0793SBaptiste Daroussin draw_borders(struct bsddialog_conf conf, WINDOW *win, int rows, int cols,
118*c76f0793SBaptiste Daroussin     enum elevation elev);
119*c76f0793SBaptiste Daroussin 
120*c76f0793SBaptiste Daroussin WINDOW *
121*c76f0793SBaptiste Daroussin new_boxed_window(struct bsddialog_conf conf, int y, int x, int rows, int cols,
122*c76f0793SBaptiste Daroussin     enum elevation elev);
123*c76f0793SBaptiste Daroussin 
124*c76f0793SBaptiste Daroussin int
125*c76f0793SBaptiste Daroussin new_widget_withtextpad(struct bsddialog_conf conf, WINDOW **shadow,
126*c76f0793SBaptiste Daroussin     WINDOW **widget, int y, int x, int h, int w, enum elevation elev,
127*c76f0793SBaptiste Daroussin     WINDOW **textpad, int *htextpad, char *text, bool buttons);
128*c76f0793SBaptiste Daroussin 
129*c76f0793SBaptiste Daroussin int
130*c76f0793SBaptiste Daroussin update_widget_withtextpad(struct bsddialog_conf conf, WINDOW *shadow,
131*c76f0793SBaptiste Daroussin     WINDOW *widget, int h, int w, enum elevation elev, WINDOW *textpad,
132*c76f0793SBaptiste Daroussin     int *htextpad, char *text, bool buttons);
133*c76f0793SBaptiste Daroussin 
134*c76f0793SBaptiste Daroussin void
135*c76f0793SBaptiste Daroussin end_widget_withtextpad(struct bsddialog_conf conf, WINDOW *window, int h, int w,
136*c76f0793SBaptiste Daroussin     WINDOW *textpad, WINDOW *shadow);
137*c76f0793SBaptiste Daroussin 
138*c76f0793SBaptiste Daroussin int
139*c76f0793SBaptiste Daroussin new_widget(struct bsddialog_conf conf, WINDOW **widget, int *y, int *x,
140*c76f0793SBaptiste Daroussin     char *text, int *h, int *w, WINDOW **shadow, bool buttons);
141*c76f0793SBaptiste Daroussin 
142*c76f0793SBaptiste Daroussin void
143*c76f0793SBaptiste Daroussin end_widget(struct bsddialog_conf conf, WINDOW *window, int h, int w,
144*c76f0793SBaptiste Daroussin     WINDOW *shadow);
145*c76f0793SBaptiste Daroussin 
146*c76f0793SBaptiste Daroussin #endif
147