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 /* unicode */ 47 unsigned int strcols(const char *mbstring); 48 int str_props(const char *mbstring, unsigned int *cols, bool *has_multi_col); 49 void mvwaddwch(WINDOW *w, int y, int x, wchar_t wch); 50 wchar_t* alloc_mbstows(const char *mbstring); 51 52 /* error buffer */ 53 const char *get_error_string(void); 54 void set_error_string(const char *string); 55 56 #define RETURN_ERROR(str) do { \ 57 set_error_string(str); \ 58 return (BSDDIALOG_ERROR); \ 59 } while (0) 60 61 /* buttons */ 62 struct buttons { 63 unsigned int nbuttons; 64 #define MAXBUTTONS 6 /* ok + extra + cancel + help + 2 generics */ 65 const char *label[MAXBUTTONS]; 66 wchar_t first[MAXBUTTONS]; 67 int value[MAXBUTTONS]; 68 int curr; 69 unsigned int sizebutton; /* including left and right delimiters */ 70 }; 71 72 #define BUTTON_OK_LABEL "OK" 73 #define BUTTON_CANCEL_LABEL "Cancel" 74 void 75 get_buttons(struct bsddialog_conf *conf, struct buttons *bs, 76 const char *yesoklabel, const char *nocancellabel); 77 78 void 79 draw_buttons(WINDOW *window, struct buttons bs, bool shortcut); 80 81 int buttons_min_width(struct buttons bs); 82 bool shortcut_buttons(wint_t key, struct buttons *bs); 83 84 /* help window with F1 key */ 85 int f1help(struct bsddialog_conf *conf); 86 87 /* cleaner */ 88 int hide_widget(int y, int x, int h, int w, bool withshadow); 89 90 /* (auto) size and (auto) position */ 91 #define SCREENLINES (getmaxy(stdscr)) 92 #define SCREENCOLS (getmaxx(stdscr)) 93 94 int 95 text_size(struct bsddialog_conf *conf, int rows, int cols, const char *text, 96 struct buttons *bs, int rowsnotext, int startwtext, int *htext, int *wtext); 97 98 int widget_max_height(struct bsddialog_conf *conf); 99 int widget_max_width(struct bsddialog_conf *conf); 100 101 int 102 widget_min_height(struct bsddialog_conf *conf, int htext, int minwidget, 103 bool withbuttons); 104 105 int 106 widget_min_width(struct bsddialog_conf *conf, int wtext, int minwidget, 107 struct buttons *bs); 108 109 int 110 set_widget_size(struct bsddialog_conf *conf, int rows, int cols, int *h, 111 int *w); 112 113 int 114 set_widget_position(struct bsddialog_conf *conf, int *y, int *x, int h, int w); 115 116 /* widget builders */ 117 enum elevation { RAISED, LOWERED }; 118 119 void 120 draw_borders(struct bsddialog_conf *conf, WINDOW *win, int rows, int cols, 121 enum elevation elev); 122 123 WINDOW * 124 new_boxed_window(struct bsddialog_conf *conf, int y, int x, int rows, int cols, 125 enum elevation elev); 126 127 int 128 new_dialog(struct bsddialog_conf *conf, WINDOW **shadow, WINDOW **widget, int y, 129 int x, int h, int w, WINDOW **textpad, const char *text, struct buttons *bs, 130 bool shortcutbuttons); 131 132 int 133 update_dialog(struct bsddialog_conf *conf, WINDOW *shadow, WINDOW *widget, 134 int y, int x, int h, int w, WINDOW *textpad, const char *text, 135 struct buttons *bs, bool shortcutbuttons); 136 137 void 138 end_dialog(struct bsddialog_conf *conf, WINDOW *shadow, WINDOW *widget, 139 WINDOW *textpad); 140 141 #endif 142