1 /* 2 * Copyright (c) 2002, 2006, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * 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 sun.util.logging.PlatformLogger; 32 33 class XScrollbarPeer extends XComponentPeer implements ScrollbarPeer, XScrollbarClient { 34 private final static PlatformLogger log = PlatformLogger.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 * Paint the scrollbar. 95 */ 96 @Override 97 void paintPeer(final Graphics g) { 98 final Color[] colors = getGUIcolors(); 99 g.setColor(colors[BACKGROUND_COLOR]); 100 tsb.paint(g, colors, true); 101 // paint the whole scrollbar 102 } 103 104 public void repaintScrollbarRequest(XScrollbar sb) { 105 repaint(); 106 } 107 108 /** 109 * The value has changed. 110 */ 111 public void notifyValue(XScrollbar obj, int type, int value, boolean isAdjusting) { 112 Scrollbar sb = (Scrollbar)target; 113 sb.setValue(value); 114 postEvent( new AdjustmentEvent(sb, AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, type, value, isAdjusting)); 115 } 116 117 /** 118 * 119 * @see java.awt.event.MouseEvent 120 * MouseEvent.MOUSE_CLICKED 121 * MouseEvent.MOUSE_PRESSED 122 * MouseEvent.MOUSE_RELEASED 123 * MouseEvent.MOUSE_MOVED 124 * MouseEvent.MOUSE_ENTERED 125 * MouseEvent.MOUSE_EXITED 126 * MouseEvent.MOUSE_DRAGGED 127 */ 128 public void handleJavaMouseEvent( MouseEvent mouseEvent ) { 129 super.handleJavaMouseEvent(mouseEvent); 130 131 int x = mouseEvent.getX(); 132 int y = mouseEvent.getY(); 133 int modifiers = mouseEvent.getModifiers(); 134 int id = mouseEvent.getID(); 135 136 137 if ((mouseEvent.getModifiers() & InputEvent.BUTTON1_MASK) == 0) { 138 return; 139 } 140 141 switch (mouseEvent.getID()) { 142 case MouseEvent.MOUSE_PRESSED: 143 target.requestFocus(); 144 tsb.handleMouseEvent(id, modifiers,x,y); 145 break; 146 147 case MouseEvent.MOUSE_RELEASED: 148 tsb.handleMouseEvent(id, modifiers,x,y); 149 break; 150 151 case MouseEvent.MOUSE_DRAGGED: 152 tsb.handleMouseEvent(id, modifiers,x,y); 153 break; 154 } 155 } 156 157 public void handleJavaKeyEvent(KeyEvent event) { 158 super.handleJavaKeyEvent(event); 159 if (log.isLoggable(PlatformLogger.FINEST)) log.finer("KeyEvent on scrollbar: " + event); 160 if (!(event.isConsumed()) && event.getID() == KeyEvent.KEY_RELEASED) { 161 switch(event.getKeyCode()) { 162 case KeyEvent.VK_UP: 163 log.finer("Scrolling up"); 164 tsb.notifyValue(tsb.getValue() - tsb.getUnitIncrement()); 165 break; 166 case KeyEvent.VK_DOWN: 167 log.finer("Scrolling down"); 168 tsb.notifyValue(tsb.getValue() + tsb.getUnitIncrement()); 169 break; 170 case KeyEvent.VK_LEFT: 171 log.finer("Scrolling up"); 172 tsb.notifyValue(tsb.getValue() - tsb.getUnitIncrement()); 173 break; 174 case KeyEvent.VK_RIGHT: 175 log.finer("Scrolling down"); 176 tsb.notifyValue(tsb.getValue() + tsb.getUnitIncrement()); 177 break; 178 case KeyEvent.VK_PAGE_UP: 179 log.finer("Scrolling page up"); 180 tsb.notifyValue(tsb.getValue() - tsb.getBlockIncrement()); 181 break; 182 case KeyEvent.VK_PAGE_DOWN: 183 log.finer("Scrolling page down"); 184 tsb.notifyValue(tsb.getValue() + tsb.getBlockIncrement()); 185 break; 186 case KeyEvent.VK_HOME: 187 log.finer("Scrolling to home"); 188 tsb.notifyValue(0); 189 break; 190 case KeyEvent.VK_END: 191 log.finer("Scrolling to end"); 192 tsb.notifyValue(tsb.getMaximum()); 193 break; 194 } 195 } 196 } 197 198 public void setValue(int value) { 199 tsb.setValue(value); 200 repaint(); 201 } 202 203 public void setValues(int value, int visible, int minimum, int maximum) { 204 205 tsb.setValues(value, visible, minimum, maximum); 206 repaint(); 207 } 208 209 public void setLineIncrement(int l) { 210 tsb.setUnitIncrement(l); 211 } 212 213 public void setPageIncrement(int p) { 214 tsb.setBlockIncrement(p); 215 } 216 217 public void layout() { 218 super.layout(); 219 tsb.setSize(width, height); 220 } 221 }