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