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 = 0;
257 if(l->visible!=All)
258 flupfront(l);
259 frselect(&l->f, mousectl);
260 if(l->f.p0==l->f.p1){
261 if(mousep->msec-l->click<Clicktime && l->f.p0+l->origin==l->p0){
262 ret = 1;
263 l->click = 0;
264 }else
265 l->click = mousep->msec;
266 }else
267 l->click = 0;
268 l->p0 = l->f.p0+l->origin, l->p1 = l->f.p1+l->origin;
269 return ret;
272 void
273 flsetselect(Flayer *l, long p0, long p1)
275 ulong fp0, fp1;
277 l->click = 0;
278 if(l->visible==None || !flprepare(l)){
279 l->p0 = p0, l->p1 = p1;
280 return;
282 l->p0 = p0, l->p1 = p1;
283 flfp0p1(l, &fp0, &fp1);
284 if(fp0==l->f.p0 && fp1==l->f.p1)
285 return;
287 if(fp1<=l->f.p0 || fp0>=l->f.p1 || l->f.p0==l->f.p1 || fp0==fp1){
288 /* no overlap or trivial repainting */
289 frdrawsel(&l->f, frptofchar(&l->f, l->f.p0), l->f.p0, l->f.p1, 0);
290 frdrawsel(&l->f, frptofchar(&l->f, fp0), fp0, fp1, 1);
291 goto Refresh;
293 /* the current selection and the desired selection overlap and are both non-empty */
294 if(fp0 < l->f.p0){
295 /* extend selection backwards */
296 frdrawsel(&l->f, frptofchar(&l->f, fp0), fp0, l->f.p0, 1);
297 }else if(fp0 > l->f.p0){
298 /* trim first part of selection */
299 frdrawsel(&l->f, frptofchar(&l->f, l->f.p0), l->f.p0, fp0, 0);
301 if(fp1 > l->f.p1){
302 /* extend selection forwards */
303 frdrawsel(&l->f, frptofchar(&l->f, l->f.p1), l->f.p1, fp1, 1);
304 }else if(fp1 < l->f.p1){
305 /* trim last part of selection */
306 frdrawsel(&l->f, frptofchar(&l->f, fp1), fp1, l->f.p1, 0);
309 Refresh:
310 l->f.p0 = fp0;
311 l->f.p1 = fp1;
312 if(l->visible==Some)
313 flrefresh(l, l->entire, 0);
316 void
317 flfp0p1(Flayer *l, ulong *pp0, ulong *pp1)
319 long p0 = l->p0-l->origin, p1 = l->p1-l->origin;
321 if(p0 < 0)
322 p0 = 0;
323 if(p1 < 0)
324 p1 = 0;
325 if(p0 > l->f.nchars)
326 p0 = l->f.nchars;
327 if(p1 > l->f.nchars)
328 p1 = l->f.nchars;
329 *pp0 = p0;
330 *pp1 = p1;
333 Rectangle
334 rscale(Rectangle r, Point old, Point new)
336 r.min.x = r.min.x*new.x/old.x;
337 r.min.y = r.min.y*new.y/old.y;
338 r.max.x = r.max.x*new.x/old.x;
339 r.max.y = r.max.y*new.y/old.y;
340 return r;
343 void
344 flresize(Rectangle dr)
346 int i;
347 Flayer *l;
348 Frame *f;
349 Rectangle r, olDrect;
350 int move;
352 olDrect = lDrect;
353 lDrect = dr;
354 move = 0;
355 /* no moving on rio; must repaint */
356 if(0 && Dx(dr)==Dx(olDrect) && Dy(dr)==Dy(olDrect))
357 move = 1;
358 else
359 draw(screen, lDrect, display->white, nil, ZP);
360 for(i=0; i<nllist; i++){
361 l = llist[i];
362 l->lastsr = ZR;
363 f = &l->f;
364 if(move)
365 r = rectaddpt(rectsubpt(l->entire, olDrect.min), dr.min);
366 else{
367 r = rectaddpt(rscale(rectsubpt(l->entire, olDrect.min),
368 subpt(olDrect.max, olDrect.min),
369 subpt(dr.max, dr.min)), dr.min);
370 if(l->visible==Some && f->b){
371 freeimage(f->b);
372 frclear(f, 0);
374 f->b = 0;
375 if(l->visible!=None)
376 frclear(f, 0);
378 if(!rectclip(&r, dr))
379 panic("flresize");
380 if(r.max.x-r.min.x<100)
381 r.min.x = dr.min.x;
382 if(r.max.x-r.min.x<100)
383 r.max.x = dr.max.x;
384 if(r.max.y-r.min.y<2*FLMARGIN+f->font->height)
385 r.min.y = dr.min.y;
386 if(r.max.y-r.min.y<2*FLMARGIN+f->font->height)
387 r.max.y = dr.max.y;
388 if(!move)
389 l->visible = None;
390 frsetrects(f, insetrect(flrect(l, r), FLMARGIN), f->b);
391 if(!move && f->b)
392 scrdraw(l, scrtotal(l));
394 newvisibilities(1);
397 int
398 flprepare(Flayer *l)
400 Frame *f;
401 ulong n;
402 Rune *r;
404 if(l->visible == None)
405 return 0;
406 f = &l->f;
407 if(f->b == 0){
408 if(l->visible == All)
409 f->b = screen;
410 else if((f->b = allocimage(display, l->entire, screen->chan, 0, 0))==0)
411 return 0;
412 draw(f->b, l->entire, f->cols[BACK], nil, ZP);
413 border(f->b, l->entire, l==llist[0]? FLMARGIN : 1, f->cols[BORD], ZP);
414 n = f->nchars;
415 frinit(f, f->entire, f->font, f->b, 0);
416 f->maxtab = maxtab*stringwidth(f->font, "0");
417 r = (*l->textfn)(l, n, &n);
418 frinsert(f, r, r+n, (ulong)0);
419 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0);
420 flfp0p1(l, &f->p0, &f->p1);
421 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 1);
422 l->lastsr = ZR;
423 scrdraw(l, scrtotal(l));
425 return 1;
428 static int somevis, someinvis, justvis;
430 Vis
431 visibility(Flayer *l)
433 somevis = someinvis = 0;
434 justvis = 1;
435 flrefresh(l, l->entire, 0);
436 justvis = 0;
437 if(somevis==0)
438 return None;
439 if(someinvis==0)
440 return All;
441 return Some;
444 void
445 flrefresh(Flayer *l, Rectangle r, int i)
447 Flayer *t;
448 Rectangle s;
450 Top:
451 if((t=llist[i++]) == l){
452 if(!justvis)
453 draw(screen, r, l->f.b, nil, r.min);
454 somevis = 1;
455 }else{
456 if(!rectXrect(t->entire, r))
457 goto Top; /* avoid stacking unnecessarily */
458 if(t->entire.min.x>r.min.x){
459 s = r;
460 s.max.x = t->entire.min.x;
461 flrefresh(l, s, i);
462 r.min.x = t->entire.min.x;
464 if(t->entire.min.y>r.min.y){
465 s = r;
466 s.max.y = t->entire.min.y;
467 flrefresh(l, s, i);
468 r.min.y = t->entire.min.y;
470 if(t->entire.max.x<r.max.x){
471 s = r;
472 s.min.x = t->entire.max.x;
473 flrefresh(l, s, i);
474 r.max.x = t->entire.max.x;
476 if(t->entire.max.y<r.max.y){
477 s = r;
478 s.min.y = t->entire.max.y;
479 flrefresh(l, s, i);
480 r.max.y = t->entire.max.y;
482 /* remaining piece of r is blocked by t; forget about it */
483 someinvis = 1;