1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 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 /*
32  * Utils to implement widgets - Internal library  API - Dafult values
33  */
34 
35 #define HBORDERS	2
36 #define VBORDERS	2
37 
38 /* ncurses has not a Ctrl key macro */
39 #define KEY_CTRL(x) ((x) & 0x1f)
40 
41 /* Set default aspect ratio to 9 */
42 #define GET_ASPECT_RATIO(conf) (conf->aspect_ratio > 0 ? conf->aspect_ratio : 9)
43 
44 /* debug */
45 #define BSDDIALOG_DEBUG(y,x,fmt, ...) do {	\
46 	mvprintw(y, x, fmt, __VA_ARGS__);	\
47 	refresh();				\
48 } while (0)
49 
50 /* error buffer */
51 const char *get_error_string(void);
52 void set_error_string(char *string);
53 
54 #define RETURN_ERROR(str) do {			\
55 	set_error_string(str);			\
56 	return BSDDIALOG_ERROR;			\
57 } while (0)
58 
59 /* Buttons */
60 #define LABEL_cancel_label	"Cancel"
61 #define LABEL_exit_label	"EXIT"
62 #define LABEL_extra_label	"Extra"
63 #define LABEL_help_label	"Help"
64 #define LABEL_no_label		"No"
65 #define LABEL_ok_label		"OK"
66 #define LABEL_yes_label		"Yes"
67 #define BUTTONLABEL(l) (conf->button.l != NULL ? conf->button.l : LABEL_ ##l)
68 
69 #define MAXBUTTONS	6 /* yes|ok + extra + no|cancel + help + 2 generics */
70 struct buttons {
71 	unsigned int nbuttons;
72 	char *label[MAXBUTTONS];
73 	int value[MAXBUTTONS];
74 	int curr;
75 	unsigned int sizebutton; /* including left and right delimiters */
76 };
77 
78 void
79 get_buttons(struct bsddialog_conf *conf, struct buttons *bs, char *yesoklabel,
80     char *extralabel, char *nocancellabel, char *helplabel);
81 
82 void
83 draw_button(WINDOW *window, int y, int x, int size, char *text, bool selected,
84     bool shortkey);
85 
86 void
87 draw_buttons(WINDOW *window, int y, int cols, struct buttons bs, bool shortkey);
88 
89 /* help window with F1 key */
90 int f1help(struct bsddialog_conf *conf);
91 
92 /* cleaner */
93 int hide_widget(int y, int x, int h, int w, bool withshadow);
94 
95 /* (auto) size and (auto) position */
96 int
97 get_text_properties(struct bsddialog_conf *conf, char *text, int *maxword,
98     int *maxline, int *nlines);
99 
100 int widget_max_height(struct bsddialog_conf *conf);
101 int widget_max_width(struct bsddialog_conf *conf);
102 
103 int
104 set_widget_size(struct bsddialog_conf *conf, int rows, int cols, int *h, int *w);
105 
106 int
107 set_widget_position(struct bsddialog_conf *conf, int *y, int *x, int h, int w);
108 
109 /* widget builders */
110 int
111 print_textpad(struct bsddialog_conf *conf, WINDOW *pad, int *rows, int cols,
112     char *text);
113 
114 enum elevation { RAISED, LOWERED };
115 
116 void
117 draw_borders(struct bsddialog_conf *conf, WINDOW *win, int rows, int cols,
118     enum elevation elev);
119 
120 WINDOW *
121 new_boxed_window(struct bsddialog_conf *conf, int y, int x, int rows, int cols,
122     enum elevation elev);
123 
124 int
125 new_widget_withtextpad(struct bsddialog_conf *conf, WINDOW **shadow,
126     WINDOW **widget, int y, int x, int h, int w, enum elevation elev,
127     WINDOW **textpad, int *htextpad, char *text, bool buttons);
128 
129 int
130 update_widget_withtextpad(struct bsddialog_conf *conf, WINDOW *shadow,
131     WINDOW *widget, int h, int w, enum elevation elev, WINDOW *textpad,
132     int *htextpad, char *text, bool buttons);
133 
134 void
135 end_widget_withtextpad(struct bsddialog_conf *conf, WINDOW *window, int h, int w,
136     WINDOW *textpad, WINDOW *shadow);
137 
138 #endif
139