Mercurial > projects > dwt-win
annotate dwt/custom/PopupList.d @ 246:fd9c62a2998e
Updater SWT 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 01 Jul 2008 10:15:59 +0200 |
parents | a59d51c12b42 |
children | 349b8c12e243 |
rev | line source |
---|---|
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
1 /******************************************************************************* |
246 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
155 | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwt.custom.PopupList; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.DWTException; | |
18 import dwt.events.ControlEvent; | |
19 import dwt.events.ControlListener; | |
20 import dwt.events.KeyEvent; | |
21 import dwt.events.KeyListener; | |
22 import dwt.events.MouseEvent; | |
23 import dwt.events.MouseListener; | |
24 import dwt.graphics.Font; | |
25 import dwt.graphics.Point; | |
26 import dwt.graphics.Rectangle; | |
27 import dwt.widgets.Display; | |
28 import dwt.widgets.Event; | |
29 import dwt.widgets.List; | |
30 import dwt.widgets.Listener; | |
31 import dwt.widgets.Shell; | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
32 import dwt.dwthelper.utils; |
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
33 |
155 | 34 /** |
35 * A PopupList is a list of selectable items that appears in its own shell positioned above | |
36 * its parent shell. It is used for selecting items when editing a Table cell (similar to the | |
37 * list that appears when you open a Combo box). | |
38 * | |
39 * The list will be positioned so that it does not run off the screen and the largest number of items | |
40 * are visible. It may appear above the current cursor location or below it depending how close you | |
41 * are to the edge of the screen. | |
246 | 42 * |
43 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
155 | 44 */ |
45 public class PopupList { | |
46 Shell shell; | |
47 List list; | |
48 int minimumWidth; | |
49 /** | |
50 * Creates a PopupList above the specified shell. | |
51 * | |
52 * @param parent a Shell control which will be the parent of the new instance (cannot be null) | |
53 */ | |
54 public this(Shell parent) { | |
55 this (parent, 0); | |
56 } | |
57 /** | |
58 * Creates a PopupList above the specified shell. | |
59 * | |
60 * @param parent a widget which will be the parent of the new instance (cannot be null) | |
61 * @param style the style of widget to construct | |
62 * | |
63 * @since 3.0 | |
64 */ | |
65 public this(Shell parent, int style) { | |
66 shell = new Shell(parent, checkStyle(style)); | |
67 | |
68 list = new List(shell, DWT.SINGLE | DWT.V_SCROLL); | |
69 | |
70 // close dialog if user selects outside of the shell | |
71 shell.addListener(DWT.Deactivate, new class() Listener { | |
72 public void handleEvent(Event e){ | |
73 shell.setVisible (false); | |
74 } | |
75 }); | |
76 | |
77 // resize shell when list resizes | |
78 shell.addControlListener(new class() ControlListener { | |
79 public void controlMoved(ControlEvent e){} | |
80 public void controlResized(ControlEvent e){ | |
81 Rectangle shellSize = shell.getClientArea(); | |
82 list.setSize(shellSize.width, shellSize.height); | |
83 } | |
84 }); | |
85 | |
86 // return list selection on Mouse Up or Carriage Return | |
87 list.addMouseListener(new class() MouseListener { | |
88 public void mouseDoubleClick(MouseEvent e){} | |
89 public void mouseDown(MouseEvent e){} | |
90 public void mouseUp(MouseEvent e){ | |
91 shell.setVisible (false); | |
92 } | |
93 }); | |
94 list.addKeyListener(new class() KeyListener { | |
95 public void keyReleased(KeyEvent e){} | |
96 public void keyPressed(KeyEvent e){ | |
97 if (e.character is '\r'){ | |
98 shell.setVisible (false); | |
99 } | |
100 } | |
101 }); | |
102 | |
103 } | |
104 private static int checkStyle (int style) { | |
105 int mask = DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT; | |
106 return style & mask; | |
107 } | |
108 /** | |
109 * Gets the widget font. | |
110 * <p> | |
111 * @return the widget font | |
112 * | |
113 * @exception DWTException <ul> | |
114 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
115 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
116 * </ul> | |
117 */ | |
118 public Font getFont () { | |
119 return list.getFont(); | |
120 } | |
121 /** | |
122 * Gets the items. | |
123 * <p> | |
124 * This operation will fail if the items cannot | |
125 * be queried from the OS. | |
126 * | |
127 * @return the items in the widget | |
128 * | |
129 * @exception DWTException <ul> | |
130 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
131 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
132 * </ul> | |
133 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
134 public String[] getItems () { |
155 | 135 return list.getItems(); |
136 } | |
137 /** | |
138 * Gets the minimum width of the list. | |
139 * | |
140 * @return the minimum width of the list | |
141 */ | |
142 public int getMinimumWidth () { | |
143 return minimumWidth; | |
144 } | |
145 /** | |
146 * Launches the Popup List, waits for an item to be selected and then closes the PopupList. | |
147 * | |
148 * @param rect the initial size and location of the PopupList; the dialog will be | |
149 * positioned so that it does not run off the screen and the largest number of items are visible | |
150 * | |
151 * @return the text of the selected item or null if no item is selected | |
152 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
153 public String open (Rectangle rect) { |
155 | 154 |
155 Point listSize = list.computeSize (rect.width, DWT.DEFAULT, false); | |
156 Rectangle screenSize = shell.getDisplay().getBounds(); | |
157 | |
158 // Position the dialog so that it does not run off the screen and the largest number of items are visible | |
159 int spaceBelow = screenSize.height - (rect.y + rect.height) - 30; | |
160 int spaceAbove = rect.y - 30; | |
161 | |
162 int y = 0; | |
163 if (spaceAbove > spaceBelow && listSize.y > spaceBelow) { | |
164 // place popup list above table cell | |
165 if (listSize.y > spaceAbove){ | |
166 listSize.y = spaceAbove; | |
167 } else { | |
168 listSize.y += 2; | |
169 } | |
170 y = rect.y - listSize.y; | |
171 | |
172 } else { | |
173 // place popup list below table cell | |
174 if (listSize.y > spaceBelow){ | |
175 listSize.y = spaceBelow; | |
176 } else { | |
177 listSize.y += 2; | |
178 } | |
179 y = rect.y + rect.height; | |
180 } | |
181 | |
182 // Make dialog as wide as the cell | |
183 listSize.x = rect.width; | |
184 // dialog width should not be less than minimumWidth | |
185 if (listSize.x < minimumWidth) | |
186 listSize.x = minimumWidth; | |
187 | |
188 // Align right side of dialog with right side of cell | |
189 int x = rect.x + rect.width - listSize.x; | |
190 | |
191 shell.setBounds(x, y, listSize.x, listSize.y); | |
192 | |
193 shell.open(); | |
194 list.setFocus(); | |
195 | |
196 Display display = shell.getDisplay(); | |
197 while (!shell.isDisposed () && shell.isVisible ()) { | |
198 if (!display.readAndDispatch()) display.sleep(); | |
199 } | |
200 | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
201 String result = null; |
155 | 202 if (!shell.isDisposed ()) { |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
203 String [] strings = list.getSelection (); |
155 | 204 shell.dispose(); |
205 if (strings.length !is 0) result = strings [0]; | |
206 } | |
207 return result; | |
208 } | |
209 /** | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
210 * Selects an item with text that starts with specified String. |
155 | 211 * <p> |
212 * If the item is not currently selected, it is selected. | |
213 * If the item at an index is selected, it remains selected. | |
214 * If the string is not matched, it is ignored. | |
215 * | |
216 * @param string the text of the item | |
217 * | |
218 * @exception DWTException <ul> | |
219 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
220 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
221 * </ul> | |
222 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
223 public void select(String string) { |
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
224 String[] items = list.getItems(); |
155 | 225 |
226 // find the first entry in the list that starts with the | |
227 // specified string | |
228 if (string !is null){ | |
229 for (int i = 0; i < items.length; i++) { | |
230 if ( tango.text.Util.locatePattern( items[i], string) is 0 ){ | |
231 int index = list.indexOf(items[i]); | |
232 list.select(index); | |
233 break; | |
234 } | |
235 } | |
236 } | |
237 } | |
238 /** | |
239 * Sets the widget font. | |
240 * <p> | |
241 * When new font is null, the font reverts | |
242 * to the default system font for the widget. | |
243 * | |
244 * @param font the new font (or null) | |
245 * | |
246 * @exception DWTException <ul> | |
247 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
248 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
249 * </ul> | |
250 */ | |
251 public void setFont (Font font) { | |
252 list.setFont(font); | |
253 } | |
254 /** | |
255 * Sets all items. | |
256 * <p> | |
257 * The previous selection is cleared. | |
258 * The previous items are deleted. | |
259 * The new items are added. | |
260 * The top index is set to 0. | |
261 * | |
262 * @param strings the array of items | |
263 * | |
264 * This operation will fail when an item is null | |
265 * or could not be added in the OS. | |
266 * | |
267 * @exception IllegalArgumentException <ul> | |
268 * <li>ERROR_INVALID_ARGUMENT - if an item in the items array is null</li> | |
269 * </ul> | |
270 * @exception DWTException <ul> | |
271 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
272 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
273 * </ul> | |
274 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
275 public void setItems (String[] strings) { |
155 | 276 list.setItems(strings); |
277 } | |
278 /** | |
279 * Sets the minimum width of the list. | |
280 * | |
281 * @param width the minimum width of the list | |
282 */ | |
283 public void setMinimumWidth (int width) { | |
284 if (width < 0) | |
285 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
286 | |
287 minimumWidth = width; | |
288 } | |
289 } |