jdk/src/solaris/classes/sun/awt/X11/XLabelPeer.java
author yan
Mon, 06 Oct 2008 16:45:00 +0400
changeset 1966 12a51fb0db0d
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
5100701: Toolkit.getLockingKeyState() does not work on XToolkit, but works on Motif Summary: Does not work on Motif but works on XToolkit now; implemented using XQueryPointer. Reviewed-by: anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.peer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
class XLabelPeer extends XComponentPeer implements LabelPeer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
     * Create the label
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static final int            TEXT_XPAD = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static final int            TEXT_YPAD = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    String label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    int alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    FontMetrics cachedFontMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    Font oldfont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    FontMetrics getFontMetrics()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if (cachedFontMetrics != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            return cachedFontMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        else return getFontMetrics(getPeerFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    void preInit(XCreateWindowParams params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        super.preInit(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        Label target = (Label) this.target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        label = target.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (label == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            label = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        alignment = target.getAlignment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    XLabelPeer(Label target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        super(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Minimum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        FontMetrics fm = getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            w = fm.stringWidth(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        catch (NullPointerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            w = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return new Dimension(w + TEXT_XPAD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                             fm.getAscent() + fm.getMaxDescent() + TEXT_YPAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Paint the label
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // NOTE: This method is called by privileged threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int textX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int textY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        g.setColor(getPeerBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        g.fillRect(0, 0, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        Font f = getPeerFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        g.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (cachedFontMetrics == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            cachedFontMetrics = fm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (oldfont != f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                cachedFontMetrics = fm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        switch (alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
          case Label.LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
              textX = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
              textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
          case Label.RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
              textX = width - (fm.stringWidth(label) + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
              textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
          case Label.CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
              textX = (width - fm.stringWidth(label)) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
              textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            g.setColor(getPeerForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            g.drawString(label, textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            g.setColor(getPeerBackground().brighter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            g.drawString(label, textX, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            g.setColor(getPeerBackground().darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            g.drawString(label, textX - 1, textY - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public void setText(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        label = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (label == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            label = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public void setFont(Font f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        super.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        target.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public void setAlignment(int align) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        alignment = align;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
}