1 11 package org.eclipse.swt.custom; 12 13 14 import org.eclipse.swt.graphics.*; 15 import org.eclipse.swt.widgets.*; 16 import org.eclipse.swt.*; 17 18 37 38 public class ViewForm extends Composite { 39 40 46 public int marginWidth = 0; 47 53 public int marginHeight = 0; 54 61 public int horizontalSpacing = 1; 62 68 public int verticalSpacing = 1; 69 70 78 public static RGB borderInsideRGB = new RGB (132, 130, 132); 79 87 public static RGB borderMiddleRGB = new RGB (143, 141, 138); 88 96 public static RGB borderOutsideRGB = new RGB (171, 168, 165); 97 98 Control topLeft; 100 Control topCenter; 101 Control topRight; 102 Control content; 103 104 boolean separateTopCenter = false; 106 boolean showBorder = false; 107 108 int separator = -1; 109 int borderTop = 0; 110 int borderBottom = 0; 111 int borderLeft = 0; 112 int borderRight = 0; 113 int highlight = 0; 114 Point oldSize; 115 116 Color selectionBackground; 117 118 static final int OFFSCREEN = -200; 119 static final int BORDER1_COLOR = SWT.COLOR_WIDGET_NORMAL_SHADOW; 120 static final int SELECTION_BACKGROUND = SWT.COLOR_LIST_BACKGROUND; 121 148 public ViewForm(Composite parent, int style) { 149 super(parent, checkStyle(style)); 150 super.setLayout(new ViewFormLayout()); 151 152 setBorderVisible((style & SWT.BORDER) != 0); 153 154 Listener listener = new Listener() { 155 public void handleEvent(Event e) { 156 switch (e.type) { 157 case SWT.Dispose: onDispose(); break; 158 case SWT.Paint: onPaint(e.gc); break; 159 case SWT.Resize: onResize(); break; 160 } 161 } 162 }; 163 164 int[] events = new int[] {SWT.Dispose, SWT.Paint, SWT.Resize}; 165 166 for (int i = 0; i < events.length; i++) { 167 addListener(events[i], listener); 168 } 169 } 170 171 static int checkStyle (int style) { 172 int mask = SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; 173 return style & mask | SWT.NO_REDRAW_RESIZE; 174 } 175 176 184 public Rectangle computeTrim (int x, int y, int width, int height) { 185 checkWidget (); 186 int trimX = x - borderLeft - highlight; 187 int trimY = y - borderTop - highlight; 188 int trimWidth = width + borderLeft + borderRight + 2*highlight; 189 int trimHeight = height + borderTop + borderBottom + 2*highlight; 190 return new Rectangle(trimX, trimY, trimWidth, trimHeight); 191 } 192 public Rectangle getClientArea() { 193 checkWidget(); 194 Rectangle clientArea = super.getClientArea(); 195 clientArea.x += borderLeft; 196 clientArea.y += borderTop; 197 clientArea.width -= borderLeft + borderRight; 198 clientArea.height -= borderTop + borderBottom; 199 return clientArea; 200 } 201 206 public Control getContent() { 207 return content; 209 } 210 216 public Control getTopCenter() { 217 return topCenter; 219 } 220 226 public Control getTopLeft() { 227 return topLeft; 229 } 230 236 public Control getTopRight() { 237 return topRight; 239 } 240 void onDispose() { 241 topLeft = null; 242 topCenter = null; 243 topRight = null; 244 content = null; 245 oldSize = null; 246 selectionBackground = null; 247 } 248 void onPaint(GC gc) { 249 Color gcForeground = gc.getForeground(); 250 Point size = getSize(); 251 Color border = getDisplay().getSystemColor(BORDER1_COLOR); 252 if (showBorder) { 253 gc.setForeground(border); 254 gc.drawRectangle(0, 0, size.x - 1, size.y - 1); 255 if (highlight > 0) { 256 int x1 = 1; 257 int y1 = 1; 258 int x2 = size.x - 1; 259 int y2 = size.y - 1; 260 int[] shape = new int[] {x1,y1, x2,y1, x2,y2, x1,y2, x1,y1+highlight, 261 x1+highlight,y1+highlight, x1+highlight,y2-highlight, 262 x2-highlight,y2-highlight, x2-highlight,y1+highlight, x1,y1+highlight}; 263 Color highlightColor = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION); 264 gc.setBackground(highlightColor); 265 gc.fillPolygon(shape); 266 } 267 } 268 if (separator > -1) { 269 gc.setForeground(border); 270 gc.drawLine(borderLeft + highlight, separator, size.x - borderLeft - borderRight - highlight, separator); 271 } 272 gc.setForeground(gcForeground); 273 } 274 void onResize() { 275 Point size = getSize(); 276 if (oldSize == null || oldSize.x == 0 || oldSize.y == 0) { 277 redraw(); 278 } else { 279 int width = 0; 280 if (oldSize.x < size.x) { 281 width = size.x - oldSize.x + borderRight + highlight; 282 } else if (oldSize.x > size.x) { 283 width = borderRight + highlight; 284 } 285 redraw(size.x - width, 0, width, size.y, false); 286 287 int height = 0; 288 if (oldSize.y < size.y) { 289 height = size.y - oldSize.y + borderBottom + highlight; 290 } 291 if (oldSize.y > size.y) { 292 height = borderBottom + highlight; 293 } 294 redraw(0, size.y - height, size.x, height, false); 295 } 296 oldSize = size; 297 } 298 311 public void setContent(Control content) { 312 checkWidget(); 313 if (content != null && content.getParent() != this) { 314 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 315 } 316 if (this.content != null && !this.content.isDisposed()) { 317 this.content.setBounds(OFFSCREEN, OFFSCREEN, 0, 0); 318 } 319 this.content = content; 320 layout(false); 321 } 322 337 public void setLayout (Layout layout) { 338 checkWidget(); 339 return; 340 } 341 void setSelectionBackground (Color color) { 342 checkWidget(); 343 if (selectionBackground == color) return; 344 if (color == null) color = getDisplay().getSystemColor(SELECTION_BACKGROUND); 345 selectionBackground = color; 346 redraw(); 347 } 348 362 public void setTopCenter(Control topCenter) { 363 checkWidget(); 364 if (topCenter != null && topCenter.getParent() != this) { 365 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 366 } 367 if (this.topCenter != null && !this.topCenter.isDisposed()) { 368 Point size = this.topCenter.getSize(); 369 this.topCenter.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y); 370 } 371 this.topCenter = topCenter; 372 layout(false); 373 } 374 388 public void setTopLeft(Control c) { 389 checkWidget(); 390 if (c != null && c.getParent() != this) { 391 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 392 } 393 if (this.topLeft != null && !this.topLeft.isDisposed()) { 394 Point size = this.topLeft.getSize(); 395 this.topLeft.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y); 396 } 397 this.topLeft = c; 398 layout(false); 399 } 400 414 public void setTopRight(Control c) { 415 checkWidget(); 416 if (c != null && c.getParent() != this) { 417 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 418 } 419 if (this.topRight != null && !this.topRight.isDisposed()) { 420 Point size = this.topRight.getSize(); 421 this.topRight.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y); 422 } 423 this.topRight = c; 424 layout(false); 425 } 426 436 public void setBorderVisible(boolean show) { 437 checkWidget(); 438 if (showBorder == show) return; 439 440 showBorder = show; 441 if (showBorder) { 442 borderLeft = borderTop = borderRight = borderBottom = 1; 443 if ((getStyle() & SWT.FLAT)== 0) highlight = 2; 444 } else { 445 borderBottom = borderTop = borderLeft = borderRight = 0; 446 highlight = 0; 447 } 448 layout(false); 449 redraw(); 450 } 451 463 public void setTopCenterSeparate(boolean show) { 464 checkWidget(); 465 separateTopCenter = show; 466 layout(false); 467 } 468 } 469 | Popular Tags |