comparison dwt/custom/CCombo.d @ 41:6337764516f1

Sync dwt/custom with dwt-linux (took copy of complete folder)
author Frank Benoit <benoit@tionex.de>
date Tue, 07 Oct 2008 16:29:55 +0200
parents f565d3a95c0a
children 63a09873578e
comparison
equal deleted inserted replaced
40:fbe68c33eeee 41:6337764516f1
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others. 2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 *
11 * Port to the D programming language: 10 * Port to the D programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com> 11 * Frank Benoit <benoit@tionex.de>
13 *******************************************************************************/ 12 *******************************************************************************/
14 module dwt.custom.CCombo; 13 module dwt.custom.CCombo;
14
15
15 16
16 import dwt.DWT; 17 import dwt.DWT;
17 import dwt.DWTException; 18 import dwt.DWTException;
18 import dwt.accessibility.ACC; 19 import dwt.accessibility.ACC;
19 import dwt.accessibility.AccessibleAdapter; 20 import dwt.accessibility.AccessibleAdapter;
44 import dwt.widgets.Shell; 45 import dwt.widgets.Shell;
45 import dwt.widgets.Text; 46 import dwt.widgets.Text;
46 import dwt.widgets.TypedListener; 47 import dwt.widgets.TypedListener;
47 import dwt.widgets.Widget; 48 import dwt.widgets.Widget;
48 49
50 static import tango.text.convert.Utf;
51 static import tango.text.Unicode;
52 static import tango.text.convert.Format;
49 import dwt.dwthelper.utils; 53 import dwt.dwthelper.utils;
54 import dwt.dwthelper.Runnable;
50 55
51 /** 56 /**
52 * The CCombo class represents a selectable user interface object 57 * The CCombo class represents a selectable user interface object
53 * that combines a text field and a list and issues notification 58 * that combines a text field and a list and issues notification
54 * when an item is selected from the list. 59 * when an item is selected from the list.
55 * <p> 60 * <p>
56 * CCombo was written to work around certain limitations in the native 61 * CCombo was written to work around certain limitations in the native
57 * combo box. Specifically, on win32, the height of a CCombo can be set; 62 * combo box. Specifically, on win32, the height of a CCombo can be set;
58 * attempts to set the height of a Combo are ignored. CCombo can be used 63 * attempts to set the height of a Combo are ignored. CCombo can be used
59 * anywhere that having the increased flexibility is more important than 64 * anywhere that having the increased flexibility is more important than
60 * getting native L&F, but the decision should not be taken lightly. 65 * getting native L&F, but the decision should not be taken lightly.
61 * There is no is no strict requirement that CCombo look or behave 66 * There is no is no strict requirement that CCombo look or behave
62 * the same as the native combo box. 67 * the same as the native combo box.
63 * </p> 68 * </p>
64 * <p> 69 * <p>
65 * Note that although this class is a subclass of <code>Composite</code>, 70 * Note that although this class is a subclass of <code>Composite</code>,
69 * <dt><b>Styles:</b> 74 * <dt><b>Styles:</b>
70 * <dd>BORDER, READ_ONLY, FLAT</dd> 75 * <dd>BORDER, READ_ONLY, FLAT</dd>
71 * <dt><b>Events:</b> 76 * <dt><b>Events:</b>
72 * <dd>DefaultSelection, Modify, Selection, Verify</dd> 77 * <dd>DefaultSelection, Modify, Selection, Verify</dd>
73 * </dl> 78 * </dl>
74 */ 79 *
75 public final class CCombo : Composite 80 * @see <a href="http://www.eclipse.org/swt/snippets/#ccombo">CCombo snippets</a>
76 { 81 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: CustomControlExample</a>
82 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
83 */
84 public final class CCombo : Composite {
85
86 alias Composite.computeSize computeSize;
77 87
78 Text text; 88 Text text;
79 List list; 89 List list;
80 int visibleItemCount = 5; 90 int visibleItemCount = 5;
81 Shell popup; 91 Shell popup;
83 bool hasFocus; 93 bool hasFocus;
84 Listener listener, filter; 94 Listener listener, filter;
85 Color foreground, background; 95 Color foreground, background;
86 Font font; 96 Font font;
87 97
88 /** 98 /**
89 * Constructs a new instance of this class given its parent 99 * Constructs a new instance of this class given its parent
90 * and a style value describing its behavior and appearance. 100 * and a style value describing its behavior and appearance.
91 * <p> 101 * <p>
92 * The style value is either one of the style constants defined in 102 * The style value is either one of the style constants defined in
93 * class <code>DWT</code> which is applicable to instances of this 103 * class <code>DWT</code> which is applicable to instances of this
94 * class, or must be built by <em>bitwise OR</em>'ing together 104 * class, or must be built by <em>bitwise OR</em>'ing together
95 * (that is, using the <code>int</code> "|" operator) two or more 105 * (that is, using the <code>int</code> "|" operator) two or more
96 * of those <code>DWT</code> style constants. The class description 106 * of those <code>DWT</code> style constants. The class description
97 * lists the style constants that are applicable to the class. 107 * lists the style constants that are applicable to the class.
98 * Style bits are also inherited from superclasses. 108 * Style bits are also inherited from superclasses.
99 * </p> 109 * </p>
100 * 110 *
101 * @param parent a widget which will be the parent of the new instance (cannot be null) 111 * @param parent a widget which will be the parent of the new instance (cannot be null)
102 * @param style the style of widget to construct 112 * @param style the style of widget to construct
103 * 113 *
104 * @exception IllegalArgumentException <ul> 114 * @exception IllegalArgumentException <ul>
105 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> 115 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
106 * </ul> 116 * </ul>
107 * @exception DWTException <ul> 117 * @exception DWTException <ul>
108 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> 118 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
109 * </ul> 119 * </ul>
110 * 120 *
111 * @see DWT#BORDER 121 * @see DWT#BORDER
112 * @see DWT#READ_ONLY 122 * @see DWT#READ_ONLY
113 * @see DWT#FLAT 123 * @see DWT#FLAT
114 * @see Widget#getStyle() 124 * @see Widget#getStyle()
115 */ 125 */
116 public this (Composite parent, int style) 126 public this (Composite parent, int style) {
117 { 127 super (parent, style = checkStyle (style));
118 super(parent, style = checkStyle(style)); 128
119 129 int textStyle = DWT.SINGLE;
120 int textStyle = DWT.SINGLE; 130 if ((style & DWT.READ_ONLY) !is 0) textStyle |= DWT.READ_ONLY;
121 if ((style & DWT.READ_ONLY) !is 0) 131 if ((style & DWT.FLAT) !is 0) textStyle |= DWT.FLAT;
122 textStyle |= DWT.READ_ONLY; 132 text = new Text (this, textStyle);
123 if ((style & DWT.FLAT) !is 0) 133 int arrowStyle = DWT.ARROW | DWT.DOWN;
124 textStyle |= DWT.FLAT; 134 if ((style & DWT.FLAT) !is 0) arrowStyle |= DWT.FLAT;
125 text = new Text(this, textStyle); 135 arrow = new Button (this, arrowStyle);
126 int arrowStyle = DWT.ARROW | DWT.DOWN; 136
127 if ((style & DWT.FLAT) !is 0) 137 listener = new class() Listener {
128 arrowStyle |= DWT.FLAT; 138 public void handleEvent (Event event) {
129 arrow = new Button(this, arrowStyle); 139 if (popup is event.widget) {
130 140 popupEvent (event);
131 listener = new class Listener 141 return;
132 { 142 }
133 public void handleEvent (Event event) 143 if (text is event.widget) {
134 { 144 textEvent (event);
135 if (popup is event.widget) 145 return;
136 { 146 }
137 popupEvent(event); 147 if (list is event.widget) {
138 return; 148 listEvent (event);
139 } 149 return;
140 if (text is event.widget) 150 }
141 { 151 if (arrow is event.widget) {
142 textEvent(event); 152 arrowEvent (event);
143 return; 153 return;
144 } 154 }
145 if (list is event.widget) 155 if (this.outer is event.widget) {
146 { 156 comboEvent (event);
147 listEvent(event); 157 return;
148 return; 158 }
149 } 159 if (getShell () is event.widget) {
150 if (arrow is event.widget) 160 getDisplay().asyncExec(new class() Runnable {
151 { 161 public void run() {
152 arrowEvent(event); 162 if (isDisposed()) return;
153 return; 163 handleFocus (DWT.FocusOut);
154 }
155 if (this is event.widget)
156 {
157 comboEvent(event);
158 return;
159 }
160 if (getShell() is event.widget)
161 {
162 getDisplay().asyncExec(new class Runnable
163 {
164 public void run ()
165 {
166 if (isDisposed())
167 return;
168 handleFocus(DWT.FocusOut);
169 }
170 });
171 }
172 }
173 };
174 filter = new class Listener
175 {
176 public void handleEvent (Event event)
177 {
178 Shell shell = (cast(Control) event.widget).getShell();
179 if (shell is this.getShell())
180 {
181 handleFocus(DWT.FocusOut);
182 }
183 }
184 };
185
186 int[] comboEvents = [DWT.Dispose, DWT.FocusIn, DWT.Move, DWT.Resize];
187 for (int i = 0; i < comboEvents.length; i++)
188 this.addListener(comboEvents[i], listener);
189
190 int[] textEvents = [DWT.DefaultSelection, DWT.KeyDown, DWT.KeyUp,
191 DWT.MenuDetect, DWT.Modify, DWT.MouseDown, DWT.MouseUp,
192 DWT.MouseDoubleClick, DWT.MouseWheel, DWT.Traverse,
193 DWT.FocusIn, DWT.Verify];
194 for (int i = 0; i < textEvents.length; i++)
195 text.addListener(textEvents[i], listener);
196
197 int[] arrowEvents = [DWT.MouseDown, DWT.MouseUp, DWT.Selection,
198 DWT.FocusIn];
199 for (int i = 0; i < arrowEvents.length; i++)
200 arrow.addListener(arrowEvents[i], listener);
201
202 createPopup(null, -1);
203 initAccessible();
204 }
205
206 static int checkStyle (int style)
207 {
208 int
209 mask = DWT.BORDER | DWT.READ_ONLY | DWT.FLAT | DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT;
210 return DWT.NO_FOCUS | (style & mask);
211 }
212
213 /**
214 * Adds the argument to the end of the receiver's list.
215 *
216 * @param String the new item
217 *
218 * @exception IllegalArgumentException <ul>
219 * <li>ERROR_NULL_ARGUMENT - if the String is null</li>
220 * </ul>
221 * @exception DWTException <ul>
222 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
223 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
224 * </ul>
225 *
226 * @see #add(String,int)
227 */
228 public void add (String str)
229 {
230 checkWidget();
231 if (str is null)
232 DWT.error(DWT.ERROR_NULL_ARGUMENT);
233 list.add(str);
234 }
235
236 /**
237 * Adds the argument to the receiver's list at the given
238 * zero-relative index.
239 * <p>
240 * Note: To add an item at the end of the list, use the
241 * result of calling <code>getItemCount()</code> as the
242 * index or use <code>add(String)</code>.
243 * </p>
244 *
245 * @param String the new item
246 * @param index the index for the item
247 *
248 * @exception IllegalArgumentException <ul>
249 * <li>ERROR_NULL_ARGUMENT - if the String is null</li>
250 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list (inclusive)</li>
251 * </ul>
252 * @exception DWTException <ul>
253 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
254 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
255 * </ul>
256 *
257 * @see #add(String)
258 */
259 public void add (String str, int index)
260 {
261 checkWidget();
262 if (str is null)
263 DWT.error(DWT.ERROR_NULL_ARGUMENT);
264 list.add(str, index);
265 }
266
267 /**
268 * Adds the listener to the collection of listeners who will
269 * be notified when the receiver's text is modified, by sending
270 * it one of the messages defined in the <code>ModifyListener</code>
271 * interface.
272 *
273 * @param listener the listener which should be notified
274 *
275 * @exception IllegalArgumentException <ul>
276 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
277 * </ul>
278 * @exception DWTException <ul>
279 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
280 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
281 * </ul>
282 *
283 * @see ModifyListener
284 * @see #removeModifyListener
285 */
286 public void addModifyListener (ModifyListener listener)
287 {
288 checkWidget();
289 if (listener is null)
290 DWT.error(DWT.ERROR_NULL_ARGUMENT);
291 TypedListener typedListener = new TypedListener(listener);
292 addListener(DWT.Modify, typedListener);
293 }
294
295 /**
296 * Adds the listener to the collection of listeners who will
297 * be notified when the user changes the receiver's selection, by sending
298 * it one of the messages defined in the <code>SelectionListener</code>
299 * interface.
300 * <p>
301 * <code>widgetSelected</code> is called when the combo's list selection changes.
302 * <code>widgetDefaultSelected</code> is typically called when ENTER is pressed the combo's text area.
303 * </p>
304 *
305 * @param listener the listener which should be notified when the user changes the receiver's selection
306 *
307 * @exception IllegalArgumentException <ul>
308 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
309 * </ul>
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 * @see SelectionListener
316 * @see #removeSelectionListener
317 * @see SelectionEvent
318 */
319 public void addSelectionListener (SelectionListener listener)
320 {
321 checkWidget();
322 if (listener is null)
323 DWT.error(DWT.ERROR_NULL_ARGUMENT);
324 TypedListener typedListener = new TypedListener(listener);
325 addListener(DWT.Selection, typedListener);
326 addListener(DWT.DefaultSelection, typedListener);
327 }
328
329 /**
330 * Adds the listener to the collection of listeners who will
331 * be notified when the receiver's text is verified, by sending
332 * it one of the messages defined in the <code>VerifyListener</code>
333 * interface.
334 *
335 * @param listener the listener which should be notified
336 *
337 * @exception IllegalArgumentException <ul>
338 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
339 * </ul>
340 * @exception DWTException <ul>
341 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
342 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
343 * </ul>
344 *
345 * @see VerifyListener
346 * @see #removeVerifyListener
347 *
348 * @since 3.3
349 */
350 public void addVerifyListener (VerifyListener listener)
351 {
352 checkWidget();
353 if (listener is null)
354 DWT.error(DWT.ERROR_NULL_ARGUMENT);
355 TypedListener typedListener = new TypedListener(listener);
356 addListener(DWT.Verify, typedListener);
357 }
358
359 void arrowEvent (Event event)
360 {
361 switch (event.type)
362 {
363 case DWT.FocusIn:
364 {
365 handleFocus(DWT.FocusIn);
366 break;
367 }
368 case DWT.MouseDown:
369 {
370 Event mouseEvent = new Event();
371 mouseEvent.button = event.button;
372 mouseEvent.count = event.count;
373 mouseEvent.stateMask = event.stateMask;
374 mouseEvent.time = event.time;
375 mouseEvent.x = event.x;
376 mouseEvent.y = event.y;
377 notifyListeners(DWT.MouseDown, mouseEvent);
378 event.doit = mouseEvent.doit;
379 break;
380 }
381 case DWT.MouseUp:
382 {
383 Event mouseEvent = new Event();
384 mouseEvent.button = event.button;
385 mouseEvent.count = event.count;
386 mouseEvent.stateMask = event.stateMask;
387 mouseEvent.time = event.time;
388 mouseEvent.x = event.x;
389 mouseEvent.y = event.y;
390 notifyListeners(DWT.MouseUp, mouseEvent);
391 event.doit = mouseEvent.doit;
392 break;
393 }
394 case DWT.Selection:
395 {
396 text.setFocus();
397 dropDown(!isDropped());
398 break;
399 }
400
401 default:
402 break;
403 }
404 }
405
406 /**
407 * Sets the selection in the receiver's text field to an empty
408 * selection starting just before the first character. If the
409 * text field is editable, this has the effect of placing the
410 * i-beam at the start of the text.
411 * <p>
412 * Note: To clear the selected items in the receiver's list,
413 * use <code>deselectAll()</code>.
414 * </p>
415 *
416 * @exception DWTException <ul>
417 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
418 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
419 * </ul>
420 *
421 * @see #deselectAll
422 */
423 public void clearSelection ()
424 {
425 checkWidget();
426 text.clearSelection();
427 list.deselectAll();
428 }
429
430 void comboEvent (Event event)
431 {
432 switch (event.type)
433 {
434 case DWT.Dispose:
435 if (popup !is null && !popup.isDisposed())
436 {
437 list.removeListener(DWT.Dispose, listener);
438 popup.dispose();
439 }
440 Shell shell = getShell();
441 shell.removeListener(DWT.Deactivate, listener);
442 Display display = getDisplay();
443 display.removeFilter(DWT.FocusIn, filter);
444 popup = null;
445 text = null;
446 list = null;
447 arrow = null;
448 break;
449 case DWT.FocusIn:
450 Control focusControl = getDisplay().getFocusControl();
451 if (focusControl is arrow || focusControl is list)
452 return;
453 if (isDropped())
454 {
455 list.setFocus();
456 }
457 else
458 {
459 text.setFocus();
460 }
461 break;
462 case DWT.Move:
463 dropDown(false);
464 break;
465 case DWT.Resize:
466 internalLayout(false);
467 break;
468
469 default:
470 break;
471 }
472 }
473
474 public Point computeSize (int wHint, int hHint, bool changed)
475 {
476 checkWidget();
477 int width = 0, height = 0;
478 String[] items = list.getItems();
479 GC gc = new GC(text);
480 int spacer = gc.StringExtent(" ").x; //$NON-NLS-1$
481 int textWidth = gc.StringExtent(text.getText()).x;
482 for (int i = 0; i < items.length; i++)
483 {
484 textWidth = Math.max(gc.StringExtent(items[i]).x, textWidth);
485 }
486 gc.dispose();
487 Point textSize = text.computeSize(DWT.DEFAULT, DWT.DEFAULT, changed);
488 Point arrowSize = arrow.computeSize(DWT.DEFAULT, DWT.DEFAULT, changed);
489 Point listSize = list.computeSize(DWT.DEFAULT, DWT.DEFAULT, changed);
490 int borderWidth = getBorderWidth();
491
492 height = Math.max(textSize.y, arrowSize.y);
493 width = Math.max(
494 textWidth + 2 * spacer + arrowSize.x + 2 * borderWidth,
495 listSize.x);
496 if (wHint !is DWT.DEFAULT)
497 width = wHint;
498 if (hHint !is DWT.DEFAULT)
499 height = hHint;
500 return new Point(width + 2 * borderWidth, height + 2 * borderWidth);
501 }
502
503 /**
504 * Copies the selected text.
505 * <p>
506 * The current selection is copied to the clipboard.
507 * </p>
508 *
509 * @exception DWTException <ul>
510 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
511 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
512 * </ul>
513 *
514 * @since 3.3
515 */
516 public void copy ()
517 {
518 checkWidget();
519 text.copy();
520 }
521
522 void createPopup (String[] items, int selectionIndex)
523 {
524 // create shell and list
525 popup = new Shell(getShell(), DWT.NO_TRIM | DWT.ON_TOP);
526 int style = getStyle();
527 int listStyle = DWT.SINGLE | DWT.V_SCROLL;
528 if ((style & DWT.FLAT) !is 0)
529 listStyle |= DWT.FLAT;
530 if ((style & DWT.RIGHT_TO_LEFT) !is 0)
531 listStyle |= DWT.RIGHT_TO_LEFT;
532 if ((style & DWT.LEFT_TO_RIGHT) !is 0)
533 listStyle |= DWT.LEFT_TO_RIGHT;
534 list = new List(popup, listStyle);
535 if (font !is null)
536 list.setFont(font);
537 if (foreground !is null)
538 list.setForeground(foreground);
539 if (background !is null)
540 list.setBackground(background);
541
542 int[] popupEvents = [DWT.Close, DWT.Paint, DWT.Deactivate];
543 for (int i = 0; i < popupEvents.length; i++)
544 popup.addListener(popupEvents[i], listener);
545 int[] listEvents = [DWT.MouseUp, DWT.Selection, DWT.Traverse,
546 DWT.KeyDown, DWT.KeyUp, DWT.FocusIn, DWT.Dispose];
547 for (int i = 0; i < listEvents.length; i++)
548 list.addListener(listEvents[i], listener);
549
550 if (items !is null)
551 list.setItems(items);
552 if (selectionIndex !is -1)
553 list.setSelection(selectionIndex);
554 }
555
556 /**
557 * Cuts the selected text.
558 * <p>
559 * The current selection is first copied to the
560 * clipboard and then deleted from the widget.
561 * </p>
562 *
563 * @exception DWTException <ul>
564 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
565 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
566 * </ul>
567 *
568 * @since 3.3
569 */
570 public void cut ()
571 {
572 checkWidget();
573 text.cut();
574 }
575
576 /**
577 * Deselects the item at the given zero-relative index in the receiver's
578 * list. If the item at the index was already deselected, it remains
579 * deselected. Indices that are out of range are ignored.
580 *
581 * @param index the index of the item to deselect
582 *
583 * @exception DWTException <ul>
584 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
585 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
586 * </ul>
587 */
588 public void deselect (int index)
589 {
590 checkWidget();
591 if (0 <= index && index < list.getItemCount() && index is list.getSelectionIndex() && text.getText().opEquals(
592 list.getItem(index)))
593 {
594 text.setText(""); //$NON-NLS-1$
595 list.deselect(index);
596 }
597 }
598
599 /**
600 * Deselects all selected items in the receiver's list.
601 * <p>
602 * Note: To clear the selection in the receiver's text field,
603 * use <code>clearSelection()</code>.
604 * </p>
605 *
606 * @exception DWTException <ul>
607 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
608 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
609 * </ul>
610 *
611 * @see #clearSelection
612 */
613 public void deselectAll ()
614 {
615 checkWidget();
616 text.setText(""); //$NON-NLS-1$
617 list.deselectAll();
618 }
619
620 void dropDown (bool drop)
621 {
622 if (drop is isDropped())
623 return;
624 if (!drop)
625 {
626 popup.setVisible(false);
627 if (!isDisposed() && isFocusControl())
628 {
629 text.setFocus();
630 }
631 return;
632 }
633
634 if (getShell() !is popup.getParent())
635 {
636 String[] items = list.getItems();
637 int selectionIndex = list.getSelectionIndex();
638 list.removeListener(DWT.Dispose, listener);
639 popup.dispose();
640 popup = null;
641 list = null;
642 createPopup(items, selectionIndex);
643 }
644
645 Point size = getSize();
646 int itemCount = list.getItemCount();
647 itemCount = (itemCount is 0) ? visibleItemCount : Math.min(
648 visibleItemCount, itemCount);
649 int itemHeight = list.getItemHeight() * itemCount;
650 Point listSize = list.computeSize(DWT.DEFAULT, itemHeight, false);
651 list.setBounds(1, 1, Math.max(size.x - 2, listSize.x), listSize.y);
652
653 int index = list.getSelectionIndex();
654 if (index !is -1)
655 list.setTopIndex(index);
656 Display display = getDisplay();
657 Rectangle listRect = list.getBounds();
658 Rectangle parentRect = display.map(getParent(), null, getBounds());
659 Point comboSize = getSize();
660 Rectangle displayRect = getMonitor().getClientArea();
661 int width = Math.max(comboSize.x, listRect.width + 2);
662 int height = listRect.height + 2;
663 int x = parentRect.x;
664 int y = parentRect.y + comboSize.y;
665 if (y + height > displayRect.y + displayRect.height)
666 y = parentRect.y - height;
667 if (x + width > displayRect.x + displayRect.width)
668 x = displayRect.x + displayRect.width - listRect.width;
669 popup.setBounds(x, y, width, height);
670 popup.setVisible(true);
671 if (isFocusControl())
672 list.setFocus();
673 }
674
675 /*
676 * Return the lowercase of the first non-'&' character following
677 * an '&' character in the given String. If there are no '&'
678 * characters in the given String, return '\0'.
679 */
680 char _findMnemonic (String str)
681 {
682 if (str is null)
683 return '\0';
684 int index = 0;
685 int length = str.length();
686 do
687 {
688 while (index < length && str.charAt(index) !is '&')
689 index++;
690 if (++index >= length)
691 return '\0';
692 if (str.charAt(index) !is '&')
693 return CharacterToLower(string.charAt(index));
694 index++;
695 } while (index < length);
696 return '\0';
697 }
698
699 /*
700 * Return the Label immediately preceding the receiver in the z-order,
701 * or null if none.
702 */
703 Label getAssociatedLabel ()
704 {
705 Control[] siblings = getParent().getChildren();
706 for (int i = 0; i < siblings.length; i++)
707 {
708 if (siblings[i] is this)
709 {
710 if (i > 0 && cast(Label) (siblings[i - 1]))
711 {
712 return cast(Label) (siblings[i - 1]);
713 }
714 }
715 }
716 return null;
717 }
718
719 public Control[] getChildren ()
720 {
721 checkWidget();
722 return new Control[0];
723 }
724
725 /**
726 * Gets the editable state.
727 *
728 * @return whether or not the receiver is editable
729 *
730 * @exception DWTException <ul>
731 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
732 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
733 * </ul>
734 *
735 * @since 3.0
736 */
737 public bool getEditable ()
738 {
739 checkWidget();
740 return text.getEditable();
741 }
742
743 /**
744 * Returns the item at the given, zero-relative index in the
745 * receiver's list. Throws an exception if the index is out
746 * of range.
747 *
748 * @param index the index of the item to return
749 * @return the item at the given index
750 *
751 * @exception IllegalArgumentException <ul>
752 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
753 * </ul>
754 * @exception DWTException <ul>
755 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
756 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
757 * </ul>
758 */
759 public String getItem (int index)
760 {
761 checkWidget();
762 return list.getItem(index);
763 }
764
765 /**
766 * Returns the number of items contained in the receiver's list.
767 *
768 * @return the number of items
769 *
770 * @exception DWTException <ul>
771 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
772 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
773 * </ul>
774 */
775 public int getItemCount ()
776 {
777 checkWidget();
778 return list.getItemCount();
779 }
780
781 /**
782 * Returns the height of the area which would be used to
783 * display <em>one</em> of the items in the receiver's list.
784 *
785 * @return the height of one item
786 *
787 * @exception DWTException <ul>
788 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
789 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
790 * </ul>
791 */
792 public int getItemHeight ()
793 {
794 checkWidget();
795 return list.getItemHeight();
796 }
797
798 /**
799 * Returns an array of <code>String</code>s which are the items
800 * in the receiver's list.
801 * <p>
802 * Note: This is not the actual structure used by the receiver
803 * to maintain its list of items, so modifying the array will
804 * not affect the receiver.
805 * </p>
806 *
807 * @return the items in the receiver's list
808 *
809 * @exception DWTException <ul>
810 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
811 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
812 * </ul>
813 */
814 public String[] getItems ()
815 {
816 checkWidget();
817 return list.getItems();
818 }
819
820 /**
821 * Returns <code>true</code> if the receiver's list is visible,
822 * and <code>false</code> otherwise.
823 * <p>
824 * If one of the receiver's ancestors is not visible or some
825 * other condition makes the receiver not visible, this method
826 * may still indicate that it is considered visible even though
827 * it may not actually be showing.
828 * </p>
829 *
830 * @return the receiver's list's visibility state
831 *
832 * @exception DWTException <ul>
833 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
834 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
835 * </ul>
836 *
837 * @since 3.4
838 */
839 public bool getListVisible ()
840 {
841 checkWidget();
842 return isDropped();
843 }
844
845 public Menu getMenu ()
846 {
847 return text.getMenu();
848 }
849
850 /**
851 * Returns a <code>Point</code> whose x coordinate is the start
852 * of the selection in the receiver's text field, and whose y
853 * coordinate is the end of the selection. The returned values
854 * are zero-relative. An "empty" selection as indicated by
855 * the the x and y coordinates having the same value.
856 *
857 * @return a point representing the selection start and end
858 *
859 * @exception DWTException <ul>
860 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
861 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
862 * </ul>
863 */
864 public Point getSelection ()
865 {
866 checkWidget();
867 return text.getSelection();
868 }
869
870 /**
871 * Returns the zero-relative index of the item which is currently
872 * selected in the receiver's list, or -1 if no item is selected.
873 *
874 * @return the index of the selected item
875 *
876 * @exception DWTException <ul>
877 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
878 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
879 * </ul>
880 */
881 public int getSelectionIndex ()
882 {
883 checkWidget();
884 return list.getSelectionIndex();
885 }
886
887 public int getStyle ()
888 {
889 int style = super.getStyle();
890 style &= ~DWT.READ_ONLY;
891 if (!text.getEditable())
892 style |= DWT.READ_ONLY;
893 return style;
894 }
895
896 /**
897 * Returns a String containing a copy of the contents of the
898 * receiver's text field.
899 *
900 * @return the receiver's text
901 *
902 * @exception DWTException <ul>
903 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
904 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
905 * </ul>
906 */
907 public String getText ()
908 {
909 checkWidget();
910 return text.getText();
911 }
912
913 /**
914 * Returns the height of the receivers's text field.
915 *
916 * @return the text height
917 *
918 * @exception DWTException <ul>
919 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
920 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
921 * </ul>
922 */
923 public int getTextHeight ()
924 {
925 checkWidget();
926 return text.getLineHeight();
927 }
928
929 /**
930 * Returns the maximum number of characters that the receiver's
931 * text field is capable of holding. If this has not been changed
932 * by <code>setTextLimit()</code>, it will be the constant
933 * <code>Combo.LIMIT</code>.
934 *
935 * @return the text limit
936 *
937 * @exception DWTException <ul>
938 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
939 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
940 * </ul>
941 */
942 public int getTextLimit ()
943 {
944 checkWidget();
945 return text.getTextLimit();
946 }
947
948 /**
949 * Gets the number of items that are visible in the drop
950 * down portion of the receiver's list.
951 *
952 * @return the number of items that are visible
953 *
954 * @exception DWTException <ul>
955 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
956 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
957 * </ul>
958 *
959 * @since 3.0
960 */
961 public int getVisibleItemCount ()
962 {
963 checkWidget();
964 return visibleItemCount;
965 }
966
967 void handleFocus (int type)
968 {
969 if (isDisposed())
970 return;
971 switch (type)
972 {
973 case DWT.FocusIn:
974 {
975 if (hasFocus)
976 return;
977 if (getEditable())
978 text.selectAll();
979 hasFocus = true;
980 Shell shell = getShell();
981 shell.removeListener(DWT.Deactivate, listener);
982 shell.addListener(DWT.Deactivate, listener);
983 Display display = getDisplay();
984 display.removeFilter(DWT.FocusIn, filter);
985 display.addFilter(DWT.FocusIn, filter);
986 Event e = new Event();
987 notifyListeners(DWT.FocusIn, e);
988 break;
989 }
990 case DWT.FocusOut:
991 {
992 if (!hasFocus)
993 return;
994 Control focusControl = getDisplay().getFocusControl();
995 if (focusControl is arrow || focusControl is list || focusControl is text)
996 return;
997 hasFocus = false;
998 Shell shell = getShell();
999 shell.removeListener(DWT.Deactivate, listener);
1000 Display display = getDisplay();
1001 display.removeFilter(DWT.FocusIn, filter);
1002 Event e = new Event();
1003 notifyListeners(DWT.FocusOut, e);
1004 break;
1005 }
1006
1007 default:
1008 break;
1009 }
1010 }
1011
1012 /**
1013 * Searches the receiver's list starting at the first item
1014 * (index 0) until an item is found that is equal to the
1015 * argument, and returns the index of that item. If no item
1016 * is found, returns -1.
1017 *
1018 * @param String the search item
1019 * @return the index of the item
1020 *
1021 * @exception IllegalArgumentException <ul>
1022 * <li>ERROR_NULL_ARGUMENT - if the String is null</li>
1023 * </ul>
1024 * @exception DWTException <ul>
1025 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1026 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1027 * </ul>
1028 */
1029 public int indexOf (String String)
1030 {
1031 checkWidget();
1032 if (String is null)
1033 DWT.error(DWT.ERROR_NULL_ARGUMENT);
1034 return list.indexOf(String);
1035 }
1036
1037 /**
1038 * Searches the receiver's list starting at the given,
1039 * zero-relative index until an item is found that is equal
1040 * to the argument, and returns the index of that item. If
1041 * no item is found or the starting index is out of range,
1042 * returns -1.
1043 *
1044 * @param String the search item
1045 * @param start the zero-relative index at which to begin the search
1046 * @return the index of the item
1047 *
1048 * @exception IllegalArgumentException <ul>
1049 * <li>ERROR_NULL_ARGUMENT - if the String is null</li>
1050 * </ul>
1051 * @exception DWTException <ul>
1052 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1053 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1054 * </ul>
1055 */
1056 public int indexOf (String String, int start)
1057 {
1058 checkWidget();
1059 if (String is null)
1060 DWT.error(DWT.ERROR_NULL_ARGUMENT);
1061 return list.indexOf(String, start);
1062 }
1063
1064 void initAccessible ()
1065 {
1066 AccessibleAdapter accessibleAdapter = new class AccessibleAdapter
1067 {
1068 public void getName (AccessibleEvent e)
1069 {
1070 String name = null;
1071 Label label = getAssociatedLabel();
1072 if (label !is null)
1073 {
1074 name = stripMnemonic(label.getText());
1075 }
1076 e.result = name;
1077 }
1078
1079 public void getKeyboardShortcut (AccessibleEvent e)
1080 {
1081 String shortcut = null;
1082 Label label = getAssociatedLabel();
1083 if (label !is null)
1084 {
1085 String text = label.getText();
1086 if (text !is null)
1087 {
1088 char mnemonic = _findMnemonic(text);
1089 if (mnemonic !is '\0')
1090 {
1091 shortcut = "Alt+" + mnemonic; //$NON-NLS-1$
1092 }
1093 }
1094 }
1095 e.result = shortcut;
1096 }
1097
1098 public void getHelp (AccessibleEvent e)
1099 {
1100 e.result = getToolTipText();
1101 }
1102 } ;
1103 getAccessible().addAccessibleListener(accessibleAdapter);
1104 text.getAccessible().addAccessibleListener(accessibleAdapter);
1105 list.getAccessible().addAccessibleListener(accessibleAdapter);
1106
1107 arrow.getAccessible().addAccessibleListener(
1108 new class AccessibleAdapter
1109 {
1110 public void getName (AccessibleEvent e)
1111 {
1112 e.result = isDropped() ? DWT.getMessage("DWT_Close") : DWT.getMessage(
1113 "DWT_Open"); //$NON-NLS-1$ //$NON-NLS-2$
1114 }
1115
1116 public void getKeyboardShortcut (AccessibleEvent e)
1117 {
1118 e.result = "Alt+Down Arrow"; //$NON-NLS-1$
1119 }
1120
1121 public void getHelp (AccessibleEvent e)
1122 {
1123 e.result = getToolTipText();
1124 } 164 }
1125 }); 165 });
1126 166 }
1127 getAccessible().addAccessibleTextListener(new class 167 }
1128 AccessibleTextAdapter 168 };
1129 { 169 filter = new class() Listener {
1130 public void getCaretOffset (AccessibleTextEvent e) 170 public void handleEvent(Event event) {
1131 { 171 Shell shell = (cast(Control)event.widget).getShell ();
1132 e.offset = text.getCaretPosition(); 172 if (shell is this.outer.getShell ()) {
1133 } 173 handleFocus (DWT.FocusOut);
1134 174 }
1135 public void getSelectionRange (AccessibleTextEvent e) 175 }
1136 { 176 };
1137 Point sel = text.getSelection(); 177
1138 e.offset = sel.x; 178 int [] comboEvents = [DWT.Dispose, DWT.FocusIn, DWT.Move, DWT.Resize];
1139 e.length = sel.y - sel.x; 179 for (int i=0; i<comboEvents.length; i++) this.addListener (comboEvents [i], listener);
1140 } 180
1141 }); 181 int [] textEvents = [DWT.DefaultSelection, DWT.KeyDown, DWT.KeyUp, DWT.MenuDetect, DWT.Modify, DWT.MouseDown, DWT.MouseUp, DWT.MouseDoubleClick, DWT.MouseWheel, DWT.Traverse, DWT.FocusIn, DWT.Verify];
1142 182 for (int i=0; i<textEvents.length; i++) text.addListener (textEvents [i], listener);
1143 getAccessible().addAccessibleControlListener(new class 183
1144 AccessibleControlAdapter 184 int [] arrowEvents = [DWT.MouseDown, DWT.MouseUp, DWT.Selection, DWT.FocusIn];
1145 { 185 for (int i=0; i<arrowEvents.length; i++) arrow.addListener (arrowEvents [i], listener);
1146 public void getChildAtPoint (AccessibleControlEvent e) 186
1147 { 187 createPopup(null, -1);
1148 Point testPoint = toControl(e.x, e.y); 188 initAccessible();
1149 if (getBounds().contains(testPoint)) 189 }
1150 { 190 static int checkStyle (int style) {
1151 e.childID = ACC.CHILDID_SELF; 191 int mask = DWT.BORDER | DWT.READ_ONLY | DWT.FLAT | DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT;
192 return DWT.NO_FOCUS | (style & mask);
193 }
194 /**
195 * Adds the argument to the end of the receiver's list.
196 *
197 * @param string the new item
198 *
199 * @exception DWTException <ul>
200 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
201 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
202 * </ul>
203 *
204 * @see #add(String,int)
205 */
206 public void add (String string) {
207 checkWidget();
208 // DWT extension: allow null for zero length string
209 //if (string is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
210 list.add (string);
211 }
212 /**
213 * Adds the argument to the receiver's list at the given
214 * zero-relative index.
215 * <p>
216 * Note: To add an item at the end of the list, use the
217 * result of calling <code>getItemCount()</code> as the
218 * index or use <code>add(String)</code>.
219 * </p>
220 *
221 * @param string the new item
222 * @param index the index for the item
223 *
224 * @exception IllegalArgumentException <ul>
225 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list (inclusive)</li>
226 * </ul>
227 * @exception DWTException <ul>
228 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
229 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
230 * </ul>
231 *
232 * @see #add(String)
233 */
234 public void add (String string, int index) {
235 checkWidget();
236 // DWT extension: allow null for zero length string
237 //if (string is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
238 list.add (string, index);
239 }
240 /**
241 * Adds the listener to the collection of listeners who will
242 * be notified when the receiver's text is modified, by sending
243 * it one of the messages defined in the <code>ModifyListener</code>
244 * interface.
245 *
246 * @param listener the listener which should be notified
247 *
248 * @exception IllegalArgumentException <ul>
249 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
250 * </ul>
251 * @exception DWTException <ul>
252 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
253 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
254 * </ul>
255 *
256 * @see ModifyListener
257 * @see #removeModifyListener
258 */
259 public void addModifyListener (ModifyListener listener) {
260 checkWidget();
261 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
262 TypedListener typedListener = new TypedListener (listener);
263 addListener (DWT.Modify, typedListener);
264 }
265 /**
266 * Adds the listener to the collection of listeners who will
267 * be notified when the user changes the receiver's selection, by sending
268 * it one of the messages defined in the <code>SelectionListener</code>
269 * interface.
270 * <p>
271 * <code>widgetSelected</code> is called when the combo's list selection changes.
272 * <code>widgetDefaultSelected</code> is typically called when ENTER is pressed the combo's text area.
273 * </p>
274 *
275 * @param listener the listener which should be notified when the user changes the receiver's selection
276 *
277 * @exception IllegalArgumentException <ul>
278 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
279 * </ul>
280 * @exception DWTException <ul>
281 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
282 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
283 * </ul>
284 *
285 * @see SelectionListener
286 * @see #removeSelectionListener
287 * @see SelectionEvent
288 */
289 public void addSelectionListener(SelectionListener listener) {
290 checkWidget();
291 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
292 TypedListener typedListener = new TypedListener (listener);
293 addListener (DWT.Selection,typedListener);
294 addListener (DWT.DefaultSelection,typedListener);
295 }
296 /**
297 * Adds the listener to the collection of listeners who will
298 * be notified when the receiver's text is verified, by sending
299 * it one of the messages defined in the <code>VerifyListener</code>
300 * interface.
301 *
302 * @param listener the listener which should be notified
303 *
304 * @exception IllegalArgumentException <ul>
305 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
306 * </ul>
307 * @exception DWTException <ul>
308 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
309 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
310 * </ul>
311 *
312 * @see VerifyListener
313 * @see #removeVerifyListener
314 *
315 * @since 3.3
316 */
317 public void addVerifyListener (VerifyListener listener) {
318 checkWidget();
319 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
320 TypedListener typedListener = new TypedListener (listener);
321 addListener (DWT.Verify,typedListener);
322 }
323 void arrowEvent (Event event) {
324 switch (event.type) {
325 case DWT.FocusIn: {
326 handleFocus (DWT.FocusIn);
327 break;
328 }
329 case DWT.MouseDown: {
330 Event mouseEvent = new Event ();
331 mouseEvent.button = event.button;
332 mouseEvent.count = event.count;
333 mouseEvent.stateMask = event.stateMask;
334 mouseEvent.time = event.time;
335 mouseEvent.x = event.x; mouseEvent.y = event.y;
336 notifyListeners (DWT.MouseDown, mouseEvent);
337 event.doit = mouseEvent.doit;
338 break;
339 }
340 case DWT.MouseUp: {
341 Event mouseEvent = new Event ();
342 mouseEvent.button = event.button;
343 mouseEvent.count = event.count;
344 mouseEvent.stateMask = event.stateMask;
345 mouseEvent.time = event.time;
346 mouseEvent.x = event.x; mouseEvent.y = event.y;
347 notifyListeners (DWT.MouseUp, mouseEvent);
348 event.doit = mouseEvent.doit;
349 break;
350 }
351 case DWT.Selection: {
352 text.setFocus();
353 dropDown (!isDropped ());
354 break;
355 }
356 default:
357 }
358 }
359 /**
360 * Sets the selection in the receiver's text field to an empty
361 * selection starting just before the first character. If the
362 * text field is editable, this has the effect of placing the
363 * i-beam at the start of the text.
364 * <p>
365 * Note: To clear the selected items in the receiver's list,
366 * use <code>deselectAll()</code>.
367 * </p>
368 *
369 * @exception DWTException <ul>
370 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
371 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
372 * </ul>
373 *
374 * @see #deselectAll
375 */
376 public void clearSelection () {
377 checkWidget ();
378 text.clearSelection ();
379 list.deselectAll ();
380 }
381 void comboEvent (Event event) {
382 switch (event.type) {
383 case DWT.Dispose:
384 if (popup !is null && !popup.isDisposed ()) {
385 list.removeListener (DWT.Dispose, listener);
386 popup.dispose ();
387 }
388 Shell shell = getShell ();
389 shell.removeListener (DWT.Deactivate, listener);
390 Display display = getDisplay ();
391 display.removeFilter (DWT.FocusIn, filter);
392 popup = null;
393 text = null;
394 list = null;
395 arrow = null;
396 break;
397 case DWT.FocusIn:
398 Control focusControl = getDisplay ().getFocusControl ();
399 if (focusControl is arrow || focusControl is list) return;
400 if (isDropped()) {
401 list.setFocus();
402 } else {
403 text.setFocus();
404 }
405 break;
406 case DWT.Move:
407 dropDown (false);
408 break;
409 case DWT.Resize:
410 internalLayout (false);
411 break;
412 default:
413 }
414 }
415
416 public override Point computeSize (int wHint, int hHint, bool changed) {
417 checkWidget ();
418 int width = 0, height = 0;
419 String[] items = list.getItems ();
420 GC gc = new GC (text);
421 int spacer = gc.stringExtent (" ").x; //$NON-NLS-1$
422 int textWidth = gc.stringExtent (text.getText ()).x;
423 for (int i = 0; i < items.length; i++) {
424 textWidth = Math.max (gc.stringExtent (items[i]).x, textWidth);
425 }
426 gc.dispose ();
427 Point textSize = text.computeSize (DWT.DEFAULT, DWT.DEFAULT, changed);
428 Point arrowSize = arrow.computeSize (DWT.DEFAULT, DWT.DEFAULT, changed);
429 Point listSize = list.computeSize (DWT.DEFAULT, DWT.DEFAULT, changed);
430 int borderWidth = getBorderWidth ();
431
432 height = Math.max (textSize.y, arrowSize.y);
433 width = Math.max (textWidth + 2*spacer + arrowSize.x + 2*borderWidth, listSize.x);
434 if (wHint !is DWT.DEFAULT) width = wHint;
435 if (hHint !is DWT.DEFAULT) height = hHint;
436 return new Point (width + 2*borderWidth, height + 2*borderWidth);
437 }
438 /**
439 * Copies the selected text.
440 * <p>
441 * The current selection is copied to the clipboard.
442 * </p>
443 *
444 * @exception DWTException <ul>
445 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
446 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
447 * </ul>
448 *
449 * @since 3.3
450 */
451 public void copy () {
452 checkWidget ();
453 text.copy ();
454 }
455 void createPopup(String[] items, int selectionIndex) {
456 // create shell and list
457 popup = new Shell (getShell (), DWT.NO_TRIM | DWT.ON_TOP);
458 int style = getStyle ();
459 int listStyle = DWT.SINGLE | DWT.V_SCROLL;
460 if ((style & DWT.FLAT) !is 0) listStyle |= DWT.FLAT;
461 if ((style & DWT.RIGHT_TO_LEFT) !is 0) listStyle |= DWT.RIGHT_TO_LEFT;
462 if ((style & DWT.LEFT_TO_RIGHT) !is 0) listStyle |= DWT.LEFT_TO_RIGHT;
463 list = new List (popup, listStyle);
464 if (font !is null) list.setFont (font);
465 if (foreground !is null) list.setForeground (foreground);
466 if (background !is null) list.setBackground (background);
467
468 int [] popupEvents = [DWT.Close, DWT.Paint, DWT.Deactivate];
469 for (int i=0; i<popupEvents.length; i++) popup.addListener (popupEvents [i], listener);
470 int [] listEvents = [DWT.MouseUp, DWT.Selection, DWT.Traverse, DWT.KeyDown, DWT.KeyUp, DWT.FocusIn, DWT.Dispose];
471 for (int i=0; i<listEvents.length; i++) list.addListener (listEvents [i], listener);
472
473 if (items !is null) list.setItems (items);
474 if (selectionIndex !is -1) list.setSelection (selectionIndex);
475 }
476 /**
477 * Cuts the selected text.
478 * <p>
479 * The current selection is first copied to the
480 * clipboard and then deleted from the widget.
481 * </p>
482 *
483 * @exception DWTException <ul>
484 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
485 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
486 * </ul>
487 *
488 * @since 3.3
489 */
490 public void cut () {
491 checkWidget ();
492 text.cut ();
493 }
494 /**
495 * Deselects the item at the given zero-relative index in the receiver's
496 * list. If the item at the index was already deselected, it remains
497 * deselected. Indices that are out of range are ignored.
498 *
499 * @param index the index of the item to deselect
500 *
501 * @exception DWTException <ul>
502 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
503 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
504 * </ul>
505 */
506 public void deselect (int index) {
507 checkWidget ();
508 if (0 <= index && index < list.getItemCount () &&
509 index is list.getSelectionIndex() &&
510 text.getText().equals(list.getItem(index))) {
511 text.setText(""); //$NON-NLS-1$
512 list.deselect (index);
513 }
514 }
515 /**
516 * Deselects all selected items in the receiver's list.
517 * <p>
518 * Note: To clear the selection in the receiver's text field,
519 * use <code>clearSelection()</code>.
520 * </p>
521 *
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 * @see #clearSelection
528 */
529 public void deselectAll () {
530 checkWidget ();
531 text.setText(""); //$NON-NLS-1$
532 list.deselectAll ();
533 }
534 void dropDown (bool drop) {
535 if (drop is isDropped () || !isVisible()) return;
536 if (!drop) {
537 popup.setVisible (false);
538 if (!isDisposed () && isFocusControl()) {
539 text.setFocus();
540 }
541 return;
542 }
543
544 if (getShell() !is popup.getParent ()) {
545 String[] items = list.getItems ();
546 int selectionIndex = list.getSelectionIndex ();
547 list.removeListener (DWT.Dispose, listener);
548 popup.dispose();
549 popup = null;
550 list = null;
551 createPopup (items, selectionIndex);
552 }
553
554 Point size = getSize ();
555 int itemCount = list.getItemCount ();
556 itemCount = (itemCount is 0) ? visibleItemCount : Math.min(visibleItemCount, itemCount);
557 int itemHeight = list.getItemHeight () * itemCount;
558 Point listSize = list.computeSize (DWT.DEFAULT, itemHeight, false);
559 list.setBounds (1, 1, Math.max (size.x - 2, listSize.x), listSize.y);
560
561 int index = list.getSelectionIndex ();
562 if (index !is -1) list.setTopIndex (index);
563 Display display = getDisplay ();
564 Rectangle listRect = list.getBounds ();
565 Rectangle parentRect = display.map (getParent (), null, getBounds ());
566 Point comboSize = getSize ();
567 Rectangle displayRect = getMonitor ().getClientArea ();
568 int width = Math.max (comboSize.x, listRect.width + 2);
569 int height = listRect.height + 2;
570 int x = parentRect.x;
571 int y = parentRect.y + comboSize.y;
572 if (y + height > displayRect.y + displayRect.height) y = parentRect.y - height;
573 if (x + width > displayRect.x + displayRect.width) x = displayRect.x + displayRect.width - listRect.width;
574 popup.setBounds (x, y, width, height);
575 popup.setVisible (true);
576 if (isFocusControl()) list.setFocus ();
577 }
578 /*
579 * Return the lowercase of the first non-'&' character following
580 * an '&' character in the given string. If there are no '&'
581 * characters in the given string, return '\0'.
582 */
583 dchar _findMnemonic (String string) {
584 if (string is null) return '\0';
585 int index = 0;
586 int length = string.length;
587 do {
588 while (index < length && string[index] !is '&') index++;
589 if (++index >= length) return '\0';
590 if (string[index] !is '&') {
591 dchar[1] d; uint ate;
592 auto d2 = tango.text.convert.Utf.toString32( string[ index .. Math.min( index +4, string.length )], d, &ate );
593 auto d3 = tango.text.Unicode.toLower( d2, d2 );
594 return d3[0];
595 }
596 index++;
597 } while (index < length);
598 return '\0';
599 }
600 /*
601 * Return the Label immediately preceding the receiver in the z-order,
602 * or null if none.
603 */
604 Label getAssociatedLabel () {
605 Control[] siblings = getParent ().getChildren ();
606 for (int i = 0; i < siblings.length; i++) {
607 if (siblings [i] is this) {
608 if (i > 0 && ( null !is cast(Label)siblings [i-1] )) {
609 return cast(Label) siblings [i-1];
610 }
611 }
612 }
613 return null;
614 }
615 public override Control [] getChildren () {
616 checkWidget();
617 return new Control [0];
618 }
619 /**
620 * Gets the editable state.
621 *
622 * @return whether or not the receiver is editable
623 *
624 * @exception DWTException <ul>
625 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
626 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
627 * </ul>
628 *
629 * @since 3.0
630 */
631 public bool getEditable () {
632 checkWidget ();
633 return text.getEditable();
634 }
635 /**
636 * Returns the item at the given, zero-relative index in the
637 * receiver's list. Throws an exception if the index is out
638 * of range.
639 *
640 * @param index the index of the item to return
641 * @return the item at the given index
642 *
643 * @exception IllegalArgumentException <ul>
644 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
645 * </ul>
646 * @exception DWTException <ul>
647 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
648 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
649 * </ul>
650 */
651 public String getItem (int index) {
652 checkWidget();
653 return list.getItem (index);
654 }
655 /**
656 * Returns the number of items contained in the receiver's list.
657 *
658 * @return the number of items
659 *
660 * @exception DWTException <ul>
661 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
662 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
663 * </ul>
664 */
665 public int getItemCount () {
666 checkWidget ();
667 return list.getItemCount ();
668 }
669 /**
670 * Returns the height of the area which would be used to
671 * display <em>one</em> of the items in the receiver's list.
672 *
673 * @return the height of one item
674 *
675 * @exception DWTException <ul>
676 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
677 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
678 * </ul>
679 */
680 public int getItemHeight () {
681 checkWidget ();
682 return list.getItemHeight ();
683 }
684 /**
685 * Returns an array of <code>String</code>s which are the items
686 * in the receiver's list.
687 * <p>
688 * Note: This is not the actual structure used by the receiver
689 * to maintain its list of items, so modifying the array will
690 * not affect the receiver.
691 * </p>
692 *
693 * @return the items in the receiver's list
694 *
695 * @exception DWTException <ul>
696 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
697 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
698 * </ul>
699 */
700 public String [] getItems () {
701 checkWidget ();
702 return list.getItems ();
703 }
704 /**
705 * Returns <code>true</code> if the receiver's list is visible,
706 * and <code>false</code> otherwise.
707 * <p>
708 * If one of the receiver's ancestors is not visible or some
709 * other condition makes the receiver not visible, this method
710 * may still indicate that it is considered visible even though
711 * it may not actually be showing.
712 * </p>
713 *
714 * @return the receiver's list's visibility state
715 *
716 * @exception DWTException <ul>
717 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
718 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
719 * </ul>
720 *
721 * @since 3.4
722 */
723 public bool getListVisible () {
724 checkWidget ();
725 return isDropped();
726 }
727 public override Menu getMenu() {
728 return text.getMenu();
729 }
730 /**
731 * Returns a <code>Point</code> whose x coordinate is the start
732 * of the selection in the receiver's text field, and whose y
733 * coordinate is the end of the selection. The returned values
734 * are zero-relative. An "empty" selection as indicated by
735 * the the x and y coordinates having the same value.
736 *
737 * @return a point representing the selection start and end
738 *
739 * @exception DWTException <ul>
740 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
741 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
742 * </ul>
743 */
744 public Point getSelection () {
745 checkWidget ();
746 return text.getSelection ();
747 }
748 /**
749 * Returns the zero-relative index of the item which is currently
750 * selected in the receiver's list, or -1 if no item is selected.
751 *
752 * @return the index of the selected item
753 *
754 * @exception DWTException <ul>
755 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
756 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
757 * </ul>
758 */
759 public int getSelectionIndex () {
760 checkWidget ();
761 return list.getSelectionIndex ();
762 }
763 public override int getStyle () {
764 int style = super.getStyle ();
765 style &= ~DWT.READ_ONLY;
766 if (!text.getEditable()) style |= DWT.READ_ONLY;
767 return style;
768 }
769 /**
770 * Returns a string containing a copy of the contents of the
771 * receiver's text field.
772 *
773 * @return the receiver's text
774 *
775 * @exception DWTException <ul>
776 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
777 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
778 * </ul>
779 */
780 public String getText () {
781 checkWidget ();
782 return text.getText ();
783 }
784 /**
785 * Returns the height of the receivers's text field.
786 *
787 * @return the text height
788 *
789 * @exception DWTException <ul>
790 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
791 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
792 * </ul>
793 */
794 public int getTextHeight () {
795 checkWidget ();
796 return text.getLineHeight ();
797 }
798 /**
799 * Returns the maximum number of characters that the receiver's
800 * text field is capable of holding. If this has not been changed
801 * by <code>setTextLimit()</code>, it will be the constant
802 * <code>Combo.LIMIT</code>.
803 *
804 * @return the text limit
805 *
806 * @exception DWTException <ul>
807 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
808 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
809 * </ul>
810 */
811 public int getTextLimit () {
812 checkWidget ();
813 return text.getTextLimit ();
814 }
815 /**
816 * Gets the number of items that are visible in the drop
817 * down portion of the receiver's list.
818 *
819 * @return the number of items that are visible
820 *
821 * @exception DWTException <ul>
822 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
823 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
824 * </ul>
825 *
826 * @since 3.0
827 */
828 public int getVisibleItemCount () {
829 checkWidget ();
830 return visibleItemCount;
831 }
832 void handleFocus (int type) {
833 if (isDisposed ()) return;
834 switch (type) {
835 case DWT.FocusIn: {
836 if (hasFocus) return;
837 if (getEditable ()) text.selectAll ();
838 hasFocus = true;
839 Shell shell = getShell ();
840 shell.removeListener (DWT.Deactivate, listener);
841 shell.addListener (DWT.Deactivate, listener);
842 Display display = getDisplay ();
843 display.removeFilter (DWT.FocusIn, filter);
844 display.addFilter (DWT.FocusIn, filter);
845 Event e = new Event ();
846 notifyListeners (DWT.FocusIn, e);
847 break;
848 }
849 case DWT.FocusOut: {
850 if (!hasFocus) return;
851 Control focusControl = getDisplay ().getFocusControl ();
852 if (focusControl is arrow || focusControl is list || focusControl is text) return;
853 hasFocus = false;
854 Shell shell = getShell ();
855 shell.removeListener(DWT.Deactivate, listener);
856 Display display = getDisplay ();
857 display.removeFilter (DWT.FocusIn, filter);
858 Event e = new Event ();
859 notifyListeners (DWT.FocusOut, e);
860 break;
861 }
862 default:
863 }
864 }
865 /**
866 * Searches the receiver's list starting at the first item
867 * (index 0) until an item is found that is equal to the
868 * argument, and returns the index of that item. If no item
869 * is found, returns -1.
870 *
871 * @param string the search item
872 * @return the index of the item
873 *
874 * @exception DWTException <ul>
875 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
876 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
877 * </ul>
878 */
879 public int indexOf (String string) {
880 checkWidget ();
881 // DWT extension: allow null for zero length string
882 //if (string is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
883 return list.indexOf (string);
884 }
885 /**
886 * Searches the receiver's list starting at the given,
887 * zero-relative index until an item is found that is equal
888 * to the argument, and returns the index of that item. If
889 * no item is found or the starting index is out of range,
890 * returns -1.
891 *
892 * @param string the search item
893 * @param start the zero-relative index at which to begin the search
894 * @return the index of the item
895 *
896 * @exception DWTException <ul>
897 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
898 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
899 * </ul>
900 */
901 public int indexOf (String string, int start) {
902 checkWidget ();
903 // DWT extension: allow null for zero length string
904 //if (string is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
905 return list.indexOf (string, start);
906 }
907
908 void initAccessible() {
909 AccessibleAdapter accessibleAdapter = new class() AccessibleAdapter {
910 public void getName (AccessibleEvent e) {
911 String name = null;
912 Label label = getAssociatedLabel ();
913 if (label !is null) {
914 name = stripMnemonic (label.getText());
915 }
916 e.result = name;
917 }
918 public void getKeyboardShortcut(AccessibleEvent e) {
919 String shortcut = null;
920 Label label = getAssociatedLabel ();
921 if (label !is null) {
922 String text = label.getText ();
923 if (text !is null) {
924 dchar mnemonic = _findMnemonic (text);
925 if (mnemonic !is '\0') {
926 shortcut = tango.text.convert.Format.Format( "Alt+{}", mnemonic ); //$NON-NLS-1$
927 }
1152 } 928 }
1153 } 929 }
1154 930 e.result = shortcut;
1155 public void getLocation (AccessibleControlEvent e) 931 }
1156 { 932 public void getHelp (AccessibleEvent e) {
1157 Rectangle location = getBounds(); 933 e.result = getToolTipText ();
1158 Point pt = getParent().toDisplay(location.x, location.y); 934 }
1159 e.x = pt.x; 935 };
1160 e.y = pt.y; 936 getAccessible ().addAccessibleListener (accessibleAdapter);
1161 e.width = location.width; 937 text.getAccessible ().addAccessibleListener (accessibleAdapter);
1162 e.height = location.height; 938 list.getAccessible ().addAccessibleListener (accessibleAdapter);
1163 } 939
1164 940 arrow.getAccessible ().addAccessibleListener (new class() AccessibleAdapter {
1165 public void getChildCount (AccessibleControlEvent e) 941 public void getName (AccessibleEvent e) {
1166 { 942 e.result = isDropped () ? DWT.getMessage ("SWT_Close") : DWT.getMessage ("SWT_Open"); //$NON-NLS-1$ //$NON-NLS-2$
1167 e.detail = 0; 943 }
1168 } 944 public void getKeyboardShortcut (AccessibleEvent e) {
1169 945 e.result = "Alt+Down Arrow"; //$NON-NLS-1$
1170 public void getRole (AccessibleControlEvent e) 946 }
1171 { 947 public void getHelp (AccessibleEvent e) {
1172 e.detail = ACC.ROLE_COMBOBOX; 948 e.result = getToolTipText ();
1173 } 949 }
1174 950 });
1175 public void getState (AccessibleControlEvent e) 951
1176 { 952 getAccessible().addAccessibleTextListener (new class() AccessibleTextAdapter {
1177 e.detail = ACC.STATE_NORMAL; 953 public void getCaretOffset (AccessibleTextEvent e) {
1178 } 954 e.offset = text.getCaretPosition ();
1179 955 }
1180 public void getValue (AccessibleControlEvent e) 956 public void getSelectionRange(AccessibleTextEvent e) {
1181 { 957 Point sel = text.getSelection();
1182 e.result = getText(); 958 e.offset = sel.x;
1183 } 959 e.length = sel.y - sel.x;
1184 }); 960 }
1185 961 });
1186 text.getAccessible().addAccessibleControlListener(new class 962
1187 AccessibleControlAdapter 963 getAccessible().addAccessibleControlListener (new class() AccessibleControlAdapter {
1188 { 964 public void getChildAtPoint (AccessibleControlEvent e) {
1189 public void getRole (AccessibleControlEvent e) 965 Point testPoint = toControl (e.x, e.y);
1190 { 966 if (getBounds ().contains (testPoint)) {
1191 e.detail = text.getEditable() ? ACC.ROLE_TEXT : ACC.ROLE_LABEL; 967 e.childID = ACC.CHILDID_SELF;
1192 } 968 }
1193 }); 969 }
1194 970
1195 arrow.getAccessible().addAccessibleControlListener( 971 public void getLocation (AccessibleControlEvent e) {
1196 new class AccessibleControlAdapter 972 Rectangle location = getBounds ();
1197 { 973 Point pt = getParent().toDisplay (location.x, location.y);
1198 public void getDefaultAction (AccessibleControlEvent e) 974 e.x = pt.x;
1199 { 975 e.y = pt.y;
1200 e.result = isDropped() ? DWT.getMessage("DWT_Close") : DWT.getMessage( 976 e.width = location.width;
1201 "DWT_Open"); //$NON-NLS-1$ //$NON-NLS-2$ 977 e.height = location.height;
1202 } 978 }
1203 }); 979
980 public void getChildCount (AccessibleControlEvent e) {
981 e.detail = 0;
982 }
983
984 public void getRole (AccessibleControlEvent e) {
985 e.detail = ACC.ROLE_COMBOBOX;
986 }
987
988 public void getState (AccessibleControlEvent e) {
989 e.detail = ACC.STATE_NORMAL;
990 }
991
992 public void getValue (AccessibleControlEvent e) {
993 e.result = getText ();
994 }
995 });
996
997 text.getAccessible ().addAccessibleControlListener (new class() AccessibleControlAdapter {
998 public void getRole (AccessibleControlEvent e) {
999 e.detail = text.getEditable () ? ACC.ROLE_TEXT : ACC.ROLE_LABEL;
1000 }
1001 });
1002
1003 arrow.getAccessible ().addAccessibleControlListener (new class() AccessibleControlAdapter {
1004 public void getDefaultAction (AccessibleControlEvent e) {
1005 e.result = isDropped () ? DWT.getMessage ("SWT_Close") : DWT.getMessage ("SWT_Open"); //$NON-NLS-1$ //$NON-NLS-2$
1006 }
1007 });
1008 }
1009 bool isDropped () {
1010 return popup.getVisible ();
1011 }
1012 public override bool isFocusControl () {
1013 checkWidget();
1014 if (text.isFocusControl () || arrow.isFocusControl () || list.isFocusControl () || popup.isFocusControl ()) {
1015 return true;
1204 } 1016 }
1205 1017 return super.isFocusControl ();
1206 bool isDropped () 1018 }
1207 { 1019 void internalLayout (bool changed) {
1208 return popup.getVisible(); 1020 if (isDropped ()) dropDown (false);
1209 } 1021 Rectangle rect = getClientArea ();
1210 1022 int width = rect.width;
1211 public bool isFocusControl () 1023 int height = rect.height;
1212 { 1024 Point arrowSize = arrow.computeSize (DWT.DEFAULT, height, changed);
1213 checkWidget(); 1025 text.setBounds (0, 0, width - arrowSize.x, height);
1214 if (text.isFocusControl() || arrow.isFocusControl() || list.isFocusControl() || popup.isFocusControl()) 1026 arrow.setBounds (width - arrowSize.x, 0, arrowSize.x, arrowSize.y);
1215 { 1027 }
1216 return true; 1028 void listEvent (Event event) {
1217 } 1029 switch (event.type) {
1218 return super.isFocusControl(); 1030 case DWT.Dispose:
1219 } 1031 if (getShell () !is popup.getParent ()) {
1220 1032 String[] items = list.getItems ();
1221 void internalLayout (bool changed) 1033 int selectionIndex = list.getSelectionIndex ();
1222 { 1034 popup = null;
1223 if (isDropped()) 1035 list = null;
1224 dropDown(false); 1036 createPopup (items, selectionIndex);
1225 Rectangle rect = getClientArea(); 1037 }
1226 int width = rect.width; 1038 break;
1227 int height = rect.height; 1039 case DWT.FocusIn: {
1228 Point arrowSize = arrow.computeSize(DWT.DEFAULT, height, changed); 1040 handleFocus (DWT.FocusIn);
1229 text.setBounds(0, 0, width - arrowSize.x, height); 1041 break;
1230 arrow.setBounds(width - arrowSize.x, 0, arrowSize.x, arrowSize.y); 1042 }
1231 } 1043 case DWT.MouseUp: {
1232 1044 if (event.button !is 1) return;
1233 void listEvent (Event event) 1045 dropDown (false);
1234 { 1046 break;
1235 switch (event.type) 1047 }
1236 { 1048 case DWT.Selection: {
1237 case DWT.Dispose: 1049 int index = list.getSelectionIndex ();
1238 if (getShell() !is popup.getParent()) 1050 if (index is -1) return;
1239 { 1051 text.setText (list.getItem (index));
1240 String[] items = list.getItems(); 1052 text.selectAll ();
1241 int selectionIndex = list.getSelectionIndex(); 1053 list.setSelection (index);
1242 popup = null; 1054 Event e = new Event ();
1243 list = null; 1055 e.time = event.time;
1244 createPopup(items, selectionIndex); 1056 e.stateMask = event.stateMask;
1245 } 1057 e.doit = event.doit;
1246 break; 1058 notifyListeners (DWT.Selection, e);
1247 case DWT.FocusIn: 1059 event.doit = e.doit;
1248 { 1060 break;
1249 handleFocus(DWT.FocusIn); 1061 }
1250 break; 1062 case DWT.Traverse: {
1251 } 1063 switch (event.detail) {
1252 case DWT.MouseUp: 1064 case DWT.TRAVERSE_RETURN:
1253 { 1065 case DWT.TRAVERSE_ESCAPE:
1254 if (event.button !is 1) 1066 case DWT.TRAVERSE_ARROW_PREVIOUS:
1067 case DWT.TRAVERSE_ARROW_NEXT:
1068 event.doit = false;
1069 break;
1070 case DWT.TRAVERSE_TAB_NEXT:
1071 case DWT.TRAVERSE_TAB_PREVIOUS:
1072 event.doit = text.traverse(event.detail);
1073 event.detail = DWT.TRAVERSE_NONE;
1074 if (event.doit) dropDown(false);
1255 return; 1075 return;
1256 dropDown(false); 1076 default:
1257 break; 1077 }
1258 } 1078 Event e = new Event ();
1259 case DWT.Selection: 1079 e.time = event.time;
1260 { 1080 e.detail = event.detail;
1261 int index = list.getSelectionIndex(); 1081 e.doit = event.doit;
1262 if (index is -1) 1082 e.character = event.character;
1263 return; 1083 e.keyCode = event.keyCode;
1264 text.setText(list.getItem(index)); 1084 notifyListeners (DWT.Traverse, e);
1265 text.selectAll(); 1085 event.doit = e.doit;
1266 list.setSelection(index); 1086 event.detail = e.detail;
1267 Event e = new Event(); 1087 break;
1088 }
1089 case DWT.KeyUp: {
1090 Event e = new Event ();
1091 e.time = event.time;
1092 e.character = event.character;
1093 e.keyCode = event.keyCode;
1094 e.stateMask = event.stateMask;
1095 notifyListeners (DWT.KeyUp, e);
1096 break;
1097 }
1098 case DWT.KeyDown: {
1099 if (event.character is DWT.ESC) {
1100 // Escape key cancels popup list
1101 dropDown (false);
1102 }
1103 if ((event.stateMask & DWT.ALT) !is 0 && (event.keyCode is DWT.ARROW_UP || event.keyCode is DWT.ARROW_DOWN)) {
1104 dropDown (false);
1105 }
1106 if (event.character is DWT.CR) {
1107 // Enter causes default selection
1108 dropDown (false);
1109 Event e = new Event ();
1268 e.time = event.time; 1110 e.time = event.time;
1269 e.stateMask = event.stateMask; 1111 e.stateMask = event.stateMask;
1270 e.doit = event.doit; 1112 notifyListeners (DWT.DefaultSelection, e);
1271 notifyListeners(DWT.Selection, e); 1113 }
1272 event.doit = e.doit; 1114 // At this point the widget may have been disposed.
1273 break; 1115 // If so, do not continue.
1274 } 1116 if (isDisposed ()) break;
1275 case DWT.Traverse: 1117 Event e = new Event();
1276 { 1118 e.time = event.time;
1277 switch (event.detail) 1119 e.character = event.character;
1278 { 1120 e.keyCode = event.keyCode;
1279 case DWT.TRAVERSE_RETURN: 1121 e.stateMask = event.stateMask;
1280 case DWT.TRAVERSE_ESCAPE: 1122 notifyListeners(DWT.KeyDown, e);
1281 case DWT.TRAVERSE_ARROW_PREVIOUS: 1123 break;
1282 case DWT.TRAVERSE_ARROW_NEXT: 1124
1283 event.doit = false; 1125 }
1126 default:
1127 }
1128 }
1129 /**
1130 * Pastes text from clipboard.
1131 * <p>
1132 * The selected text is deleted from the widget
1133 * and new text inserted from the clipboard.
1134 * </p>
1135 *
1136 * @exception DWTException <ul>
1137 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1138 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1139 * </ul>
1140 *
1141 * @since 3.3
1142 */
1143 public void paste () {
1144 checkWidget ();
1145 text.paste ();
1146 }
1147 void popupEvent(Event event) {
1148 switch (event.type) {
1149 case DWT.Paint:
1150 // draw black rectangle around list
1151 Rectangle listRect = list.getBounds();
1152 Color black = getDisplay().getSystemColor(DWT.COLOR_BLACK);
1153 event.gc.setForeground(black);
1154 event.gc.drawRectangle(0, 0, listRect.width + 1, listRect.height + 1);
1155 break;
1156 case DWT.Close:
1157 event.doit = false;
1158 dropDown (false);
1159 break;
1160 case DWT.Deactivate:
1161 /*
1162 * Bug in GTK. When the arrow button is pressed the popup control receives a
1163 * deactivate event and then the arrow button receives a selection event. If
1164 * we hide the popup in the deactivate event, the selection event will show
1165 * it again. To prevent the popup from showing again, we will let the selection
1166 * event of the arrow button hide the popup.
1167 * In Windows, hiding the popup during the deactivate causes the deactivate
1168 * to be called twice and the selection event to be disappear.
1169 */
1170 if (!"carbon".equals(DWT.getPlatform())) {
1171 Point point = arrow.toControl(getDisplay().getCursorLocation());
1172 Point size = arrow.getSize();
1173 Rectangle rect = new Rectangle(0, 0, size.x, size.y);
1174 if (!rect.contains(point)) dropDown (false);
1175 } else {
1176 dropDown(false);
1177 }
1178 break;
1179 default:
1180 }
1181 }
1182 public override void redraw () {
1183 super.redraw();
1184 text.redraw();
1185 arrow.redraw();
1186 if (popup.isVisible()) list.redraw();
1187 }
1188 public override void redraw (int x, int y, int width, int height, bool all) {
1189 super.redraw(x, y, width, height, true);
1190 }
1191
1192 /**
1193 * Removes the item from the receiver's list at the given
1194 * zero-relative index.
1195 *
1196 * @param index the index for the item
1197 *
1198 * @exception IllegalArgumentException <ul>
1199 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1200 * </ul>
1201 * @exception DWTException <ul>
1202 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1203 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1204 * </ul>
1205 */
1206 public void remove (int index) {
1207 checkWidget();
1208 list.remove (index);
1209 }
1210 /**
1211 * Removes the items from the receiver's list which are
1212 * between the given zero-relative start and end
1213 * indices (inclusive).
1214 *
1215 * @param start the start of the range
1216 * @param end the end of the range
1217 *
1218 * @exception IllegalArgumentException <ul>
1219 * <li>ERROR_INVALID_RANGE - if either the start or end are not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1220 * </ul>
1221 * @exception DWTException <ul>
1222 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1223 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1224 * </ul>
1225 */
1226 public void remove (int start, int end) {
1227 checkWidget();
1228 list.remove (start, end);
1229 }
1230 /**
1231 * Searches the receiver's list starting at the first item
1232 * until an item is found that is equal to the argument,
1233 * and removes that item from the list.
1234 *
1235 * @param string the item to remove
1236 *
1237 * @exception IllegalArgumentException <ul>
1238 * <li>ERROR_INVALID_ARGUMENT - if the string is not found in the list</li>
1239 * </ul>
1240 * @exception DWTException <ul>
1241 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1242 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1243 * </ul>
1244 */
1245 public void remove (String string) {
1246 checkWidget();
1247 // DWT extension: allow null for zero length string
1248 //if (string is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
1249 list.remove (string);
1250 }
1251 /**
1252 * Removes all of the items from the receiver's list and clear the
1253 * contents of receiver's text field.
1254 * <p>
1255 * @exception DWTException <ul>
1256 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1257 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1258 * </ul>
1259 */
1260 public void removeAll () {
1261 checkWidget();
1262 text.setText (""); //$NON-NLS-1$
1263 list.removeAll ();
1264 }
1265 /**
1266 * Removes the listener from the collection of listeners who will
1267 * be notified when the receiver's text is modified.
1268 *
1269 * @param listener the listener which should no longer be notified
1270 *
1271 * @exception IllegalArgumentException <ul>
1272 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
1273 * </ul>
1274 * @exception DWTException <ul>
1275 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1276 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1277 * </ul>
1278 *
1279 * @see ModifyListener
1280 * @see #addModifyListener
1281 */
1282 public void removeModifyListener (ModifyListener listener) {
1283 checkWidget();
1284 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
1285 removeListener(DWT.Modify, listener);
1286 }
1287 /**
1288 * Removes the listener from the collection of listeners who will
1289 * be notified when the user changes the receiver's selection.
1290 *
1291 * @param listener the listener which should no longer be notified
1292 *
1293 * @exception IllegalArgumentException <ul>
1294 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
1295 * </ul>
1296 * @exception DWTException <ul>
1297 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1298 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1299 * </ul>
1300 *
1301 * @see SelectionListener
1302 * @see #addSelectionListener
1303 */
1304 public void removeSelectionListener (SelectionListener listener) {
1305 checkWidget();
1306 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
1307 removeListener(DWT.Selection, listener);
1308 removeListener(DWT.DefaultSelection,listener);
1309 }
1310 /**
1311 * Removes the listener from the collection of listeners who will
1312 * be notified when the control is verified.
1313 *
1314 * @param listener the listener which should no longer be notified
1315 *
1316 * @exception IllegalArgumentException <ul>
1317 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
1318 * </ul>
1319 * @exception DWTException <ul>
1320 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1321 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1322 * </ul>
1323 *
1324 * @see VerifyListener
1325 * @see #addVerifyListener
1326 *
1327 * @since 3.3
1328 */
1329 public void removeVerifyListener (VerifyListener listener) {
1330 checkWidget();
1331 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
1332 removeListener(DWT.Verify, listener);
1333 }
1334 /**
1335 * Selects the item at the given zero-relative index in the receiver's
1336 * list. If the item at the index was already selected, it remains
1337 * selected. Indices that are out of range are ignored.
1338 *
1339 * @param index the index of the item to select
1340 *
1341 * @exception DWTException <ul>
1342 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1343 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1344 * </ul>
1345 */
1346 public void select (int index) {
1347 checkWidget();
1348 if (index is -1) {
1349 list.deselectAll ();
1350 text.setText (""); //$NON-NLS-1$
1351 return;
1352 }
1353 if (0 <= index && index < list.getItemCount()) {
1354 if (index !is getSelectionIndex()) {
1355 text.setText (list.getItem (index));
1356 text.selectAll ();
1357 list.select (index);
1358 list.showSelection ();
1359 }
1360 }
1361 }
1362 public override void setBackground (Color color) {
1363 super.setBackground(color);
1364 background = color;
1365 if (text !is null) text.setBackground(color);
1366 if (list !is null) list.setBackground(color);
1367 if (arrow !is null) arrow.setBackground(color);
1368 }
1369 /**
1370 * Sets the editable state.
1371 *
1372 * @param editable the new editable state
1373 *
1374 * @exception DWTException <ul>
1375 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1376 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1377 * </ul>
1378 *
1379 * @since 3.0
1380 */
1381 public void setEditable (bool editable) {
1382 checkWidget ();
1383 text.setEditable(editable);
1384 }
1385 public override void setEnabled (bool enabled) {
1386 super.setEnabled(enabled);
1387 if (popup !is null) popup.setVisible (false);
1388 if (text !is null) text.setEnabled(enabled);
1389 if (arrow !is null) arrow.setEnabled(enabled);
1390 }
1391 public override bool setFocus () {
1392 checkWidget();
1393 if (!isEnabled () || !isVisible ()) return false;
1394 if (isFocusControl ()) return true;
1395 return text.setFocus ();
1396 }
1397 public override void setFont (Font font) {
1398 super.setFont (font);
1399 this.font = font;
1400 text.setFont (font);
1401 list.setFont (font);
1402 internalLayout (true);
1403 }
1404 public override void setForeground (Color color) {
1405 super.setForeground(color);
1406 foreground = color;
1407 if (text !is null) text.setForeground(color);
1408 if (list !is null) list.setForeground(color);
1409 if (arrow !is null) arrow.setForeground(color);
1410 }
1411 /**
1412 * Sets the text of the item in the receiver's list at the given
1413 * zero-relative index to the string argument. This is equivalent
1414 * to <code>remove</code>'ing the old item at the index, and then
1415 * <code>add</code>'ing the new item at that index.
1416 *
1417 * @param index the index for the item
1418 * @param string the new text for the item
1419 *
1420 * @exception IllegalArgumentException <ul>
1421 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1422 * </ul>
1423 * @exception DWTException <ul>
1424 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1425 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1426 * </ul>
1427 */
1428 public void setItem (int index, String string) {
1429 checkWidget();
1430 list.setItem (index, string);
1431 }
1432 /**
1433 * Sets the receiver's list to be the given array of items.
1434 *
1435 * @param items the array of items
1436 *
1437 * @exception IllegalArgumentException <ul>
1438 * <li>ERROR_INVALID_ARGUMENT - if an item in the items array is null</li>
1439 * </ul>
1440 * @exception DWTException <ul>
1441 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1442 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1443 * </ul>
1444 */
1445 public void setItems (String [] items) {
1446 checkWidget ();
1447 list.setItems (items);
1448 if (!text.getEditable ()) text.setText (""); //$NON-NLS-1$
1449 }
1450 /**
1451 * Sets the layout which is associated with the receiver to be
1452 * the argument which may be null.
1453 * <p>
1454 * Note: No Layout can be set on this Control because it already
1455 * manages the size and position of its children.
1456 * </p>
1457 *
1458 * @param layout the receiver's new layout or null
1459 *
1460 * @exception DWTException <ul>
1461 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1462 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1463 * </ul>
1464 */
1465 public override void setLayout (Layout layout) {
1466 checkWidget ();
1467 return;
1468 }
1469 /**
1470 * Marks the receiver's list as visible if the argument is <code>true</code>,
1471 * and marks it invisible otherwise.
1472 * <p>
1473 * If one of the receiver's ancestors is not visible or some
1474 * other condition makes the receiver not visible, marking
1475 * it visible may not actually cause it to be displayed.
1476 * </p>
1477 *
1478 * @param visible the new visibility state
1479 *
1480 * @exception DWTException <ul>
1481 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1482 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1483 * </ul>
1484 *
1485 * @since 3.4
1486 */
1487 public void setListVisible (bool visible) {
1488 checkWidget ();
1489 dropDown(visible);
1490 }
1491 public override void setMenu(Menu menu) {
1492 text.setMenu(menu);
1493 }
1494 /**
1495 * Sets the selection in the receiver's text field to the
1496 * range specified by the argument whose x coordinate is the
1497 * start of the selection and whose y coordinate is the end
1498 * of the selection.
1499 *
1500 * @param selection a point representing the new selection start and end
1501 *
1502 * @exception IllegalArgumentException <ul>
1503 * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
1504 * </ul>
1505 * @exception DWTException <ul>
1506 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1507 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1508 * </ul>
1509 */
1510 public void setSelection (Point selection) {
1511 checkWidget();
1512 if (selection is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
1513 text.setSelection (selection.x, selection.y);
1514 }
1515
1516 /**
1517 * Sets the contents of the receiver's text field to the
1518 * given string.
1519 * <p>
1520 * Note: The text field in a <code>Combo</code> is typically
1521 * only capable of displaying a single line of text. Thus,
1522 * setting the text to a string containing line breaks or
1523 * other special characters will probably cause it to
1524 * display incorrectly.
1525 * </p>
1526 *
1527 * @param string the new text
1528 *
1529 * @exception DWTException <ul>
1530 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1531 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1532 * </ul>
1533 */
1534 public void setText (String string) {
1535 checkWidget();
1536 // DWT extension: allow null for zero length string
1537 //if (string is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
1538 int index = list.indexOf (string);
1539 if (index is -1) {
1540 list.deselectAll ();
1541 text.setText (string);
1542 return;
1543 }
1544 text.setText (string);
1545 text.selectAll ();
1546 list.setSelection (index);
1547 list.showSelection ();
1548 }
1549 /**
1550 * Sets the maximum number of characters that the receiver's
1551 * text field is capable of holding to be the argument.
1552 *
1553 * @param limit new text limit
1554 *
1555 * @exception IllegalArgumentException <ul>
1556 * <li>ERROR_CANNOT_BE_ZERO - if the limit is zero</li>
1557 * </ul>
1558 * @exception DWTException <ul>
1559 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1560 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1561 * </ul>
1562 */
1563 public void setTextLimit (int limit) {
1564 checkWidget();
1565 text.setTextLimit (limit);
1566 }
1567
1568 public override void setToolTipText (String string) {
1569 checkWidget();
1570 super.setToolTipText(string);
1571 arrow.setToolTipText (string);
1572 text.setToolTipText (string);
1573 }
1574
1575 public override void setVisible (bool visible) {
1576 super.setVisible(visible);
1577 /*
1578 * At this point the widget may have been disposed in a FocusOut event.
1579 * If so then do not continue.
1580 */
1581 if (isDisposed ()) return;
1582 // TEMPORARY CODE
1583 if (popup is null || popup.isDisposed ()) return;
1584 if (!visible) popup.setVisible (false);
1585 }
1586 /**
1587 * Sets the number of items that are visible in the drop
1588 * down portion of the receiver's list.
1589 *
1590 * @param count the new number of items to be visible
1591 *
1592 * @exception DWTException <ul>
1593 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1594 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1595 * </ul>
1596 *
1597 * @since 3.0
1598 */
1599 public void setVisibleItemCount (int count) {
1600 checkWidget ();
1601 if (count < 0) return;
1602 visibleItemCount = count;
1603 }
1604 String stripMnemonic (String string) {
1605 int index = 0;
1606 int length_ = string.length;
1607 do {
1608 while ((index < length_) && (string[index] !is '&')) index++;
1609 if (++index >= length_) return string;
1610 if (string[index] !is '&') {
1611 return string[0 .. index-1] ~ string[index .. length_];
1612 }
1613 index++;
1614 } while (index < length_);
1615 return string;
1616 }
1617 void textEvent (Event event) {
1618 switch (event.type) {
1619 case DWT.FocusIn: {
1620 handleFocus (DWT.FocusIn);
1621 break;
1622 }
1623 case DWT.DefaultSelection: {
1624 dropDown (false);
1625 Event e = new Event ();
1626 e.time = event.time;
1627 e.stateMask = event.stateMask;
1628 notifyListeners (DWT.DefaultSelection, e);
1629 break;
1630 }
1631 case DWT.KeyDown: {
1632 Event keyEvent = new Event ();
1633 keyEvent.time = event.time;
1634 keyEvent.character = event.character;
1635 keyEvent.keyCode = event.keyCode;
1636 keyEvent.stateMask = event.stateMask;
1637 notifyListeners (DWT.KeyDown, keyEvent);
1638 if (isDisposed ()) break;
1639 event.doit = keyEvent.doit;
1640 if (!event.doit) break;
1641 if (event.keyCode is DWT.ARROW_UP || event.keyCode is DWT.ARROW_DOWN) {
1642 event.doit = false;
1643 if ((event.stateMask & DWT.ALT) !is 0) {
1644 bool dropped = isDropped ();
1645 text.selectAll ();
1646 if (!dropped) setFocus ();
1647 dropDown (!dropped);
1284 break; 1648 break;
1285 case DWT.TRAVERSE_TAB_NEXT:
1286 case DWT.TRAVERSE_TAB_PREVIOUS:
1287 event.doit = text.traverse(event.detail);
1288 event.detail = DWT.TRAVERSE_NONE;
1289 if (event.doit)
1290 dropDown(false);
1291 return;
1292 } 1649 }
1293 Event e = new Event(); 1650
1294 e.time = event.time; 1651 int oldIndex = getSelectionIndex ();
1295 e.detail = event.detail; 1652 if (event.keyCode is DWT.ARROW_UP) {
1296 e.doit = event.doit; 1653 select (Math.max (oldIndex - 1, 0));
1297 e.character = event.character; 1654 } else {
1298 e.keyCode = event.keyCode; 1655 select (Math.min (oldIndex + 1, getItemCount () - 1));
1299 notifyListeners(DWT.Traverse, e);
1300 event.doit = e.doit;
1301 event.detail = e.detail;
1302 break;
1303 }
1304 case DWT.KeyUp:
1305 {
1306 Event e = new Event();
1307 e.time = event.time;
1308 e.character = event.character;
1309 e.keyCode = event.keyCode;
1310 e.stateMask = event.stateMask;
1311 notifyListeners(DWT.KeyUp, e);
1312 break;
1313 }
1314 case DWT.KeyDown:
1315 {
1316 if (event.character is DWT.ESC)
1317 {
1318 // Escape key cancels popup list
1319 dropDown(false);
1320 } 1656 }
1321 if ((event.stateMask & DWT.ALT) !is 0 && (event.keyCode is DWT.ARROW_UP || event.keyCode is DWT.ARROW_DOWN)) 1657 if (oldIndex !is getSelectionIndex ()) {
1322 {
1323 dropDown(false);
1324 }
1325 if (event.character is DWT.CR)
1326 {
1327 // Enter causes default selection
1328 dropDown(false);
1329 Event e = new Event(); 1658 Event e = new Event();
1330 e.time = event.time; 1659 e.time = event.time;
1331 e.stateMask = event.stateMask; 1660 e.stateMask = event.stateMask;
1332 notifyListeners(DWT.DefaultSelection, e); 1661 notifyListeners (DWT.Selection, e);
1333 } 1662 }
1334 // At this point the widget may have been disposed. 1663 if (isDisposed ()) break;
1335 // If so, do not continue. 1664 }
1336 if (isDisposed()) 1665
1666 // Further work : Need to add support for incremental search in
1667 // pop up list as characters typed in text widget
1668 break;
1669 }
1670 case DWT.KeyUp: {
1671 Event e = new Event ();
1672 e.time = event.time;
1673 e.character = event.character;
1674 e.keyCode = event.keyCode;
1675 e.stateMask = event.stateMask;
1676 notifyListeners (DWT.KeyUp, e);
1677 event.doit = e.doit;
1678 break;
1679 }
1680 case DWT.MenuDetect: {
1681 Event e = new Event ();
1682 e.time = event.time;
1683 notifyListeners (DWT.MenuDetect, e);
1684 break;
1685 }
1686 case DWT.Modify: {
1687 list.deselectAll ();
1688 Event e = new Event ();
1689 e.time = event.time;
1690 notifyListeners (DWT.Modify, e);
1691 break;
1692 }
1693 case DWT.MouseDown: {
1694 Event mouseEvent = new Event ();
1695 mouseEvent.button = event.button;
1696 mouseEvent.count = event.count;
1697 mouseEvent.stateMask = event.stateMask;
1698 mouseEvent.time = event.time;
1699 mouseEvent.x = event.x; mouseEvent.y = event.y;
1700 notifyListeners (DWT.MouseDown, mouseEvent);
1701 if (isDisposed ()) break;
1702 event.doit = mouseEvent.doit;
1703 if (!event.doit) break;
1704 if (event.button !is 1) return;
1705 if (text.getEditable ()) return;
1706 bool dropped = isDropped ();
1707 text.selectAll ();
1708 if (!dropped) setFocus ();
1709 dropDown (!dropped);
1710 break;
1711 }
1712 case DWT.MouseUp: {
1713 Event mouseEvent = new Event ();
1714 mouseEvent.button = event.button;
1715 mouseEvent.count = event.count;
1716 mouseEvent.stateMask = event.stateMask;
1717 mouseEvent.time = event.time;
1718 mouseEvent.x = event.x; mouseEvent.y = event.y;
1719 notifyListeners (DWT.MouseUp, mouseEvent);
1720 if (isDisposed ()) break;
1721 event.doit = mouseEvent.doit;
1722 if (!event.doit) break;
1723 if (event.button !is 1) return;
1724 if (text.getEditable ()) return;
1725 text.selectAll ();
1726 break;
1727 }
1728 case DWT.MouseDoubleClick: {
1729 Event mouseEvent = new Event ();
1730 mouseEvent.button = event.button;
1731 mouseEvent.count = event.count;
1732 mouseEvent.stateMask = event.stateMask;
1733 mouseEvent.time = event.time;
1734 mouseEvent.x = event.x; mouseEvent.y = event.y;
1735 notifyListeners (DWT.MouseDoubleClick, mouseEvent);
1736 break;
1737 }
1738 case DWT.MouseWheel: {
1739 Event keyEvent = new Event ();
1740 keyEvent.time = event.time;
1741 keyEvent.keyCode = event.count > 0 ? DWT.ARROW_UP : DWT.ARROW_DOWN;
1742 keyEvent.stateMask = event.stateMask;
1743 notifyListeners (DWT.KeyDown, keyEvent);
1744 if (isDisposed ()) break;
1745 event.doit = keyEvent.doit;
1746 if (!event.doit) break;
1747 if (event.count !is 0) {
1748 event.doit = false;
1749 int oldIndex = getSelectionIndex ();
1750 if (event.count > 0) {
1751 select (Math.max (oldIndex - 1, 0));
1752 } else {
1753 select (Math.min (oldIndex + 1, getItemCount () - 1));
1754 }
1755 if (oldIndex !is getSelectionIndex ()) {
1756 Event e = new Event();
1757 e.time = event.time;
1758 e.stateMask = event.stateMask;
1759 notifyListeners (DWT.Selection, e);
1760 }
1761 if (isDisposed ()) break;
1762 }
1763 break;
1764 }
1765 case DWT.Traverse: {
1766 switch (event.detail) {
1767 case DWT.TRAVERSE_ARROW_PREVIOUS:
1768 case DWT.TRAVERSE_ARROW_NEXT:
1769 // The enter causes default selection and
1770 // the arrow keys are used to manipulate the list contents so
1771 // do not use them for traversal.
1772 event.doit = false;
1337 break; 1773 break;
1338 Event e = new Event(); 1774 case DWT.TRAVERSE_TAB_PREVIOUS:
1339 e.time = event.time; 1775 event.doit = traverse(DWT.TRAVERSE_TAB_PREVIOUS);
1340 e.character = event.character; 1776 event.detail = DWT.TRAVERSE_NONE;
1341 e.keyCode = event.keyCode; 1777 return;
1342 e.stateMask = event.stateMask; 1778 default:
1343 notifyListeners(DWT.KeyDown, e); 1779 }
1344 break; 1780 Event e = new Event ();
1345 1781 e.time = event.time;
1346 } 1782 e.detail = event.detail;
1347 1783 e.doit = event.doit;
1348 default: 1784 e.character = event.character;
1349 break; 1785 e.keyCode = event.keyCode;
1350 } 1786 notifyListeners (DWT.Traverse, e);
1787 event.doit = e.doit;
1788 event.detail = e.detail;
1789 break;
1790 }
1791 case DWT.Verify: {
1792 Event e = new Event ();
1793 e.text = event.text;
1794 e.start = event.start;
1795 e.end = event.end;
1796 e.character = event.character;
1797 e.keyCode = event.keyCode;
1798 e.stateMask = event.stateMask;
1799 notifyListeners (DWT.Verify, e);
1800 event.doit = e.doit;
1801 break;
1802 }
1803 default:
1351 } 1804 }
1352 1805 }
1353 /** 1806 }
1354 * Pastes text from clipboard.
1355 * <p>
1356 * The selected text is deleted from the widget
1357 * and new text inserted from the clipboard.
1358 * </p>
1359 *
1360 * @exception DWTException <ul>
1361 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1362 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1363 * </ul>
1364 *
1365 * @since 3.3
1366 */
1367 public void paste ()
1368 {
1369 checkWidget();
1370 text.paste();
1371 }
1372
1373 void popupEvent (Event event)
1374 {
1375 switch (event.type)
1376 {
1377 case DWT.Paint:
1378 // draw black rectangle around list
1379 Rectangle listRect = list.getBounds();
1380 Color black = getDisplay().getSystemColor(DWT.COLOR_BLACK);
1381 event.gc.setForeground(black);
1382 event.gc.drawRectangle(0, 0, listRect.width + 1,
1383 listRect.height + 1);
1384 break;
1385 case DWT.Close:
1386 event.doit = false;
1387 dropDown(false);
1388 break;
1389 case DWT.Deactivate:
1390 /*
1391 * Bug in GTK. When the arrow button is pressed the popup control receives a
1392 * deactivate event and then the arrow button receives a selection event. If
1393 * we hide the popup in the deactivate event, the selection event will show
1394 * it again. To prevent the popup from showing again, we will let the selection
1395 * event of the arrow button hide the popup.
1396 * In Windows, hiding the popup during the deactivate causes the deactivate
1397 * to be called twice and the selection event to be disappear.
1398 */
1399 if (!"carbon".opEquals(DWT.getPlatform()))
1400 {
1401 Point point = arrow.toControl(
1402 getDisplay().getCursorLocation());
1403 Point size = arrow.getSize();
1404 Rectangle rect = new Rectangle(0, 0, size.x, size.y);
1405 if (!rect.contains(point))
1406 dropDown(false);
1407 }
1408 else
1409 {
1410 dropDown(false);
1411 }
1412 break;
1413
1414 default:
1415 break;
1416 }
1417 }
1418
1419 public void redraw ()
1420 {
1421 super.redraw();
1422 text.redraw();
1423 arrow.redraw();
1424 if (popup.isVisible())
1425 list.redraw();
1426 }
1427
1428 public void redraw (int x, int y, int width, int height, bool all)
1429 {
1430 super.redraw(x, y, width, height, true);
1431 }
1432
1433 /**
1434 * Removes the item from the receiver's list at the given
1435 * zero-relative index.
1436 *
1437 * @param index the index for the item
1438 *
1439 * @exception IllegalArgumentException <ul>
1440 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1441 * </ul>
1442 * @exception DWTException <ul>
1443 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1444 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1445 * </ul>
1446 */
1447 public void remove (int index)
1448 {
1449 checkWidget();
1450 list.remove(index);
1451 }
1452
1453 /**
1454 * Removes the items from the receiver's list which are
1455 * between the given zero-relative start and end
1456 * indices (inclusive).
1457 *
1458 * @param start the start of the range
1459 * @param end the end of the range
1460 *
1461 * @exception IllegalArgumentException <ul>
1462 * <li>ERROR_INVALID_RANGE - if either the start or end are not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1463 * </ul>
1464 * @exception DWTException <ul>
1465 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1466 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1467 * </ul>
1468 */
1469 public void remove (int start, int end)
1470 {
1471 checkWidget();
1472 list.remove(start, end);
1473 }
1474
1475 /**
1476 * Searches the receiver's list starting at the first item
1477 * until an item is found that is equal to the argument,
1478 * and removes that item from the list.
1479 *
1480 * @param String the item to remove
1481 *
1482 * @exception IllegalArgumentException <ul>
1483 * <li>ERROR_NULL_ARGUMENT - if the String is null</li>
1484 * <li>ERROR_INVALID_ARGUMENT - if the String is not found in the list</li>
1485 * </ul>
1486 * @exception DWTException <ul>
1487 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1488 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1489 * </ul>
1490 */
1491 public void remove (String String)
1492 {
1493 checkWidget();
1494 if (String is null)
1495 DWT.error(DWT.ERROR_NULL_ARGUMENT);
1496 list.remove(String);
1497 }
1498
1499 /**
1500 * Removes all of the items from the receiver's list and clear the
1501 * contents of receiver's text field.
1502 * <p>
1503 * @exception DWTException <ul>
1504 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1505 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1506 * </ul>
1507 */
1508 public void removeAll ()
1509 {
1510 checkWidget();
1511 text.setText(""); //$NON-NLS-1$
1512 list.removeAll();
1513 }
1514
1515 /**
1516 * Removes the listener from the collection of listeners who will
1517 * be notified when the receiver's text is modified.
1518 *
1519 * @param listener the listener which should no longer be notified
1520 *
1521 * @exception IllegalArgumentException <ul>
1522 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
1523 * </ul>
1524 * @exception DWTException <ul>
1525 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1526 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1527 * </ul>
1528 *
1529 * @see ModifyListener
1530 * @see #addModifyListener
1531 */
1532 public void removeModifyListener (ModifyListener listener)
1533 {
1534 checkWidget();
1535 if (listener is null)
1536 DWT.error(DWT.ERROR_NULL_ARGUMENT);
1537 removeListener(DWT.Modify, listener);
1538 }
1539
1540 /**
1541 * Removes the listener from the collection of listeners who will
1542 * be notified when the user changes the receiver's selection.
1543 *
1544 * @param listener the listener which should no longer be notified
1545 *
1546 * @exception IllegalArgumentException <ul>
1547 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
1548 * </ul>
1549 * @exception DWTException <ul>
1550 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1551 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1552 * </ul>
1553 *
1554 * @see SelectionListener
1555 * @see #addSelectionListener
1556 */
1557 public void removeSelectionListener (SelectionListener listener)
1558 {
1559 checkWidget();
1560 if (listener is null)
1561 DWT.error(DWT.ERROR_NULL_ARGUMENT);
1562 removeListener(DWT.Selection, listener);
1563 removeListener(DWT.DefaultSelection, listener);
1564 }
1565
1566 /**
1567 * Removes the listener from the collection of listeners who will
1568 * be notified when the control is verified.
1569 *
1570 * @param listener the listener which should no longer be notified
1571 *
1572 * @exception IllegalArgumentException <ul>
1573 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
1574 * </ul>
1575 * @exception DWTException <ul>
1576 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1577 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1578 * </ul>
1579 *
1580 * @see VerifyListener
1581 * @see #addVerifyListener
1582 *
1583 * @since 3.3
1584 */
1585 public void removeVerifyListener (VerifyListener listener)
1586 {
1587 checkWidget();
1588 if (listener is null)
1589 DWT.error(DWT.ERROR_NULL_ARGUMENT);
1590 removeListener(DWT.Verify, listener);
1591 }
1592
1593 /**
1594 * Selects the item at the given zero-relative index in the receiver's
1595 * list. If the item at the index was already selected, it remains
1596 * selected. Indices that are out of range are ignored.
1597 *
1598 * @param index the index of the item to select
1599 *
1600 * @exception DWTException <ul>
1601 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1602 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1603 * </ul>
1604 */
1605 public void select (int index)
1606 {
1607 checkWidget();
1608 if (index is -1)
1609 {
1610 list.deselectAll();
1611 text.setText(""); //$NON-NLS-1$
1612 return;
1613 }
1614 if (0 <= index && index < list.getItemCount())
1615 {
1616 if (index !is getSelectionIndex())
1617 {
1618 text.setText(list.getItem(index));
1619 text.selectAll();
1620 list.select(index);
1621 list.showSelection();
1622 }
1623 }
1624 }
1625
1626 public void setBackground (Color color)
1627 {
1628 super.setBackground(color);
1629 background = color;
1630 if (text !is null)
1631 text.setBackground(color);
1632 if (list !is null)
1633 list.setBackground(color);
1634 if (arrow !is null)
1635 arrow.setBackground(color);
1636 }
1637
1638 /**
1639 * Sets the editable state.
1640 *
1641 * @param editable the new editable state
1642 *
1643 * @exception DWTException <ul>
1644 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1645 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1646 * </ul>
1647 *
1648 * @since 3.0
1649 */
1650 public void setEditable (bool editable)
1651 {
1652 checkWidget();
1653 text.setEditable(editable);
1654 }
1655
1656 public void setEnabled (bool enabled)
1657 {
1658 super.setEnabled(enabled);
1659 if (popup !is null)
1660 popup.setVisible(false);
1661 if (text !is null)
1662 text.setEnabled(enabled);
1663 if (arrow !is null)
1664 arrow.setEnabled(enabled);
1665 }
1666
1667 public bool setFocus ()
1668 {
1669 checkWidget();
1670 if (!isEnabled() || !isVisible())
1671 return false;
1672 if (isFocusControl())
1673 return true;
1674 return text.setFocus();
1675 }
1676
1677 public void setFont (Font font)
1678 {
1679 super.setFont(font);
1680 this.font = font;
1681 text.setFont(font);
1682 list.setFont(font);
1683 internalLayout(true);
1684 }
1685
1686 public void setForeground (Color color)
1687 {
1688 super.setForeground(color);
1689 foreground = color;
1690 if (text !is null)
1691 text.setForeground(color);
1692 if (list !is null)
1693 list.setForeground(color);
1694 if (arrow !is null)
1695 arrow.setForeground(color);
1696 }
1697
1698 /**
1699 * Sets the text of the item in the receiver's list at the given
1700 * zero-relative index to the String argument. This is equivalent
1701 * to <code>remove</code>'ing the old item at the index, and then
1702 * <code>add</code>'ing the new item at that index.
1703 *
1704 * @param index the index for the item
1705 * @param String the new text for the item
1706 *
1707 * @exception IllegalArgumentException <ul>
1708 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
1709 * <li>ERROR_NULL_ARGUMENT - if the String is null</li>
1710 * </ul>
1711 * @exception DWTException <ul>
1712 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1713 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1714 * </ul>
1715 */
1716 public void setItem (int index, String String)
1717 {
1718 checkWidget();
1719 list.setItem(index, String);
1720 }
1721
1722 /**
1723 * Sets the receiver's list to be the given array of items.
1724 *
1725 * @param items the array of items
1726 *
1727 * @exception IllegalArgumentException <ul>
1728 * <li>ERROR_NULL_ARGUMENT - if the items array is null</li>
1729 * <li>ERROR_INVALID_ARGUMENT - if an item in the items array is null</li>
1730 * </ul>
1731 * @exception DWTException <ul>
1732 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1733 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1734 * </ul>
1735 */
1736 public void setItems (String[] items)
1737 {
1738 checkWidget();
1739 list.setItems(items);
1740 if (!text.getEditable())
1741 text.setText(""); //$NON-NLS-1$
1742 }
1743
1744 /**
1745 * Sets the layout which is associated with the receiver to be
1746 * the argument which may be null.
1747 * <p>
1748 * Note: No Layout can be set on this Control because it already
1749 * manages the size and position of its children.
1750 * </p>
1751 *
1752 * @param layout the receiver's new layout or null
1753 *
1754 * @exception DWTException <ul>
1755 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1756 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1757 * </ul>
1758 */
1759 public void setLayout (Layout layout)
1760 {
1761 checkWidget();
1762 return;
1763 }
1764
1765 /**
1766 * Marks the receiver's list as visible if the argument is <code>true</code>,
1767 * and marks it invisible otherwise.
1768 * <p>
1769 * If one of the receiver's ancestors is not visible or some
1770 * other condition makes the receiver not visible, marking
1771 * it visible may not actually cause it to be displayed.
1772 * </p>
1773 *
1774 * @param visible the new visibility state
1775 *
1776 * @exception DWTException <ul>
1777 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1778 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1779 * </ul>
1780 *
1781 * @since 3.4
1782 */
1783 public void setListVisible (bool visible)
1784 {
1785 checkWidget();
1786 dropDown(visible);
1787 }
1788
1789 public void setMenu (Menu menu)
1790 {
1791 text.setMenu(menu);
1792 }
1793
1794 /**
1795 * Sets the selection in the receiver's text field to the
1796 * range specified by the argument whose x coordinate is the
1797 * start of the selection and whose y coordinate is the end
1798 * of the selection.
1799 *
1800 * @param selection a point representing the new selection start and end
1801 *
1802 * @exception IllegalArgumentException <ul>
1803 * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
1804 * </ul>
1805 * @exception DWTException <ul>
1806 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1807 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1808 * </ul>
1809 */
1810 public void setSelection (Point selection)
1811 {
1812 checkWidget();
1813 if (selection is null)
1814 DWT.error(DWT.ERROR_NULL_ARGUMENT);
1815 text.setSelection(selection.x, selection.y);
1816 }
1817
1818 /**
1819 * Sets the contents of the receiver's text field to the
1820 * given String.
1821 * <p>
1822 * Note: The text field in a <code>Combo</code> is typically
1823 * only capable of displaying a single line of text. Thus,
1824 * setting the text to a String containing line breaks or
1825 * other special characters will probably cause it to
1826 * display incorrectly.
1827 * </p>
1828 *
1829 * @param String the new text
1830 *
1831 * @exception IllegalArgumentException <ul>
1832 * <li>ERROR_NULL_ARGUMENT - if the String is null</li>
1833 * </ul>
1834 * @exception DWTException <ul>
1835 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1836 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1837 * </ul>
1838 */
1839 public void setText (String String)
1840 {
1841 checkWidget();
1842 if (String is null)
1843 DWT.error(DWT.ERROR_NULL_ARGUMENT);
1844 int index = list.indexOf(String);
1845 if (index is -1)
1846 {
1847 list.deselectAll();
1848 text.setText(String);
1849 return;
1850 }
1851 text.setText(String);
1852 text.selectAll();
1853 list.setSelection(index);
1854 list.showSelection();
1855 }
1856
1857 /**
1858 * Sets the maximum number of characters that the receiver's
1859 * text field is capable of holding to be the argument.
1860 *
1861 * @param limit new text limit
1862 *
1863 * @exception IllegalArgumentException <ul>
1864 * <li>ERROR_CANNOT_BE_ZERO - if the limit is zero</li>
1865 * </ul>
1866 * @exception DWTException <ul>
1867 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1868 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1869 * </ul>
1870 */
1871 public void setTextLimit (int limit)
1872 {
1873 checkWidget();
1874 text.setTextLimit(limit);
1875 }
1876
1877 public void setToolTipText (String String)
1878 {
1879 checkWidget();
1880 super.setToolTipText(String);
1881 arrow.setToolTipText(String);
1882 text.setToolTipText(String);
1883 }
1884
1885 public void setVisible (bool visible)
1886 {
1887 super.setVisible(visible);
1888 /*
1889 * At this point the widget may have been disposed in a FocusOut event.
1890 * If so then do not continue.
1891 */
1892 if (isDisposed())
1893 return;
1894 if (!visible)
1895 popup.setVisible(false);
1896 }
1897
1898 /**
1899 * Sets the number of items that are visible in the drop
1900 * down portion of the receiver's list.
1901 *
1902 * @param count the new number of items to be visible
1903 *
1904 * @exception DWTException <ul>
1905 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1906 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1907 * </ul>
1908 *
1909 * @since 3.0
1910 */
1911 public void setVisibleItemCount (int count)
1912 {
1913 checkWidget();
1914 if (count < 0)
1915 return;
1916 visibleItemCount = count;
1917 }
1918
1919 String stripMnemonic (String String)
1920 {
1921 int index = 0;
1922 int length = String.length();
1923 do
1924 {
1925 while ((index < length) && (String.charAt(index) !is '&'))
1926 index++;
1927 if (++index >= length)
1928 return String;
1929 if (String.charAt(index) !is '&')
1930 {
1931 return String.substring(0, index - 1) + String.substring(index,
1932 length);
1933 }
1934 index++;
1935 } while (index < length);
1936 return String;
1937 }
1938
1939 void textEvent (Event event)
1940 {
1941 switch (event.type)
1942 {
1943 case DWT.FocusIn:
1944 {
1945 handleFocus(DWT.FocusIn);
1946 break;
1947 }
1948 case DWT.DefaultSelection:
1949 {
1950 dropDown(false);
1951 Event e = new Event();
1952 e.time = event.time;
1953 e.stateMask = event.stateMask;
1954 notifyListeners(DWT.DefaultSelection, e);
1955 break;
1956 }
1957 case DWT.KeyDown:
1958 {
1959 Event keyEvent = new Event();
1960 keyEvent.time = event.time;
1961 keyEvent.character = event.character;
1962 keyEvent.keyCode = event.keyCode;
1963 keyEvent.stateMask = event.stateMask;
1964 notifyListeners(DWT.KeyDown, keyEvent);
1965 if (isDisposed())
1966 break;
1967 event.doit = keyEvent.doit;
1968 if (!event.doit)
1969 break;
1970 if (event.keyCode is DWT.ARROW_UP || event.keyCode is DWT.ARROW_DOWN)
1971 {
1972 event.doit = false;
1973 if ((event.stateMask & DWT.ALT) !is 0)
1974 {
1975 bool dropped = isDropped();
1976 text.selectAll();
1977 if (!dropped)
1978 setFocus();
1979 dropDown(!dropped);
1980 break;
1981 }
1982
1983 int oldIndex = getSelectionIndex();
1984 if (event.keyCode is DWT.ARROW_UP)
1985 {
1986 select(Math.max(oldIndex - 1, 0));
1987 }
1988 else
1989 {
1990 select(Math.min(oldIndex + 1, getItemCount() - 1));
1991 }
1992 if (oldIndex !is getSelectionIndex())
1993 {
1994 Event e = new Event();
1995 e.time = event.time;
1996 e.stateMask = event.stateMask;
1997 notifyListeners(DWT.Selection, e);
1998 }
1999 if (isDisposed())
2000 break;
2001 }
2002
2003 // Further work : Need to add support for incremental search in
2004 // pop up list as characters typed in text widget
2005 break;
2006 }
2007 case DWT.KeyUp:
2008 {
2009 Event e = new Event();
2010 e.time = event.time;
2011 e.character = event.character;
2012 e.keyCode = event.keyCode;
2013 e.stateMask = event.stateMask;
2014 notifyListeners(DWT.KeyUp, e);
2015 event.doit = e.doit;
2016 break;
2017 }
2018 case DWT.MenuDetect:
2019 {
2020 Event e = new Event();
2021 e.time = event.time;
2022 notifyListeners(DWT.MenuDetect, e);
2023 break;
2024 }
2025 case DWT.Modify:
2026 {
2027 list.deselectAll();
2028 Event e = new Event();
2029 e.time = event.time;
2030 notifyListeners(DWT.Modify, e);
2031 break;
2032 }
2033 case DWT.MouseDown:
2034 {
2035 Event mouseEvent = new Event();
2036 mouseEvent.button = event.button;
2037 mouseEvent.count = event.count;
2038 mouseEvent.stateMask = event.stateMask;
2039 mouseEvent.time = event.time;
2040 mouseEvent.x = event.x;
2041 mouseEvent.y = event.y;
2042 notifyListeners(DWT.MouseDown, mouseEvent);
2043 if (isDisposed())
2044 break;
2045 event.doit = mouseEvent.doit;
2046 if (!event.doit)
2047 break;
2048 if (event.button !is 1)
2049 return;
2050 if (text.getEditable())
2051 return;
2052 bool dropped = isDropped();
2053 text.selectAll();
2054 if (!dropped)
2055 setFocus();
2056 dropDown(!dropped);
2057 break;
2058 }
2059 case DWT.MouseUp:
2060 {
2061 Event mouseEvent = new Event();
2062 mouseEvent.button = event.button;
2063 mouseEvent.count = event.count;
2064 mouseEvent.stateMask = event.stateMask;
2065 mouseEvent.time = event.time;
2066 mouseEvent.x = event.x;
2067 mouseEvent.y = event.y;
2068 notifyListeners(DWT.MouseUp, mouseEvent);
2069 if (isDisposed())
2070 break;
2071 event.doit = mouseEvent.doit;
2072 if (!event.doit)
2073 break;
2074 if (event.button !is 1)
2075 return;
2076 if (text.getEditable())
2077 return;
2078 text.selectAll();
2079 break;
2080 }
2081 case DWT.MouseDoubleClick:
2082 {
2083 Event mouseEvent = new Event();
2084 mouseEvent.button = event.button;
2085 mouseEvent.count = event.count;
2086 mouseEvent.stateMask = event.stateMask;
2087 mouseEvent.time = event.time;
2088 mouseEvent.x = event.x;
2089 mouseEvent.y = event.y;
2090 notifyListeners(DWT.MouseDoubleClick, mouseEvent);
2091 break;
2092 }
2093 case DWT.MouseWheel:
2094 {
2095 Event keyEvent = new Event();
2096 keyEvent.time = event.time;
2097 keyEvent.keyCode = event.count > 0 ? DWT.ARROW_UP : DWT.ARROW_DOWN;
2098 keyEvent.stateMask = event.stateMask;
2099 notifyListeners(DWT.KeyDown, keyEvent);
2100 if (isDisposed())
2101 break;
2102 event.doit = keyEvent.doit;
2103 if (!event.doit)
2104 break;
2105 if (event.count !is 0)
2106 {
2107 event.doit = false;
2108 int oldIndex = getSelectionIndex();
2109 if (event.count > 0)
2110 {
2111 select(Math.max(oldIndex - 1, 0));
2112 }
2113 else
2114 {
2115 select(Math.min(oldIndex + 1, getItemCount() - 1));
2116 }
2117 if (oldIndex !is getSelectionIndex())
2118 {
2119 Event e = new Event();
2120 e.time = event.time;
2121 e.stateMask = event.stateMask;
2122 notifyListeners(DWT.Selection, e);
2123 }
2124 if (isDisposed())
2125 break;
2126 }
2127 break;
2128 }
2129 case DWT.Traverse:
2130 {
2131 switch (event.detail)
2132 {
2133 case DWT.TRAVERSE_ARROW_PREVIOUS:
2134 case DWT.TRAVERSE_ARROW_NEXT:
2135 // The enter causes default selection and
2136 // the arrow keys are used to manipulate the list contents so
2137 // do not use them for traversal.
2138 event.doit = false;
2139 break;
2140 case DWT.TRAVERSE_TAB_PREVIOUS:
2141 event.doit = traverse(DWT.TRAVERSE_TAB_PREVIOUS);
2142 event.detail = DWT.TRAVERSE_NONE;
2143 return;
2144 }
2145 Event e = new Event();
2146 e.time = event.time;
2147 e.detail = event.detail;
2148 e.doit = event.doit;
2149 e.character = event.character;
2150 e.keyCode = event.keyCode;
2151 notifyListeners(DWT.Traverse, e);
2152 event.doit = e.doit;
2153 event.detail = e.detail;
2154 break;
2155 }
2156 case DWT.Verify:
2157 {
2158 Event e = new Event();
2159 e.text = event.text;
2160 e.start = event.start;
2161 e.end = event.end;
2162 e.character = event.character;
2163 e.keyCode = event.keyCode;
2164 e.stateMask = event.stateMask;
2165 notifyListeners(DWT.Verify, e);
2166 event.doit = e.doit;
2167 break;
2168 }
2169
2170 default:
2171 break;
2172 }
2173 }
2174 }