1 11 package org.eclipse.swt.layout; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.graphics.*; 15 import org.eclipse.swt.widgets.*; 16 17 87 public final class FormLayout extends Layout { 88 89 95 public int marginWidth = 0; 96 97 103 public int marginHeight = 0; 104 105 106 114 public int marginLeft = 0; 115 116 124 public int marginTop = 0; 125 126 134 public int marginRight = 0; 135 136 144 public int marginBottom = 0; 145 146 154 public int spacing = 0; 155 156 159 public FormLayout () { 160 } 161 162 222 int computeHeight (Control control, FormData data, boolean flushCache) { 223 FormAttachment top = data.getTopAttachment (control, spacing, flushCache); 224 FormAttachment bottom = data.getBottomAttachment (control, spacing, flushCache); 225 FormAttachment height = bottom.minus (top); 226 if (height.numerator == 0) { 227 if (bottom.numerator == 0) return bottom.offset; 228 if (bottom.numerator == bottom.denominator) return -top.offset; 229 if (bottom.offset <= 0) { 230 return -top.offset * top.denominator / bottom.numerator; 231 } 232 int divider = bottom.denominator - bottom.numerator; 233 return bottom.denominator * bottom.offset / divider; 234 } 235 return height.solveY (data.getHeight (control, flushCache)); 236 } 237 238 protected Point computeSize (Composite composite, int wHint, int hHint, boolean flushCache) { 239 Point size = layout (composite, false, 0, 0, wHint, hHint, flushCache); 240 if (wHint != SWT.DEFAULT) size.x = wHint; 241 if (hHint != SWT.DEFAULT) size.y = hHint; 242 return size; 243 } 244 245 protected boolean flushCache (Control control) { 246 Object data = control.getLayoutData (); 247 if (data != null) ((FormData) data).flushCache (); 248 return true; 249 } 250 251 String getName () { 252 String string = getClass ().getName (); 253 int index = string.lastIndexOf ('.'); 254 if (index == -1) return string; 255 return string.substring (index + 1, string.length ()); 256 } 257 258 262 int computeWidth (Control control, FormData data, boolean flushCache) { 263 FormAttachment left = data.getLeftAttachment (control, spacing, flushCache); 264 FormAttachment right = data.getRightAttachment (control, spacing, flushCache); 265 FormAttachment width = right.minus (left); 266 if (width.numerator == 0) { 267 if (right.numerator == 0) return right.offset; 268 if (right.numerator == right.denominator) return -left.offset; 269 if (right.offset <= 0) { 270 return -left.offset * left.denominator / left.numerator; 271 } 272 int divider = right.denominator - right.numerator; 273 return right.denominator * right.offset / divider; 274 } 275 return width.solveY (data.getWidth (control, flushCache)); 276 } 277 278 protected void layout (Composite composite, boolean flushCache) { 279 Rectangle rect = composite.getClientArea (); 280 int x = rect.x + marginLeft + marginWidth; 281 int y = rect.y + marginTop + marginHeight; 282 int width = Math.max (0, rect.width - marginLeft - 2 * marginWidth - marginRight); 283 int height = Math.max (0, rect.height - marginTop - 2 * marginHeight - marginBottom); 284 layout (composite, true, x, y, width, height, flushCache); 285 } 286 287 Point layout (Composite composite, boolean move, int x, int y, int width, int height, boolean flushCache) { 288 Control [] children = composite.getChildren (); 289 for (int i=0; i<children.length; i++) { 290 Control child = children [i]; 291 FormData data = (FormData) child.getLayoutData (); 292 if (data == null) child.setLayoutData (data = new FormData ()); 293 if (flushCache) data.flushCache (); 294 data.cacheLeft = data.cacheRight = data.cacheTop = data.cacheBottom = null; 295 } 296 boolean [] flush = null; 297 Rectangle [] bounds = null; 298 int w = 0, h = 0; 299 for (int i=0; i<children.length; i++) { 300 Control child = children [i]; 301 FormData data = (FormData) child.getLayoutData (); 302 if (width != SWT.DEFAULT) { 303 data.needed = false; 304 FormAttachment left = data.getLeftAttachment (child, spacing, flushCache); 305 FormAttachment right = data.getRightAttachment (child, spacing, flushCache); 306 int x1 = left.solveX (width), x2 = right.solveX (width); 307 if (data.height == SWT.DEFAULT && !data.needed) { 308 int trim = 0; 309 if (child instanceof Scrollable) { 311 Rectangle rect = ((Scrollable) child).computeTrim (0, 0, 0, 0); 312 trim = rect.width; 313 } else { 314 trim = child.getBorderWidth () * 2; 315 } 316 data.cacheWidth = data.cacheHeight = -1; 317 int currentWidth = Math.max (0, x2 - x1 - trim); 318 data.computeSize (child, currentWidth, data.height, flushCache); 319 if (flush == null) flush = new boolean [children.length]; 320 flush [i] = true; 321 } 322 w = Math.max (x2, w); 323 if (move) { 324 if (bounds == null) bounds = new Rectangle [children.length]; 325 bounds [i] = new Rectangle (0, 0, 0, 0); 326 bounds [i].x = x + x1; 327 bounds [i].width = x2 - x1; 328 } 329 } else { 330 w = Math.max (computeWidth (child, data, flushCache), w); 331 } 332 } 333 for (int i=0; i<children.length; i++) { 334 Control child = children [i]; 335 FormData data = (FormData) child.getLayoutData (); 336 if (height != SWT.DEFAULT) { 337 int y1 = data.getTopAttachment (child, spacing, flushCache).solveX (height); 338 int y2 = data.getBottomAttachment (child, spacing, flushCache).solveX (height); 339 h = Math.max (y2, h); 340 if (move) { 341 bounds [i].y = y + y1; 342 bounds [i].height = y2 - y1; 343 } 344 } else { 345 h = Math.max (computeHeight (child, data, flushCache), h); 346 } 347 } 348 for (int i=0; i<children.length; i++) { 349 Control child = children [i]; 350 FormData data = (FormData) child.getLayoutData (); 351 if (flush != null && flush [i]) data.cacheWidth = data.cacheHeight = -1; 352 data.cacheLeft = data.cacheRight = data.cacheTop = data.cacheBottom = null; 353 } 354 if (move) { 355 for (int i=0; i<children.length; i++) { 356 children [i].setBounds (bounds [i]); 357 } 358 } 359 w += marginLeft + marginWidth * 2 + marginRight; 360 h += marginTop + marginHeight * 2 + marginBottom; 361 return new Point (w, h); 362 } 363 364 370 public String toString () { 371 String string = getName ()+" {"; 372 if (marginWidth != 0) string += "marginWidth="+marginWidth+" "; 373 if (marginHeight != 0) string += "marginHeight="+marginHeight+" "; 374 if (marginLeft != 0) string += "marginLeft="+marginLeft+" "; 375 if (marginRight != 0) string += "marginRight="+marginRight+" "; 376 if (marginTop != 0) string += "marginTop="+marginTop+" "; 377 if (marginBottom != 0) string += "marginBottom="+marginBottom+" "; 378 if (spacing != 0) string += "spacing="+spacing+" "; 379 string = string.trim(); 380 string += "}"; 381 return string; 382 } 383 } 384 | Popular Tags |