comparison ui.c @ 504:60e04709ce0f

Refactor window backbuffer to use integer as internal storage to simplify line handling and buffer drawing.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 02 Jun 2012 22:00:25 +0300
parents bac3f9af112c
children 8734a02a86ec
comparison
equal deleted inserted replaced
503:bac3f9af112c 504:60e04709ce0f
192 return; 192 return;
193 } 193 }
194 } 194 }
195 } 195 }
196 196
197
197 static BOOL nnwin_get_color(char const **s, int *col) 198 static BOOL nnwin_get_color(char const **s, int *col)
198 { 199 {
199 int val = 0; 200 int val = 0;
200 201
201 while (**s >= '0' && **s <= '9') 202 while (**s >= '0' && **s <= '9')
214 215
215 return TRUE; 216 return TRUE;
216 } 217 }
217 218
218 219
219 #define QPUTCH(ch) th_vputch(&(win->line->buf), &(win->line->bufsize), &(win->line->len), ch) 220 #define QPUTCH(ch) nnwin_putch(&(win->line->buf), &(win->line->bufsize), &(win->line->len), col, ch)
221 static BOOL nnwin_putch(int **buf, size_t *bufsize, size_t *len, int color, char ch)
222 {
223 if (*buf == NULL)
224 *bufsize = *len = 0;
225
226 if (*buf == NULL || *len + 1 >= *bufsize)
227 {
228 *bufsize += TH_BUFGROW;
229 *buf = (int *) th_realloc(*buf, *bufsize * sizeof(int));
230 if (*buf == NULL)
231 return FALSE;
232 }
233
234 (*buf)[*len] = ((unsigned char) ch) | color;
235 (*len)++;
236
237 return TRUE;
238 }
239
220 240
221 int nnwin_print(nn_window_t *win, const char *fmt) 241 int nnwin_print(nn_window_t *win, const char *fmt)
222 { 242 {
223 const char *s = fmt; 243 const char *s = fmt;
224 int col = 0; 244 int col = 0;
236 { 256 {
237 s++; 257 s++;
238 if (*s == '½') 258 if (*s == '½')
239 { 259 {
240 QPUTCH(*s); 260 QPUTCH(*s);
241 QPUTCH(*s);
242 win->line->chlen++;
243 } 261 }
244 else 262 else
245 { 263 {
246 if (!nnwin_get_color(&s, &col)) 264 if (!nnwin_get_color(&s, &col))
247 return -1; 265 return -1;
248
249 QPUTCH('½');
250
251 if (!th_growbuf(&(win->line->buf), &(win->line->bufsize), &(win->line->len), sizeof(int)))
252 return -2;
253
254 memcpy(win->line->buf + win->line->len, &col, sizeof(int));
255 win->line->len += sizeof(int);
256 } 266 }
257 } 267 }
258 else if (*s == '\n') 268 else if (*s == '\n')
259 { 269 {
260 QPUTCH(0);
261 th_ringbuf_add(win->data, win->line); 270 th_ringbuf_add(win->data, win->line);
262 win->line = NULL; 271 win->line = NULL;
263 win->dirty = TRUE; 272 win->dirty = TRUE;
264 } 273 }
265 else if (*s != '\r') 274 else if (*s != '\r')
266 { 275 {
267 QPUTCH((unsigned char) *s == 255 ? ' ' : *s); 276 QPUTCH((unsigned char) *s == 255 ? ' ' : *s);
268 win->line->chlen++;
269 } 277 }
270 278
271 s++; 279 s++;
272 } 280 }
273 281
302 { 310 {
303 const char *s = fmt; 311 const char *s = fmt;
304 int col = 0; 312 int col = 0;
305 while (*s) 313 while (*s)
306 { 314 {
307 if (clip && getcurx(win) >= getmaxx(win)) 315 if (clip && getcurx(win) >= scrWidth)
308 return; 316 return;
309 317
310 if (*s == '½') 318 if (*s == '½')
311 { 319 {
312 s++; 320 s++;
355 for (y = scrHeight - 4, offs = buf->size - 1 - currWin->pos; offs >= 0 && y > 0; offs--) 363 for (y = scrHeight - 4, offs = buf->size - 1 - currWin->pos; offs >= 0 && y > 0; offs--)
356 { 364 {
357 nn_line_t *line = buf->data[offs]; 365 nn_line_t *line = buf->data[offs];
358 if (line != NULL) 366 if (line != NULL)
359 { 367 {
360 const char *s = line->buf; 368 const int *s = line->buf;
361 int col = 0; 369 size_t pos;
362 y -= 1 + (line->chlen / scrWidth); 370
363 wmove(stdscr, y, 0); 371 y -= (line->len / scrWidth);
364 372 if (line->len % scrWidth != 0)
365 while (*s) 373 y -= 1;
374
375 if (y < 0)
366 { 376 {
367 if (*s == '½') 377 size_t r;
378 int x;
379 for (r = -y, x = 0, pos = 0; r && pos < line->len; pos++)
368 { 380 {
369 s++; 381 if (++x >= scrWidth)
370 if (*s == '½')
371 { 382 {
372 waddch(stdscr, ((unsigned char) *s) | col); 383 x = 0;
373 s++; 384 r++;
374 }
375 else
376 {
377 memcpy(&col, s, sizeof(int));
378 s += sizeof(int);
379 } 385 }
380 } 386 }
381 else 387 y = 0;
382 {
383 waddch(stdscr, ((unsigned char) *s) | col);
384 s++;
385 }
386 } 388 }
389
390 wmove(stdscr, y, 0);
391
392 for (pos = 0; pos < line->len; pos++)
393 waddch(stdscr, s[pos]);
387 } 394 }
388 } 395 }
389 396
390 currWin->dirty = FALSE; 397 currWin->dirty = FALSE;
391 } 398 }