comparison 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
comparison
equal deleted inserted replaced
502:52da406caf54 503:bac3f9af112c
192 return; 192 return;
193 } 193 }
194 } 194 }
195 } 195 }
196 196
197 static BOOL nnwin_get_color(char const **s, int *col)
198 {
199 int val = 0;
200
201 while (**s >= '0' && **s <= '9')
202 {
203 val *= 10;
204 val += (**s - '0');
205 (*s)++;
206 }
207 if (**s != '½')
208 return FALSE;
209
210 if (val < 9)
211 *col = A_DIM | COLOR_PAIR(val);
212 else if (val < 30)
213 *col = A_BOLD | COLOR_PAIR(val - 9);
214
215 return TRUE;
216 }
217
197 218
198 #define QPUTCH(ch) th_vputch(&(win->line->buf), &(win->line->bufsize), &(win->line->len), ch) 219 #define QPUTCH(ch) th_vputch(&(win->line->buf), &(win->line->bufsize), &(win->line->len), ch)
199 220
200 int nnwin_print(nn_window_t *win, const char *fmt) 221 int nnwin_print(nn_window_t *win, const char *fmt)
201 { 222 {
220 QPUTCH(*s); 241 QPUTCH(*s);
221 win->line->chlen++; 242 win->line->chlen++;
222 } 243 }
223 else 244 else
224 { 245 {
225 int val = 0; 246 if (!nnwin_get_color(&s, &col))
226 while (*s >= '0' && *s <= '9') 247 return -1;
227 { 248
228 val *= 10;
229 val += (*s - '0');
230 s++;
231 }
232 if (*s != '½') return -1;
233
234 if (val < 9)
235 col = A_DIM | COLOR_PAIR(val);
236 else if (val < 30)
237 col = A_BOLD | COLOR_PAIR(val - 9);
238
239 QPUTCH('½'); 249 QPUTCH('½');
240 250
241 if (!th_growbuf(&(win->line->buf), &(win->line->bufsize), &(win->line->len), sizeof(int))) 251 if (!th_growbuf(&(win->line->buf), &(win->line->bufsize), &(win->line->len), sizeof(int)))
242 return -2; 252 return -2;
243 253
286 else 296 else
287 return NULL; 297 return NULL;
288 } 298 }
289 299
290 300
291 void nnwin_update(BOOL force, nn_editbuf_t *ebuf, char *optUserName, int optUserColor) 301 static void nnwin_print_str(WINDOW *win, const char *fmt, BOOL clip)
292 { 302 {
293 if (force) 303 const char *s = fmt;
304 int col = 0;
305 while (*s)
306 {
307 if (clip && getcurx(win) >= getmaxx(win))
308 return;
309
310 if (*s == '½')
311 {
312 s++;
313 if (*s == '½')
314 {
315 waddch(win, ((unsigned char) *s) | col);
316 s++;
317 }
318 else
319 {
320 if (!nnwin_get_color(&s, &col))
321 return;
322 }
323 }
324 else
325 {
326 waddch(win, ((unsigned char) *s) | col);
327 s++;
328 }
329 }
330 }
331
332
333 void nnwin_update(BOOL force, nn_editbuf_t *ebuf, char *username, int usercolor)
334 {
335 int sx, sy;
336
337 // Save cursor position
338 getyx(stdscr, sy, sx);
339
340 // Clear screen if forced or main or editbuf are dirty
341 if (force || (currWin != NULL && currWin->dirty) || (ebuf != NULL && ebuf->dirty))
294 { 342 {
295 wattrset(stdscr, A_NORMAL); 343 wattrset(stdscr, A_NORMAL);
296 wbkgdset(stdscr, COLOR_PAIR(0)); 344 wbkgdset(stdscr, COLOR_PAIR(0));
297 werase(stdscr); 345 werase(stdscr);
346 force = TRUE;
298 } 347 }
299 348
300 // Check if update is forced or if the window is dirty 349 // Check if update is forced or if the window is dirty
301 if (currWin != NULL && (force || currWin->dirty)) 350 if (currWin != NULL && (force || currWin->dirty))
302 { 351 {
303 int h = scrHeight - 5, y, offs; 352 int y, offs;
304 qringbuf_t *buf = currWin->data; 353 qringbuf_t *buf = currWin->data;
305 354
306 wattrset(stdscr, A_NORMAL); 355 for (y = scrHeight - 4, offs = buf->size - 1 - currWin->pos; offs >= 0 && y > 0; offs--)
307 wbkgdset(stdscr, COLOR_PAIR(0)); 356 {
308 wmove(stdscr, 0, 0);
309
310 for (y = 0, offs = buf->size - h - currWin->pos; offs >= 0 && offs < buf->size - currWin->pos && offs < buf->size && y < h; offs++)
311 {
312 int col = 0;
313 nn_line_t *line = buf->data[offs]; 357 nn_line_t *line = buf->data[offs];
314 if (line == NULL) 358 if (line != NULL)
315 continue; 359 {
316 360 const char *s = line->buf;
317 while (*s && y < h) 361 int col = 0;
318 { 362 y -= 1 + (line->chlen / scrWidth);
319 if (*s == '½') 363 wmove(stdscr, y, 0);
364
365 while (*s)
320 { 366 {
321 s++;
322 if (*s == '½') 367 if (*s == '½')
368 {
369 s++;
370 if (*s == '½')
371 {
372 waddch(stdscr, ((unsigned char) *s) | col);
373 s++;
374 }
375 else
376 {
377 memcpy(&col, s, sizeof(int));
378 s += sizeof(int);
379 }
380 }
381 else
323 { 382 {
324 waddch(stdscr, ((unsigned char) *s) | col); 383 waddch(stdscr, ((unsigned char) *s) | col);
325 s++; 384 s++;
326 } 385 }
327 else
328 {
329 memcpy(&col, s, sizeof(int));
330 s += sizeof(int);
331 }
332 } 386 }
333 else
334 {
335 waddch(stdscr, ((unsigned char) *s) | col);
336 s++;
337 }
338 y = getcury(stdscr);
339 } 387 }
340 } 388 }
341 389
342 currWin->dirty = FALSE; 390 currWin->dirty = FALSE;
343 } 391 }
344 392
345 { 393 // Update statusline
346 // Statusline 394 {
347 char tmpStr[128]; 395 char tmpStamp[32], tmpStr[128];
348 int i; 396 int i;
349 397
350 str_get_timestamp(tmpStr, sizeof(tmpStr), "%H:%M:%S"); 398 str_get_timestamp(tmpStamp, sizeof(tmpStamp), "%H:%M:%S");
351 399
400 #if 0
401 snprintf(tmpStr, sizeof(tmpStr),
402 " ½10½%s½13½ | ½16½%s½13½ | ½11½#%06x½13½ | WIN: %d: %s / %d | ½11½",
403 tmpStamp,
404 username,
405 usercolor,
406 currWin->num + 1,
407 currWin->id != NULL ? currWin->id : "MAIN",
408 currWin->pos);
409 #else
410 snprintf(tmpStr, sizeof(tmpStr),
411 " %s | %s | #%06x | WIN: %d: %s / %d | ",
412 tmpStamp,
413 username,
414 usercolor,
415 currWin->num + 1,
416 currWin->id != NULL ? currWin->id : "MAIN",
417 currWin->pos);
418 #endif
419
352 wmove(stdscr, scrHeight - 4, 0); 420 wmove(stdscr, scrHeight - 4, 0);
353 wbkgdset(stdscr, COLOR_PAIR(10)); 421 wbkgdset(stdscr, COLOR_PAIR(10));
354 wclrtoeol(stdscr); 422 wclrtoeol(stdscr);
355 423 nnwin_print_str(stdscr, tmpStr, TRUE);
356 wattrset(stdscr, A_BOLD | COLOR_PAIR(11));
357 mvwaddstr(stdscr, scrHeight - 4, 1, tmpStr);
358
359 wattrset(stdscr, A_BOLD | COLOR_PAIR(13));
360 waddstr(stdscr, " | ");
361 wattrset(stdscr, A_BOLD | COLOR_PAIR(16));
362 waddstr(stdscr, optUserName);
363 wattrset(stdscr, A_BOLD | COLOR_PAIR(13));
364
365 wattrset(stdscr, A_BOLD | COLOR_PAIR(13));
366 waddstr(stdscr, " | ");
367 wattrset(stdscr, A_BOLD | COLOR_PAIR(11));
368 snprintf(tmpStr, sizeof(tmpStr), "#%06x", optUserColor);
369 waddstr(stdscr, tmpStr);
370
371 wattrset(stdscr, A_BOLD | COLOR_PAIR(13));
372 waddstr(stdscr, " | WIN: ");
373 snprintf(tmpStr, sizeof(tmpStr), "%d: %s / %d",
374 currWin->num + 1,
375 currWin->id != NULL ? currWin->id : "MAIN",
376 currWin->pos);
377 waddstr(stdscr, tmpStr);
378
379 wattrset(stdscr, A_BOLD | COLOR_PAIR(13));
380 waddstr(stdscr, " | ");
381 wattrset(stdscr, A_BOLD | COLOR_PAIR(11));
382 424
383 for (i = 0; i < SET_MAX_WINDOWS; i++) 425 for (i = 0; i < SET_MAX_WINDOWS; i++)
384 { 426 {
385 if (chatWindows[i] != NULL && chatWindows[i]->dirty) 427 if (chatWindows[i] != NULL && chatWindows[i]->dirty)
386 { 428 {
387 snprintf(tmpStr, sizeof(tmpStr), "%d ", i + 1); 429 snprintf(tmpStr, sizeof(tmpStr), "%d ", i + 1);
388 waddstr(stdscr, tmpStr); 430 waddstr(stdscr, tmpStr);
389 } 431 }
390 } 432 }
391 } 433 }
392 434
393 if (ebuf != NULL) 435 // Restore cursor position
394 // && (force || ebuf->dirty)) 436 wmove(stdscr, sy, sx);
437
438 // Update editbuf if needed
439 if (ebuf != NULL && (force || ebuf->dirty))
395 { 440 {
396 int yoffs = ebuf->pos / scrWidth, 441 int yoffs = ebuf->pos / scrWidth,
397 xoffs = ebuf->pos % scrWidth; 442 xoffs = ebuf->pos % scrWidth;
398 char *tmp; 443 char *tmp;
399 444