ref: 6d1b825eaca4da51d7b9860e3ff6a0e432c7f925
dir: /guitest.c/
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <cursor.h>
#include <thread.h>
#include <mouse.h>
#include <keyboard.h>
#include <frame.h>
#include "mez.h"
Mousectl *mc;
Keyboardctl *kc;
char buf[32000];
Rune lorem[32000];
enum
{
MOUSE,
KEYBD,
RESIZE,
NCHAN,
Scrollwid = 12, /* width of scroll bar */
Scrollgap = 4, /* gap right of scroll bar */
Margin = 4, /* margin around text */
};
void
error(char *s)
{
fprint(2, "%s: error: %s: %r\n", argv0, s);
threadexits(s);
}
void*
emallocz(ulong sz, int clr)
{
void *p;
if((p = mallocz(sz, clr)) == nil)
error("Out of memory");
setmalloctag(p, getcallerpc(&sz));
return p;
}
void*
erealloc(void *ptr, ulong size)
{
void *p;
if((p = realloc(ptr, size)) == nil)
error("Out of memory");
setmalloctag(p, getcallerpc(&ptr));
return p;
}
uint
lines(Rune *text)
{
Rune *e;
uint l;
e = text;
for(l = 0; e != nil; l++)
e = runestrchr(e+1, L'\n');
return l;
}
#define max(a, b) ((a)>(b)?(a):(b))
#define min(a, b) ((a)<(b)?(a):(b))
void
textdraw(Text *t)
{
Rectangle scrpos;
draw(t->screen, t->screen->r, t->cols[BACK], nil, ZP);
draw(t->screen, t->scrollr, t->cols[BORD], nil, ZP);
scrpos = t->scrollr;
scrpos.min.y = scrpos.min.y+max(0, Dy(scrpos))*t->topline/t->nlines;
scrpos.max.y = scrpos.min.y+Dy(t->scrollr)*t->Frame.maxlines/t->nlines;
scrpos = insetrect(scrpos, 1);
draw(screen, scrpos, t->cols[BACK], nil, ZP);
frinsert(t, t->text + t->lines[t->topline], t->text + t->textlen, 0);
flushimage(display, 1);
}
void
textaddline(Text *t, uint sol)
{
if(t->nlines == t->cap){
t->cap *= 2;
t->lines = erealloc(t->lines, t->cap*sizeof(*t->lines));
}
t->lines[t->nlines++] = sol;
}
void
textinit(Text *t)
{
Rune *rp;
uint sol;
t->scrollr = t->screen->r;
t->scrollr.max.x = t->screen->r.min.x + Scrollwid + 1;
t->bodyr.min.x = t->screen->r.min.x + Scrollwid + Scrollgap + Margin;
t->bodyr.min.y = t->screen->r.min.y;
t->bodyr.max = t->screen->r.max;
t->cap = 1024;
t->lines = emallocz(t->cap*sizeof(*t->lines), 1);
t->nlines = sol = 0;
for(rp = t->text; *rp != L'\0'; rp++)
if(*rp == L'\n'){
textaddline(t, sol);
sol = rp + 1 - t->text;
}
t->topline = 0;
frclear(t, 0);
frinit(t, t->bodyr, display->defaultfont, t->screen, t->cols);
}
void
texttopline(Text *t, int y)
{
t->topline = min(t->nlines-1, max(0, y-t->scrollr.min.y)*t->nlines/Dy(t->scrollr));
}
void
threadmain(int argc, char **argv)
{
Rune r, *rs;
Mouse m;
Chan *chan;
char *s, *wsys;
int len, fd;
len = readn(0, buf, sizeof(buf));
buf[len] = '\0';
for(s = buf, rs = lorem; *s != '\0'; s+=len, rs++)
len = chartorune(rs, s);
*rs = L'\0';
if(initdraw(nil, nil, "guitest") < 0)
sysfatal("initdraw: %r");
if((mc = initmouse(nil, screen)) == nil)
sysfatal("initmouse failed: %r");
if((kc = initkeyboard(nil)) == nil)
sysfatal("initmouse failed: %r");
chan = emallocz(sizeof(*chan), 1);
chan->body.text = lorem;
chan->body.textlen = runestrlen(chan->body.text);
chan->body.cols[BACK] = allocimagemix(display, DPurpleblue, DWhite);
chan->body.cols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
chan->body.cols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
chan->body.cols[TEXT] = display->black;
chan->body.cols[HTEXT] = display->black;
chan->body.screen = screen;
textinit(&chan->body);
if((wsys = getenv("wsys")) == nil)
sysfatal("cannot find $wsys: %r");
if((fd = open(wsys, ORDWR)) < 0)
sysfatal("cannot open $wsys: %r");
if(mount(fd, -1, "/mnt/wsysnicks", MREPL, "new") < 0)
sysfatal("cannot create new window: %r");
if(gengetwindow(display, "/mnt/wsysnicks/winname", &chan->nicks.screen, &chan->nicks._screen, Refnone) < 0)
sysfatal("cannot get window: %r");
Alt a[NCHAN+1] = {
[MOUSE] = {mc->c, &m, CHANRCV},
[RESIZE] = {mc->resizec, nil, CHANRCV},
[KEYBD] = {kc->c, &r, CHANRCV},
[NCHAN] = {nil, nil, CHANEND},
};
textdraw(&chan->body);
draw(chan->nicks.screen, chan->nicks.screen->r, chan->body.cols[HIGH], nil, ZP);
flushimage(display, 1);
for(;;)switch(alt(a)){
default:
break;
case KEYBD:
if(r == Kdel)
goto end;
break;
case MOUSE:
if(!ptinrect(m.xy, chan->body.scrollr))
break;
while(m.buttons == 2){
texttopline(&chan->body, m.xy.y);
textdraw(&chan->body);
readmouse(mc);
m = *mc;
}
break;
case RESIZE:
if(getwindow(display, Refnone) < 0)
sysfatal("%s: %r", argv0);
textinit(&chan->body);
textdraw(&chan->body);
break;
}
end:
unmount(nil, "/mnt/wsysnicks");
threadexitsall(0);
}