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