001: /*
002: * Copyright 1995-2001 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.awt.motif;
026:
027: import java.awt.*;
028: import java.awt.peer.*;
029: import java.awt.event.ActionEvent;
030:
031: class MButtonPeer extends MComponentPeer implements ButtonPeer {
032: native void create(MComponentPeer peer);
033:
034: public native void setLabel(String label);
035:
036: MButtonPeer(Button target) {
037: super (target);
038: }
039:
040: public Dimension getMinimumSize() {
041: FontMetrics fm = getFontMetrics(target.getFont());
042: String label = ((Button) target).getLabel();
043: if (label == null) {
044: label = "";
045: }
046: return new Dimension(fm.stringWidth(label) + 14,
047: fm.getHeight() + 8);
048: }
049:
050: public boolean isFocusable() {
051: return true;
052: }
053:
054: // NOTE: This method is called by privileged threads.
055: // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
056: public void action(final long when, final int modifiers) {
057: MToolkit.executeOnEventHandlerThread(target, new Runnable() {
058: public void run() {
059: postEvent(new ActionEvent(target,
060: ActionEvent.ACTION_PERFORMED, ((Button) target)
061: .getActionCommand(), when, modifiers));
062: }
063: });
064: }
065:
066: /*
067: * Print the native component by rendering the Motif look ourselves.
068: * ToDo(aim): needs to query native motif for more accurate size and
069: * color information.
070: */
071: public void print(Graphics g) {
072: Button b = (Button) target;
073: Dimension d = b.size();
074: Color bg = b.getBackground();
075: Color fg = b.getForeground();
076:
077: g.setColor(bg);
078: g.fillRect(2, 2, d.width - 3, d.height - 3);
079: draw3DRect(g, bg, 1, 1, d.width - 2, d.height - 2, true);
080:
081: g.setColor(fg);
082: g.setFont(b.getFont());
083: FontMetrics fm = g.getFontMetrics();
084: String lbl = b.getLabel();
085: g
086: .drawString(lbl, (d.width - fm.stringWidth(lbl)) / 2,
087: (d.height + fm.getMaxAscent() - fm
088: .getMaxDescent()) / 2);
089:
090: target.print(g);
091: }
092:
093: /**
094: * DEPRECATED
095: */
096: public Dimension minimumSize() {
097: return getMinimumSize();
098: }
099:
100: }
|