Mercurial > projects > dwt-mac
comparison dwt/widgets/CoolBar.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 649b8e223d5a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2006 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 *******************************************************************************/ | |
11 module dwt.widgets.CoolBar; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.DWTException; | |
18 import dwt.graphics.Color; | |
19 import dwt.graphics.Cursor; | |
20 import dwt.graphics.GC; | |
21 import dwt.graphics.Point; | |
22 import dwt.graphics.Rectangle; | |
23 | |
24 /** | |
25 * Instances of this class provide an area for dynamically | |
26 * positioning the items they contain. | |
27 * <p> | |
28 * The item children that may be added to instances of this class | |
29 * must be of type <code>CoolItem</code>. | |
30 * </p><p> | |
31 * Note that although this class is a subclass of <code>Composite</code>, | |
32 * it does not make sense to add <code>Control</code> children to it, | |
33 * or set a layout on it. | |
34 * </p><p> | |
35 * <dl> | |
36 * <dt><b>Styles:</b></dt> | |
37 * <dd>FLAT, HORIZONTAL, VERTICAL</dd> | |
38 * <dt><b>Events:</b></dt> | |
39 * <dd>(none)</dd> | |
40 * </dl> | |
41 * </p><p> | |
42 * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified. | |
43 * </p><p> | |
44 * IMPORTANT: This class is <em>not</em> intended to be subclassed. | |
45 * </p> | |
46 */ | |
47 public class CoolBar extends Composite { | |
48 CoolItem[][] items = new CoolItem[0][0]; | |
49 CoolItem[] originalItems = new CoolItem[0]; | |
50 Cursor hoverCursor, dragCursor, cursor; | |
51 CoolItem dragging = null; | |
52 int mouseXOffset, itemXOffset; | |
53 bool isLocked = false; | |
54 bool inDispose = false; | |
55 static final int ROW_SPACING = 2; | |
56 static final int CLICK_DISTANCE = 3; | |
57 static final int DEFAULT_COOLBAR_WIDTH = 0; | |
58 static final int DEFAULT_COOLBAR_HEIGHT = 0; | |
59 | |
60 /** | |
61 * Constructs a new instance of this class given its parent | |
62 * and a style value describing its behavior and appearance. | |
63 * <p> | |
64 * The style value is either one of the style constants defined in | |
65 * class <code>DWT</code> which is applicable to instances of this | |
66 * class, or must be built by <em>bitwise OR</em>'ing together | |
67 * (that is, using the <code>int</code> "|" operator) two or more | |
68 * of those <code>DWT</code> style constants. The class description | |
69 * lists the style constants that are applicable to the class. | |
70 * Style bits are also inherited from superclasses. | |
71 * </p> | |
72 * | |
73 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
74 * @param style the style of control to construct | |
75 * | |
76 * @exception IllegalArgumentException <ul> | |
77 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
78 * </ul> | |
79 * @exception DWTException <ul> | |
80 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
81 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
82 * </ul> | |
83 * | |
84 * @see DWT | |
85 * @see Widget#checkSubclass | |
86 * @see Widget#getStyle | |
87 */ | |
88 public CoolBar (Composite parent, int style) { | |
89 super (parent, checkStyle(style)); | |
90 if ((style & DWT.VERTICAL) !is 0) { | |
91 this.style |= DWT.VERTICAL; | |
92 hoverCursor = new Cursor(display, DWT.CURSOR_SIZENS); | |
93 } else { | |
94 this.style |= DWT.HORIZONTAL; | |
95 hoverCursor = new Cursor(display, DWT.CURSOR_SIZEWE); | |
96 } | |
97 dragCursor = new Cursor(display, DWT.CURSOR_SIZEALL); | |
98 Listener listener = new Listener() { | |
99 public void handleEvent(Event event) { | |
100 switch (event.type) { | |
101 case DWT.Dispose: onDispose(event); break; | |
102 case DWT.MouseDown: onMouseDown(event); break; | |
103 case DWT.MouseExit: onMouseExit(); break; | |
104 case DWT.MouseMove: onMouseMove(event); break; | |
105 case DWT.MouseUp: onMouseUp(event); break; | |
106 case DWT.MouseDoubleClick: onMouseDoubleClick(event); break; | |
107 case DWT.Paint: onPaint(event); break; | |
108 case DWT.Resize: onResize(); break; | |
109 } | |
110 } | |
111 }; | |
112 int[] events = new int[] { | |
113 DWT.Dispose, | |
114 DWT.MouseDown, | |
115 DWT.MouseExit, | |
116 DWT.MouseMove, | |
117 DWT.MouseUp, | |
118 DWT.MouseDoubleClick, | |
119 DWT.Paint, | |
120 DWT.Resize | |
121 }; | |
122 for (int i = 0; i < events.length; i++) { | |
123 addListener(events[i], listener); | |
124 } | |
125 } | |
126 static int checkStyle (int style) { | |
127 style |= DWT.NO_FOCUS; | |
128 return (style | DWT.NO_REDRAW_RESIZE) & ~(DWT.V_SCROLL | DWT.H_SCROLL); | |
129 } | |
130 void _setCursor (Cursor cursor) { | |
131 if (this.cursor !is null) return; | |
132 super.setCursor (cursor); | |
133 } | |
134 protected void checkSubclass () { | |
135 if (!isValidSubclass ()) error (DWT.ERROR_INVALID_SUBCLASS); | |
136 } | |
137 public Point computeSize (int wHint, int hHint, bool changed) { | |
138 checkWidget(); | |
139 int width = 0, height = 0; | |
140 wrapItems((style & DWT.VERTICAL) !is 0 ? hHint : wHint); | |
141 bool flat = (style & DWT.FLAT) !is 0; | |
142 for (int row = 0; row < items.length; row++) { | |
143 int rowWidth = 0, rowHeight = 0; | |
144 for (int i = 0; i < items[row].length; i++) { | |
145 CoolItem item = items[row][i]; | |
146 rowWidth += item.preferredWidth; | |
147 rowHeight = Math.max(rowHeight, item.preferredHeight); | |
148 } | |
149 height += rowHeight; | |
150 if (!flat && row > 0) height += ROW_SPACING; | |
151 width = Math.max(width, rowWidth); | |
152 } | |
153 wrapItems(getWidth()); | |
154 if (width is 0) width = DEFAULT_COOLBAR_WIDTH; | |
155 if (height is 0) height = DEFAULT_COOLBAR_HEIGHT; | |
156 if (wHint !is DWT.DEFAULT) width = wHint; | |
157 if (hHint !is DWT.DEFAULT) height = hHint; | |
158 Rectangle trim = computeTrim(0, 0, width, height); | |
159 return fixPoint(trim.width, trim.height); | |
160 } | |
161 CoolItem getGrabbedItem(int x, int y) { | |
162 for (int row = 0; row < items.length; row++) { | |
163 for (int i = 0; i < items[row].length; i++) { | |
164 CoolItem item = items[row][i]; | |
165 Rectangle bounds = item.internalGetBounds(); | |
166 bounds.width = CoolItem.MINIMUM_WIDTH; | |
167 if (bounds.x > x) break; | |
168 if (bounds.y > y) return null; | |
169 if (bounds.contains(x, y)) { | |
170 return item; | |
171 } | |
172 } | |
173 } | |
174 return null; | |
175 } | |
176 /** | |
177 * Returns the item that is currently displayed at the given, | |
178 * zero-relative index. Throws an exception if the index is | |
179 * out of range. | |
180 * | |
181 * @param index the visual index of the item to return | |
182 * @return the item at the given visual index | |
183 * | |
184 * @exception IllegalArgumentException <ul> | |
185 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li> | |
186 * </ul> | |
187 * @exception DWTException <ul> | |
188 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
189 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
190 * </ul> | |
191 */ | |
192 public CoolItem getItem (int index) { | |
193 checkWidget(); | |
194 if (index < 0) error (DWT.ERROR_INVALID_RANGE); | |
195 for (int row = 0; row < items.length; row++) { | |
196 if (items[row].length > index) { | |
197 return items[row][index]; | |
198 } else { | |
199 index -= items[row].length; | |
200 } | |
201 } | |
202 error (DWT.ERROR_INVALID_RANGE); | |
203 return null; | |
204 } | |
205 /** | |
206 * Returns the number of items contained in the receiver. | |
207 * | |
208 * @return the number of items | |
209 * | |
210 * @exception DWTException <ul> | |
211 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
212 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
213 * </ul> | |
214 */ | |
215 public int getItemCount () { | |
216 checkWidget(); | |
217 return originalItems.length; | |
218 } | |
219 /** | |
220 * Returns an array of <code>CoolItem</code>s in the order | |
221 * in which they are currently being displayed. | |
222 * <p> | |
223 * Note: This is not the actual structure used by the receiver | |
224 * to maintain its list of items, so modifying the array will | |
225 * not affect the receiver. | |
226 * </p> | |
227 * | |
228 * @return the receiver's items in their current visual order | |
229 * | |
230 * @exception DWTException <ul> | |
231 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
232 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
233 * </ul> | |
234 */ | |
235 public CoolItem [] getItems () { | |
236 checkWidget(); | |
237 CoolItem [] result = new CoolItem [getItemCount()]; | |
238 int offset = 0; | |
239 for (int row = 0; row < items.length; row++) { | |
240 System.arraycopy(items[row], 0, result, offset, items[row].length); | |
241 offset += items[row].length; | |
242 } | |
243 return result; | |
244 } | |
245 Point findItem (CoolItem item) { | |
246 for (int row = 0; row < items.length; row++) { | |
247 for (int i = 0; i < items[row].length; i++) { | |
248 if (items[row][i].equals(item)) return new Point(i, row); | |
249 } | |
250 } | |
251 return new Point(-1, -1); | |
252 } | |
253 void fixEvent (Event event) { | |
254 if ((style & DWT.VERTICAL) !is 0) { | |
255 int tmp = event.x; | |
256 event.x = event.y; | |
257 event.y = tmp; | |
258 } | |
259 } | |
260 Rectangle fixRectangle (int x, int y, int width, int height) { | |
261 if ((style & DWT.VERTICAL) !is 0) { | |
262 return new Rectangle(y, x, height, width); | |
263 } | |
264 return new Rectangle(x, y, width, height); | |
265 } | |
266 Point fixPoint (int x, int y) { | |
267 if ((style & DWT.VERTICAL) !is 0) { | |
268 return new Point(y, x); | |
269 } | |
270 return new Point(x, y); | |
271 } | |
272 /** | |
273 * Searches the receiver's items in the order they are currently | |
274 * being displayed, starting at the first item (index 0), until | |
275 * an item is found that is equal to the argument, and returns | |
276 * the index of that item. If no item is found, returns -1. | |
277 * | |
278 * @param item the search item | |
279 * @return the visual order index of the search item, or -1 if the item is not found | |
280 * | |
281 * @exception IllegalArgumentException <ul> | |
282 * <li>ERROR_NULL_ARGUMENT - if the item is null</li> | |
283 * <li>ERROR_INVALID_ARGUMENT - if the item is disposed</li> | |
284 * </ul> | |
285 * @exception DWTException <ul> | |
286 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
287 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
288 * </ul> | |
289 */ | |
290 public int indexOf (CoolItem item) { | |
291 checkWidget(); | |
292 if (item is null) error (DWT.ERROR_NULL_ARGUMENT); | |
293 if (item.isDisposed()) error (DWT.ERROR_INVALID_ARGUMENT); | |
294 int answer = 0; | |
295 for (int row = 0; row < items.length; row++) { | |
296 for (int i = 0; i < items[row].length; i++) { | |
297 if (items[row][i].equals(item)) { | |
298 return answer; | |
299 } else { | |
300 answer++; | |
301 } | |
302 } | |
303 } | |
304 return -1; | |
305 } | |
306 /** | |
307 * Insert the item into the row. Adjust the x and width values | |
308 * appropriately. | |
309 */ | |
310 void insertItemIntoRow(CoolItem item, int rowIndex, int x_root) { | |
311 int barWidth = getWidth(); | |
312 int rowY = items[rowIndex][0].internalGetBounds().y; | |
313 int x = Math.max(0, Math.abs(x_root - toDisplay(new Point(0, 0)).x)); | |
314 | |
315 /* Find the insertion index and add the item. */ | |
316 int index; | |
317 for (index = 0; index < items[rowIndex].length; index++) { | |
318 if (x < items[rowIndex][index].internalGetBounds().x) break; | |
319 } | |
320 if (index is 0) { | |
321 item.wrap = true; | |
322 items[rowIndex][0].wrap = false; | |
323 } | |
324 int oldLength = items[rowIndex].length; | |
325 CoolItem[] newRow = new CoolItem[oldLength + 1]; | |
326 System.arraycopy(items[rowIndex], 0, newRow, 0, index); | |
327 newRow[index] = item; | |
328 System.arraycopy(items[rowIndex], index, newRow, index + 1, oldLength - index); | |
329 items[rowIndex] = newRow; | |
330 | |
331 /* Adjust the width of the item to the left. */ | |
332 if (index > 0) { | |
333 CoolItem left = items[rowIndex][index - 1]; | |
334 Rectangle leftBounds = left.internalGetBounds(); | |
335 int newWidth = x - leftBounds.x; | |
336 if (newWidth < left.internalGetMinimumWidth()) { | |
337 x += left.internalGetMinimumWidth() - newWidth; | |
338 newWidth = left.internalGetMinimumWidth(); | |
339 } | |
340 left.setBounds(leftBounds.x, leftBounds.y, newWidth, leftBounds.height); | |
341 left.requestedWidth = newWidth; | |
342 } | |
343 | |
344 /* Set the item's bounds. */ | |
345 int width = 0, height = item.internalGetBounds().height; | |
346 if (index < items[rowIndex].length - 1) { | |
347 CoolItem right = items[rowIndex][index + 1]; | |
348 width = right.internalGetBounds().x - x; | |
349 if (width < right.internalGetMinimumWidth()) { | |
350 moveRight(right, right.internalGetMinimumWidth() - width); | |
351 width = right.internalGetBounds().x - x; | |
352 } | |
353 item.setBounds(x, rowY, width, height); | |
354 if (width < item.internalGetMinimumWidth()) moveLeft(item, item.internalGetMinimumWidth() - width); | |
355 } else { | |
356 width = Math.max(item.internalGetMinimumWidth(), barWidth - x); | |
357 item.setBounds(x, rowY, width, height); | |
358 if (x + width > barWidth) moveLeft(item, x + width - barWidth); | |
359 } | |
360 Rectangle bounds = item.internalGetBounds(); | |
361 item.requestedWidth = bounds.width; | |
362 internalRedraw(bounds.x, bounds.y, item.internalGetMinimumWidth(), bounds.height); | |
363 } | |
364 void internalRedraw (int x, int y, int width, int height) { | |
365 if ((style & DWT.VERTICAL) !is 0) { | |
366 redraw (y, x, height, width, false); | |
367 } else { | |
368 redraw (x, y, width, height, false); | |
369 } | |
370 } | |
371 void createItem (CoolItem item, int index) { | |
372 int itemCount = getItemCount(), row = 0; | |
373 if (!(0 <= index && index <= itemCount)) error (DWT.ERROR_INVALID_RANGE); | |
374 if (items.length is 0) { | |
375 items = new CoolItem[1][1]; | |
376 items[0][0] = item; | |
377 } else { | |
378 int i = index; | |
379 /* find the row to insert into */ | |
380 if (index < itemCount) { | |
381 while (i > items[row].length) { | |
382 i -= items[row].length; | |
383 row++; | |
384 } | |
385 } else { | |
386 row = items.length - 1; | |
387 i = items[row].length; | |
388 } | |
389 | |
390 // Set the last item in the row to the preferred size | |
391 // and add the new one just to it's right | |
392 int lastIndex = items[row].length - 1; | |
393 CoolItem lastItem = items[row][lastIndex]; | |
394 if (lastItem.ideal) { | |
395 Rectangle bounds = lastItem.internalGetBounds(); | |
396 bounds.width = lastItem.preferredWidth; | |
397 bounds.height = lastItem.preferredHeight; | |
398 lastItem.requestedWidth = lastItem.preferredWidth; | |
399 lastItem.setBounds(bounds.x, bounds.y, bounds.width, bounds.height); | |
400 } | |
401 if (i is 0) { | |
402 item.wrap = true; | |
403 items[row][0].wrap = false; | |
404 } | |
405 int oldLength = items[row].length; | |
406 CoolItem[] newRow = new CoolItem[oldLength + 1]; | |
407 System.arraycopy(items[row], 0, newRow, 0, i); | |
408 newRow[i] = item; | |
409 System.arraycopy(items[row], i, newRow, i + 1, oldLength - i); | |
410 items[row] = newRow; | |
411 } | |
412 item.requestedWidth = CoolItem.MINIMUM_WIDTH; | |
413 | |
414 int length = originalItems.length; | |
415 CoolItem [] newOriginals = new CoolItem [length + 1]; | |
416 System.arraycopy (originalItems, 0, newOriginals, 0, index); | |
417 System.arraycopy (originalItems, index, newOriginals, index + 1, length - index); | |
418 newOriginals [index] = item; | |
419 originalItems = newOriginals; | |
420 layoutItems(); | |
421 | |
422 } | |
423 void destroyItem(CoolItem item) { | |
424 if (inDispose) return; | |
425 int row = findItem(item).y; | |
426 if (row is -1) return; | |
427 Rectangle bounds = item.internalGetBounds(); | |
428 removeItemFromRow(item, row, true); | |
429 | |
430 int index = 0; | |
431 while (index < originalItems.length) { | |
432 if (originalItems [index] is item) break; | |
433 index++; | |
434 } | |
435 int length = originalItems.length - 1; | |
436 CoolItem [] newOriginals = new CoolItem [length]; | |
437 System.arraycopy (originalItems, 0, newOriginals, 0, index); | |
438 System.arraycopy (originalItems, index + 1, newOriginals, index, length - index); | |
439 originalItems = newOriginals; | |
440 | |
441 internalRedraw(bounds.x, bounds.y, CoolItem.MINIMUM_WIDTH, bounds.height); | |
442 relayout(); | |
443 } | |
444 void moveDown(CoolItem item, int x_root) { | |
445 int oldRowIndex = findItem(item).y; | |
446 bool resize = false; | |
447 if (items[oldRowIndex].length is 1) { | |
448 resize = true; | |
449 /* If this is the only item in the bottom row, don't move it. */ | |
450 if (oldRowIndex is items.length - 1) return; | |
451 } | |
452 int newRowIndex = (items[oldRowIndex].length is 1) ? oldRowIndex : oldRowIndex + 1; | |
453 removeItemFromRow(item, oldRowIndex, false); | |
454 Rectangle old = item.internalGetBounds(); | |
455 internalRedraw(old.x, old.y, CoolItem.MINIMUM_WIDTH, old.height); | |
456 if (newRowIndex is items.length) { | |
457 /* Create a new bottom row for the item. */ | |
458 CoolItem[][] newRows = new CoolItem[items.length + 1][]; | |
459 System.arraycopy(items, 0, newRows, 0, items.length); | |
460 int row = items.length; | |
461 newRows[row] = new CoolItem[1]; | |
462 newRows[row][0] = item; | |
463 items = newRows; | |
464 resize = true; | |
465 item.wrap = true; | |
466 } else { | |
467 insertItemIntoRow(item, newRowIndex, x_root); | |
468 } | |
469 if (resize) { | |
470 relayout(); | |
471 } else { | |
472 layoutItems(); | |
473 } | |
474 } | |
475 void moveLeft(CoolItem item, int pixels) { | |
476 Point point = findItem(item); | |
477 int row = point.y; | |
478 int index = point.x; | |
479 if (index is 0) return; | |
480 Rectangle bounds = item.internalGetBounds(); | |
481 int minSpaceOnLeft = 0; | |
482 for (int i = 0; i < index; i++) { | |
483 minSpaceOnLeft += items[row][i].internalGetMinimumWidth(); | |
484 } | |
485 int x = Math.max(minSpaceOnLeft, bounds.x - pixels); | |
486 CoolItem left = items[row][index - 1]; | |
487 Rectangle leftBounds = left.internalGetBounds(); | |
488 if (leftBounds.x + left.internalGetMinimumWidth() > x) { | |
489 int shift = leftBounds.x + left.internalGetMinimumWidth() - x; | |
490 moveLeft(left, shift); | |
491 leftBounds = left.internalGetBounds(); | |
492 } | |
493 int leftWidth = Math.max(left.internalGetMinimumWidth(), leftBounds.width - pixels); | |
494 left.setBounds(leftBounds.x, leftBounds.y, leftWidth, leftBounds.height); | |
495 left.requestedWidth = leftWidth; | |
496 int width = bounds.width + (bounds.x - x); | |
497 item.setBounds(x, bounds.y, width, bounds.height); | |
498 item.requestedWidth = width; | |
499 | |
500 int damagedWidth = bounds.x - x + CoolItem.MINIMUM_WIDTH; | |
501 if (damagedWidth > CoolItem.MINIMUM_WIDTH) { | |
502 internalRedraw(x, bounds.y, damagedWidth, bounds.height); | |
503 } | |
504 } | |
505 void moveRight(CoolItem item, int pixels) { | |
506 Point point = findItem(item); | |
507 int row = point.y; | |
508 int index = point.x; | |
509 if (index is 0) return; | |
510 Rectangle bounds = item.internalGetBounds(); | |
511 int minSpaceOnRight = 0; | |
512 for (int i = index; i < items[row].length; i++) { | |
513 minSpaceOnRight += items[row][i].internalGetMinimumWidth(); | |
514 } | |
515 int max = getWidth() - minSpaceOnRight; | |
516 int x = Math.min(max, bounds.x + pixels); | |
517 int width = 0; | |
518 if (index + 1 is items[row].length) { | |
519 width = getWidth() - x; | |
520 } else { | |
521 CoolItem right = items[row][index + 1]; | |
522 Rectangle rightBounds = right.internalGetBounds(); | |
523 if (x + item.internalGetMinimumWidth() > rightBounds.x) { | |
524 int shift = x + item.internalGetMinimumWidth() - rightBounds.x; | |
525 moveRight(right, shift); | |
526 rightBounds = right.internalGetBounds(); | |
527 } | |
528 width = rightBounds.x - x; | |
529 } | |
530 item.setBounds(x, bounds.y, width, bounds.height); | |
531 item.requestedWidth = width; | |
532 CoolItem left = items[row][index - 1]; | |
533 Rectangle leftBounds = left.internalGetBounds(); | |
534 int leftWidth = x - leftBounds.x; | |
535 left.setBounds(leftBounds.x, leftBounds.y, leftWidth, leftBounds.height); | |
536 left.requestedWidth = leftWidth; | |
537 | |
538 int damagedWidth = x - bounds.x + CoolItem.MINIMUM_WIDTH + CoolItem.MARGIN_WIDTH; | |
539 if (x - bounds.x > 0) { | |
540 internalRedraw(bounds.x - CoolItem.MARGIN_WIDTH, bounds.y, damagedWidth, bounds.height); | |
541 } | |
542 } | |
543 void moveUp(CoolItem item, int x_root) { | |
544 Point point = findItem(item); | |
545 int oldRowIndex = point.y; | |
546 bool resize = false; | |
547 if (items[oldRowIndex].length is 1) { | |
548 resize = true; | |
549 /* If this is the only item in the top row, don't move it. */ | |
550 if (oldRowIndex is 0) return; | |
551 } | |
552 removeItemFromRow(item, oldRowIndex, false); | |
553 Rectangle old = item.internalGetBounds(); | |
554 internalRedraw(old.x, old.y, CoolItem.MINIMUM_WIDTH, old.height); | |
555 int newRowIndex = Math.max(0, oldRowIndex - 1); | |
556 if (oldRowIndex is 0) { | |
557 /* Create a new top row for the item. */ | |
558 CoolItem[][] newRows = new CoolItem[items.length + 1][]; | |
559 System.arraycopy(items, 0, newRows, 1, items.length); | |
560 newRows[0] = new CoolItem[1]; | |
561 newRows[0][0] = item; | |
562 items = newRows; | |
563 resize = true; | |
564 item.wrap = true; | |
565 } else { | |
566 insertItemIntoRow(item, newRowIndex, x_root); | |
567 } | |
568 if (resize) { | |
569 relayout(); | |
570 } else { | |
571 layoutItems(); | |
572 } | |
573 } | |
574 void onDispose(Event event) { | |
575 /* | |
576 * Usually when an item is disposed, destroyItem will change the size of the items array | |
577 * and reset the bounds of all the remaining cool items. | |
578 * Since the whole cool bar is being disposed, this is not necessary. For speed | |
579 * the inDispose flag is used to skip over this part of the item dispose. | |
580 */ | |
581 if (inDispose) return; | |
582 inDispose = true; | |
583 notifyListeners(DWT.Dispose, event); | |
584 event.type = DWT.None; | |
585 for (int i = 0; i < items.length; i++) { | |
586 for (int j = 0; j < items[i].length; j++) { | |
587 items[i][j].dispose(); | |
588 } | |
589 } | |
590 hoverCursor.dispose(); | |
591 dragCursor.dispose(); | |
592 cursor = null; | |
593 } | |
594 void onMouseDown(Event event) { | |
595 if (isLocked || event.button !is 1) return; | |
596 fixEvent(event); | |
597 dragging = getGrabbedItem(event.x, event.y); | |
598 if (dragging !is null) { | |
599 mouseXOffset = event.x; | |
600 itemXOffset = mouseXOffset - dragging.internalGetBounds().x; | |
601 _setCursor(dragCursor); | |
602 } | |
603 fixEvent(event); | |
604 } | |
605 void onMouseExit() { | |
606 if (dragging is null) _setCursor(null); | |
607 } | |
608 void onMouseMove(Event event) { | |
609 if (isLocked) return; | |
610 fixEvent(event); | |
611 CoolItem grabbed = getGrabbedItem(event.x, event.y); | |
612 if (dragging !is null) { | |
613 int left_root = toDisplay(new Point(event.x - itemXOffset, event.y)).x; | |
614 Rectangle bounds = dragging.internalGetBounds(); | |
615 if (event.y < bounds.y) { | |
616 moveUp(dragging, left_root); | |
617 } else if (event.y > bounds.y + bounds.height){ | |
618 moveDown(dragging, left_root); | |
619 } else if (event.x < mouseXOffset) { | |
620 int distance = Math.min(mouseXOffset, bounds.x + itemXOffset) - event.x; | |
621 if (distance > 0) moveLeft(dragging, distance); | |
622 } else if (event.x > mouseXOffset) { | |
623 int distance = event.x - Math.max(mouseXOffset, bounds.x + itemXOffset); | |
624 if (distance > 0) moveRight(dragging, distance); | |
625 } | |
626 mouseXOffset = event.x; | |
627 } else { | |
628 if (grabbed !is null) { | |
629 _setCursor(hoverCursor); | |
630 } else { | |
631 _setCursor(null); | |
632 } | |
633 } | |
634 fixEvent(event); | |
635 } | |
636 void onMouseUp(Event event) { | |
637 _setCursor(null); | |
638 dragging = null; | |
639 } | |
640 void onMouseDoubleClick(Event event) { | |
641 if (isLocked) return; | |
642 dragging = null; | |
643 fixEvent(event); | |
644 CoolItem target = getGrabbedItem(event.x, event.y); | |
645 if (target is null) { | |
646 _setCursor(null); | |
647 } else { | |
648 Point location = findItem(target); | |
649 int row = location.y; | |
650 int index = location.x; | |
651 if (items[row].length > 1) { | |
652 Rectangle bounds = target.internalGetBounds(); | |
653 int maxSize = getWidth (); | |
654 for (int i = 0; i < items[row].length; i++) { | |
655 if (i !is index) { | |
656 maxSize -= items[row][i].internalGetMinimumWidth(); | |
657 } | |
658 } | |
659 if (bounds.width is maxSize) { | |
660 /* The item is at its maximum width. It should be resized to its minimum width. */ | |
661 int distance = bounds.width - target.internalGetMinimumWidth(); | |
662 if (index + 1 < items[row].length) { | |
663 /* There is an item to the right. Maximize it. */ | |
664 CoolItem right = items[row][index + 1]; | |
665 moveLeft(right, distance); | |
666 } else { | |
667 /* There is no item to the right. Move the item all the way right. */ | |
668 moveRight(target, distance); | |
669 } | |
670 } else if (bounds.width < target.preferredWidth) { | |
671 /* The item is less than its preferredWidth. Resize to preferredWidth. */ | |
672 int distance = target.preferredWidth - bounds.width; | |
673 if (index + 1 < items[row].length) { | |
674 CoolItem right = items[row][index + 1]; | |
675 moveRight(right, distance); | |
676 distance = target.preferredWidth - target.internalGetBounds().width; | |
677 } | |
678 if (distance > 0) { | |
679 moveLeft(target, distance); | |
680 } | |
681 } else { | |
682 /* The item is at its minimum width. Maximize it. */ | |
683 for (int i = 0; i < items[row].length; i++) { | |
684 if (i !is index) { | |
685 CoolItem item = items[row][i]; | |
686 item.requestedWidth = Math.max(item.internalGetMinimumWidth(), CoolItem.MINIMUM_WIDTH); | |
687 } | |
688 } | |
689 target.requestedWidth = maxSize; | |
690 layoutItems(); | |
691 } | |
692 _setCursor(hoverCursor); | |
693 } | |
694 } | |
695 fixEvent(event); | |
696 } | |
697 void onPaint(Event event) { | |
698 GC gc = event.gc; | |
699 if (items.length is 0) return; | |
700 Color shadowColor = display.getSystemColor(DWT.COLOR_WIDGET_NORMAL_SHADOW); | |
701 Color highlightColor = display.getSystemColor(DWT.COLOR_WIDGET_HIGHLIGHT_SHADOW); | |
702 bool vertical = (style & DWT.VERTICAL) !is 0; | |
703 bool flat = (style & DWT.FLAT) !is 0; | |
704 int stopX = getWidth(); | |
705 Rectangle rect; | |
706 Rectangle clipping = gc.getClipping(); | |
707 for (int row = 0; row < items.length; row++) { | |
708 Rectangle bounds = new Rectangle(0, 0, 0, 0); | |
709 for (int i = 0; i < items[row].length; i++) { | |
710 bounds = items[row][i].internalGetBounds(); | |
711 rect = fixRectangle(bounds.x, bounds.y, bounds.width, bounds.height); | |
712 if (!clipping.intersects(rect)) continue; | |
713 bool nativeGripper = false; | |
714 | |
715 /* Draw gripper. */ | |
716 if (!isLocked) { | |
717 rect = fixRectangle(bounds.x, bounds.y, CoolItem.MINIMUM_WIDTH, bounds.height); | |
718 if (!flat) nativeGripper = drawGripper(rect.x, rect.y, rect.width, rect.height, vertical); | |
719 if (!nativeGripper) { | |
720 int grabberTrim = 2; | |
721 int grabberHeight = bounds.height - (2 * grabberTrim) - 1; | |
722 gc.setForeground(shadowColor); | |
723 rect = fixRectangle( | |
724 bounds.x + CoolItem.MARGIN_WIDTH, | |
725 bounds.y + grabberTrim, | |
726 2, | |
727 grabberHeight); | |
728 gc.drawRectangle(rect); | |
729 gc.setForeground(highlightColor); | |
730 rect = fixRectangle( | |
731 bounds.x + CoolItem.MARGIN_WIDTH, | |
732 bounds.y + grabberTrim + 1, | |
733 bounds.x + CoolItem.MARGIN_WIDTH, | |
734 bounds.y + grabberTrim + grabberHeight - 1); | |
735 gc.drawLine(rect.x, rect.y, rect.width, rect.height); | |
736 rect = fixRectangle( | |
737 bounds.x + CoolItem.MARGIN_WIDTH, | |
738 bounds.y + grabberTrim, | |
739 bounds.x + CoolItem.MARGIN_WIDTH + 1, | |
740 bounds.y + grabberTrim); | |
741 gc.drawLine(rect.x, rect.y, rect.width, rect.height); | |
742 } | |
743 } | |
744 | |
745 /* Draw separator. */ | |
746 if (!flat && !nativeGripper && i !is 0) { | |
747 gc.setForeground(shadowColor); | |
748 rect = fixRectangle(bounds.x, bounds.y, bounds.x, bounds.y + bounds.height - 1); | |
749 gc.drawLine(rect.x, rect.y, rect.width, rect.height); | |
750 gc.setForeground(highlightColor); | |
751 rect = fixRectangle(bounds.x + 1, bounds.y, bounds.x + 1, bounds.y + bounds.height - 1); | |
752 gc.drawLine(rect.x, rect.y, rect.width, rect.height); | |
753 } | |
754 } | |
755 if (!flat && row + 1 < items.length) { | |
756 /* Draw row separator. */ | |
757 int separatorY = bounds.y + bounds.height; | |
758 gc.setForeground(shadowColor); | |
759 rect = fixRectangle(0, separatorY, stopX, separatorY); | |
760 gc.drawLine(rect.x, rect.y, rect.width, rect.height); | |
761 gc.setForeground(highlightColor); | |
762 rect = fixRectangle(0, separatorY + 1, stopX, separatorY + 1); | |
763 gc.drawLine(rect.x, rect.y, rect.width, rect.height); | |
764 } | |
765 } | |
766 } | |
767 void onResize () { | |
768 layoutItems (); | |
769 } | |
770 void removeControl (Control control) { | |
771 super.removeControl (control); | |
772 CoolItem [] items = getItems (); | |
773 for (int i=0; i<items.length; i++) { | |
774 CoolItem item = items [i]; | |
775 if (item.control is control) item.setControl (null); | |
776 } | |
777 } | |
778 /** | |
779 * Remove the item from the row. Adjust the x and width values | |
780 * appropriately. | |
781 */ | |
782 void removeItemFromRow(CoolItem item, int rowIndex, bool disposed) { | |
783 int index = findItem(item).x; | |
784 int newLength = items[rowIndex].length - 1; | |
785 Rectangle itemBounds = item.internalGetBounds(); | |
786 item.wrap = false; | |
787 if (newLength > 0) { | |
788 CoolItem[] newRow = new CoolItem[newLength]; | |
789 System.arraycopy(items[rowIndex], 0, newRow, 0, index); | |
790 System.arraycopy(items[rowIndex], index + 1, newRow, index, newRow.length - index); | |
791 items[rowIndex] = newRow; | |
792 items[rowIndex][0].wrap = true; | |
793 } else { | |
794 CoolItem[][] newRows = new CoolItem[items.length - 1][]; | |
795 System.arraycopy(items, 0, newRows, 0, rowIndex); | |
796 System.arraycopy(items, rowIndex + 1, newRows, rowIndex, newRows.length - rowIndex); | |
797 items = newRows; | |
798 return; | |
799 } | |
800 if (!disposed) { | |
801 if (index is 0) { | |
802 CoolItem first = items[rowIndex][0]; | |
803 Rectangle bounds = first.internalGetBounds(); | |
804 int width = bounds.x + bounds.width; | |
805 first.setBounds(0, bounds.y, width, bounds.height); | |
806 first.requestedWidth = width; | |
807 internalRedraw(bounds.x, bounds.y, CoolItem.MINIMUM_WIDTH, bounds.height); | |
808 } else { | |
809 CoolItem previous = items[rowIndex][index - 1]; | |
810 Rectangle bounds = previous.internalGetBounds(); | |
811 int width = bounds.width + itemBounds.width; | |
812 previous.setBounds(bounds.x, bounds.y, width, bounds.height); | |
813 previous.requestedWidth = width; | |
814 } | |
815 } | |
816 } | |
817 /** | |
818 * Return the height of the bar after it has | |
819 * been properly laid out for the given width. | |
820 */ | |
821 int layoutItems () { | |
822 int y = 0, width; | |
823 if ((style&DWT.VERTICAL) !is 0) { | |
824 width = getClientArea().height; | |
825 } else { | |
826 width = getClientArea().width; | |
827 } | |
828 wrapItems(width); | |
829 int rowSpacing = (style & DWT.FLAT) !is 0 ? 0 : ROW_SPACING; | |
830 for (int row = 0; row < items.length; row++) { | |
831 int count = items[row].length; | |
832 int x = 0; | |
833 | |
834 /* determine the height and the available width for the row */ | |
835 int rowHeight = 0; | |
836 int available = width; | |
837 for (int i = 0; i < count; i++) { | |
838 CoolItem item = items[row][i]; | |
839 rowHeight = Math.max(rowHeight, item.internalGetBounds().height); | |
840 available -= item.internalGetMinimumWidth(); | |
841 } | |
842 if (row > 0) y += rowSpacing; | |
843 | |
844 /* lay the items out */ | |
845 for (int i = 0; i < count; i++) { | |
846 CoolItem child = items[row][i]; | |
847 int newWidth = available + child.internalGetMinimumWidth(); | |
848 if (i + 1 < count) { | |
849 newWidth = Math.min(newWidth, child.requestedWidth); | |
850 available -= (newWidth - child.internalGetMinimumWidth()); | |
851 } | |
852 Rectangle oldBounds = child.internalGetBounds(); | |
853 Rectangle newBounds = new Rectangle(x, y, newWidth, rowHeight); | |
854 if (!oldBounds.equals(newBounds)) { | |
855 child.setBounds(newBounds.x, newBounds.y, newBounds.width, newBounds.height); | |
856 Rectangle damage = new Rectangle(0, 0, 0, 0); | |
857 /* Cases are in descending order from most area to redraw to least. */ | |
858 if (oldBounds.y !is newBounds.y) { | |
859 damage = newBounds; | |
860 damage.add(oldBounds); | |
861 /* Redraw the row separator as well. */ | |
862 damage.y -= rowSpacing; | |
863 damage.height += 2 * rowSpacing; | |
864 } else if (oldBounds.height !is newBounds.height) { | |
865 /* | |
866 * Draw from the bottom of the gripper to the bottom of the new area. | |
867 * (Bottom of the gripper is -3 from the bottom of the item). | |
868 */ | |
869 damage.y = newBounds.y + Math.min(oldBounds.height, newBounds.height) - 3; | |
870 damage.height = newBounds.y + newBounds.height + rowSpacing; | |
871 damage.x = oldBounds.x - CoolItem.MARGIN_WIDTH; | |
872 damage.width = oldBounds.width + CoolItem.MARGIN_WIDTH; | |
873 } else if (oldBounds.x !is newBounds.x) { | |
874 /* Redraw only the difference between the separators. */ | |
875 damage.x = Math.min(oldBounds.x, newBounds.x); | |
876 damage.width = Math.abs(oldBounds.x - newBounds.x) + CoolItem.MINIMUM_WIDTH; | |
877 damage.y = oldBounds.y; | |
878 damage.height = oldBounds.height; | |
879 } | |
880 internalRedraw(damage.x, damage.y, damage.width, damage.height); | |
881 } | |
882 x += newWidth; | |
883 } | |
884 y += rowHeight; | |
885 } | |
886 return y; | |
887 } | |
888 void relayout() { | |
889 Point size = getSize(); | |
890 int height = layoutItems(); | |
891 if ((style & DWT.VERTICAL) !is 0) { | |
892 Rectangle trim = computeTrim (0, 0, height, 0); | |
893 if (height !is size.x) super.setSize(trim.width, size.y); | |
894 } else { | |
895 Rectangle trim = computeTrim (0, 0, 0, height); | |
896 if (height !is size.y) super.setSize(size.x, trim.height); | |
897 } | |
898 } | |
899 /** | |
900 * Returns an array of zero-relative ints that map | |
901 * the creation order of the receiver's items to the | |
902 * order in which they are currently being displayed. | |
903 * <p> | |
904 * Specifically, the indices of the returned array represent | |
905 * the current visual order of the items, and the contents | |
906 * of the array represent the creation order of the items. | |
907 * </p><p> | |
908 * Note: This is not the actual structure used by the receiver | |
909 * to maintain its list of items, so modifying the array will | |
910 * not affect the receiver. | |
911 * </p> | |
912 * | |
913 * @return the current visual order of the receiver's items | |
914 * | |
915 * @exception DWTException <ul> | |
916 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
917 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
918 * </ul> | |
919 */ | |
920 public int[] getItemOrder () { | |
921 checkWidget (); | |
922 int count = getItemCount (); | |
923 int [] indices = new int [count]; | |
924 count = 0; | |
925 for (int i = 0; i < items.length; i++) { | |
926 for (int j = 0; j < items[i].length; j++) { | |
927 CoolItem item = items[i][j]; | |
928 int index = 0; | |
929 while (index<originalItems.length) { | |
930 if (originalItems [index] is item) break; | |
931 index++; | |
932 } | |
933 if (index is originalItems.length) error (DWT.ERROR_CANNOT_GET_ITEM); | |
934 indices [count++] = index; | |
935 } | |
936 } | |
937 return indices; | |
938 } | |
939 void setItemOrder (int[] itemOrder) { | |
940 if (itemOrder is null) error(DWT.ERROR_NULL_ARGUMENT); | |
941 int count = originalItems.length; | |
942 if (itemOrder.length !is count) error(DWT.ERROR_INVALID_ARGUMENT); | |
943 | |
944 /* Ensure that itemOrder does not contain any duplicates. */ | |
945 bool [] set = new bool [count]; | |
946 for (int i = 0; i < set.length; i++) set [i] = false; | |
947 for (int i = 0; i < itemOrder.length; i++) { | |
948 if (itemOrder [i] < 0 || itemOrder [i] >= count) error (DWT.ERROR_INVALID_ARGUMENT); | |
949 if (set [itemOrder [i]]) error (DWT.ERROR_INVALID_ARGUMENT); | |
950 set [itemOrder [i]] = true; | |
951 } | |
952 | |
953 CoolItem[] row = new CoolItem[count]; | |
954 for (int i = 0; i < count; i++) { | |
955 row[i] = originalItems[itemOrder[i]]; | |
956 } | |
957 items = new CoolItem[1][count]; | |
958 items[0] = row; | |
959 } | |
960 /** | |
961 * Returns an array of points whose x and y coordinates describe | |
962 * the widths and heights (respectively) of the items in the receiver | |
963 * in the order in which they are currently being displayed. | |
964 * | |
965 * @return the receiver's item sizes in their current visual order | |
966 * | |
967 * @exception DWTException <ul> | |
968 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
969 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
970 * </ul> | |
971 */ | |
972 public Point[] getItemSizes () { | |
973 checkWidget(); | |
974 CoolItem[] items = getItems(); | |
975 Point[] sizes = new Point[items.length]; | |
976 for (int i = 0; i < items.length; i++) { | |
977 sizes[i] = items[i].getSize(); | |
978 } | |
979 return sizes; | |
980 } | |
981 void setItemSizes (Point[] sizes) { | |
982 if (sizes is null) error(DWT.ERROR_NULL_ARGUMENT); | |
983 CoolItem[] items = getItems(); | |
984 if (sizes.length !is items.length) error(DWT.ERROR_INVALID_ARGUMENT); | |
985 for (int i = 0; i < items.length; i++) { | |
986 items[i].setSize(sizes[i]); | |
987 } | |
988 } | |
989 /** | |
990 * Returns whether or not the receiver is 'locked'. When a coolbar | |
991 * is locked, its items cannot be repositioned. | |
992 * | |
993 * @return true if the coolbar is locked, false otherwise | |
994 * | |
995 * @exception DWTException <ul> | |
996 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
997 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
998 * </ul> | |
999 * | |
1000 * @since 2.0 | |
1001 */ | |
1002 public bool getLocked () { | |
1003 checkWidget (); | |
1004 return isLocked; | |
1005 } | |
1006 int getWidth () { | |
1007 if ((style & DWT.VERTICAL) !is 0) return getSize().y; | |
1008 return getSize().x; | |
1009 } | |
1010 /** | |
1011 * Returns an array of ints that describe the zero-relative | |
1012 * indices of any item(s) in the receiver that will begin on | |
1013 * a new row. The 0th visible item always begins the first row, | |
1014 * therefore it does not count as a wrap index. | |
1015 * | |
1016 * @return an array containing the receiver's wrap indices, or an empty array if all items are in one row | |
1017 * | |
1018 * @exception DWTException <ul> | |
1019 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1020 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1021 * </ul> | |
1022 */ | |
1023 public int[] getWrapIndices () { | |
1024 checkWidget(); | |
1025 if (items.length <= 1) return new int[]{}; | |
1026 int[] wrapIndices = new int[items.length - 1]; | |
1027 int i = 0, nextWrap = items[0].length; | |
1028 for (int row = 1; row < items.length; row++) { | |
1029 if (items[row][0].wrap) wrapIndices[i++] = nextWrap; | |
1030 nextWrap += items[row].length; | |
1031 } | |
1032 if (i !is wrapIndices.length) { | |
1033 int[] tmp = new int[i]; | |
1034 System.arraycopy(wrapIndices, 0, tmp, 0, i); | |
1035 return tmp; | |
1036 } | |
1037 return wrapIndices; | |
1038 } | |
1039 /** | |
1040 * Sets whether or not the receiver is 'locked'. When a coolbar | |
1041 * is locked, its items cannot be repositioned. | |
1042 * | |
1043 * @param locked lock the coolbar if true, otherwise unlock the coolbar | |
1044 * | |
1045 * @exception DWTException <ul> | |
1046 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1047 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1048 * </ul> | |
1049 * | |
1050 * @since 2.0 | |
1051 */ | |
1052 public void setLocked (bool locked) { | |
1053 checkWidget (); | |
1054 if (isLocked !is locked) { | |
1055 redraw(); | |
1056 } | |
1057 isLocked = locked; | |
1058 | |
1059 } | |
1060 /** | |
1061 * Sets the indices of all item(s) in the receiver that will | |
1062 * begin on a new row. The indices are given in the order in | |
1063 * which they are currently being displayed. The 0th item | |
1064 * always begins the first row, therefore it does not count | |
1065 * as a wrap index. If indices is null or empty, the items | |
1066 * will be placed on one line. | |
1067 * | |
1068 * @param indices an array of wrap indices, or null | |
1069 * | |
1070 * @exception DWTException <ul> | |
1071 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1072 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1073 * </ul> | |
1074 */ | |
1075 public void setWrapIndices (int[] indices) { | |
1076 checkWidget(); | |
1077 if (indices is null) indices = new int[0]; | |
1078 int count = originalItems.length; | |
1079 for (int i=0; i<indices.length; i++) { | |
1080 if (indices[i] < 0 || indices[i] >= count) { | |
1081 error (DWT.ERROR_INVALID_ARGUMENT); | |
1082 } | |
1083 } | |
1084 for (int i=0; i<originalItems.length; i++) { | |
1085 originalItems[i].wrap = false; | |
1086 } | |
1087 for (int i=0; i<indices.length; i++) { | |
1088 int index = indices[i]; | |
1089 for (int row = 0; row < items.length; row++) { | |
1090 if (items[row].length > index) { | |
1091 items[row][index].wrap = true; | |
1092 break; | |
1093 } else { | |
1094 index -= items[row].length; | |
1095 } | |
1096 } | |
1097 } | |
1098 relayout(); | |
1099 } | |
1100 public void setCursor (Cursor cursor) { | |
1101 checkWidget (); | |
1102 super.setCursor (this.cursor = cursor); | |
1103 } | |
1104 /** | |
1105 * Sets the receiver's item order, wrap indices, and item sizes | |
1106 * all at once. This method is typically used to restore the | |
1107 * displayed state of the receiver to a previously stored state. | |
1108 * <p> | |
1109 * The item order is the order in which the items in the receiver | |
1110 * should be displayed, given in terms of the zero-relative ordering | |
1111 * of when the items were added. | |
1112 * </p><p> | |
1113 * The wrap indices are the indices of all item(s) in the receiver | |
1114 * that will begin on a new row. The indices are given in the order | |
1115 * specified by the item order. The 0th item always begins the first | |
1116 * row, therefore it does not count as a wrap index. If wrap indices | |
1117 * is null or empty, the items will be placed on one line. | |
1118 * </p><p> | |
1119 * The sizes are specified in an array of points whose x and y | |
1120 * coordinates describe the new widths and heights (respectively) | |
1121 * of the receiver's items in the order specified by the item order. | |
1122 * </p> | |
1123 * | |
1124 * @param itemOrder an array of indices that describe the new order to display the items in | |
1125 * @param wrapIndices an array of wrap indices, or null | |
1126 * @param sizes an array containing the new sizes for each of the receiver's items in visual order | |
1127 * | |
1128 * @exception DWTException <ul> | |
1129 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1130 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1131 * </ul> | |
1132 * @exception IllegalArgumentException <ul> | |
1133 * <li>ERROR_NULL_ARGUMENT - if item order or sizes is null</li> | |
1134 * <li>ERROR_INVALID_ARGUMENT - if item order or sizes is not the same length as the number of items</li> | |
1135 * </ul> | |
1136 */ | |
1137 public void setItemLayout (int[] itemOrder, int[] wrapIndices, Point[] sizes) { | |
1138 checkWidget(); | |
1139 setItemOrder(itemOrder); | |
1140 setWrapIndices(wrapIndices); | |
1141 setItemSizes(sizes); | |
1142 relayout(); | |
1143 } | |
1144 void wrapItems (int maxWidth) { | |
1145 int itemCount = originalItems.length; | |
1146 if (itemCount < 2) return; | |
1147 CoolItem[] itemsVisual = new CoolItem[itemCount]; | |
1148 int start = 0; | |
1149 for (int row = 0; row < items.length; row++) { | |
1150 System.arraycopy(items[row], 0, itemsVisual, start, items[row].length); | |
1151 start += items[row].length; | |
1152 } | |
1153 CoolItem[][] newItems = new CoolItem[itemCount][]; | |
1154 int rowCount = 0, rowWidth = 0; | |
1155 start = 0; | |
1156 for (int i = 0; i < itemCount; i++) { | |
1157 CoolItem item = itemsVisual[i]; | |
1158 int itemWidth = item.internalGetMinimumWidth(); | |
1159 if ((i > 0 && item.wrap) || (maxWidth !is DWT.DEFAULT && rowWidth + itemWidth > maxWidth)) { | |
1160 if (i is start) { | |
1161 newItems[rowCount] = new CoolItem[1]; | |
1162 newItems[rowCount][0] = item; | |
1163 start = i + 1; | |
1164 rowWidth = 0; | |
1165 } else { | |
1166 int count = i - start; | |
1167 newItems[rowCount] = new CoolItem[count]; | |
1168 System.arraycopy(itemsVisual, start, newItems[rowCount], 0, count); | |
1169 start = i; | |
1170 rowWidth = itemWidth; | |
1171 } | |
1172 rowCount++; | |
1173 } else { | |
1174 rowWidth += itemWidth; | |
1175 } | |
1176 } | |
1177 if (start < itemCount) { | |
1178 int count = itemCount - start; | |
1179 newItems[rowCount] = new CoolItem[count]; | |
1180 System.arraycopy(itemsVisual, start, newItems[rowCount], 0, count); | |
1181 rowCount++; | |
1182 } | |
1183 if (newItems.length !is rowCount) { | |
1184 CoolItem[][] tmp = new CoolItem[rowCount][]; | |
1185 System.arraycopy(newItems, 0, tmp, 0, rowCount); | |
1186 items = tmp; | |
1187 } else { | |
1188 items = newItems; | |
1189 } | |
1190 } | |
1191 } |