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