1 /*
   2  * Copyright (c) 2002, 2018, 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                       @SuppressWarnings("deprecation")
 124                       final int modifiers = e.getModifiers();
 125                       action(e.getWhen(), modifiers);
 126                   }
 127                   pressed = false;
 128                   armed = false;
 129                   repaint();
 130               }
 131 
 132               break;
 133 
 134           case  MouseEvent.MOUSE_ENTERED:
 135               if (pressed)
 136                   armed = true;
 137               break;
 138           case MouseEvent.MOUSE_EXITED:
 139               armed = false;
 140               break;
 141         }
 142     }
 143 
 144 
 145     // NOTE: This method is called by privileged threads.
 146     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
 147     public void action(final long when, final int modifiers) {
 148         postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
 149                                   ((Button)target).getActionCommand(),
 150                                   when, modifiers));
 151     }
 152 
 153 
 154     public void focusGained(FocusEvent e) {
 155         super.focusGained(e);
 156         repaint();
 157     }
 158 
 159     public void focusLost(FocusEvent e) {
 160         super.focusLost(e);
 161         repaint();
 162     }
 163 
 164     void handleJavaKeyEvent(KeyEvent e) {
 165         int id = e.getID();
 166         switch (id) {
 167           case KeyEvent.KEY_PRESSED:
 168               if (e.getKeyCode() == KeyEvent.VK_SPACE)
 169               {
 170                   pressed=true;
 171                   armed=true;
 172                   repaint();
 173                   @SuppressWarnings("deprecation")
 174                   final int modifiers = e.getModifiers();
 175                   action(e.getWhen(), modifiers);
 176               }
 177 
 178               break;
 179 
 180           case KeyEvent.KEY_RELEASED:
 181               if (e.getKeyCode() == KeyEvent.VK_SPACE)
 182               {
 183                   pressed = false;
 184                   armed = false;
 185                   repaint();
 186               }
 187 
 188               break;
 189 
 190 
 191         }
 192     }
 193 
 194     public Dimension getMinimumSize() {
 195         FontMetrics fm = getFontMetrics(getPeerFont());
 196         if ( label == null ) {
 197             label = "";
 198         }
 199         return new Dimension(fm.stringWidth(label) + 14,
 200                              fm.getHeight() + 8);
 201     }
 202 
 203     /**
 204      * This method is called from Toolkit Thread and so it should not call any
 205      * client code.
 206      */
 207     @Override
 208     void paintPeer(final Graphics g) {
 209         if (!disposed) {
 210             Dimension size = getPeerSize();
 211             g.setColor( getPeerBackground() );   /* erase the existing button remains */
 212             g.fillRect(0,0, size.width , size.height);
 213             paintBorder(g,borderInsets.left,
 214                         borderInsets.top,
 215                         size.width-(borderInsets.left+borderInsets.right),
 216                         size.height-(borderInsets.top+borderInsets.bottom));
 217 
 218             FontMetrics fm = g.getFontMetrics();
 219 
 220             Rectangle textRect,iconRect,viewRect;
 221 
 222             textRect = new Rectangle();
 223             viewRect = new Rectangle();
 224             iconRect = new Rectangle();
 225 
 226 
 227             viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);
 228             viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);
 229             viewRect.x = contentAreaInsets.left;
 230             viewRect.y = contentAreaInsets.top;
 231             String llabel = (label != null) ? label : "";
 232             // layout the text and icon
 233             String text = SwingUtilities.layoutCompoundLabel(
 234                                                              fm, llabel, null,
 235                                                              SwingConstants.CENTER, SwingConstants.CENTER,
 236                                                              SwingConstants.CENTER, SwingConstants.CENTER,
 237                                                              viewRect, iconRect, textRect, 0);
 238 
 239             Font f = getPeerFont();
 240 
 241             g.setFont(f);
 242 
 243             // perform UI specific press action, e.g. Windows L&F shifts text
 244             if (pressed && armed) {
 245                 paintButtonPressed(g,target);
 246             }
 247 
 248             paintText(g, target, textRect, text);
 249 
 250             if (hasFocus()) {
 251                 // paint UI specific focus
 252                 paintFocus(g,focusInsets.left,
 253                            focusInsets.top,
 254                            size.width-(focusInsets.left+focusInsets.right)-1,
 255                            size.height-(focusInsets.top+focusInsets.bottom)-1);
 256             }
 257         }
 258         flush();
 259     }
 260 
 261     public void paintBorder(Graphics g, int x, int y, int w, int h) {
 262         drawMotif3DRect(g, x, y, w-1, h-1, pressed);
 263     }
 264 
 265     protected void paintFocus(Graphics g, int x, int y, int w, int h){
 266         g.setColor(focusColor);
 267         g.drawRect(x,y,w,h);
 268     }
 269 
 270     protected void paintButtonPressed(Graphics g, Component b) {
 271         Dimension size = getPeerSize();
 272         g.setColor(selectColor);
 273         g.fillRect(contentAreaInsets.left,
 274                    contentAreaInsets.top,
 275                    size.width-(contentAreaInsets.left+contentAreaInsets.right),
 276                    size.height-(contentAreaInsets.top+contentAreaInsets.bottom));
 277 
 278     }
 279     protected void paintText(Graphics g, Component c, Rectangle textRect, String text) {
 280         FontMetrics fm = g.getFontMetrics();
 281 
 282         int mnemonicIndex = -1;
 283 
 284         /* Draw the Text */
 285         if(isEnabled()) {
 286             /*** paint the text normally */
 287             g.setColor(getPeerForeground());
 288             BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
 289         }
 290         else {
 291             /*** paint the text disabled ***/
 292             g.setColor(getPeerBackground().brighter());
 293             BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
 294                                                          textRect.x, textRect.y + fm.getAscent());
 295             g.setColor(getPeerBackground().darker());
 296             BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
 297                                                          textRect.x - 1, textRect.y + fm.getAscent() - 1);
 298         }
 299     }
 300 }