shithub: map

ref: 5b8b0cbeb3461b0a710584ee77f1cb6c501a5406
dir: /map.c/

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

NWindow *mainwindow;
NImage *nimage;

Image *testimage;

int debug;

void
inittestimage(void)
{
	Image *r, *g;
	testimage = allocimage(display, Rect(0, 0, 100, 100), screen->chan, 1, DWhite);
	r = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DRed);
	g = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DGreen);
	draw(testimage, Rect(0, 0, 50, 50), r, nil, ZP);
	draw(testimage, Rect(50, 50, 100, 100), g, nil, ZP);
	freeimage(r);
	freeimage(g);
}

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

int
exitclicked(Mouse, Nelem *el, void*)
{
	exits(nil);
}

static void
traperrout(void)
{
	int p[2];
	pipe(p);
	dup(p[0], 2);
	int fd = create("/srv/map.errout", OWRITE|ORCLOSE, 0666);
	if (fd < 0)
		sysfatal("create: %r");
	fprint(fd, "%d\n", p[1]);
	close(p[1]);
}

void
main(int argc, char **argv)
{
	Event ev;
	int z = 0;
	char *url;
	
	ARGBEGIN{
	case 'd':
		debug++;
	}ARGEND;
	
	if (initdraw(nil, nil, "map") < 0)
		sysfatal("%r");
	
	if (debug)
		traperrout();
	
	nateborders = 1;
	
	einit(Emouse);
	nateinit();
	
	inittestimage();
	
	NAssign(NWindow, &mainwindow, New_Window(nil))
	->MakeRoot()
	->Slot(
		New_VBox(nil)
		->Slot(
			New_HBox("menu")
			->AutoHeight(1)
			->Slot(
				New_Button(nil)
				->AutoSize(1)
				->Border(1, display->black)
				->Label("Exit")
				->OnClick(exitclicked, nil)
			)
			->Slot(
				New_Button(nil)
				->AutoSize(1)
				->Label("Test")
				->Border(1, display->black)
			)
		)
		->Slot(
			New_Box(nil)
			->Border(1, display->black)
			->Size(Pt(200, 300))
			->Slot(
				NAssign(NImage, &nimage, New_Image("map"))
				->Image(testimage)
			)
		)
		->Slot(
			New_Box(nil)
			->Border(1, display->black)
			->Size(Pt(500, 30))
			->Slot(
				New_Label(nil)
				->Label("Status Line")
			)
		)
	);
	
	nateredraw(1);
	
	for (;;) {
		switch (event(&ev)) {
		case Emouse:
			if (ev.mouse.buttons & 4)
				exits(nil);
			else
				natemouseevent(ev.mouse);
			break;
		}
	}
	
	GPos loc = getlocation();
	GBundle bundle = getbundle(loc, z);
	GProvider *prov = getprovider();
	
	url = prov->formaturl(bundle);
	print("%s\n", url);
}