Handle hostnames with upper-case letters
[webmin.git] / file / CbButton.java
1 import java.awt.*;
2 import java.util.*;
3
4 public class CbButton extends Canvas
5 {
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;
10
11         Image image;
12         String string;
13         CbButtonCallback callback;
14         int imode;
15         int iwidth, iheight, pwidth, pheight, twidth, theight;
16         boolean inside, indent;
17
18         CbButtonGroup group;
19         boolean selected;
20
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;
23
24         public CbButton(Image i, CbButtonCallback cb)
25         {
26         this(i, null, LEFT, cb);
27         }
28
29         public CbButton(String s, CbButtonCallback cb)
30         {
31         this(null, s, LEFT, cb);
32         }
33
34         public CbButton(Image i, String s, int im, CbButtonCallback cb)
35         {
36         image = i;
37         string = s;
38         imode = im;
39         callback = cb;
40         if (image != null) {
41                 iwidth = Util.getWidth(image);
42                 iheight = Util.getHeight(image);
43                 }
44         if (string != null) {
45                 twidth = Util.fnm.stringWidth(string);
46                 theight = Util.fnm.getHeight();
47                 }
48         if (image != null && string != null) {
49                 switch(imode) {
50                 case LEFT:
51                 case RIGHT:
52                         pwidth = iwidth + twidth + 6;
53                         pheight = Math.max(iheight , theight) + 4;
54                         break;
55                 case ABOVE:
56                 case BELOW:
57                         pwidth = Math.max(iwidth, twidth) + 4;
58                         pheight = iheight + theight + 6;
59                         break;
60                         }
61                 }
62         else if (image != null) {
63                 pwidth = iwidth + 4;
64                 pheight = iheight + 4;
65                 }
66         else if (string != null) {
67                 pwidth = twidth + 8;
68                 pheight = theight + 8;
69                 }
70         }
71
72         /**Make this button part of a mutual-exclusion group. Only one such
73          * button can be indented at a time
74          */
75         public void setGroup(CbButtonGroup g)
76         {
77         group = g;
78         group.add(this);
79         }
80
81         /**Make this button the selected one in it's group
82          */
83         public void select()
84         {
85         if (group != null)
86                 group.select(this);
87         }
88
89         /**Display the given string
90          */
91         public void setText(String s)
92         {
93         string = s;
94         image = null;
95         twidth = Util.fnm.stringWidth(string);
96         theight = Util.fnm.getHeight();
97         repaint();
98         }
99
100         /**Display the given image
101          */
102         public void setImage(Image i)
103         {
104         string = null;
105         image = i;
106         iwidth = Util.getWidth(image);
107         iheight = Util.getHeight(image);
108         repaint();
109         }
110
111         /**Display the given image and text, with the given alignment mode
112          */
113         public void setImageText(Image i, String s, int m)
114         {
115         image = i;
116         string = s;
117         imode = m;
118         twidth = Util.fnm.stringWidth(string);
119         theight = Util.fnm.getHeight();
120         iwidth = Util.getWidth(image);
121         iheight = Util.getHeight(image);
122         repaint();
123         }
124
125         public void paint(Graphics g)
126         {
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;
133         g.setColor(c2);
134         g.fillRect(0, 0, w-1, h-1);
135         g.setColor(hi);
136         g.drawLine(0, 0, w-2, 0);
137         g.drawLine(0, 0, 0, h-2);
138         g.setColor(lo);
139         g.drawLine(w-1, h-1, w-1, 1);
140         g.drawLine(w-1, h-1, 1, h-1);
141         if (inside) {
142                 /* g.setColor(hi);
143                 g.drawLine(1, 1, w-3, 1);
144                 g.drawLine(1, 1, 1, h-3); */
145                 g.setColor(lo);
146                 g.drawLine(w-2, h-2, w-2, 2);
147                 g.drawLine(w-2, h-2, 2, h-2);
148                 }
149
150         g.setColor(c3);
151         g.setFont(Util.f);
152         if (image != null && string != null) {
153                 if (imode == LEFT) {
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);
157                         g.drawString(string,
158                                      (w - is.width - twidth - 2)/2 +is.width +2,
159                                      (h + theight - Util.fnm.getDescent())/2);
160                         }
161                 else if (imode == RIGHT) {
162                         }
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);
169                         }
170                 else if (imode == BELOW) {
171                         }
172                 }
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);
177                 }
178         else if (string != null) {
179                 g.drawString(string, (w - twidth)/2,
180                                      (h+theight-Util.fnm.getDescent())/2);
181                 }
182         }
183
184         public void update(Graphics g) { paint(g); }
185
186         public boolean mouseEnter(Event e, int x, int y)
187         {
188         inside = true;
189         repaint();
190         return true;
191         }
192
193         public boolean mouseExit(Event e, int x, int y)
194         {
195         inside = false;
196         repaint();
197         return true;
198         }
199
200         public boolean mouseDown(Event e, int x, int y)
201         {
202         indent = true;
203         repaint();
204         return true;
205         }
206
207         public boolean mouseUp(Event e, int x, int y)
208         {
209         if (x >= 0 && y >= 0 && x < size().width && y < size().height) {
210                 if (callback != null)
211                         callback.click(this);
212                 select();
213                 }
214         indent = false;
215         repaint();
216         return true;
217         }
218
219         public Dimension preferredSize()
220         {
221         return new Dimension(pwidth, pheight);
222         }
223
224         public Dimension minimumSize()
225         {
226         return preferredSize();
227         }
228
229         private Dimension imgSize(int mw, int mh)
230         {
231         float ws = (float)mw/(float)iwidth,
232               hs = (float)mh/(float)iheight;
233         float s = ws < hs ? ws : hs;
234         if (s > 1) s = 1;
235         return new Dimension((int)(iwidth*s), (int)(iheight*s));
236         }
237 }
238
239
240 interface CbButtonCallback
241 {
242         void click(CbButton b);
243 }
244
245
246 class CbButtonGroup
247 {
248         Vector buttons = new Vector();
249
250         void add(CbButton b)
251         {
252         buttons.addElement(b);
253         }
254
255         void select(CbButton b)
256         {
257         for(int i=0; i<buttons.size(); i++) {
258                 CbButton but = (CbButton)buttons.elementAt(i);
259                 but.selected = (b == but);
260                 but.repaint();
261                 }
262         }
263 }
264