]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/samterm/flayer.c
/sys/src/cmd/ndb/dns.h:
[plan9front.git] / sys / src / cmd / samterm / flayer.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <mouse.h>
6 #include <keyboard.h>
7 #include <frame.h>
8 #include "flayer.h"
9 #include "samterm.h"
10
11 #define DELTA   10
12
13 static Flayer   **llist;        /* front to back */
14 static int      nllist;
15 static int      nlalloc;
16 static Rectangle lDrect;
17
18 Vis             visibility(Flayer *);
19 void            newvisibilities(int);
20 void            llinsert(Flayer*);
21 void            lldelete(Flayer*);
22
23 Image   *maincols[NCOL];
24 Image   *cmdcols[NCOL];
25
26 void
27 flstart(Rectangle r)
28 {
29         lDrect = r;
30
31         /* Main text is yellowish */
32         maincols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
33         maincols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
34         maincols[BORD] = allocimage(display, Rect(0,0,2,2), screen->chan, 1, DYellowgreen);
35         maincols[TEXT] = display->black;
36         maincols[HTEXT] = display->black;
37
38         /* Command text is blueish */
39         cmdcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
40         cmdcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
41         cmdcols[BORD] = allocimage(display, Rect(0,0,2,2), screen->chan, 1, DPurpleblue);
42         cmdcols[TEXT] = display->black;
43         cmdcols[HTEXT] = display->black;
44 }
45
46 void
47 flnew(Flayer *l, Rune *(*fn)(Flayer*, long, ulong*), int u0, void *u1)
48 {
49         if(nllist == nlalloc){
50                 nlalloc += DELTA;
51                 llist = realloc(llist, nlalloc*sizeof(Flayer**));
52                 if(llist == 0)
53                         panic("flnew");
54         }
55         l->textfn = fn;
56         l->user0 = u0;
57         l->user1 = u1;
58         l->lastsr = ZR;
59         llinsert(l);
60 }
61
62 Rectangle
63 flrect(Flayer *l, Rectangle r)
64 {
65         rectclip(&r, lDrect);
66         l->entire = r;
67         l->scroll = insetrect(r, FLMARGIN);
68         r.min.x =
69          l->scroll.max.x = r.min.x+FLMARGIN+FLSCROLLWID+(FLGAP-FLMARGIN);
70         return r;
71 }
72
73 void
74 flinit(Flayer *l, Rectangle r, Font *ft, Image **cols)
75 {
76         lldelete(l);
77         llinsert(l);
78         l->visible = All;
79         l->origin = l->p0 = l->p1 = 0;
80         frinit(&l->f, insetrect(flrect(l, r), FLMARGIN), ft, screen, cols);
81         l->f.maxtab = maxtab*stringwidth(ft, "0");
82         newvisibilities(1);
83         draw(screen, l->entire, l->f.cols[BACK], nil, ZP);
84         scrdraw(l, 0L);
85         flborder(l, 0);
86 }
87
88 void
89 flclose(Flayer *l)
90 {
91         if(l->visible == All)
92                 draw(screen, l->entire, display->white, nil, ZP);
93         else if(l->visible == Some){
94                 if(l->f.b == 0)
95                         l->f.b = allocimage(display, l->entire, screen->chan, 0, DNofill);
96                 if(l->f.b){
97                         draw(l->f.b, l->entire, display->white, nil, ZP);
98                         flrefresh(l, l->entire, 0);
99                 }
100         }
101         frclear(&l->f, 1);
102         lldelete(l);
103         if(l->f.b && l->visible!=All)
104                 freeimage(l->f.b);
105         l->textfn = 0;
106         newvisibilities(1);
107 }
108
109 void
110 flborder(Flayer *l, int wide)
111 {
112         if(flprepare(l)){
113                 border(l->f.b, l->entire, FLMARGIN, l->f.cols[BACK], ZP);
114                 border(l->f.b, l->entire, wide? FLMARGIN : 1, l->f.cols[BORD], ZP);
115                 if(l->visible==Some)
116                         flrefresh(l, l->entire, 0);
117         }
118 }
119
120 Flayer *
121 flwhich(Point p)
122 {
123         int i;
124
125         if(p.x==0 && p.y==0)
126                 return nllist? llist[0] : 0;
127         for(i=0; i<nllist; i++)
128                 if(ptinrect(p, llist[i]->entire))
129                         return llist[i];
130         return 0;
131 }
132
133 void
134 flupfront(Flayer *l)
135 {
136         int v = l->visible;
137
138         lldelete(l);
139         llinsert(l);
140         if(v!=All)
141                 newvisibilities(0);
142 }
143
144 void
145 newvisibilities(int redraw)
146         /* if redraw false, we know it's a flupfront, and needn't
147          * redraw anyone becoming partially covered */
148 {
149         int i;
150         Vis ov;
151         Flayer *l;
152
153         for(i = 0; i<nllist; i++){
154                 l = llist[i];
155                 l->lastsr = ZR; /* make sure scroll bar gets redrawn */
156                 ov = l->visible;
157                 l->visible = visibility(l);
158 #define V(a, b) (((a)<<2)|((b)))
159                 switch(V(ov, l->visible)){
160                 case V(Some, None):
161                         if(l->f.b)
162                                 freeimage(l->f.b);
163                 case V(All, None):
164                 case V(All, Some):
165                         l->f.b = 0;
166                         frclear(&l->f, 0);
167                         break;
168
169                 case V(Some, Some):
170                         if(l->f.b==0 && redraw)
171                 case V(None, Some):
172                                 flprepare(l);
173                         if(l->f.b && redraw){
174                                 flrefresh(l, l->entire, 0);
175                                 freeimage(l->f.b);
176                                 l->f.b = 0;
177                                 frclear(&l->f, 0);
178                         }
179                 case V(None, None):
180                 case V(All, All):
181                         break;
182
183                 case V(Some, All):
184                         if(l->f.b){
185                                 draw(screen, l->entire, l->f.b, nil, l->entire.min);
186                                 freeimage(l->f.b);
187                                 l->f.b = screen;
188                                 break;
189                         }
190                 case V(None, All):
191                         flprepare(l);
192                         break;
193                 }
194                 if(ov==None && l->visible!=None)
195                         flnewlyvisible(l);
196         }
197 }
198
199 void
200 llinsert(Flayer *l)
201 {
202         int i;
203         for(i=nllist; i>0; --i)
204                 llist[i]=llist[i-1];
205         llist[0]=l;
206         nllist++;
207 }
208
209 void
210 lldelete(Flayer *l)
211 {
212         int i;
213
214         for(i=0; i<nllist; i++)
215                 if(llist[i]==l){
216                         --nllist;
217                         for(; i<nllist; i++)
218                                 llist[i] = llist[i+1];
219                         return;
220                 }
221         panic("lldelete");
222 }
223
224 void
225 flinsert(Flayer *l, Rune *sp, Rune *ep, long p0)
226 {
227         if(flprepare(l)){
228                 frinsert(&l->f, sp, ep, p0-l->origin);
229                 scrdraw(l, scrtotal(l));
230                 if(l->visible==Some)
231                         flrefresh(l, l->entire, 0);
232         }
233 }
234
235 void
236 fldelete(Flayer *l, long p0, long p1)
237 {
238         if(flprepare(l)){
239                 p0 -= l->origin;
240                 if(p0 < 0)
241                         p0 = 0;
242                 p1 -= l->origin;
243                 if(p1<0)
244                         p1 = 0;
245                 frdelete(&l->f, p0, p1);
246                 scrdraw(l, scrtotal(l));
247                 if(l->visible==Some)
248                         flrefresh(l, l->entire, 0);
249         }
250 }
251
252 int
253 flselect(Flayer *l, ulong *p)
254 {
255         static int clickcount;
256         static Point clickpt = {-10, -10};
257         int dt, dx, dy;
258
259         if(l->visible!=All)
260                 flupfront(l);
261         dt = mousep->msec - l->click;
262         dx = abs(mousep->xy.x - clickpt.x);
263         dy = abs(mousep->xy.y - clickpt.y);
264         *p = frcharofpt(&l->f, mousep->xy) + l->origin;
265
266         l->click = mousep->msec;
267         clickpt = mousep->xy;
268
269         if(dx < 3 && dy < 3 && dt < Clicktime && clickcount < 3)
270                 return ++clickcount;
271         clickcount = 0;
272
273         frselect(&l->f, mousectl);
274         l->p0 = l->f.p0+l->origin;
275         l->p1 = l->f.p1+l->origin;
276         return 0;
277 }
278
279 void
280 flsetselect(Flayer *l, long p0, long p1)
281 {
282         ulong fp0, fp1;
283
284         if(l->visible==None || !flprepare(l)){
285                 l->p0 = p0, l->p1 = p1;
286                 return;
287         }
288         l->p0 = p0, l->p1 = p1;
289         flfp0p1(l, &fp0, &fp1);
290         if(fp0==l->f.p0 && fp1==l->f.p1)
291                 return;
292
293         if(fp1<=l->f.p0 || fp0>=l->f.p1 || l->f.p0==l->f.p1 || fp0==fp1){
294                 /* no overlap or trivial repainting */
295                 frdrawsel(&l->f, frptofchar(&l->f, l->f.p0), l->f.p0, l->f.p1, 0);
296                 frdrawsel(&l->f, frptofchar(&l->f, fp0), fp0, fp1, 1);
297                 goto Refresh;
298         }
299         /* the current selection and the desired selection overlap and are both non-empty */
300         if(fp0 < l->f.p0){
301                 /* extend selection backwards */
302                 frdrawsel(&l->f, frptofchar(&l->f, fp0), fp0, l->f.p0, 1);
303         }else if(fp0 > l->f.p0){
304                 /* trim first part of selection */
305                 frdrawsel(&l->f, frptofchar(&l->f, l->f.p0), l->f.p0, fp0, 0);
306         }
307         if(fp1 > l->f.p1){
308                 /* extend selection forwards */
309                 frdrawsel(&l->f, frptofchar(&l->f, l->f.p1), l->f.p1, fp1, 1);
310         }else if(fp1 < l->f.p1){
311                 /* trim last part of selection */
312                 frdrawsel(&l->f, frptofchar(&l->f, fp1), fp1, l->f.p1, 0);
313         }
314
315     Refresh:
316         l->f.p0 = fp0;
317         l->f.p1 = fp1;
318         if(l->visible==Some)
319                 flrefresh(l, l->entire, 0);
320 }
321
322 void
323 flfp0p1(Flayer *l, ulong *pp0, ulong *pp1)
324 {
325         long p0 = l->p0-l->origin, p1 = l->p1-l->origin;
326
327         if(p0 < 0)
328                 p0 = 0;
329         if(p1 < 0)
330                 p1 = 0;
331         if(p0 > l->f.nchars)
332                 p0 = l->f.nchars;
333         if(p1 > l->f.nchars)
334                 p1 = l->f.nchars;
335         *pp0 = p0;
336         *pp1 = p1;
337 }
338
339 Rectangle
340 rscale(Rectangle r, Point old, Point new)
341 {
342         r.min.x = r.min.x*new.x/old.x;
343         r.min.y = r.min.y*new.y/old.y;
344         r.max.x = r.max.x*new.x/old.x;
345         r.max.y = r.max.y*new.y/old.y;
346         return r;
347 }
348
349 void
350 flresize(Rectangle dr)
351 {
352         int i;
353         Flayer *l;
354         Frame *f;
355         Rectangle r, olDrect;
356         int move;
357
358         olDrect = lDrect;
359         lDrect = dr;
360         move = 0;
361         /* no moving on rio; must repaint */
362         if(0 && Dx(dr)==Dx(olDrect) && Dy(dr)==Dy(olDrect))
363                 move = 1;
364         else
365                 draw(screen, lDrect, display->white, nil, ZP);
366         for(i=0; i<nllist; i++){
367                 l = llist[i];
368                 l->lastsr = ZR;
369                 f = &l->f;
370                 if(move)
371                         r = rectaddpt(rectsubpt(l->entire, olDrect.min), dr.min);
372                 else{
373                         r = rectaddpt(rscale(rectsubpt(l->entire, olDrect.min),
374                                 subpt(olDrect.max, olDrect.min),
375                                 subpt(dr.max, dr.min)), dr.min);
376                         if(l->visible==Some && f->b){
377                                 freeimage(f->b);
378                                 frclear(f, 0);
379                         }
380                         f->b = 0;
381                         if(l->visible!=None)
382                                 frclear(f, 0);
383                 }
384                 if(!rectclip(&r, dr))
385                         panic("flresize");
386                 if(r.max.x-r.min.x<100)
387                         r.min.x = dr.min.x;
388                 if(r.max.x-r.min.x<100)
389                         r.max.x = dr.max.x;
390                 if(r.max.y-r.min.y<2*FLMARGIN+f->font->height)
391                         r.min.y = dr.min.y;
392                 if(r.max.y-r.min.y<2*FLMARGIN+f->font->height)
393                         r.max.y = dr.max.y;
394                 if(!move)
395                         l->visible = None;
396                 frsetrects(f, insetrect(flrect(l, r), FLMARGIN), f->b);
397                 if(!move && f->b)
398                         scrdraw(l, scrtotal(l));
399         }
400         newvisibilities(1);
401 }
402
403 int
404 flprepare(Flayer *l)
405 {
406         Frame *f;
407         ulong n;
408         Rune *r;
409
410         if(l->visible == None)
411                 return 0;
412         f = &l->f;
413         if(f->b == 0){
414                 if(l->visible == All)
415                         f->b = screen;
416                 else if((f->b = allocimage(display, l->entire, screen->chan, 0, 0))==0)
417                         return 0;
418                 draw(f->b, l->entire, f->cols[BACK], nil, ZP);
419                 border(f->b, l->entire, l==llist[0]? FLMARGIN : 1, f->cols[BORD], ZP);
420                 n = f->nchars;
421                 frinit(f, f->entire, f->font, f->b, 0);
422                 f->maxtab = maxtab*stringwidth(f->font, "0");
423                 r = (*l->textfn)(l, n, &n);
424                 frinsert(f, r, r+n, (ulong)0);
425                 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0);
426                 flfp0p1(l, &f->p0, &f->p1);
427                 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 1);
428                 l->lastsr = ZR;
429                 scrdraw(l, scrtotal(l));
430         }
431         return 1;
432 }
433
434 static  int     somevis, someinvis, justvis;
435
436 Vis
437 visibility(Flayer *l)
438 {
439         somevis = someinvis = 0;
440         justvis = 1;
441         flrefresh(l, l->entire, 0);
442         justvis = 0;
443         if(somevis==0)
444                 return None;
445         if(someinvis==0)
446                 return All;
447         return Some;
448 }
449
450 void
451 flrefresh(Flayer *l, Rectangle r, int i)
452 {
453         Flayer *t;
454         Rectangle s;
455
456     Top:
457         if((t=llist[i++]) == l){
458                 if(!justvis)
459                         draw(screen, r, l->f.b, nil, r.min);
460                 somevis = 1;
461         }else{
462                 if(!rectXrect(t->entire, r))
463                         goto Top;       /* avoid stacking unnecessarily */
464                 if(t->entire.min.x>r.min.x){
465                         s = r;
466                         s.max.x = t->entire.min.x;
467                         flrefresh(l, s, i);
468                         r.min.x = t->entire.min.x;
469                 }
470                 if(t->entire.min.y>r.min.y){
471                         s = r;
472                         s.max.y = t->entire.min.y;
473                         flrefresh(l, s, i);
474                         r.min.y = t->entire.min.y;
475                 }
476                 if(t->entire.max.x<r.max.x){
477                         s = r;
478                         s.min.x = t->entire.max.x;
479                         flrefresh(l, s, i);
480                         r.max.x = t->entire.max.x;
481                 }
482                 if(t->entire.max.y<r.max.y){
483                         s = r;
484                         s.min.y = t->entire.max.y;
485                         flrefresh(l, s, i);
486                         r.max.y = t->entire.max.y;
487                 }
488                 /* remaining piece of r is blocked by t; forget about it */
489                 someinvis = 1;
490         }
491 }