shithub: libnate

ref: c9e889a6d73bcfcd63a93a8f74732411304ef257
dir: /test/ntest.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include "../nate.h"
#include "../n_window.h"
#include "../n_hbox.h"
#include "../n_vbox.h"
#include "../n_box.h"
#include "../n_label.h"
#include "../n_image.h"
#include "../n_vlist.h"

//#define T_VBOX
//#define T_HBOX
//#define T_BOX
//#define T_GRID
#define T_LIST

char*
getlabel(void)
{
	return "ABC";
}

NBox *box1;
NBox *box2;

NArray listsource;

typedef struct Item Item;
struct Item {
	char *label;
	char *num;
};

Item items[] = {
	{ "ABC", "5" },
	{ "DEF", "8" },
	{ "GHI", "100" },
};

Nelem*
generatelistitem(void *item, int n, void*)
{
	Nelem *el;
	Item *it = item;
	
	NAssign(NHBoxAccessors, &el, New_HBox(nil))
	->Slot(NSlot(),
		New_Label(nil)
		->Label(it->label)
	)
	->Slot(NSlot(),
		New_Label(nil)
		->Label(it->num)
	);
	return el;
}

NVList *vlist;

int
callclick(Mouse, Nelem* el, void*)
{
	int id = 0;
	if (el == box1)
		id = 1;
	if (el == box2)
		id = 2;
	fprint(2, "click: %s (%d)\n", el->type, id);
	return 1;
}

void
eresized(int new)
{
	if (new && getwindow(display, Refnone) < 0)
		sysfatal("getwindow: %r");
	
	nateredraw(1);
}

void
main(int argc, char **argv)
{
	USED(argc, argv);
	Nelem* mainwindow;
	Event ev;
	int e;
	
	if (initdraw(nil, nil, "nate test") < 0)
		sysfatal("initdraw: %r");
	
	/* send 2 output to /srv/ntest */
	if (1) {
		int p[2];
		pipe(p);
		dup(p[0], 2);
		int fd = create("/srv/ntest", OWRITE|ORCLOSE, 0666);
		if (fd < 0)
			sysfatal("create: %r");
		fprint(fd, "%d\n", p[1]);
		close(p[1]);
	}
	
	/* debug nate */
	nateborders = 1;
	//natetracehit = 1;
	natetracedraw = 1;
	natedebugfd = 2;
	
	einit(Emouse);
	nateinit();
	
	Image* red = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DRed);
	Image* green = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DGreen);
	Image* blue = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DBlue);
	
#ifdef T_VBOX
	NAssign(NWindowAccessors, &mainwindow, New_Window("window"))
	->MakeRoot()
	->Slot(NSlot(),
		New_VBox("vbox")
		->Slot(NSlot(),
			New_Label("first_label")
			->Label("first")
		)
		->Slot(NSlot(),
			New_Label("second_label")
			->Label("second")
		)
	);
#endif

#ifdef T_HBOX
	NAssign(NWindowAccessors, &mainwindow, New_Window("window"))
	->MakeRoot()
	->Slot(NSlot(),
		New_HBox("hbox")
		->Slot(NSlot(),
			New_Label("first_label")
			->Label("first")
		)
		->Slot(NSlot(),
			New_Label("second_label")
			->Label("second")
		)
	);
#endif

#ifdef T_GRID
	NAssign(NWindowAccessors, &mainwindow, New_Window("window"))
	->MakeRoot()
	->Slot(NSlot(),
		New_VBox("vbox")
		->Slot(NSlot(),
			New_HBox("hbox1")
			->Slot(NSlot(),
				New_Label("l1")
				->Label("first")
			)
			->Slot(NSlot(),
				New_Label("l2")
				->Label("second")
			)
		)
		->Slot(NSlot(),
			New_HBox("hbox2")
			->Slot(NSlot(),
				New_Label("l3")
				->Label("third")
			)
			->Slot(NSlot(),
				New_Label("l4")
				->Label("fourth")
			)
		)
	);
#endif

#ifdef T_BOX
	NAssign(NWindowAccessors, &mainwindow, New_Window("window"))
	->MakeRoot()
	->Slot(NSlot(),
		New_HBox("hbox")
		->Slot(NSlot(),
			New_Box("fillbox")
			->Border(5, green)
			->Size(Pt(100, 100))
			->Padding(NMargin2(5, 5))
			->Slot(NSlot(),
				New_Label(nil)
				->Label("fill")
				->Align(CENTER)
			)
		)
		->Slot(NSlotx(LEFT, 0),
			New_Box("nofillbox")
			->Border(5, green)
			->Size(Pt(100, 100))
			->Padding(NMargin2(5, 5))
			->Slot(NSlot(),
				New_Label(nil)
				->Label("nofill")
				->Align(CENTER)
			)
		)
		->Slot(NSlotx(LEFT, FILLX),
			New_Box("fillxbox")
			->Border(5, green)
			->Size(Pt(100, 100))
			->Padding(NMargin2(5, 5))
			->Slot(NSlot(),
				New_Label(nil)
				->Label("fillx")
				->Align(CENTER)
			)
		)
		->Slot(NSlotx(LEFT, FILLY),
			New_Box("fillybox")
			->Border(5, green)
			->Size(Pt(100, 100))
			->Padding(NMargin2(5, 5))
			->Slot(NSlot(),
				New_Label(nil)
				->Label("filly")
				->Align(CENTER)
			)
		)
	);
#endif

#ifdef T_LIST
	listsource.data = items;
	listsource.size = sizeof(Item);
	listsource.num = sizeof(items)/sizeof(*items);

	NAssign(NWindowAccessors, &mainwindow, New_Window("window"))
	->MakeRoot()
	->Slot(NSlot(),
		NAssign(NVListAccessors, &vlist, New_VList("vlist"))
		->Source(&listsource)
		->GenerateWidget(generatelistitem, nil)
	);
	
	vlist->notifyupdate(vlist);
#endif
	
	eresized(0);
	
	for (;;) {
		e = event(&ev);
		
		switch (e) {
		case Emouse:
			natemouseevent(ev.mouse);
			break;
		default:
			break;
		}
	}
}