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