From mboxrd@z Thu Jan 1 00:00:00 1970 From: erik quanstrom Date: Wed, 19 Dec 2012 08:32:26 -0500 To: 9fans@9fans.net Message-ID: In-Reply-To: <62328632c05339f73df807068c511920@rei2.9hal> References: <62328632c05339f73df807068c511920@rei2.9hal> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Subject: Re: [9fans] Good sample GUI code (window creation, management, Topicbox-Message-UUID: f7f28884-ead7-11e9-9d60-3106f5b1d025 i don't think there are any tricks. here's a pretty minimal program that displays colors. usage might be: color 0x448844FF 0x000055FF - erik ---- #include #include #include #include #include #include static Mousectl *m; static long colors[1024]; static Image *c[1024]; static int ncolor; int rflag; void redraw(int) { int i, x, y; ulong chan; chan = rflag ? CHAN3(CGreen, 8, CBlue, 8, CRed, 8) : RGB24; if(c[0] == 0) for(i = 0; i < ncolor; i++){ c[i] = allocimage(display, Rect(0, 0, 1, 1), chan, 1, colors[i]); if(c[i] == 0) sysfatal("allocimage: %r"); } x = screen->r.min.x; y = screen->r.min.y; for(i = 0; i < ncolor; i++){ draw(screen, Rect(x, y, x+48, y+48), c[i], nil, ZP); x += 50; if(x > screen->r.max.x-60){ x = screen->r.min.x; y += 50; } } } void resizeproc(void *) { for(; recv(m->resizec, 0);){ if(getwindow(display, Refnone) < 0){ fprint(2, "test: can't reattach to window\n"); threadexitsall("resize"); } redraw(1); flushimage(display, 1); } } static void drawe(Display*, char *e) { fprint(2, "%s\n", e); threadexitsall("libdraw"); } int init(void) { // newwindow("-dx 300 -dy 300"); if(initdraw(drawe, 0, "test") < 0){ fprint(2, "test: initdraw failed: %r"); return -1; } m = initmouse(0, screen); return 0; } void kbdproc(void*) { Keyboardctl *k; k = initkeyboard(0); recv(k->c, 0); threadexitsall(""); } void mouseproc(void*) { while(readmouse(m) >= 0) ; threadexitsall("readmouse"); } void usage(void) { fprint(2, "usage: color [-r] color1 ...\n"); threadexitsall("usage"); } void threadmain(int argc, char *argv[]) { ARGBEGIN{ case 'r': rflag ^= rflag; break; default: usage(); }ARGEND; if(argc == 0) usage(); if(argc > nelem(colors)) argc = nelem(colors); for(ncolor = 0; ncolor < argc; ncolor++) colors[ncolor] = strtoul(argv[ncolor], 0, 16); if(init() < 0) threadexitsall("initdraw"); redraw(1); proccreate(kbdproc, 0, 1024); proccreate(mouseproc, 0, 8192); proccreate(resizeproc, 0, 8*8192); for(;;) sleep(10000); }