J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.awt.X11; |
| 27 | |
| 28 | import java.awt.*; |
| 29 | import java.awt.event.*; |
| 30 | import java.awt.peer.*; |
| 31 | import java.lang.reflect.*; |
| 32 | import sun.awt.SunToolkit; |
| 33 | |
| 34 | class XScrollPanePeer extends XComponentPeer implements ScrollPanePeer, XScrollbarClient { |
| 35 | |
| 36 | public final static int MARGIN = 1; |
| 37 | public final static int SCROLLBAR; |
| 38 | public final static int SPACE = 2; |
| 39 | public final static int SCROLLBAR_INSET = 2; |
| 40 | |
| 41 | public final static int VERTICAL = 1 << 0; |
| 42 | public final static int HORIZONTAL = 1 << 1; |
| 43 | |
| 44 | private static Method m_setValue; |
| 45 | static { |
| 46 | m_setValue = SunToolkit.getMethod(ScrollPaneAdjustable.class, "setTypedValue", new Class[] {Integer.TYPE, Integer.TYPE}); |
| 47 | SCROLLBAR = XToolkit.getUIDefaults().getInt("ScrollBar.defaultWidth"); |
| 48 | } |
| 49 | |
| 50 | XVerticalScrollbar vsb; |
| 51 | XHorizontalScrollbar hsb; |
| 52 | XWindow clip; |
| 53 | |
| 54 | int active=VERTICAL; |
| 55 | int hsbSpace; |
| 56 | int vsbSpace; |
| 57 | |
| 58 | static class XScrollPaneContentWindow extends XWindow { |
| 59 | XScrollPaneContentWindow(ScrollPane target, long parentWindow) { |
| 60 | super(target, parentWindow); |
| 61 | } |
| 62 | public String getWMName() { |
| 63 | return "ScrollPane content"; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | XScrollPanePeer(ScrollPane target) { |
| 68 | super(target); |
| 69 | |
| 70 | // Create the clip window. The field "clip" must be null when |
| 71 | // we call winCreate, or the parent of clip will be set to itself! |
| 72 | clip = null; |
| 73 | |
| 74 | |
| 75 | XWindow c = new XScrollPaneContentWindow(target,window); |
| 76 | clip = c; |
| 77 | |
| 78 | vsb = new XVerticalScrollbar(this); |
| 79 | |
| 80 | hsb = new XHorizontalScrollbar(this); |
| 81 | |
| 82 | if (target.getScrollbarDisplayPolicy() == ScrollPane.SCROLLBARS_ALWAYS) { |
| 83 | vsbSpace = hsbSpace = SCROLLBAR; |
| 84 | } else { |
| 85 | vsbSpace = hsbSpace = 0; |
| 86 | } |
| 87 | |
| 88 | int unitIncrement = 1; |
| 89 | Adjustable vAdjustable = target.getVAdjustable(); |
| 90 | if (vAdjustable != null){ |
| 91 | unitIncrement = vAdjustable.getUnitIncrement(); |
| 92 | } |
| 93 | int h = height-hsbSpace; |
| 94 | vsb.setValues(0, h, 0, h, unitIncrement, Math.max(1, (int)(h * 0.90))); |
| 95 | vsb.setSize(vsbSpace-SCROLLBAR_INSET, h); |
| 96 | |
| 97 | unitIncrement = 1; |
| 98 | Adjustable hAdjustable = target.getHAdjustable(); |
| 99 | if (hAdjustable != null){ |
| 100 | unitIncrement = hAdjustable.getUnitIncrement(); |
| 101 | } |
| 102 | int w = width - vsbSpace; |
| 103 | hsb.setValues(0, w, 0, w, unitIncrement, Math.max(1, (int)(w * 0.90))); |
| 104 | hsb.setSize(w, hsbSpace-SCROLLBAR_INSET); |
| 105 | |
| 106 | setViewportSize(); |
| 107 | clip.xSetVisible(true); |
| 108 | |
| 109 | |
| 110 | } |
| 111 | |
| 112 | public long getContentWindow() |
| 113 | { |
| 114 | return (clip == null) ? window : clip.getWindow(); |
| 115 | } |
| 116 | |
| 117 | public void setBounds(int x, int y, int w, int h, int op) { |
| 118 | super.setBounds(x, y, w, h, op); |
| 119 | |
| 120 | if (clip == null) return; |
| 121 | setScrollbarSpace(); |
| 122 | setViewportSize(); |
| 123 | repaint(); |
| 124 | } |
| 125 | |
| 126 | public Insets getInsets() { |
| 127 | return new Insets(MARGIN, MARGIN, MARGIN+hsbSpace, MARGIN+vsbSpace); |
| 128 | } |
| 129 | |
| 130 | public int getHScrollbarHeight() { |
| 131 | return SCROLLBAR; |
| 132 | } |
| 133 | |
| 134 | public int getVScrollbarWidth() { |
| 135 | return SCROLLBAR; |
| 136 | } |
| 137 | |
| 138 | public void childResized(int w, int h) { |
| 139 | if (setScrollbarSpace()) { |
| 140 | setViewportSize(); |
| 141 | } |
| 142 | repaint(); |
| 143 | } |
| 144 | |
| 145 | Dimension getChildSize() { |
| 146 | ScrollPane sp = (ScrollPane)target; |
| 147 | if (sp.countComponents() > 0) { |
| 148 | Component c = sp.getComponent(0); |
| 149 | return c.size(); |
| 150 | } else { |
| 151 | return new Dimension(0, 0); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | boolean setScrollbarSpace() { |
| 156 | ScrollPane sp = (ScrollPane)target; |
| 157 | boolean changed = false; |
| 158 | int sbDisplayPolicy = sp.getScrollbarDisplayPolicy(); |
| 159 | |
| 160 | if (sbDisplayPolicy == ScrollPane.SCROLLBARS_NEVER) { |
| 161 | return changed; |
| 162 | } |
| 163 | Dimension cSize = getChildSize(); |
| 164 | |
| 165 | if (sbDisplayPolicy == ScrollPane.SCROLLBARS_AS_NEEDED) { |
| 166 | int oldHsbSpace = hsbSpace; |
| 167 | int oldVsbSpace = vsbSpace; |
| 168 | hsbSpace = (cSize.width <= (width - 2*MARGIN) ? 0 : SCROLLBAR); |
| 169 | vsbSpace = (cSize.height <= (height - 2*MARGIN) ? 0 : SCROLLBAR); |
| 170 | |
| 171 | if (hsbSpace == 0 && vsbSpace != 0) { |
| 172 | hsbSpace = (cSize.width <= (width - SCROLLBAR - 2*MARGIN) ? 0 : SCROLLBAR); |
| 173 | } |
| 174 | if (vsbSpace == 0 && hsbSpace != 0) { |
| 175 | vsbSpace = (cSize.height <= (height - SCROLLBAR - 2*MARGIN) ? 0 : SCROLLBAR); |
| 176 | } |
| 177 | if (oldHsbSpace != hsbSpace || oldVsbSpace != vsbSpace) { |
| 178 | changed = true; |
| 179 | } |
| 180 | } |
| 181 | if (vsbSpace > 0) { |
| 182 | int vis = height - (2*MARGIN) - hsbSpace; |
| 183 | int max = Math.max(cSize.height, vis); |
| 184 | vsb.setValues(vsb.getValue(), vis, 0, max); |
| 185 | vsb.setBlockIncrement((int)(vsb.getVisibleAmount() * .90)); |
| 186 | vsb.setSize(vsbSpace-SCROLLBAR_INSET, height-hsbSpace); |
| 187 | // Adjustable vadj = sp.getVAdjustable(); |
| 188 | // vadj.setVisibleAmount(vsb.vis); |
| 189 | // vadj.setMaximum(vsb.max); |
| 190 | // vadj.setBlockIncrement(vsb.page); |
| 191 | } |
| 192 | if (hsbSpace > 0) { |
| 193 | int vis = width - (2*MARGIN) - vsbSpace; |
| 194 | int max = Math.max(cSize.width, vis); |
| 195 | hsb.setValues(hsb.getValue(), vis, 0, max); |
| 196 | hsb.setBlockIncrement((int)(hsb.getVisibleAmount() * .90)); |
| 197 | hsb.setSize(width-vsbSpace, hsbSpace-SCROLLBAR_INSET); |
| 198 | // Adjustable hadj = sp.getHAdjustable(); |
| 199 | // hadj.setVisibleAmount(hsb.vis); |
| 200 | // hadj.setMaximum(hsb.max); |
| 201 | // hadj.setBlockIncrement(hsb.page); |
| 202 | } |
| 203 | |
| 204 | // Check to see if we hid either of the scrollbars but left |
| 205 | // ourselves scrolled off of the top and/or right of the pane. |
| 206 | // If we did, we need to scroll to the top and/or right of of |
| 207 | // the pane to make it visible. |
| 208 | // |
| 209 | // Reminder: see if there is a better place to put this code. |
| 210 | boolean must_scroll = false; |
| 211 | |
| 212 | // Get the point at which the ScrollPane is currently located |
| 213 | // if number of components > 0 |
| 214 | Point p = new Point(0, 0); |
| 215 | |
| 216 | if (((ScrollPane)target).getComponentCount() > 0){ |
| 217 | |
| 218 | p = ((ScrollPane)target).getComponent(0).location(); |
| 219 | |
| 220 | if ((vsbSpace == 0) && (p.y < 0)) { |
| 221 | p.y = 0; |
| 222 | must_scroll = true; |
| 223 | } |
| 224 | |
| 225 | if ((hsbSpace == 0) && (p.x < 0)) { |
| 226 | p.x = 0; |
| 227 | must_scroll = true; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if (must_scroll) |
| 232 | scroll(x, y, VERTICAL | HORIZONTAL); |
| 233 | |
| 234 | return changed; |
| 235 | } |
| 236 | |
| 237 | void setViewportSize() { |
| 238 | clip.xSetBounds(MARGIN, MARGIN, |
| 239 | width - (2*MARGIN) - vsbSpace, |
| 240 | height - (2*MARGIN) - hsbSpace); |
| 241 | } |
| 242 | |
| 243 | public void setUnitIncrement(Adjustable adj, int u) { |
| 244 | if (adj.getOrientation() == Adjustable.VERTICAL) { |
| 245 | vsb.setUnitIncrement(u); |
| 246 | } else { |
| 247 | // HORIZONTAL |
| 248 | hsb.setUnitIncrement(u); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | public void setValue(Adjustable adj, int v) { |
| 253 | if (adj.getOrientation() == Adjustable.VERTICAL) { |
| 254 | scroll(-1, v, VERTICAL); |
| 255 | } else { |
| 256 | // HORIZONTAL |
| 257 | scroll(v, -1, HORIZONTAL); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | public void setScrollPosition(int x, int y) { |
| 262 | scroll(x, y, VERTICAL | HORIZONTAL); |
| 263 | } |
| 264 | |
| 265 | void scroll(int x, int y, int flag) { |
| 266 | scroll(x, y, flag, AdjustmentEvent.TRACK); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Scroll the contents to position x, y |
| 271 | */ |
| 272 | void scroll(int x, int y, int flag, int type) { |
| 273 | checkSecurity(); |
| 274 | ScrollPane sp = (ScrollPane)target; |
| 275 | Component c = getScrollChild(); |
| 276 | if (c == null) { |
| 277 | return; |
| 278 | } |
| 279 | int sx, sy; |
| 280 | Color colors[] = getGUIcolors(); |
| 281 | |
| 282 | if (sp.getScrollbarDisplayPolicy() == ScrollPane.SCROLLBARS_NEVER) { |
| 283 | sx = -x; |
| 284 | sy = -y; |
| 285 | } else { |
| 286 | Point p = c.location(); |
| 287 | sx = p.x; |
| 288 | sy = p.y; |
| 289 | |
| 290 | if ((flag & HORIZONTAL) != 0) { |
| 291 | hsb.setValue(Math.min(x, hsb.getMaximum()-hsb.getVisibleAmount())); |
| 292 | ScrollPaneAdjustable hadj = (ScrollPaneAdjustable)sp.getHAdjustable(); |
| 293 | setAdjustableValue(hadj, hsb.getValue(), type); |
| 294 | sx = -(hsb.getValue()); |
| 295 | Graphics g = getGraphics(); |
| 296 | try { |
| 297 | paintHorScrollbar(g, colors, true); |
| 298 | } finally { |
| 299 | g.dispose(); |
| 300 | } |
| 301 | } |
| 302 | if ((flag & VERTICAL) != 0) { |
| 303 | vsb.setValue(Math.min(y, vsb.getMaximum() - vsb.getVisibleAmount())); |
| 304 | ScrollPaneAdjustable vadj = (ScrollPaneAdjustable)sp.getVAdjustable(); |
| 305 | setAdjustableValue(vadj, vsb.getValue(), type); |
| 306 | sy = -(vsb.getValue()); |
| 307 | Graphics g = getGraphics(); |
| 308 | try { |
| 309 | paintVerScrollbar(g, colors, true); |
| 310 | } finally { |
| 311 | g.dispose(); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | c.move(sx, sy); |
| 316 | } |
| 317 | |
| 318 | void setAdjustableValue(ScrollPaneAdjustable adj, int value, int type) { |
| 319 | try { |
| 320 | m_setValue.invoke(adj, new Object[] {Integer.valueOf(value), Integer.valueOf(type)}); |
| 321 | } catch (IllegalAccessException iae) { |
| 322 | adj.setValue(value); |
| 323 | } catch (IllegalArgumentException iae2) { |
| 324 | adj.setValue(value); |
| 325 | } catch (InvocationTargetException ite) { |
| 326 | adj.setValue(value); |
| 327 | ite.getCause().printStackTrace(); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | |
| 332 | public void paint(Graphics g) { |
| 333 | paintComponent(g); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | void paintScrollBars(Graphics g, Color[] colors) { |
| 338 | if (vsbSpace > 0) { |
| 339 | paintVerScrollbar(g, colors, true); |
| 340 | // paint the whole scrollbar |
| 341 | } |
| 342 | |
| 343 | if (hsbSpace > 0) { |
| 344 | paintHorScrollbar(g, colors, true); |
| 345 | // paint the whole scrollbar |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void repaintScrollBars() { |
| 350 | Graphics g = getGraphics(); |
| 351 | Color colors[] = getGUIcolors(); |
| 352 | if (g != null) { |
| 353 | paintScrollBars(g,colors); |
| 354 | } |
| 355 | g.dispose(); |
| 356 | } |
| 357 | |
| 358 | public void repaintScrollbarRequest(XScrollbar sb) { |
| 359 | Graphics g = getGraphics(); |
| 360 | Color colors[] = getGUIcolors(); |
| 361 | if (g != null) { |
| 362 | if (sb == vsb) { |
| 363 | paintVerScrollbar(g,colors,true); |
| 364 | } |
| 365 | else if (sb == hsb) { |
| 366 | paintHorScrollbar(g,colors,true); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Paint the scrollpane. |
| 373 | */ |
| 374 | public void paintComponent(Graphics g) { |
| 375 | |
| 376 | Color colors[] = getGUIcolors(); |
| 377 | g.setColor(colors[BACKGROUND_COLOR]); |
| 378 | int h = height - hsbSpace; |
| 379 | int w = width - vsbSpace; |
| 380 | |
| 381 | g.fillRect(0, 0, w, h); |
| 382 | |
| 383 | // paint rectangular region between scrollbars |
| 384 | g.fillRect(w, h, vsbSpace, hsbSpace); |
| 385 | |
| 386 | if (MARGIN > 0) { |
| 387 | draw3DRect(g, colors, 0, 0, w - 1, h - 1, false); |
| 388 | } |
| 389 | |
| 390 | paintScrollBars(g,colors); |
| 391 | } |
| 392 | |
| 393 | public void handleEvent(java.awt.AWTEvent e) { |
| 394 | super.handleEvent(e); |
| 395 | |
| 396 | int id = e.getID(); |
| 397 | switch(id) { |
| 398 | case PaintEvent.PAINT: |
| 399 | case PaintEvent.UPDATE: |
| 400 | repaintScrollBars(); |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /** |
| 407 | * Paint the horizontal scrollbar to the screen |
| 408 | * |
| 409 | * @param g the graphics context to draw into |
| 410 | * @param colors the colors used to draw the scrollbar |
| 411 | * @param paintAll paint the whole scrollbar if true, just the thumb if false |
| 412 | */ |
| 413 | void paintHorScrollbar(Graphics g, Color colors[], boolean paintAll) { |
| 414 | if (hsbSpace <= 0) { |
| 415 | return; |
| 416 | } |
| 417 | Graphics ng = g.create(); |
| 418 | g.setColor(colors[BACKGROUND_COLOR]); |
| 419 | |
| 420 | // SCROLLBAR is the height of scrollbar area |
| 421 | // but the actual scrollbar is SCROLLBAR-SPACE high; |
| 422 | // the rest must be filled with background color |
| 423 | int w = width - vsbSpace - (2*MARGIN); |
| 424 | g.fillRect(MARGIN, height-SCROLLBAR, w, SPACE); |
| 425 | g.fillRect(0, height-SCROLLBAR, MARGIN, SCROLLBAR); |
| 426 | g.fillRect(MARGIN + w, height-SCROLLBAR, MARGIN, SCROLLBAR); |
| 427 | |
| 428 | try { |
| 429 | ng.translate(MARGIN, height - (SCROLLBAR - SPACE)); |
| 430 | hsb.paint(ng, colors, paintAll); |
| 431 | } |
| 432 | finally { |
| 433 | ng.dispose(); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | } |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | /** |
| 443 | * Paint the vertical scrollbar to the screen |
| 444 | * |
| 445 | * @param g the graphics context to draw into |
| 446 | * @param colors the colors used to draw the scrollbar |
| 447 | * @param paintAll paint the whole scrollbar if true, just the thumb if false |
| 448 | */ |
| 449 | void paintVerScrollbar(Graphics g, Color colors[], boolean paintAll) { |
| 450 | if (vsbSpace <= 0) { |
| 451 | return; |
| 452 | } |
| 453 | Graphics ng = g.create(); |
| 454 | g.setColor(colors[BACKGROUND_COLOR]); |
| 455 | |
| 456 | // SCROLLBAR is the width of scrollbar area |
| 457 | // but the actual scrollbar is SCROLLBAR-SPACE wide; |
| 458 | // the rest must be filled with background color |
| 459 | int h = height - hsbSpace - (2*MARGIN); |
| 460 | g.fillRect(width-SCROLLBAR, MARGIN, SPACE, h); |
| 461 | g.fillRect(width-SCROLLBAR, 0, SCROLLBAR, MARGIN); |
| 462 | g.fillRect(width-SCROLLBAR, MARGIN+h, SCROLLBAR, MARGIN); |
| 463 | |
| 464 | try { |
| 465 | ng.translate(width - (SCROLLBAR - SPACE), MARGIN); |
| 466 | vsb.paint(ng, colors, paintAll); |
| 467 | } |
| 468 | finally { |
| 469 | ng.dispose(); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * |
| 475 | * @see java.awt.event.MouseEvent |
| 476 | * MouseEvent.MOUSE_CLICKED |
| 477 | * MouseEvent.MOUSE_PRESSED |
| 478 | * MouseEvent.MOUSE_RELEASED |
| 479 | * MouseEvent.MOUSE_MOVED |
| 480 | * MouseEvent.MOUSE_ENTERED |
| 481 | * MouseEvent.MOUSE_EXITED |
| 482 | * MouseEvent.MOUSE_DRAGGED |
| 483 | */ |
| 484 | public void handleJavaMouseEvent( MouseEvent mouseEvent ) { |
| 485 | super.handleJavaMouseEvent(mouseEvent); |
| 486 | int modifiers = mouseEvent.getModifiers(); |
| 487 | int id = mouseEvent.getID(); |
| 488 | int x = mouseEvent.getX(); |
| 489 | int y = mouseEvent.getY(); |
| 490 | |
| 491 | |
| 492 | // super.handleMouseEvent(mouseEvent); |
| 493 | |
| 494 | if ((modifiers & InputEvent.BUTTON1_MASK) == 0) { |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | switch (id) { |
| 499 | case MouseEvent.MOUSE_PRESSED: |
| 500 | if (inVerticalScrollbar(x,y )) { |
| 501 | active = VERTICAL; |
| 502 | int h = height - hsbSpace - (2*MARGIN); |
| 503 | vsb.handleMouseEvent(id,modifiers,x - (width - SCROLLBAR + SPACE),y-MARGIN); |
| 504 | } else if (inHorizontalScrollbar(x, y) ) { |
| 505 | active = HORIZONTAL; |
| 506 | int w = width - 2*MARGIN - vsbSpace; |
| 507 | hsb.handleMouseEvent(id,modifiers,x-MARGIN,y-(height - SCROLLBAR + SPACE)); |
| 508 | } |
| 509 | break; |
| 510 | |
| 511 | // On mouse up, pass the event through to the scrollbar to stop |
| 512 | // scrolling. The x & y passed do not matter. |
| 513 | case MouseEvent.MOUSE_RELEASED: |
| 514 | // winReleaseCursorFocus(); |
| 515 | if (active == VERTICAL) { |
| 516 | vsb.handleMouseEvent(id,modifiers,x,y); |
| 517 | } else if (active == HORIZONTAL) { |
| 518 | hsb.handleMouseEvent(id,modifiers,x,y); |
| 519 | } |
| 520 | break; |
| 521 | |
| 522 | |
| 523 | case MouseEvent.MOUSE_DRAGGED: |
| 524 | if ((active == VERTICAL)) { |
| 525 | int h = height - 2*MARGIN - hsbSpace; |
| 526 | vsb.handleMouseEvent(id,modifiers,x-(width - SCROLLBAR + SPACE),y-MARGIN); |
| 527 | } else if ((active == HORIZONTAL)) { |
| 528 | int w = width - 2*MARGIN - vsbSpace; |
| 529 | hsb.handleMouseEvent(id,modifiers,x-MARGIN,y-(height - SCROLLBAR + SPACE)); |
| 530 | } |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * return value from the scrollbar |
| 537 | */ |
| 538 | public void notifyValue(XScrollbar obj, int type, int v, boolean isAdjusting) { |
| 539 | if (obj == vsb) { |
| 540 | scroll(-1, v, VERTICAL, type); |
| 541 | } else if ((XHorizontalScrollbar)obj == hsb) { |
| 542 | scroll(v, -1, HORIZONTAL, type); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * return true if the x and y position is in the verticalscrollbar |
| 548 | */ |
| 549 | boolean inVerticalScrollbar(int x, int y) { |
| 550 | if (vsbSpace <= 0) { |
| 551 | return false; |
| 552 | } |
| 553 | int h = height - MARGIN - hsbSpace; |
| 554 | return (x >= width - (SCROLLBAR - SPACE)) && (x < width) && (y >= MARGIN) && (y < h); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * return true if the x and y position is in the horizontal scrollbar |
| 559 | */ |
| 560 | boolean inHorizontalScrollbar(int x, int y) { |
| 561 | if (hsbSpace <= 0) { |
| 562 | return false; |
| 563 | } |
| 564 | int w = width - MARGIN - vsbSpace; |
| 565 | return (x >= MARGIN) && (x < w) && (y >= height - (SCROLLBAR - SPACE)) && (y < height); |
| 566 | } |
| 567 | |
| 568 | private Component getScrollChild() { |
| 569 | ScrollPane sp = (ScrollPane)target; |
| 570 | Component child = null; |
| 571 | try { |
| 572 | child = sp.getComponent(0); |
| 573 | } catch (ArrayIndexOutOfBoundsException e) { |
| 574 | // do nothing. in this case we return null |
| 575 | } |
| 576 | return child; |
| 577 | } |
| 578 | |
| 579 | int vval; |
| 580 | int hval; |
| 581 | int vmax; |
| 582 | int hmax; |
| 583 | /* |
| 584 | * Print the native component by rendering the Motif look ourselves. |
| 585 | * ToDo(aim): needs to query native motif for more accurate size and |
| 586 | * color information. |
| 587 | */ |
| 588 | public void print(Graphics g) { |
| 589 | ScrollPane sp = (ScrollPane)target; |
| 590 | Dimension d = sp.size(); |
| 591 | Color bg = sp.getBackground(); |
| 592 | Color fg = sp.getForeground(); |
| 593 | Point p = sp.getScrollPosition(); |
| 594 | Component c = getScrollChild(); |
| 595 | Dimension cd; |
| 596 | if (c != null) { |
| 597 | cd = c.size(); |
| 598 | } else { |
| 599 | cd = new Dimension(0, 0); |
| 600 | } |
| 601 | int sbDisplay = sp.getScrollbarDisplayPolicy(); |
| 602 | int vvis, hvis, vmin, hmin, vmax, hmax, vval, hval; |
| 603 | |
| 604 | switch (sbDisplay) { |
| 605 | case ScrollPane.SCROLLBARS_NEVER: |
| 606 | hsbSpace = vsbSpace = 0; |
| 607 | break; |
| 608 | case ScrollPane.SCROLLBARS_ALWAYS: |
| 609 | hsbSpace = vsbSpace = SCROLLBAR; |
| 610 | break; |
| 611 | case ScrollPane.SCROLLBARS_AS_NEEDED: |
| 612 | hsbSpace = (cd.width <= (d.width - 2*MARGIN)? 0 : SCROLLBAR); |
| 613 | vsbSpace = (cd.height <= (d.height - 2*MARGIN)? 0 : SCROLLBAR); |
| 614 | |
| 615 | if (hsbSpace == 0 && vsbSpace != 0) { |
| 616 | hsbSpace = (cd.width <= (d.width - SCROLLBAR - 2*MARGIN)? 0 : SCROLLBAR); |
| 617 | } |
| 618 | if (vsbSpace == 0 && hsbSpace != 0) { |
| 619 | vsbSpace = (cd.height <= (d.height - SCROLLBAR - 2*MARGIN)? 0 : SCROLLBAR); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | vvis = hvis = vmin = hmin = vmax = hmax = vval = hval = 0; |
| 624 | |
| 625 | if (vsbSpace > 0) { |
| 626 | vmin = 0; |
| 627 | vvis = d.height - (2*MARGIN) - hsbSpace; |
| 628 | vmax = Math.max(cd.height - vvis, 0); |
| 629 | vval = p.y; |
| 630 | } |
| 631 | if (hsbSpace > 0) { |
| 632 | hmin = 0; |
| 633 | hvis = d.width - (2*MARGIN) - vsbSpace; |
| 634 | hmax = Math.max(cd.width - hvis, 0); |
| 635 | hval = p.x; |
| 636 | } |
| 637 | |
| 638 | // need to be careful to add the margins back in here because |
| 639 | // we're drawing the margin border, after all! |
| 640 | int w = d.width - vsbSpace; |
| 641 | int h = d.height - hsbSpace; |
| 642 | |
| 643 | g.setColor(bg); |
| 644 | g.fillRect(0, 0, d.width, d.height); |
| 645 | |
| 646 | if (hsbSpace > 0) { |
| 647 | int sbw = d.width - vsbSpace; |
| 648 | g.fillRect(1, d.height - SCROLLBAR - 3, sbw - 1, SCROLLBAR - 3); |
| 649 | Graphics ng = g.create(); |
| 650 | try { |
| 651 | ng.translate(0, d.height - (SCROLLBAR - 2)); |
| 652 | drawScrollbar(ng, bg, SCROLLBAR - 2, sbw, |
| 653 | hmin, hmax, hval, hvis, true); |
| 654 | } finally { |
| 655 | ng.dispose(); |
| 656 | } |
| 657 | } |
| 658 | if (vsbSpace > 0) { |
| 659 | int sbh = d.height - hsbSpace; |
| 660 | g.fillRect(d.width - SCROLLBAR - 3, 1, SCROLLBAR - 3, sbh - 1); |
| 661 | Graphics ng = g.create(); |
| 662 | try { |
| 663 | ng.translate(d.width - (SCROLLBAR - 2), 0); |
| 664 | drawScrollbar(ng, bg, SCROLLBAR - 2, sbh, |
| 665 | vmin, vmax, vval, vvis, false); |
| 666 | } finally { |
| 667 | ng.dispose(); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | draw3DRect(g, bg, 0, 0, w - 1, h - 1, false); |
| 672 | |
| 673 | target.print(g); |
| 674 | sp.printComponents(g); |
| 675 | } |
| 676 | |
| 677 | } |