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