2 This code was taken from 9front repository (https://code.9front.org/hg/plan9front).
3 It is subject to license from 9front, below is a reproduction of the license.
5 Copyright (c) 20XX 9front
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 /* additional keyboard codes needed - defined here to avoid API change */
41 eenter(char *ask, char *buf, int len, Mouse *m)
43 int done, down, tick, n, h, w, l, i;
44 Image *b, *save, *backcol, *bordcol;
51 backcol = allocimagemix(display, DPurpleblue, DWhite);
52 bordcol = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
53 if(backcol == nil || bordcol == nil)
74 p = stringsize(font, " ");
80 replclipr(b, 0, b->r);
84 p = stringsize(font, buf ? buf : "");
87 p.x += stringwidth(font, ask);
89 r = rectaddpt(insetrect(Rpt(ZP, p), -4), o);
94 if(r.min.x < screen->r.min.x)
95 p.x = screen->r.min.x - r.min.x;
96 if(r.min.y < screen->r.min.y)
97 p.y = screen->r.min.y - r.min.y;
100 if(r.max.x > screen->r.max.x)
101 p.x = r.max.x - screen->r.max.x;
102 if(r.max.y > screen->r.max.y)
103 p.y = r.max.y - screen->r.max.y;
106 r = insetrect(r, -2);
108 save = allocimage(display, r, b->chan, 0, DNofill);
113 draw(save, r, b, nil, r.min);
115 draw(b, r, backcol, nil, ZP);
116 border(b, r, 2, bordcol, ZP);
117 p = addpt(r.min, Pt(6, 6));
119 p = string(b, p, bordcol, ZP, font, ask);
124 p = stringn(b, p, display->black, ZP, font, buf, utfnlen(buf, tick));
125 draw(b, Rect(p.x-1, p.y, p.x+2, p.y+3), display->black, nil, ZP);
126 draw(b, Rect(p.x, p.y, p.x+1, p.y+h), display->black, nil, ZP);
127 draw(b, Rect(p.x-1, p.y+h-3, p.x+2, p.y+h), display->black, nil, ZP);
128 p = string(b, p, display->black, ZP, font, buf+tick);
130 flushimage(display, 1);
140 /* screen might have been resized */
141 if(b != screen || !eqrect(screen->clipr, sc)){
147 replclipr(b, 0, b->r);
156 if(buf == nil || k == Keof || k == '\n'){
160 if(k == Knack || k == Kesc){
162 buf[n = tick = 0] = 0;
165 if(k == Ksoh || k == Khome){
169 if(k == Kenq || k == Kend){
175 tick += chartorune(&k, buf+tick);
179 for(i = 0; i < n; i += l){
180 l = chartorune(&k, buf+tick);
192 strchr(" !\"#$%&'()*+,-./:;<=>?@`[\\]^{|}~", buf[tick-1]))
201 for(i = 0; i < n; i += l){
202 l = chartorune(&k, buf+i);
204 memmove(buf+i, buf+i+l, n - (i+l));
212 if(k < 0x20 || k == Kdel || (k & 0xFF00) == KF || (k & 0xFF00) == Spec)
214 if((len-n) <= (l = runelen(k)))
216 memmove(buf+tick+l, buf+tick, n - tick);
217 runetochar(buf+tick, &k);
223 if(!ptinrect(m->xy, r)){
229 if(buf && m->xy.x >= (t.x - w)){
231 for(i = 0; i < n; i += l){
232 l = chartorune(&k, buf+i);
233 t.x += stringnwidth(font, buf+i, 1);
245 draw(b, save->r, save, nil, save->r.min);
255 flushimage(display, 1);