Path: blob/master/src/java.desktop/unix/classes/sun/awt/X11/XButtonPeer.java
41159 views
/*1* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.awt.X11;2526import java.awt.*;27import java.awt.peer.*;28import java.awt.event.MouseEvent;29import java.awt.event.FocusEvent;30import java.awt.event.KeyEvent;31import java.awt.event.ActionEvent;32import javax.swing.plaf.basic.*;33import javax.swing.SwingUtilities;34import javax.swing.SwingConstants;35public class XButtonPeer extends XComponentPeer implements ButtonPeer {36private boolean pressed;37private boolean armed;38private Insets focusInsets;39private Insets borderInsets;40private Insets contentAreaInsets;4142private static final String propertyPrefix = "Button" + ".";43protected Color focusColor = SystemColor.windowText;4445private boolean disposed = false;4647String label;4849protected String getPropertyPrefix() {50return propertyPrefix;51}5253void preInit(XCreateWindowParams params) {54super.preInit(params);55borderInsets = new Insets(2,2,2,2);56focusInsets = new Insets(0,0,0,0);57contentAreaInsets = new Insets(3,3,3,3);58}596061public XButtonPeer(Button target) {62super(target);63pressed = false;64armed = false;65label = target.getLabel();66updateMotifColors(getPeerBackground());67}6869public void dispose() {70synchronized (target)71{72disposed = true;73}74super.dispose();75}7677public boolean isFocusable() {78return true;79}8081@Override82public void setLabel(String label) {83if (label == null) {84label = "";85}86if (!label.equals(this.label)) {87this.label = label;88repaint();89}90}9192public void setBackground(Color c) {93updateMotifColors(c);94super.setBackground(c);95}9697void handleJavaMouseEvent(MouseEvent e) {98super.handleJavaMouseEvent(e);99int id = e.getID();100switch (id) {101case MouseEvent.MOUSE_PRESSED:102if (XToolkit.isLeftMouseButton(e) ) {103Button b = (Button) e.getSource();104105if(b.contains(e.getX(), e.getY())) {106if (!isEnabled()) {107// Disabled buttons ignore all input...108return;109}110pressed = true;111armed = true;112repaint();113}114}115116break;117118case MouseEvent.MOUSE_RELEASED:119if (XToolkit.isLeftMouseButton(e)) {120if (armed)121{122@SuppressWarnings("deprecation")123final int modifiers = e.getModifiers();124action(e.getWhen(), modifiers);125}126pressed = false;127armed = false;128repaint();129}130131break;132133case MouseEvent.MOUSE_ENTERED:134if (pressed)135armed = true;136break;137case MouseEvent.MOUSE_EXITED:138armed = false;139break;140}141}142143144// NOTE: This method is called by privileged threads.145// DO NOT INVOKE CLIENT CODE ON THIS THREAD!146public void action(final long when, final int modifiers) {147postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,148((Button)target).getActionCommand(),149when, modifiers));150}151152153public void focusGained(FocusEvent e) {154super.focusGained(e);155repaint();156}157158public void focusLost(FocusEvent e) {159super.focusLost(e);160repaint();161}162163void handleJavaKeyEvent(KeyEvent e) {164int id = e.getID();165switch (id) {166case KeyEvent.KEY_PRESSED:167if (e.getKeyCode() == KeyEvent.VK_SPACE)168{169pressed=true;170armed=true;171repaint();172@SuppressWarnings("deprecation")173final int modifiers = e.getModifiers();174action(e.getWhen(), modifiers);175}176177break;178179case KeyEvent.KEY_RELEASED:180if (e.getKeyCode() == KeyEvent.VK_SPACE)181{182pressed = false;183armed = false;184repaint();185}186187break;188189190}191}192193public Dimension getMinimumSize() {194FontMetrics fm = getFontMetrics(getPeerFont());195if ( label == null ) {196label = "";197}198return new Dimension(fm.stringWidth(label) + 14,199fm.getHeight() + 8);200}201202/**203* This method is called from Toolkit Thread and so it should not call any204* client code.205*/206@Override207void paintPeer(final Graphics g) {208if (!disposed) {209Dimension size = getPeerSize();210g.setColor( getPeerBackground() ); /* erase the existing button remains */211g.fillRect(0,0, size.width , size.height);212paintBorder(g,borderInsets.left,213borderInsets.top,214size.width-(borderInsets.left+borderInsets.right),215size.height-(borderInsets.top+borderInsets.bottom));216217FontMetrics fm = g.getFontMetrics();218219Rectangle textRect,iconRect,viewRect;220221textRect = new Rectangle();222viewRect = new Rectangle();223iconRect = new Rectangle();224225226viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);227viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);228viewRect.x = contentAreaInsets.left;229viewRect.y = contentAreaInsets.top;230String llabel = (label != null) ? label : "";231// layout the text and icon232String text = SwingUtilities.layoutCompoundLabel(233fm, llabel, null,234SwingConstants.CENTER, SwingConstants.CENTER,235SwingConstants.CENTER, SwingConstants.CENTER,236viewRect, iconRect, textRect, 0);237238Font f = getPeerFont();239240g.setFont(f);241242// perform UI specific press action, e.g. Windows L&F shifts text243if (pressed && armed) {244paintButtonPressed(g,target);245}246247paintText(g, target, textRect, text);248249if (hasFocus()) {250// paint UI specific focus251paintFocus(g,focusInsets.left,252focusInsets.top,253size.width-(focusInsets.left+focusInsets.right)-1,254size.height-(focusInsets.top+focusInsets.bottom)-1);255}256}257flush();258}259260public void paintBorder(Graphics g, int x, int y, int w, int h) {261drawMotif3DRect(g, x, y, w-1, h-1, pressed);262}263264protected void paintFocus(Graphics g, int x, int y, int w, int h){265g.setColor(focusColor);266g.drawRect(x,y,w,h);267}268269protected void paintButtonPressed(Graphics g, Component b) {270Dimension size = getPeerSize();271g.setColor(selectColor);272g.fillRect(contentAreaInsets.left,273contentAreaInsets.top,274size.width-(contentAreaInsets.left+contentAreaInsets.right),275size.height-(contentAreaInsets.top+contentAreaInsets.bottom));276277}278protected void paintText(Graphics g, Component c, Rectangle textRect, String text) {279FontMetrics fm = g.getFontMetrics();280281int mnemonicIndex = -1;282283/* Draw the Text */284if(isEnabled()) {285/*** paint the text normally */286g.setColor(getPeerForeground());287BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );288}289else {290/*** paint the text disabled ***/291g.setColor(getPeerBackground().brighter());292BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,293textRect.x, textRect.y + fm.getAscent());294g.setColor(getPeerBackground().darker());295BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,296textRect.x - 1, textRect.y + fm.getAscent() - 1);297}298}299}300301302