diff options
Diffstat (limited to 'contrib/bsddialog/lib/lib_util.h')
-rw-r--r-- | contrib/bsddialog/lib/lib_util.h | 185 |
1 files changed, 101 insertions, 84 deletions
diff --git a/contrib/bsddialog/lib/lib_util.h b/contrib/bsddialog/lib/lib_util.h index b6fe1dd86b59..1a502147c441 100644 --- a/contrib/bsddialog/lib/lib_util.h +++ b/contrib/bsddialog/lib/lib_util.h @@ -1,7 +1,7 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2021 Alfonso Sabato Siciliano + * Copyright (c) 2021-2023 Alfonso Sabato Siciliano * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -28,119 +28,136 @@ #ifndef _LIBBSDDIALOG_UTIL_H_ #define _LIBBSDDIALOG_UTIL_H_ -/* - * Utils to implement widgets - Internal library API - */ - -#define HBORDERS 2 -#define VBORDERS 2 - -/* ncurses has not a Ctrl key macro */ -#define KEY_CTRL(x) ((x) & 0x1f) +#define BORDER 1 +#define BORDERS (BORDER + BORDER) +#define TEXTHMARGIN 1 +#define TEXTHMARGINS (TEXTHMARGIN + TEXTHMARGIN) +#define HBUTTONS 2 +#define OK_LABEL "OK" +#define CANCEL_LABEL "Cancel" -/* Set default aspect ratio to 9 */ -#define GET_ASPECT_RATIO(conf) (conf.aspect_ratio > 0 ? conf.aspect_ratio : 9) +/* theme util */ +extern struct bsddialog_theme t; +extern bool hastermcolors; +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define MAX(a,b) (((a)>(b))?(a):(b)) /* debug */ -#define BSDDIALOG_DEBUG(y,x,fmt, ...) do { \ - mvprintw(y, x, fmt, __VA_ARGS__); \ - refresh(); \ +#define BSDDIALOG_DEBUG(y,x,fmt, ...) do { \ + mvprintw(y, x, fmt, __VA_ARGS__); \ + refresh(); \ } while (0) - -/* error buffer */ -const char *get_error_string(void); -void set_error_string(char *string); - -#define RETURN_ERROR(str) do { \ - set_error_string(str); \ - return BSDDIALOG_ERROR; \ +/* error and diagnostic */ +#define RETURN_ERROR(str) do { \ + set_error_string(str); \ + return (BSDDIALOG_ERROR); \ +} while (0) +#define RETURN_FMTERROR(fmt, ...) do { \ + set_fmt_error_string(fmt, __VA_ARGS__); \ + return (BSDDIALOG_ERROR); \ +} while (0) +/* check ptr */ +#define CHECK_PTR(p) do { \ + if (p == NULL) \ + RETURN_ERROR("*" #p " is NULL"); \ +} while (0) +#define CHECK_ARRAY(nitem, a) do { \ + if(nitem > 0 && a == NULL) \ + RETURN_FMTERROR(#nitem " is %d but *" #a " is NULL", nitem); \ } while (0) +/* widget utils */ +#define TEXTPAD(d, downnotext) rtextpad(d, 0, 0, 0, downnotext) +#define SCREENLINES (getmaxy(stdscr)) +#define SCREENCOLS (getmaxx(stdscr)) +#define CHECK_STR(s) (s == NULL ? "" : s) +#define DRAW_BUTTONS(d) do { \ + draw_buttons(&d); \ + wnoutrefresh(d.widget); \ +} while (0) + +/* internal types */ +enum elevation { RAISED, LOWERED }; -/* Buttons */ -#define LABEL_cancel_label "Cancel" -#define LABEL_exit_label "EXIT" -#define LABEL_extra_label "Extra" -#define LABEL_help_label "Help" -#define LABEL_no_label "No" -#define LABEL_ok_label "OK" -#define LABEL_yes_label "Yes" -#define BUTTONLABEL(l) (conf.button.l != NULL ? conf.button.l : LABEL_ ##l) - -#define MAXBUTTONS 4 /* yes|ok - extra - no|cancel - help */ struct buttons { unsigned int nbuttons; - char *label[MAXBUTTONS]; +#define MAXBUTTONS 10 /* 3left + ok + extra + cancel + help + 3 right */ + const char *label[MAXBUTTONS]; + bool shortcut; + wchar_t first[MAXBUTTONS]; int value[MAXBUTTONS]; int curr; +#define BUTTONVALUE(bs) bs.value[bs.curr] unsigned int sizebutton; /* including left and right delimiters */ }; -void -get_buttons(struct bsddialog_conf conf, struct buttons *bs, char *yesoklabel, - char *extralabel, char *nocancellabel, char *helplabel); - -void -draw_button(WINDOW *window, int y, int x, int size, char *text, bool selected, - bool shortkey); - -void -draw_buttons(WINDOW *window, int y, int cols, struct buttons bs, bool shortkey); - -/* help window with F1 key */ -int f1help(struct bsddialog_conf conf); - -/* cleaner */ -int hide_widget(int y, int x, int h, int w, bool withshadow); +struct dialog { + bool built; /* true after the first draw_dialog() */ + struct bsddialog_conf *conf; /* Checked API conf */ + WINDOW *widget; /* Size and position refer to widget */ + int y, x; /* Current position, API conf.[y|x]: -1, >=0 */ + int rows, cols; /* API rows and cols: -1, 0, >0 */ + int h, w; /* Current height and width */ + const char *text; /* Checked API text, at least "" */ + WINDOW *textpad; /* Fake for textbox */ + struct buttons bs; /* bs.nbuttons = 0 for no buttons */ + WINDOW *shadow; +}; -/* (auto) size and (auto) position */ -int -get_text_properties(struct bsddialog_conf conf, char *text, int *maxword, - int *maxline, int *nlines); +/* error and diagnostic */ +const char *get_error_string(void); +void set_error_string(const char *string); +void set_fmt_error_string(const char *fmt, ...); -int widget_max_height(struct bsddialog_conf conf); -int widget_max_width(struct bsddialog_conf conf); +/* multicolumn character string */ +unsigned int strcols(const char *mbstring); +int str_props(const char *mbstring, unsigned int *cols, bool *has_multi_col); +void mvwaddwch(WINDOW *w, int y, int x, wchar_t wch); +wchar_t* alloc_mbstows(const char *mbstring); -int -set_widget_size(struct bsddialog_conf conf, int rows, int cols, int *h, int *w); +/* buttons */ +void +set_buttons(struct dialog *d, bool shortcut, const char *oklabel, + const char *canclabel); +void draw_buttons(struct dialog *d); +bool shortcut_buttons(wint_t key, struct buttons *bs); -int -set_widget_position(struct bsddialog_conf conf, int *y, int *x, int h, int w); +/* widget utils */ +int hide_dialog(struct dialog *d); +int f1help_dialog(struct bsddialog_conf *conf); -/* widget builders */ void -print_text(struct bsddialog_conf conf, WINDOW *pad, int starty, int minx, - int maxx, char *text); - -enum elevation { RAISED, LOWERED }; +draw_borders(struct bsddialog_conf *conf, WINDOW *win, enum elevation elev); void -draw_borders(struct bsddialog_conf conf, WINDOW *win, int rows, int cols, +update_box(struct bsddialog_conf *conf, WINDOW *win, int y, int x, int h, int w, enum elevation elev); -WINDOW * -new_boxed_window(struct bsddialog_conf conf, int y, int x, int rows, int cols, - enum elevation elev); +void +rtextpad(struct dialog *d, int ytext, int xtext, int upnotext, int downnotext); +/* (auto) sizing and (auto) position */ int -new_widget_withtextpad(struct bsddialog_conf conf, WINDOW **shadow, - WINDOW **widget, int y, int x, int h, int w, enum elevation elev, - WINDOW **textpad, int *htextpad, char *text, bool buttons); +set_widget_size(struct bsddialog_conf *conf, int rows, int cols, int *h, + int *w); int -update_widget_withtextpad(struct bsddialog_conf conf, WINDOW *shadow, - WINDOW *widget, int h, int w, enum elevation elev, WINDOW *textpad, - int *htextpad, char *text, bool buttons); +set_widget_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h, + int *w, const char *text, int *rowstext, struct buttons *bs, int hnotext, + int minw); -void -end_widget_withtextpad(struct bsddialog_conf conf, WINDOW *window, int h, int w, - WINDOW *textpad, WINDOW *shadow); +int widget_checksize(int h, int w, struct buttons *bs, int hnotext, int minw); int -new_widget(struct bsddialog_conf conf, WINDOW **widget, int *y, int *x, - char *text, int *h, int *w, WINDOW **shadow, bool buttons); +set_widget_position(struct bsddialog_conf *conf, int *y, int *x, int h, int w); -void -end_widget(struct bsddialog_conf conf, WINDOW *window, int h, int w, - WINDOW *shadow); +int dialog_size_position(struct dialog *d, int hnotext, int minw, int *htext); + +/* dialog */ +void end_dialog(struct dialog *d); +int draw_dialog(struct dialog *d); + +int +prepare_dialog(struct bsddialog_conf *conf, const char *text, int rows, + int cols, struct dialog *d); #endif |