blob: 16cfc4ef972b5c1d5bdeadde98202feddde7ec18 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1995-1996 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package sun.awt.motif;
26
27import java.awt.*;
28import java.awt.peer.*;
29
30class MLabelPeer extends MComponentPeer implements LabelPeer {
31 native void create(MComponentPeer parent);
32
33 public void initialize() {
34 Label l = (Label)target;
35 String txt;
36 int align;
37
38 if ((txt = l.getText()) != null) {
39 setText(l.getText());
40 }
41 if ((align = l.getAlignment()) != Label.LEFT) {
42 setAlignment(align);
43 }
44 super.initialize();
45 }
46
47 MLabelPeer(Label target) {
48 super(target);
49 }
50
51 public Dimension getMinimumSize() {
52 FontMetrics fm = getFontMetrics(target.getFont());
53 String label = ((Label)target).getText();
54 if (label == null) label = "";
55 return new Dimension(fm.stringWidth(label) + 14,
56 fm.getHeight() + 8);
57 }
58
59 public native void setText(String label);
60 public native void setAlignment(int alignment);
61
62 /*
63 * Print the native component by rendering the Motif look ourselves.
64 */
65 public void print(Graphics g) {
66 Label l = (Label)target;
67 Dimension d = l.size();
68 Color bg = l.getBackground();
69 Color fg = l.getForeground();
70
71 g.setColor(bg);
72 g.fillRect(1, 1, d.width - 2, d.height - 2);
73
74 g.setColor(fg);
75 g.setFont(l.getFont());
76 FontMetrics fm = g.getFontMetrics();
77 String lbl = l.getText();
78
79 switch (l.getAlignment()) {
80 case Label.LEFT:
81 g.drawString(lbl, 2,
82 (d.height + fm.getMaxAscent() - fm.getMaxDescent()) / 2);
83 break;
84 case Label.RIGHT:
85 g.drawString(lbl, d.width - (fm.stringWidth(lbl) + 2),
86 (d.height + fm.getMaxAscent() - fm.getMaxDescent()) / 2);
87 break;
88 case Label.CENTER:
89 g.drawString(lbl, (d.width - fm.stringWidth(lbl)) / 2,
90 (d.height + fm.getMaxAscent() - fm.getMaxDescent()) / 2);
91 break;
92 }
93
94 target.print(g);
95 }
96
97 /**
98 * DEPRECATED
99 */
100 public Dimension minimumSize() {
101 return getMinimumSize();
102 }
103
104}