/* Terminality - a portable terminal handling library * Copyright (C) 1998-2002, Emil Mikulic. * This is LGPL - look at COPYING.LIB */ /* Project: Terminality * File: extra.c * Authors: Volker Loew, Emil Mikulic * Description: Provide some extra functions */ #include "extra.h" #include #include #include #include const char extra_rcsid[] = "$Id: extra.c,v 1.33 2002/07/26 01:39:40 darkmoon Exp $"; /* Draws a box using the corner and line special chars. */ void drawbox (int x1, int y1, int x2, int y2) { int i, tmp; /* Switch values if necessary */ if (y1 > y2) { tmp = y2; y2 = y1; y1 = tmp; } if (x1 > x2) { tmp = x2; x2 = x1; x1 = tmp; } /* Upper left corner */ gotoxy(x1,y1); writech(C_UL); /* Top line */ for (i = x1+1; i y2) { tmp = y2; y2 = y1; y1 = tmp; } if (x1 > x2) { tmp = x2; x2 = x1; x1 = tmp; } /* On a coloured square */ bgcolor(basecolor); /* Upper left corner */ fgcolor(lite); gotoxy(x1,y1); writech(C_UL); /* Top line */ for (i = x1+1; i change foreground color to ESC b change background color to is given as a single hexadecimal digit printc() will return the length of the printed string */ int printc(char *format, ...) { unsigned char buf[BUFSIZE], tmp; int length,i; va_list va; color c; /* Print into buffer */ va_start(va, format); length = vsnprintf(buf, BUFSIZE, format, va); va_end(va); if (length == -1) return -1; /* Print to screen */ for (i=0; i= 48) && (tmp <= 48+9)) c = tmp - 48; else if ((tmp >= 65) && (tmp <= 70)) c = tmp - 65 + 10; else c = LightGray; if (buf[i+1] == 'f') fgcolor(c); else bgcolor(c); i += 2; } else #if (USE_NCURSES) && (!__DJGPP__) writech(_c((chtype)(buf[i]))); #else writech(buf[i]); #endif } return length; } /* Write a formatted string (works like printf) at x,y */ int writexy (int x, int y, char *format, ...) { char buf[BUFSIZE]; int length; va_list va; /* Move cursor */ gotoxy(x, y); /* Print into buffer */ va_start(va, format); length = vsnprintf(buf, BUFSIZE, format, va); va_end(va); if (length == -1) return -1; /* Spit it out */ printc(buf); /* Done */ return length; } /* Centerprint a formatted string (works like printf) at line y */ int center (int y, char *format, ...) { char buf[BUFSIZE]; int length, tmp_len, i; va_list va; /* Print into buffer */ va_start(va, format); tmp_len = length = vsnprintf(buf, BUFSIZE, format, va); va_end(va); if (length == -1) return -1; /* Account for color escape codes */ for (i=0; i w) { tbuf[tpos-last_space+1] = 10; last_nl = last_space; } last_space = 1; last_nl = 1; case 32: if (last_nl-2 > w) { tbuf[tpos-last_space+1] = 10; last_nl = last_space; } last_space = 1; default: last_nl++; last_space++; } } /* Save the position of each line */ lines[0] = 0; for (tpos=0; tpos= h) scrolly -= h; else scrolly = 0; break; case KEY_PGDOWN: if (scrolly+2*h <= numlines) scrolly += h; else if (numlines > h) scrolly = numlines-h; break; case KEY_HOME: scrollx = 0; scrolly = 0; break; case KEY_END: scrollx = 0; if (numlines > h) scrolly = numlines-h; break; default: beep(); break; } } while (result == 101); set_cursor(cs); xfree(tbuf); }