comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/custom/CTabFolder.d @ 0:6dd524f61e62

add dwt win and basic java stuff
author Frank Benoit <benoit@tionex.de>
date Mon, 02 Mar 2009 14:44:16 +0100
parents
children 950d84783eac
comparison
equal deleted inserted replaced
-1:000000000000 0:6dd524f61e62
1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.swt.custom.CTabFolder;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.SWTException;
17 import org.eclipse.swt.accessibility.ACC;
18 import org.eclipse.swt.accessibility.Accessible;
19 import org.eclipse.swt.accessibility.AccessibleAdapter;
20 import org.eclipse.swt.accessibility.AccessibleControlAdapter;
21 import org.eclipse.swt.accessibility.AccessibleControlEvent;
22 import org.eclipse.swt.accessibility.AccessibleEvent;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.events.SelectionListener;
26 import org.eclipse.swt.graphics.Color;
27 import org.eclipse.swt.graphics.Font;
28 import org.eclipse.swt.graphics.FontData;
29 import org.eclipse.swt.graphics.GC;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.swt.graphics.RGB;
33 import org.eclipse.swt.graphics.Rectangle;
34 import org.eclipse.swt.graphics.Region;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.Event;
39 import org.eclipse.swt.widgets.Layout;
40 import org.eclipse.swt.widgets.Listener;
41 import org.eclipse.swt.widgets.Menu;
42 import org.eclipse.swt.widgets.MenuItem;
43 import org.eclipse.swt.widgets.TypedListener;
44 import org.eclipse.swt.custom.CTabItem;
45 import org.eclipse.swt.custom.CTabFolder2Listener;
46 import org.eclipse.swt.custom.CTabFolderListener;
47 import org.eclipse.swt.custom.CTabFolderLayout;
48 import org.eclipse.swt.custom.CTabFolderEvent;
49
50 import java.lang.all;
51 import tango.util.Convert;
52 static import tango.text.convert.Utf;
53
54 /**
55 *
56 * Instances of this class implement the notebook user interface
57 * metaphor. It allows the user to select a notebook page from
58 * set of pages.
59 * <p>
60 * The item children that may be added to instances of this class
61 * must be of type <code>CTabItem</code>.
62 * <code>Control</code> children are created and then set into a
63 * tab item using <code>CTabItem#setControl</code>.
64 * </p><p>
65 * Note that although this class is a subclass of <code>Composite</code>,
66 * it does not make sense to set a layout on it.
67 * </p><p>
68 * <dl>
69 * <dt><b>Styles:</b></dt>
70 * <dd>CLOSE, TOP, BOTTOM, FLAT, BORDER, SINGLE, MULTI</dd>
71 * <dt><b>Events:</b></dt>
72 * <dd>Selection</dd>
73 * <dd>"CTabFolder2"</dd>
74 * </dl>
75 * <p>
76 * Note: Only one of the styles TOP and BOTTOM
77 * may be specified.
78 * </p><p>
79 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
80 * </p>
81 *
82 * @see <a href="http://www.eclipse.org/swt/snippets/#ctabfolder">CTabFolder, CTabItem snippets</a>
83 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: CustomControlExample</a>
84 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
85 */
86
87 public class CTabFolder : Composite {
88
89 /**
90 * marginWidth specifies the number of pixels of horizontal margin
91 * that will be placed along the left and right edges of the form.
92 *
93 * The default value is 0.
94 */
95 public int marginWidth = 0;
96 /**
97 * marginHeight specifies the number of pixels of vertical margin
98 * that will be placed along the top and bottom edges of the form.
99 *
100 * The default value is 0.
101 */
102 public int marginHeight = 0;
103
104 /**
105 * A multiple of the tab height that specifies the minimum width to which a tab
106 * will be compressed before scrolling arrows are used to navigate the tabs.
107 *
108 * NOTE This field is badly named and can not be fixed for backwards compatibility.
109 * It should not be capitalized.
110 *
111 * @deprecated This field is no longer used. See setMinimumCharacters(int)
112 */
113 public int MIN_TAB_WIDTH = 4;
114
115 /**
116 * Color of innermost line of drop shadow border.
117 *
118 * NOTE This field is badly named and can not be fixed for backwards compatibility.
119 * It should be capitalized.
120 *
121 * @deprecated drop shadow border is no longer drawn in 3.0
122 */
123 public static RGB borderInsideRGB;
124 /**
125 * Color of middle line of drop shadow border.
126 *
127 * NOTE This field is badly named and can not be fixed for backwards compatibility.
128 * It should be capitalized.
129 *
130 * @deprecated drop shadow border is no longer drawn in 3.0
131 */
132 public static RGB borderMiddleRGB;
133 /**
134 * Color of outermost line of drop shadow border.
135 *
136 * NOTE This field is badly named and can not be fixed for backwards compatibility.
137 * It should be capitalized.
138 *
139 * @deprecated drop shadow border is no longer drawn in 3.0
140 */
141 public static RGB borderOutsideRGB;
142
143 /* sizing, positioning */
144 int xClient, yClient;
145 bool onBottom = false;
146 bool single = false;
147 bool simple = true;
148 int fixedTabHeight = SWT.DEFAULT;
149 int tabHeight;
150 int minChars = 20;
151
152 /* item management */
153 CTabItem items[];
154 int firstIndex = -1; // index of the left most visible tab.
155 int selectedIndex = -1;
156 int[] priority;
157 bool mru = false;
158 Listener listener;
159
160 /* External Listener management */
161 CTabFolder2Listener[] folderListeners;
162 // support for deprecated listener mechanism
163 CTabFolderListener[] tabListeners;
164
165 /* Selected item appearance */
166 Image selectionBgImage;
167 Color[] selectionGradientColors;
168 int[] selectionGradientPercents;
169 bool selectionGradientVertical;
170 Color selectionForeground;
171 Color selectionBackground; //selection fade end
172 Color selectionFadeStart;
173
174 Color selectionHighlightGradientBegin = null; //null is no highlight
175 //Although we are given new colours all the time to show different states (active, etc),
176 //some of which may have a highlight and some not, we'd like to retain the highlight colours
177 //as a cache so that we can reuse them if we're again told to show the highlight.
178 //We are relying on the fact that only one tab state usually gets a highlight, so only
179 //a single cache is required. If that happens to not be true, cache simply becomes less effective,
180 //but we don't leak colours.
181 Color[] selectionHighlightGradientColorsCache = null; //null is a legal value, check on access
182
183 /* Unselected item appearance */
184 Image bgImage;
185 Color[] gradientColors;
186 int[] gradientPercents;
187 bool gradientVertical;
188 bool showUnselectedImage = true;
189
190 static Color borderColor;
191
192 // close, min/max and chevron buttons
193 bool showClose = false;
194 bool showUnselectedClose = true;
195
196 Rectangle chevronRect;
197 int chevronImageState = NORMAL;
198 bool showChevron = false;
199 Menu showMenu;
200
201 bool showMin = false;
202 Rectangle minRect;
203 bool minimized = false;
204 int minImageState = NORMAL;
205
206 bool showMax = false;
207 Rectangle maxRect;
208 bool maximized = false;
209 int maxImageState = NORMAL;
210
211 Control topRight;
212 Rectangle topRightRect;
213 int topRightAlignment = SWT.RIGHT;
214
215 // borders and shapes
216 int borderLeft = 0;
217 int borderRight = 0;
218 int borderTop = 0;
219 int borderBottom = 0;
220
221 int highlight_margin = 0;
222 int highlight_header = 0;
223
224 int[] curve;
225 int[] topCurveHighlightStart;
226 int[] topCurveHighlightEnd;
227 int curveWidth = 0;
228 int curveIndent = 0;
229
230 // when disposing CTabFolder, don't try to layout the items or
231 // change the selection as each child is destroyed.
232 bool inDispose = false;
233
234 // keep track of size changes in order to redraw only affected area
235 // on Resize
236 Point oldSize;
237 Font oldFont;
238
239 // internal constants
240 static const int DEFAULT_WIDTH = 64;
241 static const int DEFAULT_HEIGHT = 64;
242 static const int BUTTON_SIZE = 18;
243
244 static const int[] TOP_LEFT_CORNER = [0,6, 1,5, 1,4, 4,1, 5,1, 6,0];
245
246 //TOP_LEFT_CORNER_HILITE is laid out in reverse (ie. top to bottom)
247 //so can fade in same direction as right swoop curve
248 static const int[] TOP_LEFT_CORNER_HILITE = [5,2, 4,2, 3,3, 2,4, 2,5, 1,6];
249
250 static const int[] TOP_RIGHT_CORNER = [-6,0, -5,1, -4,1, -1,4, -1,5, 0,6];
251 static const int[] BOTTOM_LEFT_CORNER = [0,-6, 1,-5, 1,-4, 4,-1, 5,-1, 6,0];
252 static const int[] BOTTOM_RIGHT_CORNER = [-6,0, -5,-1, -4,-1, -1,-4, -1,-5, 0,-6];
253
254 static const int[] SIMPLE_TOP_LEFT_CORNER = [0,2, 1,1, 2,0];
255 static const int[] SIMPLE_TOP_RIGHT_CORNER = [-2,0, -1,1, 0,2];
256 static const int[] SIMPLE_BOTTOM_LEFT_CORNER = [0,-2, 1,-1, 2,0];
257 static const int[] SIMPLE_BOTTOM_RIGHT_CORNER = [-2,0, -1,-1, 0,-2];
258 static const int[] SIMPLE_UNSELECTED_INNER_CORNER = [0,0];
259
260 static const int[] TOP_LEFT_CORNER_BORDERLESS = [0,6, 1,5, 1,4, 4,1, 5,1, 6,0];
261 static const int[] TOP_RIGHT_CORNER_BORDERLESS = [-7,0, -6,1, -5,1, -2,4, -2,5, -1,6];
262 static const int[] BOTTOM_LEFT_CORNER_BORDERLESS = [0,-6, 1,-6, 1,-5, 2,-4, 4,-2, 5,-1, 6,-1, 6,0];
263 static const int[] BOTTOM_RIGHT_CORNER_BORDERLESS = [-7,0, -7,-1, -6,-1, -5,-2, -3,-4, -2,-5, -2,-6, -1,-6];
264
265 static const int[] SIMPLE_TOP_LEFT_CORNER_BORDERLESS = [0,2, 1,1, 2,0];
266 static const int[] SIMPLE_TOP_RIGHT_CORNER_BORDERLESS= [-3,0, -2,1, -1,2];
267 static const int[] SIMPLE_BOTTOM_LEFT_CORNER_BORDERLESS = [0,-3, 1,-2, 2,-1, 3,0];
268 static const int[] SIMPLE_BOTTOM_RIGHT_CORNER_BORDERLESS = [-4,0, -3,-1, -2,-2, -1,-3];
269
270 static const int SELECTION_FOREGROUND = SWT.COLOR_LIST_FOREGROUND;
271 static const int SELECTION_BACKGROUND = SWT.COLOR_LIST_BACKGROUND;
272 static const int BORDER1_COLOR = SWT.COLOR_WIDGET_NORMAL_SHADOW;
273 static const int FOREGROUND = SWT.COLOR_WIDGET_FOREGROUND;
274 static const int BACKGROUND = SWT.COLOR_WIDGET_BACKGROUND;
275 static const int BUTTON_BORDER = SWT.COLOR_WIDGET_DARK_SHADOW;
276 static const int BUTTON_FILL = SWT.COLOR_LIST_BACKGROUND;
277
278 static const int NONE = 0;
279 static const int NORMAL = 1;
280 static const int HOT = 2;
281 static const int SELECTED = 3;
282 static const RGB CLOSE_FILL;
283
284 static const int CHEVRON_CHILD_ID = 0;
285 static const int MINIMIZE_CHILD_ID = 1;
286 static const int MAXIMIZE_CHILD_ID = 2;
287 static const int EXTRA_CHILD_ID_COUNT = 3;
288
289 static this(){
290 borderInsideRGB = new RGB (132, 130, 132);
291 borderMiddleRGB = new RGB (143, 141, 138);
292 borderOutsideRGB = new RGB (171, 168, 165);
293 CLOSE_FILL = new RGB(252, 160, 160);
294 }
295
296 /**
297 * Constructs a new instance of this class given its parent
298 * and a style value describing its behavior and appearance.
299 * <p>
300 * The style value is either one of the style constants defined in
301 * class <code>SWT</code> which is applicable to instances of this
302 * class, or must be built by <em>bitwise OR</em>'ing together
303 * (that is, using the <code>int</code> "|" operator) two or more
304 * of those <code>SWT</code> style constants. The class description
305 * lists the style constants that are applicable to the class.
306 * Style bits are also inherited from superclasses.
307 * </p>
308 *
309 * @param parent a widget which will be the parent of the new instance (cannot be null)
310 * @param style the style of widget to construct
311 *
312 * @exception IllegalArgumentException <ul>
313 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
314 * </ul>
315 * @exception SWTException <ul>
316 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
317 * </ul>
318 *
319 * @see SWT#TOP
320 * @see SWT#BOTTOM
321 * @see SWT#FLAT
322 * @see SWT#BORDER
323 * @see SWT#SINGLE
324 * @see SWT#MULTI
325 * @see #getStyle()
326 */
327 public this(Composite parent, int style) {
328 chevronRect = new Rectangle(0, 0, 0, 0);
329 minRect = new Rectangle(0, 0, 0, 0);
330 maxRect = new Rectangle(0, 0, 0, 0);
331 topRightRect = new Rectangle(0, 0, 0, 0);
332 super(parent, checkStyle (parent, style));
333 super.setLayout(new CTabFolderLayout());
334 int style2 = super.getStyle();
335 oldFont = getFont();
336 onBottom = (style2 & SWT.BOTTOM) !is 0;
337 showClose = (style2 & SWT.CLOSE) !is 0;
338 // showMin = (style2 & SWT.MIN) !is 0; - conflicts with SWT.TOP
339 // showMax = (style2 & SWT.MAX) !is 0; - conflicts with SWT.BOTTOM
340 single = (style2 & SWT.SINGLE) !is 0;
341 borderLeft = borderRight = (style & SWT.BORDER) !is 0 ? 1 : 0;
342 borderTop = onBottom ? borderLeft : 0;
343 borderBottom = onBottom ? 0 : borderLeft;
344 highlight_header = (style & SWT.FLAT) !is 0 ? 1 : 3;
345 highlight_margin = (style & SWT.FLAT) !is 0 ? 0 : 2;
346 //set up default colors
347 Display display = getDisplay();
348 selectionForeground = display.getSystemColor(SELECTION_FOREGROUND);
349 selectionBackground = display.getSystemColor(SELECTION_BACKGROUND);
350 borderColor = display.getSystemColor(BORDER1_COLOR);
351 updateTabHeight(false);
352
353 initAccessible();
354
355 // Add all listeners
356 listener = new class() Listener {
357 public void handleEvent(Event event) {
358 switch (event.type) {
359 case SWT.Dispose: onDispose(event); break;
360 case SWT.DragDetect: onDragDetect(event); break;
361 case SWT.FocusIn: onFocus(event); break;
362 case SWT.FocusOut: onFocus(event); break;
363 case SWT.KeyDown: onKeyDown(event); break;
364 case SWT.MouseDoubleClick: onMouseDoubleClick(event); break;
365 case SWT.MouseDown: onMouse(event); break;
366 case SWT.MouseEnter: onMouse(event); break;
367 case SWT.MouseExit: onMouse(event); break;
368 case SWT.MouseMove: onMouse(event); break;
369 case SWT.MouseUp: onMouse(event); break;
370 case SWT.Paint: onPaint(event); break;
371 case SWT.Resize: onResize(); break;
372 case SWT.Traverse: onTraverse(event); break;
373 default:
374 }
375 }
376 };
377
378 int[] folderEvents = [
379 SWT.Dispose,
380 SWT.DragDetect,
381 SWT.FocusIn,
382 SWT.FocusOut,
383 SWT.KeyDown,
384 SWT.MouseDoubleClick,
385 SWT.MouseDown,
386 SWT.MouseEnter,
387 SWT.MouseExit,
388 SWT.MouseMove,
389 SWT.MouseUp,
390 SWT.Paint,
391 SWT.Resize,
392 SWT.Traverse,
393 ];
394 for (int i = 0; i < folderEvents.length; i++) {
395 addListener(folderEvents[i], listener);
396 }
397 }
398 static int checkStyle (Composite parent, int style) {
399 int mask = SWT.CLOSE | SWT.TOP | SWT.BOTTOM | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT | SWT.SINGLE | SWT.MULTI;
400 style = style & mask;
401 // TOP and BOTTOM are mutually exclusive.
402 // TOP is the default
403 if ((style & SWT.TOP) !is 0) style = style & ~SWT.BOTTOM;
404 // SINGLE and MULTI are mutually exclusive.
405 // MULTI is the default
406 if ((style & SWT.MULTI) !is 0) style = style & ~SWT.SINGLE;
407 // reduce the flash by not redrawing the entire area on a Resize event
408 style |= SWT.NO_REDRAW_RESIZE;
409 //TEMPORARY CODE
410 /*
411 * The default background on carbon and some GTK themes is not a solid color
412 * but a texture. To show the correct default background, we must allow
413 * the operating system to draw it and therefore, we can not use the
414 * NO_BACKGROUND style. The NO_BACKGROUND style is not required on platforms
415 * that use double buffering which is true in both of these cases.
416 */
417 String platform = SWT.getPlatform();
418 if ("carbon"==platform || "gtk"==platform) return style; //$NON-NLS-1$ //$NON-NLS-2$
419
420 //TEMPORARY CODE
421 /*
422 * In Right To Left orientation on Windows, all GC calls that use a brush are drawing
423 * offset by one pixel. This results in some parts of the CTabFolder not drawing correctly.
424 * To alleviate some of the appearance problems, allow the OS to draw the background.
425 * This does not draw correctly but the result is less obviously wrong.
426 */
427 if ((style & SWT.RIGHT_TO_LEFT) !is 0) return style;
428 if ((parent.getStyle() & SWT.MIRRORED) !is 0 && (style & SWT.LEFT_TO_RIGHT) is 0) return style;
429
430 return style | SWT.NO_BACKGROUND;
431 }
432 static void fillRegion(GC gc, Region region) {
433 // NOTE: region passed in to this function will be modified
434 Region clipping = new Region();
435 gc.getClipping(clipping);
436 region.intersect(clipping);
437 gc.setClipping(region);
438 gc.fillRectangle(region.getBounds());
439 gc.setClipping(clipping);
440 clipping.dispose();
441 }
442 /**
443 *
444 * Adds the listener to the collection of listeners who will
445 * be notified when a tab item is closed, minimized, maximized,
446 * restored, or to show the list of items that are not
447 * currently visible.
448 *
449 * @param listener the listener which should be notified
450 *
451 * @exception IllegalArgumentException <ul>
452 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
453 * </ul>
454 *
455 * @exception SWTException <ul>
456 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
457 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
458 * </ul>
459 *
460 * @see CTabFolder2Listener
461 * @see #removeCTabFolder2Listener(CTabFolder2Listener)
462 *
463 * @since 3.0
464 */
465 public void addCTabFolder2Listener(CTabFolder2Listener listener) {
466 checkWidget();
467 if (listener is null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
468 // add to array
469 CTabFolder2Listener[] newListeners = new CTabFolder2Listener[folderListeners.length + 1];
470 SimpleType!(CTabFolder2Listener).arraycopy(folderListeners, 0, newListeners, 0, folderListeners.length);
471 folderListeners = newListeners;
472 folderListeners[folderListeners.length - 1] = listener;
473 }
474 /**
475 * Adds the listener to the collection of listeners who will
476 * be notified when a tab item is closed.
477 *
478 * @param listener the listener which should be notified
479 *
480 * @exception IllegalArgumentException <ul>
481 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
482 * </ul>
483 * @exception SWTException <ul>
484 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
485 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
486 * </ul>
487 *
488 * @see CTabFolderListener
489 * @see #removeCTabFolderListener(CTabFolderListener)
490 *
491 * @deprecated use addCTabFolder2Listener(CTabFolder2Listener)
492 */
493 public void addCTabFolderListener(CTabFolderListener listener) {
494 checkWidget();
495 if (listener is null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
496 // add to array
497 CTabFolderListener[] newTabListeners = new CTabFolderListener[tabListeners.length + 1];
498 SimpleType!(CTabFolderListener).arraycopy(tabListeners, 0, newTabListeners, 0, tabListeners.length);
499 tabListeners = newTabListeners;
500 tabListeners[tabListeners.length - 1] = listener;
501 // display close button to be backwards compatible
502 if (!showClose) {
503 showClose = true;
504 updateItems();
505 redraw();
506 }
507 }
508 /**
509 * Adds the listener to the collection of listeners who will
510 * be notified when the user changes the receiver's selection, by sending
511 * it one of the messages defined in the <code>SelectionListener</code>
512 * interface.
513 * <p>
514 * <code>widgetSelected</code> is called when the user changes the selected tab.
515 * <code>widgetDefaultSelected</code> is not called.
516 * </p>
517 *
518 * @param listener the listener which should be notified when the user changes the receiver's selection
519 *
520 * @exception IllegalArgumentException <ul>
521 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
522 * </ul>
523 * @exception SWTException <ul>
524 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
525 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
526 * </ul>
527 *
528 * @see SelectionListener
529 * @see #removeSelectionListener
530 * @see SelectionEvent
531 */
532 public void addSelectionListener(SelectionListener listener) {
533 checkWidget();
534 if (listener is null) {
535 SWT.error(SWT.ERROR_NULL_ARGUMENT);
536 }
537 TypedListener typedListener = new TypedListener(listener);
538 addListener(SWT.Selection, typedListener);
539 addListener(SWT.DefaultSelection, typedListener);
540 }
541 void antialias (int[] shape, RGB lineRGB, RGB innerRGB, RGB outerRGB, GC gc){
542 // Don't perform anti-aliasing on Mac and WPF because the platform
543 // already does it. The simple style also does not require anti-aliasing.
544 if (simple || "carbon".equals(SWT.getPlatform()) || "wpf".equals(SWT.getPlatform())) return; //$NON-NLS-1$
545 // Don't perform anti-aliasing on low resolution displays
546 if (getDisplay().getDepth() < 15) return;
547 if (outerRGB !is null) {
548 int index = 0;
549 bool left = true;
550 int oldY = onBottom ? 0 : getSize().y;
551 int[] outer = new int[shape.length];
552 for (int i = 0; i < shape.length/2; i++) {
553 if (left && (index + 3 < shape.length)) {
554 left = onBottom ? oldY <= shape[index+3] : oldY >= shape[index+3];
555 oldY = shape[index+1];
556 }
557 outer[index] = shape[index] + (left ? -1 : +1);
558 index++;
559 outer[index] = shape[index];
560 index++;
561 }
562 RGB from = lineRGB;
563 RGB to = outerRGB;
564 int red = from.red + 2*(to.red - from.red)/3;
565 int green = from.green + 2*(to.green - from.green)/3;
566 int blue = from.blue + 2*(to.blue - from.blue)/3;
567 Color color = new Color(getDisplay(), red, green, blue);
568 gc.setForeground(color);
569 gc.drawPolyline(outer);
570 color.dispose();
571 }
572 if (innerRGB !is null) {
573 int[] inner = new int[shape.length];
574 int index = 0;
575 bool left = true;
576 int oldY = onBottom ? 0 : getSize().y;
577 for (int i = 0; i < shape.length/2; i++) {
578 if (left && (index + 3 < shape.length)) {
579 left = onBottom ? oldY <= shape[index+3] : oldY >= shape[index+3];
580 oldY = shape[index+1];
581 }
582 inner[index] = shape[index] + (left ? +1 : -1);
583 index++;
584 inner[index] = shape[index];
585 index++;
586 }
587 RGB from = lineRGB;
588 RGB to = innerRGB;
589 int red = from.red + 2*(to.red - from.red)/3;
590 int green = from.green + 2*(to.green - from.green)/3;
591 int blue = from.blue + 2*(to.blue - from.blue)/3;
592 Color color = new Color(getDisplay(), red, green, blue);
593 gc.setForeground(color);
594 gc.drawPolyline(inner);
595 color.dispose();
596 }
597 }
598 public override Rectangle computeTrim (int x, int y, int width, int height) {
599 checkWidget();
600 int trimX = x - marginWidth - highlight_margin - borderLeft;
601 int trimWidth = width + borderLeft + borderRight + 2*marginWidth + 2*highlight_margin;
602 if (minimized) {
603 int trimY = onBottom ? y - borderTop : y - highlight_header - tabHeight - borderTop;
604 int trimHeight = borderTop + borderBottom + tabHeight + highlight_header;
605 return new Rectangle (trimX, trimY, trimWidth, trimHeight);
606 } else {
607 int trimY = onBottom ? y - marginHeight - highlight_margin - borderTop: y - marginHeight - highlight_header - tabHeight - borderTop;
608 int trimHeight = height + borderTop + borderBottom + 2*marginHeight + tabHeight + highlight_header + highlight_margin;
609 return new Rectangle (trimX, trimY, trimWidth, trimHeight);
610 }
611 }
612 void createItem (CTabItem item, int index) {
613 if (0 > index || index > getItemCount ())SWT.error (SWT.ERROR_INVALID_RANGE);
614 item.parent = this;
615 CTabItem[] newItems = new CTabItem [items.length + 1];
616 System.arraycopy(items, 0, newItems, 0, index);
617 newItems[index] = item;
618 System.arraycopy(items, index, newItems, index + 1, items.length - index);
619 items = newItems;
620 if (selectedIndex >= index) selectedIndex ++;
621 int[] newPriority = new int[priority.length + 1];
622 int next = 0, priorityIndex = priority.length;
623 for (int i = 0; i < priority.length; i++) {
624 if (!mru && priority[i] is index) {
625 priorityIndex = next++;
626 }
627 newPriority[next++] = priority[i] >= index ? priority[i] + 1 : priority[i];
628 }
629 newPriority[priorityIndex] = index;
630 priority = newPriority;
631
632 if (items.length is 1) {
633 if (!updateTabHeight(false)) updateItems();
634 redraw();
635 } else {
636 updateItems();
637 redrawTabs();
638 }
639 }
640 void destroyItem (CTabItem item) {
641 if (inDispose) return;
642 int index = indexOf(item);
643 if (index is -1) return;
644
645 if (items.length is 1) {
646 items = new CTabItem[0];
647 priority = new int[0];
648 firstIndex = -1;
649 selectedIndex = -1;
650
651 Control control = item.getControl();
652 if (control !is null && !control.isDisposed()) {
653 control.setVisible(false);
654 }
655 setToolTipText(null);
656 setButtonBounds();
657 redraw();
658 return;
659 }
660
661 CTabItem[] newItems = new CTabItem [items.length - 1];
662 System.arraycopy(items, 0, newItems, 0, index);
663 System.arraycopy(items, index + 1, newItems, index, items.length - index - 1);
664 items = newItems;
665
666 int[] newPriority = new int[priority.length - 1];
667 int next = 0;
668 for (int i = 0; i < priority.length; i++) {
669 if (priority [i] is index) continue;
670 newPriority[next++] = priority[i] > index ? priority[i] - 1 : priority [i];
671 }
672 priority = newPriority;
673
674 // move the selection if this item is selected
675 if (selectedIndex is index) {
676 Control control = item.getControl();
677 selectedIndex = -1;
678 int nextSelection = mru ? priority[0] : Math.max(0, index - 1);
679 setSelection(nextSelection, true);
680 if (control !is null && !control.isDisposed()) {
681 control.setVisible(false);
682 }
683 } else if (selectedIndex > index) {
684 selectedIndex --;
685 }
686
687 updateItems();
688 redrawTabs();
689 }
690 void drawBackground(GC gc, int[] shape, bool selected) {
691 Color defaultBackground = selected ? selectionBackground : getBackground();
692 Image image = selected ? selectionBgImage : bgImage;
693 Color[] colors = selected ? selectionGradientColors : gradientColors;
694 int[] percents = selected ? selectionGradientPercents : gradientPercents;
695 bool vertical = selected ? selectionGradientVertical : gradientVertical;
696 Point size = getSize();
697 int width = size.x;
698 int height = tabHeight + highlight_header;
699 int x = 0;
700 if (borderLeft > 0) {
701 x += 1; width -= 2;
702 }
703 int y = onBottom ? size.y - borderBottom - height : borderTop;
704 drawBackground(gc, shape, x, y, width, height, defaultBackground, image, colors, percents, vertical);
705 }
706 void drawBackground(GC gc, int[] shape, int x, int y, int width, int height, Color defaultBackground, Image image, Color[] colors, int[] percents, bool vertical) {
707 Region clipping = new Region();
708 gc.getClipping(clipping);
709 Region region = new Region();
710 region.add(shape);
711 region.intersect(clipping);
712 gc.setClipping(region);
713
714 if (image !is null) {
715 // draw the background image in shape
716 gc.setBackground(defaultBackground);
717 gc.fillRectangle(x, y, width, height);
718 Rectangle imageRect = image.getBounds();
719 gc.drawImage(image, imageRect.x, imageRect.y, imageRect.width, imageRect.height, x, y, width, height);
720 } else if (colors !is null) {
721 // draw gradient
722 if (colors.length is 1) {
723 Color background = colors[0] !is null ? colors[0] : defaultBackground;
724 gc.setBackground(background);
725 gc.fillRectangle(x, y, width, height);
726 } else {
727 if (vertical) {
728 if (onBottom) {
729 int pos = 0;
730 if (percents[percents.length - 1] < 100) {
731 pos = percents[percents.length - 1] * height / 100;
732 gc.setBackground(defaultBackground);
733 gc.fillRectangle(x, y, width, pos);
734 }
735 Color lastColor = colors[colors.length-1];
736 if (lastColor is null) lastColor = defaultBackground;
737 for (int i = percents.length-1; i >= 0; i--) {
738 gc.setForeground(lastColor);
739 lastColor = colors[i];
740 if (lastColor is null) lastColor = defaultBackground;
741 gc.setBackground(lastColor);
742 int gradientHeight = percents[i] * height / 100;
743 gc.fillGradientRectangle(x, y+pos, width, gradientHeight, true);
744 pos += gradientHeight;
745 }
746 } else {
747 Color lastColor = colors[0];
748 if (lastColor is null) lastColor = defaultBackground;
749 int pos = 0;
750 for (int i = 0; i < percents.length; i++) {
751 gc.setForeground(lastColor);
752 lastColor = colors[i + 1];
753 if (lastColor is null) lastColor = defaultBackground;
754 gc.setBackground(lastColor);
755 int gradientHeight = percents[i] * height / 100;
756 gc.fillGradientRectangle(x, y+pos, width, gradientHeight, true);
757 pos += gradientHeight;
758 }
759 if (pos < height) {
760 gc.setBackground(defaultBackground);
761 gc.fillRectangle(x, pos, width, height-pos+1);
762 }
763 }
764 } else { //horizontal gradient
765 y = 0;
766 height = getSize().y;
767 Color lastColor = colors[0];
768 if (lastColor is null) lastColor = defaultBackground;
769 int pos = 0;
770 for (int i = 0; i < percents.length; ++i) {
771 gc.setForeground(lastColor);
772 lastColor = colors[i + 1];
773 if (lastColor is null) lastColor = defaultBackground;
774 gc.setBackground(lastColor);
775 int gradientWidth = (percents[i] * width / 100) - pos;
776 gc.fillGradientRectangle(x+pos, y, gradientWidth, height, false);
777 pos += gradientWidth;
778 }
779 if (pos < width) {
780 gc.setBackground(defaultBackground);
781 gc.fillRectangle(x+pos, y, width-pos, height);
782 }
783 }
784 }
785 } else {
786 // draw a solid background using default background in shape
787 if ((getStyle() & SWT.NO_BACKGROUND) !is 0 || defaultBackground!=getBackground()) {
788 gc.setBackground(defaultBackground);
789 gc.fillRectangle(x, y, width, height);
790 }
791 }
792 gc.setClipping(clipping);
793 clipping.dispose();
794 region.dispose();
795 }
796 void drawBody(Event event) {
797 GC gc = event.gc;
798 Point size = getSize();
799
800 // fill in body
801 if (!minimized){
802 int width = size.x - borderLeft - borderRight - 2*highlight_margin;
803 int height = size.y - borderTop - borderBottom - tabHeight - highlight_header - highlight_margin;
804 // Draw highlight margin
805 if (highlight_margin > 0) {
806 int[] shape = null;
807 if (onBottom) {
808 int x1 = borderLeft;
809 int y1 = borderTop;
810 int x2 = size.x - borderRight;
811 int y2 = size.y - borderBottom - tabHeight - highlight_header;
812 shape = [x1,y1, x2,y1, x2,y2, x2-highlight_margin,y2,
813 x2-highlight_margin, y1+highlight_margin, x1+highlight_margin,y1+highlight_margin,
814 x1+highlight_margin,y2, x1,y2];
815 } else {
816 int x1 = borderLeft;
817 int y1 = borderTop + tabHeight + highlight_header;
818 int x2 = size.x - borderRight;
819 int y2 = size.y - borderBottom;
820 shape = [x1,y1, x1+highlight_margin,y1, x1+highlight_margin,y2-highlight_margin,
821 x2-highlight_margin,y2-highlight_margin, x2-highlight_margin,y1,
822 x2,y1, x2,y2, x1,y2];
823 }
824 // If horizontal gradient, show gradient across the whole area
825 if (selectedIndex !is -1 && selectionGradientColors !is null && selectionGradientColors.length > 1 && !selectionGradientVertical) {
826 drawBackground(gc, shape, true);
827 } else if (selectedIndex is -1 && gradientColors !is null && gradientColors.length > 1 && !gradientVertical) {
828 drawBackground(gc, shape, false);
829 } else {
830 gc.setBackground(selectedIndex is -1 ? getBackground() : selectionBackground);
831 gc.fillPolygon(shape);
832 }
833 }
834 //Draw client area
835 if ((getStyle() & SWT.NO_BACKGROUND) !is 0) {
836 gc.setBackground(getBackground());
837 gc.fillRectangle(xClient - marginWidth, yClient - marginHeight, width, height);
838 }
839 } else {
840 if ((getStyle() & SWT.NO_BACKGROUND) !is 0) {
841 int height = borderTop + tabHeight + highlight_header + borderBottom;
842 if (size.y > height) {
843 gc.setBackground(getParent().getBackground());
844 gc.fillRectangle(0, height, size.x, size.y - height);
845 }
846 }
847 }
848
849 //draw 1 pixel border around outside
850 if (borderLeft > 0) {
851 gc.setForeground(borderColor);
852 int x1 = borderLeft - 1;
853 int x2 = size.x - borderRight;
854 int y1 = onBottom ? borderTop - 1 : borderTop + tabHeight;
855 int y2 = onBottom ? size.y - tabHeight - borderBottom - 1 : size.y - borderBottom;
856 gc.drawLine(x1, y1, x1, y2); // left
857 gc.drawLine(x2, y1, x2, y2); // right
858 if (onBottom) {
859 gc.drawLine(x1, y1, x2, y1); // top
860 } else {
861 gc.drawLine(x1, y2, x2, y2); // bottom
862 }
863 }
864 }
865
866 void drawChevron(GC gc) {
867 if (chevronRect.width is 0 || chevronRect.height is 0) return;
868 // draw chevron (10x7)
869 Display display = getDisplay();
870 Point dpi = display.getDPI();
871 int fontHeight = 72 * 10 / dpi.y;
872 FontData fd = getFont().getFontData()[0];
873 fd.setHeight(fontHeight);
874 Font f = new Font(display, fd);
875 int fHeight = f.getFontData()[0].getHeight() * dpi.y / 72;
876 int indent = Math.max(2, (chevronRect.height - fHeight - 4) /2);
877 int x = chevronRect.x + 2;
878 int y = chevronRect.y + indent;
879 int count;
880 if (single) {
881 count = selectedIndex is -1 ? items.length : items.length - 1;
882 } else {
883 int showCount = 0;
884 while (showCount < priority.length && items[priority[showCount]].showing) {
885 showCount++;
886 }
887 count = items.length - showCount;
888 }
889 String chevronString = count > 99 ? "99+" : to!(String)(count); //$NON-NLS-1$
890 switch (chevronImageState) {
891 case NORMAL: {
892 Color chevronBorder = single ? getSelectionForeground() : getForeground();
893 gc.setForeground(chevronBorder);
894 gc.setFont(f);
895 gc.drawLine(x,y, x+2,y+2);
896 gc.drawLine(x+2,y+2, x,y+4);
897 gc.drawLine(x+1,y, x+3,y+2);
898 gc.drawLine(x+3,y+2, x+1,y+4);
899 gc.drawLine(x+4,y, x+6,y+2);
900 gc.drawLine(x+6,y+2, x+5,y+4);
901 gc.drawLine(x+5,y, x+7,y+2);
902 gc.drawLine(x+7,y+2, x+4,y+4);
903 gc.drawString(chevronString, x+7, y+3, true);
904 break;
905 }
906 case HOT: {
907 gc.setForeground(display.getSystemColor(BUTTON_BORDER));
908 gc.setBackground(display.getSystemColor(BUTTON_FILL));
909 gc.setFont(f);
910 gc.fillRoundRectangle(chevronRect.x, chevronRect.y, chevronRect.width, chevronRect.height, 6, 6);
911 gc.drawRoundRectangle(chevronRect.x, chevronRect.y, chevronRect.width - 1, chevronRect.height - 1, 6, 6);
912 gc.drawLine(x,y, x+2,y+2);
913 gc.drawLine(x+2,y+2, x,y+4);
914 gc.drawLine(x+1,y, x+3,y+2);
915 gc.drawLine(x+3,y+2, x+1,y+4);
916 gc.drawLine(x+4,y, x+6,y+2);
917 gc.drawLine(x+6,y+2, x+5,y+4);
918 gc.drawLine(x+5,y, x+7,y+2);
919 gc.drawLine(x+7,y+2, x+4,y+4);
920 gc.drawString(chevronString, x+7, y+3, true);
921 break;
922 }
923 case SELECTED: {
924 gc.setForeground(display.getSystemColor(BUTTON_BORDER));
925 gc.setBackground(display.getSystemColor(BUTTON_FILL));
926 gc.setFont(f);
927 gc.fillRoundRectangle(chevronRect.x, chevronRect.y, chevronRect.width, chevronRect.height, 6, 6);
928 gc.drawRoundRectangle(chevronRect.x, chevronRect.y, chevronRect.width - 1, chevronRect.height - 1, 6, 6);
929 gc.drawLine(x+1,y+1, x+3,y+3);
930 gc.drawLine(x+3,y+3, x+1,y+5);
931 gc.drawLine(x+2,y+1, x+4,y+3);
932 gc.drawLine(x+4,y+3, x+2,y+5);
933 gc.drawLine(x+5,y+1, x+7,y+3);
934 gc.drawLine(x+7,y+3, x+6,y+5);
935 gc.drawLine(x+6,y+1, x+8,y+3);
936 gc.drawLine(x+8,y+3, x+5,y+5);
937 gc.drawString(chevronString, x+8, y+4, true);
938 break;
939 }
940 default:
941 }
942 f.dispose();
943 }
944 void drawMaximize(GC gc) {
945 if (maxRect.width is 0 || maxRect.height is 0) return;
946 Display display = getDisplay();
947 // 5x4 or 7x9
948 int x = maxRect.x + (CTabFolder.BUTTON_SIZE - 10)/2;
949 int y = maxRect.y + 3;
950
951 gc.setForeground(display.getSystemColor(BUTTON_BORDER));
952 gc.setBackground(display.getSystemColor(BUTTON_FILL));
953
954 switch (maxImageState) {
955 case NORMAL: {
956 if (!maximized) {
957 gc.fillRectangle(x, y, 9, 9);
958 gc.drawRectangle(x, y, 9, 9);
959 gc.drawLine(x+1, y+2, x+8, y+2);
960 } else {
961 gc.fillRectangle(x, y+3, 5, 4);
962 gc.fillRectangle(x+2, y, 5, 4);
963 gc.drawRectangle(x, y+3, 5, 4);
964 gc.drawRectangle(x+2, y, 5, 4);
965 gc.drawLine(x+3, y+1, x+6, y+1);
966 gc.drawLine(x+1, y+4, x+4, y+4);
967 }
968 break;
969 }
970 case HOT: {
971 gc.fillRoundRectangle(maxRect.x, maxRect.y, maxRect.width, maxRect.height, 6, 6);
972 gc.drawRoundRectangle(maxRect.x, maxRect.y, maxRect.width - 1, maxRect.height - 1, 6, 6);
973 if (!maximized) {
974 gc.fillRectangle(x, y, 9, 9);
975 gc.drawRectangle(x, y, 9, 9);
976 gc.drawLine(x+1, y+2, x+8, y+2);
977 } else {
978 gc.fillRectangle(x, y+3, 5, 4);
979 gc.fillRectangle(x+2, y, 5, 4);
980 gc.drawRectangle(x, y+3, 5, 4);
981 gc.drawRectangle(x+2, y, 5, 4);
982 gc.drawLine(x+3, y+1, x+6, y+1);
983 gc.drawLine(x+1, y+4, x+4, y+4);
984 }
985 break;
986 }
987 case SELECTED: {
988 gc.fillRoundRectangle(maxRect.x, maxRect.y, maxRect.width, maxRect.height, 6, 6);
989 gc.drawRoundRectangle(maxRect.x, maxRect.y, maxRect.width - 1, maxRect.height - 1, 6, 6);
990 if (!maximized) {
991 gc.fillRectangle(x+1, y+1, 9, 9);
992 gc.drawRectangle(x+1, y+1, 9, 9);
993 gc.drawLine(x+2, y+3, x+9, y+3);
994 } else {
995 gc.fillRectangle(x+1, y+4, 5, 4);
996 gc.fillRectangle(x+3, y+1, 5, 4);
997 gc.drawRectangle(x+1, y+4, 5, 4);
998 gc.drawRectangle(x+3, y+1, 5, 4);
999 gc.drawLine(x+4, y+2, x+7, y+2);
1000 gc.drawLine(x+2, y+5, x+5, y+5);
1001 }
1002 break;
1003 }
1004 default:
1005 }
1006 }
1007 void drawMinimize(GC gc) {
1008 if (minRect.width is 0 || minRect.height is 0) return;
1009 Display display = getDisplay();
1010 // 5x4 or 9x3
1011 int x = minRect.x + (BUTTON_SIZE - 10)/2;
1012 int y = minRect.y + 3;
1013
1014 gc.setForeground(display.getSystemColor(BUTTON_BORDER));
1015 gc.setBackground(display.getSystemColor(BUTTON_FILL));
1016
1017 switch (minImageState) {
1018 case NORMAL: {
1019 if (!minimized) {
1020 gc.fillRectangle(x, y, 9, 3);
1021 gc.drawRectangle(x, y, 9, 3);
1022 } else {
1023 gc.fillRectangle(x, y+3, 5, 4);
1024 gc.fillRectangle(x+2, y, 5, 4);
1025 gc.drawRectangle(x, y+3, 5, 4);
1026 gc.drawRectangle(x+2, y, 5, 4);
1027 gc.drawLine(x+3, y+1, x+6, y+1);
1028 gc.drawLine(x+1, y+4, x+4, y+4);
1029 }
1030 break;
1031 }
1032 case HOT: {
1033 gc.fillRoundRectangle(minRect.x, minRect.y, minRect.width, minRect.height, 6, 6);
1034 gc.drawRoundRectangle(minRect.x, minRect.y, minRect.width - 1, minRect.height - 1, 6, 6);
1035 if (!minimized) {
1036 gc.fillRectangle(x, y, 9, 3);
1037 gc.drawRectangle(x, y, 9, 3);
1038 } else {
1039 gc.fillRectangle(x, y+3, 5, 4);
1040 gc.fillRectangle(x+2, y, 5, 4);
1041 gc.drawRectangle(x, y+3, 5, 4);
1042 gc.drawRectangle(x+2, y, 5, 4);
1043 gc.drawLine(x+3, y+1, x+6, y+1);
1044 gc.drawLine(x+1, y+4, x+4, y+4);
1045 }
1046 break;
1047 }
1048 case SELECTED: {
1049 gc.fillRoundRectangle(minRect.x, minRect.y, minRect.width, minRect.height, 6, 6);
1050 gc.drawRoundRectangle(minRect.x, minRect.y, minRect.width - 1, minRect.height - 1, 6, 6);
1051 if (!minimized) {
1052 gc.fillRectangle(x+1, y+1, 9, 3);
1053 gc.drawRectangle(x+1, y+1, 9, 3);
1054 } else {
1055 gc.fillRectangle(x+1, y+4, 5, 4);
1056 gc.fillRectangle(x+3, y+1, 5, 4);
1057 gc.drawRectangle(x+1, y+4, 5, 4);
1058 gc.drawRectangle(x+3, y+1, 5, 4);
1059 gc.drawLine(x+4, y+2, x+7, y+2);
1060 gc.drawLine(x+2, y+5, x+5, y+5);
1061 }
1062 break;
1063 }
1064 default:
1065 }
1066 }
1067 void drawTabArea(Event event) {
1068 GC gc = event.gc;
1069 Point size = getSize();
1070 int[] shape = null;
1071
1072 if (tabHeight is 0) {
1073 int style = getStyle();
1074 if ((style & SWT.FLAT) !is 0 && (style & SWT.BORDER) is 0) return;
1075 int x1 = borderLeft - 1;
1076 int x2 = size.x - borderRight;
1077 int y1 = onBottom ? size.y - borderBottom - highlight_header - 1 : borderTop + highlight_header;
1078 int y2 = onBottom ? size.y - borderBottom : borderTop;
1079 if (borderLeft > 0 && onBottom) y2 -= 1;
1080
1081 shape = [x1, y1, x1,y2, x2,y2, x2,y1];
1082
1083 // If horizontal gradient, show gradient across the whole area
1084 if (selectedIndex !is -1 && selectionGradientColors !is null && selectionGradientColors.length > 1 && !selectionGradientVertical) {
1085 drawBackground(gc, shape, true);
1086 } else if (selectedIndex is -1 && gradientColors !is null && gradientColors.length > 1 && !gradientVertical) {
1087 drawBackground(gc, shape, false);
1088 } else {
1089 gc.setBackground(selectedIndex is -1 ? getBackground() : selectionBackground);
1090 gc.fillPolygon(shape);
1091 }
1092
1093 //draw 1 pixel border
1094 if (borderLeft > 0) {
1095 gc.setForeground(borderColor);
1096 gc.drawPolyline(shape);
1097 }
1098 return;
1099 }
1100
1101 int x = Math.max(0, borderLeft - 1);
1102 int y = onBottom ? size.y - borderBottom - tabHeight : borderTop;
1103 int width = size.x - borderLeft - borderRight + 1;
1104 int height = tabHeight - 1;
1105
1106 // Draw Tab Header
1107 if (onBottom) {
1108 int[] left, right;
1109 if ((getStyle() & SWT.BORDER) !is 0) {
1110 left = simple ? SIMPLE_BOTTOM_LEFT_CORNER : BOTTOM_LEFT_CORNER;
1111 right = simple ? SIMPLE_BOTTOM_RIGHT_CORNER : BOTTOM_RIGHT_CORNER;
1112 } else {
1113 left = simple ? SIMPLE_BOTTOM_LEFT_CORNER_BORDERLESS : BOTTOM_LEFT_CORNER_BORDERLESS;
1114 right = simple ? SIMPLE_BOTTOM_RIGHT_CORNER_BORDERLESS : BOTTOM_RIGHT_CORNER_BORDERLESS;
1115 }
1116 shape = new int[left.length + right.length + 4];
1117 int index = 0;
1118 shape[index++] = x;
1119 shape[index++] = y-highlight_header;
1120 for (int i = 0; i < left.length/2; i++) {
1121 shape[index++] = x+left[2*i];
1122 shape[index++] = y+height+left[2*i+1];
1123 if (borderLeft is 0) shape[index-1] += 1;
1124 }
1125 for (int i = 0; i < right.length/2; i++) {
1126 shape[index++] = x+width+right[2*i];
1127 shape[index++] = y+height+right[2*i+1];
1128 if (borderLeft is 0) shape[index-1] += 1;
1129 }
1130 shape[index++] = x+width;
1131 shape[index++] = y-highlight_header;
1132 } else {
1133 int[] left, right;
1134 if ((getStyle() & SWT.BORDER) !is 0) {
1135 left = simple ? SIMPLE_TOP_LEFT_CORNER : TOP_LEFT_CORNER;
1136 right = simple ? SIMPLE_TOP_RIGHT_CORNER : TOP_RIGHT_CORNER;
1137 } else {
1138 left = simple ? SIMPLE_TOP_LEFT_CORNER_BORDERLESS : TOP_LEFT_CORNER_BORDERLESS;
1139 right = simple ? SIMPLE_TOP_RIGHT_CORNER_BORDERLESS : TOP_RIGHT_CORNER_BORDERLESS;
1140 }
1141 shape = new int[left.length + right.length + 4];
1142 int index = 0;
1143 shape[index++] = x;
1144 shape[index++] = y+height+highlight_header + 1;
1145 for (int i = 0; i < left.length/2; i++) {
1146 shape[index++] = x+left[2*i];
1147 shape[index++] = y+left[2*i+1];
1148 }
1149 for (int i = 0; i < right.length/2; i++) {
1150 shape[index++] = x+width+right[2*i];
1151 shape[index++] = y+right[2*i+1];
1152 }
1153 shape[index++] = x+width;
1154 shape[index++] = y+height+highlight_header + 1;
1155 }
1156 // Fill in background
1157 bool bkSelected = single && selectedIndex !is -1;
1158 drawBackground(gc, shape, bkSelected);
1159 // Fill in parent background for non-rectangular shape
1160 Region r = new Region();
1161 r.add(new Rectangle(x, y, width + 1, height + 1));
1162 r.subtract(shape);
1163 gc.setBackground(getParent().getBackground());
1164 fillRegion(gc, r);
1165 r.dispose();
1166
1167 // Draw the unselected tabs.
1168 if (!single) {
1169 for (int i=0; i < items.length; i++) {
1170 if (i !is selectedIndex && event.getBounds().intersects(items[i].getBounds())) {
1171 items[i].onPaint(gc, false);
1172 }
1173 }
1174 }
1175
1176 // Draw selected tab
1177 if (selectedIndex !is -1) {
1178 CTabItem item = items[selectedIndex];
1179 item.onPaint(gc, true);
1180 } else {
1181 // if no selected tab - draw line across bottom of all tabs
1182 int x1 = borderLeft;
1183 int y1 = (onBottom) ? size.y - borderBottom - tabHeight - 1 : borderTop + tabHeight;
1184 int x2 = size.x - borderRight;
1185 gc.setForeground(borderColor);
1186 gc.drawLine(x1, y1, x2, y1);
1187 }
1188
1189 // Draw Buttons
1190 drawChevron(gc);
1191 drawMinimize(gc);
1192 drawMaximize(gc);
1193
1194 // Draw border line
1195 if (borderLeft > 0) {
1196 RGB outside = getParent().getBackground().getRGB();
1197 antialias(shape, borderColor.getRGB(), null, outside, gc);
1198 gc.setForeground(borderColor);
1199 gc.drawPolyline(shape);
1200 }
1201 }
1202 /**
1203 * Returns <code>true</code> if the receiver's border is visible.
1204 *
1205 * @return the receiver's border visibility state
1206 *
1207 * @exception SWTException <ul>
1208 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1209 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1210 * </ul>
1211 *
1212 * @since 3.0
1213 */
1214 public bool getBorderVisible() {
1215 checkWidget();
1216 return borderLeft is 1;
1217 }
1218 public override Rectangle getClientArea() {
1219 checkWidget();
1220 if (minimized) return new Rectangle(xClient, yClient, 0, 0);
1221 Point size = getSize();
1222 int width = size.x - borderLeft - borderRight - 2*marginWidth - 2*highlight_margin;
1223 int height = size.y - borderTop - borderBottom - 2*marginHeight - highlight_margin - highlight_header;
1224 height -= tabHeight;
1225 return new Rectangle(xClient, yClient, width, height);
1226 }
1227 /**
1228 * Return the tab that is located at the specified index.
1229 *
1230 * @param index the index of the tab item
1231 * @return the item at the specified index
1232 *
1233 * @exception IllegalArgumentException <ul>
1234 * <li>ERROR_INVALID_RANGE - if the index is out of range</li>
1235 * </ul>
1236 * @exception SWTException <ul>
1237 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
1238 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
1239 * </ul>
1240 */
1241 public CTabItem getItem (int index) {
1242 //checkWidget();
1243 if (index < 0 || index >= items.length)
1244 SWT.error(SWT.ERROR_INVALID_RANGE);
1245 return items [index];
1246 }
1247 /**
1248 * Gets the item at a point in the widget.
1249 *
1250 * @param pt the point in coordinates relative to the CTabFolder
1251 * @return the item at a point or null
1252 *
1253 * @exception SWTException <ul>
1254 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
1255 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
1256 * </ul>
1257 */
1258 public CTabItem getItem (Point pt) {
1259 //checkWidget();
1260 if (items.length is 0) return null;
1261 Point size = getSize();
1262 if (size.x <= borderLeft + borderRight) return null;
1263 if (showChevron && chevronRect.contains(pt)) return null;
1264 for (int i = 0; i < priority.length; i++) {
1265 CTabItem item = items[priority[i]];
1266 Rectangle rect = item.getBounds();
1267 if (rect.contains(pt)) return item;
1268 }
1269 return null;
1270 }
1271 /**
1272 * Return the number of tabs in the folder.
1273 *
1274 * @return the number of tabs in the folder
1275 *
1276 * @exception SWTException <ul>
1277 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
1278 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
1279 * </ul>
1280 */
1281 public int getItemCount(){
1282 //checkWidget();
1283 return items.length;
1284 }
1285 /**
1286 * Return the tab items.
1287 *
1288 * @return the tab items
1289 *
1290 * @exception SWTException <ul>
1291 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
1292 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
1293 * </ul>
1294 */
1295 public CTabItem [] getItems() {
1296 //checkWidget();
1297 CTabItem[] tabItems = new CTabItem [items.length];
1298 System.arraycopy(items, 0, tabItems, 0, items.length);
1299 return tabItems;
1300 }
1301 /*
1302 * Return the lowercase of the first non-'&' character following
1303 * an '&' character in the given string. If there are no '&'
1304 * characters in the given string, return '\0'.
1305 */
1306 dchar _findMnemonic (String string) {
1307 if (string is null) return '\0';
1308 int index = 0;
1309 int length_ = string.length;
1310 do {
1311 while (index < length_ && string[index] !is '&') index++;
1312 if (++index >= length_) return '\0';
1313 if (string[index] !is '&') return CharacterFirstToLower(string[index..$]);
1314 index++;
1315 } while (index < length_);
1316 return '\0';
1317 }
1318 String stripMnemonic (String string) {
1319 int index = 0;
1320 int length_ = string.length;
1321 do {
1322 while ((index < length_) && (string[index] !is '&')) index++;
1323 if (++index >= length_) return string;
1324 if (string[index] !is '&') {
1325 return string.substring(0, index-1) ~ string.substring(index, length_);
1326 }
1327 index++;
1328 } while (index < length_);
1329 return string;
1330 }
1331 /**
1332 * Returns <code>true</code> if the receiver is minimized.
1333 *
1334 * @return the receiver's minimized state
1335 *
1336 * @exception SWTException <ul>
1337 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1338 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1339 * </ul>
1340 *
1341 * @since 3.0
1342 */
1343 public bool getMinimized() {
1344 checkWidget();
1345 return minimized;
1346 }
1347 /**
1348 * Returns <code>true</code> if the minimize button
1349 * is visible.
1350 *
1351 * @return the visibility of the minimized button
1352 *
1353 * @exception SWTException <ul>
1354 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1355 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1356 * </ul>
1357 *
1358 * @since 3.0
1359 */
1360 public bool getMinimizeVisible() {
1361 checkWidget();
1362 return showMin;
1363 }
1364 /**
1365 * Returns the number of characters that will
1366 * appear in a fully compressed tab.
1367 *
1368 * @return number of characters that will appear in a fully compressed tab
1369 *
1370 * @since 3.0
1371 */
1372 public int getMinimumCharacters() {
1373 checkWidget();
1374 return minChars;
1375 }
1376 /**
1377 * Returns <code>true</code> if the receiver is maximized.
1378 * <p>
1379 *
1380 * @return the receiver's maximized state
1381 *
1382 * @exception SWTException <ul>
1383 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1384 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1385 * </ul>
1386 *
1387 * @since 3.0
1388 */
1389 public bool getMaximized() {
1390 checkWidget();
1391 return maximized;
1392 }
1393 /**
1394 * Returns <code>true</code> if the maximize button
1395 * is visible.
1396 *
1397 * @return the visibility of the maximized button
1398 *
1399 * @exception SWTException <ul>
1400 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1401 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1402 * </ul>
1403 *
1404 * @since 3.0
1405 */
1406 public bool getMaximizeVisible() {
1407 checkWidget();
1408 return showMax;
1409 }
1410 /**
1411 * Returns <code>true</code> if the receiver displays most
1412 * recently used tabs and <code>false</code> otherwise.
1413 * <p>
1414 * When there is not enough horizontal space to show all the tabs,
1415 * by default, tabs are shown sequentially from left to right in
1416 * order of their index. When the MRU visibility is turned on,
1417 * the tabs that are visible will be the tabs most recently selected.
1418 * Tabs will still maintain their left to right order based on index
1419 * but only the most recently selected tabs are visible.
1420 * <p>
1421 * For example, consider a CTabFolder that contains "Tab 1", "Tab 2",
1422 * "Tab 3" and "Tab 4" (in order by index). The user selects
1423 * "Tab 1" and then "Tab 3". If the CTabFolder is now
1424 * compressed so that only two tabs are visible, by default,
1425 * "Tab 2" and "Tab 3" will be shown ("Tab 3" since it is currently
1426 * selected and "Tab 2" because it is the previous item in index order).
1427 * If MRU visibility is enabled, the two visible tabs will be "Tab 1"
1428 * and "Tab 3" (in that order from left to right).</p>
1429 *
1430 * @return the receiver's header's visibility state
1431 *
1432 * @exception SWTException <ul>
1433 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1434 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1435 * </ul>
1436 *
1437 * @since 3.1
1438 */
1439 public bool getMRUVisible() {
1440 checkWidget();
1441 return mru;
1442 }
1443 int getRightItemEdge (){
1444 int x = getSize().x - borderRight - 3;
1445 if (showMin) x -= BUTTON_SIZE;
1446 if (showMax) x -= BUTTON_SIZE;
1447 if (showChevron) x -= 3*BUTTON_SIZE/2;
1448 if (topRight !is null && topRightAlignment !is SWT.FILL) {
1449 Point rightSize = topRight.computeSize(SWT.DEFAULT, SWT.DEFAULT);
1450 x -= rightSize.x + 3;
1451 }
1452 return Math.max(0, x);
1453 }
1454 /**
1455 * Return the selected tab item, or null if there is no selection.
1456 *
1457 * @return the selected tab item, or null if none has been selected
1458 *
1459 * @exception SWTException <ul>
1460 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
1461 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
1462 * </ul>
1463 */
1464 public CTabItem getSelection() {
1465 //checkWidget();
1466 if (selectedIndex is -1) return null;
1467 return items[selectedIndex];
1468 }
1469 /**
1470 * Returns the receiver's selection background color.
1471 *
1472 * @return the selection background color of the receiver
1473 *
1474 * @exception SWTException <ul>
1475 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1476 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1477 * </ul>
1478 *
1479 * @since 3.0
1480 */
1481 public Color getSelectionBackground() {
1482 checkWidget();
1483 return selectionBackground;
1484 }
1485 /**
1486 * Returns the receiver's selection foreground color.
1487 *
1488 * @return the selection foreground color of the receiver
1489 *
1490 * @exception SWTException <ul>
1491 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1492 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1493 * </ul>
1494 *
1495 * @since 3.0
1496 */
1497 public Color getSelectionForeground() {
1498 checkWidget();
1499 return selectionForeground;
1500 }
1501 /**
1502 * Return the index of the selected tab item, or -1 if there
1503 * is no selection.
1504 *
1505 * @return the index of the selected tab item or -1
1506 *
1507 * @exception SWTException <ul>
1508 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
1509 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
1510 * </ul>
1511 */
1512 public int getSelectionIndex() {
1513 //checkWidget();
1514 return selectedIndex;
1515 }
1516 /**
1517 * Returns <code>true</code> if the CTabFolder is rendered
1518 * with a simple, traditional shape.
1519 *
1520 * @return <code>true</code> if the CTabFolder is rendered with a simple shape
1521 *
1522 * @since 3.0
1523 */
1524 public bool getSimple() {
1525 checkWidget();
1526 return simple;
1527 }
1528 /**
1529 * Returns <code>true</code> if the CTabFolder only displays the selected tab
1530 * and <code>false</code> if the CTabFolder displays multiple tabs.
1531 *
1532 * @return <code>true</code> if the CTabFolder only displays the selected tab and <code>false</code> if the CTabFolder displays multiple tabs
1533 *
1534 * @since 3.0
1535 */
1536 public bool getSingle() {
1537 checkWidget();
1538 return single;
1539 }
1540
1541 public override int getStyle() {
1542 int style = super.getStyle();
1543 style &= ~(SWT.TOP | SWT.BOTTOM);
1544 style |= onBottom ? SWT.BOTTOM : SWT.TOP;
1545 style &= ~(SWT.SINGLE | SWT.MULTI);
1546 style |= single ? SWT.SINGLE : SWT.MULTI;
1547 if (borderLeft !is 0) style |= SWT.BORDER;
1548 style &= ~SWT.CLOSE;
1549 if (showClose) style |= SWT.CLOSE;
1550 return style;
1551 }
1552 /**
1553 * Returns the height of the tab
1554 *
1555 * @return the height of the tab
1556 *
1557 * @exception SWTException <ul>
1558 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
1559 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
1560 * </ul>
1561 */
1562 public int getTabHeight(){
1563 checkWidget();
1564 if (fixedTabHeight !is SWT.DEFAULT) return fixedTabHeight;
1565 return tabHeight - 1; // -1 for line drawn across top of tab
1566 }
1567 /**
1568 * Returns the position of the tab. Possible values are SWT.TOP or SWT.BOTTOM.
1569 *
1570 * @return the position of the tab
1571 *
1572 * @exception SWTException <ul>
1573 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
1574 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
1575 * </ul>
1576 */
1577 public int getTabPosition(){
1578 checkWidget();
1579 return onBottom ? SWT.BOTTOM : SWT.TOP;
1580 }
1581 /**
1582 * Returns the control in the top right corner of the tab folder.
1583 * Typically this is a close button or a composite with a menu and close button.
1584 *
1585 * @return the control in the top right corner of the tab folder or null
1586 *
1587 * @exception SWTException <ul>
1588 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
1589 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
1590 * </ul>
1591 *
1592 * @since 2.1
1593 */
1594 public Control getTopRight() {
1595 checkWidget();
1596 return topRight;
1597 }
1598 /**
1599 * Returns <code>true</code> if the close button appears
1600 * when the user hovers over an unselected tabs.
1601 *
1602 * @return <code>true</code> if the close button appears on unselected tabs
1603 *
1604 * @since 3.0
1605 */
1606 public bool getUnselectedCloseVisible() {
1607 checkWidget();
1608 return showUnselectedClose;
1609 }
1610 /**
1611 * Returns <code>true</code> if an image appears
1612 * in unselected tabs.
1613 *
1614 * @return <code>true</code> if an image appears in unselected tabs
1615 *
1616 * @since 3.0
1617 */
1618 public bool getUnselectedImageVisible() {
1619 checkWidget();
1620 return showUnselectedImage;
1621 }
1622 /**
1623 * Return the index of the specified tab or -1 if the tab is not
1624 * in the receiver.
1625 *
1626 * @param item the tab item for which the index is required
1627 *
1628 * @return the index of the specified tab item or -1
1629 *
1630 * @exception IllegalArgumentException <ul>
1631 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
1632 * </ul>
1633 *
1634 * @exception SWTException <ul>
1635 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
1636 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
1637 * </ul>
1638 */
1639 public int indexOf(CTabItem item) {
1640 checkWidget();
1641 if (item is null) {
1642 SWT.error(SWT.ERROR_NULL_ARGUMENT);
1643 }
1644 for (int i = 0; i < items.length; i++) {
1645 if (items[i] is item) return i;
1646 }
1647 return -1;
1648 }
1649 void initAccessible() {
1650 Accessible accessible = getAccessible();
1651 accessible.addAccessibleListener(new class() AccessibleAdapter {
1652 public void getName(AccessibleEvent e) {
1653 String name = null;
1654 int childID = e.childID;
1655 if (childID >= 0 && childID < items.length) {
1656 name = stripMnemonic(items[childID].getText());
1657 } else if (childID is items.length + CHEVRON_CHILD_ID) {
1658 name = SWT.getMessage("SWT_ShowList"); //$NON-NLS-1$
1659 } else if (childID is items.length + MINIMIZE_CHILD_ID) {
1660 name = minimized ? SWT.getMessage("SWT_Restore") : SWT.getMessage("SWT_Minimize"); //$NON-NLS-1$ //$NON-NLS-2$
1661 } else if (childID is items.length + MAXIMIZE_CHILD_ID) {
1662 name = maximized ? SWT.getMessage("SWT_Restore") : SWT.getMessage("SWT_Maximize"); //$NON-NLS-1$ //$NON-NLS-2$
1663 }
1664 e.result = name;
1665 }
1666
1667 public void getHelp(AccessibleEvent e) {
1668 String help = null;
1669 int childID = e.childID;
1670 if (childID is ACC.CHILDID_SELF) {
1671 help = getToolTipText();
1672 } else if (childID >= 0 && childID < items.length) {
1673 help = items[childID].getToolTipText();
1674 }
1675 e.result = help;
1676 }
1677
1678 public void getKeyboardShortcut(AccessibleEvent e) {
1679 String shortcut = null;
1680 int childID = e.childID;
1681 if (childID >= 0 && childID < items.length) {
1682 String text = items[childID].getText();
1683 if (text !is null) {
1684 dchar mnemonic = _findMnemonic(text);
1685 if (mnemonic !is '\0') {
1686 shortcut = "Alt+"~tango.text.convert.Utf.toString([mnemonic]); //$NON-NLS-1$
1687 }
1688 }
1689 }
1690 e.result = shortcut;
1691 }
1692 });
1693
1694 accessible.addAccessibleControlListener(new class() AccessibleControlAdapter {
1695 public void getChildAtPoint(AccessibleControlEvent e) {
1696 Point testPoint = toControl(e.x, e.y);
1697 int childID = ACC.CHILDID_NONE;
1698 for (int i = 0; i < items.length; i++) {
1699 if (items[i].getBounds().contains(testPoint)) {
1700 childID = i;
1701 break;
1702 }
1703 }
1704 if (childID is ACC.CHILDID_NONE) {
1705 if (showChevron && chevronRect.contains(testPoint)) {
1706 childID = items.length + CHEVRON_CHILD_ID;
1707 } else if (showMin && minRect.contains(testPoint)) {
1708 childID = items.length + MINIMIZE_CHILD_ID;
1709 } else if (showMax && maxRect.contains(testPoint)) {
1710 childID = items.length + MAXIMIZE_CHILD_ID;
1711 } else {
1712 Rectangle location = getBounds();
1713 location.height = location.height - getClientArea().height;
1714 if (location.contains(testPoint)) {
1715 childID = ACC.CHILDID_SELF;
1716 }
1717 }
1718 }
1719 e.childID = childID;
1720 }
1721
1722 public void getLocation(AccessibleControlEvent e) {
1723 Rectangle location = null;
1724 Point pt = null;
1725 int childID = e.childID;
1726 if (childID is ACC.CHILDID_SELF) {
1727 location = getBounds();
1728 pt = getParent().toDisplay(location.x, location.y);
1729 } else {
1730 if (childID >= 0 && childID < items.length && items[childID].isShowing()) {
1731 location = items[childID].getBounds();
1732 } else if (showChevron && childID is items.length + CHEVRON_CHILD_ID) {
1733 location = chevronRect;
1734 } else if (showMin && childID is items.length + MINIMIZE_CHILD_ID) {
1735 location = minRect;
1736 } else if (showMax && childID is items.length + MAXIMIZE_CHILD_ID) {
1737 location = maxRect;
1738 }
1739 if (location !is null) {
1740 pt = toDisplay(location.x, location.y);
1741 }
1742 }
1743 if (location !is null && pt !is null) {
1744 e.x = pt.x;
1745 e.y = pt.y;
1746 e.width = location.width;
1747 e.height = location.height;
1748 }
1749 }
1750
1751 public void getChildCount(AccessibleControlEvent e) {
1752 e.detail = items.length + EXTRA_CHILD_ID_COUNT;
1753 }
1754
1755 public void getDefaultAction(AccessibleControlEvent e) {
1756 String action = null;
1757 int childID = e.childID;
1758 if (childID >= 0 && childID < items.length) {
1759 action = SWT.getMessage ("SWT_Switch"); //$NON-NLS-1$
1760 }
1761 if (childID >= items.length && childID < items.length + EXTRA_CHILD_ID_COUNT) {
1762 action = SWT.getMessage ("SWT_Press"); //$NON-NLS-1$
1763 }
1764 e.result = action;
1765 }
1766
1767 public void getFocus(AccessibleControlEvent e) {
1768 int childID = ACC.CHILDID_NONE;
1769 if (isFocusControl()) {
1770 if (selectedIndex is -1) {
1771 childID = ACC.CHILDID_SELF;
1772 } else {
1773 childID = selectedIndex;
1774 }
1775 }
1776 e.childID = childID;
1777 }
1778
1779 public void getRole(AccessibleControlEvent e) {
1780 int role = 0;
1781 int childID = e.childID;
1782 if (childID is ACC.CHILDID_SELF) {
1783 role = ACC.ROLE_TABFOLDER;
1784 } else if (childID >= 0 && childID < items.length) {
1785 role = ACC.ROLE_TABITEM;
1786 } else if (childID >= items.length && childID < items.length + EXTRA_CHILD_ID_COUNT) {
1787 role = ACC.ROLE_PUSHBUTTON;
1788 }
1789 e.detail = role;
1790 }
1791
1792 public void getSelection(AccessibleControlEvent e) {
1793 e.childID = (selectedIndex is -1) ? ACC.CHILDID_NONE : selectedIndex;
1794 }
1795
1796 public void getState(AccessibleControlEvent e) {
1797 int state = 0;
1798 int childID = e.childID;
1799 if (childID is ACC.CHILDID_SELF) {
1800 state = ACC.STATE_NORMAL;
1801 } else if (childID >= 0 && childID < items.length) {
1802 state = ACC.STATE_SELECTABLE;
1803 if (isFocusControl()) {
1804 state |= ACC.STATE_FOCUSABLE;
1805 }
1806 if (selectedIndex is childID) {
1807 state |= ACC.STATE_SELECTED;
1808 if (isFocusControl()) {
1809 state |= ACC.STATE_FOCUSED;
1810 }
1811 }
1812 } else if (childID is items.length + CHEVRON_CHILD_ID) {
1813 state = showChevron ? ACC.STATE_NORMAL : ACC.STATE_INVISIBLE;
1814 } else if (childID is items.length + MINIMIZE_CHILD_ID) {
1815 state = showMin ? ACC.STATE_NORMAL : ACC.STATE_INVISIBLE;
1816 } else if (childID is items.length + MAXIMIZE_CHILD_ID) {
1817 state = showMax ? ACC.STATE_NORMAL : ACC.STATE_INVISIBLE;
1818 }
1819 e.detail = state;
1820 }
1821
1822 public void getChildren(AccessibleControlEvent e) {
1823 int childIdCount = items.length + EXTRA_CHILD_ID_COUNT;
1824 Object[] children = new Object[childIdCount];
1825 for (int i = 0; i < childIdCount; i++) {
1826 children[i] = new Integer(i);
1827 }
1828 e.children = children;
1829 }
1830 });
1831
1832 addListener(SWT.Selection, new class(accessible) Listener {
1833 Accessible acc;
1834 this( Accessible a ){
1835 this.acc = a;
1836 }
1837 public void handleEvent(Event event) {
1838 if (isFocusControl()) {
1839 if (selectedIndex is -1) {
1840 acc.setFocus(ACC.CHILDID_SELF);
1841 } else {
1842 acc.setFocus(selectedIndex);
1843 }
1844 }
1845 }
1846 });
1847
1848 addListener(SWT.FocusIn, new class(accessible) Listener {
1849 Accessible acc;
1850 this( Accessible a ){ this.acc = a; }
1851 public void handleEvent(Event event) {
1852 if (selectedIndex is -1) {
1853 acc.setFocus(ACC.CHILDID_SELF);
1854 } else {
1855 acc.setFocus(selectedIndex);
1856 }
1857 }
1858 });
1859 }
1860
1861 void onKeyDown (Event event) {
1862 switch (event.keyCode) {
1863 case SWT.ARROW_LEFT:
1864 case SWT.ARROW_RIGHT:
1865 int count = items.length;
1866 if (count is 0) return;
1867 if (selectedIndex is -1) return;
1868 int leadKey = (getStyle() & SWT.RIGHT_TO_LEFT) !is 0 ? SWT.ARROW_RIGHT : SWT.ARROW_LEFT;
1869 int offset = event.keyCode is leadKey ? -1 : 1;
1870 int index;
1871 if (!mru) {
1872 index = selectedIndex + offset;
1873 } else {
1874 int[] visible = new int[items.length];
1875 int idx = 0;
1876 int current = -1;
1877 for (int i = 0; i < items.length; i++) {
1878 if (items[i].showing) {
1879 if (i is selectedIndex) current = idx;
1880 visible [idx++] = i;
1881 }
1882 }
1883 if (current + offset >= 0 && current + offset < idx){
1884 index = visible [current + offset];
1885 } else {
1886 if (showChevron) {
1887 CTabFolderEvent e = new CTabFolderEvent(this);
1888 e.widget = this;
1889 e.time = event.time;
1890 e.x = chevronRect.x;
1891 e.y = chevronRect.y;
1892 e.width = chevronRect.width;
1893 e.height = chevronRect.height;
1894 e.doit = true;
1895 for (int i = 0; i < folderListeners.length; i++) {
1896 folderListeners[i].showList(e);
1897 }
1898 if (e.doit && !isDisposed()) {
1899 showList(chevronRect);
1900 }
1901 }
1902 return;
1903 }
1904 }
1905 if (index < 0 || index >= count) return;
1906 setSelection (index, true);
1907 forceFocus();
1908 default:
1909 }
1910 }
1911 void onDispose(Event event) {
1912 removeListener(SWT.Dispose, listener);
1913 notifyListeners(SWT.Dispose, event);
1914 event.type = SWT.None;
1915 /*
1916 * Usually when an item is disposed, destroyItem will change the size of the items array,
1917 * reset the bounds of all the tabs and manage the widget associated with the tab.
1918 * Since the whole folder is being disposed, this is not necessary. For speed
1919 * the inDispose flag is used to skip over this part of the item dispose.
1920 */
1921 inDispose = true;
1922
1923 if (showMenu !is null && !showMenu.isDisposed()) {
1924 showMenu.dispose();
1925 showMenu = null;
1926 }
1927 int length = items.length;
1928 for (int i = 0; i < length; i++) {
1929 if (items[i] !is null) {
1930 items[i].dispose();
1931 }
1932 }
1933
1934 selectionGradientColors = null;
1935 selectionGradientPercents = null;
1936 selectionBgImage = null;
1937
1938 selectionBackground = null;
1939 selectionForeground = null;
1940 disposeSelectionHighlightGradientColors();
1941 }
1942 void onDragDetect(Event event) {
1943 bool consume = false;
1944 if (chevronRect.contains(event.x, event.y) ||
1945 minRect.contains(event.x, event.y) ||
1946 maxRect.contains(event.x, event.y)){
1947 consume = true;
1948 } else {
1949 for (int i = 0; i < items.length; i++) {
1950 if (items[i].closeRect.contains(event.x, event.y)) {
1951 consume = true;
1952 break;
1953 }
1954 }
1955 }
1956 if (consume) {
1957 event.type = SWT.None;
1958 }
1959 }
1960 void onFocus(Event event) {
1961 checkWidget();
1962 if (selectedIndex >= 0) {
1963 redraw();
1964 } else {
1965 setSelection(0, true);
1966 }
1967 }
1968 bool onMnemonic (Event event) {
1969 auto key = event.character;
1970 for (int i = 0; i < items.length; i++) {
1971 if (items[i] !is null) {
1972 auto mnemonic = _findMnemonic (items[i].getText ());
1973 if (mnemonic !is '\0') {
1974 if ( CharacterToLower(key) is mnemonic) {
1975 setSelection(i, true);
1976 return true;
1977 }
1978 }
1979 }
1980 }
1981 return false;
1982 }
1983 void onMouseDoubleClick(Event event) {
1984 if (event.button !is 1 ||
1985 (event.stateMask & SWT.BUTTON2) !is 0 ||
1986 (event.stateMask & SWT.BUTTON3) !is 0) return;
1987 Event e = new Event();
1988 e.item = getItem(new Point(event.x, event.y));
1989 if (e.item !is null) {
1990 notifyListeners(SWT.DefaultSelection, e);
1991 }
1992 }
1993 void onMouse(Event event) {
1994 int x = event.x, y = event.y;
1995 switch (event.type) {
1996 case SWT.MouseEnter: {
1997 setToolTipText(null);
1998 break;
1999 }
2000 case SWT.MouseExit: {
2001 if (minImageState !is NORMAL) {
2002 minImageState = NORMAL;
2003 redraw(minRect.x, minRect.y, minRect.width, minRect.height, false);
2004 }
2005 if (maxImageState !is NORMAL) {
2006 maxImageState = NORMAL;
2007 redraw(maxRect.x, maxRect.y, maxRect.width, maxRect.height, false);
2008 }
2009 if (chevronImageState !is NORMAL) {
2010 chevronImageState = NORMAL;
2011 redraw(chevronRect.x, chevronRect.y, chevronRect.width, chevronRect.height, false);
2012 }
2013 for (int i=0; i<items.length; i++) {
2014 CTabItem item = items[i];
2015 if (i !is selectedIndex && item.closeImageState !is NONE) {
2016 item.closeImageState = NONE;
2017 redraw(item.closeRect.x, item.closeRect.y, item.closeRect.width, item.closeRect.height, false);
2018 }
2019 if (i is selectedIndex && item.closeImageState !is NORMAL) {
2020 item.closeImageState = NORMAL;
2021 redraw(item.closeRect.x, item.closeRect.y, item.closeRect.width, item.closeRect.height, false);
2022 }
2023 }
2024 break;
2025 }
2026 case SWT.MouseDown: {
2027 if (minRect.contains(x, y)) {
2028 if (event.button !is 1) return;
2029 minImageState = SELECTED;
2030 redraw(minRect.x, minRect.y, minRect.width, minRect.height, false);
2031 update();
2032 return;
2033 }
2034 if (maxRect.contains(x, y)) {
2035 if (event.button !is 1) return;
2036 maxImageState = SELECTED;
2037 redraw(maxRect.x, maxRect.y, maxRect.width, maxRect.height, false);
2038 update();
2039 return;
2040 }
2041 if (chevronRect.contains(x, y)) {
2042 if (event.button !is 1) return;
2043 if (chevronImageState !is HOT) {
2044 chevronImageState = HOT;
2045 } else {
2046 chevronImageState = SELECTED;
2047 }
2048 redraw(chevronRect.x, chevronRect.y, chevronRect.width, chevronRect.height, false);
2049 update();
2050 return;
2051 }
2052 CTabItem item = null;
2053 if (single) {
2054 if (selectedIndex !is -1) {
2055 Rectangle bounds = items[selectedIndex].getBounds();
2056 if (bounds.contains(x, y)){
2057 item = items[selectedIndex];
2058 }
2059 }
2060 } else {
2061 for (int i=0; i<items.length; i++) {
2062 Rectangle bounds = items[i].getBounds();
2063 if (bounds.contains(x, y)){
2064 item = items[i];
2065 }
2066 }
2067 }
2068 if (item !is null) {
2069 if (item.closeRect.contains(x,y)){
2070 if (event.button !is 1) return;
2071 item.closeImageState = SELECTED;
2072 redraw(item.closeRect.x, item.closeRect.y, item.closeRect.width, item.closeRect.height, false);
2073 update();
2074 return;
2075 }
2076 int index = indexOf(item);
2077 if (item.showing){
2078 setSelection(index, true);
2079 }
2080 return;
2081 }
2082 break;
2083 }
2084 case SWT.MouseMove: {
2085 _setToolTipText(event.x, event.y);
2086 bool close = false, minimize = false, maximize = false, chevron = false;
2087 if (minRect.contains(x, y)) {
2088 minimize = true;
2089 if (minImageState !is SELECTED && minImageState !is HOT) {
2090 minImageState = HOT;
2091 redraw(minRect.x, minRect.y, minRect.width, minRect.height, false);
2092 }
2093 }
2094 if (maxRect.contains(x, y)) {
2095 maximize = true;
2096 if (maxImageState !is SELECTED && maxImageState !is HOT) {
2097 maxImageState = HOT;
2098 redraw(maxRect.x, maxRect.y, maxRect.width, maxRect.height, false);
2099 }
2100 }
2101 if (chevronRect.contains(x, y)) {
2102 chevron = true;
2103 if (chevronImageState !is SELECTED && chevronImageState !is HOT) {
2104 chevronImageState = HOT;
2105 redraw(chevronRect.x, chevronRect.y, chevronRect.width, chevronRect.height, false);
2106 }
2107 }
2108 if (minImageState !is NORMAL && !minimize) {
2109 minImageState = NORMAL;
2110 redraw(minRect.x, minRect.y, minRect.width, minRect.height, false);
2111 }
2112 if (maxImageState !is NORMAL && !maximize) {
2113 maxImageState = NORMAL;
2114 redraw(maxRect.x, maxRect.y, maxRect.width, maxRect.height, false);
2115 }
2116 if (chevronImageState !is NORMAL && !chevron) {
2117 chevronImageState = NORMAL;
2118 redraw(chevronRect.x, chevronRect.y, chevronRect.width, chevronRect.height, false);
2119 }
2120 for (int i=0; i<items.length; i++) {
2121 CTabItem item = items[i];
2122 close = false;
2123 if (item.getBounds().contains(x, y)) {
2124 close = true;
2125 if (item.closeRect.contains(x, y)) {
2126 if (item.closeImageState !is SELECTED && item.closeImageState !is HOT) {
2127 item.closeImageState = HOT;
2128 redraw(item.closeRect.x, item.closeRect.y, item.closeRect.width, item.closeRect.height, false);
2129 }
2130 } else {
2131 if (item.closeImageState !is NORMAL) {
2132 item.closeImageState = NORMAL;
2133 redraw(item.closeRect.x, item.closeRect.y, item.closeRect.width, item.closeRect.height, false);
2134 }
2135 }
2136 }
2137 if (i !is selectedIndex && item.closeImageState !is NONE && !close) {
2138 item.closeImageState = NONE;
2139 redraw(item.closeRect.x, item.closeRect.y, item.closeRect.width, item.closeRect.height, false);
2140 }
2141 if (i is selectedIndex && item.closeImageState !is NORMAL && !close) {
2142 item.closeImageState = NORMAL;
2143 redraw(item.closeRect.x, item.closeRect.y, item.closeRect.width, item.closeRect.height, false);
2144 }
2145 }
2146 break;
2147 }
2148 case SWT.MouseUp: {
2149 if (event.button !is 1) return;
2150 if (chevronRect.contains(x, y)) {
2151 bool selected = chevronImageState is SELECTED;
2152 if (!selected) return;
2153 CTabFolderEvent e = new CTabFolderEvent(this);
2154 e.widget = this;
2155 e.time = event.time;
2156 e.x = chevronRect.x;
2157 e.y = chevronRect.y;
2158 e.width = chevronRect.width;
2159 e.height = chevronRect.height;
2160 e.doit = true;
2161 for (int i = 0; i < folderListeners.length; i++) {
2162 folderListeners[i].showList(e);
2163 }
2164 if (e.doit && !isDisposed()) {
2165 showList(chevronRect);
2166 }
2167 return;
2168 }
2169 if (minRect.contains(x, y)) {
2170 bool selected = minImageState is SELECTED;
2171 minImageState = HOT;
2172 redraw(minRect.x, minRect.y, minRect.width, minRect.height, false);
2173 if (!selected) return;
2174 CTabFolderEvent e = new CTabFolderEvent(this);
2175 e.widget = this;
2176 e.time = event.time;
2177 for (int i = 0; i < folderListeners.length; i++) {
2178 if (minimized) {
2179 folderListeners[i].restore(e);
2180 } else {
2181 folderListeners[i].minimize(e);
2182 }
2183 }
2184 return;
2185 }
2186 if (maxRect.contains(x, y)) {
2187 bool selected = maxImageState is SELECTED;
2188 maxImageState = HOT;
2189 redraw(maxRect.x, maxRect.y, maxRect.width, maxRect.height, false);
2190 if (!selected) return;
2191 CTabFolderEvent e = new CTabFolderEvent(this);
2192 e.widget = this;
2193 e.time = event.time;
2194 for (int i = 0; i < folderListeners.length; i++) {
2195 if (maximized) {
2196 folderListeners[i].restore(e);
2197 } else {
2198 folderListeners[i].maximize(e);
2199 }
2200 }
2201 return;
2202 }
2203 CTabItem item = null;
2204 if (single) {
2205 if (selectedIndex !is -1) {
2206 Rectangle bounds = items[selectedIndex].getBounds();
2207 if (bounds.contains(x, y)){
2208 item = items[selectedIndex];
2209 }
2210 }
2211 } else {
2212 for (int i=0; i<items.length; i++) {
2213 Rectangle bounds = items[i].getBounds();
2214 if (bounds.contains(x, y)){
2215 item = items[i];
2216 }
2217 }
2218 }
2219 if (item !is null) {
2220 if (item.closeRect.contains(x,y)) {
2221 bool selected = item.closeImageState is SELECTED;
2222 item.closeImageState = HOT;
2223 redraw(item.closeRect.x, item.closeRect.y, item.closeRect.width, item.closeRect.height, false);
2224 if (!selected) return;
2225 CTabFolderEvent e = new CTabFolderEvent(this);
2226 e.widget = this;
2227 e.time = event.time;
2228 e.item = item;
2229 e.doit = true;
2230 for (int j = 0; j < folderListeners.length; j++) {
2231 CTabFolder2Listener listener = folderListeners[j];
2232 listener.close(e);
2233 }
2234 for (int j = 0; j < tabListeners.length; j++) {
2235 CTabFolderListener listener = tabListeners[j];
2236 listener.itemClosed(e);
2237 }
2238 if (e.doit) item.dispose();
2239 if (!isDisposed() && item.isDisposed()) {
2240 Display display = getDisplay();
2241 Point pt = display.getCursorLocation();
2242 pt = display.map(null, this, pt.x, pt.y);
2243 CTabItem nextItem = getItem(pt);
2244 if (nextItem !is null) {
2245 if (nextItem.closeRect.contains(pt)) {
2246 if (nextItem.closeImageState !is SELECTED && nextItem.closeImageState !is HOT) {
2247 nextItem.closeImageState = HOT;
2248 redraw(nextItem.closeRect.x, nextItem.closeRect.y, nextItem.closeRect.width, nextItem.closeRect.height, false);
2249 }
2250 } else {
2251 if (nextItem.closeImageState !is NORMAL) {
2252 nextItem.closeImageState = NORMAL;
2253 redraw(nextItem.closeRect.x, nextItem.closeRect.y, nextItem.closeRect.width, nextItem.closeRect.height, false);
2254 }
2255 }
2256 }
2257 }
2258 return;
2259 }
2260 }
2261 default:
2262 }
2263 }
2264 }
2265 bool onPageTraversal(Event event) {
2266 int count = items.length;
2267 if (count is 0) return false;
2268 int index = selectedIndex;
2269 if (index is -1) {
2270 index = 0;
2271 } else {
2272 int offset = (event.detail is SWT.TRAVERSE_PAGE_NEXT) ? 1 : -1;
2273 if (!mru) {
2274 index = (selectedIndex + offset + count) % count;
2275 } else {
2276 int[] visible = new int[items.length];
2277 int idx = 0;
2278 int current = -1;
2279 for (int i = 0; i < items.length; i++) {
2280 if (items[i].showing) {
2281 if (i is selectedIndex) current = idx;
2282 visible [idx++] = i;
2283 }
2284 }
2285 if (current + offset >= 0 && current + offset < idx){
2286 index = visible [current + offset];
2287 } else {
2288 if (showChevron) {
2289 CTabFolderEvent e = new CTabFolderEvent(this);
2290 e.widget = this;
2291 e.time = event.time;
2292 e.x = chevronRect.x;
2293 e.y = chevronRect.y;
2294 e.width = chevronRect.width;
2295 e.height = chevronRect.height;
2296 e.doit = true;
2297 for (int i = 0; i < folderListeners.length; i++) {
2298 folderListeners[i].showList(e);
2299 }
2300 if (e.doit && !isDisposed()) {
2301 showList(chevronRect);
2302 }
2303 }
2304 return true;
2305 }
2306 }
2307 }
2308 setSelection (index, true);
2309 return true;
2310 }
2311 void onPaint(Event event) {
2312 if (inDispose) return;
2313 Font font = getFont();
2314 if (oldFont is null || oldFont!=font) {
2315 // handle case where default font changes
2316 oldFont = font;
2317 if (!updateTabHeight(false)) {
2318 updateItems();
2319 redraw();
2320 return;
2321 }
2322 }
2323
2324 GC gc = event.gc;
2325 Font gcFont = gc.getFont();
2326 Color gcBackground = gc.getBackground();
2327 Color gcForeground = gc.getForeground();
2328
2329 // Useful for debugging paint problems
2330 //{
2331 //Point size = getSize();
2332 //gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_GREEN));
2333 //gc.fillRectangle(-10, -10, size.x + 20, size.y+20);
2334 //}
2335
2336 drawBody(event);
2337
2338 gc.setFont(gcFont);
2339 gc.setForeground(gcForeground);
2340 gc.setBackground(gcBackground);
2341
2342 drawTabArea(event);
2343
2344 gc.setFont(gcFont);
2345 gc.setForeground(gcForeground);
2346 gc.setBackground(gcBackground);
2347 }
2348
2349 void onResize() {
2350 if (updateItems()) redrawTabs();
2351
2352 Point size = getSize();
2353 if (oldSize is null) {
2354 redraw();
2355 } else {
2356 if (onBottom && size.y !is oldSize.y) {
2357 redraw();
2358 } else {
2359 int x1 = Math.min(size.x, oldSize.x);
2360 if (size.x !is oldSize.x) x1 -= borderRight + highlight_margin + 2;
2361 if (!simple) x1 -= 5; // rounded top right corner
2362 int y1 = Math.min(size.y, oldSize.y);
2363 if (size.y !is oldSize.y) y1 -= borderBottom + highlight_margin;
2364 int x2 = Math.max(size.x, oldSize.x);
2365 int y2 = Math.max(size.y, oldSize.y);
2366 redraw(0, y1, x2, y2 - y1, false);
2367 redraw(x1, 0, x2 - x1, y2, false);
2368 }
2369 }
2370 oldSize = size;
2371 }
2372 void onTraverse (Event event) {
2373 switch (event.detail) {
2374 case SWT.TRAVERSE_ESCAPE:
2375 case SWT.TRAVERSE_RETURN:
2376 case SWT.TRAVERSE_TAB_NEXT:
2377 case SWT.TRAVERSE_TAB_PREVIOUS:
2378 Control focusControl = getDisplay().getFocusControl();
2379 if (focusControl is this) event.doit = true;
2380 break;
2381 case SWT.TRAVERSE_MNEMONIC:
2382 event.doit = onMnemonic(event);
2383 if (event.doit) event.detail = SWT.TRAVERSE_NONE;
2384 break;
2385 case SWT.TRAVERSE_PAGE_NEXT:
2386 case SWT.TRAVERSE_PAGE_PREVIOUS:
2387 event.doit = onPageTraversal(event);
2388 event.detail = SWT.TRAVERSE_NONE;
2389 break;
2390 default:
2391 }
2392 }
2393 void redrawTabs() {
2394 Point size = getSize();
2395 if (onBottom) {
2396 redraw(0, size.y - borderBottom - tabHeight - highlight_header - 1, size.x, borderBottom + tabHeight + highlight_header + 1, false);
2397 } else {
2398 redraw(0, 0, size.x, borderTop + tabHeight + highlight_header + 1, false);
2399 }
2400 }
2401 /**
2402 * Removes the listener.
2403 *
2404 * @param listener the listener which should no longer be notified
2405 *
2406 * @exception IllegalArgumentException <ul>
2407 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2408 * </ul>
2409 *
2410 * @exception SWTException <ul>
2411 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
2412 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
2413 * </ul>
2414 *
2415 * @see #addCTabFolder2Listener(CTabFolder2Listener)
2416 *
2417 * @since 3.0
2418 */
2419 public void removeCTabFolder2Listener(CTabFolder2Listener listener) {
2420 checkWidget();
2421 if (listener is null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
2422 if (folderListeners.length is 0) return;
2423 int index = -1;
2424 for (int i = 0; i < folderListeners.length; i++) {
2425 if (listener is folderListeners[i]){
2426 index = i;
2427 break;
2428 }
2429 }
2430 if (index is -1) return;
2431 if (folderListeners.length is 1) {
2432 folderListeners = new CTabFolder2Listener[0];
2433 return;
2434 }
2435 CTabFolder2Listener[] newTabListeners = new CTabFolder2Listener[folderListeners.length - 1];
2436 SimpleType!(CTabFolder2Listener).arraycopy(folderListeners, 0, newTabListeners, 0, index);
2437 SimpleType!(CTabFolder2Listener).arraycopy(folderListeners, index + 1, newTabListeners, index, folderListeners.length - index - 1);
2438 folderListeners = newTabListeners;
2439 }
2440 /**
2441 * Removes the listener.
2442 *
2443 * @param listener the listener which should no longer be notified
2444 *
2445 * @exception IllegalArgumentException <ul>
2446 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2447 * </ul>
2448 *
2449 * @exception SWTException <ul>
2450 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
2451 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
2452 * </ul>
2453 *
2454 * @deprecated see removeCTabFolderCloseListener(CTabFolderListener)
2455 */
2456 public void removeCTabFolderListener(CTabFolderListener listener) {
2457 checkWidget();
2458 if (listener is null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
2459 if (tabListeners.length is 0) return;
2460 int index = -1;
2461 for (int i = 0; i < tabListeners.length; i++) {
2462 if (listener is tabListeners[i]){
2463 index = i;
2464 break;
2465 }
2466 }
2467 if (index is -1) return;
2468 if (tabListeners.length is 1) {
2469 tabListeners = new CTabFolderListener[0];
2470 return;
2471 }
2472 CTabFolderListener[] newTabListeners = new CTabFolderListener[tabListeners.length - 1];
2473 SimpleType!(CTabFolderListener).arraycopy(tabListeners, 0, newTabListeners, 0, index);
2474 SimpleType!(CTabFolderListener).arraycopy(tabListeners, index + 1, newTabListeners, index, tabListeners.length - index - 1);
2475 tabListeners = newTabListeners;
2476 }
2477 /**
2478 * Removes the listener from the collection of listeners who will
2479 * be notified when the user changes the receiver's selection.
2480 *
2481 * @param listener the listener which should no longer be notified
2482 *
2483 * @exception IllegalArgumentException <ul>
2484 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2485 * </ul>
2486 * @exception SWTException <ul>
2487 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2488 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2489 * </ul>
2490 *
2491 * @see SelectionListener
2492 * @see #addSelectionListener
2493 */
2494 public void removeSelectionListener(SelectionListener listener) {
2495 checkWidget();
2496 if (listener is null) {
2497 SWT.error(SWT.ERROR_NULL_ARGUMENT);
2498 }
2499 removeListener(SWT.Selection, listener);
2500 removeListener(SWT.DefaultSelection, listener);
2501 }
2502 public override void setBackground (Color color) {
2503 super.setBackground(color);
2504 redraw();
2505 }
2506 /**
2507 * Specify a gradient of colours to be drawn in the background of the unselected tabs.
2508 * For example to draw a gradient that varies from dark blue to blue and then to
2509 * white, use the following call to setBackground:
2510 * <pre>
2511 * cfolder.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_DARK_BLUE),
2512 * display.getSystemColor(SWT.COLOR_BLUE),
2513 * display.getSystemColor(SWT.COLOR_WHITE),
2514 * display.getSystemColor(SWT.COLOR_WHITE)},
2515 * new int[] {25, 50, 100});
2516 * </pre>
2517 *
2518 * @param colors an array of Color that specifies the colors to appear in the gradient
2519 * in order of appearance left to right. The value <code>null</code> clears the
2520 * background gradient. The value <code>null</code> can be used inside the array of
2521 * Color to specify the background color.
2522 * @param percents an array of integers between 0 and 100 specifying the percent of the width
2523 * of the widget at which the color should change. The size of the percents array must be one
2524 * less than the size of the colors array.
2525 *
2526 * @exception SWTException <ul>
2527 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
2528 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
2529 * </ul>
2530 *
2531 * @since 3.0
2532 */
2533 void setBackground(Color[] colors, int[] percents) {
2534 setBackground(colors, percents, false);
2535 }
2536 /**
2537 * Specify a gradient of colours to be drawn in the background of the unselected tab.
2538 * For example to draw a vertical gradient that varies from dark blue to blue and then to
2539 * white, use the following call to setBackground:
2540 * <pre>
2541 * cfolder.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_DARK_BLUE),
2542 * display.getSystemColor(SWT.COLOR_BLUE),
2543 * display.getSystemColor(SWT.COLOR_WHITE),
2544 * display.getSystemColor(SWT.COLOR_WHITE)},
2545 * new int[] {25, 50, 100}, true);
2546 * </pre>
2547 *
2548 * @param colors an array of Color that specifies the colors to appear in the gradient
2549 * in order of appearance left to right. The value <code>null</code> clears the
2550 * background gradient. The value <code>null</code> can be used inside the array of
2551 * Color to specify the background color.
2552 * @param percents an array of integers between 0 and 100 specifying the percent of the width
2553 * of the widget at which the color should change. The size of the percents array must be one
2554 * less than the size of the colors array.
2555 *
2556 * @param vertical indicate the direction of the gradient. True is vertical and false is horizontal.
2557 *
2558 * @exception SWTException <ul>
2559 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
2560 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
2561 * </ul>
2562 *
2563 * @since 3.0
2564 */
2565 void setBackground(Color[] colors, int[] percents, bool vertical) {
2566 checkWidget();
2567 if (colors !is null) {
2568 if (percents is null || percents.length !is colors.length - 1) {
2569 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
2570 }
2571 for (int i = 0; i < percents.length; i++) {
2572 if (percents[i] < 0 || percents[i] > 100) {
2573 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
2574 }
2575 if (i > 0 && percents[i] < percents[i-1]) {
2576 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
2577 }
2578 }
2579 if (getDisplay().getDepth() < 15) {
2580 // Don't use gradients on low color displays
2581 colors = [colors[colors.length - 1]];
2582 percents = null;
2583 }
2584 }
2585
2586 // Are these settings the same as before?
2587 if (bgImage is null) {
2588 if ((gradientColors !is null) && (colors !is null) &&
2589 (gradientColors.length is colors.length)) {
2590 bool same = false;
2591 for (int i = 0; i < gradientColors.length; i++) {
2592 if (gradientColors[i] is null) {
2593 same = colors[i] is null;
2594 } else {
2595 same = cast(bool)(gradientColors[i]==colors[i]);
2596 }
2597 if (!same) break;
2598 }
2599 if (same) {
2600 for (int i = 0; i < gradientPercents.length; i++) {
2601 same = gradientPercents[i] is percents[i];
2602 if (!same) break;
2603 }
2604 }
2605 if (same && this.gradientVertical is vertical) return;
2606 }
2607 } else {
2608 bgImage = null;
2609 }
2610 // Store the new settings
2611 if (colors is null) {
2612 gradientColors = null;
2613 gradientPercents = null;
2614 gradientVertical = false;
2615 setBackground(cast(Color)null);
2616 } else {
2617 gradientColors = new Color[colors.length];
2618 for (int i = 0; i < colors.length; ++i) {
2619 gradientColors[i] = colors[i];
2620 }
2621 gradientPercents = new int[percents.length];
2622 for (int i = 0; i < percents.length; ++i) {
2623 gradientPercents[i] = percents[i];
2624 }
2625 gradientVertical = vertical;
2626 setBackground(gradientColors[gradientColors.length-1]);
2627 }
2628
2629 // Refresh with the new settings
2630 redraw();
2631 }
2632
2633 /**
2634 * Set the image to be drawn in the background of the unselected tab. Image
2635 * is stretched or compressed to cover entire unselected tab area.
2636 *
2637 * @param image the image to be drawn in the background
2638 *
2639 * @exception SWTException <ul>
2640 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2641 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2642 * </ul>
2643 *
2644 * @since 3.0
2645 */
2646 void setBackground(Image image) {
2647 checkWidget();
2648 if (image is bgImage) return;
2649 if (image !is null) {
2650 gradientColors = null;
2651 gradientPercents = null;
2652 }
2653 bgImage = image;
2654 redraw();
2655 }
2656 /**
2657 * Toggle the visibility of the border
2658 *
2659 * @param show true if the border should be displayed
2660 *
2661 * @exception SWTException <ul>
2662 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2663 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2664 * </ul>
2665 */
2666 public void setBorderVisible(bool show) {
2667 checkWidget();
2668 if ((borderLeft is 1) is show) return;
2669 borderLeft = borderRight = show ? 1 : 0;
2670 borderTop = onBottom ? borderLeft : 0;
2671 borderBottom = onBottom ? 0 : borderLeft;
2672 Rectangle rectBefore = getClientArea();
2673 updateItems();
2674 Rectangle rectAfter = getClientArea();
2675 if (rectBefore!=rectAfter) {
2676 notifyListeners(SWT.Resize, new Event());
2677 }
2678 redraw();
2679 }
2680 void setButtonBounds() {
2681 Point size = getSize();
2682 int oldX, oldY, oldWidth, oldHeight;
2683 // max button
2684 oldX = maxRect.x;
2685 oldY = maxRect.y;
2686 oldWidth = maxRect.width;
2687 oldHeight = maxRect.height;
2688 maxRect.x = maxRect.y = maxRect.width = maxRect.height = 0;
2689 if (showMax) {
2690 maxRect.x = size.x - borderRight - BUTTON_SIZE - 3;
2691 if (borderRight > 0) maxRect.x += 1;
2692 maxRect.y = onBottom ? size.y - borderBottom - tabHeight + (tabHeight - BUTTON_SIZE)/2: borderTop + (tabHeight - BUTTON_SIZE)/2;
2693 maxRect.width = BUTTON_SIZE;
2694 maxRect.height = BUTTON_SIZE;
2695 }
2696 if (oldX !is maxRect.x || oldWidth !is maxRect.width ||
2697 oldY !is maxRect.y || oldHeight !is maxRect.height) {
2698 int left = Math.min(oldX, maxRect.x);
2699 int right = Math.max(oldX + oldWidth, maxRect.x + maxRect.width);
2700 int top = onBottom ? size.y - borderBottom - tabHeight: borderTop + 1;
2701 redraw(left, top, right - left, tabHeight, false);
2702 }
2703
2704 // min button
2705 oldX = minRect.x;
2706 oldY = minRect.y;
2707 oldWidth = minRect.width;
2708 oldHeight = minRect.height;
2709 minRect.x = minRect.y = minRect.width = minRect.height = 0;
2710 if (showMin) {
2711 minRect.x = size.x - borderRight - maxRect.width - BUTTON_SIZE - 3;
2712 if (borderRight > 0) minRect.x += 1;
2713 minRect.y = onBottom ? size.y - borderBottom - tabHeight + (tabHeight - BUTTON_SIZE)/2: borderTop + (tabHeight - BUTTON_SIZE)/2;
2714 minRect.width = BUTTON_SIZE;
2715 minRect.height = BUTTON_SIZE;
2716 }
2717 if (oldX !is minRect.x || oldWidth !is minRect.width ||
2718 oldY !is minRect.y || oldHeight !is minRect.height) {
2719 int left = Math.min(oldX, minRect.x);
2720 int right = Math.max(oldX + oldWidth, minRect.x + minRect.width);
2721 int top = onBottom ? size.y - borderBottom - tabHeight: borderTop + 1;
2722 redraw(left, top, right - left, tabHeight, false);
2723 }
2724
2725 // top right control
2726 oldX = topRightRect.x;
2727 oldY = topRightRect.y;
2728 oldWidth = topRightRect.width;
2729 oldHeight = topRightRect.height;
2730 topRightRect.x = topRightRect.y = topRightRect.width = topRightRect.height = 0;
2731 if (topRight !is null) {
2732 switch (topRightAlignment) {
2733 case SWT.FILL: {
2734 int rightEdge = size.x - borderRight - 3 - maxRect.width - minRect.width;
2735 if (!simple && borderRight > 0 && !showMax && !showMin) rightEdge -= 2;
2736 if (single) {
2737 if (items.length is 0 || selectedIndex is -1) {
2738 topRightRect.x = borderLeft + 3;
2739 topRightRect.width = rightEdge - topRightRect.x;
2740 } else {
2741 // fill size is 0 if item compressed
2742 CTabItem item = items[selectedIndex];
2743 if (item.x + item.width + 7 + 3*BUTTON_SIZE/2 >= rightEdge) break;
2744 topRightRect.x = item.x + item.width + 7 + 3*BUTTON_SIZE/2;
2745 topRightRect.width = rightEdge - topRightRect.x;
2746 }
2747 } else {
2748 // fill size is 0 if chevron showing
2749 if (showChevron) break;
2750 if (items.length is 0) {
2751 topRightRect.x = borderLeft + 3;
2752 } else {
2753 CTabItem item = items[items.length - 1];
2754 topRightRect.x = item.x + item.width;
2755 if (!simple && items.length - 1 is selectedIndex) topRightRect.x += curveWidth - curveIndent;
2756 }
2757 topRightRect.width = Math.max(0, rightEdge - topRightRect.x);
2758 }
2759 topRightRect.y = onBottom ? size.y - borderBottom - tabHeight: borderTop + 1;
2760 topRightRect.height = tabHeight - 1;
2761 break;
2762 }
2763 case SWT.RIGHT: {
2764 Point topRightSize = topRight.computeSize(SWT.DEFAULT, tabHeight, false);
2765 int rightEdge = size.x - borderRight - 3 - maxRect.width - minRect.width;
2766 if (!simple && borderRight > 0 && !showMax && !showMin) rightEdge -= 2;
2767 topRightRect.x = rightEdge - topRightSize.x;
2768 topRightRect.width = topRightSize.x;
2769 topRightRect.y = onBottom ? size.y - borderBottom - tabHeight: borderTop + 1;
2770 topRightRect.height = tabHeight - 1;
2771 }
2772 default:
2773 }
2774 topRight.setBounds(topRightRect);
2775 }
2776 if (oldX !is topRightRect.x || oldWidth !is topRightRect.width ||
2777 oldY !is topRightRect.y || oldHeight !is topRightRect.height) {
2778 int left = Math.min(oldX, topRightRect.x);
2779 int right = Math.max(oldX + oldWidth, topRightRect.x + topRightRect.width);
2780 int top = onBottom ? size.y - borderBottom - tabHeight : borderTop + 1;
2781 redraw(left, top, right - left, tabHeight, false);
2782 }
2783
2784 // chevron button
2785 oldX = chevronRect.x;
2786 oldY = chevronRect.y;
2787 oldWidth = chevronRect.width;
2788 oldHeight = chevronRect.height;
2789 chevronRect.x = chevronRect.y = chevronRect.height = chevronRect.width = 0;
2790 if (single) {
2791 if (selectedIndex is -1 || items.length > 1) {
2792 chevronRect.width = 3*BUTTON_SIZE/2;
2793 chevronRect.height = BUTTON_SIZE;
2794 chevronRect.y = onBottom ? size.y - borderBottom - tabHeight + (tabHeight - chevronRect.height)/2 : borderTop + (tabHeight - chevronRect.height)/2;
2795 if (selectedIndex is -1) {
2796 chevronRect.x = size.x - borderRight - 3 - minRect.width - maxRect.width - topRightRect.width - chevronRect.width;
2797 } else {
2798 CTabItem item = items[selectedIndex];
2799 int w = size.x - borderRight - 3 - minRect.width - maxRect.width - chevronRect.width;
2800 if (topRightRect.width > 0) w -= topRightRect.width + 3;
2801 chevronRect.x = Math.min(item.x + item.width + 3, w);
2802 }
2803 if (borderRight > 0) chevronRect.x += 1;
2804 }
2805 } else {
2806 if (showChevron) {
2807 chevronRect.width = 3*BUTTON_SIZE/2;
2808 chevronRect.height = BUTTON_SIZE;
2809 int i = 0, lastIndex = -1;
2810 while (i < priority.length && items[priority[i]].showing) {
2811 lastIndex = Math.max(lastIndex, priority[i++]);
2812 }
2813 if (lastIndex is -1) lastIndex = firstIndex;
2814 CTabItem lastItem = items[lastIndex];
2815 int w = lastItem.x + lastItem.width + 3;
2816 if (!simple && lastIndex is selectedIndex) w += curveWidth - 2*curveIndent;
2817 chevronRect.x = Math.min(w, getRightItemEdge());
2818 chevronRect.y = onBottom ? size.y - borderBottom - tabHeight + (tabHeight - chevronRect.height)/2 : borderTop + (tabHeight - chevronRect.height)/2;
2819 }
2820 }
2821 if (oldX !is chevronRect.x || oldWidth !is chevronRect.width ||
2822 oldY !is chevronRect.y || oldHeight !is chevronRect.height) {
2823 int left = Math.min(oldX, chevronRect.x);
2824 int right = Math.max(oldX + oldWidth, chevronRect.x + chevronRect.width);
2825 int top = onBottom ? size.y - borderBottom - tabHeight: borderTop + 1;
2826 redraw(left, top, right - left, tabHeight, false);
2827 }
2828 }
2829 public override void setFont(Font font) {
2830 checkWidget();
2831 if (font !is null && font==getFont()) return;
2832 super.setFont(font);
2833 oldFont = getFont();
2834 if (!updateTabHeight(false)) {
2835 updateItems();
2836 redraw();
2837 }
2838 }
2839 public override void setForeground (Color color) {
2840 super.setForeground(color);
2841 redraw();
2842 }
2843 /**
2844 * Display an insert marker before or after the specified tab item.
2845 *
2846 * A value of null will clear the mark.
2847 *
2848 * @param item the item with which the mark is associated or null
2849 *
2850 * @param after true if the mark should be displayed after the specified item
2851 *
2852 * @exception SWTException <ul>
2853 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2854 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2855 * </ul>
2856 */
2857 public void setInsertMark(CTabItem item, bool after) {
2858 checkWidget();
2859 }
2860 /**
2861 * Display an insert marker before or after the specified tab item.
2862 *
2863 * A value of -1 will clear the mark.
2864 *
2865 * @param index the index of the item with which the mark is associated or null
2866 *
2867 * @param after true if the mark should be displayed after the specified item
2868 *
2869 * @exception IllegalArgumentException<ul>
2870 * </ul>
2871 *
2872 * @exception SWTException <ul>
2873 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2874 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2875 * </ul>
2876 */
2877 public void setInsertMark(int index, bool after) {
2878 checkWidget();
2879 if (index < -1 || index >= getItemCount()) {
2880 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
2881 }
2882 }
2883 bool setItemLocation() {
2884 bool changed = false;
2885 if (items.length is 0) return false;
2886 Point size = getSize();
2887 int y = onBottom ? Math.max(borderBottom, size.y - borderBottom - tabHeight) : borderTop;
2888 if (single) {
2889 int defaultX = getDisplay().getBounds().width + 10; // off screen
2890 for (int i = 0; i < items.length; i++) {
2891 CTabItem item = items[i];
2892 if (i is selectedIndex) {
2893 firstIndex = selectedIndex;
2894 int oldX = item.x, oldY = item.y;
2895 item.x = borderLeft;
2896 item.y = y;
2897 item.showing = true;
2898 if (showClose || item.showClose) {
2899 item.closeRect.x = borderLeft + CTabItem.LEFT_MARGIN;
2900 item.closeRect.y = onBottom ? size.y - borderBottom - tabHeight + (tabHeight - BUTTON_SIZE)/2: borderTop + (tabHeight - BUTTON_SIZE)/2;
2901 }
2902 if (item.x !is oldX || item.y !is oldY) changed = true;
2903 } else {
2904 item.x = defaultX;
2905 item.showing = false;
2906 }
2907 }
2908 } else {
2909 int rightItemEdge = getRightItemEdge();
2910 int maxWidth = rightItemEdge - borderLeft;
2911 int width = 0;
2912 for (int i = 0; i < priority.length; i++) {
2913 CTabItem item = items[priority[i]];
2914 width += item.width;
2915 item.showing = i is 0 ? true : item.width > 0 && width <= maxWidth;
2916 if (!simple && priority[i] is selectedIndex) width += curveWidth - 2*curveIndent;
2917 }
2918 int x = 0;
2919 int defaultX = getDisplay().getBounds().width + 10; // off screen
2920 firstIndex = items.length - 1;
2921 for (int i = 0; i < items.length; i++) {
2922 CTabItem item = items[i];
2923 if (!item.showing) {
2924 if (item.x !is defaultX) changed = true;
2925 item.x = defaultX;
2926 } else {
2927 firstIndex = Math.min(firstIndex, i);
2928 if (item.x !is x || item.y !is y) changed = true;
2929 item.x = x;
2930 item.y = y;
2931 if (i is selectedIndex) {
2932 int edge = Math.min(item.x + item.width, rightItemEdge);
2933 item.closeRect.x = edge - CTabItem.RIGHT_MARGIN - BUTTON_SIZE;
2934 } else {
2935 item.closeRect.x = item.x + item.width - CTabItem.RIGHT_MARGIN - BUTTON_SIZE;
2936 }
2937 item.closeRect.y = onBottom ? size.y - borderBottom - tabHeight + (tabHeight - BUTTON_SIZE)/2: borderTop + (tabHeight - BUTTON_SIZE)/2;
2938 x = x + item.width;
2939 if (!simple && i is selectedIndex) x += curveWidth - 2*curveIndent;
2940 }
2941 }
2942 }
2943 return changed;
2944 }
2945 bool setItemSize() {
2946 bool changed = false;
2947 if (isDisposed()) return changed;
2948 Point size = getSize();
2949 if (size.x <= 0 || size.y <= 0) return changed;
2950 xClient = borderLeft + marginWidth + highlight_margin;
2951 if (onBottom) {
2952 yClient = borderTop + highlight_margin + marginHeight;
2953 } else {
2954 yClient = borderTop + tabHeight + highlight_header + marginHeight;
2955 }
2956 showChevron = false;
2957 if (single) {
2958 showChevron = true;
2959 if (selectedIndex !is -1) {
2960 CTabItem tab = items[selectedIndex];
2961 GC gc = new GC(this);
2962 int width = tab.preferredWidth(gc, true, false);
2963 gc.dispose();
2964 width = Math.min(width, getRightItemEdge() - borderLeft);
2965 if (tab.height !is tabHeight || tab.width !is width) {
2966 changed = true;
2967 tab.shortenedText = null;
2968 tab.shortenedTextWidth = 0;
2969 tab.height = tabHeight;
2970 tab.width = width;
2971 tab.closeRect.width = tab.closeRect.height = 0;
2972 if (showClose || tab.showClose) {
2973 tab.closeRect.width = BUTTON_SIZE;
2974 tab.closeRect.height = BUTTON_SIZE;
2975 }
2976 }
2977 }
2978 return changed;
2979 }
2980
2981 if (items.length is 0) return changed;
2982
2983 int[] widths;
2984 GC gc = new GC(this);
2985 int tabAreaWidth = size.x - borderLeft - borderRight - 3;
2986 if (showMin) tabAreaWidth -= BUTTON_SIZE;
2987 if (showMax) tabAreaWidth -= BUTTON_SIZE;
2988 if (topRightAlignment is SWT.RIGHT && topRight !is null) {
2989 Point rightSize = topRight.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
2990 tabAreaWidth -= rightSize.x + 3;
2991 }
2992 if (!simple) tabAreaWidth -= curveWidth - 2*curveIndent;
2993 tabAreaWidth = Math.max(0, tabAreaWidth);
2994
2995 // First, try the minimum tab size at full compression.
2996 int minWidth = 0;
2997 int[] minWidths = new int[items.length];
2998 for (int i = 0; i < priority.length; i++) {
2999 int index = priority[i];
3000 minWidths[index] = items[index].preferredWidth(gc, index is selectedIndex, true);
3001 minWidth += minWidths[index];
3002 if (minWidth > tabAreaWidth) break;
3003 }
3004 if (minWidth > tabAreaWidth) {
3005 // full compression required and a chevron
3006 showChevron = items.length > 1;
3007 if (showChevron) tabAreaWidth -= 3*BUTTON_SIZE/2;
3008 widths = minWidths;
3009 int index = selectedIndex !is -1 ? selectedIndex : 0;
3010 if (tabAreaWidth < widths[index]) {
3011 widths[index] = Math.max(0, tabAreaWidth);
3012 }
3013 } else {
3014 int maxWidth = 0;
3015 int[] maxWidths = new int[items.length];
3016 for (int i = 0; i < items.length; i++) {
3017 maxWidths[i] = items[i].preferredWidth(gc, i is selectedIndex, false);
3018 maxWidth += maxWidths[i];
3019 }
3020 if (maxWidth <= tabAreaWidth) {
3021 // no compression required
3022 widths = maxWidths;
3023 } else {
3024 // determine compression for each item
3025 int extra = (tabAreaWidth - minWidth) / items.length;
3026 while (true) {
3027 int large = 0, totalWidth = 0;
3028 for (int i = 0 ; i < items.length; i++) {
3029 if (maxWidths[i] > minWidths[i] + extra) {
3030 totalWidth += minWidths[i] + extra;
3031 large++;
3032 } else {
3033 totalWidth += maxWidths[i];
3034 }
3035 }
3036 if (totalWidth >= tabAreaWidth) {
3037 extra--;
3038 break;
3039 }
3040 if (large is 0 || tabAreaWidth - totalWidth < large) break;
3041 extra++;
3042 }
3043 widths = new int[items.length];
3044 for (int i = 0; i < items.length; i++) {
3045 widths[i] = Math.min(maxWidths[i], minWidths[i] + extra);
3046 }
3047 }
3048 }
3049 gc.dispose();
3050
3051 for (int i = 0; i < items.length; i++) {
3052 CTabItem tab = items[i];
3053 int width = widths[i];
3054 if (tab.height !is tabHeight || tab.width !is width) {
3055 changed = true;
3056 tab.shortenedText = null;
3057 tab.shortenedTextWidth = 0;
3058 tab.height = tabHeight;
3059 tab.width = width;
3060 tab.closeRect.width = tab.closeRect.height = 0;
3061 if (showClose || tab.showClose) {
3062 if (i is selectedIndex || showUnselectedClose) {
3063 tab.closeRect.width = BUTTON_SIZE;
3064 tab.closeRect.height = BUTTON_SIZE;
3065 }
3066 }
3067 }
3068 }
3069 return changed;
3070 }
3071 /**
3072 * Marks the receiver's maximize button as visible if the argument is <code>true</code>,
3073 * and marks it invisible otherwise.
3074 *
3075 * @param visible the new visibility state
3076 *
3077 * @exception SWTException <ul>
3078 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3079 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3080 * </ul>
3081 *
3082 * @since 3.0
3083 */
3084 public void setMaximizeVisible(bool visible) {
3085 checkWidget();
3086 if (showMax is visible) return;
3087 // display maximize button
3088 showMax = visible;
3089 updateItems();
3090 redraw();
3091 }
3092 /**
3093 * Sets the layout which is associated with the receiver to be
3094 * the argument which may be null.
3095 * <p>
3096 * Note: No Layout can be set on this Control because it already
3097 * manages the size and position of its children.
3098 * </p>
3099 *
3100 * @param layout the receiver's new layout or null
3101 *
3102 * @exception SWTException <ul>
3103 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3104 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3105 * </ul>
3106 */
3107 public override void setLayout (Layout layout) {
3108 checkWidget();
3109 return;
3110 }
3111 /**
3112 * Sets the maximized state of the receiver.
3113 *
3114 * @param maximize the new maximized state
3115 *
3116 * @exception SWTException <ul>
3117 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3118 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3119 * </ul>
3120 *
3121 * @since 3.0
3122 */
3123 public void setMaximized(bool maximize) {
3124 checkWidget ();
3125 if (this.maximized is maximize) return;
3126 if (maximize && this.minimized) setMinimized(false);
3127 this.maximized = maximize;
3128 redraw(maxRect.x, maxRect.y, maxRect.width, maxRect.height, false);
3129 }
3130 /**
3131 * Marks the receiver's minimize button as visible if the argument is <code>true</code>,
3132 * and marks it invisible otherwise.
3133 *
3134 * @param visible the new visibility state
3135 *
3136 * @exception SWTException <ul>
3137 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3138 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3139 * </ul>
3140 *
3141 * @since 3.0
3142 */
3143 public void setMinimizeVisible(bool visible) {
3144 checkWidget();
3145 if (showMin is visible) return;
3146 // display minimize button
3147 showMin = visible;
3148 updateItems();
3149 redraw();
3150 }
3151 /**
3152 * Sets the minimized state of the receiver.
3153 *
3154 * @param minimize the new minimized state
3155 *
3156 * @exception SWTException <ul>
3157 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3158 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3159 * </ul>
3160 *
3161 * @since 3.0
3162 */
3163 public void setMinimized(bool minimize) {
3164 checkWidget ();
3165 if (this.minimized is minimize) return;
3166 if (minimize && this.maximized) setMaximized(false);
3167 this.minimized = minimize;
3168 redraw(minRect.x, minRect.y, minRect.width, minRect.height, false);
3169 }
3170
3171 /**
3172 * Sets the minimum number of characters that will
3173 * be displayed in a fully compressed tab.
3174 *
3175 * @param count the minimum number of characters that will be displayed in a fully compressed tab
3176 *
3177 * @exception SWTException <ul>
3178 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3179 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3180 * <li>ERROR_INVALID_RANGE - if the count is less than zero</li>
3181 * </ul>
3182 *
3183 * @since 3.0
3184 */
3185 public void setMinimumCharacters(int count) {
3186 checkWidget ();
3187 if (count < 0) SWT.error(SWT.ERROR_INVALID_RANGE);
3188 if (minChars is count) return;
3189 minChars = count;
3190 if (updateItems()) redrawTabs();
3191 }
3192
3193 /**
3194 * When there is not enough horizontal space to show all the tabs,
3195 * by default, tabs are shown sequentially from left to right in
3196 * order of their index. When the MRU visibility is turned on,
3197 * the tabs that are visible will be the tabs most recently selected.
3198 * Tabs will still maintain their left to right order based on index
3199 * but only the most recently selected tabs are visible.
3200 * <p>
3201 * For example, consider a CTabFolder that contains "Tab 1", "Tab 2",
3202 * "Tab 3" and "Tab 4" (in order by index). The user selects
3203 * "Tab 1" and then "Tab 3". If the CTabFolder is now
3204 * compressed so that only two tabs are visible, by default,
3205 * "Tab 2" and "Tab 3" will be shown ("Tab 3" since it is currently
3206 * selected and "Tab 2" because it is the previous item in index order).
3207 * If MRU visibility is enabled, the two visible tabs will be "Tab 1"
3208 * and "Tab 3" (in that order from left to right).</p>
3209 *
3210 * @param show the new visibility state
3211 *
3212 * @exception SWTException <ul>
3213 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3214 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3215 * </ul>
3216 *
3217 * @since 3.1
3218 */
3219 public void setMRUVisible(bool show) {
3220 checkWidget();
3221 if (mru is show) return;
3222 mru = show;
3223 if (!mru) {
3224 int idx = firstIndex;
3225 int next = 0;
3226 for (int i = firstIndex; i < items.length; i++) {
3227 priority[next++] = i;
3228 }
3229 for (int i = 0; i < idx; i++) {
3230 priority[next++] = i;
3231 }
3232 if (updateItems()) redrawTabs();
3233 }
3234 }
3235 /**
3236 * Set the selection to the tab at the specified item.
3237 *
3238 * @param item the tab item to be selected
3239 *
3240 * @exception IllegalArgumentException <ul>
3241 * <li>ERROR_NULL_ARGUMENT - if the item is null</li>
3242 * </ul>
3243 *
3244 * @exception SWTException <ul>
3245 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
3246 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
3247 * </ul>
3248 */
3249 public void setSelection(CTabItem item) {
3250 checkWidget();
3251 if (item is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
3252 int index = indexOf(item);
3253 setSelection(index);
3254 }
3255 /**
3256 * Set the selection to the tab at the specified index.
3257 *
3258 * @param index the index of the tab item to be selected
3259 *
3260 * @exception SWTException <ul>
3261 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3262 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3263 * </ul>
3264 */
3265 public void setSelection(int index) {
3266 checkWidget();
3267 if (index < 0 || index >= items.length) return;
3268 CTabItem selection = items[index];
3269 if (selectedIndex is index) {
3270 showItem(selection);
3271 return;
3272 }
3273
3274 int oldIndex = selectedIndex;
3275 selectedIndex = index;
3276 if (oldIndex !is -1) {
3277 items[oldIndex].closeImageState = NONE;
3278 }
3279 selection.closeImageState = NORMAL;
3280 selection.showing = false;
3281
3282 Control newControl = selection.control;
3283 Control oldControl = null;
3284 if (oldIndex !is -1) {
3285 oldControl = items[oldIndex].control;
3286 }
3287
3288 if (newControl !is oldControl) {
3289 if (newControl !is null && !newControl.isDisposed()) {
3290 newControl.setBounds(getClientArea());
3291 newControl.setVisible(true);
3292 }
3293 if (oldControl !is null && !oldControl.isDisposed()) {
3294 oldControl.setVisible(false);
3295 }
3296 }
3297 showItem(selection);
3298 redraw();
3299 }
3300 void setSelection(int index, bool notify) {
3301 int oldSelectedIndex = selectedIndex;
3302 setSelection(index);
3303 if (notify && selectedIndex !is oldSelectedIndex && selectedIndex !is -1) {
3304 Event event = new Event();
3305 event.item = getItem(selectedIndex);
3306 notifyListeners(SWT.Selection, event);
3307 }
3308 }
3309 /**
3310 * Sets the receiver's selection background color to the color specified
3311 * by the argument, or to the default system color for the control
3312 * if the argument is null.
3313 *
3314 * @param color the new color (or null)
3315 *
3316 * @exception IllegalArgumentException <ul>
3317 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
3318 * </ul>
3319 * @exception SWTException <ul>
3320 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3321 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3322 * </ul>
3323 *
3324 * @since 3.0
3325 */
3326 public void setSelectionBackground (Color color) {
3327 checkWidget();
3328 setSelectionHighlightGradientColor(null);
3329 if (selectionBackground is color) return;
3330 if (color is null) color = getDisplay().getSystemColor(SELECTION_BACKGROUND);
3331 selectionBackground = color;
3332 if (selectedIndex > -1) redraw();
3333 }
3334 /**
3335 * Specify a gradient of colours to be draw in the background of the selected tab.
3336 * For example to draw a gradient that varies from dark blue to blue and then to
3337 * white, use the following call to setBackground:
3338 * <pre>
3339 * cfolder.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_DARK_BLUE),
3340 * display.getSystemColor(SWT.COLOR_BLUE),
3341 * display.getSystemColor(SWT.COLOR_WHITE),
3342 * display.getSystemColor(SWT.COLOR_WHITE)},
3343 * new int[] {25, 50, 100});
3344 * </pre>
3345 *
3346 * @param colors an array of Color that specifies the colors to appear in the gradient
3347 * in order of appearance left to right. The value <code>null</code> clears the
3348 * background gradient. The value <code>null</code> can be used inside the array of
3349 * Color to specify the background color.
3350 * @param percents an array of integers between 0 and 100 specifying the percent of the width
3351 * of the widget at which the color should change. The size of the percents array must be one
3352 * less than the size of the colors array.
3353 *
3354 * @exception SWTException <ul>
3355 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
3356 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
3357 * </ul>
3358 */
3359 public void setSelectionBackground(Color[] colors, int[] percents) {
3360 setSelectionBackground(colors, percents, false);
3361 }
3362 /**
3363 * Specify a gradient of colours to be draw in the background of the selected tab.
3364 * For example to draw a vertical gradient that varies from dark blue to blue and then to
3365 * white, use the following call to setBackground:
3366 * <pre>
3367 * cfolder.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_DARK_BLUE),
3368 * display.getSystemColor(SWT.COLOR_BLUE),
3369 * display.getSystemColor(SWT.COLOR_WHITE),
3370 * display.getSystemColor(SWT.COLOR_WHITE)},
3371 * new int[] {25, 50, 100}, true);
3372 * </pre>
3373 *
3374 * @param colors an array of Color that specifies the colors to appear in the gradient
3375 * in order of appearance left to right. The value <code>null</code> clears the
3376 * background gradient. The value <code>null</code> can be used inside the array of
3377 * Color to specify the background color.
3378 * @param percents an array of integers between 0 and 100 specifying the percent of the width
3379 * of the widget at which the color should change. The size of the percents array must be one
3380 * less than the size of the colors array.
3381 *
3382 * @param vertical indicate the direction of the gradient. True is vertical and false is horizontal.
3383 *
3384 * @exception SWTException <ul>
3385 * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
3386 * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
3387 * </ul>
3388 *
3389 * @since 3.0
3390 */
3391 public void setSelectionBackground(Color[] colors, int[] percents, bool vertical) {
3392 checkWidget();
3393 int colorsLength;
3394 Color highlightBeginColor = null; //null is no highlight
3395
3396 if (colors !is null) {
3397 //The colors array can optionally have an extra entry which describes the highlight top color
3398 //Thus its either one or two larger than the percents array
3399 if (percents is null ||
3400 ! ((percents.length is colors.length - 1) || (percents.length is colors.length - 2))){
3401 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3402 }
3403 for (int i = 0; i < percents.length; i++) {
3404 if (percents[i] < 0 || percents[i] > 100) {
3405 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3406 }
3407 if (i > 0 && percents[i] < percents[i-1]) {
3408 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3409 }
3410 }
3411 //If the colors is exactly two more than percents then last is highlight
3412 //Keep track of *real* colorsLength (minus the highlight)
3413 if(percents.length is colors.length - 2) {
3414 highlightBeginColor = colors[colors.length - 1];
3415 colorsLength = colors.length - 1;
3416 } else {
3417 colorsLength = colors.length;
3418 }
3419 if (getDisplay().getDepth() < 15) {
3420 // Don't use gradients on low color displays
3421 colors = [colors[colorsLength - 1]];
3422 colorsLength = colors.length;
3423 percents = null;
3424 }
3425 } else {
3426 colorsLength = 0;
3427 }
3428
3429 // Are these settings the same as before?
3430 if (selectionBgImage is null) {
3431 if ((selectionGradientColors !is null) && (colors !is null) &&
3432 (selectionGradientColors.length is colorsLength)) {
3433 bool same = false;
3434 for (int i = 0; i < selectionGradientColors.length; i++) {
3435 if (selectionGradientColors[i] is null) {
3436 same = colors[i] is null;
3437 } else {
3438 same = cast(bool)(selectionGradientColors[i]==colors[i]);
3439 }
3440 if (!same) break;
3441 }
3442 if (same) {
3443 for (int i = 0; i < selectionGradientPercents.length; i++) {
3444 same = selectionGradientPercents[i] is percents[i];
3445 if (!same) break;
3446 }
3447 }
3448 if (same && this.selectionGradientVertical is vertical) return;
3449 }
3450 } else {
3451 selectionBgImage = null;
3452 }
3453 // Store the new settings
3454 if (colors is null) {
3455 selectionGradientColors = null;
3456 selectionGradientPercents = null;
3457 selectionGradientVertical = false;
3458 setSelectionBackground(cast(Color)null);
3459 setSelectionHighlightGradientColor(null);
3460 } else {
3461 selectionGradientColors = new Color[colorsLength];
3462 for (int i = 0; i < colorsLength; ++i) {
3463 selectionGradientColors[i] = colors[i];
3464 }
3465 selectionGradientPercents = new int[percents.length];
3466 for (int i = 0; i < percents.length; ++i) {
3467 selectionGradientPercents[i] = percents[i];
3468 }
3469 selectionGradientVertical = vertical;
3470 setSelectionBackground(selectionGradientColors[selectionGradientColors.length-1]);
3471 setSelectionHighlightGradientColor(highlightBeginColor);
3472 }
3473
3474 // Refresh with the new settings
3475 if (selectedIndex > -1) redraw();
3476 }
3477
3478 /*
3479 * Set the color for the highlight start for selected tabs.
3480 * Update the cache of highlight gradient colors if required.
3481 */
3482
3483 void setSelectionHighlightGradientColor(Color start) {
3484 //Set to null to match all the early return cases.
3485 //For early returns, don't realloc the cache, we may get a cache hit next time we're given the highlight
3486 selectionHighlightGradientBegin = null;
3487
3488 if(start is null)
3489 return;
3490
3491 //don't bother on low colour
3492 if (getDisplay().getDepth() < 15)
3493 return;
3494
3495 //don't bother if we don't have a background gradient
3496 if(selectionGradientColors.length < 2)
3497 return;
3498
3499 //OK we know its a valid gradient now
3500 selectionHighlightGradientBegin = start;
3501
3502 if(! isSelectionHighlightColorsCacheHit(start))
3503 createSelectionHighlightGradientColors(start); //if no cache hit then compute new ones
3504 }
3505
3506 /*
3507 * Return true if given start color, the cache of highlight colors we have
3508 * would match the highlight colors we'd compute.
3509 */
3510 bool isSelectionHighlightColorsCacheHit(Color start) {
3511
3512 if(selectionHighlightGradientColorsCache is null)
3513 return false;
3514
3515 //this case should never happen but check to be safe before accessing array indexes
3516 if(selectionHighlightGradientColorsCache.length < 2)
3517 return false;
3518
3519 Color highlightBegin = selectionHighlightGradientColorsCache[0];
3520 Color highlightEnd = selectionHighlightGradientColorsCache[selectionHighlightGradientColorsCache.length - 1];
3521
3522 if( highlightBegin!=start)
3523 return false;
3524
3525 //Compare number of colours we have vs. we'd compute
3526 if(selectionHighlightGradientColorsCache.length !is tabHeight)
3527 return false;
3528
3529 //Compare existing highlight end to what it would be (selectionBackground)
3530 if( highlightEnd!=selectionBackground)
3531 return false;
3532
3533 return true;
3534 }
3535
3536 /**
3537 * Set the image to be drawn in the background of the selected tab. Image
3538 * is stretched or compressed to cover entire selection tab area.
3539 *
3540 * @param image the image to be drawn in the background
3541 *
3542 * @exception SWTException <ul>
3543 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3544 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3545 * </ul>
3546 */
3547 public void setSelectionBackground(Image image) {
3548 checkWidget();
3549 setSelectionHighlightGradientColor(null);
3550 if (image is selectionBgImage) return;
3551 if (image !is null) {
3552 selectionGradientColors = null;
3553 selectionGradientPercents = null;
3554 disposeSelectionHighlightGradientColors();
3555 }
3556 selectionBgImage = image;
3557 if (selectedIndex > -1) redraw();
3558 }
3559 /**
3560 * Set the foreground color of the selected tab.
3561 *
3562 * @param color the color of the text displayed in the selected tab
3563 *
3564 * @exception SWTException <ul>
3565 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3566 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3567 * </ul>
3568 */
3569 public void setSelectionForeground (Color color) {
3570 checkWidget();
3571 if (selectionForeground is color) return;
3572 if (color is null) color = getDisplay().getSystemColor(SELECTION_FOREGROUND);
3573 selectionForeground = color;
3574 if (selectedIndex > -1) redraw();
3575 }
3576
3577 /*
3578 * Allocate colors for the highlight line.
3579 * Colours will be a gradual blend ranging from to.
3580 * Blend length will be tab height.
3581 * Recompute this if tab height changes.
3582 * Could remain null if there'd be no gradient (start=end or low colour display)
3583 */
3584 void createSelectionHighlightGradientColors(Color start) {
3585 disposeSelectionHighlightGradientColors(); //dispose if existing
3586
3587 if(start is null) //shouldn't happen but just to be safe
3588 return;
3589
3590 //alloc colours for entire height to ensure it matches wherever we stop drawing
3591 int fadeGradientSize = tabHeight;
3592
3593 RGB from = start.getRGB();
3594 RGB to = selectionBackground.getRGB();
3595
3596 selectionHighlightGradientColorsCache = new Color[fadeGradientSize];
3597 int denom = fadeGradientSize - 1;
3598
3599 for (int i = 0; i < fadeGradientSize; i++) {
3600 int propFrom = denom - i;
3601 int propTo = i;
3602 int red = (to.red * propTo + from.red * propFrom) / denom;
3603 int green = (to.green * propTo + from.green * propFrom) / denom;
3604 int blue = (to.blue * propTo + from.blue * propFrom) / denom;
3605 selectionHighlightGradientColorsCache[i] = new Color(getDisplay(), red, green, blue);
3606 }
3607 }
3608
3609 void disposeSelectionHighlightGradientColors() {
3610 if(selectionHighlightGradientColorsCache is null)
3611 return;
3612 for (int i = 0; i < selectionHighlightGradientColorsCache.length; i++) {
3613 selectionHighlightGradientColorsCache[i].dispose();
3614 }
3615 selectionHighlightGradientColorsCache = null;
3616 }
3617
3618 /*
3619 * Return the gradient start color for selected tabs, which is the start of the tab fade
3620 * (end is selectionBackground).
3621 */
3622 Color getSelectionBackgroundGradientBegin() {
3623 if (selectionGradientColors is null)
3624 return getSelectionBackground();
3625 if (selectionGradientColors.length is 0)
3626 return getSelectionBackground();
3627 return selectionGradientColors[0];
3628 }
3629
3630 /**
3631 * Sets the shape that the CTabFolder will use to render itself.
3632 *
3633 * @param simple <code>true</code> if the CTabFolder should render itself in a simple, traditional style
3634 *
3635 * @exception SWTException <ul>
3636 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3637 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3638 * </ul>
3639 *
3640 * @since 3.0
3641 */
3642 public void setSimple(bool simple) {
3643 checkWidget();
3644 if (this.simple !is simple) {
3645 this.simple = simple;
3646 Rectangle rectBefore = getClientArea();
3647 updateItems();
3648 Rectangle rectAfter = getClientArea();
3649 if (rectBefore!=rectAfter) {
3650 notifyListeners(SWT.Resize, new Event());
3651 }
3652 redraw();
3653 }
3654 }
3655 /**
3656 * Sets the number of tabs that the CTabFolder should display
3657 *
3658 * @param single <code>true</code> if only the selected tab should be displayed otherwise, multiple tabs will be shown.
3659 *
3660 * @exception SWTException <ul>
3661 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3662 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3663 * </ul>
3664 *
3665 * @since 3.0
3666 */
3667 public void setSingle(bool single) {
3668 checkWidget();
3669 if (this.single !is single) {
3670 this.single = single;
3671 if (!single) {
3672 for (int i = 0; i < items.length; i++) {
3673 if (i !is selectedIndex && items[i].closeImageState is NORMAL) {
3674 items[i].closeImageState = NONE;
3675 }
3676 }
3677 }
3678 Rectangle rectBefore = getClientArea();
3679 updateItems();
3680 Rectangle rectAfter = getClientArea();
3681 if (rectBefore!=rectAfter) {
3682 notifyListeners(SWT.Resize, new Event());
3683 }
3684 redraw();
3685 }
3686 }
3687 /**
3688 * Specify a fixed height for the tab items. If no height is specified,
3689 * the default height is the height of the text or the image, whichever
3690 * is greater. Specifying a height of -1 will revert to the default height.
3691 *
3692 * @param height the pixel value of the height or -1
3693 *
3694 * @exception SWTException <ul>
3695 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3696 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3697 * <li>ERROR_INVALID_ARGUMENT - if called with a height of less than 0</li>
3698 * </ul>
3699 */
3700 public void setTabHeight(int height) {
3701 checkWidget();
3702 if (height < -1) {
3703 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3704 }
3705 fixedTabHeight = height;
3706 updateTabHeight(false);
3707 }
3708 /**
3709 * Specify whether the tabs should appear along the top of the folder
3710 * or along the bottom of the folder.
3711 *
3712 * @param position <code>SWT.TOP</code> for tabs along the top or <code>SWT.BOTTOM</code> for tabs along the bottom
3713 *
3714 * @exception SWTException <ul>
3715 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3716 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3717 * <li>ERROR_INVALID_ARGUMENT - if the position value is not either SWT.TOP or SWT.BOTTOM</li>
3718 * </ul>
3719 *
3720 * @since 3.0
3721 */
3722 public void setTabPosition(int position) {
3723 checkWidget();
3724 if (position !is SWT.TOP && position !is SWT.BOTTOM) {
3725 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3726 }
3727 if (onBottom !is (position is SWT.BOTTOM)) {
3728 onBottom = position is SWT.BOTTOM;
3729 borderTop = onBottom ? borderLeft : 0;
3730 borderBottom = onBottom ? 0 : borderRight;
3731 updateTabHeight(true);
3732 Rectangle rectBefore = getClientArea();
3733 updateItems();
3734 Rectangle rectAfter = getClientArea();
3735 if (rectBefore!=rectAfter) {
3736 notifyListeners(SWT.Resize, new Event());
3737 }
3738 redraw();
3739 }
3740 }
3741 /**
3742 * Set the control that appears in the top right corner of the tab folder.
3743 * Typically this is a close button or a composite with a Menu and close button.
3744 * The topRight control is optional. Setting the top right control to null will
3745 * remove it from the tab folder.
3746 *
3747 * @param control the control to be displayed in the top right corner or null
3748 *
3749 * @exception SWTException <ul>
3750 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3751 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3752 * <li>ERROR_INVALID_ARGUMENT - if the control is not a child of this CTabFolder</li>
3753 * </ul>
3754 *
3755 * @since 2.1
3756 */
3757 public void setTopRight(Control control) {
3758 setTopRight(control, SWT.RIGHT);
3759 }
3760 /**
3761 * Set the control that appears in the top right corner of the tab folder.
3762 * Typically this is a close button or a composite with a Menu and close button.
3763 * The topRight control is optional. Setting the top right control to null
3764 * will remove it from the tab folder.
3765 * <p>
3766 * The alignment parameter sets the layout of the control in the tab area.
3767 * <code>SWT.RIGHT</code> will cause the control to be positioned on the far
3768 * right of the folder and it will have its default size. <code>SWT.FILL</code>
3769 * will size the control to fill all the available space to the right of the
3770 * last tab. If there is no available space, the control will not be visible.
3771 * </p>
3772 *
3773 * @param control the control to be displayed in the top right corner or null
3774 * @param alignment <code>SWT.RIGHT</code> or <code>SWT.FILL</code>
3775 *
3776 * @exception SWTException <ul>
3777 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3778 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3779 * <li>ERROR_INVALID_ARGUMENT - if the control is not a child of this CTabFolder</li>
3780 * </ul>
3781 *
3782 * @since 3.0
3783 */
3784 public void setTopRight(Control control, int alignment) {
3785 checkWidget();
3786 if (alignment !is SWT.RIGHT && alignment !is SWT.FILL) {
3787 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3788 }
3789 if (control !is null && control.getParent() !is this) {
3790 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3791 }
3792 topRight = control;
3793 topRightAlignment = alignment;
3794 if (updateItems()) redraw();
3795 }
3796 /**
3797 * Specify whether the close button appears
3798 * when the user hovers over an unselected tabs.
3799 *
3800 * @param visible <code>true</code> makes the close button appear
3801 *
3802 * @exception SWTException <ul>
3803 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3804 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3805 * </ul>
3806 *
3807 * @since 3.0
3808 */
3809 public void setUnselectedCloseVisible(bool visible) {
3810 checkWidget();
3811 if (showUnselectedClose is visible) return;
3812 // display close button when mouse hovers
3813 showUnselectedClose = visible;
3814 updateItems();
3815 redraw();
3816 }
3817 /**
3818 * Specify whether the image appears on unselected tabs.
3819 *
3820 * @param visible <code>true</code> makes the image appear
3821 *
3822 * @exception SWTException <ul>
3823 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3824 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3825 * </ul>
3826 *
3827 * @since 3.0
3828 */
3829 public void setUnselectedImageVisible(bool visible) {
3830 checkWidget();
3831 if (showUnselectedImage is visible) return;
3832 // display image on unselected items
3833 showUnselectedImage = visible;
3834 updateItems();
3835 redraw();
3836 }
3837 /**
3838 * Shows the item. If the item is already showing in the receiver,
3839 * this method simply returns. Otherwise, the items are scrolled until
3840 * the item is visible.
3841 *
3842 * @param item the item to be shown
3843 *
3844 * @exception IllegalArgumentException <ul>
3845 * <li>ERROR_NULL_ARGUMENT - if the item is null</li>
3846 * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</li>
3847 * </ul>
3848 * @exception SWTException <ul>
3849 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3850 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3851 * </ul>
3852 *
3853 * @see CTabFolder#showSelection()
3854 *
3855 * @since 2.0
3856 */
3857 public void showItem (CTabItem item) {
3858 checkWidget();
3859 if (item is null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
3860 if (item.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3861 int index = indexOf(item);
3862 if (index is -1) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3863 int idx = -1;
3864 for (int i = 0; i < priority.length; i++) {
3865 if (priority[i] is index) {
3866 idx = i;
3867 break;
3868 }
3869 }
3870 if (mru) {
3871 // move to front of mru order
3872 int[] newPriority = new int[priority.length];
3873 System.arraycopy(priority, 0, newPriority, 1, idx);
3874 System.arraycopy(priority, idx+1, newPriority, idx+1, priority.length - idx - 1);
3875 newPriority[0] = index;
3876 priority = newPriority;
3877 }
3878 if (item.isShowing()) return;
3879 updateItems(index);
3880 redrawTabs();
3881 }
3882 void showList (Rectangle rect) {
3883 if (items.length is 0 || !showChevron) return;
3884 if (showMenu is null || showMenu.isDisposed()) {
3885 showMenu = new Menu(this);
3886 } else {
3887 MenuItem[] items = showMenu.getItems();
3888 for (int i = 0; i < items.length; i++) {
3889 items[i].dispose();
3890 }
3891 }
3892 static const String id = "CTabFolder_showList_Index"; //$NON-NLS-1$
3893 for (int i = 0; i < items.length; i++) {
3894 CTabItem tab = items[i];
3895 if (tab.showing) continue;
3896 MenuItem item = new MenuItem(showMenu, SWT.NONE);
3897 item.setText(tab.getText());
3898 item.setImage(tab.getImage());
3899 item.setData(id, tab);
3900 item.addSelectionListener(new class() SelectionAdapter {
3901 public void widgetSelected(SelectionEvent e) {
3902 MenuItem menuItem = cast(MenuItem)e.widget;
3903 int index = indexOf(cast(CTabItem)menuItem.getData(id));
3904 this.outer.setSelection(index, true);
3905 }
3906 });
3907 }
3908 int x = rect.x;
3909 int y = rect.y + rect.height;
3910 Point location = getDisplay().map(this, null, x, y);
3911 showMenu.setLocation(location.x, location.y);
3912 showMenu.setVisible(true);
3913 }
3914 /**
3915 * Shows the selection. If the selection is already showing in the receiver,
3916 * this method simply returns. Otherwise, the items are scrolled until
3917 * the selection is visible.
3918 *
3919 * @exception SWTException <ul>
3920 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3921 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3922 * </ul>
3923 *
3924 * @see CTabFolder#showItem(CTabItem)
3925 *
3926 * @since 2.0
3927 */
3928 public void showSelection () {
3929 checkWidget ();
3930 if (selectedIndex !is -1) {
3931 showItem(getSelection());
3932 }
3933 }
3934
3935 void _setToolTipText (int x, int y) {
3936 String oldTip = getToolTipText();
3937 String newTip = _getToolTip(x, y);
3938 if (newTip is null || newTip!=oldTip) {
3939 setToolTipText(newTip);
3940 }
3941 }
3942
3943 bool updateItems() {
3944 return updateItems(selectedIndex);
3945 }
3946
3947 bool updateItems(int showIndex) {
3948 if (!single && !mru && showIndex !is -1) {
3949 // make sure selected item will be showing
3950 int firstIndex = showIndex;
3951 if (priority[0] < showIndex) {
3952 int maxWidth = getRightItemEdge() - borderLeft;
3953 if (!simple) maxWidth -= curveWidth - 2*curveIndent;
3954 int width = 0;
3955 int[] widths = new int[items.length];
3956 GC gc = new GC(this);
3957 for (int i = priority[0]; i <= showIndex; i++) {
3958 widths[i] = items[i].preferredWidth(gc, i is selectedIndex, true);
3959 width += widths[i];
3960 if (width > maxWidth) break;
3961 }
3962 if (width > maxWidth) {
3963 width = 0;
3964 for (int i = showIndex; i >= 0; i--) {
3965 if (widths[i] is 0) widths[i] = items[i].preferredWidth(gc, i is selectedIndex, true);
3966 width += widths[i];
3967 if (width > maxWidth) break;
3968 firstIndex = i;
3969 }
3970 } else {
3971 firstIndex = priority[0];
3972 for (int i = showIndex + 1; i < items.length; i++) {
3973 widths[i] = items[i].preferredWidth(gc, i is selectedIndex, true);
3974 width += widths[i];
3975 if (width >= maxWidth) break;
3976 }
3977 if (width < maxWidth) {
3978 for (int i = priority[0] - 1; i >= 0; i--) {
3979 if (widths[i] is 0) widths[i] = items[i].preferredWidth(gc, i is selectedIndex, true);
3980 width += widths[i];
3981 if (width > maxWidth) break;
3982 firstIndex = i;
3983 }
3984 }
3985 }
3986 gc.dispose();
3987 }
3988 if (firstIndex !is priority[0]) {
3989 int index = 0;
3990 for (int i = firstIndex; i < items.length; i++) {
3991 priority[index++] = i;
3992 }
3993 for (int i = 0; i < firstIndex; i++) {
3994 priority[index++] = i;
3995 }
3996 }
3997 }
3998
3999 bool oldShowChevron = showChevron;
4000 bool changed = setItemSize();
4001 changed |= setItemLocation();
4002 setButtonBounds();
4003 changed |= showChevron !is oldShowChevron;
4004 if (changed && getToolTipText() !is null) {
4005 Point pt = getDisplay().getCursorLocation();
4006 pt = toControl(pt);
4007 _setToolTipText(pt.x, pt.y);
4008 }
4009 return changed;
4010 }
4011 bool updateTabHeight(bool force){
4012 int style = getStyle();
4013 if (fixedTabHeight is 0 && (style & SWT.FLAT) !is 0 && (style & SWT.BORDER) is 0) highlight_header = 0;
4014 int oldHeight = tabHeight;
4015 if (fixedTabHeight !is SWT.DEFAULT) {
4016 tabHeight = fixedTabHeight is 0 ? 0 : fixedTabHeight + 1; // +1 for line drawn across top of tab
4017 } else {
4018 int tempHeight = 0;
4019 GC gc = new GC(this);
4020 if (items.length is 0) {
4021 tempHeight = gc.textExtent("Default", CTabItem.FLAGS).y + CTabItem.TOP_MARGIN + CTabItem.BOTTOM_MARGIN; //$NON-NLS-1$
4022 } else {
4023 for (int i=0; i < items.length; i++) {
4024 tempHeight = Math.max(tempHeight, items[i].preferredHeight(gc));
4025 }
4026 }
4027 gc.dispose();
4028 tabHeight = tempHeight;
4029 }
4030 if (!force && tabHeight is oldHeight) return false;
4031
4032 oldSize = null;
4033 if (onBottom) {
4034 int d = tabHeight - 12;
4035 curve = [0,13+d, 0,12+d, 2,12+d, 3,11+d, 5,11+d, 6,10+d, 7,10+d, 9,8+d, 10,8+d,
4036 11,7+d, 11+d,7,
4037 12+d,6, 13+d,6, 15+d,4, 16+d,4, 17+d,3, 19+d,3, 20+d,2, 22+d,2, 23+d,1];
4038 curveWidth = 26+d;
4039 curveIndent = curveWidth/3;
4040 } else {
4041 int d = tabHeight - 12;
4042 curve = [0,0, 0,1, 2,1, 3,2, 5,2, 6,3, 7,3, 9,5, 10,5,
4043 11,6, 11+d,6+d,
4044 12+d,7+d, 13+d,7+d, 15+d,9+d, 16+d,9+d, 17+d,10+d, 19+d,10+d, 20+d,11+d, 22+d,11+d, 23+d,12+d];
4045 curveWidth = 26+d;
4046 curveIndent = curveWidth/3;
4047
4048 //this could be static but since values depend on curve, better to keep in one place
4049 topCurveHighlightStart = [
4050 0, 2, 1, 2, 2, 2,
4051 3, 3, 4, 3, 5, 3,
4052 6, 4, 7, 4,
4053 8, 5,
4054 9, 6, 10, 6];
4055
4056 //also, by adding in 'd' here we save some math cost when drawing the curve
4057 topCurveHighlightEnd = [
4058 10+d, 6+d,
4059 11+d, 7+d,
4060 12+d, 8+d, 13+d, 8+d,
4061 14+d, 9+d,
4062 15+d, 10+d, 16+d, 10+d,
4063 17+d, 11+d, 18+d, 11+d, 19+d, 11+d,
4064 20+d, 12+d, 21+d, 12+d, 22+d, 12+d ];
4065 }
4066 notifyListeners(SWT.Resize, new Event());
4067 return true;
4068 }
4069 String _getToolTip(int x, int y) {
4070 if (showMin && minRect.contains(x, y)) return minimized ? SWT.getMessage("SWT_Restore") : SWT.getMessage("SWT_Minimize"); //$NON-NLS-1$ //$NON-NLS-2$
4071 if (showMax && maxRect.contains(x, y)) return maximized ? SWT.getMessage("SWT_Restore") : SWT.getMessage("SWT_Maximize"); //$NON-NLS-1$ //$NON-NLS-2$
4072 if (showChevron && chevronRect.contains(x, y)) return SWT.getMessage("SWT_ShowList"); //$NON-NLS-1$
4073 CTabItem item = getItem(new Point (x, y));
4074 if (item is null) return null;
4075 if (!item.showing) return null;
4076 if ((showClose || item.showClose) && item.closeRect.contains(x, y)) {
4077 return SWT.getMessage("SWT_Close"); //$NON-NLS-1$
4078 }
4079 return item.getToolTipText();
4080 }
4081 }