summaryrefslogtreecommitdiff
path: root/libprefab/textbox.c
blob: 6211bfdb88bfe6148b50734f7c510c2d6db76029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <lib9.h>
#include <draw.h>
#include <interp.h>
#include <isa.h>
#include "../libinterp/runt.h"
#include <drawif.h>
#include <prefab.h>

PCompound*
layoutbox(Prefab_Environ *e, Draw_Rect rr, String *titletext, List *texttext)
{
	Draw_Rect er, r, lr;
	PCompound *pc;
	Prefab_Compound *c;
	PElement *title, *text;
	Image *disp;
	Draw_Image *ddisp;
	Screen *screen;
	Heap *h;
	Rectangle t;
	int wid, w;
	Point p, pt;

	screen = lookupscreen(e->screen);
	if(screen == nil)
		return H;

	gchalt++;
	wid = Dx(rr);
	P2P(p, rr.min);
	title = H;
	text = H;
	if(texttext != H){
		er.min.x = 0;
		er.min.y = 0;
		er.max.x = wid-5;
		er.max.y = Dy(rr);
		text = layoutelement(e, texttext, er, EText);
		if(text == H){
			gchalt--;
			return H;
		}
		if(wid <= 0)
			wid = Dx(text->e.r)+5;
	}
	if(titletext != H){
		/* see how wide title wants to be */
		memset(&er, 0, sizeof er);
		title = textelement(e, titletext, er, ETitle);
		if(title == H){
    Errtitle:
			destroy(text);
			gchalt--;
			return H;
		}
		w = 2+1+3+Dx(title->e.r)+1;
		/* if title is wider than text, adjust wid accordingly */
		if(text!=0 && Dx(text->e.r)<w){
			if(Dx(text->e.r) < 100){	/* narrow text; don't let title get too wide */
				if(w > 250+5)
					w = 250+5;
				wid = w;
			}
			destroy(title);
			er.min.x = 0;
			er.min.y = 0;
			er.max.x = wid-5;
			er.max.y = 0;
			title = textelement(e, titletext, er, ETitle);
			if(title == H)
				goto Errtitle;
		}
		if(wid <= 0)
			wid = Dx(title->e.r)+5;
	}

	h = heapz(TCompound);
	pc = H2D(PCompound*, h);
	c = &pc->c;
	c->title = (Prefab_Element*)title;
	c->contents = (Prefab_Element*)text;
	/* now can just destroy c to clean up */

	r.min = DPOINT(p);
	r.max.x = r.min.x+wid;
	r.max.y = p.y+2+1 + 1+1;
	if(title != H)
		r.max.y += title->nkids*e->style->titlefont->height+1;
	if(text != H)
		r.max.y += Dy(text->e.r);

	er = edgerect(e, DPOINT(p), &r);

	R2R(t, er);
	disp = allocwindow(screen, t, Refbackup /*refreshcompound*/, DWhite);
	if(disp == nil){
    Err:
		destroy(c);
		gchalt--;
		return H;
	}
	if((ddisp=mkdrawimage(disp, e->screen, e->screen->display, nil)) == H){
		freeimage(disp);
		goto Err;
	}

	lr = r;
	if(title != H){
		pt.x = r.min.x+3;
		pt.y = r.min.y+3;
		translateelement(&title->e, pt);
		lr.min.y = title->e.r.max.y+1;
	}

	if(text != H)
		translateelement((Prefab_Element*)text, subpt(IPOINT(lr.min), IPOINT(text->e.r.min)));

	c->r = r;
	c->environ = e;
	c->image = ddisp;
	D2H(e)->ref++;
	pc->display = screen->display;
	gchalt--;
	return pc;

}

PCompound*
textbox(Prefab_Environ *e, Draw_Rect rr, String *titletext, String *texttext)
{
	PCompound *pc;
	List *l;

	l = listoflayout(e->style, texttext, EText);
	pc = layoutbox(e, rr, titletext, l);
	free(l);
	return pc;
}