summaryrefslogtreecommitdiff
path: root/sys/src/cmd/spred/spred.c
blob: 08472c205eed3e0239fcb315fe4487bd1985f830 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
#include <frame.h>
#include <cursor.h>
#include "dat.h"
#include "fns.h"

Mousectl *mc;
Keyboardctl *kc;
int quitok;

enum {
	ZEROX,
	RESIZE,
	CLOSE,
	WRITE,
	QUIT,
	WIN
};

int 
quit(void)
{
	File *f;
	
	if(!quitok)
		for(f = flist.next; f != &flist; f = f->next)
			if(f->change > 0){
				cmdprint("?\n");
				quitok = 1;
				return 0;
			}
	return -1;
}

static char *
menugen(int n)
{
	File *f;
	static int mw;
	static char buf[512];
	int rc;
	char *p;

	switch(n){
	case ZEROX: return "zerox";
	case CLOSE: return "close";
	case RESIZE: return "resize";
	case WRITE: return "write";
	case QUIT: return "quit";
	}
	if(n < WIN)
		sysfatal("menugen: no string for n=%d", n);
	n -= WIN;
	if(n == 0){
		mw = 0;
		for(f = flist.next; f != &flist; f = f->next){
			rc = filtitlelen(f);
			if(rc > mw)
				mw = rc;
		}
		return "~~spred~~";
	}
	for(f = flist.next; f != &flist; f = f->next)
		if(--n == 0){
			p = filtitle(f, buf, buf + sizeof(buf));
			rc = mw - utflen(buf);
			if(p + rc >= buf + sizeof(buf))
				rc = buf + sizeof(buf) - p - 1;
			memset(p, ' ', rc);
			p[rc] = 0;
			return buf;
		}
	return nil;
	
}

static int
rmb(void)
{
	static Menu menu = {nil, menugen};
	int n;
	Win *w;
	File *f;

	if(actw != nil && actw->tab->rmb != nil && actw->tab->rmb(actw, mc) >= 0)
		return 0;
	n = menuhit(3, mc, &menu, nil);
	if(n < 0)
		return 0;
	switch(n){
	case ZEROX:
		w = winsel(mc, 3);
		if(w != nil)
			winzerox(w, mc);
		return 0;
	case CLOSE:
		w = winsel(mc, 3);
		if(w != nil)
			winclose(w);
		return 0;
	case RESIZE:
		winresize(winsel(mc, 3), mc);
		return 0;
	case WRITE:
		w = winsel(mc, 3);
		if(w != nil)
			winwrite(w, nil);
		return 0;
	case QUIT:
		return quit();
	}
	if(n < WIN)
		sysfatal("rmb: no action for n=%d", n);
	if(n == 0){
		setfocus(cmdw);
		return 0;
	}
	n -= WIN;
	for(f = flist.next; f != &flist; f = f->next)
		if(--n == 0){
			if(f->wins.wnext == &f->wins){
				newwinsel(f->type, mc, f);
				return 0;
			}
			for(w = f->wins.wnext; w != &f->wins && w != actw; w = w->wnext)
				;
			if(w->wnext == &f->wins)
				w = w->wnext;
			setfocus(w->wnext);
			return 0;
		}
	return 0;
}

static void
loop(void)
{
	Rune r;
	int n;

	Alt a[] = {
		{mc->c, &mc->Mouse, CHANRCV},
		{kc->c, &r, CHANRCV},
		{mc->resizec, &n, CHANRCV},
		{nil, nil, CHANEND}
	};
	
	for(;;){
		flushimage(display, 1);
		switch(alt(a)){
		case 0:
			if((mc->buttons & 1) != 0)
				winclick(mc);
			if((mc->buttons & 2) != 0)
				if(actw != nil && actw->tab->menu != nil)
					actw->tab->menu(actw, mc);
			if((mc->buttons & 4) != 0)
				if(rmb() < 0)
					return;
			break;
		case 1:
			if(actw != nil && actw->tab->key != nil)
				actw->tab->key(actw, r);
			break;
		case 2:
			resize();
			break;
		}
	}
}

void
threadmain(int argc, char **argv)
{
	ARGBEGIN {
	default:
		;
	} ARGEND;
	
	quotefmtinstall();
	if(initdraw(nil, nil, nil) < 0)
		sysfatal("initdraw: %r");
	initwin();
	mc = initmouse(nil, screen);
	if(mc == nil)
		sysfatal("initmouse: %r");
	kc = initkeyboard(nil);
	if(kc == nil)
		sysfatal("initkeyboard: %r");
	loop();
	threadexitsall(nil);
}

Cursor crosscursor = {
	{-7, -7},
	{0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0,
	 0x03, 0xC0, 0x03, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF,
	 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0xC0, 0x03, 0xC0,
	 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, },
	{0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
	 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x7F, 0xFE,
	 0x7F, 0xFE, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
	 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00, }
};