1 /*
   2  * Copyright (c) 2002, 2013, 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 package sun.awt.X11;
  26 
  27 import java.awt.*;
  28 import java.awt.peer.*;
  29 import java.awt.event.MouseEvent;
  30 import java.awt.event.FocusEvent;
  31 import java.awt.event.KeyEvent;
  32 import java.awt.event.ActionEvent;
  33 import javax.swing.plaf.basic.*;
  34 import javax.swing.SwingUtilities;
  35 import javax.swing.SwingConstants;
  36 public class XButtonPeer extends XComponentPeer implements ButtonPeer {
  37     private boolean pressed;
  38     private boolean armed;
  39     private Insets focusInsets;
  40     private Insets borderInsets;
  41     private Insets contentAreaInsets;
  42 
  43     private static final String propertyPrefix = "Button" + ".";
  44     protected Color focusColor =  SystemColor.windowText;
  45 
  46     private boolean disposed = false;
  47 
  48     String label;
  49 
  50     protected String getPropertyPrefix() {
  51         return propertyPrefix;
  52     }
  53 
  54     void preInit(XCreateWindowParams params) {
  55         super.preInit(params);
  56         borderInsets = new Insets(2,2,2,2);
  57         focusInsets = new Insets(0,0,0,0);
  58         contentAreaInsets = new Insets(3,3,3,3);
  59     }
  60 
  61 
  62     public  XButtonPeer(Button target) {
  63         super(target);
  64         pressed = false;
  65         armed = false;
  66         label = target.getLabel();
  67         updateMotifColors(getPeerBackground());
  68     }
  69 
  70     public  void dispose() {
  71         synchronized (target)
  72         {
  73             disposed = true;
  74         }
  75         super.dispose();
  76     }
  77 
  78     public boolean isFocusable() {
  79         return true;
  80     }
  81 
  82     @Override
  83     public void setLabel(String label) {
  84         if (label == null) {
  85             label = "";
  86         }
  87         if (!label.equals(this.label)) {
  88             this.label = label;
  89             repaint();
  90         }
  91     }
  92 
  93     public void setBackground(Color c) {
  94         updateMotifColors(c);
  95         super.setBackground(c);
  96     }
  97 
  98     void handleJavaMouseEvent(MouseEvent e) {
  99         super.handleJavaMouseEvent(e);
 100         int id = e.getID();
 101         switch (id) {
 102           case MouseEvent.MOUSE_PRESSED:
 103               if (XToolkit.isLeftMouseButton(e) ) {
 104                   Button b = (Button) e.getSource();
 105 
 106                   if(b.contains(e.getX(), e.getY())) {
 107                       if (!isEnabled()) {
 108                           // Disabled buttons ignore all input...
 109                           return;
 110                       }
 111                       pressed = true;
 112                       armed = true;
 113                       repaint();
 114                   }
 115               }
 116 
 117               break;
 118 
 119           case MouseEvent.MOUSE_RELEASED:
 120               if (XToolkit.isLeftMouseButton(e)) {
 121                   if (armed)
 122                   {
 123                       action(e.getWhen(),e.getModifiers());
 124                   }
 125                   pressed = false;
 126                   armed = false;
 127                   repaint();
 128               }
 129 
 130               break;
 131 
 132           case  MouseEvent.MOUSE_ENTERED:
 133               if (pressed)
 134                   armed = true;
 135               break;
 136           case MouseEvent.MOUSE_EXITED:
 137               armed = false;
 138               break;
 139         }
 140     }
 141 
 142 
 143     // NOTE: This method is called by privileged threads.
 144     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
 145     public void action(final long when, final int modifiers) {
 146         postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
 147                                   ((Button)target).getActionCommand(),
 148                                   when, modifiers));
 149     }
 150 
 151 
 152     public void focusGained(FocusEvent e) {
 153         super.focusGained(e);
 154         repaint();
 155     }
 156 
 157     public void focusLost(FocusEvent e) {
 158         super.focusLost(e);
 159         repaint();
 160     }
 161 
 162     void handleJavaKeyEvent(KeyEvent e) {
 163         int id = e.getID();
 164         switch (id) {
 165           case KeyEvent.KEY_PRESSED:
 166               if (e.getKeyCode() == KeyEvent.VK_SPACE)
 167               {
 168                   pressed=true;
 169                   armed=true;
 170                   repaint();
 171                   action(e.getWhen(),e.getModifiers());
 172               }
 173 
 174               break;
 175 
 176           case KeyEvent.KEY_RELEASED:
 177               if (e.getKeyCode() == KeyEvent.VK_SPACE)
 178               {
 179                   pressed = false;
 180                   armed = false;
 181                   repaint();
 182               }
 183 
 184               break;
 185 
 186 
 187         }
 188     }
 189 
 190     public Dimension getMinimumSize() {
 191         FontMetrics fm = getFontMetrics(getPeerFont());
 192         if ( label == null ) {
 193             label = "";
 194         }
 195         return new Dimension(fm.stringWidth(label) + 14,
 196                              fm.getHeight() + 8);
 197     }
 198 
 199     /**
 200      * DEPRECATED
 201      */
 202     public Dimension minimumSize() {
 203         return getMinimumSize();
 204     }
 205     /**
 206      * This method is called from Toolkit Thread and so it should not call any
 207      * client code.
 208      */
 209     @Override
 210     void paintPeer(final Graphics g) {
 211         if (!disposed) {
 212             Dimension size = getPeerSize();
 213             g.setColor( getPeerBackground() );   /* erase the existing button remains */
 214             g.fillRect(0,0, size.width , size.height);
 215             paintBorder(g,borderInsets.left,
 216                         borderInsets.top,
 217                         size.width-(borderInsets.left+borderInsets.right),
 218                         size.height-(borderInsets.top+borderInsets.bottom));
 219 
 220             FontMetrics fm = g.getFontMetrics();
 221 
 222             Rectangle textRect,iconRect,viewRect;
 223 
 224             textRect = new Rectangle();
 225             viewRect = new Rectangle();
 226             iconRect = new Rectangle();
 227 
 228 
 229             viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);
 230             viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);
 231             viewRect.x = contentAreaInsets.left;
 232             viewRect.y = contentAreaInsets.top;
 233             String llabel = (label != null) ? label : "";
 234             // layout the text and icon
 235             String text = SwingUtilities.layoutCompoundLabel(
 236                                                              fm, llabel, null,
 237                                                              SwingConstants.CENTER, SwingConstants.CENTER,
 238                                                              SwingConstants.CENTER, SwingConstants.CENTER,
 239                                                              viewRect, iconRect, textRect, 0);
 240 
 241             Font f = getPeerFont();
 242 
 243             g.setFont(f);
 244 
 245             // perform UI specific press action, e.g. Windows L&F shifts text
 246             if (pressed && armed) {
 247                 paintButtonPressed(g,target);
 248             }
 249 
 250             paintText(g, target, textRect, text);
 251 
 252             if (hasFocus()) {
 253                 // paint UI specific focus
 254                 paintFocus(g,focusInsets.left,
 255                            focusInsets.top,
 256                            size.width-(focusInsets.left+focusInsets.right)-1,
 257                            size.height-(focusInsets.top+focusInsets.bottom)-1);
 258             }
 259         }
 260         flush();
 261     }
 262 
 263     public void paintBorder(Graphics g, int x, int y, int w, int h) {
 264         drawMotif3DRect(g, x, y, w-1, h-1, pressed);
 265     }
 266 
 267     protected void paintFocus(Graphics g, int x, int y, int w, int h){
 268         g.setColor(focusColor);
 269         g.drawRect(x,y,w,h);
 270     }
 271 
 272     protected void paintButtonPressed(Graphics g, Component b) {
 273         Dimension size = getPeerSize();
 274         g.setColor(selectColor);
 275         g.fillRect(contentAreaInsets.left,
 276                    contentAreaInsets.top,
 277                    size.width-(contentAreaInsets.left+contentAreaInsets.right),
 278                    size.height-(contentAreaInsets.top+contentAreaInsets.bottom));
 279 
 280     }
 281     protected void paintText(Graphics g, Component c, Rectangle textRect, String text) {
 282         FontMetrics fm = g.getFontMetrics();
 283 
 284         int mnemonicIndex = -1;
 285 
 286         /* Draw the Text */
 287         if(isEnabled()) {
 288             /*** paint the text normally */
 289             g.setColor(getPeerForeground());
 290             BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
 291         }
 292         else {
 293             /*** paint the text disabled ***/
 294             g.setColor(getPeerBackground().brighter());
 295             BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
 296                                                          textRect.x, textRect.y + fm.getAscent());
 297             g.setColor(getPeerBackground().darker());
 298             BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
 299                                                          textRect.x - 1, textRect.y + fm.getAscent() - 1);
 300         }
 301     }
 302 }