comparison dwt/custom/CTabItem.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children e831403a80a9 f565d3a95c0a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *
11 * Port to the D Programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com>
13 *******************************************************************************/
14 module dwt.custom;
15
16 import dwt.DWT;
17 import dwt.DWTException;
18 import dwt.custom.CTabFolder;
19 import dwt.graphics.Color;
20 import dwt.graphics.Font;
21 import dwt.graphics.GC;
22 import dwt.graphics.Image;
23 import dwt.graphics.Point;
24 import dwt.graphics.RGB;
25 import dwt.graphics.Rectangle;
26 import dwt.graphics.TextLayout;
27 import dwt.widgets.Control;
28 import dwt.widgets.Display;
29 import dwt.widgets.Item;
30 import dwt.widgets.Widget;
31
32 import dwt.dwthelper.utils;
33
34 /**
35 * Instances of this class represent a selectable user interface object
36 * that represent a page in a notebook widget.
37 *
38 * <dl>
39 * <dt><b>Styles:</b></dt>
40 * <dd>DWT.CLOSE</dd>
41 * <dt><b>Events:</b></dt>
42 * <dd>(none)</dd>
43 * </dl>
44 * <p>
45 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
46 * </p>
47 */
48 public class CTabItem : Item {
49 CTabFolder parent;
50 int x, y, width, height = 0;
51 Control control; // the tab page
52
53 String toolTipText;
54 String shortenedText;
55 int shortenedTextWidth;
56
57 // Appearance
58 Font font;
59 Image disabledImage;
60
61 Rectangle closeRect = new Rectangle(0, 0, 0, 0);
62 int closeImageState = CTabFolder.NONE;
63 bool showClose = false;
64 bool showing = false;
65
66 // internal constants
67 static final int TOP_MARGIN = 2;
68 static final int BOTTOM_MARGIN = 2;
69 static final int LEFT_MARGIN = 4;
70 static final int RIGHT_MARGIN = 4;
71 static final int INTERNAL_SPACING = 4;
72 static final int FLAGS = DWT.DRAW_TRANSPARENT | DWT.DRAW_MNEMONIC;
73 static final String ELLIPSIS = "..."; //$NON-NLS-1$ // could use the ellipsis glyph on some platforms "\u2026"
74
75 /**
76 * Constructs a new instance of this class given its parent
77 * (which must be a <code>CTabFolder</code>) and a style value
78 * describing its behavior and appearance. The item is added
79 * to the end of the items maintained by its parent.
80 * <p>
81 * The style value is either one of the style constants defined in
82 * class <code>DWT</code> which is applicable to instances of this
83 * class, or must be built by <em>bitwise OR</em>'ing together
84 * (that is, using the <code>int</code> "|" operator) two or more
85 * of those <code>DWT</code> style constants. The class description
86 * lists the style constants that are applicable to the class.
87 * Style bits are also inherited from superclasses.
88 * </p>
89 *
90 * @param parent a CTabFolder which will be the parent of the new instance (cannot be null)
91 * @param style the style of control to construct
92 *
93 * @exception IllegalArgumentException <ul>
94 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
95 * </ul>
96 * @exception DWTException <ul>
97 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
98 * </ul>
99 *
100 * @see DWT
101 * @see Widget#getStyle()
102 */
103 public this (CTabFolder parent, int style) {
104 this(parent, style, parent.getItemCount());
105 }
106
107 /**
108 * Constructs a new instance of this class given its parent
109 * (which must be a <code>CTabFolder</code>), a style value
110 * describing its behavior and appearance, and the index
111 * at which to place it in the items maintained by its parent.
112 * <p>
113 * The style value is either one of the style constants defined in
114 * class <code>DWT</code> which is applicable to instances of this
115 * class, or must be built by <em>bitwise OR</em>'ing together
116 * (that is, using the <code>int</code> "|" operator) two or more
117 * of those <code>DWT</code> style constants. The class description
118 * lists the style constants that are applicable to the class.
119 * Style bits are also inherited from superclasses.
120 * </p>
121 *
122 * @param parent a CTabFolder which will be the parent of the new instance (cannot be null)
123 * @param style the style of control to construct
124 * @param index the zero-relative index to store the receiver in its parent
125 *
126 * @exception IllegalArgumentException <ul>
127 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
128 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the parent (inclusive)</li>
129 * </ul>
130 * @exception DWTException <ul>
131 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
132 * </ul>
133 *
134 * @see DWT
135 * @see Widget#getStyle()
136 */
137 public this (CTabFolder parent, int style, int index) {
138 super(parent, style);
139 showClose = (style & DWT.CLOSE) !is 0;
140 parent.createItem(this, index);
141 }
142
143 /*
144 * Return whether to use ellipses or just truncate labels
145 */
146 bool useEllipses () {
147 return parent.simple;
148 }
149
150 String shortenText (GC gc, String text, int width) {
151 return useEllipses() ? shortenText(gc, text, width, ELLIPSIS) : shortenText(gc, text, width, ""); //$NON-NLS-1$
152 }
153
154 String shortenText (GC gc, String text, int width, String ellipses) {
155 if (gc.textExtent(text, FLAGS).x <= width)
156 return text;
157 int ellipseWidth = gc.textExtent(ellipses, FLAGS).x;
158 int length = text.length;
159 TextLayout layout = new TextLayout(getDisplay());
160 layout.setText(text);
161 int end = layout.getPreviousOffset(length, DWT.MOVEMENT_CLUSTER);
162 while (end > 0) {
163 text = text.substring(0, end);
164 int l = gc.textExtent(text, FLAGS).x;
165 if (l + ellipseWidth <= width) {
166 break;
167 }
168 end = layout.getPreviousOffset(end, DWT.MOVEMENT_CLUSTER);
169 }
170 layout.dispose();
171 return end is 0 ? text.substring(0, 1) : text + ellipses;
172 }
173
174 public void dispose () {
175 if (isDisposed())
176 return;
177 //if (!isValidThread ()) error (DWT.ERROR_THREAD_INVALID_ACCESS);
178 parent.destroyItem(this);
179 super.dispose();
180 parent = null;
181 control = null;
182 toolTipText = null;
183 shortenedText = null;
184 font = null;
185 }
186
187 void drawClose (GC gc) {
188 if (closeRect.width is 0 || closeRect.height is 0)
189 return;
190 Display display = getDisplay();
191
192 // draw X 9x9
193 int indent = Math.max(1, (CTabFolder.BUTTON_SIZE - 9) / 2);
194 int x = closeRect.x + indent;
195 int y = closeRect.y + indent;
196 y += parent.onBottom ? -1 : 1;
197
198 Color closeBorder = display.getSystemColor(CTabFolder.BUTTON_BORDER);
199 switch (closeImageState) {
200 case CTabFolder.NORMAL: {
201 int[] shape = new int[]
202 {
203 x , y , x + 2 , y , x + 4 , y + 2 , x + 5 , y + 2 , x + 7 , y , x + 9 , y , x + 9 , y + 2 , x + 7 , y + 4 , x + 7 , y + 5 , x + 9 , y + 7 , x + 9 , y + 9 , x + 7 , y + 9 , x + 5 , y + 7 , x + 4 , y + 7 , x + 2 , y + 9 , x , y + 9 , x , y + 7 , x + 2 , y + 5 , x + 2 , y + 4 , x , y + 2
204 };
205 gc.setBackground(display.getSystemColor(CTabFolder.BUTTON_FILL));
206 gc.fillPolygon(shape);
207 gc.setForeground(closeBorder);
208 gc.drawPolygon(shape);
209 break;
210 }
211 case CTabFolder.HOT: {
212 int[] shape = new int[]
213 {
214 x , y , x + 2 , y , x + 4 , y + 2 , x + 5 , y + 2 , x + 7 , y , x + 9 , y , x + 9 , y + 2 , x + 7 , y + 4 , x + 7 , y + 5 , x + 9 , y + 7 , x + 9 , y + 9 , x + 7 , y + 9 , x + 5 , y + 7 , x + 4 , y + 7 , x + 2 , y + 9 , x , y + 9 , x , y + 7 , x + 2 , y + 5 , x + 2 , y + 4 , x , y + 2
215 };
216 Color fill = new Color(display, CTabFolder.CLOSE_FILL);
217 gc.setBackground(fill);
218 gc.fillPolygon(shape);
219 fill.dispose();
220 gc.setForeground(closeBorder);
221 gc.drawPolygon(shape);
222 break;
223 }
224 case CTabFolder.SELECTED: {
225 int[] shape = new int[]
226 {
227 x + 1 , y + 1 , x + 3 , y + 1 , x + 5 , y + 3 , x + 6 , y + 3 , x + 8 , y + 1 , x + 10 , y + 1 , x + 10 , y + 3 , x + 8 , y + 5 , x + 8 , y + 6 , x + 10 , y + 8 , x + 10 , y + 10 , x + 8 , y + 10 , x + 6 , y + 8 , x + 5 , y + 8 , x + 3 , y + 10 , x + 1 , y + 10 , x + 1 , y + 8 , x + 3 , y + 6 , x + 3 , y + 5 , x + 1 , y + 3
228 };
229 Color fill = new Color(display, CTabFolder.CLOSE_FILL);
230 gc.setBackground(fill);
231 gc.fillPolygon(shape);
232 fill.dispose();
233 gc.setForeground(closeBorder);
234 gc.drawPolygon(shape);
235 break;
236 }
237 case CTabFolder.NONE: {
238 int[] shape = new int[]
239 {
240 x , y , x + 10 , y , x + 10 , y + 10 , x , y + 10
241 };
242 if (parent.gradientColors !is null && !parent.gradientVertical) {
243 parent.drawBackground(gc, shape, false);
244 }
245 else {
246 Color defaultBackground = parent.getBackground();
247 Image image = parent.bgImage;
248 Color[] colors = parent.gradientColors;
249 int[] percents = parent.gradientPercents;
250 bool vertical = parent.gradientVertical;
251 parent.drawBackground(gc, shape, x, y, 10, 10, defaultBackground, image, colors, percents, vertical);
252 }
253 break;
254 }
255 }
256 }
257
258 void drawSelected (GC gc) {
259 Point size = parent.getSize();
260 int rightEdge = Math.min(x + width, parent.getRightItemEdge());
261
262 // Draw selection border across all tabs
263 int xx = parent.borderLeft;
264 int
265 yy = parent.onBottom ? size.y - parent.borderBottom - parent.tabHeight - parent.highlight_header : parent.borderTop + parent.tabHeight + 1;
266 int ww = size.x - parent.borderLeft - parent.borderRight;
267 int hh = parent.highlight_header - 1;
268 int[] shape = new int[]
269 {
270 xx , yy , xx + ww , yy , xx + ww , yy + hh , xx , yy + hh
271 };
272 if (parent.selectionGradientColors !is null && !parent.selectionGradientVertical) {
273 parent.drawBackground(gc, shape, true);
274 }
275 else {
276 gc.setBackground(parent.selectionBackground);
277 gc.fillRectangle(xx, yy, ww, hh);
278 }
279
280 if (parent.single) {
281 if (!showing)
282 return;
283 }
284 else {
285 // if selected tab scrolled out of view or partially out of view
286 // just draw bottom line
287 if (!showing) {
288 int x1 = Math.max(0, parent.borderLeft - 1);
289 int y1 = (parent.onBottom) ? y - 1 : y + height;
290 int x2 = size.x - parent.borderRight;
291 gc.setForeground(CTabFolder.borderColor);
292 gc.drawLine(x1, y1, x2, y1);
293 return;
294 }
295
296 // draw selected tab background and outline
297 shape = null;
298 if (this.parent.onBottom) {
299 int[] left = parent.simple ? CTabFolder.SIMPLE_BOTTOM_LEFT_CORNER : CTabFolder.BOTTOM_LEFT_CORNER;
300 int[] right = parent.simple ? CTabFolder.SIMPLE_BOTTOM_RIGHT_CORNER : parent.curve;
301 if (parent.borderLeft is 0 && parent.indexOf(this) is parent.firstIndex) {
302 left = new int[]
303 {
304 x , y + height
305 };
306 }
307 shape = new int[left.length + right.length + 8];
308 int index = 0;
309 shape[index++] = x; // first point repeated here because below we reuse shape to draw outline
310 shape[index++] = y - 1;
311 shape[index++] = x;
312 shape[index++] = y - 1;
313 for (int i = 0; i < left.length / 2; i++) {
314 shape[index++] = x + left[2 * i];
315 shape[index++] = y + height + left[2 * i + 1] - 1;
316 }
317 for (int i = 0; i < right.length / 2; i++) {
318 shape[index++] = parent.simple ? rightEdge - 1 + right[2 * i] : rightEdge - parent.curveIndent + right[2 * i];
319 shape[index++] = parent.simple ? y + height + right[2 * i + 1] - 1 : y + right[2 * i + 1] - 2;
320 }
321 shape[index++] = parent.simple ? rightEdge - 1 : rightEdge + parent.curveWidth - parent.curveIndent;
322 shape[index++] = y - 1;
323 shape[index++] = parent.simple ? rightEdge - 1 : rightEdge + parent.curveWidth - parent.curveIndent;
324 shape[index++] = y - 1;
325 }
326 else {
327 int[] left = parent.simple ? CTabFolder.SIMPLE_TOP_LEFT_CORNER : CTabFolder.TOP_LEFT_CORNER;
328 int[] right = parent.simple ? CTabFolder.SIMPLE_TOP_RIGHT_CORNER : parent.curve;
329 if (parent.borderLeft is 0 && parent.indexOf(this) is parent.firstIndex) {
330 left = new int[]
331 {
332 x , y
333 };
334 }
335 shape = new int[left.length + right.length + 8];
336 int index = 0;
337 shape[index++] = x; // first point repeated here because below we reuse shape to draw outline
338 shape[index++] = y + height + 1;
339 shape[index++] = x;
340 shape[index++] = y + height + 1;
341 for (int i = 0; i < left.length / 2; i++) {
342 shape[index++] = x + left[2 * i];
343 shape[index++] = y + left[2 * i + 1];
344 }
345 for (int i = 0; i < right.length / 2; i++) {
346 shape[index++] = parent.simple ? rightEdge - 1 + right[2 * i] : rightEdge - parent.curveIndent + right[2 * i];
347 shape[index++] = y + right[2 * i + 1];
348 }
349 shape[index++] = parent.simple ? rightEdge - 1 : rightEdge + parent.curveWidth - parent.curveIndent;
350 shape[index++] = y + height + 1;
351 shape[index++] = parent.simple ? rightEdge - 1 : rightEdge + parent.curveWidth - parent.curveIndent;
352 shape[index++] = y + height + 1;
353 }
354
355 Rectangle clipping = gc.getClipping();
356 Rectangle bounds = getBounds();
357 bounds.height += 1;
358 if (parent.onBottom)
359 bounds.y -= 1;
360 bool tabInPaint = clipping.intersects(bounds);
361
362 if (tabInPaint) {
363 // fill in tab background
364 if (parent.selectionGradientColors !is null && !parent.selectionGradientVertical) {
365 parent.drawBackground(gc, shape, true);
366 }
367 else {
368 Color defaultBackground = parent.selectionBackground;
369 Image image = parent.selectionBgImage;
370 Color[] colors = parent.selectionGradientColors;
371 int[] percents = parent.selectionGradientPercents;
372 bool vertical = parent.selectionGradientVertical;
373 xx = x;
374 yy = parent.onBottom ? y - 1 : y + 1;
375 ww = width;
376 hh = height;
377 if (!parent.single && !parent.simple)
378 ww += parent.curveWidth - parent.curveIndent;
379 parent.drawBackground(gc, shape, xx, yy, ww, hh, defaultBackground, image, colors, percents, vertical);
380 }
381 }
382
383 //Highlight MUST be drawn before the outline so that outline can cover it in the right spots (start of swoop)
384 //otherwise the curve looks jagged
385 drawHighlight(gc, rightEdge);
386
387 // draw outline
388 shape[0] = Math.max(0, parent.borderLeft - 1);
389 if (parent.borderLeft is 0 && parent.indexOf(this) is parent.firstIndex) {
390 shape[1] = parent.onBottom ? y + height - 1 : y;
391 shape[5] = shape[3] = shape[1];
392 }
393 shape[shape.length - 2] = size.x - parent.borderRight + 1;
394 for (int i = 0; i < shape.length / 2; i++) {
395 if (shape[2 * i + 1] is y + height + 1)
396 shape[2 * i + 1] -= 1;
397 }
398 RGB inside = parent.selectionBackground.getRGB();
399 if (parent.selectionBgImage !is null || (parent.selectionGradientColors !is null && parent.selectionGradientColors.length > 1)) {
400 inside = null;
401 }
402 RGB outside = parent.getBackground().getRGB();
403 if (parent.bgImage !is null || (parent.gradientColors !is null && parent.gradientColors.length > 1)) {
404 outside = null;
405 }
406 parent.antialias(shape, CTabFolder.borderColor.getRGB(), inside, outside, gc);
407 gc.setForeground(CTabFolder.borderColor);
408 gc.drawPolyline(shape);
409
410 if (!tabInPaint)
411 return;
412 }
413
414 // draw Image
415 int xDraw = x + LEFT_MARGIN;
416 if (parent.single && (parent.showClose || showClose))
417 xDraw += CTabFolder.BUTTON_SIZE;
418 Image image = getImage();
419 if (image !is null) {
420 Rectangle imageBounds = image.getBounds();
421 // only draw image if it won't overlap with close button
422 int maxImageWidth = rightEdge - xDraw - RIGHT_MARGIN;
423 if (!parent.single && closeRect.width > 0)
424 maxImageWidth -= closeRect.width + INTERNAL_SPACING;
425 if (imageBounds.width < maxImageWidth) {
426 int imageX = xDraw;
427 int imageY = y + (height - imageBounds.height) / 2;
428 imageY += parent.onBottom ? -1 : 1;
429 gc.drawImage(image, imageX, imageY);
430 xDraw += imageBounds.width + INTERNAL_SPACING;
431 }
432 }
433
434 // draw Text
435 int textWidth = rightEdge - xDraw - RIGHT_MARGIN;
436 if (!parent.single && closeRect.width > 0)
437 textWidth -= closeRect.width + INTERNAL_SPACING;
438 if (textWidth > 0) {
439 Font gcFont = gc.getFont();
440 gc.setFont(font is null ? parent.getFont() : font);
441
442 if (shortenedText is null || shortenedTextWidth !is textWidth) {
443 shortenedText = shortenText(gc, getText(), textWidth);
444 shortenedTextWidth = textWidth;
445 }
446 Point extent = gc.textExtent(shortenedText, FLAGS);
447 int textY = y + (height - extent.y) / 2;
448 textY += parent.onBottom ? -1 : 1;
449
450 gc.setForeground(parent.selectionForeground);
451 gc.drawText(shortenedText, xDraw, textY, FLAGS);
452 gc.setFont(gcFont);
453
454 // draw a Focus rectangle
455 if (parent.isFocusControl()) {
456 Display display = getDisplay();
457 if (parent.simple || parent.single) {
458 gc.setBackground(display.getSystemColor(DWT.COLOR_BLACK));
459 gc.setForeground(display.getSystemColor(DWT.COLOR_WHITE));
460 gc.drawFocus(xDraw - 1, textY - 1, extent.x + 2, extent.y + 2);
461 }
462 else {
463 gc.setForeground(display.getSystemColor(CTabFolder.BUTTON_BORDER));
464 gc.drawLine(xDraw, textY + extent.y + 1, xDraw + extent.x + 1, textY + extent.y + 1);
465 }
466 }
467 }
468 if (parent.showClose || showClose)
469 drawClose(gc);
470 }
471
472 /*
473 * Draw a highlight effect along the left, top, and right edges of the tab.
474 * Only for curved tabs, on top.
475 * Do not draw if insufficient colors.
476 */
477 void drawHighlight (GC gc, int rightEdge) {
478 //only draw for curvy tabs and only draw for top tabs
479 if (parent.simple || this.parent.onBottom)
480 return;
481
482 if (parent.selectionHighlightGradientBegin is null)
483 return;
484
485 Color[] gradients = parent.selectionHighlightGradientColorsCache;
486 if (gradients is null)
487 return;
488 int gradientsSize = gradients.length;
489 if (gradientsSize is 0)
490 return; //shouldn't happen but just to be tidy
491
492 gc.setForeground(gradients[0]);
493
494 //draw top horizontal line
495 gc.drawLine(CTabFolder.TOP_LEFT_CORNER_HILITE[0] + x + 1, //rely on fact that first pair is top/right of curve
496 1 + y, rightEdge - parent.curveIndent, 1 + y);
497
498 int[] leftHighlightCurve = CTabFolder.TOP_LEFT_CORNER_HILITE;
499
500 int d = parent.tabHeight - parent.topCurveHighlightEnd.length / 2;
501
502 int lastX = 0;
503 int lastY = 0;
504 int lastColorIndex = 0;
505
506 //draw upper left curve highlight
507 for (int i = 0; i < leftHighlightCurve.length / 2; i++) {
508 int rawX = leftHighlightCurve[i * 2];
509 int rawY = leftHighlightCurve[i * 2 + 1];
510 lastX = rawX + x;
511 lastY = rawY + y;
512 lastColorIndex = rawY - 1;
513 gc.setForeground(gradients[lastColorIndex]);
514 gc.drawPoint(lastX, lastY);
515 }
516 //draw left vertical line highlight
517 for (int i = lastColorIndex; i < gradientsSize; i++) {
518 gc.setForeground(gradients[i]);
519 gc.drawPoint(lastX, 1 + lastY++);
520 }
521
522 int rightEdgeOffset = rightEdge - parent.curveIndent;
523
524 //draw right swoop highlight up to diagonal portion
525 for (int i = 0; i < parent.topCurveHighlightStart.length / 2; i++) {
526 int rawX = parent.topCurveHighlightStart[i * 2];
527 int rawY = parent.topCurveHighlightStart[i * 2 + 1];
528 lastX = rawX + rightEdgeOffset;
529 lastY = rawY + y;
530 lastColorIndex = rawY - 1;
531 if (lastColorIndex >= gradientsSize)
532 break; //can happen if tabs are unusually short and cut off the curve
533 gc.setForeground(gradients[lastColorIndex]);
534 gc.drawPoint(lastX, lastY);
535 }
536 //draw right diagonal line highlight
537 for (int i = lastColorIndex; i < lastColorIndex + d; i++) {
538 if (i >= gradientsSize)
539 break; //can happen if tabs are unusually short and cut off the curve
540 gc.setForeground(gradients[i]);
541 gc.drawPoint(1 + lastX++, 1 + lastY++);
542 }
543
544 //draw right swoop highlight from diagonal portion to end
545 for (int i = 0; i < parent.topCurveHighlightEnd.length / 2; i++) {
546 int rawX = parent.topCurveHighlightEnd[i * 2]; //d is already encoded in this value
547 int rawY = parent.topCurveHighlightEnd[i * 2 + 1]; //d already encoded
548 lastX = rawX + rightEdgeOffset;
549 lastY = rawY + y;
550 lastColorIndex = rawY - 1;
551 if (lastColorIndex >= gradientsSize)
552 break; //can happen if tabs are unusually short and cut off the curve
553 gc.setForeground(gradients[lastColorIndex]);
554 gc.drawPoint(lastX, lastY);
555 }
556 }
557
558 /*
559 * Draw the unselected border for the receiver on the right.
560 *
561 * @param gc
562 */
563 void drawRightUnselectedBorder (GC gc) {
564
565 int[] shape = null;
566 int startX = x + width - 1;
567
568 if (this.parent.onBottom) {
569 int[] right = parent.simple ? CTabFolder.SIMPLE_UNSELECTED_INNER_CORNER : CTabFolder.BOTTOM_RIGHT_CORNER;
570
571 shape = new int[right.length + 2];
572 int index = 0;
573
574 for (int i = 0; i < right.length / 2; i++) {
575 shape[index++] = startX + right[2 * i];
576 shape[index++] = y + height + right[2 * i + 1] - 1;
577 }
578 shape[index++] = startX;
579 shape[index++] = y - 1;
580 }
581 else {
582 int[] right = parent.simple ? CTabFolder.SIMPLE_UNSELECTED_INNER_CORNER : CTabFolder.TOP_RIGHT_CORNER;
583
584 shape = new int[right.length + 2];
585 int index = 0;
586
587 for (int i = 0; i < right.length / 2; i++) {
588 shape[index++] = startX + right[2 * i];
589 shape[index++] = y + right[2 * i + 1];
590 }
591
592 shape[index++] = startX;
593 shape[index++] = y + height;
594
595 }
596
597 drawBorder(gc, shape);
598
599 }
600
601 /*
602 * Draw the border of the tab
603 *
604 * @param gc
605 * @param shape
606 */
607 void drawBorder (GC gc, int[] shape) {
608
609 gc.setForeground(CTabFolder.borderColor);
610 gc.drawPolyline(shape);
611 }
612
613 /*
614 * Draw the unselected border for the receiver on the left.
615 *
616 * @param gc
617 */
618 void drawLeftUnselectedBorder (GC gc) {
619
620 int[] shape = null;
621 if (this.parent.onBottom) {
622 int[] left = parent.simple ? CTabFolder.SIMPLE_UNSELECTED_INNER_CORNER : CTabFolder.BOTTOM_LEFT_CORNER;
623
624 shape = new int[left.length + 2];
625 int index = 0;
626 shape[index++] = x;
627 shape[index++] = y - 1;
628 for (int i = 0; i < left.length / 2; i++) {
629 shape[index++] = x + left[2 * i];
630 shape[index++] = y + height + left[2 * i + 1] - 1;
631 }
632 }
633 else {
634 int[] left = parent.simple ? CTabFolder.SIMPLE_UNSELECTED_INNER_CORNER : CTabFolder.TOP_LEFT_CORNER;
635
636 shape = new int[left.length + 2];
637 int index = 0;
638 shape[index++] = x;
639 shape[index++] = y + height;
640 for (int i = 0; i < left.length / 2; i++) {
641 shape[index++] = x + left[2 * i];
642 shape[index++] = y + left[2 * i + 1];
643 }
644
645 }
646
647 drawBorder(gc, shape);
648 }
649
650 void drawUnselected (GC gc) {
651 // Do not draw partial items
652 if (!showing)
653 return;
654
655 Rectangle clipping = gc.getClipping();
656 Rectangle bounds = getBounds();
657 if (!clipping.intersects(bounds))
658 return;
659
660 // draw border
661 int index = parent.indexOf(this);
662
663 if (index > 0 && index < parent.selectedIndex)
664 drawLeftUnselectedBorder(gc);
665 // If it is the last one then draw a line
666 if (index > parent.selectedIndex)
667 drawRightUnselectedBorder(gc);
668
669 // draw Image
670 int xDraw = x + LEFT_MARGIN;
671 Image image = getImage();
672 if (image !is null && parent.showUnselectedImage) {
673 Rectangle imageBounds = image.getBounds();
674 // only draw image if it won't overlap with close button
675 int maxImageWidth = x + width - xDraw - RIGHT_MARGIN;
676 if (parent.showUnselectedClose && (parent.showClose || showClose)) {
677 maxImageWidth -= closeRect.width + INTERNAL_SPACING;
678 }
679 if (imageBounds.width < maxImageWidth) {
680 int imageX = xDraw;
681 int imageHeight = imageBounds.height;
682 int imageY = y + (height - imageHeight) / 2;
683 imageY += parent.onBottom ? -1 : 1;
684 int imageWidth = imageBounds.width * imageHeight / imageBounds.height;
685 gc.drawImage(image, imageBounds.x, imageBounds.y, imageBounds.width, imageBounds.height, imageX, imageY, imageWidth, imageHeight);
686 xDraw += imageWidth + INTERNAL_SPACING;
687 }
688 }
689 // draw Text
690 int textWidth = x + width - xDraw - RIGHT_MARGIN;
691 if (parent.showUnselectedClose && (parent.showClose || showClose)) {
692 textWidth -= closeRect.width + INTERNAL_SPACING;
693 }
694 if (textWidth > 0) {
695 Font gcFont = gc.getFont();
696 gc.setFont(font is null ? parent.getFont() : font);
697 if (shortenedText is null || shortenedTextWidth !is textWidth) {
698 shortenedText = shortenText(gc, getText(), textWidth);
699 shortenedTextWidth = textWidth;
700 }
701 Point extent = gc.textExtent(shortenedText, FLAGS);
702 int textY = y + (height - extent.y) / 2;
703 textY += parent.onBottom ? -1 : 1;
704 gc.setForeground(parent.getForeground());
705 gc.drawText(shortenedText, xDraw, textY, FLAGS);
706 gc.setFont(gcFont);
707 }
708 // draw close
709 if (parent.showUnselectedClose && (parent.showClose || showClose))
710 drawClose(gc);
711 }
712
713 /**
714 * Returns a rectangle describing the receiver's size and location
715 * relative to its parent.
716 *
717 * @return the receiver's bounding column rectangle
718 *
719 * @exception DWTException <ul>
720 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
721 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
722 * </ul>
723 */
724 public Rectangle getBounds () {
725 //checkWidget();
726 int w = width;
727 if (!parent.simple && !parent.single && parent.indexOf(this) is parent.selectedIndex)
728 w += parent.curveWidth - parent.curveIndent;
729 return new Rectangle(x, y, w, height);
730 }
731
732 /**
733 * Gets the control that is displayed in the content area of the tab item.
734 *
735 * @return the control
736 *
737 * @exception DWTException <ul>
738 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
739 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
740 * </ul>
741 */
742 public Control getControl () {
743 checkWidget();
744 return control;
745 }
746
747 /**
748 * Get the image displayed in the tab if the tab is disabled.
749 *
750 * @return the disabled image or null
751 *
752 * @exception DWTException <ul>
753 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
754 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
755 * </ul>
756 *
757 * @deprecated the disabled image is not used
758 */
759 public Image getDisabledImage () {
760 checkWidget();
761 return disabledImage;
762 }
763
764 /**
765 * Returns the font that the receiver will use to paint textual information.
766 *
767 * @return the receiver's font
768 *
769 * @exception DWTException <ul>
770 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
771 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
772 * </ul>
773 *
774 * @since 3.0
775 */
776 public Font getFont () {
777 checkWidget();
778 if (font !is null)
779 return font;
780 return parent.getFont();
781 }
782
783 /**
784 * Returns the receiver's parent, which must be a <code>CTabFolder</code>.
785 *
786 * @return the receiver's parent
787 *
788 * @exception DWTException <ul>
789 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
790 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
791 * </ul>
792 */
793 public CTabFolder getParent () {
794 //checkWidget();
795 return parent;
796 }
797
798 /**
799 * Returns <code>true</code> to indicate that the receiver's close button should be shown.
800 * Otherwise return <code>false</code>. The initial value is defined by the style (DWT.CLOSE)
801 * that was used to create the receiver.
802 *
803 * @return <code>true</code> if the close button should be shown
804 *
805 * @exception DWTException <ul>
806 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
807 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
808 * </ul>
809 *
810 * @since 3.4
811 */
812 public bool getShowClose () {
813 checkWidget();
814 return showClose;
815 }
816
817 /**
818 * Returns the receiver's tool tip text, or null if it has
819 * not been set.
820 *
821 * @return the receiver's tool tip text
822 *
823 * @exception DWTException <ul>
824 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
825 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
826 * </ul>
827 */
828 public String getToolTipText () {
829 checkWidget();
830 if (toolTipText is null && shortenedText !is null) {
831 String text = getText();
832 if (!shortenedText.opEquals(text))
833 return text;
834 }
835 return toolTipText;
836 }
837
838 /**
839 * Returns <code>true</code> if the item will be rendered in the visible area of the CTabFolder. Returns false otherwise.
840 *
841 * @return <code>true</code> if the item will be rendered in the visible area of the CTabFolder. Returns false otherwise.
842 *
843 * @exception DWTException <ul>
844 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
845 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
846 * </ul>
847 *
848 * @since 3.0
849 */
850 public bool isShowing () {
851 checkWidget();
852 return showing;
853 }
854
855 void onPaint (GC gc, bool isSelected) {
856 if (width is 0 || height is 0)
857 return;
858 if (isSelected) {
859 drawSelected(gc);
860 }
861 else {
862 drawUnselected(gc);
863 }
864 }
865
866 int preferredHeight (GC gc) {
867 Image image = getImage();
868 int h = (image is null) ? 0 : image.getBounds().height;
869 String text = getText();
870 if (font is null) {
871 h = Math.max(h, gc.textExtent(text, FLAGS).y);
872 }
873 else {
874 Font gcFont = gc.getFont();
875 gc.setFont(font);
876 h = Math.max(h, gc.textExtent(text, FLAGS).y);
877 gc.setFont(gcFont);
878 }
879 return h + TOP_MARGIN + BOTTOM_MARGIN;
880 }
881
882 int preferredWidth (GC gc, bool isSelected, bool minimum) {
883 // NOTE: preferred width does not include the "dead space" caused
884 // by the curve.
885 if (isDisposed())
886 return 0;
887 int w = 0;
888 Image image = getImage();
889 if (image !is null && (isSelected || parent.showUnselectedImage)) {
890 w += image.getBounds().width;
891 }
892 String text = null;
893 if (minimum) {
894 int minChars = parent.minChars;
895 text = minChars is 0 ? null : getText();
896 if (text !is null && text.length() > minChars) {
897 if (useEllipses()) {
898 int end = minChars < ELLIPSIS.length() + 1 ? minChars : minChars - ELLIPSIS.length();
899 text = text.substring(0, end);
900 if (minChars > ELLIPSIS.length() + 1)
901 text += ELLIPSIS;
902 }
903 else {
904 int end = minChars;
905 text = text.substring(0, end);
906 }
907 }
908 }
909 else {
910 text = getText();
911 }
912 if (text !is null) {
913 if (w > 0)
914 w += INTERNAL_SPACING;
915 if (font is null) {
916 w += gc.textExtent(text, FLAGS).x;
917 }
918 else {
919 Font gcFont = gc.getFont();
920 gc.setFont(font);
921 w += gc.textExtent(text, FLAGS).x;
922 gc.setFont(gcFont);
923 }
924 }
925 if (parent.showClose || showClose) {
926 if (isSelected || parent.showUnselectedClose) {
927 if (w > 0)
928 w += INTERNAL_SPACING;
929 w += CTabFolder.BUTTON_SIZE;
930 }
931 }
932 return w + LEFT_MARGIN + RIGHT_MARGIN;
933 }
934
935 /**
936 * Sets the control that is used to fill the client area of
937 * the tab folder when the user selects the tab item.
938 *
939 * @param control the new control (or null)
940 *
941 * @exception IllegalArgumentException <ul>
942 * <li>ERROR_INVALID_ARGUMENT - if the control has been disposed</li>
943 * <li>ERROR_INVALID_PARENT - if the control is not in the same widget tree</li>
944 * </ul>
945 * @exception DWTException <ul>
946 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
947 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
948 * </ul>
949 */
950 public void setControl (Control control) {
951 checkWidget();
952 if (control !is null) {
953 if (control.isDisposed())
954 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
955 if (control.getParent() !is parent)
956 DWT.error(DWT.ERROR_INVALID_PARENT);
957 }
958 if (this.control !is null && !this.control.isDisposed()) {
959 this.control.setVisible(false);
960 }
961 this.control = control;
962 if (this.control !is null) {
963 int index = parent.indexOf(this);
964 if (index is parent.getSelectionIndex()) {
965 this.control.setBounds(parent.getClientArea());
966 this.control.setVisible(true);
967 }
968 else {
969 this.control.setVisible(false);
970 }
971 }
972 }
973
974 /**
975 * Sets the image that is displayed if the tab item is disabled.
976 * Null will clear the image.
977 *
978 * @param image the image to be displayed when the item is disabled or null
979 *
980 * @exception DWTException <ul>
981 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
982 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
983 * </ul>
984 *
985 * @deprecated This image is not used
986 */
987 public void setDisabledImage (Image image) {
988 checkWidget();
989 if (image !is null && image.isDisposed()) {
990 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
991 }
992 this.disabledImage = image;
993 }
994
995 /**
996 * Sets the font that the receiver will use to paint textual information
997 * for this item to the font specified by the argument, or to the default font
998 * for that kind of control if the argument is null.
999 *
1000 * @param font the new font (or null)
1001 *
1002 * @exception IllegalArgumentException <ul>
1003 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
1004 * </ul>
1005 * @exception DWTException <ul>
1006 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1007 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1008 * </ul>
1009 *
1010 * @since 3.0
1011 */
1012 public void setFont (Font font) {
1013 checkWidget();
1014 if (font !is null && font.isDisposed()) {
1015 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
1016 }
1017 if (font is null && this.font is null)
1018 return;
1019 if (font !is null && font.opEquals(this.font))
1020 return;
1021 this.font = font;
1022 if (!parent.updateTabHeight(false)) {
1023 parent.updateItems();
1024 parent.redrawTabs();
1025 }
1026 }
1027
1028 public void setImage (Image image) {
1029 checkWidget();
1030 if (image !is null && image.isDisposed()) {
1031 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
1032 }
1033 Image oldImage = getImage();
1034 if (image is null && oldImage is null)
1035 return;
1036 if (image !is null && image.opEquals(oldImage))
1037 return;
1038 super.setImage(image);
1039 if (!parent.updateTabHeight(false)) {
1040 // If image is the same size as before,
1041 // redraw only the image
1042 if (oldImage !is null && image !is null) {
1043 Rectangle oldBounds = oldImage.getBounds();
1044 Rectangle bounds = image.getBounds();
1045 if (bounds.width is oldBounds.width && bounds.height is oldBounds.height) {
1046 if (showing) {
1047 bool selected = parent.indexOf(this) is parent.selectedIndex;
1048 if (selected || parent.showUnselectedImage) {
1049 int imageX = x + LEFT_MARGIN, maxImageWidth;
1050 if (selected) {
1051 if (parent.single && (parent.showClose || showClose))
1052 imageX += CTabFolder.BUTTON_SIZE;
1053 int rightEdge = Math.min(x + width, parent.getRightItemEdge());
1054 maxImageWidth = rightEdge - imageX - RIGHT_MARGIN;
1055 if (!parent.single && closeRect.width > 0)
1056 maxImageWidth -= closeRect.width + INTERNAL_SPACING;
1057 }
1058 else {
1059 maxImageWidth = x + width - imageX - RIGHT_MARGIN;
1060 if (parent.showUnselectedClose && (parent.showClose || showClose)) {
1061 maxImageWidth -= closeRect.width + INTERNAL_SPACING;
1062 }
1063 }
1064 if (bounds.width < maxImageWidth) {
1065 int imageY = y + (height - bounds.height) / 2 + (parent.onBottom ? -1 : 1);
1066 parent.redraw(imageX, imageY, bounds.width, bounds.height, false);
1067 }
1068 }
1069 }
1070 return;
1071 }
1072 }
1073 parent.updateItems();
1074 parent.redrawTabs();
1075 }
1076 }
1077
1078 /**
1079 * Sets to <code>true</code> to indicate that the receiver's close button should be shown.
1080 * If the parent (CTabFolder) was created with DWT.CLOSE style, changing this value has
1081 * no effect.
1082 *
1083 * @param close the new state of the close button
1084 *
1085 * @exception DWTException <ul>
1086 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1087 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1088 * </ul>
1089 *
1090 * @since 3.4
1091 */
1092 public void setShowClose (bool close) {
1093 checkWidget();
1094 if (showClose is close)
1095 return;
1096 showClose = close;
1097 parent.updateItems();
1098 parent.redrawTabs();
1099 }
1100
1101 public void setText (String String) {
1102 checkWidget();
1103 if (String is null)
1104 DWT.error(DWT.ERROR_NULL_ARGUMENT);
1105 if (String.opEquals(getText()))
1106 return;
1107 super.setText(String);
1108 shortenedText = null;
1109 shortenedTextWidth = 0;
1110 if (!parent.updateTabHeight(false)) {
1111 parent.updateItems();
1112 parent.redrawTabs();
1113 }
1114 }
1115
1116 /**
1117 * Sets the receiver's tool tip text to the argument, which
1118 * may be null indicating that no tool tip text should be shown.
1119 *
1120 * @param String the new tool tip text (or null)
1121 *
1122 * @exception DWTException <ul>
1123 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1124 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1125 * </ul>
1126 */
1127 public void setToolTipText (String String) {
1128 checkWidget();
1129 toolTipText = String;
1130 }
1131
1132 }