0N/A/*
2273N/A * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1472N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
1879N/Apackage sun.awt.X11;
1879N/A
1879N/Aimport java.awt.*;
1879N/Aimport java.awt.peer.*;
1879N/Aimport java.awt.event.MouseEvent;
1879N/Aimport java.awt.event.FocusEvent;
0N/Aimport java.awt.event.KeyEvent;
0N/Aimport java.awt.event.ActionEvent;
0N/Aimport javax.swing.plaf.basic.*;
2062N/Aimport javax.swing.SwingUtilities;
0N/Aimport javax.swing.SwingConstants;
2062N/A
3863N/Apublic class XButtonPeer extends XComponentPeer implements ButtonPeer {
2062N/A
2062N/A boolean pressed;
0N/A boolean armed;
0N/A
0N/A private Insets focusInsets;
0N/A private Insets borderInsets;
0N/A private Insets contentAreaInsets;
0N/A
0N/A private final static String propertyPrefix = "Button" + ".";
0N/A protected Color focusColor = SystemColor.windowText;
0N/A
0N/A private boolean disposed = false;
0N/A
2062N/A String label;
2062N/A
2062N/A protected String getPropertyPrefix() {
2062N/A return propertyPrefix;
3863N/A }
2062N/A
2062N/A void preInit(XCreateWindowParams params) {
0N/A super.preInit(params);
0N/A borderInsets = new Insets(2,2,2,2);
0N/A focusInsets = new Insets(0,0,0,0);
0N/A contentAreaInsets = new Insets(3,3,3,3);
0N/A }
0N/A
2062N/A
2062N/A public XButtonPeer(Button target) {
0N/A super(target);
2062N/A pressed = false;
0N/A armed = false;
0N/A label = target.getLabel();
0N/A updateMotifColors(getPeerBackground());
2062N/A }
0N/A
0N/A public void dispose() {
0N/A synchronized (target)
0N/A {
0N/A disposed = true;
0N/A }
2062N/A super.dispose();
0N/A }
0N/A
0N/A public boolean isFocusable() {
0N/A return true;
0N/A }
2062N/A
0N/A public void setLabel(java.lang.String label) {
0N/A this.label = label;
0N/A repaint();
0N/A }
0N/A
0N/A public void paint(Graphics g) {
0N/A paint(g,target);
0N/A }
0N/A
0N/A public void setBackground(Color c) {
2062N/A updateMotifColors(c);
0N/A super.setBackground(c);
0N/A }
0N/A
0N/A void handleJavaMouseEvent(MouseEvent e) {
0N/A super.handleJavaMouseEvent(e);
0N/A int id = e.getID();
0N/A switch (id) {
0N/A case MouseEvent.MOUSE_PRESSED:
0N/A if (XToolkit.isLeftMouseButton(e) ) {
2062N/A Button b = (Button) e.getSource();
0N/A
0N/A if(b.contains(e.getX(), e.getY())) {
0N/A if (!isEnabled()) {
0N/A // Disabled buttons ignore all input...
0N/A return;
0N/A }
2062N/A pressed = true;
2062N/A armed = true;
0N/A repaint();
0N/A }
2062N/A }
0N/A
0N/A break;
0N/A
0N/A case MouseEvent.MOUSE_RELEASED:
0N/A if (XToolkit.isLeftMouseButton(e)) {
0N/A if (armed)
0N/A {
2062N/A action(e.getWhen(),e.getModifiers());
0N/A }
0N/A pressed = false;
0N/A armed = false;
0N/A repaint();
0N/A }
0N/A
0N/A break;
0N/A
2062N/A case MouseEvent.MOUSE_ENTERED:
0N/A if (pressed)
0N/A armed = true;
0N/A// repaint();
0N/A
0N/A break;
0N/A
0N/A case MouseEvent.MOUSE_EXITED:
0N/A armed = false;
0N/A// repaint();
0N/A
0N/A break;
0N/A
0N/A }
0N/A }
0N/A
0N/A
0N/A // NOTE: This method is called by privileged threads.
0N/A // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
0N/A public void action(final long when, final int modifiers) {
0N/A postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
0N/A ((Button)target).getActionCommand(),
0N/A when, modifiers));
2062N/A }
0N/A
0N/A
0N/A public void focusGained(FocusEvent e) {
0N/A super.focusGained(e);
0N/A repaint();
0N/A }
0N/A
0N/A public void focusLost(FocusEvent e) {
0N/A super.focusLost(e);
0N/A repaint();
0N/A }
0N/A
3863N/A void handleJavaKeyEvent(KeyEvent e) {
0N/A int id = e.getID();
0N/A switch (id) {
0N/A case KeyEvent.KEY_PRESSED:
0N/A if (e.getKeyCode() == KeyEvent.VK_SPACE)
0N/A {
0N/A pressed=true;
0N/A armed=true;
0N/A repaint();
0N/A action(e.getWhen(),e.getModifiers());
0N/A }
0N/A
0N/A break;
0N/A
0N/A case KeyEvent.KEY_RELEASED:
0N/A if (e.getKeyCode() == KeyEvent.VK_SPACE)
2062N/A {
0N/A pressed = false;
0N/A armed = false;
0N/A repaint();
0N/A }
0N/A
0N/A break;
0N/A
0N/A
0N/A }
2062N/A }
0N/A
0N/A public Dimension getMinimumSize() {
0N/A FontMetrics fm = getFontMetrics(getPeerFont());
0N/A if ( label == null ) {
2062N/A label = "";
0N/A }
0N/A return new Dimension(fm.stringWidth(label) + 14,
0N/A fm.getHeight() + 8);
0N/A }
0N/A
0N/A /**
0N/A * DEPRECATED
0N/A */
2062N/A public Dimension minimumSize() {
0N/A return getMinimumSize();
0N/A }
0N/A
0N/A
0N/A /*
0N/A This method is called from Toolkit Thread and so it should not call any client code
0N/A
0N/A */
0N/A public void paint(Graphics g, Component c)
0N/A {
0N/A if (!disposed && (g != null))
0N/A {
0N/A Dimension size = getPeerSize();
0N/A
0N/A g.setColor( getPeerBackground() ); /* erase the existing button remains */
0N/A g.fillRect(0,0, size.width , size.height);
0N/A paintBorder(g,borderInsets.left,
0N/A borderInsets.top,
0N/A size.width-(borderInsets.left+borderInsets.right),
0N/A size.height-(borderInsets.top+borderInsets.bottom));
0N/A
0N/A FontMetrics fm = g.getFontMetrics();
0N/A
0N/A Rectangle textRect,iconRect,viewRect;
0N/A
0N/A textRect = new Rectangle();
0N/A viewRect = new Rectangle();
0N/A iconRect = new Rectangle();
0N/A
0N/A
0N/A viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);
0N/A viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);
0N/A
0N/A viewRect.x = contentAreaInsets.left;
0N/A viewRect.y = contentAreaInsets.right;
0N/A String llabel = (label != null) ? label : "";
0N/A
0N/A // layout the text and icon
0N/A String text = SwingUtilities.layoutCompoundLabel(
0N/A fm, llabel, null,
0N/A SwingConstants.CENTER, SwingConstants.CENTER,
0N/A SwingConstants.CENTER, SwingConstants.CENTER,
0N/A viewRect, iconRect, textRect, 0);
0N/A
0N/A Font f = getPeerFont();
0N/A
0N/A g.setFont(f);
0N/A
0N/A // perform UI specific press action, e.g. Windows L&F shifts text
0N/A if (pressed && armed) {
0N/A paintButtonPressed(g,target);
0N/A }
0N/A
0N/A paintText(g, target, textRect, text);
0N/A
0N/A if (hasFocus()) {
0N/A // paint UI specific focus
0N/A paintFocus(g,focusInsets.left,
0N/A focusInsets.top,
0N/A size.width-(focusInsets.left+focusInsets.right)-1,
0N/A size.height-(focusInsets.top+focusInsets.bottom)-1);
0N/A }
0N/A }
0N/A flush();
0N/A }
0N/A
0N/A public void paintBorder(Graphics g, int x, int y, int w, int h) {
drawMotif3DRect(g, x, y, w-1, h-1, pressed);
}
public void setFont(Font f) {
super.setFont(f);
target.repaint();
}
protected void paintFocus(Graphics g, int x, int y, int w, int h){
g.setColor(focusColor);
g.drawRect(x,y,w,h);
}
protected void paintButtonPressed(Graphics g, Component b) {
Dimension size = getPeerSize();
g.setColor(selectColor);
g.fillRect(contentAreaInsets.left,
contentAreaInsets.top,
size.width-(contentAreaInsets.left+contentAreaInsets.right),
size.height-(contentAreaInsets.top+contentAreaInsets.bottom));
}
protected void paintText(Graphics g, Component c, Rectangle textRect, String text) {
FontMetrics fm = g.getFontMetrics();
int mnemonicIndex = -1;
/* Draw the Text */
if(isEnabled()) {
/*** paint the text normally */
g.setColor(getPeerForeground());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
}
else {
/*** paint the text disabled ***/
g.setColor(getPeerBackground().brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x, textRect.y + fm.getAscent());
g.setColor(c.getBackground().darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x - 1, textRect.y + fm.getAscent() - 1);
}
}
}