Blob


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