1 11 package org.eclipse.ui.internal.presentations.r21.widgets; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.SWTException; 15 import org.eclipse.swt.events.ControlAdapter; 16 import org.eclipse.swt.events.ControlEvent; 17 import org.eclipse.swt.events.PaintEvent; 18 import org.eclipse.swt.events.PaintListener; 19 import org.eclipse.swt.graphics.Color; 20 import org.eclipse.swt.graphics.Font; 21 import org.eclipse.swt.graphics.GC; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.graphics.RGB; 24 import org.eclipse.swt.graphics.Rectangle; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Event; 28 import org.eclipse.swt.widgets.Layout; 29 import org.eclipse.swt.widgets.Listener; 30 31 50 51 public class ViewForm extends Composite { 52 53 59 public int marginWidth = 0; 60 61 67 public int marginHeight = 0; 68 69 72 public static RGB borderInsideRGB = new RGB(132, 130, 132); 73 74 77 public static RGB borderMiddleRGB = new RGB(143, 141, 138); 78 79 82 public static RGB borderOutsideRGB = new RGB(171, 168, 165); 83 84 private Control topLeft; 86 87 private Control topCenter; 88 89 private Control topRight; 90 91 private Control content; 92 93 private boolean separateTopCenter = false; 95 96 private int drawLine1 = -1; 97 98 private int drawLine2 = -1; 99 100 private boolean showBorder = false; 101 102 private int BORDER_TOP = 0; 103 104 private int BORDER_BOTTOM = 0; 105 106 private int BORDER_LEFT = 0; 107 108 private int BORDER_RIGHT = 0; 109 110 private Color borderColor1; 111 112 private Color borderColor2; 113 114 private Color borderColor3; 115 116 private Rectangle oldArea; 117 118 private static final int OFFSCREEN = -200; 119 120 147 public ViewForm(Composite parent, int style) { 148 super(parent, checkStyle(style)); 149 150 borderColor1 = new Color(getDisplay(), borderInsideRGB); 151 borderColor2 = new Color(getDisplay(), borderMiddleRGB); 152 borderColor3 = new Color(getDisplay(), borderOutsideRGB); 153 setBorderVisible((style & SWT.BORDER) != 0); 154 155 addPaintListener(new PaintListener() { 156 public void paintControl(PaintEvent event) { 157 onPaint(event.gc); 158 } 159 }); 160 addControlListener(new ControlAdapter() { 161 public void controlResized(ControlEvent e) { 162 onResize(); 163 } 164 }); 165 166 addListener(SWT.Dispose, new Listener() { 167 public void handleEvent(Event e) { 168 onDispose(); 169 } 170 }); 171 } 172 173 177 private static int checkStyle(int style) { 178 int mask = SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; 179 return style & mask | SWT.NO_REDRAW_RESIZE; 180 } 181 182 public Point computeSize(int wHint, int hHint, boolean changed) { 183 checkWidget(); 184 Point leftSize = new Point(0, 0); 186 if (topLeft != null) { 187 leftSize = topLeft.computeSize(SWT.DEFAULT, SWT.DEFAULT); 188 leftSize.x += 1; } 190 Point centerSize = new Point(0, 0); 191 if (topCenter != null) { 192 centerSize = topCenter.computeSize(SWT.DEFAULT, SWT.DEFAULT); 193 } 194 Point rightSize = new Point(0, 0); 195 if (topRight != null) { 196 rightSize = topRight.computeSize(SWT.DEFAULT, SWT.DEFAULT); 197 } 198 Point size = new Point(0, 0); 199 if (separateTopCenter 201 || (wHint != SWT.DEFAULT && leftSize.x + centerSize.x 202 + rightSize.x > wHint)) { 203 size.x = leftSize.x + rightSize.x; 204 size.x = Math.max(centerSize.x, size.x); 205 size.y = Math.max(leftSize.y, rightSize.y) + 1; if (topCenter != null) { 207 size.y += centerSize.y; 208 } 209 } else { 210 size.x = leftSize.x + centerSize.x + rightSize.x; 211 size.y = Math.max(leftSize.y, Math.max(centerSize.y, rightSize.y)) + 1; } 213 214 if (content != null) { 215 Point contentSize = new Point(0, 0); 216 contentSize = content.computeSize(SWT.DEFAULT, SWT.DEFAULT); 217 size.x = Math.max(size.x, contentSize.x); 218 size.y += contentSize.y + 1; } 220 221 size.x += 2 * marginWidth; 222 size.y += 2 * marginHeight; 223 224 if (wHint != SWT.DEFAULT) { 225 size.x = wHint; 226 } 227 if (hHint != SWT.DEFAULT) { 228 size.y = hHint; 229 } 230 231 Rectangle trim = computeTrim(0, 0, size.x, size.y); 232 return new Point(trim.width, trim.height); 233 } 234 235 public Rectangle computeTrim(int x, int y, int width, int height) { 236 checkWidget(); 237 int trimX = x - BORDER_LEFT; 238 int trimY = y - BORDER_TOP; 239 int trimWidth = width + BORDER_LEFT + BORDER_RIGHT; 240 int trimHeight = height + BORDER_TOP + BORDER_BOTTOM; 241 return new Rectangle(trimX, trimY, trimWidth, trimHeight); 242 } 243 244 public Rectangle getClientArea() { 245 checkWidget(); 246 Rectangle clientArea = super.getClientArea(); 247 clientArea.x += BORDER_LEFT; 248 clientArea.y += BORDER_TOP; 249 clientArea.width -= BORDER_LEFT + BORDER_RIGHT; 250 clientArea.height -= BORDER_TOP + BORDER_BOTTOM; 251 return clientArea; 252 } 253 254 259 public Control getContent() { 260 return content; 262 } 263 264 270 public Control getTopCenter() { 271 return topCenter; 273 } 274 275 281 public Control getTopLeft() { 282 return topLeft; 284 } 285 286 292 public Control getTopRight() { 293 return topRight; 295 } 296 297 public void layout(boolean changed) { 298 checkWidget(); 299 Rectangle rect = getClientArea(); 300 301 drawLine1 = -1; 302 drawLine2 = -1; 303 304 Point leftSize = new Point(0, 0); 305 if (topLeft != null && !topLeft.isDisposed()) { 306 leftSize = topLeft.computeSize(SWT.DEFAULT, SWT.DEFAULT); 307 } 308 Point centerSize = new Point(0, 0); 309 if (topCenter != null && !topCenter.isDisposed()) { 310 centerSize = topCenter.computeSize(SWT.DEFAULT, SWT.DEFAULT); 311 } 312 Point rightSize = new Point(0, 0); 313 if (topRight != null && !topRight.isDisposed()) { 314 rightSize = topRight.computeSize(SWT.DEFAULT, SWT.DEFAULT); 315 } 316 317 int minTopWidth = leftSize.x + centerSize.x + rightSize.x + 2 318 * marginWidth + 1; int height = rect.y + marginHeight; 320 321 boolean top = false; 322 if (separateTopCenter || minTopWidth > rect.width) { 323 int topHeight = Math.max(rightSize.y, leftSize.y); 324 if (topRight != null && !topRight.isDisposed()) { 325 top = true; 326 topRight.setBounds(rect.x + rect.width - marginWidth 327 - rightSize.x, rect.y + 1 + marginHeight, rightSize.x, 328 topHeight); 329 height += 1 + topHeight; } 331 if (topLeft != null && !topLeft.isDisposed()) { 332 top = true; 333 leftSize = topLeft.computeSize(rect.width - 2 * marginWidth 334 - rightSize.x - 1, SWT.DEFAULT); 335 topLeft.setBounds(rect.x + 1 + marginWidth, rect.y + 1 336 + marginHeight, leftSize.x, topHeight); 337 height = Math 338 .max(height, rect.y + marginHeight + 1 + topHeight); } 340 if (topCenter != null && !topCenter.isDisposed()) { 341 top = true; 342 if (height > rect.y + marginHeight) { 343 drawLine1 = height; 344 height += 1; } 346 centerSize = topCenter.computeSize( 347 rect.width - 2 * marginWidth, SWT.DEFAULT); 348 topCenter.setBounds(rect.x + rect.width - marginWidth 349 - centerSize.x, height, centerSize.x, centerSize.y); 350 height += centerSize.y; 351 352 } 353 } else { 354 int topHeight = Math.max(rightSize.y, Math.max(centerSize.y, 355 leftSize.y)); 356 if (topRight != null && !topRight.isDisposed()) { 357 top = true; 358 topRight.setBounds(rect.x + rect.width - marginWidth 359 - rightSize.x, rect.y + marginHeight + 1, rightSize.x, topHeight); 361 height += 1 + topHeight; } 363 if (topCenter != null && !topCenter.isDisposed()) { 364 top = true; 365 topCenter.setBounds(rect.x + rect.width - marginWidth 366 - rightSize.x - centerSize.x, 367 rect.y + marginHeight + 1, centerSize.x, topHeight); 369 height = Math 370 .max(height, rect.y + marginHeight + 1 + topHeight); } 372 if (topLeft != null && !topLeft.isDisposed()) { 373 top = true; 374 leftSize = topLeft.computeSize(rect.width - 2 * marginWidth 375 - rightSize.x - centerSize.x - 1, topHeight); 376 topLeft.setBounds(rect.x + marginWidth + 1, rect.y + marginHeight + 1, leftSize.x, topHeight); 379 height = Math 380 .max(height, rect.y + marginHeight + 1 + topHeight); } 382 } 383 384 if (content != null && !content.isDisposed()) { 385 if (top) { 386 drawLine2 = height; 387 height += 1; } 389 content 390 .setBounds(rect.x + marginWidth, height, rect.width - 2 391 * marginWidth, rect.y + rect.height - height 392 - marginHeight); 393 } 394 } 395 396 private void onDispose() { 397 if (borderColor1 != null) { 398 borderColor1.dispose(); 399 } 400 borderColor1 = null; 401 402 if (borderColor2 != null) { 403 borderColor2.dispose(); 404 } 405 borderColor2 = null; 406 407 if (borderColor3 != null) { 408 borderColor3.dispose(); 409 } 410 borderColor3 = null; 411 412 topLeft = null; 413 topCenter = null; 414 topRight = null; 415 content = null; 416 oldArea = null; 417 } 418 419 422 private void onPaint(GC gc) { 423 Rectangle d = super.getClientArea(); 424 425 if (showBorder) { 426 if ((getStyle() & SWT.FLAT) != 0) { 427 gc.setForeground(borderColor1); 428 gc.drawRectangle(d.x, d.y, d.x + d.width - 1, d.y + d.height 429 - 1); 430 } else { 431 gc.setForeground(borderColor1); 432 gc.drawRectangle(d.x, d.y, d.x + d.width - 3, d.y + d.height 433 - 3); 434 435 gc.setForeground(borderColor2); 436 gc.drawLine(d.x + 1, d.y + d.height - 2, d.x + d.width - 1, d.y 437 + d.height - 2); 438 gc.drawLine(d.x + d.width - 2, d.y + 1, d.x + d.width - 2, d.y 439 + d.height - 1); 440 441 gc.setForeground(borderColor3); 442 gc.drawLine(d.x + 2, d.y + d.height - 1, d.x + d.width - 2, d.y 443 + d.height - 1); 444 gc.drawLine(d.x + d.width - 1, d.y + 2, d.x + d.width - 1, d.y 445 + d.height - 2); 446 } 447 } 448 449 if (drawLine1 != -1) { 450 gc.setForeground(borderColor1); 452 gc.drawLine(d.x + BORDER_LEFT, drawLine1, d.x + d.width 453 - BORDER_RIGHT, drawLine1); 454 } 455 if (drawLine2 != -1) { 456 gc.setForeground(borderColor1); 458 gc.drawLine(d.x + BORDER_LEFT, drawLine2, d.x + d.width 459 - BORDER_RIGHT, drawLine2); 460 } 461 int y = drawLine1; 463 if (y == -1) { 464 y = drawLine2; 465 } 466 if (y != -1) { 467 gc.setForeground(getDisplay().getSystemColor( 468 SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW)); 469 gc.drawLine(d.x + BORDER_LEFT + marginWidth, d.y + BORDER_TOP 470 + marginHeight, d.x + BORDER_LEFT + marginWidth, y - 1); 471 gc.drawLine(d.x + BORDER_LEFT + marginWidth, d.y + BORDER_TOP 472 + marginHeight, d.x + d.width - BORDER_RIGHT - marginWidth 473 - 1, d.y + BORDER_TOP + marginHeight); 474 } 475 476 gc.setForeground(getForeground()); 477 } 478 479 private void onResize() { 480 layout(); 481 482 Rectangle area = super.getClientArea(); 483 if (oldArea == null || oldArea.width == 0 || oldArea.height == 0) { 484 redraw(); 485 } else { 486 int width = 0; 487 if (oldArea.width < area.width) { 488 width = area.width - oldArea.width + BORDER_RIGHT; 489 } else if (oldArea.width > area.width) { 490 width = BORDER_RIGHT; 491 } 492 redraw(area.x + area.width - width, area.y, width, area.height, 493 false); 494 495 int height = 0; 496 if (oldArea.height < area.height) { 497 height = area.height - oldArea.height + BORDER_BOTTOM; 498 } 499 if (oldArea.height > area.height) { 500 height = BORDER_BOTTOM; 501 } 502 redraw(area.x, area.y + area.height - height, area.width, height, 503 false); 504 } 505 oldArea = area; 506 } 507 508 520 public void setContent(Control content) { 521 checkWidget(); 522 if (content != null && content.getParent() != this) { 523 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 524 } 525 if (this.content != null && !this.content.isDisposed()) { 526 this.content.setBounds(OFFSCREEN, OFFSCREEN, 0, 0); 527 } 528 this.content = content; 529 layout(); 530 } 531 532 536 public void setFont(Font f) { 537 super.setFont(f); 538 if (topLeft != null && !topLeft.isDisposed()) { 539 topLeft.setFont(f); 540 } 541 if (topCenter != null && !topCenter.isDisposed()) { 542 topCenter.setFont(f); 543 } 544 if (topRight != null && !topRight.isDisposed()) { 545 topRight.setFont(f); 546 } 547 548 layout(); 549 } 550 551 565 public void setLayout(Layout layout) { 566 checkWidget(); 567 return; 568 } 569 570 583 public void setTopCenter(Control topCenter) { 584 checkWidget(); 585 if (topCenter != null && topCenter.getParent() != this) { 586 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 587 } 588 if (this.topCenter != null && !this.topCenter.isDisposed()) { 589 this.topCenter.setBounds(OFFSCREEN, OFFSCREEN, 0, 0); 590 } 591 this.topCenter = topCenter; 592 layout(); 593 } 594 595 608 public void setTopLeft(Control c) { 609 checkWidget(); 610 if (c != null && c.getParent() != this) { 611 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 612 } 613 if (this.topLeft != null && !this.topLeft.isDisposed()) { 614 this.topLeft.setBounds(OFFSCREEN, OFFSCREEN, 0, 0); 615 } 616 this.topLeft = c; 617 layout(); 618 } 619 620 634 public void setTopRight(Control c) { 635 checkWidget(); 636 if (c != null && c.getParent() != this) { 637 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 638 } 639 if (this.topRight != null && !this.topRight.isDisposed()) { 640 this.topRight.setBounds(OFFSCREEN, OFFSCREEN, 0, 0); 641 } 642 this.topRight = c; 643 layout(); 644 } 645 646 656 public void setBorderVisible(boolean show) { 657 checkWidget(); 658 if (showBorder == show) { 659 return; 660 } 661 662 showBorder = show; 663 if (showBorder) { 664 if ((getStyle() & SWT.FLAT) != 0) { 665 BORDER_LEFT = BORDER_TOP = BORDER_RIGHT = BORDER_BOTTOM = 1; 666 } else { 667 BORDER_LEFT = BORDER_TOP = 1; 668 BORDER_RIGHT = BORDER_BOTTOM = 3; 669 } 670 } else { 671 BORDER_BOTTOM = BORDER_TOP = BORDER_LEFT = BORDER_RIGHT = 0; 672 } 673 674 layout(); 675 redraw(); 676 } 677 678 690 public void setTopCenterSeparate(boolean show) { 691 checkWidget(); 692 separateTopCenter = show; 693 layout(); 694 } 695 696 } 697 | Popular Tags |