1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-12-04 14:46:45 -05:00

Correct some bugs in center(); add the center2() function

This commit is contained in:
John Zaitseff 2011-07-15 14:55:56 +10:00
parent d9d703f48e
commit 34c787cc30
2 changed files with 69 additions and 9 deletions

View File

@ -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);

View File

@ -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)));