Mercurial > projects > dwt-mac
comparison dwt/widgets/Scale.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 649b8e223d5a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 *******************************************************************************/ | |
11 module dwt.widgets.Scale; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.DWTException; | |
18 import dwt.events.SelectionListener; | |
19 import dwt.graphics.Point; | |
20 import dwt.internal.cocoa.NSRect; | |
21 import dwt.internal.cocoa.NSSlider; | |
22 import dwt.internal.cocoa.SWTSlider; | |
23 | |
24 /** | |
25 * Instances of the receiver represent a selectable user | |
26 * interface object that present a range of continuous | |
27 * numeric values. | |
28 * <dl> | |
29 * <dt><b>Styles:</b></dt> | |
30 * <dd>HORIZONTAL, VERTICAL</dd> | |
31 * <dt><b>Events:</b></dt> | |
32 * <dd>Selection</dd> | |
33 * </dl> | |
34 * <p> | |
35 * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified. | |
36 * </p><p> | |
37 * <p> | |
38 * IMPORTANT: This class is intended to be subclassed <em>only</em> | |
39 * within the DWT implementation. | |
40 * </p> | |
41 */ | |
42 public class Scale extends Control { | |
43 int increment = 1; | |
44 int pageIncrement = 10; | |
45 | |
46 /** | |
47 * Constructs a new instance of this class given its parent | |
48 * and a style value describing its behavior and appearance. | |
49 * <p> | |
50 * The style value is either one of the style constants defined in | |
51 * class <code>DWT</code> which is applicable to instances of this | |
52 * class, or must be built by <em>bitwise OR</em>'ing together | |
53 * (that is, using the <code>int</code> "|" operator) two or more | |
54 * of those <code>DWT</code> style constants. The class description | |
55 * lists the style constants that are applicable to the class. | |
56 * Style bits are also inherited from superclasses. | |
57 * </p> | |
58 * | |
59 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
60 * @param style the style of control to construct | |
61 * | |
62 * @exception IllegalArgumentException <ul> | |
63 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
64 * </ul> | |
65 * @exception DWTException <ul> | |
66 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
67 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
68 * </ul> | |
69 * | |
70 * @see DWT#HORIZONTAL | |
71 * @see DWT#VERTICAL | |
72 * @see Widget#checkSubclass | |
73 * @see Widget#getStyle | |
74 */ | |
75 public Scale (Composite parent, int style) { | |
76 super (parent, checkStyle (style)); | |
77 } | |
78 | |
79 /** | |
80 * Adds the listener to the collection of listeners who will | |
81 * be notified when the user changes the receiver's value, by sending | |
82 * it one of the messages defined in the <code>SelectionListener</code> | |
83 * interface. | |
84 * <p> | |
85 * <code>widgetSelected</code> is called when the user changes the receiver's value. | |
86 * <code>widgetDefaultSelected</code> is not called. | |
87 * </p> | |
88 * | |
89 * @param listener the listener which should be notified | |
90 * | |
91 * @exception IllegalArgumentException <ul> | |
92 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
93 * </ul> | |
94 * @exception DWTException <ul> | |
95 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
96 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
97 * </ul> | |
98 * | |
99 * @see SelectionListener | |
100 * @see #removeSelectionListener | |
101 */ | |
102 public void addSelectionListener(SelectionListener listener) { | |
103 checkWidget(); | |
104 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); | |
105 TypedListener typedListener = new TypedListener(listener); | |
106 addListener(DWT.Selection,typedListener); | |
107 addListener(DWT.DefaultSelection,typedListener); | |
108 } | |
109 | |
110 static int checkStyle (int style) { | |
111 return checkBits (style, DWT.HORIZONTAL, DWT.VERTICAL, 0, 0, 0, 0); | |
112 } | |
113 | |
114 public Point computeSize (int wHint, int hHint, bool changed) { | |
115 checkWidget(); | |
116 NSSlider widget = (NSSlider)view; | |
117 NSRect oldRect = widget.frame(); | |
118 widget.sizeToFit(); | |
119 NSRect newRect = widget.frame(); | |
120 widget.setFrame (oldRect); | |
121 int width = 0, height = 0; | |
122 if ((style & DWT.HORIZONTAL) !is 0) { | |
123 height = (int)newRect.width; | |
124 width = height * 10; | |
125 } else { | |
126 width = (int)newRect.width; | |
127 height = width * 10; | |
128 } | |
129 if (wHint !is DWT.DEFAULT) width = wHint; | |
130 if (hHint !is DWT.DEFAULT) height = hHint; | |
131 return new Point (width, height); | |
132 } | |
133 | |
134 void createHandle () { | |
135 NSSlider widget = (NSSlider)new SWTSlider().alloc(); | |
136 NSRect rect = new NSRect(); | |
137 if ((style & DWT.HORIZONTAL) !is 0) { | |
138 rect.width = 1; | |
139 } else { | |
140 rect.height = 1; | |
141 } | |
142 widget.initWithFrame(rect); | |
143 widget.setMaxValue(100); | |
144 widget.setTag(jniRef); | |
145 view = widget; | |
146 parent.contentView().addSubview_(widget); | |
147 } | |
148 | |
149 /** | |
150 * Returns the amount that the receiver's value will be | |
151 * modified by when the up/down (or right/left) arrows | |
152 * are pressed. | |
153 * | |
154 * @return the increment | |
155 * | |
156 * @exception DWTException <ul> | |
157 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
158 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
159 * </ul> | |
160 */ | |
161 public int getIncrement () { | |
162 checkWidget(); | |
163 return increment; | |
164 } | |
165 | |
166 /** | |
167 * Returns the maximum value which the receiver will allow. | |
168 * | |
169 * @return the maximum | |
170 * | |
171 * @exception DWTException <ul> | |
172 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
173 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
174 * </ul> | |
175 */ | |
176 public int getMaximum () { | |
177 checkWidget(); | |
178 return (int)((NSSlider)view).maxValue(); | |
179 } | |
180 | |
181 /** | |
182 * Returns the minimum value which the receiver will allow. | |
183 * | |
184 * @return the minimum | |
185 * | |
186 * @exception DWTException <ul> | |
187 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
188 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
189 * </ul> | |
190 */ | |
191 public int getMinimum () { | |
192 checkWidget(); | |
193 return (int)((NSSlider)view).minValue(); | |
194 } | |
195 | |
196 /** | |
197 * Returns the amount that the receiver's value will be | |
198 * modified by when the page increment/decrement areas | |
199 * are selected. | |
200 * | |
201 * @return the page increment | |
202 * | |
203 * @exception DWTException <ul> | |
204 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
205 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
206 * </ul> | |
207 */ | |
208 public int getPageIncrement () { | |
209 checkWidget(); | |
210 return pageIncrement; | |
211 } | |
212 | |
213 /** | |
214 * Returns the 'selection', which is the receiver's position. | |
215 * | |
216 * @return the selection | |
217 * | |
218 * @exception DWTException <ul> | |
219 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
220 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
221 * </ul> | |
222 */ | |
223 public int getSelection () { | |
224 checkWidget(); | |
225 // int value = OS.GetControl32BitValue (handle); | |
226 // if ((style & DWT.VERTICAL) !is 0) { | |
227 // int minimum = OS.GetControl32BitMinimum (handle); | |
228 // int maximum = OS.GetControl32BitMaximum (handle); | |
229 // value = maximum - value + minimum; | |
230 // } | |
231 return (int)((NSSlider)view).doubleValue(); | |
232 } | |
233 | |
234 /** | |
235 * Removes the listener from the collection of listeners who will | |
236 * be notified when the user changes the receiver's value. | |
237 * | |
238 * @param listener the listener which should no longer be notified | |
239 * | |
240 * @exception IllegalArgumentException <ul> | |
241 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
242 * </ul> | |
243 * @exception DWTException <ul> | |
244 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
245 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
246 * </ul> | |
247 * | |
248 * @see SelectionListener | |
249 * @see #addSelectionListener | |
250 */ | |
251 public void removeSelectionListener(SelectionListener listener) { | |
252 checkWidget(); | |
253 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); | |
254 if (eventTable is null) return; | |
255 eventTable.unhook(DWT.Selection, listener); | |
256 eventTable.unhook(DWT.DefaultSelection,listener); | |
257 } | |
258 | |
259 /** | |
260 * Sets the amount that the receiver's value will be | |
261 * modified by when the up/down (or right/left) arrows | |
262 * are pressed to the argument, which must be at least | |
263 * one. | |
264 * | |
265 * @param increment the new increment (must be greater than zero) | |
266 * | |
267 * @exception DWTException <ul> | |
268 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
269 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
270 * </ul> | |
271 */ | |
272 public void setIncrement (int value) { | |
273 checkWidget(); | |
274 if (value < 1) return; | |
275 increment = value; | |
276 } | |
277 | |
278 /** | |
279 * Sets the maximum value that the receiver will allow. This new | |
280 * value will be ignored if it is not greater than the receiver's current | |
281 * minimum value. If the new maximum is applied then the receiver's | |
282 * selection value will be adjusted if necessary to fall within its new range. | |
283 * | |
284 * @param value the new maximum, which must be greater than the current minimum | |
285 * | |
286 * @exception DWTException <ul> | |
287 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
288 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
289 * </ul> | |
290 */ | |
291 public void setMaximum (int value) { | |
292 checkWidget(); | |
293 if (value < 0) return; | |
294 ((NSSlider)view).setMaxValue(value); | |
295 } | |
296 | |
297 /** | |
298 * Sets the minimum value that the receiver will allow. This new | |
299 * value will be ignored if it is negative or is not less than the receiver's | |
300 * current maximum value. If the new minimum is applied then the receiver's | |
301 * selection value will be adjusted if necessary to fall within its new range. | |
302 * | |
303 * @param value the new minimum, which must be nonnegative and less than the current maximum | |
304 * | |
305 * @exception DWTException <ul> | |
306 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
307 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
308 * </ul> | |
309 */ | |
310 public void setMinimum (int value) { | |
311 checkWidget(); | |
312 if (value < 0) return; | |
313 ((NSSlider)view).setMinValue(value); | |
314 } | |
315 | |
316 /** | |
317 * Sets the amount that the receiver's value will be | |
318 * modified by when the page increment/decrement areas | |
319 * are selected to the argument, which must be at least | |
320 * one. | |
321 * | |
322 * @param pageIncrement the page increment (must be greater than zero) | |
323 * | |
324 * @exception DWTException <ul> | |
325 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
326 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
327 * </ul> | |
328 */ | |
329 public void setPageIncrement (int value) { | |
330 checkWidget(); | |
331 if (value < 1) return; | |
332 pageIncrement = value; | |
333 } | |
334 | |
335 /** | |
336 * Sets the 'selection', which is the receiver's value, | |
337 * to the argument which must be greater than or equal to zero. | |
338 * | |
339 * @param value the new selection (must be zero or greater) | |
340 * | |
341 * @exception DWTException <ul> | |
342 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
343 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
344 * </ul> | |
345 */ | |
346 public void setSelection (int value) { | |
347 checkWidget(); | |
348 ((NSSlider)view).setDoubleValue(value); | |
349 } | |
350 | |
351 } |