From a75ec7479d917255db38bbaed2644cb53a17cf50 Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Sat, 16 Jul 2011 15:47:05 +1000 Subject: [PATCH] Add center3(): centre three strings --- src/intf.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/intf.h | 5 ++++ 2 files changed, 72 insertions(+) diff --git a/src/intf.c b/src/intf.c index 5fb94f4..7eae002 100644 --- a/src/intf.c +++ b/src/intf.c @@ -404,6 +404,73 @@ int center2 (WINDOW *win, int y, int attr_initial, int attr_string, } +/*----------------------------------------------------------------------- + Function: center3 - Centre three strings on the current line + Arguments: win - Window to use + y - Line on which to centre the string + attr_initial - Window attribute to use for initial string + attr_final - Window attribute to use for final string + attr_string - Window attribute to use for main string + initial - Fixed initial string + final - Fixed final string + format - printf()-like format string + ... - printf()-like arguments + Returns: int - Return code from wprintw() + + This function prints three strings in the centre of line y in the + window win. The initial string is printed using the window attributes + in attr_initial, the main string uses attr_string and the final string + uses attr_final. No spaces appear between the three strings. Please + note that wrefresh() is NOT called. Also note ordering of parameters! +*/ + +int center3 (WINDOW *win, int y, int attr_initial, int attr_final, + int attr_string, const char *initial, const char *final, + const char *format, ...) +{ + va_list args; + + int oldattr; + int len1, len2, len3; + int ret, x; + char *buf; + + + buf = malloc(BUFSIZE); + if (buf == NULL) { + err_exit("out of memory"); + } + + oldattr = getbkgd(win) & ~A_CHARTEXT; + wbkgdset(win, A_NORMAL | (oldattr & A_COLOR)); + + len1 = strlen(initial); + len3 = strlen(final); + + va_start(args, format); + len2 = vsnprintf(buf, BUFSIZE, format, args); + va_end(args); + if (len2 < 0) { + free(buf); + return ERR; + } + + x = (getmaxx(win) - (len1 + len2 + len3)) / 2; + wattrset(win, attr_initial); + mvwprintw(win, y, MAX(x, 2), "%s", initial); + wattrset(win, attr_string); + ret = wprintw(win, "%1.*s", getmaxx(win) - len1 - len3 - 4, buf); + wattrset(win, attr_final); + wprintw(win, "%s", final); + + wattrset(win, oldattr); + wbkgdset(win, oldattr); + + free(buf); + return ret; +} + + /*----------------------------------------------------------------------- Function: attrpr - Print a string with special attributes Arguments: win - Window to use diff --git a/src/intf.h b/src/intf.h index a7f2a2d..2f2ce50 100644 --- a/src/intf.h +++ b/src/intf.h @@ -176,6 +176,11 @@ extern int center2 (WINDOW *win, int y, int attr_initial, int attr_string, const char *initial, const char *format, ...) __attribute__((format (printf, 6, 7))); +extern int center3 (WINDOW *win, int y, int attr_initial, int attr_final, + int attr_string, const char *initial, const char *final, + const char *format, ...) + __attribute__((format (printf, 8, 9))); + extern int attrpr (WINDOW *win, int attr, const char *format, ...) __attribute__((format (printf, 3, 4)));