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