1 | /* |
---|
2 | * $Id: Form.c,v 1.2 1999-01-22 23:16:52 ghudson Exp $ |
---|
3 | * |
---|
4 | * Copyright 1990, 1991 by the Massachusetts Institute of Technology. |
---|
5 | * |
---|
6 | * For copying and distribution information, please see the file |
---|
7 | * <mit-copyright.h>. |
---|
8 | * |
---|
9 | */ |
---|
10 | |
---|
11 | #if (!defined(lint)) && (!defined(SABER)) |
---|
12 | static char *rcsid = |
---|
13 | "$Id: Form.c,v 1.2 1999-01-22 23:16:52 ghudson Exp $"; |
---|
14 | #endif |
---|
15 | |
---|
16 | #include "mit-copyright.h" |
---|
17 | #include <stdio.h> |
---|
18 | #include <ctype.h> |
---|
19 | #include "Jets.h" |
---|
20 | #include "Form.h" |
---|
21 | |
---|
22 | #define offset(field) XjOffset(FormJet,field) |
---|
23 | |
---|
24 | static XjResource resources[] = { |
---|
25 | { XjNx, XjCX, XjRInt, sizeof(int), |
---|
26 | offset(core.x), XjRString, XjInheritValue }, |
---|
27 | { XjNy, XjCY, XjRInt, sizeof(int), |
---|
28 | offset(core.y), XjRString, XjInheritValue }, |
---|
29 | { XjNwidth, XjCWidth, XjRInt, sizeof(int), |
---|
30 | offset(core.width), XjRString, XjInheritValue }, |
---|
31 | { XjNheight, XjCHeight, XjRInt, sizeof(int), |
---|
32 | offset(core.height), XjRString, XjInheritValue }, |
---|
33 | { XjNpadding, XjCPadding, XjRInt, sizeof(int), |
---|
34 | offset(form.padding), XjRString, "0" }, |
---|
35 | { XjNform, XjCForm, XjRString, sizeof(char *), |
---|
36 | offset(form.form), XjRString, "" } |
---|
37 | }; |
---|
38 | |
---|
39 | #undef offset |
---|
40 | |
---|
41 | static void resize(), expose(); |
---|
42 | |
---|
43 | FormClassRec formClassRec = { |
---|
44 | { |
---|
45 | /* class name */ "Form", |
---|
46 | /* jet size */ sizeof(FormRec), |
---|
47 | /* classInitialize */ NULL, |
---|
48 | /* classInitialized? */ 1, |
---|
49 | /* initialize */ NULL, |
---|
50 | /* prerealize */ NULL, |
---|
51 | /* realize */ NULL, |
---|
52 | /* event */ NULL, |
---|
53 | /* expose */ expose, |
---|
54 | /* querySize */ NULL, |
---|
55 | /* move */ NULL, |
---|
56 | /* resize */ resize, |
---|
57 | /* destroy */ NULL, |
---|
58 | /* resources */ resources, |
---|
59 | /* number of 'em */ XjNumber(resources) |
---|
60 | } |
---|
61 | }; |
---|
62 | |
---|
63 | JetClass formJetClass = (JetClass)&formClassRec; |
---|
64 | |
---|
65 | |
---|
66 | #define XJ_FORM_UNDEF -1 |
---|
67 | #define XJ_FORM_ATTACH -2 |
---|
68 | |
---|
69 | #define LEFT 0 |
---|
70 | #define TOP 1 |
---|
71 | #define RIGHT 2 |
---|
72 | #define BOTTOM 3 |
---|
73 | #define SIDES 4 |
---|
74 | |
---|
75 | |
---|
76 | static void parse(ptr, name, sides, sidestr) |
---|
77 | char **ptr, *name, sidestr[SIDES][50]; |
---|
78 | int sides[SIDES]; |
---|
79 | { |
---|
80 | char *p, *q; |
---|
81 | int i = 0; |
---|
82 | int j; |
---|
83 | |
---|
84 | p = *ptr; |
---|
85 | |
---|
86 | while (*p != ':' && *p != '\0') |
---|
87 | name[i++] = *p++; |
---|
88 | |
---|
89 | name[i] = '\0'; |
---|
90 | |
---|
91 | if (*p == '\0') |
---|
92 | { |
---|
93 | *ptr = p; |
---|
94 | return; |
---|
95 | } |
---|
96 | else |
---|
97 | p++; /* skip the colon ":" */ |
---|
98 | |
---|
99 | |
---|
100 | for (j=0; j < SIDES; j++) |
---|
101 | { |
---|
102 | sides[j] = XJ_FORM_UNDEF; |
---|
103 | |
---|
104 | while (isspace(*p)) p++; |
---|
105 | |
---|
106 | if (*p != '\0' && *p != '-') |
---|
107 | { |
---|
108 | if (isdigit(*p)) |
---|
109 | { |
---|
110 | sides[j] = atoi(p); |
---|
111 | while (isdigit(*p)) p++; |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | q = sidestr[j]; |
---|
116 | while (isalnum(*p)) |
---|
117 | *q++ = *p++; |
---|
118 | *q = '\0'; |
---|
119 | sides[j] = XJ_FORM_ATTACH; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | if (*p == '-') p++; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | while (isspace(*p)) p++; |
---|
128 | if (*p == ';') |
---|
129 | p++; |
---|
130 | |
---|
131 | *ptr = p; |
---|
132 | } |
---|
133 | |
---|
134 | #define spec(x) (x != XJ_FORM_UNDEF) |
---|
135 | #define attachment(x) (x == XJ_FORM_ATTACH) |
---|
136 | |
---|
137 | /* |
---|
138 | * This code *really* needs to be done in a better way. |
---|
139 | */ |
---|
140 | static void resize(me, mysize) |
---|
141 | FormJet me; |
---|
142 | XjSize *mysize; |
---|
143 | { |
---|
144 | Jet child, tmp[SIDES]; |
---|
145 | char *ptr; |
---|
146 | char name[50], sidestr[SIDES][50]; |
---|
147 | int x, y, width, height; |
---|
148 | int sides[SIDES]; |
---|
149 | XjSize size; |
---|
150 | char errtext[100]; |
---|
151 | int j; |
---|
152 | |
---|
153 | me->core.width = mysize->width; |
---|
154 | me->core.height = mysize->height; |
---|
155 | |
---|
156 | ptr = me->form.form; |
---|
157 | |
---|
158 | while (*ptr != '\0') |
---|
159 | { |
---|
160 | parse(&ptr, name, sides, sidestr); |
---|
161 | |
---|
162 | for (child = me->core.child; child != NULL; child = child->core.sibling) |
---|
163 | if (strcmp(name, child->core.name) == 0) |
---|
164 | { |
---|
165 | XjQuerySize(child, &size); |
---|
166 | size.width += 2 * (child->core.borderWidth + me->form.padding); |
---|
167 | size.height += 2 * (child->core.borderWidth + me->form.padding); |
---|
168 | |
---|
169 | x = child->core.x; |
---|
170 | y = child->core.y; |
---|
171 | width = size.width; |
---|
172 | height = size.height; /* just in case */ |
---|
173 | |
---|
174 | /* |
---|
175 | * Find all the attachment jets, if there are any. |
---|
176 | */ |
---|
177 | for (j=0; j < SIDES; j++) |
---|
178 | { |
---|
179 | tmp[j] = NULL; |
---|
180 | if (attachment(sides[j])) |
---|
181 | { |
---|
182 | if ((tmp[j] = XjFindJet(sidestr[j], (Jet) me)) == NULL) |
---|
183 | { |
---|
184 | sprintf(errtext, "form: couldn't find jet `%s'", |
---|
185 | sidestr[j]); |
---|
186 | XjWarning(errtext); |
---|
187 | } |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | /* |
---|
193 | * Figure out the x coord and width. |
---|
194 | */ |
---|
195 | if (spec(sides[LEFT])) |
---|
196 | { |
---|
197 | if (tmp[LEFT]) |
---|
198 | x = tmp[LEFT]->core.x + tmp[LEFT]->core.width + |
---|
199 | 2 * (tmp[LEFT]->core.borderWidth + me->form.padding); |
---|
200 | else |
---|
201 | x = (sides[LEFT] * me->core.width) / 100; |
---|
202 | |
---|
203 | if (spec(sides[RIGHT])) |
---|
204 | { |
---|
205 | if (tmp[RIGHT]) |
---|
206 | width = tmp[RIGHT]->core.x - x - 2*me->form.padding; |
---|
207 | else |
---|
208 | width = ((sides[RIGHT] * me->core.width) / 100) - x; |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | else /* sides[LEFT] was not specified */ |
---|
213 | if (spec(sides[RIGHT])) |
---|
214 | { |
---|
215 | if (tmp[RIGHT]) |
---|
216 | x = tmp[RIGHT]->core.x - width - 1; |
---|
217 | else |
---|
218 | x = ((sides[RIGHT] * me->core.width) / 100) - width; |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | /* |
---|
223 | * Figure out the y coord and height. |
---|
224 | */ |
---|
225 | if (spec(sides[TOP])) |
---|
226 | { |
---|
227 | if (tmp[TOP]) |
---|
228 | y = tmp[TOP]->core.y + tmp[TOP]->core.height + |
---|
229 | 2 * (tmp[TOP]->core.borderWidth + me->form.padding); |
---|
230 | else |
---|
231 | y = (sides[TOP] * me->core.height) / 100; |
---|
232 | |
---|
233 | if (spec(sides[BOTTOM])) |
---|
234 | { |
---|
235 | if (tmp[BOTTOM]) |
---|
236 | height = tmp[BOTTOM]->core.y - y - 2*me->form.padding; |
---|
237 | else |
---|
238 | height = ((sides[BOTTOM] * me->core.height) / 100) - y; |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | else /* sides[TOP] was not specified */ |
---|
243 | if (spec(sides[BOTTOM])) |
---|
244 | { |
---|
245 | if (tmp[BOTTOM]) |
---|
246 | y = tmp[BOTTOM]->core.y - height - 1; |
---|
247 | else |
---|
248 | y = ((sides[BOTTOM] * me->core.height) / 100) - height; |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | XjMove(child, x + me->form.padding, y + me->form.padding); |
---|
254 | size.width = width - |
---|
255 | 2 * (child->core.borderWidth + me->form.padding); |
---|
256 | size.height = height - |
---|
257 | 2 * (child->core.borderWidth + me->form.padding); |
---|
258 | XjResize(child, &size); |
---|
259 | |
---|
260 | break; |
---|
261 | } |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | static void expose(me, event) |
---|
267 | Jet me; |
---|
268 | XEvent *event; |
---|
269 | { |
---|
270 | Jet child; |
---|
271 | |
---|
272 | if (event->xexpose.count == 0) |
---|
273 | { |
---|
274 | for (child = me->core.child; child != NULL; child = child->core.sibling) |
---|
275 | XjExpose(child, event); |
---|
276 | #ifdef notdefined |
---|
277 | if (child->core.classRec->core_class.expose != NULL) |
---|
278 | child->core.classRec->core_class.expose(child, event); |
---|
279 | #endif |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | void setForm(me, form) |
---|
285 | FormJet me; |
---|
286 | char *form; |
---|
287 | { |
---|
288 | me->form.form = XjNewString(form); |
---|
289 | } |
---|