ref: e74727d7e806863494a92d4b5cdcefdeee7495e9
dir: /cmd/factory/factory.c/
#include <u.h> #include <libc.h> #include <draw.h> #include <thread.h> #include <mouse.h> #include <keyboard.h> #include <String.h> #include <widget.h> void usage(void) { fprint(2, "usage: %s", argv0); exits("usage"); } void threadmain(int argc, char **argv) { Widgetctl *wctl; Widgetmsg *msg; Menumsg mmsg; Widget *root; Rune rune; ARGBEGIN { default: usage(); } ARGEND; if(initdraw(nil, nil, "widget factory") < 0) sysfatal("initdraw: %r"); enum { MIDDLE_EXIT }; char *mentries[] = { [MIDDLE_EXIT] = "exit", nil, }; Menu middle = { .item = mentries, .lasthit = -1 }; Gridentry *entries[] = { newgridentry( Pt(0, 0), Pt(1, 1), newtextbutton(nil, "hello, world!") ), newgridentry( Pt(0, 1), Pt(2, 1), newtextbutton(nil, "haha") ), nil }; root = newgrid( GRID_DYNAMIC_SIZE, entries ); // root->content->maxsize = Pt(256, 128); if((wctl = initwidget(screen, nil, nil, root, FORWARD_KBD)) == nil) sysfatal("initwidget: %r"); wctl->middle = &middle; enum { MESSAGE, RESIZE, KEYBOARD, MENU }; Alt chans[] = { { wctl->c, &msg, CHANRCV }, { wctl->resizec, nil, CHANRCV }, { wctl->kbdc, &rune, CHANRCV }, { wctl->menuc, &mmsg, CHANRCV }, { nil, nil, CHANEND } }; for(;;) switch(alt(chans)) { case MESSAGE: switch(msg->what) { case M_BUTTON_PRESSED: // print("button %d was pressed!\n", msg->sender->id); break; case M_BUTTON_RELEASED: // print("button %d was released!\n", msg->sender->id); break; } free(msg); break; case KEYBOARD: if(rune == '') goto end; break; case MENU: switch(mmsg.button) { case M_MIDDLE: switch(mmsg.hit) { case MIDDLE_EXIT: goto end; } break; } break; case RESIZE: if(getwindow(display, Refnone) < 0) sysfatal("getwindow: cannot resize: %r"); redrawwctl(wctl); break; } end: closewidget(wctl); freewidget(root); exits(0); }