1 11 package org.eclipse.swt.custom; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.graphics.*; 15 import org.eclipse.swt.widgets.*; 16 17 100 public class ScrolledComposite extends Composite { 101 102 Control content; 103 Listener contentListener; 104 105 int minHeight = 0; 106 int minWidth = 0; 107 boolean expandHorizontal = false; 108 boolean expandVertical = false; 109 boolean alwaysShowScroll = false; 110 111 138 public ScrolledComposite(Composite parent, int style) { 139 super(parent, checkStyle(style)); 140 super.setLayout(new ScrolledCompositeLayout()); 141 ScrollBar hBar = getHorizontalBar (); 142 if (hBar != null) { 143 hBar.setVisible(false); 144 hBar.addListener (SWT.Selection, new Listener () { 145 public void handleEvent (Event e) { 146 hScroll(); 147 } 148 }); 149 } 150 151 ScrollBar vBar = getVerticalBar (); 152 if (vBar != null) { 153 vBar.setVisible(false); 154 vBar.addListener (SWT.Selection, new Listener () { 155 public void handleEvent (Event e) { 156 vScroll(); 157 } 158 }); 159 } 160 161 contentListener = new Listener() { 162 public void handleEvent(Event e) { 163 if (e.type != SWT.Resize) return; 164 layout(false); 165 } 166 }; 167 } 168 169 static int checkStyle (int style) { 170 int mask = SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; 171 return style & mask; 172 } 173 174 183 public boolean getAlwaysShowScrollBars() { 184 return alwaysShowScroll; 186 } 187 188 201 public boolean getExpandHorizontal() { 202 checkWidget(); 203 return expandHorizontal; 204 } 205 206 219 public boolean getExpandVertical() { 220 checkWidget(); 221 return expandVertical; 222 } 223 224 236 public int getMinWidth() { 237 checkWidget(); 238 return minWidth; 239 } 240 241 253 public int getMinHeight() { 254 checkWidget(); 255 return minHeight; 256 } 257 258 263 public Control getContent() { 264 return content; 266 } 267 268 void hScroll() { 269 if (content == null) return; 270 Point location = content.getLocation (); 271 ScrollBar hBar = getHorizontalBar (); 272 int hSelection = hBar.getSelection (); 273 content.setLocation (-hSelection, location.y); 274 } 275 boolean needHScroll(Rectangle contentRect, boolean vVisible) { 276 ScrollBar hBar = getHorizontalBar(); 277 if (hBar == null) return false; 278 279 Rectangle hostRect = getBounds(); 280 int border = getBorderWidth(); 281 hostRect.width -= 2*border; 282 ScrollBar vBar = getVerticalBar(); 283 if (vVisible && vBar != null) hostRect.width -= vBar.getSize().x; 284 285 if (!expandHorizontal && contentRect.width > hostRect.width) return true; 286 if (expandHorizontal && minWidth > hostRect.width) return true; 287 return false; 288 } 289 290 boolean needVScroll(Rectangle contentRect, boolean hVisible) { 291 ScrollBar vBar = getVerticalBar(); 292 if (vBar == null) return false; 293 294 Rectangle hostRect = getBounds(); 295 int border = getBorderWidth(); 296 hostRect.height -= 2*border; 297 ScrollBar hBar = getHorizontalBar(); 298 if (hVisible && hBar != null) hostRect.height -= hBar.getSize().y; 299 300 if (!expandVertical && contentRect.height > hostRect.height) return true; 301 if (expandVertical && minHeight > hostRect.height) return true; 302 return false; 303 } 304 305 320 public Point getOrigin() { 321 checkWidget(); 322 if (content == null) return new Point(0, 0); 323 Point location = content.getLocation(); 324 return new Point(-location.x, -location.y); 325 } 326 342 public void setOrigin(Point origin) { 343 setOrigin(origin.x, origin.y); 344 } 345 363 public void setOrigin(int x, int y) { 364 checkWidget(); 365 if (content == null) return; 366 ScrollBar hBar = getHorizontalBar (); 367 if (hBar != null) { 368 hBar.setSelection(x); 369 x = -hBar.getSelection (); 370 } else { 371 x = 0; 372 } 373 ScrollBar vBar = getVerticalBar (); 374 if (vBar != null) { 375 vBar.setSelection(y); 376 y = -vBar.getSelection (); 377 } else { 378 y = 0; 379 } 380 content.setLocation(x, y); 381 } 382 396 public void setAlwaysShowScrollBars(boolean show) { 397 checkWidget(); 398 if (show == alwaysShowScroll) return; 399 alwaysShowScroll = show; 400 ScrollBar hBar = getHorizontalBar (); 401 if (hBar != null && alwaysShowScroll) hBar.setVisible(true); 402 ScrollBar vBar = getVerticalBar (); 403 if (vBar != null && alwaysShowScroll) vBar.setVisible(true); 404 layout(false); 405 } 406 407 417 public void setContent(Control content) { 418 checkWidget(); 419 if (this.content != null && !this.content.isDisposed()) { 420 this.content.removeListener(SWT.Resize, contentListener); 421 this.content.setBounds(new Rectangle(-200, -200, 0, 0)); 422 } 423 424 this.content = content; 425 ScrollBar vBar = getVerticalBar (); 426 ScrollBar hBar = getHorizontalBar (); 427 if (this.content != null) { 428 if (vBar != null) { 429 vBar.setMaximum (0); 430 vBar.setThumb (0); 431 vBar.setSelection(0); 432 } 433 if (hBar != null) { 434 hBar.setMaximum (0); 435 hBar.setThumb (0); 436 hBar.setSelection(0); 437 } 438 content.setLocation(0, 0); 439 layout(false); 440 this.content.addListener(SWT.Resize, contentListener); 441 } else { 442 if (hBar != null) hBar.setVisible(alwaysShowScroll); 443 if (vBar != null) vBar.setVisible(alwaysShowScroll); 444 } 445 } 446 461 public void setExpandHorizontal(boolean expand) { 462 checkWidget(); 463 if (expand == expandHorizontal) return; 464 expandHorizontal = expand; 465 layout(false); 466 } 467 482 public void setExpandVertical(boolean expand) { 483 checkWidget(); 484 if (expand == expandVertical) return; 485 expandVertical = expand; 486 layout(false); 487 } 488 503 public void setLayout (Layout layout) { 504 checkWidget(); 505 return; 506 } 507 519 public void setMinHeight(int height) { 520 setMinSize(minWidth, height); 521 } 522 534 public void setMinSize(Point size) { 535 if (size == null) { 536 setMinSize(0, 0); 537 } else { 538 setMinSize(size.x, size.y); 539 } 540 } 541 554 public void setMinSize(int width, int height) { 555 checkWidget(); 556 if (width == minWidth && height == minHeight) return; 557 minWidth = Math.max(0, width); 558 minHeight = Math.max(0, height); 559 layout(false); 560 } 561 573 public void setMinWidth(int width) { 574 setMinSize(width, minHeight); 575 } 576 577 void vScroll() { 578 if (content == null) return; 579 Point location = content.getLocation (); 580 ScrollBar vBar = getVerticalBar (); 581 int vSelection = vBar.getSelection (); 582 content.setLocation (location.x, -vSelection); 583 } 584 } 585 | Popular Tags |