xref: /inferno-os/libprefab/iconbox.c (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1 #include <lib9.h>
2 #include <draw.h>
3 #include <interp.h>
4 #include <isa.h>
5 #include "../libinterp/runt.h"
6 #include <drawif.h>
7 #include <prefab.h>
8 
9 PCompound*
iconbox(Prefab_Environ * e,Draw_Point p,String * titletext,Draw_Image * icon,Draw_Image * mask)10 iconbox(Prefab_Environ *e, Draw_Point p, String *titletext, Draw_Image *icon, Draw_Image *mask)
11 {
12 	Draw_Rect er, r, ir;
13 	PCompound *pc;
14 	Prefab_Compound *c;
15 	PElement *elem, *title;
16 	Image *disp;
17 	Draw_Image *ddisp;
18 	Screen *screen;
19 	Heap *h;
20 	Rectangle t;
21 	Point pt;
22 
23 	screen = lookupscreen(e->screen);
24 	if(screen == nil)
25 		return H;
26 	h = heapz(TCompound);
27 	if(h == H)
28 		return H;
29 	pc = H2D(PCompound*, h);
30 	c = &pc->c;
31 
32 	gchalt++;
33 	title = H;
34 	if(titletext != H){
35 		er.min.x = 0;
36 		er.min.y = 0;
37 		er.max.x = Dx(icon->r)-5;
38 		er.max.y = 0;
39 		title = textelement(e, titletext, er, ETitle);
40 		if(title == H){
41     Err:
42 			destroy(c);
43 			gchalt--;
44 			return H;
45 		}
46 		c->title = (Prefab_Element*)title;
47 	}
48 
49 	r = icon->r;
50 	if(title != H)
51 		r.max.y += 2+1+title->nkids*e->style->titlefont->height+1;
52 
53 	er = edgerect(e, p, &r);
54 
55 	R2R(t, er);
56 	disp = allocwindow(screen, t, Refbackup /*refreshcompound*/, DWhite);
57 	if(disp == nil)
58 		goto Err;
59 
60 	if((ddisp=mkdrawimage(disp, e->screen, e->screen->display, nil)) == H){
61 		freeimage(disp);
62 		goto Err;
63 	}
64 
65 	ir = r;
66 	if(title != H){
67 		ir = r;
68 		pt.x = r.min.x+3;
69 		pt.y = r.min.y+3;
70 		translateelement(&title->e, pt);
71 		ir.min.y = title->e.r.max.y+1;
72 	}
73 
74 	elem = iconelement(e, ir, icon, mask);
75 	c->r = r;
76 	c->image = ddisp;
77 	c->environ = e;
78 	D2H(e)->ref++;
79 	c->contents = (Prefab_Element*)elem;
80 	pc->display = screen->display;
81 	gchalt--;
82 	return pc;
83 }
84