Mercurial > projects > dwt-linux
annotate dwt/widgets/CoolItem.d @ 240:ce446666f5a2
Update to SWT 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 12 May 2008 19:13:01 +0200 |
parents | 17f8449522fd |
children | 5a30aa9820f3 |
rev | line source |
---|---|
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
1 /******************************************************************************* |
76 | 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 | |
108 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
76 | 12 *******************************************************************************/ |
13 module dwt.widgets.CoolItem; | |
14 | |
15 | |
16 | |
17 import dwt.DWT; | |
18 import dwt.DWTException; | |
19 import dwt.events.SelectionEvent; | |
20 import dwt.events.SelectionListener; | |
21 import dwt.graphics.Color; | |
22 import dwt.graphics.GC; | |
23 import dwt.graphics.Image; | |
24 import dwt.graphics.ImageData; | |
25 import dwt.graphics.PaletteData; | |
26 import dwt.graphics.Point; | |
27 import dwt.graphics.RGB; | |
28 import dwt.graphics.Rectangle; | |
29 import dwt.widgets.Item; | |
30 import dwt.widgets.Control; | |
31 import dwt.widgets.CoolBar; | |
32 import dwt.widgets.ToolBar; | |
33 import dwt.widgets.Event; | |
34 import dwt.widgets.TypedListener; | |
35 import dwt.widgets.ToolBar; | |
36 import dwt.widgets.ToolItem; | |
37 import dwt.widgets.Listener; | |
38 | |
39 /** | |
40 * Instances of this class are selectable user interface | |
41 * objects that represent the dynamically positionable | |
42 * areas of a <code>CoolBar</code>. | |
43 * <dl> | |
44 * <dt><b>Styles:</b></dt> | |
45 * <dd>DROP_DOWN</dd> | |
46 * <dt><b>Events:</b></dt> | |
47 * <dd>Selection</dd> | |
48 * </dl> | |
49 * <p> | |
50 * IMPORTANT: This class is <em>not</em> intended to be subclassed. | |
51 * </p> | |
52 */ | |
53 public class CoolItem : Item { | |
54 Control control; | |
55 CoolBar parent; | |
56 bool ideal; | |
57 int preferredWidth, preferredHeight, minimumWidth, minimumHeight, requestedWidth; | |
58 Rectangle itemBounds; | |
59 | |
60 static const int MARGIN_WIDTH = 4; | |
61 static const int GRABBER_WIDTH = 2; | |
62 static const int MINIMUM_WIDTH = (2 * MARGIN_WIDTH) + GRABBER_WIDTH; | |
63 | |
64 private int CHEVRON_HORIZONTAL_TRIM = -1; //platform dependent values | |
65 private int CHEVRON_VERTICAL_TRIM = -1; | |
66 private static const int CHEVRON_LEFT_MARGIN = 2; | |
67 private static const int CHEVRON_IMAGE_WIDTH = 8; //Width to draw the double arrow | |
68 | |
69 ToolBar chevron; | |
70 bool wrap; | |
71 Image arrowImage = null; | |
72 | |
73 /** | |
74 * Constructs a new instance of this class given its parent | |
75 * (which must be a <code>CoolBar</code>) and a style value | |
76 * describing its behavior and appearance. The item is added | |
77 * to the end of the items maintained by its parent. | |
78 * <p> | |
79 * The style value is either one of the style constants defined in | |
80 * class <code>DWT</code> which is applicable to instances of this | |
81 * class, or must be built by <em>bitwise OR</em>'ing together | |
82 * (that is, using the <code>int</code> "|" operator) two or more | |
83 * of those <code>DWT</code> style constants. The class description | |
84 * lists the style constants that are applicable to the class. | |
85 * Style bits are also inherited from superclasses. | |
86 * </p> | |
87 * | |
88 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
89 * @param style the style of control to construct | |
90 * | |
91 * @exception IllegalArgumentException <ul> | |
92 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
93 * </ul> | |
94 * @exception DWTException <ul> | |
95 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
96 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
97 * </ul> | |
98 * | |
99 * @see DWT#DROP_DOWN | |
100 * @see Widget#checkSubclass | |
101 * @see Widget#getStyle | |
102 */ | |
103 public this (CoolBar parent, int style) { | |
104 super(parent, style); | |
105 itemBounds = new Rectangle(0, 0, 0, 0); | |
106 this.parent = parent; | |
107 parent.createItem (this, parent.getItemCount()); | |
108 calculateChevronTrim (); | |
109 } | |
110 /** | |
111 * Constructs a new instance of this class given its parent | |
112 * (which must be a <code>CoolBar</code>), a style value | |
113 * describing its behavior and appearance, and the index | |
114 * at which to place it in the items maintained by its parent. | |
115 * <p> | |
116 * The style value is either one of the style constants defined in | |
117 * class <code>DWT</code> which is applicable to instances of this | |
118 * class, or must be built by <em>bitwise OR</em>'ing together | |
119 * (that is, using the <code>int</code> "|" operator) two or more | |
120 * of those <code>DWT</code> style constants. The class description | |
121 * lists the style constants that are applicable to the class. | |
122 * Style bits are also inherited from superclasses. | |
123 * </p> | |
124 * | |
125 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
126 * @param style the style of control to construct | |
127 * @param index the zero-relative index at which to store the receiver in its parent | |
128 * | |
129 * @exception IllegalArgumentException <ul> | |
130 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
131 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the parent (inclusive)</li> | |
132 * </ul> | |
133 * @exception DWTException <ul> | |
134 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
135 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
136 * </ul> | |
137 * | |
138 * @see DWT#DROP_DOWN | |
139 * @see Widget#checkSubclass | |
140 * @see Widget#getStyle | |
141 */ | |
142 public this (CoolBar parent, int style, int index) { | |
143 super(parent, style); | |
144 itemBounds = new Rectangle(0, 0, 0, 0); | |
145 this.parent = parent; | |
146 parent.createItem (this, index); | |
147 calculateChevronTrim (); | |
148 } | |
149 /** | |
150 * Adds the listener to the collection of listeners that will | |
151 * be notified when the control is selected by the user, by sending it one | |
152 * of the messages defined in the <code>SelectionListener</code> | |
153 * interface. | |
154 * <p> | |
155 * If <code>widgetSelected</code> is called when the mouse is over | |
156 * the drop-down arrow (or 'chevron') portion of the cool item, | |
157 * the event object detail field contains the value <code>DWT.ARROW</code>, | |
158 * and the x and y fields in the event object represent the point at | |
159 * the bottom left of the chevron, where the menu should be popped up. | |
160 * <code>widgetDefaultSelected</code> is not called. | |
161 * </p> | |
162 * | |
163 * @param listener the listener which should be notified when the control is selected by the user | |
164 * | |
165 * @exception IllegalArgumentException <ul> | |
166 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
167 * </ul> | |
168 * @exception DWTException <ul> | |
169 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
170 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
171 * </ul> | |
172 * | |
173 * @see SelectionListener | |
174 * @see #removeSelectionListener | |
175 * @see SelectionEvent | |
176 * | |
177 * @since 2.0 | |
178 */ | |
179 public void addSelectionListener(SelectionListener listener) { | |
180 checkWidget(); | |
181 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); | |
182 TypedListener typedListener = new TypedListener (listener); | |
183 addListener (DWT.Selection,typedListener); | |
184 addListener (DWT.DefaultSelection,typedListener); | |
185 } | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
186 protected override void checkSubclass () { |
76 | 187 if (!isValidSubclass ()) error (DWT.ERROR_INVALID_SUBCLASS); |
188 } | |
189 /* | |
190 * Find the trim size of the Toolbar widget in the current platform. | |
191 */ | |
192 void calculateChevronTrim () { | |
193 ToolBar tb = new ToolBar (parent, DWT.FLAT); | |
194 ToolItem ti = new ToolItem (tb, DWT.PUSH); | |
195 Image image = new Image (display, 1, 1); | |
196 ti.setImage (image); | |
197 Point size = tb.computeSize (DWT.DEFAULT, DWT.DEFAULT); | |
198 size = parent.fixPoint(size.x, size.y); | |
199 CHEVRON_HORIZONTAL_TRIM = size.x - 1; | |
200 CHEVRON_VERTICAL_TRIM = size.y - 1; | |
201 tb.dispose (); | |
202 ti.dispose (); | |
203 image.dispose (); | |
204 } | |
205 /** | |
206 * Returns the preferred size of the receiver. | |
207 * <p> | |
208 * The <em>preferred size</em> of a <code>CoolItem</code> is the size that | |
209 * it would best be displayed at. The width hint and height hint arguments | |
210 * allow the caller to ask the instance questions such as "Given a particular | |
211 * width, how high does it need to be to show all of the contents?" | |
212 * To indicate that the caller does not wish to constrain a particular | |
213 * dimension, the constant <code>DWT.DEFAULT</code> is passed for the hint. | |
214 * </p> | |
215 * | |
216 * @param wHint the width hint (can be <code>DWT.DEFAULT</code>) | |
217 * @param hHint the height hint (can be <code>DWT.DEFAULT</code>) | |
218 * @return the preferred size | |
219 * | |
220 * @exception DWTException <ul> | |
221 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
222 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
223 * </ul> | |
224 * | |
225 * @see Layout | |
226 * @see #getBounds | |
227 * @see #getSize | |
228 * @see Control#getBorderWidth | |
229 * @see Scrollable#computeTrim | |
230 * @see Scrollable#getClientArea | |
231 */ | |
232 public Point computeSize (int wHint, int hHint) { | |
233 checkWidget(); | |
234 int width = wHint, height = hHint; | |
235 if (wHint is DWT.DEFAULT) width = 32; | |
236 if (hHint is DWT.DEFAULT) height = 32; | |
237 if ((parent.style & DWT.VERTICAL) !is 0) { | |
238 height += MINIMUM_WIDTH; | |
239 } else { | |
240 width += MINIMUM_WIDTH; | |
241 } | |
242 return new Point (width, height); | |
243 } | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
244 public override void dispose () { |
76 | 245 if (isDisposed()) return; |
246 | |
247 /* | |
248 * Must call parent.destroyItem() before super.dispose(), since it needs to | |
249 * query the bounds to properly remove the item. | |
250 */ | |
251 parent.destroyItem(this); | |
252 super.dispose (); | |
253 parent = null; | |
254 control = null; | |
255 | |
256 /* | |
257 * Although the parent for the chevron is the CoolBar (CoolItem can not be the parent) | |
258 * it has to be disposed with the item | |
259 */ | |
260 if (chevron !is null && !chevron.isDisposed()) chevron.dispose(); | |
261 chevron = null; | |
262 if (arrowImage !is null && !arrowImage.isDisposed()) arrowImage.dispose(); | |
263 arrowImage = null; | |
264 } | |
265 | |
266 Image createArrowImage (int width, int height) { | |
267 Point point = parent.fixPoint(width, height); | |
268 width = point.x; | |
269 height = point.y; | |
270 Color foreground = parent.getForeground (); | |
271 Color black = display.getSystemColor (DWT.COLOR_BLACK); | |
272 Color background = parent.getBackground (); | |
273 | |
274 PaletteData palette = new PaletteData ( [foreground.getRGB(), background.getRGB(), black.getRGB() ] ); | |
275 ImageData imageData = new ImageData (width, height, 4, palette); | |
276 imageData.transparentPixel = 1; | |
277 Image image = new Image (display, imageData); | |
278 | |
240 | 279 GC gc = new GC (image, parent.getStyle() & DWT.RIGHT_TO_LEFT); |
76 | 280 gc.setBackground (background); |
281 gc.fillRectangle (0, 0, width, height); | |
282 gc.setForeground (black); | |
283 | |
284 int startX = 0 ; | |
285 if ((parent.style & DWT.VERTICAL) !is 0) { | |
286 startX = width - CHEVRON_IMAGE_WIDTH; | |
287 } | |
288 int startY = height / 6; | |
289 int step = 2; | |
290 gc.drawLine (startX, startY, startX + step, startY + step); | |
291 gc.drawLine (startX, startY + (2 * step), startX + step, startY + step); | |
292 startX++; | |
293 gc.drawLine (startX, startY, startX + step, startY + step); | |
294 gc.drawLine (startX, startY + (2 * step), startX + step, startY + step); | |
295 startX += 3; | |
296 gc.drawLine (startX, startY, startX + step, startY + step); | |
297 gc.drawLine (startX, startY + (2 * step), startX + step, startY + step); | |
298 startX++; | |
299 gc.drawLine (startX, startY, startX + step, startY + step); | |
300 gc.drawLine (startX, startY + (2 * step), startX + step, startY + step); | |
301 gc.dispose (); | |
302 return image; | |
303 } | |
304 /** | |
305 * Returns a rectangle describing the receiver's size and location | |
306 * relative to its parent. | |
307 * | |
308 * @return the receiver's bounding rectangle | |
309 * | |
310 * @exception DWTException <ul> | |
311 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
312 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
313 * </ul> | |
314 */ | |
315 public Rectangle getBounds () { | |
316 checkWidget(); | |
317 return parent.fixRectangle(itemBounds.x, itemBounds.y, itemBounds.width, itemBounds.height); | |
318 } | |
319 Rectangle internalGetBounds () { | |
320 return new Rectangle(itemBounds.x, itemBounds.y, itemBounds.width, itemBounds.height); | |
321 } | |
322 /** | |
323 * Returns the control that is associated with the receiver. | |
324 * | |
325 * @return the control that is contained by the receiver | |
326 * | |
327 * @exception DWTException <ul> | |
328 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
329 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
330 * </ul> | |
331 */ | |
332 public Control getControl () { | |
333 checkWidget(); | |
334 return control; | |
335 } | |
336 /** | |
337 * Returns the minimum size that the cool item can | |
338 * be resized to using the cool item's gripper. | |
339 * | |
340 * @return a point containing the minimum width and height of the cool item, in pixels | |
341 * | |
342 * @exception DWTException <ul> | |
343 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
344 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
345 * </ul> | |
346 * | |
347 * @since 2.0 | |
348 */ | |
349 public Point getMinimumSize () { | |
350 checkWidget(); | |
351 return parent.fixPoint(minimumWidth, minimumHeight); | |
352 } | |
353 /** | |
354 * Returns the receiver's parent, which must be a <code>CoolBar</code>. | |
355 * | |
356 * @return the receiver's parent | |
357 * | |
358 * @exception DWTException <ul> | |
359 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
360 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
361 * </ul> | |
362 */ | |
363 public CoolBar getParent () { | |
364 checkWidget(); | |
365 return parent; | |
366 } | |
367 /** | |
368 * Returns a point describing the receiver's ideal size. | |
369 * The x coordinate of the result is the ideal width of the receiver. | |
370 * The y coordinate of the result is the ideal height of the receiver. | |
371 * | |
372 * @return the receiver's ideal size | |
373 * | |
374 * @exception DWTException <ul> | |
375 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
376 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
377 * </ul> | |
378 */ | |
379 public Point getPreferredSize () { | |
380 checkWidget(); | |
381 return parent.fixPoint(preferredWidth, preferredHeight); | |
382 } | |
383 /** | |
384 * Returns a point describing the receiver's size. The | |
385 * x coordinate of the result is the width of the receiver. | |
386 * The y coordinate of the result is the height of the | |
387 * receiver. | |
388 * | |
389 * @return the receiver's size | |
390 * | |
391 * @exception DWTException <ul> | |
392 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
393 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
394 * </ul> | |
395 */ | |
396 public Point getSize () { | |
397 checkWidget(); | |
398 return parent.fixPoint(itemBounds.width, itemBounds.height); | |
399 } | |
400 int internalGetMinimumWidth () { | |
401 int width = minimumWidth + MINIMUM_WIDTH; | |
402 if ((style & DWT.DROP_DOWN) !is 0 && width < preferredWidth) { | |
403 width += CHEVRON_IMAGE_WIDTH + CHEVRON_HORIZONTAL_TRIM + CHEVRON_LEFT_MARGIN; | |
404 } | |
405 return width; | |
406 } | |
407 /* | |
408 * Called when the chevron is selected. | |
409 */ | |
410 void onSelection (Event ev) { | |
411 Rectangle bounds = chevron.getBounds(); | |
412 Event event = new Event(); | |
413 event.detail = DWT.ARROW; | |
414 if ((parent.style & DWT.VERTICAL) !is 0) { | |
415 event.x = bounds.x + bounds.width; | |
416 event.y = bounds.y; | |
417 } else { | |
418 event.x = bounds.x; | |
419 event.y = bounds.y + bounds.height; | |
420 } | |
421 postEvent (DWT.Selection, event); | |
422 } | |
423 /** | |
424 * Removes the listener from the collection of listeners that | |
425 * will be notified when the control is selected by the user. | |
426 * | |
427 * @param listener the listener which should no longer be notified | |
428 * | |
429 * @exception IllegalArgumentException <ul> | |
430 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
431 * </ul> | |
432 * @exception DWTException <ul> | |
433 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
434 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
435 * </ul> | |
436 * | |
437 * @see SelectionListener | |
438 * @see #addSelectionListener | |
439 * | |
440 * @since 2.0 | |
441 */ | |
442 public void removeSelectionListener(SelectionListener listener) { | |
443 checkWidget(); | |
444 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); | |
445 if (eventTable is null) return; | |
446 eventTable.unhook (DWT.Selection, listener); | |
447 eventTable.unhook (DWT.DefaultSelection,listener); | |
448 } | |
449 void setBounds (int x, int y, int width, int height) { | |
450 itemBounds.x = x; | |
451 itemBounds.y = y; | |
452 itemBounds.width = width; | |
453 itemBounds.height = height; | |
454 if (control !is null) { | |
455 int controlWidth = width - MINIMUM_WIDTH; | |
456 if ((style & DWT.DROP_DOWN) !is 0 && width < preferredWidth) { | |
457 controlWidth -= CHEVRON_IMAGE_WIDTH + CHEVRON_HORIZONTAL_TRIM + CHEVRON_LEFT_MARGIN; | |
458 } | |
459 control.setBounds (parent.fixRectangle(x + MINIMUM_WIDTH, y, controlWidth, height)); | |
460 } | |
461 updateChevron(); | |
462 } | |
463 /** | |
464 * Sets the control that is associated with the receiver | |
465 * to the argument. | |
466 * | |
467 * @param control the new control that will be contained by the receiver | |
468 * | |
469 * @exception IllegalArgumentException <ul> | |
470 * <li>ERROR_INVALID_ARGUMENT - if the control has been disposed</li> | |
471 * <li>ERROR_INVALID_PARENT - if the control is not in the same widget tree</li> | |
472 * </ul> | |
473 * @exception DWTException <ul> | |
474 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
475 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
476 * </ul> | |
477 */ | |
478 public void setControl (Control control) { | |
479 checkWidget(); | |
480 if (control !is null) { | |
481 if (control.isDisposed()) error (DWT.ERROR_INVALID_ARGUMENT); | |
482 if (control.parent !is parent) error (DWT.ERROR_INVALID_PARENT); | |
483 } | |
484 this.control = control; | |
485 if (control !is null) { | |
486 int controlWidth = itemBounds.width - MINIMUM_WIDTH; | |
487 if ((style & DWT.DROP_DOWN) !is 0 && itemBounds.width < preferredWidth) { | |
488 controlWidth -= CHEVRON_IMAGE_WIDTH + CHEVRON_HORIZONTAL_TRIM + CHEVRON_LEFT_MARGIN; | |
489 } | |
490 control.setBounds (parent.fixRectangle(itemBounds.x + MINIMUM_WIDTH, itemBounds.y, controlWidth, itemBounds.height)); | |
491 } | |
492 } | |
493 /** | |
494 * Sets the minimum size that the cool item can be resized to | |
495 * using the cool item's gripper, to the point specified by the arguments. | |
496 * | |
497 * @param width the minimum width of the cool item, in pixels | |
498 * @param height the minimum height of the cool item, in pixels | |
499 * | |
500 * @exception DWTException <ul> | |
501 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
502 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
503 * </ul> | |
504 * | |
505 * @since 2.0 | |
506 */ | |
507 public void setMinimumSize (int width, int height) { | |
508 checkWidget (); | |
509 Point point = parent.fixPoint(width, height); | |
510 minimumWidth = point.x; | |
511 minimumHeight = point.y; | |
512 } | |
513 /** | |
514 * Sets the minimum size that the cool item can be resized to | |
515 * using the cool item's gripper, to the point specified by the argument. | |
516 * | |
517 * @param size a point representing the minimum width and height of the cool item, in pixels | |
518 * | |
519 * @exception IllegalArgumentException <ul> | |
520 * <li>ERROR_NULL_ARGUMENT - if the point is null</li> | |
521 * </ul> | |
522 * @exception DWTException <ul> | |
523 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
524 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
525 * </ul> | |
526 * | |
527 * @since 2.0 | |
528 */ | |
529 public void setMinimumSize (Point size) { | |
530 checkWidget (); | |
531 if (size is null) error(DWT.ERROR_NULL_ARGUMENT); | |
532 setMinimumSize(size.x, size.y); | |
533 } | |
534 /** | |
535 * Sets the receiver's ideal size to the point specified by the arguments. | |
536 * | |
537 * @param width the new ideal width for the receiver | |
538 * @param height the new ideal height for the receiver | |
539 * | |
540 * @exception DWTException <ul> | |
541 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
542 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
543 * </ul> | |
544 */ | |
545 public void setPreferredSize (int width, int height) { | |
546 checkWidget(); | |
547 ideal = true; | |
548 Point point = parent.fixPoint(width, height); | |
549 preferredWidth = Math.max (point.x, MINIMUM_WIDTH); | |
550 preferredHeight = point.y; | |
551 } | |
552 /** | |
553 * Sets the receiver's ideal size to the point specified by the argument. | |
554 * | |
555 * @param size the new ideal size for the receiver | |
556 * | |
557 * @exception IllegalArgumentException <ul> | |
558 * <li>ERROR_NULL_ARGUMENT - if the point is null</li> | |
559 * </ul> | |
560 * @exception DWTException <ul> | |
561 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
562 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
563 * </ul> | |
564 */ | |
565 public void setPreferredSize (Point size) { | |
566 checkWidget(); | |
567 if (size is null) error(DWT.ERROR_NULL_ARGUMENT); | |
568 setPreferredSize(size.x, size.y); | |
569 } | |
570 /** | |
571 * Sets the receiver's size to the point specified by the arguments. | |
572 * <p> | |
573 * Note: Attempting to set the width or height of the | |
574 * receiver to a negative number will cause that | |
575 * value to be set to zero instead. | |
576 * </p> | |
577 * | |
578 * @param width the new width for the receiver | |
579 * @param height the new height for the receiver | |
580 * | |
581 * @exception DWTException <ul> | |
582 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
583 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
584 * </ul> | |
585 */ | |
586 public void setSize (int width, int height) { | |
587 checkWidget(); | |
588 Point point = parent.fixPoint(width, height); | |
589 width = Math.max(point.x, minimumWidth + MINIMUM_WIDTH); | |
590 height = point.y; | |
591 if (!ideal) { | |
592 preferredWidth = width; | |
593 preferredHeight = height; | |
594 } | |
595 itemBounds.width = requestedWidth = width; | |
596 itemBounds.height = height; | |
597 if (control !is null) { | |
598 int controlWidth = width - MINIMUM_WIDTH; | |
599 if ((style & DWT.DROP_DOWN) !is 0 && width < preferredWidth) { | |
600 controlWidth -= CHEVRON_IMAGE_WIDTH + CHEVRON_HORIZONTAL_TRIM + CHEVRON_LEFT_MARGIN; | |
601 } | |
602 control.setSize(parent.fixPoint(controlWidth, height)); | |
603 } | |
604 parent.relayout(); | |
605 updateChevron(); | |
606 } | |
607 /** | |
608 * Sets the receiver's size to the point specified by the argument. | |
609 * <p> | |
610 * Note: Attempting to set the width or height of the | |
611 * receiver to a negative number will cause them to be | |
612 * set to zero instead. | |
613 * </p> | |
614 * | |
615 * @param size the new size for the receiver | |
616 * | |
617 * @exception IllegalArgumentException <ul> | |
618 * <li>ERROR_NULL_ARGUMENT - if the point is null</li> | |
619 * </ul> | |
620 * @exception DWTException <ul> | |
621 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
622 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
623 * </ul> | |
624 */ | |
625 public void setSize (Point size) { | |
626 checkWidget(); | |
627 if (size is null) error (DWT.ERROR_NULL_ARGUMENT); | |
628 setSize (size.x, size.y); | |
629 } | |
630 void updateChevron() { | |
631 if (control !is null) { | |
632 int width = itemBounds.width; | |
633 if ((style & DWT.DROP_DOWN) !is 0 && width < preferredWidth) { | |
634 if (chevron is null) { | |
635 chevron = new ToolBar (parent, DWT.FLAT | DWT.NO_FOCUS); | |
636 ToolItem toolItem = new ToolItem (chevron, DWT.PUSH); | |
637 toolItem.addListener (DWT.Selection, new class () Listener { | |
638 public void handleEvent (Event event) { | |
639 this.outer.onSelection (event); | |
640 } | |
641 }); | |
642 } | |
643 int controlHeight, currentImageHeight = 0; | |
644 if ((parent.style & DWT.VERTICAL) !is 0) { | |
645 controlHeight = control.getSize ().x; | |
646 if (arrowImage !is null) currentImageHeight = arrowImage.getBounds().width; | |
647 } else { | |
648 controlHeight = control.getSize ().y; | |
649 if (arrowImage !is null) currentImageHeight = arrowImage.getBounds().height; | |
650 } | |
651 int height = Math.min (controlHeight, itemBounds.height); | |
652 int imageHeight = Math.max(1, height - CHEVRON_VERTICAL_TRIM); | |
653 if (currentImageHeight !is imageHeight) { | |
654 Image image = createArrowImage (CHEVRON_IMAGE_WIDTH, imageHeight); | |
655 chevron.getItem (0).setImage (image); | |
656 if (arrowImage !is null) arrowImage.dispose (); | |
657 arrowImage = image; | |
658 } | |
659 chevron.setBackground (parent.getBackground()); | |
660 chevron.setBounds (parent.fixRectangle ( | |
661 itemBounds.x + width - CHEVRON_LEFT_MARGIN - CHEVRON_IMAGE_WIDTH - CHEVRON_HORIZONTAL_TRIM, | |
662 itemBounds.y, | |
663 CHEVRON_IMAGE_WIDTH + CHEVRON_HORIZONTAL_TRIM, | |
664 height)); | |
665 chevron.setVisible(true); | |
666 } else { | |
667 if (chevron !is null) { | |
668 chevron.setVisible(false); | |
669 } | |
670 } | |
671 } | |
672 } | |
673 } |