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