- Timestamp:
- 07/11/08 16:41:58 (4 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/itmill/toolkit/terminal/gwt/client/ui/IOrderedLayout.java
r5095 r5096 135 135 return; 136 136 } 137 boolean oldTableMode = tableMode; 137 138 tableMode = newTableMode; 138 139 … … 176 177 WidgetWrapper wr = (WidgetWrapper) childWidgetWrappers.get(i); 177 178 orientationMode = oldOrientationMode; 179 tableMode = oldTableMode; 178 180 Element oldWrElement = wr.getElementWrappingWidgetAndCaption(); 179 181 orientationMode = currentOrientationMode; 182 tableMode = newTableMode; 183 String classe = DOM.getElementAttribute(oldWrElement, "class"); 180 184 wr.resetRootElement(); 181 185 Element newWrElement = wr.getElementWrappingWidgetAndCaption(); 186 if (classe != null && classe.length() > 0) { 187 DOM.setElementAttribute(newWrElement, "class", classe); 188 } 182 189 while (DOM.getChildCount(oldWrElement) > 0) { 183 190 Element c = DOM.getFirstChild(oldWrElement); … … 306 313 307 314 // Reset sizes for the children 308 // TODO These might be optimized by combining these methods 309 updateChildHeights(); 310 updateChildWidths(); 315 updateChildSizes(); 311 316 312 317 // Paint children … … 369 374 } 370 375 371 /** Recalculate and apply child heights */ 372 private void updateChildHeights() { 376 /** Recalculate and apply the space given for each child in this layout. */ 377 private void updateChildSizes() { 378 379 int numChild = childWidgets.size(); 380 int childHeightTotal = -1; 381 int childHeightDivisor = 1; 382 int childWidthTotal = -1; 383 int childWidthDivisor = 1; 373 384 374 385 // Vertical layout is calculated by us … … 376 387 377 388 // Calculate the space for fixed contents minus marginals 378 int size;379 389 if (tableMode) { 380 390 … … 382 392 if (height != null && height.endsWith("px")) { 383 393 try { 384 size = Integer.parseInt(height.substring(0, height385 .length() - 2));394 childHeightTotal = Integer.parseInt(height.substring(0, 395 height.length() - 2)); 386 396 387 397 // For negative sizes, use measurements 388 if ( size< 0) {389 size= rootOffsetMeasure("offsetHeight");398 if (childHeightTotal < 0) { 399 childHeightTotal = rootOffsetMeasure("offsetHeight"); 390 400 } 391 401 } catch (NumberFormatException e) { 392 402 393 403 // In case of invalid number, try to measure the size; 394 size= rootOffsetMeasure("offsetHeight");404 childHeightTotal = rootOffsetMeasure("offsetHeight"); 395 405 } 396 406 } 397 407 // If not, try to measure the size 398 408 else { 399 size= rootOffsetMeasure("offsetHeight");409 childHeightTotal = rootOffsetMeasure("offsetHeight"); 400 410 } 401 411 402 412 } else { 403 size = DOM.getElementPropertyInt(root, "offsetHeight"); 404 } 405 406 size -= margins.hasTop() ? marginTop : 0; 407 size -= margins.hasBottom() ? marginBottom : 0; 413 childHeightTotal = DOM.getElementPropertyInt(root, 414 "offsetHeight"); 415 } 416 417 childHeightTotal -= margins.hasTop() ? marginTop : 0; 418 childHeightTotal -= margins.hasBottom() ? marginBottom : 0; 408 419 409 420 // Reduce spacing from the size 410 int numChild = childWidgets.size();411 421 if (hasComponentSpacing) { 412 size-= ((orientationMode == ORIENTATION_HORIZONTAL) ? hSpacing422 childHeightTotal -= ((orientationMode == ORIENTATION_HORIZONTAL) ? hSpacing 413 423 : vSpacing) 414 424 * (numChild - 1); 415 425 } 416 426 417 // Set the sizes for each child 427 // Total space is divided among the children 428 if (orientationMode == ORIENTATION_VERTICAL) { 429 childHeightDivisor = numChild; 430 } 431 } 432 433 // layout is calculated by us 434 if (width != null) { 435 436 // Calculate the space for fixed contents minus marginals 437 // If we know explicitly set pixel-size, use that 438 if (width != null && width.endsWith("px")) { 439 try { 440 childWidthTotal = Integer.parseInt(width.substring(0, width 441 .length() - 2)); 442 443 // For negative sizes, use measurements 444 if (childWidthTotal < 0) { 445 childWidthTotal = rootOffsetMeasure("offsetWidth"); 446 } 447 448 } catch (NumberFormatException e) { 449 450 // In case of invalid number, try to measure the size; 451 childWidthTotal = rootOffsetMeasure("offsetWidth"); 452 } 453 } 454 // If not, try to measure the size 455 else { 456 childWidthTotal = rootOffsetMeasure("offsetWidth"); 457 } 458 459 childWidthTotal -= margins.hasLeft() ? marginLeft : 0; 460 childWidthTotal -= margins.hasRight() ? marginRight : 0; 461 462 // Reduce spacing from the size 463 if (hasComponentSpacing 464 && orientationMode == ORIENTATION_HORIZONTAL) { 465 childWidthTotal -= hSpacing * (numChild - 1); 466 } 467 468 // Total space is divided among the children 418 469 if (orientationMode == ORIENTATION_HORIZONTAL) { 419 for (Iterator i = childWidgetWrappers.iterator(); i.hasNext();) { 420 ((WidgetWrapper) i.next()).forceHeight(size); 421 } 470 childWidthDivisor = numChild; 471 } 472 } 473 474 // Set the sizes for each child 475 for (Iterator i = childWidgetWrappers.iterator(); i.hasNext();) { 476 int w, h; 477 if (childHeightDivisor > 1) { 478 h = Math.round(((float) childHeightTotal) 479 / (childHeightDivisor--)); 480 childHeightTotal -= h; 422 481 } else { 423 for (Iterator i = childWidgetWrappers.iterator(); i.hasNext();) { 424 final int ws = Math.round(((float) size) / (numChild--)); 425 size -= ws; 426 ((WidgetWrapper) i.next()).forceHeight(ws); 427 } 428 } 429 } 430 431 // Vertically layout is calculated by the browsers 432 else { 433 for (Iterator i = childWidgetWrappers.iterator(); i.hasNext();) { 434 ((WidgetWrapper) i.next()).forceHeight(-1); 435 } 482 h = childHeightTotal; 483 } 484 if (childWidthDivisor > 1) { 485 w = Math.round(((float) childWidthTotal) 486 / (childWidthDivisor--)); 487 childWidthTotal -= h; 488 } else { 489 w = childWidthTotal; 490 } 491 WidgetWrapper ww = (WidgetWrapper) i.next(); 492 // TODO COMBINE THESE 493 ww.forceSize(w, h); 436 494 } 437 495 } … … 460 518 } 461 519 462 /** Recalculate and apply child widths */463 private void updateChildWidths() {464 // layout is calculated by us465 if (width != null) {466 467 // Calculate the space for fixed contents minus marginals468 int size;469 // If we know explicitly set pixel-size, use that470 if (width != null && width.endsWith("px")) {471 try {472 size = Integer.parseInt(width.substring(0,473 width.length() - 2));474 475 // For negative sizes, use measurements476 if (size < 0) {477 size = rootOffsetMeasure("offsetWidth");478 }479 480 } catch (NumberFormatException e) {481 482 // In case of invalid number, try to measure the size;483 size = rootOffsetMeasure("offsetWidth");484 }485 }486 // If not, try to measure the size487 else {488 size = rootOffsetMeasure("offsetWidth");489 }490 491 size -= margins.hasLeft() ? marginLeft : 0;492 size -= margins.hasRight() ? marginRight : 0;493 494 // Reduce spacing from the size495 int numChild = childWidgets.size();496 if (hasComponentSpacing497 && orientationMode == ORIENTATION_HORIZONTAL) {498 size -= hSpacing * (numChild - 1);499 }500 501 // Set the sizes for each child502 if (orientationMode == ORIENTATION_HORIZONTAL) {503 for (Iterator i = childWidgetWrappers.iterator(); i.hasNext();) {504 final int ws = Math.round(((float) size) / (numChild--));505 size -= ws;506 ((WidgetWrapper) i.next()).forceWidth(ws);507 }508 } else {509 for (Iterator i = childWidgetWrappers.iterator(); i.hasNext();) {510 ((WidgetWrapper) i.next()).forceWidth(size);511 }512 }513 }514 515 // Layout is calculated by the browsers516 else {517 for (Iterator i = childWidgetWrappers.iterator(); i.hasNext();) {518 ((WidgetWrapper) i.next()).forceWidth(-1);519 }520 }521 522 }523 524 520 /** Parse alignments from UIDL and pass whem to correct widgetwrappers */ 525 521 private void handleAlignmentsSpacingAndMargins(UIDL uidl) { … … 600 596 601 597 /** 602 * Set the height given for the wrapped widget in pixels.598 * Set the width and height given for the wrapped widget in pixels. 603 599 * 604 600 * -1 if unconstrained. 605 601 */ 606 public void force Height(int pixelHeight) {602 public void forceSize(int pixelWidth, int pixelHeight) { 607 603 608 604 // If we are already at the correct size, do nothing 609 if (lastForcedPixelHeight == pixelHeight) { 605 if (lastForcedPixelHeight == pixelHeight 606 && lastForcedPixelWidth == pixelWidth) { 610 607 return; 611 608 } 612 609 613 610 // Clipper DIV is needed? 614 if (tableMode) { 615 if (pixelHeight >= 0) { 616 if (clipperDiv == null) { 617 createClipperDiv(); 618 } 611 if (tableMode && (pixelHeight >= 0 || pixelWidth >= 0)) { 612 if (clipperDiv == null) { 613 createClipperDiv(); 619 614 } 620 // Needed to remove unnecessary clipper DIV 621 else if (clipperDiv != null && lastForcedPixelWidth < 0) { 622 removeClipperDiv(); 623 } 624 } 615 } 616 617 // ClipperDiv is not needed, remove if necessary 618 else if (clipperDiv != null) { 619 removeClipperDiv(); 620 } 621 625 622 Element e = clipperDiv != null ? clipperDiv 626 623 : getElementWrappingAlignmentStructures(); 627 624 628 625 // Overflow 626 DOM.setStyleAttribute(e, "overflowX", pixelWidth < 0 ? "" 627 : "hidden"); 629 628 DOM.setStyleAttribute(e, "overflowY", pixelHeight < 0 ? "" 630 629 : "hidden"); 631 630 632 // Set height 631 // Set size 632 DOM.setStyleAttribute(e, "width", pixelWidth < 0 ? "" : pixelWidth 633 + "px"); 633 634 DOM.setStyleAttribute(e, "height", 634 635 pixelHeight < 0 ? (e == clipperDiv || !tableMode ? "100%" 635 636 : "") : pixelHeight + "px"); 636 637 638 // Set cached values 639 lastForcedPixelWidth = pixelWidth; 637 640 lastForcedPixelHeight = pixelHeight; 638 }639 640 /**641 * Set the width given for the wrapped widget in pixels.642 *643 * -1 if unconstrained.644 */645 public void forceWidth(int pixelWidth) {646 647 // If we are already at the correct size, do nothing648 if (lastForcedPixelWidth == pixelWidth) {649 return;650 }651 652 // Clipper DIV needed653 if (tableMode) {654 if (pixelWidth >= 0) {655 if (clipperDiv == null) {656 createClipperDiv();657 }658 }659 // Needed to remove unnecessary clipper DIV660 else if (clipperDiv != null && lastForcedPixelHeight < 0) {661 removeClipperDiv();662 }663 }664 Element e = clipperDiv != null ? clipperDiv665 : getElementWrappingAlignmentStructures();666 667 // Overflow668 DOM.setStyleAttribute(e, "overflowX", pixelWidth < 0 ? ""669 : "hidden");670 671 // Set width672 DOM.setStyleAttribute(e, "width", pixelWidth < 0 ? "" : pixelWidth673 + "px");674 675 lastForcedPixelWidth = pixelWidth;676 641 } 677 642 … … 680 645 clipperDiv = DOM.createDiv(); 681 646 final Element e = getElementWrappingClipperDiv(); 647 String classe = DOM.getElementAttribute(e, "class"); 682 648 while (DOM.getChildCount(e) > 0) { 683 649 final Element c = DOM.getFirstChild(e); … … 685 651 DOM.appendChild(clipperDiv, c); 686 652 } 653 if (classe != null && classe.length() > 0) { 654 DOM.removeElementAttribute(e, "class"); 655 DOM.setElementAttribute(clipperDiv, "class", classe); 656 } 687 657 DOM.appendChild(e, clipperDiv); 688 658 } … … 691 661 private void removeClipperDiv() { 692 662 final Element e = getElementWrappingClipperDiv(); 663 String classe = DOM.getElementAttribute(clipperDiv, "class"); 693 664 while (DOM.getChildCount(clipperDiv) > 0) { 694 665 final Element c = DOM.getFirstChild(clipperDiv); … … 698 669 DOM.removeChild(e, clipperDiv); 699 670 clipperDiv = null; 671 if (classe != null && classe.length() > 0) { 672 DOM.setElementAttribute(e, "class", classe); 673 } 700 674 } 701 675 … … 1171 1145 /* documented at super */ 1172 1146 public void iLayout() { 1173 updateChildHeights(); 1174 updateChildWidths(); 1147 updateChildSizes(); 1175 1148 Util.runDescendentsLayout(this); 1176 1149 childLayoutsHaveChanged = false;
