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