summaryrefslogtreecommitdiff
path: root/sys/src/cmd/mothra/libpanel/entry.c
blob: fc54b50409ce137ef6b5a279cb06c88e1dd11094 (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
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include <panel.h>
#include "pldefs.h"
#include <keyboard.h>

typedef struct Entry Entry;
struct Entry{
	char *entry;
	char *entp;
	char *eent;
	void (*hit)(Panel *, char *);
	Point minsize;
};
#define	SLACK	7	/* enough for one extra rune and ◀ and a nul */
char *pl_snarfentry(Panel *p){
	Entry *ep;
	int n;

	if(p->flags&USERFL)	/* no snarfing from password entry */
		return nil;
	ep=p->data;
	n=ep->entp-ep->entry;
	if(n<=0) return nil;
	return smprint("%.*s", n, ep->entry);
}
void pl_pasteentry(Panel *p, char *s){
	Entry *ep;
	char *e;
	int n, m;

	ep=p->data;
	n=ep->entp-ep->entry;
	m=strlen(s);
	e=pl_erealloc(ep->entry,n+m+SLACK);
	ep->entry=e;
	e+=n;
	strncpy(e, s, m);
	e+=m;
	*e='\0';
	ep->entp=ep->eent=e;
	pldraw(p, p->b);
}
void pl_drawentry(Panel *p){
	Rectangle r;
	Entry *ep;
	char *s;

	ep=p->data;
	r=pl_box(p->b, p->r, p->state);
	s=ep->entry;
	if(p->flags & USERFL){
		char *p;
		s=strdup(s);
		for(p=s; *p; p++)
			*p='*';
	}
	if(stringwidth(font, s)<=r.max.x-r.min.x)
		pl_drawicon(p->b, r, PLACEW, 0, s);
	else
		pl_drawicon(p->b, r, PLACEE, 0, s);
	if(s != ep->entry)
		free(s);
}
int pl_hitentry(Panel *p, Mouse *m){
	if((m->buttons&7)==1){
		plgrabkb(p);

		p->state=DOWN;
		pldraw(p, p->b);
		while(m->buttons&1){
			int old;
			old=m->buttons;
			if(display->bufp > display->buf)
				flushimage(display, 1);
			*m=emouse();
			if((old&7)==1){
				if((m->buttons&7)==3){
					Entry *ep;

					plsnarf(p);

					/* cut */
					ep=p->data;
					ep->entp=ep->entry;
					*ep->entp='\0';
					pldraw(p, p->b);
				}
				if((m->buttons&7)==5)
					plpaste(p);
			}
		}
		p->state=UP;
		pldraw(p, p->b);
	}
	return 0;
}
void pl_typeentry(Panel *p, Rune c){
	int n;
	Entry *ep;
	ep=p->data;
	switch(c){
	case '\n':
	case '\r':
		*ep->entp='\0';
		if(ep->hit) ep->hit(p, ep->entry);
		return;
	case Kesc:
		plsnarf(p);
		/* no break */
	case Kdel:	/* clear */
	case Knack:	/* ^U: erase line */
		ep->entp=ep->entry;
		*ep->entp='\0';
		break;
	case Kbs:	/* ^H: erase character */
		while(ep->entp!=ep->entry && !pl_rune1st(ep->entp[-1])) *--ep->entp='\0';
		if(ep->entp!=ep->entry) *--ep->entp='\0';
		break;
	case Ketb:	/* ^W: erase word */
		while(ep->entp!=ep->entry && !pl_idchar(ep->entp[-1]))
			--ep->entp;
		while(ep->entp!=ep->entry && pl_idchar(ep->entp[-1]))
			--ep->entp;
		*ep->entp='\0';
		break;
	default:
		if(c < 0x20 || (c & 0xFF00) == KF || (c & 0xFF00) == Spec)
			break;
		ep->entp+=runetochar(ep->entp, &c);
		if(ep->entp>ep->eent){
			n=ep->entp-ep->entry;
			ep->entry=pl_erealloc(ep->entry, n+100+SLACK);
			ep->entp=ep->entry+n;
			ep->eent=ep->entp+100;
		}
		*ep->entp='\0';
		break;
	}
	pldraw(p, p->b);
}
Point pl_getsizeentry(Panel *p, Point children){
	USED(children);
	return pl_boxsize(((Entry *)p->data)->minsize, p->state);
}
void pl_childspaceentry(Panel *p, Point *ul, Point *size){
	USED(p, ul, size);
}
void pl_freeentry(Panel *p){
	Entry *ep;
	ep = p->data;
	free(ep->entry);
	ep->entry = ep->eent = ep->entp = 0;
}
void plinitentry(Panel *v, int flags, int wid, char *str, void (*hit)(Panel *, char *)){
	int elen;
	Entry *ep;
	ep=v->data;
	v->flags=flags|LEAF;
	v->state=UP;
	v->draw=pl_drawentry;
	v->hit=pl_hitentry;
	v->type=pl_typeentry;
	v->getsize=pl_getsizeentry;
	v->childspace=pl_childspaceentry;
	ep->minsize=Pt(wid, font->height);
	v->free=pl_freeentry;
	v->snarf=pl_snarfentry;
	v->paste=pl_pasteentry;
	elen=100;
	if(str) elen+=strlen(str);
	ep->entry=pl_erealloc(ep->entry, elen+SLACK);
	ep->eent=ep->entry+elen;
	strecpy(ep->entry, ep->eent, str ? str : "");
	ep->entp=ep->entry+strlen(ep->entry);
	ep->hit=hit;
	v->kind="entry";
}
Panel *plentry(Panel *parent, int flags, int wid, char *str, void (*hit)(Panel *, char *)){
	Panel *v;
	v=pl_newpanel(parent, sizeof(Entry));
	plinitentry(v, flags, wid, str, hit);
	return v;
}
char *plentryval(Panel *p){
	Entry *ep;
	ep=p->data;
	*ep->entp='\0';
	return ep->entry;
}