From 34c787cc30bf0d6e69dfded262c5e4e358205ff0 Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Fri, 15 Jul 2011 14:55:56 +1000 Subject: [PATCH] Correct some bugs in center(); add the center2() function --- src/intf.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++------ src/intf.h | 8 +++++-- 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/src/intf.c b/src/intf.c index 6621dde..ae15dc0 100644 --- a/src/intf.c +++ b/src/intf.c @@ -316,10 +316,7 @@ int center (WINDOW *win, int y, int attr, const char *format, ...) va_list args; int oldattr; - int len, ret; - int maxy, maxx; - int fill; - + int len, ret, x; char *buf; @@ -336,13 +333,72 @@ int center (WINDOW *win, int y, int attr, const char *format, ...) len = vsnprintf(buf, BUFSIZE, format, args); va_end(args); if (len < 0) { + free(buf); return ERR; } - getmaxyx(win, maxy, maxx); - fill = (maxx - len) / 2; + x = (getmaxx(win) - len) / 2; + ret = mvwprintw(win, y, MAX(x, 2), "%1.*s", getmaxx(win) - 4, buf); - ret = mvwprintw(win, y, fill > 0 ? fill : 0, "%s", buf); + wattrset(win, oldattr); + wbkgdset(win, oldattr); + + free(buf); + return ret; +} + + +/*----------------------------------------------------------------------- + Function: center2 - Centre two 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_string - Window attribute to use for main string + initial - Fixed initial string + format - printf()-like format string + ... - printf()-like arguments + Returns: int - Return code from wprintw() + + This function prints two 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. No spaces appear + between the two strings. Please note that wrefresh() is NOT called. +*/ + +int center2 (WINDOW *win, int y, int attr_initial, int attr_string, + const char *initial, const char *format, ...) +{ + va_list args; + + int oldattr; + int len1, len2; + 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); + + 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)) / 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 - 4, buf); wattrset(win, oldattr); wbkgdset(win, oldattr); diff --git a/src/intf.h b/src/intf.h index c2760f2..91c9cb5 100644 --- a/src/intf.h +++ b/src/intf.h @@ -127,8 +127,8 @@ enum color_pairs { #define ATTR_ERROR_WINDOW ATTR(COLOR_PAIR(WHITE_ON_RED), A_REVERSE) #define ATTR_WINDOW_TITLE ATTR(COLOR_PAIR(YELLOW_ON_BLACK) | A_BOLD, A_REVERSE) #define ATTR_MAP_TITLE ATTR(COLOR_PAIR(WHITE_ON_BLUE), A_NORMAL) -#define ATTR_MAP_T_HIGHLIGHT ATTR(COLOR_PAIR(WHITE_ON_BLUE) | A_BOLD, A_BOLD) -#define ATTR_MAP_T_STANDOUT ATTR(COLOR_PAIR(WHITE_ON_BLUE) | A_BOLD | A_BLINK, A_BOLD | A_BLINK) +#define ATTR_MAP_T_HIGHLIGHT ATTR(COLOR_PAIR(YELLOW_ON_BLUE) | A_BOLD, A_BOLD) +#define ATTR_MAP_T_STANDOUT ATTR(COLOR_PAIR(YELLOW_ON_BLUE) | A_BOLD | A_BLINK, A_BOLD | A_BLINK) #define ATTR_ERROR_TITLE ATTR(COLOR_PAIR(YELLOW_ON_BLACK) | A_BOLD, A_BOLD) #define ATTR_INPUT_FIELD ATTR(COLOR_PAIR(WHITE_ON_BLACK), A_BOLD | '_') #define ATTR_KEYCODE_STR ATTR(COLOR_PAIR(YELLOW_ON_BLACK) | A_BOLD, A_REVERSE) @@ -170,6 +170,10 @@ extern int txrefresh (void); extern int center (WINDOW *win, int y, int attr, const char *format, ...) __attribute__((format (printf, 4, 5))); +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 attrpr (WINDOW *win, int attr, const char *format, ...) __attribute__((format (printf, 3, 4)));