diff options
Diffstat (limited to 'graphics.c')
-rw-r--r-- | graphics.c | 98 |
1 files changed, 95 insertions, 3 deletions
@@ -74,13 +74,105 @@ drawcontainer(GuiElement *g) } void +drawtext(Rectangle rect, Rune *text, Image *fg, int firstline, int *nlinesp, int *endlinep) +{ + Point p = rect.min; + int w; + + int nlines = 1; + int newlines = 0; + int endline = 0; + int ended = 0; + int xoffset = 0; + + for(Rune *r = text; *r; r++){ + switch(*r){ + case '\n': + newlines++; +Lnewline: if(ended && endline == 0) + endline = nlines; + p.x = rect.min.x; + xoffset = 0; + if((newlines)-(*r=='\n') >= firstline) + p.y += font->height; + nlines++; + break; + case '\t': + w = 8-(xoffset%8); + xoffset += w; + + w *= stringnwidth(font, " ", 1); + xoffset += w; + if(p.x+w > rect.max.x){ + r--; + goto Lnewline; + }else + p.x += w; + break; + default: + xoffset++; + w = runestringnwidth(font, r, 1); + if(p.x+w > rect.max.x){ + r--; + goto Lnewline; + } + if(!ended && (p.y+font->height) < rect.max.y){ + if(newlines >= firstline) + runestringn(screen, p, fg, ZP, font, r, 1); + }else + ended = 1; + p.x += w; + break; + } + } + + if(endline == 0) + endline = nlines; + + if(nlinesp) + *nlinesp = nlines; + if(endlinep) + *endlinep = endline; +} + +void drawtextbox(GuiElement *g) { - Rune *text = getprop(g, Ptext, 1).text; + Rune *text = getprop(g, Ptext, 0).text; Image *fg = getprop(g, Ptextcolour, 1).colour->image; - + Image *scrollbg = getprop(g, Pbordercolour, 1).colour->image; + Image *scrollfg = getprop(g, Pbackground, 1).colour->image; + int firstline = getprop(g, Pscroll, 1).scroll; + + int lines = 0; + for(Rune *r = text; *r; r++) + lines += (*r) == '\n'; + + /* draw the scroll bar background here, then draw the actual bar later */ + Rectangle scrollbar = Rect(g->rect.min.x, g->rect.min.y, g->rect.min.x+ScrollbarWidth, g->rect.max.y); + Rectangle textrect = g->content; + textrect.min.x += ScrollbarWidth; + + draw(screen, scrollbar, scrollbg, nil, ZP); - runestring(screen, g->content.min, fg, ZP, font, text); + int nlines, endline; + drawtext(textrect, text, fg, firstline, &nlines, &endline); + + int height = Dy(scrollbar); + scrollbar.max.x -= 1; + scrollbar.min.y += (height * (firstline)) / nlines; + scrollbar.max.y -= (height * (nlines-endline)) / nlines; + + if(scrollbar.min.y > (g->rect.max.y - 2)) + scrollbar.min.y = g->rect.max.y - 2; + + draw(screen, scrollbar, scrollfg, nil, ZP); + + if(firstline > nlines){ + PropVal p; + p.scroll = nlines - 1; + setprop(g, Pscroll, p, 0); /* TODO: we don't have the write lock here :) */ + } } Colour * |