Handle hostnames with upper-case letters
[webmin.git] / file / CbSlider.java
1 import java.awt.*; 
2
3 class CbSlider extends Canvas
4 {
5         int dir, min, max, pos;
6         CbSliderCallback callback;
7         int px, py;
8         Color lc1 = Util.light_edge, lc2 = Util.body, lc3 = Util.dark_edge;
9         Color hc1 = Util.light_edge_hi, hc2 = Util.body_hi, hc3 = Util.dark_edge_hi;
10         int ticks = 0;
11         boolean inside = false, dragging = false;
12         int dragx;
13
14         /**Create a new slider
15          * @param d             0=horizontal, 1=vertical
16          * @param mi    Minimum value
17          * @param ma    Maximum value
18          * @param p             Current value
19          */
20         public CbSlider(int d, int mi, int ma, int p)
21         {
22         this(d, mi, ma, p, null);
23         }
24
25         /**Create a new slider
26          * @param d             0=horizontal, 1=vertical
27          * @param mi    Minimum value
28          * @param ma    Maximum value
29          * @param p             Current value
30          * @param cb    Object to call back to
31          */
32         public CbSlider(int d, int mi, int ma, int p, CbSliderCallback cb)
33         {
34         dir = d; min = mi; max = ma;
35         pos = p;
36         callback = cb;
37         }
38
39         /**Toggle drawing of tick-marks on the slider track
40          * @param t             The number of units/tick, or 0 to disable
41          */
42         public void setTicks(int t)
43         {
44         ticks = t;
45         repaint();
46         }
47
48         /**Returns the current slider position
49          */
50         public int getPosition() { return pos; }
51
52         /**Sets the current slider position
53          */
54         public void setPosition(int p)
55         {
56         if (pos != p) {
57                 pos = p;
58                 repaint();
59                 }
60         }
61
62         /**Returns the current minimum slider value
63          */
64         public int getMinimum() { return min; }
65
66         /**Sets the minimum slider value
67          * @param mi    The new minimum
68          */
69         public void setMinimum(int mi)
70         {
71         min = mi;
72         checkPos();
73         repaint();
74         }
75
76         /**Returns the current maximum slider value
77          */
78         public int getMaximum() { return max; }
79
80         /**Sets the maximum slider value
81          * @param mx    The new maximum
82          */
83         public void setMaximum(int mx)
84         {
85         max = mx;
86         checkPos();
87         repaint();
88         }
89
90         public void paint(Graphics g)
91         {
92         Color c1 = inside ? hc1 : lc1,
93               c2 = inside ? hc2 : lc2,
94               c3 = inside ? hc3 : lc3;
95
96         // draw slider track
97         int w = size().width, h = size().height;
98         g.setColor(c2);
99         g.fillRect(0, 0, w, h);
100         g.setColor(c3);
101         g.drawLine(8, h/2, w-8, h/2);
102         g.setColor(c1);
103         g.drawLine(8, h/2+1, w-8, h/2+1);
104
105         // draw border
106         g.setColor(c1);
107         g.drawLine(0, 0, w-1, 0);
108         g.drawLine(0, 0, 0, h-1);
109         g.setColor(c3);
110         g.drawLine(w-1, h-1, w-1, 0);
111         g.drawLine(w-1, h-1, 0, h-1);
112         if (inside) {
113                 g.drawLine(w-2, h-2, w-2, 0);
114                 g.drawLine(w-2, h-2, 0, h-2);
115                 }
116
117         // draw tick marks
118         if (ticks != 0) {
119                 int mm = max-min;
120                 for(int i=0; i<=mm; i+=ticks) {
121                         int tx = ((w-16)*i / mm) + 8;
122                         g.setColor(c3);
123                         g.drawLine(tx, h/2, tx, h/2-6);
124                         }
125                 }
126
127         // draw slider
128         px = ((w-16)*pos / (max - min)) + 8;
129         py = h/2;
130         g.setColor(c2);
131         int xpt[] = { px-3, px-3, px, px+3, px+3 };
132         int ypt[] = { py+5, py-4, py-6, py-4, py+5 };
133         g.fillPolygon(xpt, ypt, 5);
134         g.setColor(dragging ? c3 : c1);
135         g.drawLine(px-3, py+5, px-3, py-4);
136         g.drawLine(px-3, py-4, px, py-6);
137         g.setColor(dragging ? c1 : c3);
138         g.drawLine(px-3, py+5, px+3, py+5);
139         g.drawLine(px+3, py+5, px+3, py-4);
140         }
141
142         public void update(Graphics g) { paint(g); }
143
144         public boolean mouseEnter(Event e, int x, int y)
145         {
146         inside = true;
147         repaint();
148         return true;
149         }
150
151         public boolean mouseDown(Event e, int x, int y)
152         {
153         int step = ticks==0 ? (max-min)/10 : ticks;
154         if (x < px-3) {
155                 // move one tick to the left
156                 pos -= step;
157                 }
158         else if (x > px+3) {
159                 // move one tick to the right
160                 pos += step;
161                 }
162         else {
163                 // start dragging
164                 dragging = true;
165                 dragx = x-px;
166                 }
167         checkPos();
168         if (callback != null)
169                 callback.moved(this, pos);
170         repaint();
171         return true;
172         }
173
174         public boolean mouseDrag(Event e, int x, int y)
175         {
176         if (dragging) {
177                 px = x-dragx;
178                 pos = (px-8)*(max - min) / (size().width-16);
179                 checkPos();
180                 if (callback != null)
181                         callback.moving(this, pos);
182                 repaint();
183                 }
184         return dragging;
185         }
186
187         public boolean mouseUp(Event e, int x, int y)
188         {
189         if (dragging) {
190                 dragging = false;
191                 if (callback != null)
192                         callback.moved(this, pos);
193                 repaint();
194                 return true;
195                 }
196         return false;
197         }
198
199         public boolean mouseExit(Event e, int x, int y)
200         {
201         inside = false;
202         repaint();
203         return true;
204         }
205
206         protected void checkPos()
207         {
208         if (pos < min) pos = min;
209         else if (pos > max) pos = max;
210         }
211
212         public Dimension preferredSize()
213         {
214         return new Dimension(100, 20);
215         }
216
217         public Dimension minimumSize() { return preferredSize(); }
218 }
219
220 interface CbSliderCallback
221 {
222         /**Callled back when the slider stops at a new position
223          * @param s             The slider being moved
224          * @param p             New position
225          */
226         public void moved(CbSlider s, int p);
227
228         /**Callled back whenever the slider is being dragged
229          * @param s             The slider being moved
230          * @param p             New position
231          */
232         public void moving(CbSlider s, int p);
233 }