Mercurial > hg > nnchat
diff ui.c @ 503:bac3f9af112c
More work on curses cleanup. Almost working now.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 02 Jun 2012 20:03:08 +0300 |
parents | ca88945d8eda |
children | 60e04709ce0f |
line wrap: on
line diff
--- a/ui.c Fri Jun 01 17:45:38 2012 +0300 +++ b/ui.c Sat Jun 02 20:03:08 2012 +0300 @@ -194,6 +194,27 @@ } } +static BOOL nnwin_get_color(char const **s, int *col) +{ + int val = 0; + + while (**s >= '0' && **s <= '9') + { + val *= 10; + val += (**s - '0'); + (*s)++; + } + if (**s != '½') + return FALSE; + + if (val < 9) + *col = A_DIM | COLOR_PAIR(val); + else if (val < 30) + *col = A_BOLD | COLOR_PAIR(val - 9); + + return TRUE; +} + #define QPUTCH(ch) th_vputch(&(win->line->buf), &(win->line->bufsize), &(win->line->len), ch) @@ -222,20 +243,9 @@ } else { - int val = 0; - while (*s >= '0' && *s <= '9') - { - val *= 10; - val += (*s - '0'); - s++; - } - if (*s != '½') return -1; - - if (val < 9) - col = A_DIM | COLOR_PAIR(val); - else if (val < 30) - col = A_BOLD | COLOR_PAIR(val - 9); - + if (!nnwin_get_color(&s, &col)) + return -1; + QPUTCH('½'); if (!th_growbuf(&(win->line->buf), &(win->line->bufsize), &(win->line->len), sizeof(int))) @@ -288,97 +298,129 @@ } -void nnwin_update(BOOL force, nn_editbuf_t *ebuf, char *optUserName, int optUserColor) +static void nnwin_print_str(WINDOW *win, const char *fmt, BOOL clip) { - if (force) + const char *s = fmt; + int col = 0; + while (*s) + { + if (clip && getcurx(win) >= getmaxx(win)) + return; + + if (*s == '½') + { + s++; + if (*s == '½') + { + waddch(win, ((unsigned char) *s) | col); + s++; + } + else + { + if (!nnwin_get_color(&s, &col)) + return; + } + } + else + { + waddch(win, ((unsigned char) *s) | col); + s++; + } + } +} + + +void nnwin_update(BOOL force, nn_editbuf_t *ebuf, char *username, int usercolor) +{ + int sx, sy; + + // Save cursor position + getyx(stdscr, sy, sx); + + // Clear screen if forced or main or editbuf are dirty + if (force || (currWin != NULL && currWin->dirty) || (ebuf != NULL && ebuf->dirty)) { wattrset(stdscr, A_NORMAL); wbkgdset(stdscr, COLOR_PAIR(0)); werase(stdscr); + force = TRUE; } // Check if update is forced or if the window is dirty if (currWin != NULL && (force || currWin->dirty)) { - int h = scrHeight - 5, y, offs; + int y, offs; qringbuf_t *buf = currWin->data; - wattrset(stdscr, A_NORMAL); - wbkgdset(stdscr, COLOR_PAIR(0)); - wmove(stdscr, 0, 0); - - for (y = 0, offs = buf->size - h - currWin->pos; offs >= 0 && offs < buf->size - currWin->pos && offs < buf->size && y < h; offs++) + for (y = scrHeight - 4, offs = buf->size - 1 - currWin->pos; offs >= 0 && y > 0; offs--) { - int col = 0; nn_line_t *line = buf->data[offs]; - if (line == NULL) - continue; - - while (*s && y < h) + if (line != NULL) { - if (*s == '½') + const char *s = line->buf; + int col = 0; + y -= 1 + (line->chlen / scrWidth); + wmove(stdscr, y, 0); + + while (*s) { - s++; if (*s == '½') { + s++; + if (*s == '½') + { + waddch(stdscr, ((unsigned char) *s) | col); + s++; + } + else + { + memcpy(&col, s, sizeof(int)); + s += sizeof(int); + } + } + else + { waddch(stdscr, ((unsigned char) *s) | col); s++; } - else - { - memcpy(&col, s, sizeof(int)); - s += sizeof(int); - } } - else - { - waddch(stdscr, ((unsigned char) *s) | col); - s++; - } - y = getcury(stdscr); } } currWin->dirty = FALSE; } + // Update statusline { - // Statusline - char tmpStr[128]; + char tmpStamp[32], tmpStr[128]; int i; - str_get_timestamp(tmpStr, sizeof(tmpStr), "%H:%M:%S"); + str_get_timestamp(tmpStamp, sizeof(tmpStamp), "%H:%M:%S"); +#if 0 + snprintf(tmpStr, sizeof(tmpStr), + " ½10½%s½13½ | ½16½%s½13½ | ½11½#%06x½13½ | WIN: %d: %s / %d | ½11½", + tmpStamp, + username, + usercolor, + currWin->num + 1, + currWin->id != NULL ? currWin->id : "MAIN", + currWin->pos); +#else + snprintf(tmpStr, sizeof(tmpStr), + " %s | %s | #%06x | WIN: %d: %s / %d | ", + tmpStamp, + username, + usercolor, + currWin->num + 1, + currWin->id != NULL ? currWin->id : "MAIN", + currWin->pos); +#endif + wmove(stdscr, scrHeight - 4, 0); wbkgdset(stdscr, COLOR_PAIR(10)); wclrtoeol(stdscr); - - wattrset(stdscr, A_BOLD | COLOR_PAIR(11)); - mvwaddstr(stdscr, scrHeight - 4, 1, tmpStr); - - wattrset(stdscr, A_BOLD | COLOR_PAIR(13)); - waddstr(stdscr, " | "); - wattrset(stdscr, A_BOLD | COLOR_PAIR(16)); - waddstr(stdscr, optUserName); - wattrset(stdscr, A_BOLD | COLOR_PAIR(13)); - - wattrset(stdscr, A_BOLD | COLOR_PAIR(13)); - waddstr(stdscr, " | "); - wattrset(stdscr, A_BOLD | COLOR_PAIR(11)); - snprintf(tmpStr, sizeof(tmpStr), "#%06x", optUserColor); - waddstr(stdscr, tmpStr); - - wattrset(stdscr, A_BOLD | COLOR_PAIR(13)); - waddstr(stdscr, " | WIN: "); - snprintf(tmpStr, sizeof(tmpStr), "%d: %s / %d", - currWin->num + 1, - currWin->id != NULL ? currWin->id : "MAIN", - currWin->pos); - waddstr(stdscr, tmpStr); - - wattrset(stdscr, A_BOLD | COLOR_PAIR(13)); - waddstr(stdscr, " | "); - wattrset(stdscr, A_BOLD | COLOR_PAIR(11)); + nnwin_print_str(stdscr, tmpStr, TRUE); for (i = 0; i < SET_MAX_WINDOWS; i++) { @@ -389,9 +431,12 @@ } } } + + // Restore cursor position + wmove(stdscr, sy, sx); - if (ebuf != NULL) - // && (force || ebuf->dirty)) + // Update editbuf if needed + if (ebuf != NULL && (force || ebuf->dirty)) { int yoffs = ebuf->pos / scrWidth, xoffs = ebuf->pos % scrWidth;