From 10c4cb2a22e5dd89165db5a0caeb05c09740a9f7 Mon Sep 17 00:00:00 2001 From: John Zaitseff <J.Zaitseff@zap.org.au> Date: Mon, 4 Jul 2011 17:54:30 +1000 Subject: [PATCH] Add a "y" parameter to center(); add the COL_CENTER macro --- src/intf.c | 25 ++++++++++++------------- src/intf.h | 14 ++++++++------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/intf.c b/src/intf.c index f52f9c0..30fbe05 100644 --- a/src/intf.c +++ b/src/intf.c @@ -106,11 +106,10 @@ void init_screen (void) } clear(); - move(0, 0); attrset(has_colors() ? COLOR_PAIR(YELLOW_ON_CYAN) | A_BOLD : A_REVERSE | A_BOLD); - center(stdscr, true, PACKAGE_NAME); + center(stdscr, 0, true, PACKAGE_NAME); attrset(A_NORMAL); refresh(); @@ -281,25 +280,26 @@ int txrefresh (void) /*----------------------------------------------------------------------- Function: center - Centre a string on the current line Arguments: win - Window to use + y - Line on which to centre the string clrline - True to print spaces on both sides of line format - printf()-like format string ... - printf()-like arguments Returns: int - Return code from wprintw() This function prints a string (formated with wprintw(format, ...)) in - the centre of the current line in the window win. If clrline is TRUE, - spaces are printed before and after the line to make sure the current - attributes are set. The cursor is then moved to the start of the next - line, or the start of the current line (if already on the last line of - the screen). Please note that wrefresh() is NOT called. + the centre of line y in the window win. If clrline is TRUE, spaces are + printed before and after the line to make sure the current attributes + are set; in this case, the cursor is also moved to the start of the + next line (or the start of the current line if already on the last line + of the window). Please note that wrefresh() is NOT called. */ -int center (WINDOW *win, const bool clrline, const char *format, ...) +int center (WINDOW *win, int y, const bool clrline, const char *format, ...) { va_list args; int len, ret; - int y, x, maxy, maxx; + int maxy, maxx; int fill; char *buf = malloc(OUTBUFSIZE); @@ -313,13 +313,12 @@ int center (WINDOW *win, const bool clrline, const char *format, ...) return ERR; } - getyx(win, y, x); getmaxyx(win, maxy, maxx); - fill = (maxx - len) / 2; if (clrline) { wmove(win, y, 0); + if (fill > 0) { wprintw(win, "%*c", fill, ' '); } @@ -327,12 +326,12 @@ int center (WINDOW *win, const bool clrline, const char *format, ...) if (maxx - len - fill > 0) { wprintw(win, "%*c", maxx - len - fill, ' '); } + + wmove(win, (y + 1 >= maxy ? y : y + 1), 0); } else { ret = mvwprintw(win, y, fill > 0 ? fill : 0, "%s", buf); } - wmove(win, (y + 1 >= maxy ? y : y + 1), 0); - free(buf); return ret; } diff --git a/src/intf.h b/src/intf.h index 38ca37d..167b8de 100644 --- a/src/intf.h +++ b/src/intf.h @@ -39,8 +39,8 @@ * Constants and type declarations * ************************************************************************/ -#define MIN_COLS (80) /* Minimum number of columns in terminal */ #define MIN_LINES (24) /* Minimum number of lines in terminal */ +#define MIN_COLS (80) /* Minimum number of columns in terminal */ /* This version of Star Traders only utilises WIN_COLS x WIN_LINES of a @@ -48,11 +48,12 @@ be added to each newwin() call to position the window correctly. */ -#define WIN_COLS MIN_COLS /* Number of columns in main windows */ #define WIN_LINES MIN_LINES /* Number of lines in main windows */ +#define WIN_COLS MIN_COLS /* Number of columns in main windows */ -#define COL_OFFSET ((COLS - MIN_COLS) / 2) /* Window offsets */ -#define LINE_OFFSET (0) +#define LINE_OFFSET (0) /* Window offsets */ +#define COL_OFFSET ((COLS - WIN_COLS) / 2) +#define COL_CENTER(x) ((COLS - (x)) / 2) // Colour pairs used in Star Traders @@ -86,8 +87,9 @@ extern int deltxwin (void); extern int delalltxwin (void); extern int txrefresh (void); -extern int center (WINDOW *win, const bool clrline, const char *format, ...) - __attribute__((format (printf, 3, 4))); +extern int center (WINDOW *win, int y, const bool clrline, + const char *format, ...) + __attribute__((format (printf, 4, 5))); #endif /* included_INTF_H */