summaryrefslogtreecommitdiff
path: root/graphics.c
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2024-02-17 16:42:00 +0000
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2024-02-17 16:42:00 +0000
commitd9a15e56d42e45f021c01d527df67863100840a7 (patch)
tree7e01ec814a1eae74db53522a2de40f1f81f3f43f /graphics.c
parentc51962a648b3f38fc378ad0081b1d1aa037142d5 (diff)
Implement scroll bar (not clickable yet) and sam-scrollingHEADfront
Diffstat (limited to 'graphics.c')
-rw-r--r--graphics.c98
1 files changed, 95 insertions, 3 deletions
diff --git a/graphics.c b/graphics.c
index 9f14058..bbafa72 100644
--- a/graphics.c
+++ b/graphics.c
@@ -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 *