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.FINER)) {
 160             log.finer("KeyEvent on scrollbar: " + event);
 161         }
 162         if (!(event.isConsumed()) && event.getID() == KeyEvent.KEY_RELEASED) {
 163             switch(event.getKeyCode()) {
 164             case KeyEvent.VK_UP:
 165                 log.finer("Scrolling up");
 166                 tsb.notifyValue(tsb.getValue() - tsb.getUnitIncrement());
 167                 break;
 168             case KeyEvent.VK_DOWN:
 169                 log.finer("Scrolling down");
 170                 tsb.notifyValue(tsb.getValue() + tsb.getUnitIncrement());
 171                 break;
 172             case KeyEvent.VK_LEFT:
 173                 log.finer("Scrolling up");
 174                 tsb.notifyValue(tsb.getValue() - tsb.getUnitIncrement());
 175                 break;
 176             case KeyEvent.VK_RIGHT:
 177                 log.finer("Scrolling down");
 178                 tsb.notifyValue(tsb.getValue() + tsb.getUnitIncrement());
 179                 break;
 180             case KeyEvent.VK_PAGE_UP:
 181                 log.finer("Scrolling page up");
 182                 tsb.notifyValue(tsb.getValue() - tsb.getBlockIncrement());
 183                 break;
 184             case KeyEvent.VK_PAGE_DOWN:
 185                 log.finer("Scrolling page down");
 186                 tsb.notifyValue(tsb.getValue() + tsb.getBlockIncrement());
 187                 break;
 188             case KeyEvent.VK_HOME:
 189                 log.finer("Scrolling to home");
 190                 tsb.notifyValue(0);
 191                 break;
 192             case KeyEvent.VK_END:
 193                 log.finer("Scrolling to end");
 194                 tsb.notifyValue(tsb.getMaximum());
 195                 break;
 196             }
 197         }
 198     }
 199 
 200     public void setValue(int value) {
 201         tsb.setValue(value);
 202         repaint();
 203     }
 204 
 205     public void setValues(int value, int visible, int minimum, int maximum) {
 206 
 207         tsb.setValues(value, visible, minimum, maximum);
 208         repaint();
 209     }
 210 
 211     public void setLineIncrement(int l) {
 212         tsb.setUnitIncrement(l);
 213     }
 214 
 215     public void setPageIncrement(int p) {
 216         tsb.setBlockIncrement(p);
 217     }
 218 
 219     public void layout() {
 220         super.layout();
 221         tsb.setSize(width, height);
 222     }
 223 }