4 public class CbButton extends Canvas
6 public static final int LEFT = 0;
7 public static final int RIGHT = 1;
8 public static final int ABOVE = 2;
9 public static final int BELOW = 3;
13 CbButtonCallback callback;
15 int iwidth, iheight, pwidth, pheight, twidth, theight;
16 boolean inside, indent;
21 Color lc1 = Util.light_edge, lc2 = Util.body, lc3 = Util.dark_edge;
22 Color hc1 = Util.light_edge_hi, hc2 = Util.body_hi, hc3 = Util.dark_edge_hi;
24 public CbButton(Image i, CbButtonCallback cb)
26 this(i, null, LEFT, cb);
29 public CbButton(String s, CbButtonCallback cb)
31 this(null, s, LEFT, cb);
34 public CbButton(Image i, String s, int im, CbButtonCallback cb)
41 iwidth = Util.getWidth(image);
42 iheight = Util.getHeight(image);
45 twidth = Util.fnm.stringWidth(string);
46 theight = Util.fnm.getHeight();
48 if (image != null && string != null) {
52 pwidth = iwidth + twidth + 6;
53 pheight = Math.max(iheight , theight) + 4;
57 pwidth = Math.max(iwidth, twidth) + 4;
58 pheight = iheight + theight + 6;
62 else if (image != null) {
64 pheight = iheight + 4;
66 else if (string != null) {
68 pheight = theight + 8;
72 /**Make this button part of a mutual-exclusion group. Only one such
73 * button can be indented at a time
75 public void setGroup(CbButtonGroup g)
81 /**Make this button the selected one in it's group
89 /**Display the given string
91 public void setText(String s)
95 twidth = Util.fnm.stringWidth(string);
96 theight = Util.fnm.getHeight();
100 /**Display the given image
102 public void setImage(Image i)
106 iwidth = Util.getWidth(image);
107 iheight = Util.getHeight(image);
111 /**Display the given image and text, with the given alignment mode
113 public void setImageText(Image i, String s, int m)
118 twidth = Util.fnm.stringWidth(string);
119 theight = Util.fnm.getHeight();
120 iwidth = Util.getWidth(image);
121 iheight = Util.getHeight(image);
125 public void paint(Graphics g)
127 Color c1 = inside ? hc1 : lc1,
128 c2 = inside ? hc2 : lc2,
129 c3 = inside ? hc3 : lc3;
130 int w = size().width, h = size().height;
131 Color hi = indent||selected ? c3 : c1,
132 lo = indent||selected ? c1 : c3;
134 g.fillRect(0, 0, w-1, h-1);
136 g.drawLine(0, 0, w-2, 0);
137 g.drawLine(0, 0, 0, h-2);
139 g.drawLine(w-1, h-1, w-1, 1);
140 g.drawLine(w-1, h-1, 1, h-1);
143 g.drawLine(1, 1, w-3, 1);
144 g.drawLine(1, 1, 1, h-3); */
146 g.drawLine(w-2, h-2, w-2, 2);
147 g.drawLine(w-2, h-2, 2, h-2);
152 if (image != null && string != null) {
154 Dimension is = imgSize(w-twidth-6, h-4);
155 g.drawImage(image, (w - is.width - twidth - 2)/2,
156 (h-is.height)/2, is.width, is.height, this);
158 (w - is.width - twidth - 2)/2 +is.width +2,
159 (h + theight - Util.fnm.getDescent())/2);
161 else if (imode == RIGHT) {
163 else if (imode == ABOVE) {
164 //Dimension is = imgSize(w-4, h-theight-6);
165 g.drawImage(image, (w - iwidth)/2,
166 (h - iheight - theight - 2)/2,
167 iwidth, iheight, this);
168 g.drawString(string, (w - twidth)/2, iheight+Util.fnm.getHeight()+2);
170 else if (imode == BELOW) {
173 else if (image != null) {
174 Dimension is = imgSize(w-4, h-4);
175 g.drawImage(image, (w - is.width)/2, (h-is.height)/2,
176 is.width, is.height, this);
178 else if (string != null) {
179 g.drawString(string, (w - twidth)/2,
180 (h+theight-Util.fnm.getDescent())/2);
184 public void update(Graphics g) { paint(g); }
186 public boolean mouseEnter(Event e, int x, int y)
193 public boolean mouseExit(Event e, int x, int y)
200 public boolean mouseDown(Event e, int x, int y)
207 public boolean mouseUp(Event e, int x, int y)
209 if (x >= 0 && y >= 0 && x < size().width && y < size().height) {
210 if (callback != null)
211 callback.click(this);
219 public Dimension preferredSize()
221 return new Dimension(pwidth, pheight);
224 public Dimension minimumSize()
226 return preferredSize();
229 private Dimension imgSize(int mw, int mh)
231 float ws = (float)mw/(float)iwidth,
232 hs = (float)mh/(float)iheight;
233 float s = ws < hs ? ws : hs;
235 return new Dimension((int)(iwidth*s), (int)(iheight*s));
240 interface CbButtonCallback
242 void click(CbButton b);
248 Vector buttons = new Vector();
252 buttons.addElement(b);
255 void select(CbButton b)
257 for(int i=0; i<buttons.size(); i++) {
258 CbButton but = (CbButton)buttons.elementAt(i);
259 but.selected = (b == but);