2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021-2022 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_
34 #define TEXTHMARGINS (TEXTHMARGIN + TEXTHMARGIN)
37 extern struct bsddialog_theme t;
38 extern bool hastermcolors;
41 #define BSDDIALOG_DEBUG(y,x,fmt, ...) do { \
42 mvprintw(y, x, fmt, __VA_ARGS__); \
47 #define ISLEAP(year) ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
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);
56 const char *get_error_string(void);
57 void set_error_string(const char *string);
59 #define RETURN_ERROR(str) do { \
60 set_error_string(str); \
61 return (BSDDIALOG_ERROR); \
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];
72 unsigned int sizebutton; /* including left and right delimiters */
75 #define BUTTON_OK_LABEL "OK"
76 #define BUTTON_CANCEL_LABEL "Cancel"
78 get_buttons(struct bsddialog_conf *conf, struct buttons *bs,
79 const char *yesoklabel, const char *nocancellabel);
82 draw_buttons(WINDOW *window, struct buttons bs, bool shortcut);
84 int buttons_min_width(struct buttons bs);
85 bool shortcut_buttons(wint_t key, struct buttons *bs);
87 /* help window with F1 key */
88 int f1help(struct bsddialog_conf *conf);
91 int hide_widget(int y, int x, int h, int w, bool withshadow);
93 /* (auto) size and (auto) position */
94 #define SCREENLINES (getmaxy(stdscr))
95 #define SCREENCOLS (getmaxx(stdscr))
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);
101 int widget_max_height(struct bsddialog_conf *conf);
102 int widget_max_width(struct bsddialog_conf *conf);
105 widget_min_height(struct bsddialog_conf *conf, int htext, int minwidget,
109 widget_min_width(struct bsddialog_conf *conf, int wtext, int minwidget,
113 set_widget_size(struct bsddialog_conf *conf, int rows, int cols, int *h,
117 set_widget_position(struct bsddialog_conf *conf, int *y, int *x, int h, int w);
119 /* widget builders */
120 enum elevation { RAISED, LOWERED };
123 draw_borders(struct bsddialog_conf *conf, WINDOW *win, int rows, int cols,
124 enum elevation elev);
127 new_boxed_window(struct bsddialog_conf *conf, int y, int x, int rows, int cols,
128 enum elevation elev);
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);
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);
141 end_dialog(struct bsddialog_conf *conf, WINDOW *shadow, WINDOW *widget,