comparison template/dw.c @ 1441:b9577d1f0411

Removed some debug code on Windows and updated the layout code in the template.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 08 Dec 2011 11:43:28 +0000
parents fbaec6e5df63
children e0212278f794
comparison
equal deleted inserted replaced
1440:c50f445e891a 1441:b9577d1f0411
22 } 22 }
23 23
24 /* This function calculates how much space the widgets and boxes require 24 /* This function calculates how much space the widgets and boxes require
25 * and does expansion as necessary. 25 * and does expansion as necessary.
26 */ 26 */
27 int _resize_box(Box *thisbox, int *depth, int x, int y, int *usedx, int *usedy, 27 static void _resize_box(Box *thisbox, int *depth, int x, int y, int pass)
28 int pass, int *usedpadx, int *usedpady) 28 {
29 { 29 /* Current item position */
30 int z, currentx = 0, currenty = 0; 30 int z, currentx = thisbox->pad, currenty = thisbox->pad;
31 /* Used x, y and padding maximum values...
32 * These will be used to find the widest or
33 * tallest items in a box.
34 */
31 int uymax = 0, uxmax = 0; 35 int uymax = 0, uxmax = 0;
32 int upymax = 0, upxmax = 0; 36 int upymax = 0, upxmax = 0;
33 /* Used for the SIZEEXPAND */ 37
34 int nux = *usedx, nuy = *usedy; 38 /* Reset the box sizes */
35 int nupx = *usedpadx, nupy = *usedpady; 39 thisbox->minwidth = thisbox->minheight = thisbox->usedpadx = thisbox->usedpady = thisbox->pad * 2;
36
37 (*usedx) += (thisbox->pad * 2);
38 (*usedy) += (thisbox->pad * 2);
39 40
40 #if 0 41 #if 0
41 /* If there are containers which have built-in padding like 42 /* If there are containers which have built-in padding like
42 * groupboxes.. calculate the padding size and add it to the layout. 43 * groupboxes.. calculate the padding size and add it to the layout.
43 */ 44 */
58 else 59 else
59 thisbox->grouppady = 6; 60 thisbox->grouppady = 6;
60 61
61 thisbox->grouppadx = 6; 62 thisbox->grouppadx = 6;
62 63
63 (*usedx) += thisbox->grouppadx; 64 thisbox->minwidth += thisbox->grouppadx;
64 (*usedpadx) += thisbox->grouppadx; 65 thisbox->usedpadx += thisbox->grouppadx;
65 (*usedy) += thisbox->grouppady; 66 thisbox->minheight += thisbox->grouppady;
66 (*usedpady) += thisbox->grouppady; 67 thisbox->usedpady += thisbox->grouppady;
67 } 68 }
68 #endif 69 #endif
69 70
71 /* Count up all the space for all items in the box */
70 for(z=0;z<thisbox->count;z++) 72 for(z=0;z<thisbox->count;z++)
71 { 73 {
74 int itempad, itemwidth, itemheight;
75
72 if(thisbox->items[z].type == TYPEBOX) 76 if(thisbox->items[z].type == TYPEBOX)
73 { 77 {
74 int initialx, initialy;
75 Box *tmp = (Box *)_dw_get_window_pointer(thisbox->items[z].hwnd); 78 Box *tmp = (Box *)_dw_get_window_pointer(thisbox->items[z].hwnd);
76
77 initialx = x - (*usedx);
78 initialy = y - (*usedy);
79 79
80 if(tmp) 80 if(tmp)
81 { 81 {
82 int newx, newy; 82 /* On the first pass calculate the box contents */
83 int nux = *usedx, nuy = *usedy; 83 if(pass == 1)
84 int upx = *usedpadx + (tmp->pad*2), upy = *usedpady + (tmp->pad*2);
85
86 /* On the second pass we know how big the box needs to be and how
87 * much space we have, so we can calculate a ratio for the new box.
88 */
89 if(pass == 2)
90 { 84 {
91 int deep = *depth + 1; 85 (*depth)++;
92 86
93 _resize_box(tmp, &deep, x, y, &nux, &nuy, 1, &upx, &upy); 87 /* Save the newly calculated values on the box */
94 88 _resize_box(tmp, depth, x, y, pass);
95 tmp->upx = upx - *usedpadx; 89
96 tmp->upy = upy - *usedpady; 90 /* Duplicate the values in the item list for use below */
97 91 thisbox->items[z].width = tmp->minwidth;
98 newx = x - nux; 92 thisbox->items[z].height = tmp->minheight;
99 newy = y - nuy; 93
100 94 (*depth)--;
101 tmp->width = thisbox->items[z].width = initialx - newx;
102 tmp->height = thisbox->items[z].height = initialy - newy;
103
104 tmp->parentxratio = thisbox->xratio;
105 tmp->parentyratio = thisbox->yratio;
106
107 tmp->parentpad = tmp->pad;
108
109 /* Just in case */
110 tmp->xratio = thisbox->xratio;
111 tmp->yratio = thisbox->yratio;
112
113 if(thisbox->type == DW_VERT)
114 {
115 int tmppad = (thisbox->items[z].pad*2)+(tmp->pad*2)+tmp->grouppady;
116
117 if((thisbox->items[z].width - tmppad)!=0)
118 tmp->xratio = ((float)((thisbox->items[z].width * thisbox->xratio)-tmppad))/((float)(thisbox->items[z].width-tmppad));
119 }
120 else
121 {
122 if((thisbox->items[z].width-tmp->upx)!=0)
123 tmp->xratio = ((float)((thisbox->items[z].width * thisbox->xratio)-tmp->upx))/((float)(thisbox->items[z].width-tmp->upx));
124 }
125 if(thisbox->type == DW_HORZ)
126 {
127 int tmppad = (thisbox->items[z].pad*2)+(tmp->pad*2)+tmp->grouppadx;
128
129 if((thisbox->items[z].height-tmppad)!=0)
130 tmp->yratio = ((float)((thisbox->items[z].height * thisbox->yratio)-tmppad))/((float)(thisbox->items[z].height-tmppad));
131 }
132 else
133 {
134 if((thisbox->items[z].height-tmp->upy)!=0)
135 tmp->yratio = ((float)((thisbox->items[z].height * thisbox->yratio)-tmp->upy))/((float)(thisbox->items[z].height-tmp->upy));
136 }
137
138 nux = *usedx; nuy = *usedy;
139 upx = *usedpadx + (tmp->pad*2); upy = *usedpady + (tmp->pad*2);
140 }
141
142 (*depth)++;
143
144 _resize_box(tmp, depth, x, y, &nux, &nuy, pass, &upx, &upy);
145
146 (*depth)--;
147
148 newx = x - nux;
149 newy = y - nuy;
150
151 tmp->minwidth = thisbox->items[z].width = initialx - newx;
152 tmp->minheight = thisbox->items[z].height = initialy - newy;
153 }
154 }
155
156 if(pass > 1 && *depth > 0)
157 {
158 if(thisbox->type == DW_VERT)
159 {
160 int tmppad = (thisbox->items[z].pad*2)+(thisbox->parentpad*2)+thisbox->grouppadx;
161
162 if((thisbox->minwidth-tmppad) == 0)
163 thisbox->items[z].xratio = 1.0;
164 else
165 thisbox->items[z].xratio = ((float)((thisbox->width * thisbox->parentxratio)-tmppad))/((float)(thisbox->minwidth-tmppad));
166 }
167 else
168 {
169 if(thisbox->minwidth-thisbox->upx == 0)
170 thisbox->items[z].xratio = 1.0;
171 else
172 thisbox->items[z].xratio = ((float)((thisbox->width * thisbox->parentxratio)-thisbox->upx))/((float)(thisbox->minwidth-thisbox->upx));
173 }
174
175 if(thisbox->type == DW_HORZ)
176 {
177 int tmppad = (thisbox->items[z].pad*2)+(thisbox->parentpad*2)+thisbox->grouppady;
178
179 if((thisbox->minheight-tmppad) == 0)
180 thisbox->items[z].yratio = 1.0;
181 else
182 thisbox->items[z].yratio = ((float)((thisbox->height * thisbox->parentyratio)-tmppad))/((float)(thisbox->minheight-tmppad));
183 }
184 else
185 {
186 if(thisbox->minheight-thisbox->upy == 0)
187 thisbox->items[z].yratio = 1.0;
188 else
189 thisbox->items[z].yratio = ((float)((thisbox->height * thisbox->parentyratio)-thisbox->upy))/((float)(thisbox->minheight-thisbox->upy));
190 }
191
192 if(thisbox->items[z].type == TYPEBOX)
193 {
194 Box *tmp = (Box *)_dw_get_window_pointer(thisbox->items[z].hwnd);
195
196 if(tmp)
197 {
198 tmp->parentxratio = thisbox->items[z].xratio;
199 tmp->parentyratio = thisbox->items[z].yratio;
200 } 95 }
201 } 96 }
202 } 97 }
203 else 98
204 { 99 /* Precalculate these values, since they will
205 thisbox->items[z].xratio = thisbox->xratio; 100 * be used used repeatedly in the next section.
206 thisbox->items[z].yratio = thisbox->yratio; 101 */
207 } 102 itempad = thisbox->items[z].pad * 2;
208 103 itemwidth = thisbox->items[z].width + itempad;
104 itemheight = thisbox->items[z].height + itempad;
105
106 /* Calculate the totals and maximums */
209 if(thisbox->type == DW_VERT) 107 if(thisbox->type == DW_VERT)
210 { 108 {
211 int itemwidth = (thisbox->items[z].pad*2) + thisbox->items[z].width;
212
213 if(itemwidth > uxmax) 109 if(itemwidth > uxmax)
214 uxmax = itemwidth; 110 uxmax = itemwidth;
111
215 if(thisbox->items[z].hsize != SIZEEXPAND) 112 if(thisbox->items[z].hsize != SIZEEXPAND)
216 { 113 {
217 if(itemwidth > upxmax) 114 if(itemwidth > upxmax)
218 upxmax = itemwidth; 115 upxmax = itemwidth;
219 } 116 }
220 else 117 else
221 { 118 {
222 if(thisbox->items[z].pad*2 > upxmax) 119 if(itempad > upxmax)
223 upxmax = thisbox->items[z].pad*2; 120 upxmax = itempad;
224 } 121 }
122 thisbox->minheight += itemheight;
123 if(thisbox->items[z].vsize != SIZEEXPAND)
124 thisbox->usedpady += itemheight;
125 else
126 thisbox->usedpady += itempad;
225 } 127 }
226 else 128 else
227 { 129 {
228 if(thisbox->items[z].width == -1)
229 {
230 /* figure out how much space this item requires */
231 /* thisbox->items[z].width = */
232 }
233 else
234 {
235 (*usedx) += thisbox->items[z].width + (thisbox->items[z].pad*2);
236 if(thisbox->items[z].hsize != SIZEEXPAND)
237 (*usedpadx) += (thisbox->items[z].pad*2) + thisbox->items[z].width;
238 else
239 (*usedpadx) += thisbox->items[z].pad*2;
240 }
241 }
242 if(thisbox->type == DW_HORZ)
243 {
244 int itemheight = (thisbox->items[z].pad*2) + thisbox->items[z].height;
245
246 if(itemheight > uymax) 130 if(itemheight > uymax)
247 uymax = itemheight; 131 uymax = itemheight;
248 if(thisbox->items[z].vsize != SIZEEXPAND) 132 if(thisbox->items[z].vsize != SIZEEXPAND)
249 { 133 {
250 if(itemheight > upymax) 134 if(itemheight > upymax)
251 upymax = itemheight; 135 upymax = itemheight;
252 } 136 }
253 else 137 else
254 { 138 {
255 if(thisbox->items[z].pad*2 > upymax) 139 if(itempad > upymax)
256 upymax = thisbox->items[z].pad*2; 140 upymax = itempad;
257 } 141 }
258 } 142 thisbox->minwidth += itemwidth;
259 else 143 if(thisbox->items[z].hsize != SIZEEXPAND)
260 { 144 thisbox->usedpadx += itemwidth;
261 if(thisbox->items[z].height == -1)
262 {
263 /* figure out how much space this item requires */
264 /* thisbox->items[z].height = */
265 }
266 else 145 else
267 { 146 thisbox->usedpadx += itempad;
268 (*usedy) += thisbox->items[z].height + (thisbox->items[z].pad*2);
269 if(thisbox->items[z].vsize != SIZEEXPAND)
270 (*usedpady) += (thisbox->items[z].pad*2) + thisbox->items[z].height;
271 else
272 (*usedpady) += thisbox->items[z].pad*2;
273 }
274 } 147 }
275 } 148 }
276 149
277 (*usedx) += uxmax; 150 /* Add the maximums which were calculated in the previous loop */
278 (*usedy) += uymax; 151 thisbox->minwidth += uxmax;
279 (*usedpadx) += upxmax; 152 thisbox->minheight += uymax;
280 (*usedpady) += upymax; 153 thisbox->usedpadx += upxmax;
281 154 thisbox->usedpady += upymax;
282 currentx += thisbox->pad; 155
283 currenty += thisbox->pad; 156 /* Move the groupbox start past the group border */
284
285 if(thisbox->grouphwnd) 157 if(thisbox->grouphwnd)
286 { 158 {
287 currentx += 3; 159 currentx += 3;
288 currenty += thisbox->grouppady - 3; 160 currenty += thisbox->grouppady - 3;
289 } 161 }
290 162
291 /* The second pass is for expansion and actual placement. */ 163 /* The second pass is for actual placement. */
292 if(pass > 1) 164 if(pass > 1)
293 { 165 {
294 /* Any SIZEEXPAND items should be set to uxmax/uymax */
295 for(z=0;z<thisbox->count;z++)
296 {
297 if(thisbox->items[z].hsize == SIZEEXPAND && thisbox->type == DW_VERT)
298 thisbox->items[z].width = uxmax-(thisbox->items[z].pad*2);
299 if(thisbox->items[z].vsize == SIZEEXPAND && thisbox->type == DW_HORZ)
300 thisbox->items[z].height = uymax-(thisbox->items[z].pad*2);
301 /* Run this code segment again to finalize the sized after setting uxmax/uymax values. */
302 if(thisbox->items[z].type == TYPEBOX)
303 {
304 Box *tmp = (Box *)_dw_get_window_pointer(thisbox->items[z].hwnd);
305
306 if(tmp)
307 {
308 if(*depth > 0)
309 {
310 float calcval;
311
312 if(thisbox->type == DW_VERT)
313 {
314 calcval = (float)(tmp->minwidth-((thisbox->items[z].pad*2)+(thisbox->pad*2)));
315 if(calcval == 0.0)
316 tmp->xratio = thisbox->xratio;
317 else
318 tmp->xratio = ((float)((thisbox->items[z].width * thisbox->xratio)-((thisbox->items[z].pad*2)+(thisbox->pad*2))))/calcval;
319 tmp->width = thisbox->items[z].width;
320 }
321 if(thisbox->type == DW_HORZ)
322 {
323 calcval = (float)(tmp->minheight-((thisbox->items[z].pad*2)+(thisbox->pad*2)));
324 if(calcval == 0.0)
325 tmp->yratio = thisbox->yratio;
326 else
327 tmp->yratio = ((float)((thisbox->items[z].height * thisbox->yratio)-((thisbox->items[z].pad*2)+(thisbox->pad*2))))/calcval;
328 tmp->height = thisbox->items[z].height;
329 }
330 }
331
332 (*depth)++;
333
334 _resize_box(tmp, depth, x, y, &nux, &nuy, 3, &nupx, &nupy);
335
336 (*depth)--;
337
338 }
339 }
340 }
341
342 for(z=0;z<(thisbox->count);z++) 166 for(z=0;z<(thisbox->count);z++)
343 { 167 {
344 int height = thisbox->items[z].height; 168 int height = thisbox->items[z].height;
345 int width = thisbox->items[z].width; 169 int width = thisbox->items[z].width;
346 int pad = thisbox->items[z].pad; 170 int itempad = thisbox->items[z].pad * 2;
347 HWND handle = thisbox->items[z].hwnd; 171 int thispad = thisbox->pad * 2;
348 int vectorx, vectory; 172
349 173 /* Calculate the new sizes */
350 /* When upxmax != pad*2 then ratios are incorrect. */ 174 if(thisbox->items[z].hsize == SIZEEXPAND)
351 vectorx = (int)((width*thisbox->items[z].xratio)-width); 175 {
352 vectory = (int)((height*thisbox->items[z].yratio)-height); 176 if(thisbox->type == DW_HORZ)
353 177 {
178 int expandablex = thisbox->minwidth - thisbox->usedpadx;
179
180 if(expandablex)
181 width = (int)(((float)width / (float)expandablex) * (float)(x - thisbox->usedpadx));
182 }
183 else
184 width = x - (itempad + thispad + thisbox->grouppadx);
185 }
186 if(thisbox->items[z].vsize == SIZEEXPAND)
187 {
188 if(thisbox->type == DW_VERT)
189 {
190 int expandabley = thisbox->minheight - thisbox->usedpady;
191
192 if(expandabley)
193 height = (int)(((float)height / (float)expandabley) * (float)(y - thisbox->usedpady));
194 }
195 else
196 height = y - (itempad + thispad + thisbox->grouppady);
197 }
198
199 /* If the calculated size is valid... */
354 if(width > 0 && height > 0) 200 if(width > 0 && height > 0)
355 { 201 {
356 char tmpbuf[100]; 202 int pad = thisbox->items[z].pad;
357 /* This is a hack to fix rounding of the sizing */ 203 HWND handle = thisbox->items[z].hwnd;
358 if(*depth == 0)
359 {
360 vectorx++;
361 vectory++;
362 }
363
364 /* If this item isn't going to expand... reset the vectors to 0 */
365 if(thisbox->items[z].vsize != SIZEEXPAND)
366 vectory = 0;
367 if(thisbox->items[z].hsize != SIZEEXPAND)
368 vectorx = 0;
369 204
370 #if 0 205 #if 0
371 /* Here you put your platform specific placement widget placement code */ 206 /* Here you put your platform specific placement widget placement code */
372 PlaceWidget(handle, currentx + pad, currenty + pad, 207 PlaceWidget(handle, currentx + pad, currenty + pad, width, height);
373 width + vectorx, height + vectory);
374 208
375 /* If any special handling needs to be done... like diving into 209 /* If any special handling needs to be done... like diving into
376 * controls that have sub-layouts... like notebooks or splitbars... 210 * controls that have sub-layouts... like notebooks or splitbars...
377 * do that here. Figure out the sub-layout size and call _do_resize(). 211 * do that here. Figure out the sub-layout size and call _do_resize().
378 */ 212 */
379 #endif 213 #endif
380 214
215 /* Advance the current position in the box */
381 if(thisbox->type == DW_HORZ) 216 if(thisbox->type == DW_HORZ)
382 currentx += width + vectorx + (pad * 2); 217 currentx += width + (pad * 2);
383 if(thisbox->type == DW_VERT) 218 if(thisbox->type == DW_VERT)
384 currenty += height + vectory + (pad * 2); 219 currenty += height + (pad * 2);
385 } 220 }
386 } 221 }
387 } 222 }
388 return 0;
389 } 223 }
390 224
391 /* This is a convenience function used in the window's resize event 225 /* This is a convenience function used in the window's resize event
392 * to relayout the controls in the window. 226 * to relayout the controls in the window.
393 */ 227 */
395 { 229 {
396 if(x != 0 && y != 0) 230 if(x != 0 && y != 0)
397 { 231 {
398 if(thisbox) 232 if(thisbox)
399 { 233 {
400 int usedx = 0, usedy = 0, depth = 0, usedpadx = 0, usedpady = 0; 234 int depth = 0;
401 235
402 _resize_box(thisbox, &depth, x, y, &usedx, &usedy, 1, &usedpadx, &usedpady); 236 /* Calculate space requirements */
403 237 _resize_box(thisbox, &depth, x, y, 1);
404 if(usedx-usedpadx == 0 || usedy-usedpady == 0) 238
405 return; 239 /* Finally place all the boxes and controls */
406 240 _resize_box(thisbox, &depth, x, y, 2);
407 thisbox->xratio = ((float)(x-usedpadx))/((float)(usedx-usedpadx));
408 thisbox->yratio = ((float)(y-usedpady))/((float)(usedy-usedpady));
409
410 usedpadx = usedpady = usedx = usedy = depth = 0;
411
412 _resize_box(thisbox, &depth, x, y, &usedx, &usedy, 2, &usedpadx, &usedpady);
413 } 241 }
414 } 242 }
415 } 243 }
416 244
417 /* 245 /*