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