#include #include #include #include #include #include #include #include #include #include #include "embedded-view.h" // backend message reporting window static int init_console_view(embedded_view_t *view); static void update_console(embedded_view_t *view, int x1, int y1, int x2, int y2); static void event_console(embedded_view_t *view, event_t ev); static void postscript_console(embedded_view_t *view); static void latex_console(embedded_view_t *view); embedded_view_t the_console_view = { init: (int(*)(embedded_view_t*,...))init_console_view, update: update_console, event: event_console, postscript: postscript_console, latex: latex_console, mouse_moved: 0}; static int nlines; static char **lines; static void render_console(int y1, int y2, int *H, int d); int console_view_setup = 0; static int init_console_view(embedded_view_t *view) { int now_console_height; console_view_setup = 1; nlines = 0; lines = 0; render_console(0, 0, &now_console_height, 0); now_console_height += 2; the_console_view.height = now_console_height; the_console_view.width = the_console_view.vis_width - 20; return 0; } static void console_add_chunk(char *text, int len) { int dx, nl; char *l; if(nlines) { l = lines[nlines-1]; dx = 10; yzSelectFont("system"); if(l)dx += yzStringSize(l, strlen(l)); dx += yzStringSize(text, len); if(dx > the_console_view.width) { //fprintf(stderr, "width up to %d / %d\n", dx, the_console_view.width); goto newline; } //else fprintf(stderr, "no problem; %d.\n", dx); if(l)nl = strlen(l); else nl = 0; nl += len; l = realloc(l, nl + 1); strncpy(l+nl-len, text, len); l[nl] = 0; lines[nlines-1] = l; //fprintf(stderr, "line %d is now: `%s'\n", nlines-1, l); } else { newline: nlines++; lines = realloc(lines, sizeof(char*)*nlines); lines[nlines-1] = malloc(len+1); strncpy(lines[nlines-1], text, len); lines[nlines-1][len] = 0; } } void console_new_line() { nlines++; lines = realloc(lines, sizeof(char*)*nlines); lines[nlines-1] = 0; } void console_add(char *text) { char was_bottom = 1, c, *p = text, *p2 = text; extern window_t *lkb_top; window_t *oldw; if(!console_view_setup) { fprintf(stderr, "%s", text); //fflush(stdout); return; } oldw = yzSelectWindow(lkb_top); while(1) { if(*p <= ' ') { c = *p; if(c==' ')console_add_chunk(p2, p-p2+1); else if(p > p2)console_add_chunk(p2, p-p2); if(c=='\n') console_new_line(); if(c==0)break; p2 = p+1; } p++; } yzDrawScrollArea(the_console_view.scroll_area); if(was_bottom) yzSetScrollBarPosition( the_console_view.vscroll, the_console_view.height ); yzSelectWindow(oldw); } static void update_console(embedded_view_t *view, int x1, int y1, int x2, int y2) { int cx1, cy1, cx2, cy2, ox, oy; int now_console_height; yzPenColor(65535,65535,65535); yzRect(x1, y1, x2, y2); yzPenColor(0,0,0); render_console(y1, y2, &now_console_height, 1); now_console_height += 2; // XXX this should really be in embedded-view.c // e.g. set_view_size(&the_console_view, 550, now_console_height); yzPopClipRect4p(&cx1, &cy1, &cx2, &cy2); yzPopOrigin2p(&ox, &oy); if(the_console_view.height != now_console_height) { the_console_view.height = now_console_height; yzSetScrollBarMax(the_console_view.vscroll, now_console_height); } yzPushOrigin(ox, oy); yzPushClipRect(cx1, cy1, cx2, cy2); } static void render_console(int y1, int y2, int *H, int d) { int i, y = 20, x = 10; yzSelectFont("system"); for(i=0;i