about summary refs log tree commit diff stats
path: root/p9c/scratch/menu.c
blob: 97004d6d8af7edb623f96b31b4c55a4c433eecdf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>

char* options1[] = {"Middle Click", "", "Paste", "Snarf", "Exit", 0};
char* options2[] = {"Right Click", "", "Option3", "Option4", "Exit", 0};

Menu middlemenu = {options1};
Menu rightmenu = {options2};

void
eresized(int new)
{
	if(new&& getwindow(display, Refnone) < 0)
		sysfatal("can't reattach to window");
}

void
dopaste(void)
{
	int f;
	if((f = open("/dev/snarf", OREAD)) >= 0) {
		char body[30];
		read(f, body, 30);
		print("Paste: %s\n", body);
		close(f);
	}
}

void
dosnarf(void)
{
	int f;
	if((f = open("/dev/snarf", OWRITE)) >= 0) {
		char* body = "some text";
		write(f, body, strlen(body));
		print("Snarf: %s\n", body);
		close(f);
	}
}

void
main(int argc, char* argv[])
{
	// USED(argc, argv);

	Event ev;
	int e;

	initdraw(0, 0, "Example: Menu");
	eresized(0);
	einit(Emouse);

	/* Main event loop */
	for(;;) {
		e = event(&ev);
		/* Middle Click */
		if((e == Emouse) && (ev.mouse.buttons & 3)) {
			if(emenuhit(2, &ev.mouse, &middlemenu) == 2)
				dopaste();
			if(emenuhit(2, &ev.mouse, &middlemenu) == 3)
				dosnarf();
			if(emenuhit(2, &ev.mouse, &middlemenu) == 4)
				exits(nil);
		}
		/* Right Click */
		else if((e == Emouse) && (ev.mouse.buttons & 4)) {
			if(emenuhit(3, &ev.mouse, &rightmenu) == 2)
				print("Pressed Option 3\n");
			if(emenuhit(3, &ev.mouse, &rightmenu) == 3)
				print("Pressed Option 4\n");
			if(emenuhit(3, &ev.mouse, &rightmenu) == 4)
				exits(nil);
		}
	}
}