XScrollPanePeer.java revision 11171:07175dc5b2da
1327302Sdim/* 2327302Sdim * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved. 3327302Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4327302Sdim * 5327302Sdim * This code is free software; you can redistribute it and/or modify it 6327302Sdim * under the terms of the GNU General Public License version 2 only, as 7327302Sdim * published by the Free Software Foundation. Oracle designates this 8327302Sdim * particular file as subject to the "Classpath" exception as provided 9327302Sdim * by Oracle in the LICENSE file that accompanied this code. 10327302Sdim * 11327302Sdim * This code is distributed in the hope that it will be useful, but WITHOUT 12327302Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13327302Sdim * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14327302Sdim * version 2 for more details (a copy is included in the LICENSE file that 15327302Sdim * accompanied this code). 16327302Sdim * 17327302Sdim * You should have received a copy of the GNU General Public License version 18327302Sdim * 2 along with this work; if not, write to the Free Software Foundation, 19327302Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20327302Sdim * 21327302Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22327302Sdim * or visit www.oracle.com if you need additional information or have any 23327302Sdim * questions. 24327302Sdim */ 25327302Sdim 26327302Sdimpackage sun.awt.X11; 27327302Sdim 28327302Sdimimport java.awt.*; 29327302Sdimimport java.awt.event.*; 30327302Sdimimport java.awt.peer.*; 31327302Sdimimport java.lang.reflect.*; 32341825Sdim 33327302Sdimimport sun.awt.AWTAccessor; 34327302Sdim 35327302Sdimclass XScrollPanePeer extends XComponentPeer implements ScrollPanePeer, XScrollbarClient { 36327302Sdim 37327302Sdim public final static int MARGIN = 1; 38327302Sdim public final static int SCROLLBAR; 39327302Sdim public final static int SPACE = 2; 40327302Sdim public final static int SCROLLBAR_INSET = 2; 41327302Sdim 42327302Sdim public final static int VERTICAL = 1 << 0; 43327302Sdim public final static int HORIZONTAL = 1 << 1; 44327302Sdim 45327302Sdim static { 46327302Sdim SCROLLBAR = XToolkit.getUIDefaults().getInt("ScrollBar.defaultWidth"); 47341825Sdim } 48327302Sdim 49327302Sdim XVerticalScrollbar vsb; 50327302Sdim XHorizontalScrollbar hsb; 51327302Sdim XWindow clip; 52327302Sdim 53327302Sdim int active=VERTICAL; 54327302Sdim int hsbSpace; 55327302Sdim int vsbSpace; 56327302Sdim 57327302Sdim static class XScrollPaneContentWindow extends XWindow { 58327302Sdim XScrollPaneContentWindow(ScrollPane target, long parentWindow) { 59327302Sdim super(target, parentWindow); 60327302Sdim } 61327302Sdim public String getWMName() { 62327302Sdim return "ScrollPane content"; 63341825Sdim } 64327302Sdim } 65327302Sdim 66327302Sdim XScrollPanePeer(ScrollPane target) { 67327302Sdim super(target); 68327302Sdim 69327302Sdim // Create the clip window. The field "clip" must be null when 70327302Sdim // we call winCreate, or the parent of clip will be set to itself! 71327302Sdim clip = null; 72327302Sdim 73327302Sdim 74327302Sdim XWindow c = new XScrollPaneContentWindow(target,window); 75327302Sdim clip = c; 76327302Sdim 77327302Sdim vsb = new XVerticalScrollbar(this); 78327302Sdim 79327302Sdim hsb = new XHorizontalScrollbar(this); 80327302Sdim 81327302Sdim if (target.getScrollbarDisplayPolicy() == ScrollPane.SCROLLBARS_ALWAYS) { 82327302Sdim vsbSpace = hsbSpace = SCROLLBAR; 83327302Sdim } else { 84327302Sdim vsbSpace = hsbSpace = 0; 85327302Sdim } 86327302Sdim 87327302Sdim int unitIncrement = 1; 88327302Sdim Adjustable vAdjustable = target.getVAdjustable(); 89327302Sdim if (vAdjustable != null){ 90327302Sdim unitIncrement = vAdjustable.getUnitIncrement(); 91327302Sdim } 92327302Sdim int h = height-hsbSpace; 93341825Sdim vsb.setValues(0, h, 0, h, unitIncrement, Math.max(1, (int)(h * 0.90))); 94327302Sdim vsb.setSize(vsbSpace-SCROLLBAR_INSET, h); 95327302Sdim 96327302Sdim unitIncrement = 1; 97327302Sdim Adjustable hAdjustable = target.getHAdjustable(); 98327302Sdim if (hAdjustable != null){ 99327302Sdim unitIncrement = hAdjustable.getUnitIncrement(); 100327302Sdim } 101327302Sdim int w = width - vsbSpace; 102327302Sdim hsb.setValues(0, w, 0, w, unitIncrement, Math.max(1, (int)(w * 0.90))); 103327302Sdim hsb.setSize(w, hsbSpace-SCROLLBAR_INSET); 104327302Sdim 105327302Sdim setViewportSize(); 106327302Sdim clip.xSetVisible(true); 107327302Sdim 108327302Sdim 109341825Sdim } 110327302Sdim 111327302Sdim public long getContentWindow() 112327302Sdim { 113327302Sdim return (clip == null) ? window : clip.getWindow(); 114327302Sdim } 115327302Sdim 116327302Sdim public void setBounds(int x, int y, int w, int h, int op) { 117327302Sdim super.setBounds(x, y, w, h, op); 118327302Sdim 119327302Sdim if (clip == null) return; 120327302Sdim setScrollbarSpace(); 121327302Sdim setViewportSize(); 122327302Sdim repaint(); 123327302Sdim } 124327302Sdim 125341825Sdim public Insets getInsets() { 126327302Sdim return new Insets(MARGIN, MARGIN, MARGIN+hsbSpace, MARGIN+vsbSpace); 127327302Sdim } 128327302Sdim 129327302Sdim public int getHScrollbarHeight() { 130327302Sdim return SCROLLBAR; 131327302Sdim } 132327302Sdim 133327302Sdim public int getVScrollbarWidth() { 134327302Sdim return SCROLLBAR; 135327302Sdim } 136327302Sdim 137327302Sdim public void childResized(int w, int h) { 138327302Sdim if (setScrollbarSpace()) { 139327302Sdim setViewportSize(); 140327302Sdim } 141341825Sdim repaint(); 142327302Sdim } 143327302Sdim 144327302Sdim @SuppressWarnings("deprecation") 145341825Sdim Dimension getChildSize() { 146341825Sdim ScrollPane sp = (ScrollPane)target; 147341825Sdim if (sp.countComponents() > 0) { 148327302Sdim Component c = sp.getComponent(0); 149341825Sdim return c.size(); 150341825Sdim } else { 151341825Sdim return new Dimension(0, 0); 152341825Sdim } 153341825Sdim } 154327302Sdim 155341825Sdim @SuppressWarnings("deprecation") 156341825Sdim boolean setScrollbarSpace() { 157341825Sdim ScrollPane sp = (ScrollPane)target; 158327302Sdim boolean changed = false; 159341825Sdim int sbDisplayPolicy = sp.getScrollbarDisplayPolicy(); 160341825Sdim 161341825Sdim if (sbDisplayPolicy == ScrollPane.SCROLLBARS_NEVER) { 162327302Sdim return changed; 163341825Sdim } 164341825Sdim Dimension cSize = getChildSize(); 165341825Sdim 166341825Sdim if (sbDisplayPolicy == ScrollPane.SCROLLBARS_AS_NEEDED) { 167327302Sdim int oldHsbSpace = hsbSpace; 168327302Sdim int oldVsbSpace = vsbSpace; 169341825Sdim hsbSpace = (cSize.width <= (width - 2*MARGIN) ? 0 : SCROLLBAR); 170341825Sdim vsbSpace = (cSize.height <= (height - 2*MARGIN) ? 0 : SCROLLBAR); 171341825Sdim 172327302Sdim if (hsbSpace == 0 && vsbSpace != 0) { 173341825Sdim hsbSpace = (cSize.width <= (width - SCROLLBAR - 2*MARGIN) ? 0 : SCROLLBAR); 174341825Sdim } 175341825Sdim if (vsbSpace == 0 && hsbSpace != 0) { 176327302Sdim vsbSpace = (cSize.height <= (height - SCROLLBAR - 2*MARGIN) ? 0 : SCROLLBAR); 177341825Sdim } 178341825Sdim if (oldHsbSpace != hsbSpace || oldVsbSpace != vsbSpace) { 179341825Sdim changed = true; 180341825Sdim } 181327302Sdim } 182327302Sdim if (vsbSpace > 0) { 183341825Sdim int vis = height - (2*MARGIN) - hsbSpace; 184341825Sdim int max = Math.max(cSize.height, vis); 185341825Sdim vsb.setValues(vsb.getValue(), vis, 0, max); 186327302Sdim vsb.setBlockIncrement((int)(vsb.getVisibleAmount() * .90)); 187341825Sdim vsb.setSize(vsbSpace-SCROLLBAR_INSET, height-hsbSpace); 188341825Sdim // Adjustable vadj = sp.getVAdjustable(); 189341825Sdim // vadj.setVisibleAmount(vsb.vis); 190327302Sdim // vadj.setMaximum(vsb.max); 191341825Sdim // vadj.setBlockIncrement(vsb.page); 192341825Sdim } 193341825Sdim if (hsbSpace > 0) { 194341825Sdim int vis = width - (2*MARGIN) - vsbSpace; 195327302Sdim int max = Math.max(cSize.width, vis); 196327302Sdim hsb.setValues(hsb.getValue(), vis, 0, max); 197341825Sdim hsb.setBlockIncrement((int)(hsb.getVisibleAmount() * .90)); 198341825Sdim hsb.setSize(width-vsbSpace, hsbSpace-SCROLLBAR_INSET); 199341825Sdim // Adjustable hadj = sp.getHAdjustable(); 200327302Sdim // hadj.setVisibleAmount(hsb.vis); 201341825Sdim // hadj.setMaximum(hsb.max); 202341825Sdim // hadj.setBlockIncrement(hsb.page); 203341825Sdim } 204327302Sdim 205341825Sdim // Check to see if we hid either of the scrollbars but left 206341825Sdim // ourselves scrolled off of the top and/or right of the pane. 207341825Sdim // If we did, we need to scroll to the top and/or right of 208341825Sdim // the pane to make it visible. 209327302Sdim // 210327302Sdim // Reminder: see if there is a better place to put this code. 211341825Sdim boolean must_scroll = false; 212341825Sdim 213341825Sdim // Get the point at which the ScrollPane is currently located 214327302Sdim // if number of components > 0 215341825Sdim Point p = new Point(0, 0); 216341825Sdim 217341825Sdim if (((ScrollPane)target).getComponentCount() > 0){ 218327302Sdim 219341825Sdim p = ((ScrollPane)target).getComponent(0).location(); 220341825Sdim 221341825Sdim if ((vsbSpace == 0) && (p.y < 0)) { 222341825Sdim p.y = 0; 223327302Sdim must_scroll = true; 224327302Sdim } 225341825Sdim 226341825Sdim if ((hsbSpace == 0) && (p.x < 0)) { 227341825Sdim p.x = 0; 228327302Sdim must_scroll = true; 229327302Sdim } 230327302Sdim } 231327302Sdim 232327302Sdim if (must_scroll) 233327302Sdim scroll(x, y, VERTICAL | HORIZONTAL); 234327302Sdim 235327302Sdim return changed; 236327302Sdim } 237327302Sdim 238327302Sdim void setViewportSize() { 239327302Sdim clip.xSetBounds(MARGIN, MARGIN, 240327302Sdim width - (2*MARGIN) - vsbSpace, 241327302Sdim height - (2*MARGIN) - hsbSpace); 242327302Sdim } 243327302Sdim 244327302Sdim public void setUnitIncrement(Adjustable adj, int u) { 245327302Sdim if (adj.getOrientation() == Adjustable.VERTICAL) { 246327302Sdim vsb.setUnitIncrement(u); 247327302Sdim } else { 248327302Sdim // HORIZONTAL 249327302Sdim hsb.setUnitIncrement(u); 250327302Sdim } 251327302Sdim } 252327302Sdim 253327302Sdim public void setValue(Adjustable adj, int v) { 254327302Sdim if (adj.getOrientation() == Adjustable.VERTICAL) { 255327302Sdim scroll(-1, v, VERTICAL); 256327302Sdim } else { 257327302Sdim // HORIZONTAL 258327302Sdim scroll(v, -1, HORIZONTAL); 259327302Sdim } 260327302Sdim } 261327302Sdim 262327302Sdim public void setScrollPosition(int x, int y) { 263327302Sdim scroll(x, y, VERTICAL | HORIZONTAL); 264327302Sdim } 265327302Sdim 266327302Sdim void scroll(int x, int y, int flag) { 267327302Sdim scroll(x, y, flag, AdjustmentEvent.TRACK); 268327302Sdim } 269327302Sdim 270327302Sdim /** 271327302Sdim * Scroll the contents to position x, y 272327302Sdim */ 273327302Sdim @SuppressWarnings("deprecation") 274327302Sdim void scroll(int x, int y, int flag, int type) { 275327302Sdim checkSecurity(); 276327302Sdim ScrollPane sp = (ScrollPane)target; 277327302Sdim Component c = getScrollChild(); 278327302Sdim if (c == null) { 279327302Sdim return; 280327302Sdim } 281327302Sdim int sx, sy; 282327302Sdim Color colors[] = getGUIcolors(); 283327302Sdim 284327302Sdim if (sp.getScrollbarDisplayPolicy() == ScrollPane.SCROLLBARS_NEVER) { 285327302Sdim sx = -x; 286327302Sdim sy = -y; 287327302Sdim } else { 288327302Sdim Point p = c.location(); 289327302Sdim sx = p.x; 290327302Sdim sy = p.y; 291327302Sdim 292327302Sdim if ((flag & HORIZONTAL) != 0) { 293327302Sdim hsb.setValue(Math.min(x, hsb.getMaximum()-hsb.getVisibleAmount())); 294327302Sdim ScrollPaneAdjustable hadj = (ScrollPaneAdjustable)sp.getHAdjustable(); 295327302Sdim setAdjustableValue(hadj, hsb.getValue(), type); 296327302Sdim sx = -(hsb.getValue()); 297327302Sdim Graphics g = getGraphics(); 298327302Sdim if (g != null) { 299327302Sdim try { 300327302Sdim paintHorScrollbar(g, colors, true); 301327302Sdim } finally { 302327302Sdim g.dispose(); 303327302Sdim } 304327302Sdim } 305327302Sdim } 306327302Sdim if ((flag & VERTICAL) != 0) { 307327302Sdim vsb.setValue(Math.min(y, vsb.getMaximum() - vsb.getVisibleAmount())); 308327302Sdim ScrollPaneAdjustable vadj = (ScrollPaneAdjustable)sp.getVAdjustable(); 309327302Sdim setAdjustableValue(vadj, vsb.getValue(), type); 310327302Sdim sy = -(vsb.getValue()); 311327302Sdim Graphics g = getGraphics(); 312327302Sdim if (g != null) { 313327302Sdim try { 314327302Sdim paintVerScrollbar(g, colors, true); 315327302Sdim } finally { 316327302Sdim g.dispose(); 317327302Sdim } 318327302Sdim } 319327302Sdim } 320327302Sdim } 321327302Sdim c.move(sx, sy); 322327302Sdim } 323327302Sdim 324327302Sdim private void setAdjustableValue(final ScrollPaneAdjustable adj, final int value, 325327302Sdim final int type) { 326327302Sdim AWTAccessor.getScrollPaneAdjustableAccessor().setTypedValue(adj, value, 327327302Sdim type); 328327302Sdim } 329327302Sdim @Override 330327302Sdim void paintPeer(final Graphics g) { 331327302Sdim final Color[] colors = getGUIcolors(); 332327302Sdim g.setColor(colors[BACKGROUND_COLOR]); 333327302Sdim final int h = height - hsbSpace; 334327302Sdim final int w = width - vsbSpace; 335327302Sdim g.fillRect(0, 0, w, h); 336327302Sdim // paint rectangular region between scrollbars 337327302Sdim g.fillRect(w, h, vsbSpace, hsbSpace); 338327302Sdim if (MARGIN > 0) { 339327302Sdim draw3DRect(g, colors, 0, 0, w - 1, h - 1, false); 340327302Sdim } 341327302Sdim paintScrollBars(g, colors); 342327302Sdim } 343327302Sdim private void paintScrollBars(Graphics g, Color[] colors) { 344327302Sdim if (vsbSpace > 0) { 345327302Sdim paintVerScrollbar(g, colors, true); 346327302Sdim // paint the whole scrollbar 347327302Sdim } 348327302Sdim 349327302Sdim if (hsbSpace > 0) { 350327302Sdim paintHorScrollbar(g, colors, true); 351327302Sdim // paint the whole scrollbar 352327302Sdim } 353327302Sdim } 354327302Sdim void repaintScrollBars() { 355327302Sdim Graphics g = getGraphics(); 356327302Sdim Color colors[] = getGUIcolors(); 357327302Sdim if (g != null) { 358327302Sdim try { 359327302Sdim paintScrollBars(g, colors); 360327302Sdim } finally { 361327302Sdim g.dispose(); 362327302Sdim } 363327302Sdim } 364327302Sdim } 365327302Sdim public void repaintScrollbarRequest(XScrollbar sb) { 366327302Sdim Graphics g = getGraphics(); 367327302Sdim Color colors[] = getGUIcolors(); 368327302Sdim if (g != null) { 369327302Sdim try { 370327302Sdim if (sb == vsb) { 371327302Sdim paintVerScrollbar(g, colors, true); 372327302Sdim } else if (sb == hsb) { 373327302Sdim paintHorScrollbar(g, colors, true); 374327302Sdim } 375327302Sdim } finally { 376327302Sdim g.dispose(); 377327302Sdim } 378327302Sdim } 379327302Sdim } 380327302Sdim public void handleEvent(java.awt.AWTEvent e) { 381327302Sdim super.handleEvent(e); 382327302Sdim 383327302Sdim int id = e.getID(); 384327302Sdim switch(id) { 385327302Sdim case PaintEvent.PAINT: 386327302Sdim case PaintEvent.UPDATE: 387327302Sdim repaintScrollBars(); 388327302Sdim break; 389327302Sdim } 390327302Sdim } 391327302Sdim 392327302Sdim 393327302Sdim /** 394327302Sdim * Paint the horizontal scrollbar to the screen 395327302Sdim * 396327302Sdim * @param g the graphics context to draw into 397327302Sdim * @param colors the colors used to draw the scrollbar 398 * @param paintAll paint the whole scrollbar if true, just the thumb if false 399 */ 400 void paintHorScrollbar(Graphics g, Color colors[], boolean paintAll) { 401 if (hsbSpace <= 0) { 402 return; 403 } 404 Graphics ng = g.create(); 405 g.setColor(colors[BACKGROUND_COLOR]); 406 407 // SCROLLBAR is the height of scrollbar area 408 // but the actual scrollbar is SCROLLBAR-SPACE high; 409 // the rest must be filled with background color 410 int w = width - vsbSpace - (2*MARGIN); 411 g.fillRect(MARGIN, height-SCROLLBAR, w, SPACE); 412 g.fillRect(0, height-SCROLLBAR, MARGIN, SCROLLBAR); 413 g.fillRect(MARGIN + w, height-SCROLLBAR, MARGIN, SCROLLBAR); 414 415 try { 416 ng.translate(MARGIN, height - (SCROLLBAR - SPACE)); 417 hsb.paint(ng, colors, paintAll); 418 } 419 finally { 420 ng.dispose(); 421 } 422 423 424 } 425 426 427 428 429 /** 430 * Paint the vertical scrollbar to the screen 431 * 432 * @param g the graphics context to draw into 433 * @param colors the colors used to draw the scrollbar 434 * @param paintAll paint the whole scrollbar if true, just the thumb if false 435 */ 436 void paintVerScrollbar(Graphics g, Color colors[], boolean paintAll) { 437 if (vsbSpace <= 0) { 438 return; 439 } 440 Graphics ng = g.create(); 441 g.setColor(colors[BACKGROUND_COLOR]); 442 443 // SCROLLBAR is the width of scrollbar area 444 // but the actual scrollbar is SCROLLBAR-SPACE wide; 445 // the rest must be filled with background color 446 int h = height - hsbSpace - (2*MARGIN); 447 g.fillRect(width-SCROLLBAR, MARGIN, SPACE, h); 448 g.fillRect(width-SCROLLBAR, 0, SCROLLBAR, MARGIN); 449 g.fillRect(width-SCROLLBAR, MARGIN+h, SCROLLBAR, MARGIN); 450 451 try { 452 ng.translate(width - (SCROLLBAR - SPACE), MARGIN); 453 vsb.paint(ng, colors, paintAll); 454 } 455 finally { 456 ng.dispose(); 457 } 458 } 459 460 /** 461 * 462 * @see java.awt.event.MouseEvent 463 * MouseEvent.MOUSE_CLICKED 464 * MouseEvent.MOUSE_PRESSED 465 * MouseEvent.MOUSE_RELEASED 466 * MouseEvent.MOUSE_MOVED 467 * MouseEvent.MOUSE_ENTERED 468 * MouseEvent.MOUSE_EXITED 469 * MouseEvent.MOUSE_DRAGGED 470 */ 471 public void handleJavaMouseEvent( MouseEvent mouseEvent ) { 472 super.handleJavaMouseEvent(mouseEvent); 473 int modifiers = mouseEvent.getModifiers(); 474 int id = mouseEvent.getID(); 475 int x = mouseEvent.getX(); 476 int y = mouseEvent.getY(); 477 478 479 // super.handleMouseEvent(mouseEvent); 480 481 if ((modifiers & InputEvent.BUTTON1_MASK) == 0) { 482 return; 483 } 484 485 switch (id) { 486 case MouseEvent.MOUSE_PRESSED: 487 if (inVerticalScrollbar(x,y )) { 488 active = VERTICAL; 489 int h = height - hsbSpace - (2*MARGIN); 490 vsb.handleMouseEvent(id,modifiers,x - (width - SCROLLBAR + SPACE),y-MARGIN); 491 } else if (inHorizontalScrollbar(x, y) ) { 492 active = HORIZONTAL; 493 int w = width - 2*MARGIN - vsbSpace; 494 hsb.handleMouseEvent(id,modifiers,x-MARGIN,y-(height - SCROLLBAR + SPACE)); 495 } 496 break; 497 498 // On mouse up, pass the event through to the scrollbar to stop 499 // scrolling. The x & y passed do not matter. 500 case MouseEvent.MOUSE_RELEASED: 501 // winReleaseCursorFocus(); 502 if (active == VERTICAL) { 503 vsb.handleMouseEvent(id,modifiers,x,y); 504 } else if (active == HORIZONTAL) { 505 hsb.handleMouseEvent(id,modifiers,x,y); 506 } 507 break; 508 509 510 case MouseEvent.MOUSE_DRAGGED: 511 if ((active == VERTICAL)) { 512 int h = height - 2*MARGIN - hsbSpace; 513 vsb.handleMouseEvent(id,modifiers,x-(width - SCROLLBAR + SPACE),y-MARGIN); 514 } else if ((active == HORIZONTAL)) { 515 int w = width - 2*MARGIN - vsbSpace; 516 hsb.handleMouseEvent(id,modifiers,x-MARGIN,y-(height - SCROLLBAR + SPACE)); 517 } 518 break; 519 } 520 } 521 522 /** 523 * return value from the scrollbar 524 */ 525 public void notifyValue(XScrollbar obj, int type, int v, boolean isAdjusting) { 526 if (obj == vsb) { 527 scroll(-1, v, VERTICAL, type); 528 } else if ((XHorizontalScrollbar)obj == hsb) { 529 scroll(v, -1, HORIZONTAL, type); 530 } 531 } 532 533 /** 534 * return true if the x and y position is in the verticalscrollbar 535 */ 536 boolean inVerticalScrollbar(int x, int y) { 537 if (vsbSpace <= 0) { 538 return false; 539 } 540 int h = height - MARGIN - hsbSpace; 541 return (x >= width - (SCROLLBAR - SPACE)) && (x < width) && (y >= MARGIN) && (y < h); 542 } 543 544 /** 545 * return true if the x and y position is in the horizontal scrollbar 546 */ 547 boolean inHorizontalScrollbar(int x, int y) { 548 if (hsbSpace <= 0) { 549 return false; 550 } 551 int w = width - MARGIN - vsbSpace; 552 return (x >= MARGIN) && (x < w) && (y >= height - (SCROLLBAR - SPACE)) && (y < height); 553 } 554 555 private Component getScrollChild() { 556 ScrollPane sp = (ScrollPane)target; 557 Component child = null; 558 try { 559 child = sp.getComponent(0); 560 } catch (ArrayIndexOutOfBoundsException e) { 561 // do nothing. in this case we return null 562 } 563 return child; 564 } 565 566 int vval; 567 int hval; 568 int vmax; 569 int hmax; 570 /* 571 * Print the native component by rendering the Motif look ourselves. 572 * ToDo(aim): needs to query native motif for more accurate size and 573 * color information. 574 */ 575 @SuppressWarnings("deprecation") 576 public void print(Graphics g) { 577 ScrollPane sp = (ScrollPane)target; 578 Dimension d = sp.size(); 579 Color bg = sp.getBackground(); 580 Color fg = sp.getForeground(); 581 Point p = sp.getScrollPosition(); 582 Component c = getScrollChild(); 583 Dimension cd; 584 if (c != null) { 585 cd = c.size(); 586 } else { 587 cd = new Dimension(0, 0); 588 } 589 int sbDisplay = sp.getScrollbarDisplayPolicy(); 590 int vvis, hvis, vmin, hmin, vmax, hmax, vval, hval; 591 592 switch (sbDisplay) { 593 case ScrollPane.SCROLLBARS_NEVER: 594 hsbSpace = vsbSpace = 0; 595 break; 596 case ScrollPane.SCROLLBARS_ALWAYS: 597 hsbSpace = vsbSpace = SCROLLBAR; 598 break; 599 case ScrollPane.SCROLLBARS_AS_NEEDED: 600 hsbSpace = (cd.width <= (d.width - 2*MARGIN)? 0 : SCROLLBAR); 601 vsbSpace = (cd.height <= (d.height - 2*MARGIN)? 0 : SCROLLBAR); 602 603 if (hsbSpace == 0 && vsbSpace != 0) { 604 hsbSpace = (cd.width <= (d.width - SCROLLBAR - 2*MARGIN)? 0 : SCROLLBAR); 605 } 606 if (vsbSpace == 0 && hsbSpace != 0) { 607 vsbSpace = (cd.height <= (d.height - SCROLLBAR - 2*MARGIN)? 0 : SCROLLBAR); 608 } 609 } 610 611 vvis = hvis = vmin = hmin = vmax = hmax = vval = hval = 0; 612 613 if (vsbSpace > 0) { 614 vmin = 0; 615 vvis = d.height - (2*MARGIN) - hsbSpace; 616 vmax = Math.max(cd.height - vvis, 0); 617 vval = p.y; 618 } 619 if (hsbSpace > 0) { 620 hmin = 0; 621 hvis = d.width - (2*MARGIN) - vsbSpace; 622 hmax = Math.max(cd.width - hvis, 0); 623 hval = p.x; 624 } 625 626 // need to be careful to add the margins back in here because 627 // we're drawing the margin border, after all! 628 int w = d.width - vsbSpace; 629 int h = d.height - hsbSpace; 630 631 g.setColor(bg); 632 g.fillRect(0, 0, d.width, d.height); 633 634 if (hsbSpace > 0) { 635 int sbw = d.width - vsbSpace; 636 g.fillRect(1, d.height - SCROLLBAR - 3, sbw - 1, SCROLLBAR - 3); 637 Graphics ng = g.create(); 638 try { 639 ng.translate(0, d.height - (SCROLLBAR - 2)); 640 drawScrollbar(ng, bg, SCROLLBAR - 2, sbw, 641 hmin, hmax, hval, hvis, true); 642 } finally { 643 ng.dispose(); 644 } 645 } 646 if (vsbSpace > 0) { 647 int sbh = d.height - hsbSpace; 648 g.fillRect(d.width - SCROLLBAR - 3, 1, SCROLLBAR - 3, sbh - 1); 649 Graphics ng = g.create(); 650 try { 651 ng.translate(d.width - (SCROLLBAR - 2), 0); 652 drawScrollbar(ng, bg, SCROLLBAR - 2, sbh, 653 vmin, vmax, vval, vvis, false); 654 } finally { 655 ng.dispose(); 656 } 657 } 658 659 draw3DRect(g, bg, 0, 0, w - 1, h - 1, false); 660 661 target.print(g); 662 sp.printComponents(g); 663 } 664 665} 666