shithub: widget

ref: 1d84452255fa2d3c41ac2596f39d61d7e3b27b12
dir: /cmd/factory/factory.c/

View raw version
#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;
	Box *root;
	Rune rune;

	ARGBEGIN {
	default:
		usage();
	} ARGEND;

	if(initdraw(nil, nil, "widget factory") < 0)
		sysfatal("initdraw: %r");

	root = newbox(
		newtextbutton(nil, "hello, world!"), 0
	);

	root->maxsize = Pt(256, 128);

	if((wctl = initwidget(screen, nil, nil, root, FORWARD_KBD)) == nil)
		sysfatal("initwidget: %r");

	enum
	{
		MESSAGE, RESIZE, KEYBOARD
	};

	Alt chans[] = 
	{
		{ wctl->c,			&msg,	CHANRCV },
		{ wctl->resizec,	nil,	CHANRCV },
		{ wctl->kbdc,		&rune,	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 RESIZE:
			if(getwindow(display, Refnone) < 0)
				sysfatal("getwindow: cannot resize: %r");
			redrawwctl(wctl);
			break;
		}

end:
	closewidget(wctl);
	freewidget(root);

	exits(0);
}