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;
277 int ticked;
279 l->click = 0;
280 if(l->visible==None || !flprepare(l)){
281 l->p0 = p0, l->p1 = p1;
282 return;
284 l->p0 = p0, l->p1 = p1;
285 flfp0p1(l, &fp0, &fp1, &ticked);
286 if(fp0==l->f.p0 && fp1==l->f.p1){
287 if(l->f.ticked != ticked)
288 frtick(&l->f, frptofchar(&l->f, fp0), ticked);
289 return;
292 if(fp1<=l->f.p0 || fp0>=l->f.p1 || l->f.p0==l->f.p1 || fp0==fp1){
293 /* no overlap or trivial repainting */
294 frdrawsel(&l->f, frptofchar(&l->f, l->f.p0), l->f.p0, l->f.p1, 0);
295 if(fp0 != fp1 || ticked)
296 frdrawsel(&l->f, frptofchar(&l->f, fp0), fp0, fp1, 1);
297 goto Refresh;
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);
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);
315 Refresh:
316 l->f.p0 = fp0;
317 l->f.p1 = fp1;
318 if(l->visible==Some)
319 flrefresh(l, l->entire, 0);
322 void
323 flfp0p1(Flayer *l, ulong *pp0, ulong *pp1, int *ticked)
325 long p0 = l->p0-l->origin, p1 = l->p1-l->origin;
327 *ticked = p0 == p1;
328 if(p0 < 0){
329 *ticked = 0;
330 p0 = 0;
332 if(p1 < 0)
333 p1 = 0;
334 if(p0 > l->f.nchars)
335 p0 = l->f.nchars;
336 if(p1 > l->f.nchars){
337 *ticked = 0;
338 p1 = l->f.nchars;
340 *pp0 = p0;
341 *pp1 = p1;
344 Rectangle
345 rscale(Rectangle r, Point old, Point new)
347 r.min.x = r.min.x*new.x/old.x;
348 r.min.y = r.min.y*new.y/old.y;
349 r.max.x = r.max.x*new.x/old.x;
350 r.max.y = r.max.y*new.y/old.y;
351 return r;
354 void
355 flresize(Rectangle dr)
357 int i;
358 Flayer *l;
359 Frame *f;
360 Rectangle r, olDrect;
361 int move;
363 olDrect = lDrect;
364 lDrect = dr;
365 move = 0;
366 /* no moving on rio; must repaint */
367 if(0 && Dx(dr)==Dx(olDrect) && Dy(dr)==Dy(olDrect))
368 move = 1;
369 else
370 draw(screen, lDrect, display->white, nil, ZP);
371 for(i=0; i<nllist; i++){
372 l = llist[i];
373 l->lastsr = ZR;
374 f = &l->f;
375 if(move)
376 r = rectaddpt(rectsubpt(l->entire, olDrect.min), dr.min);
377 else{
378 r = rectaddpt(rscale(rectsubpt(l->entire, olDrect.min),
379 subpt(olDrect.max, olDrect.min),
380 subpt(dr.max, dr.min)), dr.min);
381 if(l->visible==Some && f->b){
382 freeimage(f->b);
383 frclear(f, 0);
385 f->b = 0;
386 if(l->visible!=None)
387 frclear(f, 0);
389 if(!rectclip(&r, dr))
390 panic("flresize");
391 if(r.max.x-r.min.x<100)
392 r.min.x = dr.min.x;
393 if(r.max.x-r.min.x<100)
394 r.max.x = dr.max.x;
395 if(r.max.y-r.min.y<2*FLMARGIN+f->font->height)
396 r.min.y = dr.min.y;
397 if(r.max.y-r.min.y<2*FLMARGIN+f->font->height)
398 r.max.y = dr.max.y;
399 if(!move)
400 l->visible = None;
401 frsetrects(f, insetrect(flrect(l, r), FLMARGIN), f->b);
402 if(!move && f->b)
403 scrdraw(l, scrtotal(l));
405 newvisibilities(1);
408 int
409 flprepare(Flayer *l)
411 Frame *f;
412 ulong n;
413 Rune *r;
414 int ticked;
416 if(l->visible == None)
417 return 0;
418 f = &l->f;
419 if(f->b == 0){
420 if(l->visible == All)
421 f->b = screen;
422 else if((f->b = allocimage(display, l->entire, screen->chan, 0, 0))==0)
423 return 0;
424 draw(f->b, l->entire, f->cols[BACK], nil, ZP);
425 border(f->b, l->entire, l==llist[0]? FLMARGIN : 1, f->cols[BORD], ZP);
426 n = f->nchars;
427 frinit(f, f->entire, f->font, f->b, 0);
428 f->maxtab = maxtab*stringwidth(f->font, "0");
429 r = (*l->textfn)(l, n, &n);
430 frinsert(f, r, r+n, (ulong)0);
431 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0);
432 flfp0p1(l, &f->p0, &f->p1, &ticked);
433 if(f->p0 != f->p1 || ticked)
434 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 1);
435 l->lastsr = ZR;
436 scrdraw(l, scrtotal(l));
438 return 1;
441 static int somevis, someinvis, justvis;
443 Vis
444 visibility(Flayer *l)
446 somevis = someinvis = 0;
447 justvis = 1;
448 flrefresh(l, l->entire, 0);
449 justvis = 0;
450 if(somevis==0)
451 return None;
452 if(someinvis==0)
453 return All;
454 return Some;
457 void
458 flrefresh(Flayer *l, Rectangle r, int i)
460 Flayer *t;
461 Rectangle s;
463 Top:
464 if((t=llist[i++]) == l){
465 if(!justvis)
466 draw(screen, r, l->f.b, nil, r.min);
467 somevis = 1;
468 }else{
469 if(!rectXrect(t->entire, r))
470 goto Top; /* avoid stacking unnecessarily */
471 if(t->entire.min.x>r.min.x){
472 s = r;
473 s.max.x = t->entire.min.x;
474 flrefresh(l, s, i);
475 r.min.x = t->entire.min.x;
477 if(t->entire.min.y>r.min.y){
478 s = r;
479 s.max.y = t->entire.min.y;
480 flrefresh(l, s, i);
481 r.min.y = t->entire.min.y;
483 if(t->entire.max.x<r.max.x){
484 s = r;
485 s.min.x = t->entire.max.x;
486 flrefresh(l, s, i);
487 r.max.x = t->entire.max.x;
489 if(t->entire.max.y<r.max.y){
490 s = r;
491 s.min.y = t->entire.max.y;
492 flrefresh(l, s, i);
493 r.max.y = t->entire.max.y;
495 /* remaining piece of r is blocked by t; forget about it */
496 someinvis = 1;