J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2006 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.util.logging.*; |
| 32 | |
| 33 | class XScrollbarPeer extends XComponentPeer implements ScrollbarPeer, XScrollbarClient { |
| 34 | private final static Logger log = Logger.getLogger("sun.awt.X11.XScrollbarPeer"); |
| 35 | |
| 36 | private static final int DEFAULT_LENGTH = 50; |
| 37 | private static final int DEFAULT_WIDTH_SOLARIS = 19; |
| 38 | private static final int DEFAULT_WIDTH_LINUX; |
| 39 | |
| 40 | XScrollbar tsb; |
| 41 | |
| 42 | static { |
| 43 | DEFAULT_WIDTH_LINUX = XToolkit.getUIDefaults().getInt("ScrollBar.defaultWidth"); |
| 44 | } |
| 45 | |
| 46 | public void preInit(XCreateWindowParams params) { |
| 47 | super.preInit(params); |
| 48 | Scrollbar target = (Scrollbar) this.target; |
| 49 | if (target.getOrientation() == Scrollbar.VERTICAL) { |
| 50 | tsb = new XVerticalScrollbar(this); |
| 51 | } else { |
| 52 | tsb = new XHorizontalScrollbar(this); |
| 53 | } |
| 54 | int min = target.getMinimum(); |
| 55 | int max = target.getMaximum(); |
| 56 | int vis = target.getVisibleAmount(); |
| 57 | int val = target.getValue(); |
| 58 | int line = target.getLineIncrement(); |
| 59 | int page = target.getPageIncrement(); |
| 60 | tsb.setValues(val, vis, min, max, line, page); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Create a scrollbar. |
| 65 | */ |
| 66 | XScrollbarPeer(Scrollbar target) { |
| 67 | super(target); |
| 68 | this.target = target; |
| 69 | xSetVisible(true); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns default size of scrollbar on the platform |
| 74 | * Currently uses hardcoded values |
| 75 | */ |
| 76 | private int getDefaultDimension() { |
| 77 | if (System.getProperty("os.name").equals("Linux")) { |
| 78 | return DEFAULT_WIDTH_LINUX; |
| 79 | } else { |
| 80 | return DEFAULT_WIDTH_SOLARIS; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Compute the minimum size for the scrollbar. |
| 86 | */ |
| 87 | public Dimension getMinimumSize() { |
| 88 | Scrollbar sb = (Scrollbar)target; |
| 89 | return (sb.getOrientation() == Scrollbar.VERTICAL) |
| 90 | ? new Dimension(getDefaultDimension(), DEFAULT_LENGTH) |
| 91 | : new Dimension(DEFAULT_LENGTH, getDefaultDimension()); |
| 92 | } |
| 93 | |
| 94 | public void repaint() { |
| 95 | Graphics g = getGraphics(); |
| 96 | if (g != null) paint(g); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Paint the scrollbar. |
| 101 | */ |
| 102 | public void paint(Graphics g) { |
| 103 | Scrollbar sb = (Scrollbar)target; |
| 104 | Color colors[] = getGUIcolors(); |
| 105 | g.setColor(colors[BACKGROUND_COLOR]); |
| 106 | tsb.paint(g, colors, true); |
| 107 | // paint the whole scrollbar |
| 108 | } |
| 109 | |
| 110 | public void repaintScrollbarRequest(XScrollbar sb) { |
| 111 | repaint(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * The value has changed. |
| 116 | */ |
| 117 | public void notifyValue(XScrollbar obj, int type, int value, boolean isAdjusting) { |
| 118 | Scrollbar sb = (Scrollbar)target; |
| 119 | sb.setValue(value); |
| 120 | postEvent( new AdjustmentEvent(sb, AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, type, value, isAdjusting)); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * |
| 125 | * @see java.awt.event.MouseEvent |
| 126 | * MouseEvent.MOUSE_CLICKED |
| 127 | * MouseEvent.MOUSE_PRESSED |
| 128 | * MouseEvent.MOUSE_RELEASED |
| 129 | * MouseEvent.MOUSE_MOVED |
| 130 | * MouseEvent.MOUSE_ENTERED |
| 131 | * MouseEvent.MOUSE_EXITED |
| 132 | * MouseEvent.MOUSE_DRAGGED |
| 133 | */ |
| 134 | public void handleJavaMouseEvent( MouseEvent mouseEvent ) { |
| 135 | super.handleJavaMouseEvent(mouseEvent); |
| 136 | |
| 137 | int x = mouseEvent.getX(); |
| 138 | int y = mouseEvent.getY(); |
| 139 | int modifiers = mouseEvent.getModifiers(); |
| 140 | int id = mouseEvent.getID(); |
| 141 | |
| 142 | |
| 143 | if ((mouseEvent.getModifiers() & InputEvent.BUTTON1_MASK) == 0) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | switch (mouseEvent.getID()) { |
| 148 | case MouseEvent.MOUSE_PRESSED: |
| 149 | target.requestFocus(); |
| 150 | tsb.handleMouseEvent(id, modifiers,x,y); |
| 151 | break; |
| 152 | |
| 153 | case MouseEvent.MOUSE_RELEASED: |
| 154 | tsb.handleMouseEvent(id, modifiers,x,y); |
| 155 | break; |
| 156 | |
| 157 | case MouseEvent.MOUSE_DRAGGED: |
| 158 | tsb.handleMouseEvent(id, modifiers,x,y); |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | public void handleJavaKeyEvent(KeyEvent event) { |
| 164 | super.handleJavaKeyEvent(event); |
| 165 | if (log.isLoggable(Level.FINEST)) log.finer("KeyEvent on scrollbar: " + event); |
| 166 | if (!(event.isConsumed()) && event.getID() == KeyEvent.KEY_RELEASED) { |
| 167 | switch(event.getKeyCode()) { |
| 168 | case KeyEvent.VK_UP: |
| 169 | log.finer("Scrolling up"); |
| 170 | tsb.notifyValue(tsb.getValue() - tsb.getUnitIncrement()); |
| 171 | break; |
| 172 | case KeyEvent.VK_DOWN: |
| 173 | log.finer("Scrolling down"); |
| 174 | tsb.notifyValue(tsb.getValue() + tsb.getUnitIncrement()); |
| 175 | break; |
| 176 | case KeyEvent.VK_LEFT: |
| 177 | log.finer("Scrolling up"); |
| 178 | tsb.notifyValue(tsb.getValue() - tsb.getUnitIncrement()); |
| 179 | break; |
| 180 | case KeyEvent.VK_RIGHT: |
| 181 | log.finer("Scrolling down"); |
| 182 | tsb.notifyValue(tsb.getValue() + tsb.getUnitIncrement()); |
| 183 | break; |
| 184 | case KeyEvent.VK_PAGE_UP: |
| 185 | log.finer("Scrolling page up"); |
| 186 | tsb.notifyValue(tsb.getValue() - tsb.getBlockIncrement()); |
| 187 | break; |
| 188 | case KeyEvent.VK_PAGE_DOWN: |
| 189 | log.finer("Scrolling page down"); |
| 190 | tsb.notifyValue(tsb.getValue() + tsb.getBlockIncrement()); |
| 191 | break; |
| 192 | case KeyEvent.VK_HOME: |
| 193 | log.finer("Scrolling to home"); |
| 194 | tsb.notifyValue(0); |
| 195 | break; |
| 196 | case KeyEvent.VK_END: |
| 197 | log.finer("Scrolling to end"); |
| 198 | tsb.notifyValue(tsb.getMaximum()); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | public void setValue(int value) { |
| 205 | tsb.setValue(value); |
| 206 | repaint(); |
| 207 | } |
| 208 | |
| 209 | public void setValues(int value, int visible, int minimum, int maximum) { |
| 210 | |
| 211 | tsb.setValues(value, visible, minimum, maximum); |
| 212 | repaint(); |
| 213 | } |
| 214 | |
| 215 | public void setLineIncrement(int l) { |
| 216 | tsb.setUnitIncrement(l); |
| 217 | } |
| 218 | |
| 219 | public void setPageIncrement(int p) { |
| 220 | tsb.setBlockIncrement(p); |
| 221 | } |
| 222 | |
| 223 | public void layout() { |
| 224 | super.layout(); |
| 225 | tsb.setSize(width, height); |
| 226 | } |
| 227 | } |