2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / javax / swing / plaf / basic / BasicButtonUI.java
blobd901e7ddeb73c07692b17b0e0936d103ae98d9de
1 /* BasicButtonUI.java
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.plaf.basic;
41 import javax.swing.*;
42 import javax.swing.plaf.*;
43 import java.awt.*;
46 public class BasicButtonUI extends ButtonUI
48 int gap = 3;
49 // int y_text_space = 2, x_text_space + 5;
51 Color textColor, disabledTextColor;
52 Color pressedBackgroundColor;
53 Color normalBackgroundColor;
56 public static ComponentUI createUI(final JComponent c)
58 return new BasicButtonUI();
62 public void installUI(final JComponent c)
64 super.installUI(c);
66 textColor = new Color(0,0,0);
67 disabledTextColor = new Color(130, 130, 130);
68 pressedBackgroundColor = new Color(150,150,150);
69 pressedBackgroundColor = new Color(150,150,150);
70 normalBackgroundColor = new Color(192,192,192);
74 public Dimension getPreferredSize(JComponent c)
76 AbstractButton b = (AbstractButton)c;
77 Dimension d = BasicGraphicsUtils.getPreferredButtonSize(b, gap);
78 // System.out.println("^^^^^^^^^^^^^^^^^^^^^^ BASIC-PREF="+d + ",T="+b.text);
79 return d;
83 public void paint(Graphics g, JComponent c)
85 AbstractButton b = (AbstractButton) c;
87 Rectangle tr = new Rectangle();
88 Rectangle ir = new Rectangle();
89 Rectangle vr = new Rectangle();
91 Font f = c.getFont();
93 g.setFont(f);
95 FontMetrics fm = g.getFontMetrics(f);
97 Insets i = c.getInsets();
99 vr.x = i.left;
100 vr.y = i.top;
101 vr.width = b.getWidth() - (i.right + vr.x);
102 vr.height = b.getHeight() - (i.bottom + vr.y);
104 //System.out.println(" VIEW-RECT-BUTTON="+vr+", insets="+i+", FONTM="+fm);
106 String text = SwingUtilities.layoutCompoundLabel(c,
107 fm,
108 b.getText(),
109 b.getIcon(),
110 b.getVerticalAlignment(),
111 b.getHorizontalAlignment(),
112 b.getVerticalTextPosition(),
113 b.getHorizontalTextPosition(),
117 gap);
119 if (b.getModel().isPressed() ||
120 b.getModel().isSelected())
122 //System.out.println("paint pressed");
123 paintButtonPressed(g, c);
125 else
127 //System.out.println("paint normal");
128 paintButtonNormal(g, c);
131 paintIcon(g, c, ir);
132 paintText(g, c, tr, b.getText());
133 paintFocus(g, c, vr, tr, ir);
137 protected void paintFocus(Graphics g,
138 JComponent c,
139 Rectangle vr,
140 Rectangle tr,
141 Rectangle ir)
145 protected void paintIcon(Graphics g,
146 JComponent c,
147 Rectangle iconRect)
149 AbstractButton b = (AbstractButton) c;
150 if (b.getIcon() != null)
152 int x = iconRect.x;
153 int y = iconRect.y;
155 System.out.println("WE HAVE AN ICON: " + b.getIcon());
157 b.getIcon().paintIcon(c, g, x, y);
159 else
161 //System.out.println("NO ICON FOR BUTTON:" + b.text);
165 protected void paintButtonPressed(Graphics g,
166 JComponent b)
168 Dimension size = b.getSize();
170 g.setColor(pressedBackgroundColor);
171 g.fillRect(1,1,size.width-2, size.height-2);
175 protected void paintButtonNormal(Graphics g,
176 JComponent b)
178 Dimension size = b.getSize();
180 g.setColor(normalBackgroundColor);
181 g.fillRect(1,1,size.width-2, size.height-2);
185 protected void paintText(Graphics g,
186 JComponent c,
187 Rectangle textRect,
188 String text)
190 Font f = c.getFont();
192 g.setFont(f);
194 FontMetrics fm = g.getFontMetrics(f);
196 g.setColor(c.isEnabled() ? textColor : disabledTextColor);
198 BasicGraphicsUtils.drawString(g,
199 text,
201 textRect.x,
202 textRect.y + fm.getAscent()/2);